< prev index next >

test/java/net/URLClassLoader/closetest/GetResourceAsStream.java

Print this page

        

*** 23,110 **** /** * @test * @bug 6899919 * @library /lib/testlibrary ! * @build jdk.testlibrary.FileUtils ! * @run shell build2.sh * @run main/othervm GetResourceAsStream */ ! import java.io.*; ! import java.net.*; public class GetResourceAsStream extends Common { ! /* * We simply test various scenarios with class/resource files * and make sure the files can be deleted after closing * the loader. Therefore, the test will only really be verified * on Windows. It will still run correctly on other platforms */ ! public static void main (String args[]) throws Exception { ! String workdir = System.getProperty("test.classes"); ! if (workdir == null) { ! workdir = args[0]; ! } ! ! /* the jar we copy for each test */ ! File srcfile = new File (workdir, "foo.jar"); ! /* the jar we use for the test */ ! File testfile = new File (workdir, "test.jar"); ! copyFile (srcfile, testfile); ! test (testfile, false, false); ! copyFile (srcfile, testfile); ! test (testfile, true, false); ! ! copyFile (srcfile, testfile); ! test (testfile, true, true); // repeat test using a directory of files ! File testdir= new File (workdir, "testdir"); ! File srcdir= new File (workdir, "test3"); ! copyDir (srcdir, testdir); ! test (testdir, true, false); } // create a loader on jarfile (or directory) // load a class , then look for a resource // then close the loader // check further new classes/resources cannot be loaded // check jar (or dir) can be deleted ! static void test (File file, boolean loadclass, boolean readall) ! throws Exception ! { ! URL[] urls = new URL[] {file.toURL()}; ! System.out.println ("Doing tests with URL: " + urls[0]); ! URLClassLoader loader = new URLClassLoader (urls); if (loadclass) { ! Class testclass = loadClass ("com.foo.TestClass", loader, true); } ! InputStream s = loader.getResourceAsStream ("hello.txt"); s.read(); if (readall) { while (s.read() != -1) ; s.close(); } ! loader.close (); // shouuld not find bye.txt now InputStream s1 = loader.getResourceAsStream("bye.txt"); if (s1 != null) { ! throw new RuntimeException ("closed loader returned resource"); } // now check we can delete the path ! rm_minus_rf (file); ! System.out.println (" ... OK"); } } --- 23,121 ---- /** * @test * @bug 6899919 * @library /lib/testlibrary ! * @build jdk.testlibrary.FileUtils CompilerUtils JarUtils * @run main/othervm GetResourceAsStream */ ! import java.io.File; ! import java.io.IOException; ! import java.io.InputStream; ! import java.net.URL; ! import java.net.URLClassLoader; ! import java.nio.file.Path; ! import java.nio.file.Paths; ! import java.nio.file.StandardOpenOption; ! import static java.nio.file.Files.write; public class GetResourceAsStream extends Common { ! public static final Path TEST_SRC = Paths.get(System ! .getProperty("test.src")); ! public static final Path TEST_WORK_DIR = Paths.get(System ! .getProperty("user.dir")); ! public static final Path TEST3 = TEST_SRC.resolve("test1"); ! public static final Path TEST3_DIR = TEST_WORK_DIR.resolve("test3"); ! public static final Path TEST_DIR = TEST_WORK_DIR.resolve("testDir"); ! public static final Path FOO_JAR = TEST_WORK_DIR.resolve("foo.jar"); ! public static final Path TEST_JAR = TEST_WORK_DIR.resolve("test.jar"); ! /* * We simply test various scenarios with class/resource files * and make sure the files can be deleted after closing * the loader. Therefore, the test will only really be verified * on Windows. It will still run correctly on other platforms */ ! public static void main(String args[]) throws Exception { ! setUpTest(); ! copyFile(FOO_JAR.toFile(), TEST_JAR.toFile()); ! test(TEST_JAR.toFile(), false, false); ! copyFile(FOO_JAR.toFile(), TEST_JAR.toFile()); ! test(TEST_JAR.toFile(), true, false); ! copyFile(FOO_JAR.toFile(), TEST_JAR.toFile()); ! test(TEST_JAR.toFile(), true, true); // repeat test using a directory of files + copyDir(TEST3_DIR.toRealPath().toFile(), TEST_DIR.toFile()); + test(TEST3_DIR.toFile(), true, false); ! } ! static void setUpTest() throws IOException { ! CompilerUtils.compile(TEST3, TEST3_DIR); ! write(TEST3_DIR.resolve("hello.txt"), "Hello World".getBytes("UTF-8"), ! StandardOpenOption.CREATE); ! write(TEST3_DIR.resolve("Bye.txt"), "Bye world".getBytes("UTF-8"), ! StandardOpenOption.CREATE); ! JarUtils.createJarFile(FOO_JAR, TEST3_DIR); } // create a loader on jarfile (or directory) // load a class , then look for a resource // then close the loader // check further new classes/resources cannot be loaded // check jar (or dir) can be deleted ! static void test(File file, boolean loadclass, boolean readall) ! throws Exception { ! URL[] urls = new URL[]{file.toURL()}; ! System.out.println("Doing tests with URL: " + urls[0]); ! URLClassLoader loader = new URLClassLoader(urls); if (loadclass) { ! Class testclass = loadClass("com.foo.TestClass", loader, true); } ! InputStream s = loader.getResourceAsStream("hello.txt"); s.read(); if (readall) { while (s.read() != -1) ; s.close(); } ! loader.close(); // shouuld not find bye.txt now InputStream s1 = loader.getResourceAsStream("bye.txt"); if (s1 != null) { ! throw new RuntimeException("closed loader returned resource"); } // now check we can delete the path ! rm_minus_rf(file); ! System.out.println(" ... OK"); } }
< prev index next >