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