1 /* 2 * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 6899919 27 * @library /lib/testlibrary /test/lib 28 * @modules jdk.compiler 29 * @build JarUtils CompilerUtils 30 * @run main/othervm GetResourceAsStream 31 */ 32 33 import java.io.File; 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; 43 44 public class GetResourceAsStream extends Common { 45 private static final String WORK_DIR = System.getProperty("user.dir"); 46 47 /* 48 * We simply test various scenarios with class/resource files 49 * and make sure the files can be deleted after closing 50 * the loader. Therefore, the test will only really be verified 51 * on Windows. It will still run correctly on other platforms 52 */ 53 public static void main (String args[]) throws Exception { 54 setup(); 55 56 /* the jar we copy for each test */ 57 File srcfile = new File(WORK_DIR, "foo.jar"); 58 59 /* the jar we use for the test */ 60 File testfile = new File(WORK_DIR, "test.jar"); 61 62 copyFile(srcfile, testfile); 63 test(testfile, false, false); 64 65 copyFile(srcfile, testfile); 66 test(testfile, true, false); 67 68 copyFile(srcfile, testfile); 69 test(testfile, true, true); 70 71 // repeat test using a directory of files 72 73 File testdir = new File(WORK_DIR, "testdir"); 74 File srcdir = new File(WORK_DIR, "test3"); 75 76 copyDir(srcdir, testdir); 77 test(testdir, true, false); 78 } 79 80 // create a loader on jarfile (or directory) 81 // load a class , then look for a resource 82 // then close the loader 83 // check further new classes/resources cannot be loaded 84 // check jar (or dir) can be deleted 85 86 static void test (File file, boolean loadclass, boolean readall) 87 throws Exception 88 { 89 URL[] urls = new URL[] {file.toURI().toURL()}; 90 System.out.println ("Doing tests with URL: " + urls[0]); 91 URLClassLoader loader = new URLClassLoader (urls); 92 if (loadclass) { 93 Class testclass = loadClass ("com.foo.TestClass", loader, true); 94 } 95 InputStream s = loader.getResourceAsStream ("hello.txt"); 96 s.read(); 97 if (readall) { 98 while (s.read() != -1) ; 99 s.close(); 100 } 101 102 loader.close (); 103 104 // should not find bye.txt now 105 InputStream s1 = loader.getResourceAsStream("bye.txt"); 106 if (s1 != null) { 107 throw new RuntimeException ("closed loader returned resource"); 108 } 109 110 // now check we can delete the path 111 rm_minus_rf (file); 112 System.out.println (" ... OK"); 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 } 136 }