Path: blob/master/test/jdk/java/net/URLClassLoader/GetURLsTest.java
41149 views
/*1* Copyright (c) 1998, 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*/2223import java.net.*;24import java.io.*;2526/*27* @test28* @summary Regression test for URLClassLoader getURLs() and addURL() methods.29* See RFE 4102580: Need URLClassLoader.getURLs() method30*/31public class GetURLsTest {32static final String TEST_DIR = System.getProperty("test.src", ".");3334public static void main(String[] args) throws Exception {35File testJars = new File(TEST_DIR, "jars");3637MyURLClassLoader ucl =38new MyURLClassLoader(new URL[] { new File(".").toURL() });39p("initial urls = ", ucl.getURLs());40URL u = ucl.getResource("GetURLsTest.java");41if (u != null) {42p("found resource = " + u);43}44ucl.addURL(new File(testJars, "class_path_test.jar").toURL());45p("new urls = ", ucl.getURLs());46Class c = ucl.loadClass("Foo");47p("found class = " + c);48}4950static class MyURLClassLoader extends URLClassLoader {51public MyURLClassLoader(URL[] urls) {52super(urls);53}54public void addURL(URL url) {55super.addURL(url);56}57}5859static void p(String s, URL[] urls) {60System.out.print(s);61if (urls.length > 0) {62for (int i = 0; i < urls.length - 1; i++) {63System.out.print(urls[i] + " ");64}65}66System.out.println(urls[urls.length - 1]);67}6869static void p(String s) {70System.out.println(s);71}72}737475