1 16 17 package org.apache.commons.logging.tccl.logfactory; 18 19 20 import java.net.URL ; 21 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.commons.logging.PathableClassLoader; 27 import org.apache.commons.logging.PathableTestSuite; 28 29 30 34 35 public class TcclEnabledTestCase extends TestCase { 36 37 39 40 43 public static Test suite() throws Exception { 44 Class thisClass = TcclEnabledTestCase.class; 45 46 PathableClassLoader dummy = new PathableClassLoader(null); 51 dummy.useSystemLoader("junit."); 52 dummy.addLogicalLib("testclasses"); 53 dummy.addLogicalLib("commons-logging"); 54 55 String thisClassPath = thisClass.getName().replace('.', '/') + ".class"; 56 URL baseUrl = dummy.findResource(thisClassPath); 57 58 PathableClassLoader emptyLoader = new PathableClassLoader(null); 66 67 PathableClassLoader parentLoader = new PathableClassLoader(null); 68 parentLoader.useSystemLoader("junit."); 69 parentLoader.addLogicalLib("commons-logging"); 70 parentLoader.addLogicalLib("testclasses"); 71 parentLoader.useExplicitLoader( 74 "org.apache.commons.logging.tccl.custom.", emptyLoader); 75 76 URL propsEnableUrl = new URL (baseUrl, "props_enable_tccl/"); 77 parentLoader.addURL(propsEnableUrl); 78 79 PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader); 80 tcclLoader.addLogicalLib("testclasses"); 81 82 Class testClass = parentLoader.loadClass(thisClass.getName()); 83 return new PathableTestSuite(testClass, tcclLoader); 84 } 85 86 89 public void setUp() throws Exception { 90 LogFactory.releaseAll(); 91 } 92 93 96 public void tearDown() { 97 LogFactory.releaseAll(); 98 } 99 100 102 105 public void testLoader() throws Exception { 106 107 ClassLoader thisClassLoader = this.getClass().getClassLoader(); 108 ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader(); 109 110 assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader); 112 113 try { 115 Class clazz = thisClassLoader.loadClass( 116 "org.apache.commons.logging.tccl.custom.MyLogFactoryImpl"); 117 fail("Unexpectedly able to load MyLogFactoryImpl via test class classloader"); 118 } catch(ClassNotFoundException ex) { 119 } 121 122 try { 124 Class clazz = tcclLoader.loadClass( 125 "org.apache.commons.logging.tccl.custom.MyLogFactoryImpl"); 126 } catch(ClassNotFoundException ex) { 127 fail("Unexpectedly unable to load MyLogFactoryImpl via tccl classloader"); 128 } 129 } 130 131 136 public void testTcclLoading() throws Exception { 137 LogFactory instance = LogFactory.getFactory(); 138 139 assertEquals( 140 "Correct LogFactory loaded", 141 "org.apache.commons.logging.tccl.custom.MyLogFactoryImpl", 142 instance.getClass().getName()); 143 } 144 } 145 | Popular Tags |