1 16 package org.apache.commons.logging.pathable; 17 18 import java.net.URL ; 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.Enumeration ; 22 23 import junit.framework.Test; 24 import junit.framework.TestCase; 25 26 import org.apache.commons.logging.PathableClassLoader; 27 import org.apache.commons.logging.PathableTestSuite; 28 29 39 40 public class ParentFirstTestCase extends TestCase { 41 42 51 public static Test suite() throws Exception { 52 Class thisClass = ParentFirstTestCase.class; 53 ClassLoader thisClassLoader = thisClass.getClassLoader(); 54 55 PathableClassLoader parent = new PathableClassLoader(null); 58 59 parent.useExplicitLoader("junit.", thisClassLoader); 65 66 parent.addLogicalLib("commons-logging"); 68 69 PathableClassLoader child = new PathableClassLoader(parent); 71 72 child.addLogicalLib("testclasses"); 75 child.addLogicalLib("commons-logging-adapters"); 76 77 PathableClassLoader context = new PathableClassLoader(child); 79 80 Class testClass = child.loadClass(thisClass.getName()); 82 83 return new PathableTestSuite(testClass, context); 85 } 86 87 93 public void testPaths() throws Exception { 94 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); 96 assertNotNull("Context classloader is null", contextLoader); 97 assertEquals("Context classloader has unexpected type", 98 PathableClassLoader.class.getName(), 99 contextLoader.getClass().getName()); 100 101 ClassLoader thisLoader = this.getClass().getClassLoader(); 103 assertNotNull("thisLoader is null", thisLoader); 104 assertEquals("thisLoader has unexpected type", 105 PathableClassLoader.class.getName(), 106 thisLoader.getClass().getName()); 107 108 assertSame("Context classloader is not child of thisLoader", 111 thisLoader, contextLoader.getParent()); 112 113 ClassLoader parentLoader = thisLoader.getParent(); 115 assertNotNull("Parent classloader is null", parentLoader); 116 assertEquals("Parent classloader has unexpected type", 117 PathableClassLoader.class.getName(), 118 parentLoader.getClass().getName()); 119 120 assertNull("Parent classloader has non-null parent", parentLoader.getParent()); 122 123 ClassLoader systemLoader = ClassLoader.getSystemClassLoader(); 127 assertNotNull("System classloader is null", systemLoader); 128 assertFalse("System classloader has unexpected type", 129 PathableClassLoader.class.getName().equals( 130 systemLoader.getClass().getName())); 131 132 Class junitTest = contextLoader.loadClass("junit.framework.Test"); 135 assertSame("Junit not loaded via systemloader", 136 systemLoader, junitTest.getClassLoader()); 137 138 Class logClass = contextLoader.loadClass("org.apache.commons.logging.Log"); 140 assertSame("Log class not loaded via parent", 141 logClass.getClassLoader(), parentLoader); 142 143 Class log4jClass = contextLoader.loadClass("org.apache.commons.logging.impl.Log4JLogger"); 146 assertSame("Log4JLogger not loaded via parent", 147 log4jClass.getClassLoader(), parentLoader); 148 149 Class testClass = contextLoader.loadClass("org.apache.commons.logging.PathableTestSuite"); 151 assertSame("PathableTestSuite not loaded via child", 152 testClass.getClassLoader(), thisLoader); 153 154 try { 156 Class noSuchClass = contextLoader.loadClass("no.such.class"); 157 fail("Class no.such.class is unexpectedly available"); 158 } catch(ClassNotFoundException ex) { 159 } 161 162 Class stringClass = contextLoader.loadClass("java.lang.String"); 164 assertNull("String class classloader is not null!", 165 stringClass.getClassLoader()); 166 } 167 168 171 public void testResource() { 172 URL resource; 173 174 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); 175 ClassLoader childLoader = contextLoader.getParent(); 176 177 resource = childLoader.getResource("nosuchfile"); 179 assertNull("Non-null URL returned for invalid resource name", resource); 180 181 resource = childLoader.getResource("org/apache/commons/logging/Log.class"); 183 assertNotNull("Unable to locate Log.class resource", resource); 184 185 resource = childLoader.getResource("org/apache/commons/logging/PathableTestSuite.class"); 187 assertNotNull("Unable to locate PathableTestSuite.class resource", resource); 188 189 resource = childLoader.getResource("org/apache/commons/logging/impl/Log4JLogger.class"); 194 assertNotNull("Unable to locate Log4JLogger.class resource", resource); 195 assertTrue("Incorrect source for Log4JLogger class", 196 resource.toString().indexOf("/commons-logging-1.") > 0); 197 } 198 199 202 public void testResources() throws Exception { 203 Enumeration resources; 204 URL [] urls; 205 206 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); 208 ClassLoader childLoader = contextLoader.getParent(); 209 ClassLoader parentLoader = childLoader.getParent(); 210 ClassLoader bootLoader = parentLoader.getParent(); 211 assertNull("Unexpected classloader hierarchy", bootLoader); 212 213 resources = childLoader.getResources("nosuchfile"); 215 urls = toURLArray(resources); 216 assertEquals("Non-null URL returned for invalid resource name", 0, urls.length); 217 218 resources = childLoader.getResources("org/apache/commons/logging/Log.class"); 220 urls = toURLArray(resources); 221 assertEquals("Unexpected number of Log.class resources found", 1, urls.length); 222 223 resources = childLoader.getResources("org/apache/commons/logging/PathableTestSuite.class"); 225 urls = toURLArray(resources); 226 assertEquals("Unexpected number of PathableTestSuite.class resources found", 1, urls.length); 227 228 resources = childLoader.getResources("org/apache/commons/logging/impl/Log4JLogger.class"); 231 urls = toURLArray(resources); 232 assertEquals("Unexpected number of Log4JLogger.class resources found", 2, urls.length); 233 234 String [] urlsToStrings = new String [2]; 237 urlsToStrings[0] = urls[0].toString(); 238 urlsToStrings[1] = urls[1].toString(); 239 Arrays.sort(urlsToStrings); 240 assertTrue("Incorrect source for Log4JLogger class", 241 urlsToStrings[0].indexOf("/commons-logging-1.") > 0); 242 assertTrue("Incorrect source for Log4JLogger class", 243 urlsToStrings[1].indexOf("/commons-logging-adapters-1.") > 0); 244 245 } 246 247 250 private static URL [] toURLArray(Enumeration e) { 251 ArrayList l = new ArrayList (); 252 while (e.hasMoreElements()) { 253 URL u = (URL ) e.nextElement(); 254 l.add(u); 255 } 256 URL [] tmp = new URL [l.size()]; 257 return (URL []) l.toArray(tmp); 258 } 259 260 263 public void testResourceAsStream() throws Exception { 264 java.io.InputStream is; 265 266 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); 268 ClassLoader childLoader = contextLoader.getParent(); 269 ClassLoader parentLoader = childLoader.getParent(); 270 ClassLoader bootLoader = parentLoader.getParent(); 271 assertNull("Unexpected classloader hierarchy", bootLoader); 272 273 is = childLoader.getResourceAsStream("nosuchfile"); 275 assertNull("Invalid resource returned non-null stream", is); 276 277 is = childLoader.getResourceAsStream("org/apache/commons/logging/Log.class"); 279 assertNotNull("Null returned for valid resource", is); 280 is.close(); 281 282 } 287 } 288 | Popular Tags |