Path: blob/master/test/jdk/java/net/URLClassLoader/closetest/GetResourceAsStream.java
41153 views
/*1* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 689991926* @library /test/lib27* @modules jdk.compiler28* @build jdk.test.lib.compiler.CompilerUtils29* jdk.test.lib.util.FileUtils30* jdk.test.lib.util.JarUtils31* jdk.test.lib.Platform32* @run main/othervm GetResourceAsStream33*/3435import java.io.File;36import java.io.IOException;37import java.io.InputStream;38import java.net.URL;39import java.net.URLClassLoader;40import java.nio.file.Files;41import java.nio.file.Path;42import java.nio.file.Paths;43import java.nio.file.StandardCopyOption;44import java.nio.file.StandardOpenOption;45import jdk.test.lib.compiler.CompilerUtils;46import jdk.test.lib.util.JarUtils;4748public class GetResourceAsStream extends Common {49private static final String WORK_DIR = System.getProperty("user.dir");5051/*52* We simply test various scenarios with class/resource files53* and make sure the files can be deleted after closing54* the loader. Therefore, the test will only really be verified55* on Windows. It will still run correctly on other platforms56*/57public static void main (String args[]) throws Exception {58setup();5960/* the jar we copy for each test */61File srcfile = new File(WORK_DIR, "foo.jar");6263/* the jar we use for the test */64File testfile = new File(WORK_DIR, "test.jar");6566copyFile(srcfile, testfile);67test(testfile, false, false);6869copyFile(srcfile, testfile);70test(testfile, true, false);7172copyFile(srcfile, testfile);73test(testfile, true, true);7475// repeat test using a directory of files7677File testdir = new File(WORK_DIR, "testdir");78File srcdir = new File(WORK_DIR, "test3");7980copyDir(srcdir, testdir);81test(testdir, true, false);82}8384// create a loader on jarfile (or directory)85// load a class , then look for a resource86// then close the loader87// check further new classes/resources cannot be loaded88// check jar (or dir) can be deleted8990static void test (File file, boolean loadclass, boolean readall)91throws Exception92{93URL[] urls = new URL[] {file.toURI().toURL()};94System.out.println ("Doing tests with URL: " + urls[0]);95URLClassLoader loader = new URLClassLoader (urls);96if (loadclass) {97Class testclass = loadClass ("com.foo.TestClass", loader, true);98}99InputStream s = loader.getResourceAsStream ("hello.txt");100s.read();101if (readall) {102while (s.read() != -1) ;103s.close();104}105106loader.close ();107108// should not find bye.txt now109InputStream s1 = loader.getResourceAsStream("bye.txt");110if (s1 != null) {111throw new RuntimeException ("closed loader returned resource");112}113114// now check we can delete the path115rm_minus_rf (file);116System.out.println (" ... OK");117}118119/**120* Prepare jars files for the tests121*/122private static void setup () throws IOException {123Path classes = Paths.get(WORK_DIR);124Path testSrc = Paths.get(System.getProperty("test.src"),125"test1", "com", "foo", "TestClass.java");126Path targetDir = classes.resolve("test3");127Path testTarget = targetDir.resolve("TestClass.java");128Files.createDirectories(targetDir);129Files.copy(testSrc, testTarget, StandardCopyOption.REPLACE_EXISTING);130// Compile sources for corresponding test131CompilerUtils.compile(targetDir, targetDir);132// Prepare txt files133Files.write(targetDir.resolve("hello.txt"), "Hello world".getBytes(),134StandardOpenOption.CREATE);135Files.write(targetDir.resolve("bye.txt"), "Bye world".getBytes(),136StandardOpenOption.CREATE);137// Create jar138JarUtils.createJarFile(classes.resolve("foo.jar"), targetDir);139}140}141142143