ClassPathTest.java revision 0
0N/A/*
0N/A * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4110602
0N/A * @author Benjamin Renaud
0N/A * @summary check that URLClassLoader correctly interprets Class-Path
0N/A *
0N/A * This test ensures that a manually constructed URLClassLoader is
0N/A * able to:
0N/A *
0N/A * 1. load a class
0N/A * 2. resolve Class-Path dependencies
0N/A * 3. have that class use a dependent class in a different JAR file
0N/A */
0N/Aimport java.util.jar.*;
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/A
0N/Apublic class ClassPathTest {
0N/A
0N/A JarFile jarFile;
0N/A Manifest manifest;
0N/A Attributes mainAttributes;
0N/A Map map;
0N/A URLClassLoader ucl;
0N/A
0N/A static class TestException extends RuntimeException {
0N/A TestException(Throwable t) {
0N/A super("URLClassLoader ClassPathTest failed with: " + t);
0N/A }
0N/A }
0N/A
0N/A public ClassPathTest() {
0N/A File local = new File(System.getProperty("test.src", "."),
0N/A "jars/class_path_test.jar");
0N/A String jarFileName = local.getPath();
0N/A
0N/A try {
0N/A jarFile = new JarFile(jarFileName);
0N/A }
0N/A catch (IOException e) {
0N/A System.err.println("Could not find jar file " + jarFileName);
0N/A throw new TestException(e);
0N/A }
0N/A try {
0N/A URL url = getUrl(new File(jarFileName));
0N/A System.out.println("url: " + url);
0N/A
0N/A ucl = new URLClassLoader(new URL[] { url });
0N/A
0N/A // Moved this inside try block, as it may raise IOException.
0N/A // (maddox)
0N/A manifest = jarFile.getManifest();
0N/A }
0N/A catch (Exception e) {
0N/A throw new TestException(e);
0N/A }
0N/A //manifest = jarFile.getManifest();
0N/A mainAttributes = manifest.getMainAttributes();
0N/A map = manifest.getEntries();
0N/A
0N/A Iterator it = map.entrySet().iterator();
0N/A Class clazz = null;
0N/A
0N/A while (it.hasNext()) {
0N/A Map.Entry e = (Map.Entry)it.next();
0N/A Attributes a = (Attributes)e.getValue();
0N/A
0N/A Attributes.Name an = new Attributes.Name("Class-Path");
0N/A if (a.containsKey(an)) {
0N/A String val = a.getValue(an);
0N/A if (val != null)
0N/A System.out.println("Class-Path: " + val);
0N/A }
0N/A
0N/A if (a.containsKey(new Attributes.Name("Java-Bean"))) {
0N/A
0N/A String beanClassName = nameToClassName((String)e.getKey());
0N/A System.out.println("JavaBean Class: " + beanClassName);
0N/A
0N/A try {
0N/A clazz = ucl.loadClass(beanClassName);
0N/A }
0N/A catch (Throwable t) {
0N/A throw new TestException(t);
0N/A } if (clazz != null) {
0N/A try {
0N/A System.out.println("instantiating " + beanClassName);
0N/A clazz.newInstance();
0N/A System.out.println("done instantiating " +
0N/A beanClassName);
0N/A } catch (Throwable t2) {
0N/A throw new TestException(t2);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A String nameToClassName(String key) {
0N/A String key2 = key.replace('/', File.separatorChar);
0N/A int li = key2.lastIndexOf(".class");
0N/A key2 = key2.substring(0, li);
0N/A return key2;
0N/A }
0N/A
0N/A private static URL getUrl(File file) {
0N/A String name;
0N/A try {
0N/A name = file.getCanonicalPath();
0N/A } catch (IOException e) {
0N/A name = file.getAbsolutePath();
0N/A }
0N/A name = name.replace(File.separatorChar, '/');
0N/A if (!name.startsWith("/")) {
0N/A name = "/" + name;
0N/A }
0N/A try {
0N/A return new URL( "file:" + name);
0N/A } catch (MalformedURLException e) {
0N/A throw new TestException(e);
0N/A }
0N/A }
0N/A
0N/A public static void main(String args[]) {
0N/A new ClassPathTest();
0N/A }
0N/A}