Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLClassLoader/closetest/CloseTest.java
38828 views
/*1* Copyright (c) 2009, 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 416787426* @library ../../../../com/sun/net/httpserver27* @library /lib/testlibrary28* @build jdk.testlibrary.* FileServerHandler29* @run shell build.sh30* @run main/othervm CloseTest31* @summary URL-downloaded jar files can consume all available file descriptors32*/3334import java.io.*;35import java.net.*;36import java.lang.reflect.*;37import com.sun.net.httpserver.*;3839public class CloseTest extends Common {4041//42// needs two jar files test1.jar and test2.jar with following structure43//44// com/foo/TestClass45// com/foo/TestClass146// com/foo/Resource147// com/foo/Resource248//49// and a directory hierarchy with the same structure/contents5051public static void main (String args[]) throws Exception {5253String workdir = System.getProperty("test.classes");54if (workdir == null) {55workdir = args[0];56}57if (!workdir.endsWith("/")) {58workdir = workdir+"/";59}6061startHttpServer (workdir+"serverRoot/");6263String testjar = workdir + "test.jar";64copyFile (workdir+"test1.jar", testjar);65test (testjar, 1);6667// repeat test with different implementation68// of test.jar (whose TestClass.getValue() returns 26970copyFile (workdir+"test2.jar", testjar);71test (testjar, 2);7273// repeat test using a directory of files74String testdir=workdir+"testdir/";75rm_minus_rf (new File(testdir));76copyDir (workdir+"test1/", testdir);77test (testdir, 1);7879testdir=workdir+"testdir/";80rm_minus_rf (new File(testdir));81copyDir (workdir+"test2/", testdir);82test (testdir, 2);83getHttpServer().stop (3);84}8586// create a loader on jarfile (or directory), plus a http loader87// load a class , then look for a resource88// also load a class from http loader89// then close the loader90// check further new classes/resources cannot be loaded91// check jar (or dir) can be deleted92// check existing classes can be loaded93// check boot classes can be loaded9495static void test (String name, int expectedValue) throws Exception {96URL url = new URL ("file", null, name);97URL url2 = getServerURL();98System.out.println ("Doing tests with URL: " + url + " and " + url2);99URL[] urls = new URL[2];100urls[0] = url;101urls[1] = url2;102URLClassLoader loader = new URLClassLoader (urls);103Class testclass = loadClass ("com.foo.TestClass", loader, true);104Class class2 = loadClass ("Test", loader, true); // from http105class2.newInstance();106Object test = testclass.newInstance();107Method method = testclass.getDeclaredMethods()[0]; // int getValue();108int res = (Integer) method.invoke (test);109110if (res != expectedValue) {111throw new RuntimeException ("wrong value from getValue() ["+res+112"/"+expectedValue+"]");113}114115// should find /resource1116URL u1 = loader.findResource ("com/foo/Resource1");117if (u1 == null) {118throw new RuntimeException ("can't find com/foo/Resource1 in test1.jar");119}120loader.close ();121122// should NOT find /resource2 even though it is in jar123URL u2 = loader.findResource ("com/foo/Resource2");124if (u2 != null) {125throw new RuntimeException ("com/foo/Resource2 unexpected in test1.jar");126}127128// load tests129loadClass ("com.foo.TestClass1", loader, false);130loadClass ("com.foo.TestClass", loader, true);131loadClass ("java.sql.Array", loader, true);132133// now check we can delete the path134rm_minus_rf (new File(name));135System.out.println (" ... OK");136}137138static HttpServer httpServer;139140static HttpServer getHttpServer() {141return httpServer;142}143144static URL getServerURL () throws Exception {145int port = httpServer.getAddress().getPort();146String s = "http://127.0.0.1:"+port+"/";147return new URL(s);148}149150static void startHttpServer (String docroot) throws Exception {151httpServer = HttpServer.create (new InetSocketAddress(0), 10);152HttpContext ctx = httpServer.createContext (153"/", new FileServerHandler(docroot)154);155httpServer.start();156}157}158159160