You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sk...@apache.org on 2005/07/06 09:17:53 UTC

svn commit: r209419 - /jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/AltHashtableTestCase.java

Author: skitching
Date: Wed Jul  6 00:17:45 2005
New Revision: 209419

URL: http://svn.apache.org/viewcvs?rev=209419&view=rev
Log:
Change to using PathableTestSuite - this permits the test to be significantly simplified.

Modified:
    jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/AltHashtableTestCase.java

Modified: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/AltHashtableTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/AltHashtableTestCase.java?rev=209419&r1=209418&r2=209419&view=diff
==============================================================================
--- jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/AltHashtableTestCase.java (original)
+++ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/AltHashtableTestCase.java Wed Jul  6 00:17:45 2005
@@ -18,30 +18,76 @@
 
 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);
+        LogFactory.getLog(AltHashtableTestCase.class);
+        ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
+        assertEquals(contextLoader, AltHashtable.lastKey);
         assertNotNull(AltHashtable.lastValue);
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org