1 /* 2 * Copyright (c) 2011, 2013, 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 28 * @build jdk.testlibrary.FileUtils CompilerUtils JarUtils 29 * @run main/othervm GetResourceAsStream 30 */ 31 32 import java.io.File; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.net.URL; 36 import java.net.URLClassLoader; 37 import java.nio.file.Path; 38 import java.nio.file.Paths; 39 import java.nio.file.StandardOpenOption; 40 import static java.nio.file.Files.write; 41 42 public class GetResourceAsStream extends Common { 43 44 public static final Path TEST_SRC = Paths.get(System 45 .getProperty("test.src")); 46 public static final Path TEST_WORK_DIR = Paths.get(System 47 .getProperty("user.dir")); 48 public static final Path TEST3 = TEST_SRC.resolve("test1"); 49 public static final Path TEST3_DIR = TEST_WORK_DIR.resolve("test3"); 50 public static final Path TEST_DIR = TEST_WORK_DIR.resolve("testDir"); 51 public static final Path FOO_JAR = TEST_WORK_DIR.resolve("foo.jar"); 52 public static final Path TEST_JAR = TEST_WORK_DIR.resolve("test.jar"); 53 /* 54 * We simply test various scenarios with class/resource files 55 * and make sure the files can be deleted after closing 56 * the loader. Therefore, the test will only really be verified 57 * on Windows. It will still run correctly on other platforms 58 */ 59 public static void main(String args[]) throws Exception { 60 61 setUpTest(); 62 63 copyFile(FOO_JAR.toFile(), TEST_JAR.toFile()); 64 test(TEST_JAR.toFile(), false, false); 65 66 copyFile(FOO_JAR.toFile(), TEST_JAR.toFile()); 67 test(TEST_JAR.toFile(), true, false); 68 69 copyFile(FOO_JAR.toFile(), TEST_JAR.toFile()); 70 test(TEST_JAR.toFile(), true, true); 71 72 // repeat test using a directory of files 73 copyDir(TEST3_DIR.toRealPath().toFile(), TEST_DIR.toFile()); 74 test(TEST3_DIR.toFile(), true, false); 75 76 } 77 78 static void setUpTest() throws IOException { 79 CompilerUtils.compile(TEST3, TEST3_DIR); 80 write(TEST3_DIR.resolve("hello.txt"), "Hello World".getBytes("UTF-8"), 81 StandardOpenOption.CREATE); 82 write(TEST3_DIR.resolve("Bye.txt"), "Bye world".getBytes("UTF-8"), 83 StandardOpenOption.CREATE); 84 JarUtils.createJarFile(FOO_JAR, TEST3_DIR); 85 86 } 87 88 // create a loader on jarfile (or directory) 89 // load a class , then look for a resource 90 // then close the loader 91 // check further new classes/resources cannot be loaded 92 // check jar (or dir) can be deleted 93 94 static void test(File file, boolean loadclass, boolean readall) 95 throws Exception { 96 URL[] urls = new URL[]{file.toURL()}; 97 System.out.println("Doing tests with URL: " + urls[0]); 98 URLClassLoader loader = new URLClassLoader(urls); 99 if (loadclass) { 100 Class testclass = loadClass("com.foo.TestClass", loader, true); 101 } 102 InputStream s = loader.getResourceAsStream("hello.txt"); 103 s.read(); 104 if (readall) { 105 while (s.read() != -1) ; 106 s.close(); 107 } 108 109 loader.close(); 110 111 // shouuld not find bye.txt now 112 InputStream s1 = loader.getResourceAsStream("bye.txt"); 113 if (s1 != null) { 114 throw new RuntimeException("closed loader returned resource"); 115 } 116 117 // now check we can delete the path 118 rm_minus_rf(file); 119 System.out.println(" ... OK"); 120 } 121 }