Path: blob/master/test/jdk/java/net/URLClassLoader/ClassPathTest.java
41149 views
/*1* Copyright (c) 1998, 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/* @test24* @bug 411060225* @author Benjamin Renaud26* @summary check that URLClassLoader correctly interprets Class-Path27*28* This test ensures that a manually constructed URLClassLoader is29* able to:30*31* 1. load a class32* 2. resolve Class-Path dependencies33* 3. have that class use a dependent class in a different JAR file34*/35import java.util.jar.*;36import java.util.*;37import java.io.*;38import java.net.*;3940public class ClassPathTest {4142JarFile jarFile;43Manifest manifest;44Attributes mainAttributes;45Map map;46URLClassLoader ucl;4748static class TestException extends RuntimeException {49TestException(Throwable t) {50super("URLClassLoader ClassPathTest failed with: " + t);51}52}5354public ClassPathTest() {55File local = new File(System.getProperty("test.src", "."),56"jars/class_path_test.jar");57String jarFileName = local.getPath();5859try {60jarFile = new JarFile(jarFileName);61}62catch (IOException e) {63System.err.println("Could not find jar file " + jarFileName);64throw new TestException(e);65}66try {67URL url = getUrl(new File(jarFileName));68System.out.println("url: " + url);6970ucl = new URLClassLoader(new URL[] { url });7172// Moved this inside try block, as it may raise IOException.73// (maddox)74manifest = jarFile.getManifest();75}76catch (Exception e) {77throw new TestException(e);78}79//manifest = jarFile.getManifest();80mainAttributes = manifest.getMainAttributes();81map = manifest.getEntries();8283Iterator it = map.entrySet().iterator();84Class clazz = null;8586while (it.hasNext()) {87Map.Entry e = (Map.Entry)it.next();88Attributes a = (Attributes)e.getValue();8990Attributes.Name an = new Attributes.Name("Class-Path");91if (a.containsKey(an)) {92String val = a.getValue(an);93if (val != null)94System.out.println("Class-Path: " + val);95}9697if (a.containsKey(new Attributes.Name("Java-Bean"))) {9899String beanClassName = nameToClassName((String)e.getKey());100System.out.println("JavaBean Class: " + beanClassName);101102try {103clazz = ucl.loadClass(beanClassName);104}105catch (Throwable t) {106throw new TestException(t);107} if (clazz != null) {108try {109System.out.println("instantiating " + beanClassName);110clazz.newInstance();111System.out.println("done instantiating " +112beanClassName);113} catch (Throwable t2) {114throw new TestException(t2);115}116}117}118}119}120121String nameToClassName(String key) {122String key2 = key.replace('/', File.separatorChar);123int li = key2.lastIndexOf(".class");124key2 = key2.substring(0, li);125return key2;126}127128private static URL getUrl(File file) {129String name;130try {131name = file.getCanonicalPath();132} catch (IOException e) {133name = file.getAbsolutePath();134}135name = name.replace(File.separatorChar, '/');136if (!name.startsWith("/")) {137name = "/" + name;138}139try {140return new URL( "file:" + name);141} catch (MalformedURLException e) {142throw new TestException(e);143}144}145146public static void main(String args[]) {147new ClassPathTest();148}149}150151152