23 |
23 |
24 /** |
24 /** |
25 * @test |
25 * @test |
26 * @bug 6899919 |
26 * @bug 6899919 |
27 * @library /lib/testlibrary |
27 * @library /lib/testlibrary |
28 * @build jdk.testlibrary.FileUtils |
28 * @modules jdk.compiler |
29 * @run shell build2.sh |
29 * @build jdk.testlibrary.FileUtils JarUtils CompilerUtils |
30 * @run main/othervm GetResourceAsStream |
30 * @run main/othervm GetResourceAsStream |
31 */ |
31 */ |
32 |
32 |
33 import java.io.*; |
33 import java.io.File; |
34 import java.net.*; |
34 import java.io.IOException; |
|
35 import java.io.InputStream; |
|
36 import java.net.URL; |
|
37 import java.net.URLClassLoader; |
|
38 import java.nio.file.Files; |
|
39 import java.nio.file.Path; |
|
40 import java.nio.file.Paths; |
|
41 import java.nio.file.StandardCopyOption; |
|
42 import java.nio.file.StandardOpenOption; |
35 |
43 |
36 public class GetResourceAsStream extends Common { |
44 public class GetResourceAsStream extends Common { |
|
45 private static final String WORK_DIR = System.getProperty("user.dir"); |
37 |
46 |
38 /* |
47 /* |
39 * We simply test various scenarios with class/resource files |
48 * We simply test various scenarios with class/resource files |
40 * and make sure the files can be deleted after closing |
49 * and make sure the files can be deleted after closing |
41 * the loader. Therefore, the test will only really be verified |
50 * the loader. Therefore, the test will only really be verified |
42 * on Windows. It will still run correctly on other platforms |
51 * on Windows. It will still run correctly on other platforms |
43 */ |
52 */ |
44 public static void main (String args[]) throws Exception { |
53 public static void main (String args[]) throws Exception { |
45 |
54 setup(); |
46 String workdir = System.getProperty("test.classes"); |
|
47 if (workdir == null) { |
|
48 workdir = args[0]; |
|
49 } |
|
50 |
55 |
51 /* the jar we copy for each test */ |
56 /* the jar we copy for each test */ |
52 File srcfile = new File (workdir, "foo.jar"); |
57 File srcfile = new File(WORK_DIR, "foo.jar"); |
53 |
58 |
54 /* the jar we use for the test */ |
59 /* the jar we use for the test */ |
55 File testfile = new File (workdir, "test.jar"); |
60 File testfile = new File(WORK_DIR, "test.jar"); |
56 |
61 |
57 copyFile (srcfile, testfile); |
62 copyFile(srcfile, testfile); |
58 test (testfile, false, false); |
63 test(testfile, false, false); |
59 |
64 |
60 copyFile (srcfile, testfile); |
65 copyFile(srcfile, testfile); |
61 test (testfile, true, false); |
66 test(testfile, true, false); |
62 |
67 |
63 copyFile (srcfile, testfile); |
68 copyFile(srcfile, testfile); |
64 test (testfile, true, true); |
69 test(testfile, true, true); |
65 |
70 |
66 // repeat test using a directory of files |
71 // repeat test using a directory of files |
67 |
72 |
68 File testdir= new File (workdir, "testdir"); |
73 File testdir = new File(WORK_DIR, "testdir"); |
69 File srcdir= new File (workdir, "test3"); |
74 File srcdir = new File(WORK_DIR, "test3"); |
70 |
75 |
71 copyDir (srcdir, testdir); |
76 copyDir(srcdir, testdir); |
72 test (testdir, true, false); |
77 test(testdir, true, false); |
73 |
|
74 } |
78 } |
75 |
79 |
76 // create a loader on jarfile (or directory) |
80 // create a loader on jarfile (or directory) |
77 // load a class , then look for a resource |
81 // load a class , then look for a resource |
78 // then close the loader |
82 // then close the loader |
95 s.close(); |
99 s.close(); |
96 } |
100 } |
97 |
101 |
98 loader.close (); |
102 loader.close (); |
99 |
103 |
100 // shouuld not find bye.txt now |
104 // should not find bye.txt now |
101 InputStream s1 = loader.getResourceAsStream("bye.txt"); |
105 InputStream s1 = loader.getResourceAsStream("bye.txt"); |
102 if (s1 != null) { |
106 if (s1 != null) { |
103 throw new RuntimeException ("closed loader returned resource"); |
107 throw new RuntimeException ("closed loader returned resource"); |
104 } |
108 } |
105 |
109 |
106 // now check we can delete the path |
110 // now check we can delete the path |
107 rm_minus_rf (file); |
111 rm_minus_rf (file); |
108 System.out.println (" ... OK"); |
112 System.out.println (" ... OK"); |
109 } |
113 } |
|
114 |
|
115 /** |
|
116 * Prepare jars files for the tests |
|
117 */ |
|
118 private static void setup () throws IOException { |
|
119 Path classes = Paths.get(WORK_DIR); |
|
120 Path testSrc = Paths.get(System.getProperty("test.src"), |
|
121 "test1", "com", "foo", "TestClass.java"); |
|
122 Path targetDir = classes.resolve("test3"); |
|
123 Path testTarget = targetDir.resolve("TestClass.java"); |
|
124 Files.createDirectories(targetDir); |
|
125 Files.copy(testSrc, testTarget, StandardCopyOption.REPLACE_EXISTING); |
|
126 // Compile sources for corresponding test |
|
127 CompilerUtils.compile(targetDir, targetDir); |
|
128 // Prepare txt files |
|
129 Files.write(targetDir.resolve("hello.txt"), "Hello world".getBytes(), |
|
130 StandardOpenOption.CREATE); |
|
131 Files.write(targetDir.resolve("bye.txt"), "Bye world".getBytes(), |
|
132 StandardOpenOption.CREATE); |
|
133 // Create jar |
|
134 JarUtils.createJarFile(classes.resolve("foo.jar"), targetDir); |
|
135 } |
110 } |
136 } |