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