Change to using PathableTestSuite - this permits the test to be significantly simplified.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@209419 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -18,30 +18,76 @@ package org.apache.commons.logging;
|
||||
|
||||
import junit.framework.*;
|
||||
|
||||
/**
|
||||
* Test the ability to force the LogFactory class to use some
|
||||
* arbitrary Hashtable implementation to store its mapping from
|
||||
* context-classloader -> LogFactory object.
|
||||
* <p>
|
||||
* This is done by
|
||||
*/
|
||||
public class AltHashtableTestCase extends TestCase {
|
||||
|
||||
public static Test suite() throws Exception {
|
||||
Class thisClass = AltHashtableTestCase.class;
|
||||
ClassLoader thisClassLoader = thisClass.getClassLoader();
|
||||
|
||||
PathableClassLoader loader = new PathableClassLoader(null);
|
||||
loader.useExplicitLoader("junit.", thisClassLoader);
|
||||
loader.addLogicalLib("testclasses");
|
||||
loader.addLogicalLib("commons-logging");
|
||||
|
||||
Class testClass = loader.loadClass(thisClass.getName());
|
||||
return new PathableTestSuite(testClass, loader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up before each test.
|
||||
* <p>
|
||||
* This method ensures that the appropriate system property is defined
|
||||
* to force the LogFactory class to use the AltHashtable class as its
|
||||
* Hashtable implementation for storing factories in.
|
||||
* <p>
|
||||
* This does make the assumption that whatever JVM we are running in
|
||||
* doesn't initialise classes until they are actually referenced (ie the
|
||||
* LogFactory class hasn't been initialised before this method is called).
|
||||
* This is true of all JVMs I know of; and if it isn't then this test will
|
||||
* fail and someone will tell us.
|
||||
*/
|
||||
public void setUp() {
|
||||
System.setProperty(
|
||||
"org.apache.commons.logging.LogFactory.HashtableImpl",
|
||||
AltHashtable.class.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that initialising the LogFactory class will cause it
|
||||
* to instantiate an object of type specified in system property
|
||||
* "org.apache.commons.logging.LogFactory.HashtableImpl".
|
||||
*/
|
||||
public void testType() {
|
||||
// Here, the reference to the LogFactory class should cause the
|
||||
// class to be loaded and initialised. It will see the property
|
||||
// set and use the AltHashtable class. If other tests in this
|
||||
// class have already been run within the same classloader then
|
||||
// LogFactory will already have been initialised, but that
|
||||
// doesn't change the effectiveness of this test.
|
||||
assertTrue(LogFactory.factories instanceof AltHashtable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that when LogFactory sees a context-classloader for the
|
||||
* first time that it creates a new entry in the LogFactory.factories
|
||||
* hashmap. In particular, this checks that this process works ok when
|
||||
* a system property has been used to specify an alternative Hashtable
|
||||
* implementation for LogFactory to use.
|
||||
*/
|
||||
public void testPutCalled() throws Exception {
|
||||
|
||||
AltHashtable.lastKey = null;
|
||||
AltHashtable.lastValue = null;
|
||||
ClassLoader classLoader = new ClassLoader() {};
|
||||
Thread thread = new Thread(
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
|
||||
LogFactory.getLog(AltHashtableTestCase.class);
|
||||
}
|
||||
}
|
||||
);
|
||||
thread.setContextClassLoader(classLoader);
|
||||
|
||||
thread.start();
|
||||
thread.join();
|
||||
|
||||
assertEquals(classLoader, AltHashtable.lastKey);
|
||||
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||
assertEquals(contextLoader, AltHashtable.lastKey);
|
||||
assertNotNull(AltHashtable.lastValue);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user