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 jdk.test.lib.compiler.CompilerUtils 30 * jdk.test.lib.util.FileUtils 31 * jdk.test.lib.Utils 32 * jdk.test.lib.Asserts 33 * jdk.test.lib.JDKToolFinder 34 * jdk.test.lib.JDKToolLauncher 35 * jdk.test.lib.Platform 36 * jdk.test.lib.process.* 37 * JarUtils 38 * @run main/othervm GetResourceAsStream 39 */ 40 41 import java.io.File; 42 import java.io.IOException; 43 import java.io.InputStream; 44 import java.net.URL; 45 import java.net.URLClassLoader; 46 import java.nio.file.Files; 47 import java.nio.file.Path; 48 import java.nio.file.Paths; 49 import java.nio.file.StandardCopyOption; 50 import java.nio.file.StandardOpenOption; 51 import jdk.test.lib.compiler.CompilerUtils; 52 53 public class GetResourceAsStream extends Common { 54 private static final String WORK_DIR = System.getProperty("user.dir"); 55 56 /* 57 * We simply test various scenarios with class/resource files 58 * and make sure the files can be deleted after closing 59 * the loader. Therefore, the test will only really be verified 60 * on Windows. It will still run correctly on other platforms 61 */ 62 public static void main (String args[]) throws Exception { 63 setup(); 64 65 /* the jar we copy for each test */ 66 File srcfile = new File(WORK_DIR, "foo.jar"); 67 68 /* the jar we use for the test */ 69 File testfile = new File(WORK_DIR, "test.jar"); 70 71 copyFile(srcfile, testfile); 72 test(testfile, false, false); 73 74 copyFile(srcfile, testfile); 75 test(testfile, true, false); 76 77 copyFile(srcfile, testfile); 78 test(testfile, true, true); 79 80 // repeat test using a directory of files 81 82 File testdir = new File(WORK_DIR, "testdir"); 83 File srcdir = new File(WORK_DIR, "test3"); 84 85 copyDir(srcdir, testdir); 86 test(testdir, true, false); 87 } 88 89 // create a loader on jarfile (or directory) 90 // load a class , then look for a resource 91 // then close the loader 92 // check further new classes/resources cannot be loaded 93 // check jar (or dir) can be deleted 94 95 static void test (File file, boolean loadclass, boolean readall) 96 throws Exception 97 { 98 URL[] urls = new URL[] {file.toURI().toURL()}; 99 System.out.println ("Doing tests with URL: " + urls[0]); 100 URLClassLoader loader = new URLClassLoader (urls); 101 if (loadclass) { 102 Class testclass = loadClass ("com.foo.TestClass", loader, true); 103 } 104 InputStream s = loader.getResourceAsStream ("hello.txt"); 105 s.read(); 106 if (readall) { 107 while (s.read() != -1) ; 108 s.close(); 109 } 110 111 loader.close (); 112 113 // should not find bye.txt now 114 InputStream s1 = loader.getResourceAsStream("bye.txt"); 115 if (s1 != null) { 116 throw new RuntimeException ("closed loader returned resource"); 117 } 118 119 // now check we can delete the path 120 rm_minus_rf (file); 121 System.out.println (" ... OK"); 122 } 123 124 /** 125 * Prepare jars files for the tests 126 */ 127 private static void setup () throws IOException { 128 Path classes = Paths.get(WORK_DIR); 129 Path testSrc = Paths.get(System.getProperty("test.src"), 130 "test1", "com", "foo", "TestClass.java"); 131 Path targetDir = classes.resolve("test3"); 132 Path testTarget = targetDir.resolve("TestClass.java"); 133 Files.createDirectories(targetDir); 134 Files.copy(testSrc, testTarget, StandardCopyOption.REPLACE_EXISTING); 135 // Compile sources for corresponding test 136 CompilerUtils.compile(targetDir, targetDir); 137 // Prepare txt files 138 Files.write(targetDir.resolve("hello.txt"), "Hello world".getBytes(), 139 StandardOpenOption.CREATE); 140 Files.write(targetDir.resolve("bye.txt"), "Bye world".getBytes(), 141 StandardOpenOption.CREATE); 142 // Create jar 143 JarUtils.createJarFile(classes.resolve("foo.jar"), targetDir); 144 } 145 }