1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.logging.pathable;
18
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Enumeration;
23 import java.util.HashSet;
24 import java.util.Set;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28
29 import org.apache.commons.logging.PathableClassLoader;
30 import org.apache.commons.logging.PathableTestSuite;
31
32
33
34
35
36
37
38
39
40
41
42
43 public class ParentFirstTestCase extends TestCase {
44
45
46
47
48
49
50
51
52
53
54 public static Test suite() throws Exception {
55 Class thisClass = ParentFirstTestCase.class;
56 ClassLoader thisClassLoader = thisClass.getClassLoader();
57
58
59
60 PathableClassLoader parent = new PathableClassLoader(null);
61
62
63
64
65
66
67 parent.useExplicitLoader("junit.", thisClassLoader);
68
69
70 parent.addLogicalLib("commons-logging");
71
72
73 PathableClassLoader child = new PathableClassLoader(parent);
74
75
76
77 child.addLogicalLib("testclasses");
78 child.addLogicalLib("commons-logging-adapters");
79
80
81 PathableClassLoader context = new PathableClassLoader(child);
82
83
84 Class testClass = child.loadClass(thisClass.getName());
85
86
87 return new PathableTestSuite(testClass, context);
88 }
89
90
91
92
93
94
95 private Set getAncestorCLs() {
96 Set s = new HashSet();
97 ClassLoader cl = this.getClass().getClassLoader();
98 while (cl != null) {
99 s.add(cl);
100 cl = cl.getParent();
101 }
102 return s;
103 }
104
105
106
107
108
109
110
111 public void testPaths() throws Exception {
112
113 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
114 assertNotNull("Context classloader is null", contextLoader);
115 assertEquals("Context classloader has unexpected type",
116 PathableClassLoader.class.getName(),
117 contextLoader.getClass().getName());
118
119
120 ClassLoader thisLoader = this.getClass().getClassLoader();
121 assertNotNull("thisLoader is null", thisLoader);
122 assertEquals("thisLoader has unexpected type",
123 PathableClassLoader.class.getName(),
124 thisLoader.getClass().getName());
125
126
127
128 assertSame("Context classloader is not child of thisLoader",
129 thisLoader, contextLoader.getParent());
130
131
132 ClassLoader parentLoader = thisLoader.getParent();
133 assertNotNull("Parent classloader is null", parentLoader);
134 assertEquals("Parent classloader has unexpected type",
135 PathableClassLoader.class.getName(),
136 parentLoader.getClass().getName());
137
138
139 assertNull("Parent classloader has non-null parent", parentLoader.getParent());
140
141
142
143
144 ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
145 assertNotNull("System classloader is null", systemLoader);
146 assertFalse("System classloader has unexpected type",
147 PathableClassLoader.class.getName().equals(
148 systemLoader.getClass().getName()));
149
150
151
152
153 Class junitTest = contextLoader.loadClass("junit.framework.Test");
154 Set ancestorCLs = getAncestorCLs();
155 assertFalse("Junit not loaded by ancestor classloader",
156 ancestorCLs.contains(junitTest.getClassLoader()));
157
158
159 Class logClass = contextLoader.loadClass("org.apache.commons.logging.Log");
160 assertSame("Log class not loaded via parent",
161 logClass.getClassLoader(), parentLoader);
162
163
164
165 Class log4jClass = contextLoader.loadClass("org.apache.commons.logging.impl.Log4JLogger");
166 assertSame("Log4JLogger not loaded via parent",
167 log4jClass.getClassLoader(), parentLoader);
168
169
170 Class testClass = contextLoader.loadClass("org.apache.commons.logging.PathableTestSuite");
171 assertSame("PathableTestSuite not loaded via child",
172 testClass.getClassLoader(), thisLoader);
173
174
175 try {
176 Class noSuchClass = contextLoader.loadClass("no.such.class");
177 fail("Class no.such.class is unexpectedly available");
178 assertNotNull(noSuchClass);
179 } catch(ClassNotFoundException ex) {
180
181 }
182
183
184 Class stringClass = contextLoader.loadClass("java.lang.String");
185 assertNull("String class classloader is not null!",
186 stringClass.getClassLoader());
187 }
188
189
190
191
192 public void testResource() {
193 URL resource;
194
195 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
196 ClassLoader childLoader = contextLoader.getParent();
197
198
199 resource = childLoader.getResource("nosuchfile");
200 assertNull("Non-null URL returned for invalid resource name", resource);
201
202
203 resource = childLoader.getResource("org/apache/commons/logging/Log.class");
204 assertNotNull("Unable to locate Log.class resource", resource);
205
206
207 resource = childLoader.getResource("org/apache/commons/logging/PathableTestSuite.class");
208 assertNotNull("Unable to locate PathableTestSuite.class resource", resource);
209
210
211
212
213
214 resource = childLoader.getResource("org/apache/commons/logging/impl/Log4JLogger.class");
215 assertNotNull("Unable to locate Log4JLogger.class resource", resource);
216 assertTrue("Incorrect source for Log4JLogger class",
217 resource.toString().indexOf("/commons-logging-1.") > 0);
218 }
219
220
221
222
223 public void testResources() throws Exception {
224 Enumeration resources;
225 URL[] urls;
226
227
228 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
229 ClassLoader childLoader = contextLoader.getParent();
230 ClassLoader parentLoader = childLoader.getParent();
231 ClassLoader bootLoader = parentLoader.getParent();
232 assertNull("Unexpected classloader hierarchy", bootLoader);
233
234
235 resources = childLoader.getResources("nosuchfile");
236 urls = toURLArray(resources);
237 assertEquals("Non-null URL returned for invalid resource name", 0, urls.length);
238
239
240 resources = childLoader.getResources("org/apache/commons/logging/Log.class");
241 urls = toURLArray(resources);
242 assertEquals("Unexpected number of Log.class resources found", 1, urls.length);
243
244
245 resources = childLoader.getResources("org/apache/commons/logging/PathableTestSuite.class");
246 urls = toURLArray(resources);
247 assertEquals("Unexpected number of PathableTestSuite.class resources found", 1, urls.length);
248
249
250
251 resources = childLoader.getResources("org/apache/commons/logging/impl/Log4JLogger.class");
252 urls = toURLArray(resources);
253 assertEquals("Unexpected number of Log4JLogger.class resources found", 2, urls.length);
254
255
256
257 String[] urlsToStrings = new String[2];
258 urlsToStrings[0] = urls[0].toString();
259 urlsToStrings[1] = urls[1].toString();
260 Arrays.sort(urlsToStrings);
261 assertTrue("Incorrect source for Log4JLogger class",
262 urlsToStrings[0].indexOf("/commons-logging-1.") > 0);
263 assertTrue("Incorrect source for Log4JLogger class",
264 urlsToStrings[1].indexOf("/commons-logging-adapters-1.") > 0);
265
266 }
267
268
269
270
271 private static URL[] toURLArray(Enumeration e) {
272 ArrayList l = new ArrayList();
273 while (e.hasMoreElements()) {
274 URL u = (URL) e.nextElement();
275 l.add(u);
276 }
277 URL[] tmp = new URL[l.size()];
278 return (URL[]) l.toArray(tmp);
279 }
280
281
282
283
284 public void testResourceAsStream() throws Exception {
285 java.io.InputStream is;
286
287
288 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
289 ClassLoader childLoader = contextLoader.getParent();
290 ClassLoader parentLoader = childLoader.getParent();
291 ClassLoader bootLoader = parentLoader.getParent();
292 assertNull("Unexpected classloader hierarchy", bootLoader);
293
294
295 is = childLoader.getResourceAsStream("nosuchfile");
296 assertNull("Invalid resource returned non-null stream", is);
297
298
299 is = childLoader.getResourceAsStream("org/apache/commons/logging/Log.class");
300 assertNotNull("Null returned for valid resource", is);
301 is.close();
302
303
304
305
306
307 }
308 }