jdk/test/java/net/URLClassLoader/ClassPathTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4110602
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary check that URLClassLoader correctly interprets Class-Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This test ensures that a manually constructed URLClassLoader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * able to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * 1. load a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * 2. resolve Class-Path dependencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * 3. have that class use a dependent class in a different JAR file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class ClassPathTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    JarFile jarFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    Manifest manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    Attributes mainAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    Map map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    URLClassLoader ucl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static class TestException extends RuntimeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        TestException(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            super("URLClassLoader ClassPathTest failed with: " + t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public ClassPathTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        File local = new File(System.getProperty("test.src", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                              "jars/class_path_test.jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        String jarFileName = local.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            jarFile = new JarFile(jarFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            System.err.println("Could not find jar file " + jarFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new TestException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            URL url = getUrl(new File(jarFileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            System.out.println("url: " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            ucl = new URLClassLoader(new URL[] { url });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            // Moved this inside try block, as it may raise IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            // (maddox)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            manifest = jarFile.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new TestException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        //manifest = jarFile.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        mainAttributes = manifest.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        map = manifest.getEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        Iterator it = map.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Class clazz = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            Map.Entry e = (Map.Entry)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            Attributes a = (Attributes)e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            Attributes.Name an = new Attributes.Name("Class-Path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (a.containsKey(an)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                String val = a.getValue(an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (val != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    System.out.println("Class-Path: " + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (a.containsKey(new Attributes.Name("Java-Bean"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                String beanClassName = nameToClassName((String)e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                System.out.println("JavaBean Class: " + beanClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    clazz = ucl.loadClass(beanClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    throw new TestException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                } if (clazz != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        System.out.println("instantiating " + beanClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        clazz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        System.out.println("done instantiating " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                           beanClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    } catch (Throwable t2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        throw new TestException(t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    String nameToClassName(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        String key2 = key.replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int li = key2.lastIndexOf(".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        key2 = key2.substring(0, li);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return key2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static URL getUrl(File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            name = file.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            name = file.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        name = name.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (!name.startsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            name = "/" + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return new URL( "file:" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new TestException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        new ClassPathTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
}