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 TcclDisabledTestCase extends TestCase { 36 37 public static final String MY_LOG_FACTORY_PKG = 38 "org.apache.commons.logging.tccl.custom"; 39 40 public static final String MY_LOG_FACTORY_IMPL = 41 MY_LOG_FACTORY_PKG + ".MyLogFactoryImpl"; 42 43 45 46 49 public static Test suite() throws Exception { 50 Class thisClass = TcclDisabledTestCase.class; 51 52 PathableClassLoader dummy = new PathableClassLoader(null); 57 dummy.useSystemLoader("junit."); 58 dummy.addLogicalLib("testclasses"); 59 dummy.addLogicalLib("commons-logging"); 60 61 String thisClassPath = thisClass.getName().replace('.', '/') + ".class"; 62 URL baseUrl = dummy.findResource(thisClassPath); 63 64 PathableClassLoader emptyLoader = new PathableClassLoader(null); 72 73 PathableClassLoader parentLoader = new PathableClassLoader(null); 74 parentLoader.useSystemLoader("junit."); 75 parentLoader.addLogicalLib("commons-logging"); 76 parentLoader.addLogicalLib("testclasses"); 77 parentLoader.useExplicitLoader( 80 MY_LOG_FACTORY_PKG + ".", emptyLoader); 81 82 URL propsEnableUrl = new URL (baseUrl, "props_disable_tccl/"); 83 parentLoader.addURL(propsEnableUrl); 84 85 PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader); 86 tcclLoader.addLogicalLib("testclasses"); 87 88 Class testClass = parentLoader.loadClass(thisClass.getName()); 89 return new PathableTestSuite(testClass, tcclLoader); 90 } 91 92 95 public void setUp() throws Exception { 96 LogFactory.releaseAll(); 97 } 98 99 102 public void tearDown() { 103 LogFactory.releaseAll(); 104 } 105 106 108 111 public void testLoader() throws Exception { 112 113 ClassLoader thisClassLoader = this.getClass().getClassLoader(); 114 ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader(); 115 116 assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader); 118 119 try { 121 Class clazz = thisClassLoader.loadClass(MY_LOG_FACTORY_IMPL); 122 fail("Unexpectedly able to load MyLogFactoryImpl via test class classloader"); 123 } catch(ClassNotFoundException ex) { 124 } 126 127 try { 129 Class clazz = tcclLoader.loadClass(MY_LOG_FACTORY_IMPL); 130 } catch(ClassNotFoundException ex) { 131 fail("Unexpectedly unable to load MyLogFactoryImpl via tccl classloader"); 132 } 133 } 134 135 141 public void testTcclLoading() throws Exception { 142 try { 143 LogFactory instance = LogFactory.getFactory(); 144 fail("Unexpectedly succeeded in loading custom factory, though TCCL disabled."); 145 } catch(org.apache.commons.logging.LogConfigurationException ex) { 146 int index = ex.getMessage().indexOf(MY_LOG_FACTORY_IMPL); 149 assertTrue("MylogFactoryImpl not found", index >= 0); 150 } 151 } 152 } 153 | Popular Tags |