View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests for the PathableTestSuite and PathableClassLoader functionality,
35   * where lookup order for the PathableClassLoader is child-first.
36   * <p>
37   * These tests assume:
38   * <ul>
39   * <li>junit is in system classpath
40   * <li>nothing else is in system classpath
41   * </ul>
42   */
43  
44  public class ChildFirstTestCase extends TestCase {
45  
46      /**
47       * Sets up a custom class loader hierarchy for this test case.
48       * The hierarchy is:
49       * <ul>
50       * <li> contextloader: child-first.
51       * <li> childloader: child-first, used to load test case.
52       * <li> parentloader: child-first, parent is the bootclass loader.
53       * </ul>
54       */
55      public static Test suite() throws Exception {
56          final Class thisClass = ChildFirstTestCase.class;
57          final ClassLoader thisClassLoader = thisClass.getClassLoader();
58  
59          // Make the parent a direct child of the bootloader to hide all
60          // other classes in the system classpath
61          final PathableClassLoader parent = new PathableClassLoader(null);
62          parent.setParentFirst(false);
63  
64          // Make the junit classes visible as a special case, as junit
65          // won't be able to call this class at all without this. The
66          // junit classes must be visible from the class loader that loaded
67          // this class, so use that as the source for future access to classes
68          // from the junit package.
69          parent.useExplicitLoader("junit.", thisClassLoader);
70          parent.useExplicitLoader("org.junit.", thisClassLoader);
71  
72          // Make the commons-logging.jar classes visible via the parent
73          parent.addLogicalLib("commons-logging");
74  
75          // Create a child class loader to load the test case through
76          final PathableClassLoader child = new PathableClassLoader(parent);
77          child.setParentFirst(false);
78  
79          // Obviously, the child class loader needs to have the test classes
80          // in its path!
81          child.addLogicalLib("testclasses");
82          child.addLogicalLib("commons-logging-adapters");
83  
84          // Create a third class loader to be the context class loader.
85          final PathableClassLoader context = new PathableClassLoader(child);
86          context.setParentFirst(false);
87  
88          // reload this class via the child class loader
89          final Class testClass = child.loadClass(thisClass.getName());
90  
91          // and return our custom TestSuite class
92          return new PathableTestSuite(testClass, context);
93      }
94  
95      /**
96       * Utility method to convert an enumeration-of-URLs into an array of URLs.
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      * Utility method to return the set of all class loaders in the
109      * parent chain starting from the one that loaded the class for
110      * this object instance.
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      * Test that the class loader hierarchy is as expected, and that
124      * calling loadClass() on various class loaders works as expected.
125      * Note that for this test case, parent-first classloading is
126      * in effect.
127      */
128     public void testPaths() throws Exception {
129         // the context class loader is not expected to be null
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         // the class loader that loaded this class is obviously not null
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         // the suite method specified that the context class loader's parent
144         // is the loader that loaded this test case.
145         assertSame("Context class loader is not child of thisLoader",
146                 thisLoader, contextLoader.getParent());
147 
148         // thisLoader's parent should be available
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         // parent should have a parent of null
156         assertNull("Parent class loader has non-null parent", parentLoader.getParent());
157 
158         // getSystemClassloader is not a PathableClassLoader; it's of a
159         // built-in type. This also verifies that system class loader is none of
160         // (context, child, parent).
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         // junit classes should be visible; their class loader is not
168         // in the hierarchy of parent class loaders for this class,
169         // though it is accessible due to trickery in the PathableClassLoader.
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         // jcl api classes should be visible only via the parent
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         // jcl adapter classes should be visible via both parent and child. However
181         // as the class loaders are child-first we should see the child one.
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         // test classes should be visible via the child only
187         final Class testClass = contextLoader.loadClass("org.apache.commons.logging.PathableTestSuite");
188         assertSame("PathableTestSuite not loaded via child",
189                 testClass.getClassLoader(), thisLoader);
190 
191         // test loading of class that is not available
192         try {
193             final Class noSuchClass = contextLoader.loadClass("no.such.class");
194             fail("Class no.such.class is unexpectedly available");
195             assertNotNull(noSuchClass); // silence warning about unused var
196         } catch (final ClassNotFoundException ex) {
197             // ok
198         }
199 
200         // String class class loader is null
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      * Test that the various flavors of ClassLoader.getResource work as expected.
208      */
209     public void testResource() {
210         URL resource;
211 
212         final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
213         final ClassLoader childLoader = contextLoader.getParent();
214 
215         // getResource where it doesn't exist
216         resource = childLoader.getResource("nosuchfile");
217         assertNull("Non-null URL returned for invalid resource name", resource);
218 
219         // getResource where it is accessible only to parent class loader
220         resource = childLoader.getResource("org/apache/commons/logging/Log.class");
221         assertNotNull("Unable to locate Log.class resource", resource);
222 
223         // getResource where it is accessible only to child class loader
224         resource = childLoader.getResource("org/apache/commons/logging/PathableTestSuite.class");
225         assertNotNull("Unable to locate PathableTestSuite.class resource", resource);
226 
227         // getResource where it is accessible to both class loaders. The one visible
228         // to the child should be returned. The URL returned will be of form
229         //  jar:file:/x/y.jar!path/to/resource. The file name part should include the jarname
230         // of form commons-logging-adapters-nnnn.jar, not commons-logging-nnnn.jar
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      * Test that getResourceAsStream works.
239      */
240     public void testResourceAsStream() throws Exception {
241         java.io.InputStream is;
242 
243         // verify the class loader hierarchy
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         // getResourceAsStream where no instances exist
251         is = childLoader.getResourceAsStream("nosuchfile");
252         assertNull("Invalid resource returned non-null stream", is);
253 
254         // getResourceAsStream where resource does exist
255         is = childLoader.getResourceAsStream("org/apache/commons/logging/Log.class");
256         assertNotNull("Null returned for valid resource", is);
257         is.close();
258 
259         // It would be nice to test parent-first ordering here, but that would require
260         // having a resource with the same name in both the parent and child loaders,
261         // but with different contents. That's a little tricky to set up so we'll
262         // skip that for now.
263     }
264 
265     /**
266      * Test that the various flavors of ClassLoader.getResources work as expected.
267      */
268     public void testResources() throws Exception {
269         Enumeration<URL> resources;
270         URL[] urls;
271 
272         // verify the class loader hierarchy
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         // getResources where no instances exist
280         resources = childLoader.getResources("nosuchfile");
281         urls = toURLArray(resources);
282         assertEquals("Non-null URL returned for invalid resource name", 0, urls.length);
283 
284         // getResources where the resource only exists in the parent
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         // getResources where the resource only exists in the child
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         // getResources where the resource exists in both.
295         // resources should be returned in order (child-resource, parent-resource).
296         //
297         // IMPORTANT: due to the fact that in java 1.4 and earlier method
298         // ClassLoader.getResources is final it isn't possible for PathableClassLoader
299         // to override this. So even when child-first is enabled the resource order
300         // is still (parent-resources, child-resources). This test verifies the expected
301         // behavior - even though it's not the desired behavior.
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         // There is no guarantee about the ordering of results returned from getResources
308         // To make this test portable across JVMs, sort the string to give them a known order
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 }