1 16 17 package org.apache.commons.logging.tccl.log; 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.Log; 26 import org.apache.commons.logging.LogConfigurationException; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.commons.logging.PathableClassLoader; 29 import org.apache.commons.logging.PathableTestSuite; 30 31 32 35 36 public class TcclDisabledTestCase extends TestCase { 37 38 public static final String MY_LOG_PKG = 39 "org.apache.commons.logging.tccl.custom"; 40 41 public static final String MY_LOG_IMPL = 42 MY_LOG_PKG + ".MyLog"; 43 44 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); 71 72 PathableClassLoader parentLoader = new PathableClassLoader(null); 73 parentLoader.useSystemLoader("junit."); 74 parentLoader.addLogicalLib("commons-logging"); 75 parentLoader.addLogicalLib("testclasses"); 76 parentLoader.useExplicitLoader(MY_LOG_PKG + ".", emptyLoader); 79 80 URL propsEnableUrl = new URL (baseUrl, "props_disable_tccl/"); 81 parentLoader.addURL(propsEnableUrl); 82 83 PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader); 84 tcclLoader.addLogicalLib("testclasses"); 85 86 Class testClass = parentLoader.loadClass(thisClass.getName()); 87 return new PathableTestSuite(testClass, tcclLoader); 88 } 89 90 93 public void setUp() throws Exception { 94 LogFactory.releaseAll(); 95 } 96 97 100 public void tearDown() { 101 LogFactory.releaseAll(); 102 } 103 104 106 109 public void testLoader() throws Exception { 110 111 ClassLoader thisClassLoader = this.getClass().getClassLoader(); 112 ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader(); 113 114 assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader); 116 117 try { 119 Class clazz = thisClassLoader.loadClass(MY_LOG_IMPL); 120 fail("Unexpectedly able to load MyLog via test class classloader"); 121 } catch(ClassNotFoundException ex) { 122 } 124 125 try { 127 Class clazz = tcclLoader.loadClass(MY_LOG_IMPL); 128 } catch(ClassNotFoundException ex) { 129 fail("Unexpectedly unable to load MyLog via tccl classloader"); 130 } 131 } 132 133 139 public void testTcclLoading() throws Exception { 140 LogFactory instance = LogFactory.getFactory(); 141 assertEquals( 142 "Correct LogFactory loaded", 143 "org.apache.commons.logging.impl.LogFactoryImpl", 144 instance.getClass().getName()); 145 146 try { 147 Log log = instance.getLog("test"); 148 fail("Unexpectedly succeeded in loading a custom Log class" 149 + " that is only accessable via the tccl."); 150 } catch(LogConfigurationException ex) { 151 int index = ex.getMessage().indexOf(MY_LOG_IMPL); 153 assertTrue("MyLog not found", index >= 0); 154 } 155 } 156 } 157 | Popular Tags |