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