Path: blob/master/test/jdk/java/net/URLClassLoader/closetest/CloseTest.java
41153 views
/*1* Copyright (c) 2009, 2019, 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* @modules java.logging27* jdk.httpserver28* jdk.compiler29* @library ../../../../com/sun/net/httpserver30* /test/lib31* @build jdk.test.lib.compiler.CompilerUtils32* jdk.test.lib.util.FileUtils33* jdk.test.lib.util.JarUtils34* jdk.test.lib.Platform35* FileServerHandler36* @run main/othervm CloseTest37* @summary URL-downloaded jar files can consume all available file descriptors38*/3940import java.io.File;41import java.io.IOException;42import java.lang.reflect.Method;43import java.net.URLClassLoader;44import java.net.InetAddress;45import java.net.InetSocketAddress;46import java.net.URL;47import java.nio.file.Files;48import java.nio.file.Path;49import java.nio.file.Paths;5051import jdk.test.lib.compiler.CompilerUtils;52import jdk.test.lib.net.URIBuilder;53import jdk.test.lib.util.JarUtils;5455import com.sun.net.httpserver.HttpContext;56import com.sun.net.httpserver.HttpServer;5758import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;5960public class CloseTest extends Common {61private static final String WORK_DIR = System.getProperty("user.dir")62+ "/";63//64// needs two jar files test1.jar and test2.jar with following structure65//66// com/foo/TestClass67// com/foo/TestClass168// com/foo/Resource169// com/foo/Resource270//71// and a directory hierarchy with the same structure/contents7273public static void main(String args[]) throws Exception {74setup();7576startHttpServer(WORK_DIR + "serverRoot/");7778String testjar = WORK_DIR + "test.jar";79copyFile(WORK_DIR + "test1.jar", testjar);80test(testjar, 1);8182// repeat test with different implementation83// of test.jar (whose TestClass.getValue() returns 284copyFile(WORK_DIR + "test2.jar", testjar);85test(testjar, 2);8687// repeat test using a directory of files88String testdir = WORK_DIR + "testdir/";89rm_minus_rf(new File(testdir));90copyDir(WORK_DIR + "test1/", testdir);91test(testdir, 1);9293testdir = WORK_DIR + "testdir/";94rm_minus_rf(new File(testdir));95copyDir(WORK_DIR + "test2/", testdir);96test(testdir, 2);97getHttpServer().stop(3);98}99100// create a loader on jarfile (or directory), plus a http loader101// load a class , then look for a resource102// also load a class from http loader103// then close the loader104// check further new classes/resources cannot be loaded105// check jar (or dir) can be deleted106// check existing classes can be loaded107// check boot classes can be loaded108109static void test(String name, int expectedValue) throws Exception {110111URL url = new URL("file", null, name);112URL url2 = getServerURL();113System.out.println("Doing tests with URL: " + url + " and " + url2);114URL[] urls = new URL[2];115urls[0] = url;116urls[1] = url2;117URLClassLoader loader = new URLClassLoader(urls);118Class testclass = loadClass("com.foo.TestClass", loader, true);119Class class2 = loadClass("Test", loader, true); // from http120class2.newInstance();121Object test = testclass.newInstance();122Method method = testclass.getDeclaredMethods()[0]; // int getValue();123int res = (Integer) method.invoke(test);124125if (res != expectedValue) {126throw new RuntimeException("wrong value from getValue() [" + res +127"/" + expectedValue + "]");128}129130// should find /resource1131URL u1 = loader.findResource("com/foo/Resource1");132if (u1 == null) {133throw new RuntimeException("can't find com/foo/Resource1 in test1.jar");134}135loader.close();136137// should NOT find /resource2 even though it is in jar138URL u2 = loader.findResource("com/foo/Resource2");139if (u2 != null) {140throw new RuntimeException("com/foo/Resource2 unexpected in test1.jar");141}142143// load tests144loadClass("com.foo.TestClass1", loader, false);145loadClass("com.foo.TestClass", loader, true);146loadClass("java.util.ArrayList", loader, true);147148// now check we can delete the path149rm_minus_rf(new File(name));150System.out.println(" ... OK");151}152153static HttpServer httpServer;154155static HttpServer getHttpServer() {156return httpServer;157}158159static URL getServerURL() throws Exception {160int port = httpServer.getAddress().getPort();161return URIBuilder.newBuilder()162.scheme("http")163.loopback()164.port(port)165.path("/")166.toURL();167}168169static void startHttpServer(String docroot) throws Exception {170httpServer = HttpServer.create(171new InetSocketAddress(InetAddress.getLoopbackAddress(), 0),17210);173HttpContext ctx = httpServer.createContext(174"/", new FileServerHandler(docroot)175);176httpServer.start();177}178179/**180* Prepare jars files for the tests181*/182private static void setup () throws IOException {183String[] tests = new String[]{"test1", "test2"};184Path workDir = Paths.get(WORK_DIR);185Path testSrc = Paths.get(System.getProperty("test.src"));186for (String test : tests) {187Path testSrcDir = testSrc.resolve(test);188Path testTargetDir = workDir.resolve(test);189// Compile sources for corresponding test190CompilerUtils.compile(testSrcDir, testTargetDir);191// Copy all resources192Path packages = Paths.get("com", "foo");193Path copySrcDir = testSrcDir.resolve(packages);194Path copyTargetDir = testTargetDir.resolve(packages);195Files.createDirectories(copyTargetDir);196Path res1 = Paths.get("Resource1");197Path res2 = Paths.get("Resource2");198Files.copy(copySrcDir.resolve(res1), copyTargetDir.resolve(res1),199REPLACE_EXISTING);200Files.copy(copySrcDir.resolve(res2), copyTargetDir.resolve(res2),201REPLACE_EXISTING);202// Create jar203JarUtils.createJarFile(workDir.resolve(test + ".jar"), testTargetDir);204}205206// Copy and compile server test class207Path serverRoot = Paths.get("serverRoot");208Path targetDir = workDir.resolve(serverRoot);209Path file = Paths.get("Test.java");210Files.createDirectories(targetDir);211Files.copy(testSrc.resolve(serverRoot).resolve(file),212targetDir.resolve(file), REPLACE_EXISTING);213CompilerUtils.compile(targetDir, targetDir);214}215}216217218