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