2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
@bug 4147599 4478150
|
|
26 |
@summary In 1.2beta4-I ClassLoader loaded classes can not link
|
|
27 |
against application classes.
|
|
28 |
*/
|
|
29 |
|
|
30 |
/*
|
|
31 |
* We are trying to test that certain methods of ClassLoader look at the same
|
|
32 |
* paths as they did in 1.1. To run this test on 1.1, you will have to pass
|
|
33 |
* "-1.1" as option on the command line.
|
|
34 |
*
|
|
35 |
* The required files are:
|
|
36 |
*
|
|
37 |
* - Loader.java (a 1.1 style class loader)
|
|
38 |
* - Loadee.java (source for a class that refers to Loader)
|
|
39 |
* - Loadee.classfile (to test findSystemClass)
|
|
40 |
* - Loadee.resource (to test getSystemResource)
|
|
41 |
* - java/lang/Object.class (to test getSystemResources)
|
|
42 |
*
|
|
43 |
* The extension ".classfile" is so the class file is not seen by any loader
|
|
44 |
* other than Loader. If you need to make any changes you will have to
|
|
45 |
* compile Loadee.java and rename Loadee.class to Loadee.classfile.
|
|
46 |
*/
|
|
47 |
|
|
48 |
import java.io.File;
|
|
49 |
import java.io.DataInputStream;
|
|
50 |
import java.io.FileInputStream;
|
|
51 |
import java.io.IOException;
|
|
52 |
import java.net.URL;
|
|
53 |
import java.util.HashSet;
|
|
54 |
|
|
55 |
|
|
56 |
/**
|
|
57 |
* A 1.1-style ClassLoader. The only class it can really load is "Loadee".
|
|
58 |
* For other classes it might be asked to load, it relies on loaders set up by
|
|
59 |
* the launcher.
|
|
60 |
*/
|
|
61 |
public class Loader extends ClassLoader {
|
|
62 |
|
|
63 |
public Class loadClass(String name, boolean resolve)
|
|
64 |
throws ClassNotFoundException {
|
|
65 |
Class c = null;
|
|
66 |
try {
|
|
67 |
c = findSystemClass(name);
|
|
68 |
} catch (ClassNotFoundException cnfe) {
|
|
69 |
}
|
|
70 |
if (c == null) {
|
|
71 |
if (!name.equals("Loadee"))
|
|
72 |
throw new Error("java.lang.ClassLoader.findSystemClass() " +
|
|
73 |
"did not find class " + name);
|
|
74 |
byte[] b = locateBytes();
|
|
75 |
c = defineClass(name, b, 0, b.length);
|
|
76 |
}
|
|
77 |
if (resolve) {
|
|
78 |
resolveClass(c);
|
|
79 |
}
|
|
80 |
return c;
|
|
81 |
}
|
|
82 |
|
|
83 |
private byte[] locateBytes() {
|
|
84 |
try {
|
|
85 |
File f = new File(System.getProperty("test.src", "."),
|
|
86 |
"Loadee.classfile");
|
|
87 |
long l = f.length();
|
|
88 |
byte[] b = new byte[(int)l];
|
|
89 |
DataInputStream in =
|
|
90 |
new DataInputStream(new FileInputStream(f));
|
|
91 |
in.readFully(b);
|
|
92 |
return b;
|
|
93 |
} catch (IOException ioe) {
|
|
94 |
ioe.printStackTrace();
|
|
95 |
throw new Error("Test failed due to IOException!");
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
private static final int FIND = 0x1;
|
|
100 |
private static final int RESOURCE = 0x2;
|
|
101 |
private static final int RESOURCES = 0x4;
|
|
102 |
|
|
103 |
public static void main(String[] args) throws Exception {
|
|
104 |
int tests = FIND | RESOURCE | RESOURCES;
|
|
105 |
|
|
106 |
if (args.length == 1 && args[0].equals("-1.1")) {
|
|
107 |
tests &= ~RESOURCES; /* Do not run getResources test. */
|
|
108 |
}
|
|
109 |
|
|
110 |
if ((tests & FIND) == FIND) {
|
|
111 |
report("findSystemClass()");
|
|
112 |
ClassLoader l = new Loader();
|
|
113 |
Class c = l.loadClass("Loadee");
|
|
114 |
Object o = c.newInstance();
|
|
115 |
}
|
|
116 |
|
|
117 |
if ((tests & RESOURCE) == RESOURCE) {
|
|
118 |
report("getSystemResource()");
|
|
119 |
URL u = getSystemResource("Loadee.resource");
|
|
120 |
if (u == null)
|
|
121 |
throw new Exception
|
|
122 |
("java.lang.ClassLoader.getSystemResource() test failed!");
|
|
123 |
}
|
|
124 |
|
|
125 |
if ((tests & RESOURCES) == RESOURCES) {
|
|
126 |
report("getSystemResources()");
|
|
127 |
java.util.Enumeration e =
|
|
128 |
getSystemResources("java/lang/Object.class");
|
|
129 |
HashSet hs = new HashSet();
|
|
130 |
while (e.hasMoreElements()) {
|
|
131 |
URL u = (URL)e.nextElement();
|
|
132 |
if (u == null)
|
|
133 |
break;
|
|
134 |
System.out.println("url: " + u);
|
|
135 |
hs.add(u);
|
|
136 |
}
|
|
137 |
if (hs.size() != 2) {
|
|
138 |
throw
|
|
139 |
new Exception("java.lang.ClassLoader.getSystemResources()"+
|
|
140 |
" did not find all resources");
|
|
141 |
}
|
|
142 |
}
|
|
143 |
}
|
|
144 |
|
|
145 |
private static void report(String s) {
|
|
146 |
System.out.println("Testing java.lang.ClassLoader." + s + " ...");
|
|
147 |
}
|
|
148 |
}
|