diff --git a/src/test/org/apache/commons/logging/PathableClassLoader.java b/src/test/org/apache/commons/logging/PathableClassLoader.java index 00f92c9..2de1ce0 100644 --- a/src/test/org/apache/commons/logging/PathableClassLoader.java +++ b/src/test/org/apache/commons/logging/PathableClassLoader.java @@ -31,12 +31,24 @@ import java.io.InputStream; import java.io.IOException; /** - * A ClassLoader which sees only the specified classes. + * A ClassLoader which sees only specified classes, and which can be + * set to do parent-first or child-first path lookup. *

* Note that this classloader is not "industrial strength"; users * looking for such a class may wish to look at the Tomcat sourcecode * instead. In particular, this class may not be threadsafe. + *

+ * Note that the ClassLoader.getResources method isn't overloaded here. + * It would be nice to ensure that when child-first lookup is set the + * resources from the child are returned earlier in the list than the + * resources from the parent. However overriding this method isn't possible + * as the java 1.4 version of ClassLoader declares this method final + * (though the java 1.5 version has removed the final qualifier). As the + * ClassLoader javadoc doesn't specify the order in which resources + * are returned, it's valid to return the resources in any order (just + * untidy) so the inherited implementation is technically ok. */ + public class PathableClassLoader extends URLClassLoader { private static final URL[] NO_URLS = new URL[0]; @@ -239,44 +251,4 @@ public class PathableClassLoader extends URLClassLoader { return super.getResourceAsStream(name); } } - - /** - * Same as parent class method except that when parentFirst is false - * the resources available from this class are returned at the head of - * the list instead of the tail. - */ - public Enumeration getResources(String name) throws IOException { - if (parentFirst) { - return super.getResources(name); - } else { - Enumeration localResources = super.findResources(name); - ClassLoader parentLoader = getParent(); - if (parentLoader == null) { - // There is no way, as far as I am aware, to call - // getResources on the bootclassloader. The Class - // class has methods getResource and getResourceAsStream - // but not getResources. So I guess we just assume there - // aren't any matches in the bootloader.. - return localResources; - } - Enumeration parentResources = parentLoader.getResources(name); - - if (!localResources.hasMoreElements()) { - return parentResources; - } - - if (!parentResources.hasMoreElements()) { - return localResources; - } - - Vector v = new Vector(); - while (localResources.hasMoreElements()) { - v.add(localResources.nextElement()); - } - while (parentResources.hasMoreElements()) { - v.add(parentResources.nextElement()); - } - return v.elements(); - } - } } diff --git a/src/test/org/apache/commons/logging/pathable/ChildFirstTestCase.java b/src/test/org/apache/commons/logging/pathable/ChildFirstTestCase.java index 124d130..edca660 100644 --- a/src/test/org/apache/commons/logging/pathable/ChildFirstTestCase.java +++ b/src/test/org/apache/commons/logging/pathable/ChildFirstTestCase.java @@ -230,15 +230,21 @@ public class ChildFirstTestCase extends TestCase { assertEquals("Unexpected number of PathableTestSuite.class resources found", 1, urls.length); // getResources where the resource exists in both. - // resources should be returned in order (child-resource, parent-resource) + // resources should be returned in order (child-resource, parent-resource). + // + // IMPORTANT: due to the fact that in java 1.4 and earlier method + // ClassLoader.getResources is final it isn't possible for PathableClassLoader + // to override this. So even when child-first is enabled the resource order + // is still (parent-resources, child-resources). This test verifies the expected + // behaviour - even though it's not the desired behaviour. + resources = childLoader.getResources("org/apache/commons/logging/impl/Log4J12Logger.class"); urls = toURLArray(resources); assertEquals("Unexpected number of Log4J12Logger.class resources found", 2, urls.length); assertTrue("Incorrect source for Log4J12Logger class", - urls[0].toString().indexOf("/commons-logging-adapters-1.") > 0); + urls[0].toString().indexOf("/commons-logging-1.") > 0); assertTrue("Incorrect source for Log4J12Logger class", - urls[1].toString().indexOf("/commons-logging-1.") > 0); - + urls[1].toString().indexOf("/commons-logging-adapters-1.") > 0); } /**