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