Author: skitching Date: Tue Jan 17 19:42:52 2006 New Revision: 370031 URL: http://svn.apache.org/viewcvs?rev=370031&view=rev Log: Add tests for new feature to disable loading of Log implementations via the TCCL. Move existing tests for disabling loading of LogFactory via TCCL to subdir "logfactory".
Added: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/custom/MyLog.java (with props) jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclDisabledTestCase.java (with props) jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclEnabledTestCase.java (with props) jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_disable_tccl/ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_disable_tccl/commons-logging.properties jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_enable_tccl/ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_enable_tccl/commons-logging.properties jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/TcclDisabledTestCase.java - copied, changed from r369755, jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/TcclDisabledTestCase.java jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/TcclEnabledTestCase.java - copied, changed from r369747, jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/TcclEnabledTestCase.java jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/props_disable_tccl/ - copied from r369747, jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/props_disable_tccl/ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/props_disable_tccl/commons-logging.properties - copied unchanged from r369755, jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/props_disable_tccl/commons-logging.properties jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/props_enable_tccl/ - copied from r369747, jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/props_enable_tccl/ Removed: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/TcclDisabledTestCase.java jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/TcclEnabledTestCase.java jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/props_disable_tccl/ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/props_enable_tccl/ Added: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/custom/MyLog.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/custom/MyLog.java?rev=370031&view=auto ============================================================================== --- jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/custom/MyLog.java (added) +++ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/custom/MyLog.java Tue Jan 17 19:42:52 2006 @@ -0,0 +1,43 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.logging.tccl.custom; + +import org.apache.commons.logging.Log; + +public class MyLog implements Log { + + public MyLog(String category) {} + + public boolean isDebugEnabled() { return false; } + public boolean isErrorEnabled() { return false; } + public boolean isFatalEnabled() { return false; } + public boolean isInfoEnabled() { return false; } + public boolean isTraceEnabled() { return false; } + public boolean isWarnEnabled() { return false; } + + public void trace(Object message) {} + public void trace(Object message, Throwable t) {} + public void debug(Object message) {} + public void debug(Object message, Throwable t) {} + public void info(Object message) {} + public void info(Object message, Throwable t) {} + public void warn(Object message) {} + public void warn(Object message, Throwable t) {} + public void error(Object message) {} + public void error(Object message, Throwable t) {} + public void fatal(Object message) {} + public void fatal(Object message, Throwable t) {} +} Propchange: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/custom/MyLog.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/custom/MyLog.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclDisabledTestCase.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclDisabledTestCase.java?rev=370031&view=auto ============================================================================== --- jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclDisabledTestCase.java (added) +++ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclDisabledTestCase.java Tue Jan 17 19:42:52 2006 @@ -0,0 +1,156 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.logging.tccl.log; + + +import java.net.URL; + +import junit.framework.Test; +import junit.framework.TestCase; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogConfigurationException; +import org.apache.commons.logging.LogFactory; +import org.apache.commons.logging.PathableClassLoader; +import org.apache.commons.logging.PathableTestSuite; + + +/** + * Verify that by default LogFactoryImpl is loaded from the tccl classloader. + */ + +public class TcclDisabledTestCase extends TestCase { + + public static final String MY_LOG_PKG = + "org.apache.commons.logging.tccl.custom"; + + public static final String MY_LOG_IMPL = + MY_LOG_PKG + ".MyLog"; + + // ------------------------------------------- JUnit Infrastructure Methods + + /** + * Return the tests included in this test suite. + */ + public static Test suite() throws Exception { + Class thisClass = TcclDisabledTestCase.class; + + // Determine the URL to this .class file, so that we can then + // append the priority dirs to it. For tidiness, load this + // class through a dummy loader though this is not absolutely + // necessary... + PathableClassLoader dummy = new PathableClassLoader(null); + dummy.useSystemLoader("junit."); + dummy.addLogicalLib("testclasses"); + dummy.addLogicalLib("commons-logging"); + + String thisClassPath = thisClass.getName().replace('.', '/') + ".class"; + URL baseUrl = dummy.findResource(thisClassPath); + + // Now set up the desired classloader hierarchy. Everything goes into + // the parent classpath, but we exclude the custom Log class. + // + // We then create a tccl classloader that can see the custom + // Log class. Therefore if that class can be found, then the + // TCCL must have been used to load it. + PathableClassLoader emptyLoader = new PathableClassLoader(null); + + PathableClassLoader parentLoader = new PathableClassLoader(null); + parentLoader.useSystemLoader("junit."); + parentLoader.addLogicalLib("commons-logging"); + parentLoader.addLogicalLib("testclasses"); + // hack to ensure that the testcase classloader can't see + // the custom MyLog + parentLoader.useExplicitLoader(MY_LOG_PKG + ".", emptyLoader); + + URL propsEnableUrl = new URL(baseUrl, "props_disable_tccl/"); + parentLoader.addURL(propsEnableUrl); + + PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader); + tcclLoader.addLogicalLib("testclasses"); + + Class testClass = parentLoader.loadClass(thisClass.getName()); + return new PathableTestSuite(testClass, tcclLoader); + } + + /** + * Set up instance variables required by this test case. + */ + public void setUp() throws Exception { + LogFactory.releaseAll(); + } + + /** + * Tear down instance variables required by this test case. + */ + public void tearDown() { + LogFactory.releaseAll(); + } + + // ----------------------------------------------------------- Test Methods + + /** + * Verify that MyLog is only loadable via the tccl. + */ + public void testLoader() throws Exception { + + ClassLoader thisClassLoader = this.getClass().getClassLoader(); + ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader(); + + // the tccl loader should NOT be the same as the loader that loaded this test class. + assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader); + + // MyLog should not be loadable via parent loader + try { + Class clazz = thisClassLoader.loadClass(MY_LOG_IMPL); + fail("Unexpectedly able to load MyLog via test class classloader"); + } catch(ClassNotFoundException ex) { + // ok, expected + } + + // MyLog should be loadable via tccl loader + try { + Class clazz = tcclLoader.loadClass(MY_LOG_IMPL); + } catch(ClassNotFoundException ex) { + fail("Unexpectedly unable to load MyLog via tccl classloader"); + } + } + + /** + * Verify that the custom Log implementation which is only accessable + * via the TCCL has NOT been loaded. Because this is only accessable via the + * TCCL, and we've use a commons-logging.properties that disables TCCL loading, + * we should see the default Log rather than the custom one. + */ + public void testTcclLoading() throws Exception { + LogFactory instance = LogFactory.getFactory(); + assertEquals( + "Correct LogFactory loaded", + "org.apache.commons.logging.impl.LogFactoryImpl", + instance.getClass().getName()); + + try { + Log log = instance.getLog("test"); + fail("Unexpectedly succeeded in loading a custom Log class" + + " that is only accessable via the tccl."); + } catch(LogConfigurationException ex) { + // ok, expected + int index = ex.getMessage().indexOf(MY_LOG_IMPL); + assertTrue("MyLog not found", index >= 0); + } + } +} Propchange: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclDisabledTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclDisabledTestCase.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclEnabledTestCase.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclEnabledTestCase.java?rev=370031&view=auto ============================================================================== --- jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclEnabledTestCase.java (added) +++ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclEnabledTestCase.java Tue Jan 17 19:42:52 2006 @@ -0,0 +1,152 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.logging.tccl.log; + + +import java.net.URL; + +import junit.framework.Test; +import junit.framework.TestCase; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.commons.logging.PathableClassLoader; +import org.apache.commons.logging.PathableTestSuite; + + +/** + * Verify that by default the standard LogFactoryImpl class loads a + * custom Log implementation via the TCCL. + */ + +public class TcclEnabledTestCase extends TestCase { + + public static final String MY_LOG_PKG = + "org.apache.commons.logging.tccl.custom"; + + public static final String MY_LOG_IMPL = + MY_LOG_PKG + ".MyLog"; + + // ------------------------------------------- JUnit Infrastructure Methods + + /** + * Return the tests included in this test suite. + */ + public static Test suite() throws Exception { + Class thisClass = TcclEnabledTestCase.class; + + // Determine the URL to this .class file, so that we can then + // append the priority dirs to it. For tidiness, load this + // class through a dummy loader though this is not absolutely + // necessary... + PathableClassLoader dummy = new PathableClassLoader(null); + dummy.useSystemLoader("junit."); + dummy.addLogicalLib("testclasses"); + dummy.addLogicalLib("commons-logging"); + + String thisClassPath = thisClass.getName().replace('.', '/') + ".class"; + URL baseUrl = dummy.findResource(thisClassPath); + + // Now set up the desired classloader hierarchy. Everything goes into + // the parent classpath, but we exclude the custom Log class. + // + // We then create a tccl classloader that can see the custom + // Log class. Therefore if that class can be found, then the + // TCCL must have been used to load it. + PathableClassLoader emptyLoader = new PathableClassLoader(null); + + PathableClassLoader parentLoader = new PathableClassLoader(null); + parentLoader.useSystemLoader("junit."); + parentLoader.addLogicalLib("commons-logging"); + parentLoader.addLogicalLib("testclasses"); + // hack to ensure that the testcase classloader can't see + // the custom MyLogFactoryImpl + parentLoader.useExplicitLoader(MY_LOG_PKG + ".", emptyLoader); + + URL propsEnableUrl = new URL(baseUrl, "props_enable_tccl/"); + parentLoader.addURL(propsEnableUrl); + + PathableClassLoader tcclLoader = new PathableClassLoader(parentLoader); + tcclLoader.addLogicalLib("testclasses"); + + Class testClass = parentLoader.loadClass(thisClass.getName()); + return new PathableTestSuite(testClass, tcclLoader); + } + + /** + * Set up instance variables required by this test case. + */ + public void setUp() throws Exception { + LogFactory.releaseAll(); + } + + /** + * Tear down instance variables required by this test case. + */ + public void tearDown() { + LogFactory.releaseAll(); + } + + // ----------------------------------------------------------- Test Methods + + /** + * Verify that MyLogFactoryImpl is only loadable via the tccl. + */ + public void testLoader() throws Exception { + + ClassLoader thisClassLoader = this.getClass().getClassLoader(); + ClassLoader tcclLoader = Thread.currentThread().getContextClassLoader(); + + // the tccl loader should NOT be the same as the loader that loaded this test class. + assertNotSame("tccl not same as test classloader", thisClassLoader, tcclLoader); + + // MyLog should not be loadable via parent loader + try { + Class clazz = thisClassLoader.loadClass(MY_LOG_IMPL); + fail("Unexpectedly able to load MyLog via test class classloader"); + } catch(ClassNotFoundException ex) { + // ok, expected + } + + // MyLog should be loadable via tccl loader + try { + Class clazz = tcclLoader.loadClass(MY_LOG_IMPL); + } catch(ClassNotFoundException ex) { + fail("Unexpectedly unable to load MyLog via tccl classloader"); + } + } + + /** + * Verify that the custom Log implementation which is only accessable + * via the TCCL has successfully been loaded as specified in the config file. + * This proves that the TCCL was used to load that class. + */ + public void testTcclLoading() throws Exception { + LogFactory instance = LogFactory.getFactory(); + + assertEquals( + "Correct LogFactory loaded", + "org.apache.commons.logging.impl.LogFactoryImpl", + instance.getClass().getName()); + + Log log = instance.getLog("test"); + assertEquals( + "Correct Log loaded", + MY_LOG_IMPL, + log.getClass().getName()); + } +} Propchange: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclEnabledTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/TcclEnabledTestCase.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_disable_tccl/commons-logging.properties URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_disable_tccl/commons-logging.properties?rev=370031&view=auto ============================================================================== --- jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_disable_tccl/commons-logging.properties (added) +++ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_disable_tccl/commons-logging.properties Tue Jan 17 19:42:52 2006 @@ -0,0 +1,3 @@ +use_tccl=false +org.apache.commons.logging.Log=org.apache.commons.logging.tccl.custom.MyLog +org.apache.commons.logging.diagnostics.dest=STDERR \ No newline at end of file Added: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_enable_tccl/commons-logging.properties URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_enable_tccl/commons-logging.properties?rev=370031&view=auto ============================================================================== --- jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_enable_tccl/commons-logging.properties (added) +++ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/log/props_enable_tccl/commons-logging.properties Tue Jan 17 19:42:52 2006 @@ -0,0 +1,2 @@ +org.apache.commons.logging.Log=org.apache.commons.logging.tccl.custom.MyLog +org.apache.commons.logging.diagnostics.dest=STDERR \ No newline at end of file Copied: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/TcclDisabledTestCase.java (from r369755, jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/TcclDisabledTestCase.java) URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/TcclDisabledTestCase.java?p2=jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/TcclDisabledTestCase.java&p1=jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/TcclDisabledTestCase.java&r1=369755&r2=370031&rev=370031&view=diff ============================================================================== --- jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/TcclDisabledTestCase.java (original) +++ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/TcclDisabledTestCase.java Tue Jan 17 19:42:52 2006 @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.apache.commons.logging.tccl; +package org.apache.commons.logging.tccl.logfactory; import java.net.URL; @@ -28,7 +28,8 @@ /** - * Verify that by default LogFactoryImpl is loaded from the tccl classloader. + * Verify that a commons-logging.properties file can prevent a custom + * LogFactoryImpl being loaded from the tccl classloader. */ public class TcclDisabledTestCase extends TestCase { @@ -74,7 +75,7 @@ parentLoader.addLogicalLib("commons-logging"); parentLoader.addLogicalLib("testclasses"); // hack to ensure that the testcase classloader can't see - // the cust MyLogFactoryImpl + // the custom MyLogFactoryImpl parentLoader.useExplicitLoader( MY_LOG_FACTORY_PKG + ".", emptyLoader); Copied: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/TcclEnabledTestCase.java (from r369747, jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/TcclEnabledTestCase.java) URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/TcclEnabledTestCase.java?p2=jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/TcclEnabledTestCase.java&p1=jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/TcclEnabledTestCase.java&r1=369747&r2=370031&rev=370031&view=diff ============================================================================== --- jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/TcclEnabledTestCase.java (original) +++ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/tccl/logfactory/TcclEnabledTestCase.java Tue Jan 17 19:42:52 2006 @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.apache.commons.logging.tccl; +package org.apache.commons.logging.tccl.logfactory; import java.net.URL; @@ -25,11 +25,11 @@ import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.PathableClassLoader; import org.apache.commons.logging.PathableTestSuite; -import org.apache.commons.logging.config.PriorityConfigTestCase; /** - * Verify that by default LogFactoryImpl is loaded from the tccl classloader. + * Verify that by default a custom LogFactoryImpl is loaded from the + * tccl classloader. */ public class TcclEnabledTestCase extends TestCase { --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]