Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLClassLoader/closetest/GetResourceAsStream.java
38828 views
/*1* Copyright (c) 2011, 2013, 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 /lib/testlibrary27* @build jdk.testlibrary.*28* @run shell build2.sh29* @run main/othervm GetResourceAsStream30*/3132import java.io.*;33import java.net.*;3435public class GetResourceAsStream extends Common {3637/*38* We simply test various scenarios with class/resource files39* and make sure the files can be deleted after closing40* the loader. Therefore, the test will only really be verified41* on Windows. It will still run correctly on other platforms42*/43public static void main (String args[]) throws Exception {4445String workdir = System.getProperty("test.classes");46if (workdir == null) {47workdir = args[0];48}4950/* the jar we copy for each test */51File srcfile = new File (workdir, "foo.jar");5253/* the jar we use for the test */54File testfile = new File (workdir, "test.jar");5556copyFile (srcfile, testfile);57test (testfile, false, false);5859copyFile (srcfile, testfile);60test (testfile, true, false);6162copyFile (srcfile, testfile);63test (testfile, true, true);6465// repeat test using a directory of files6667File testdir= new File (workdir, "testdir");68File srcdir= new File (workdir, "test3");6970copyDir (srcdir, testdir);71test (testdir, true, false);7273}7475// create a loader on jarfile (or directory)76// load a class , then look for a resource77// then close the loader78// check further new classes/resources cannot be loaded79// check jar (or dir) can be deleted8081static void test (File file, boolean loadclass, boolean readall)82throws Exception83{84URL[] urls = new URL[] {file.toURL()};85System.out.println ("Doing tests with URL: " + urls[0]);86URLClassLoader loader = new URLClassLoader (urls);87if (loadclass) {88Class testclass = loadClass ("com.foo.TestClass", loader, true);89}90InputStream s = loader.getResourceAsStream ("hello.txt");91s.read();92if (readall) {93while (s.read() != -1) ;94s.close();95}9697loader.close ();9899// shouuld not find bye.txt now100InputStream s1 = loader.getResourceAsStream("bye.txt");101if (s1 != null) {102throw new RuntimeException ("closed loader returned resource");103}104105// now check we can delete the path106rm_minus_rf (file);107System.out.println (" ... OK");108}109}110111112