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