1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.logging;
18
19 import junit.framework.*;
20
21 /***
22 * Test the ability to force the LogFactory class to use some
23 * arbitrary Hashtable implementation to store its mapping from
24 * context-classloader -> LogFactory object.
25 * <p>
26 * This is done by
27 */
28 public class AltHashtableTestCase extends TestCase {
29
30 public static Test suite() throws Exception {
31 Class thisClass = AltHashtableTestCase.class;
32 ClassLoader thisClassLoader = thisClass.getClassLoader();
33
34 PathableClassLoader loader = new PathableClassLoader(null);
35 loader.useExplicitLoader("junit.", thisClassLoader);
36 loader.addLogicalLib("testclasses");
37 loader.addLogicalLib("commons-logging");
38
39 Class testClass = loader.loadClass(thisClass.getName());
40 return new PathableTestSuite(testClass, loader);
41 }
42
43 /***
44 * Set up before each test.
45 * <p>
46 * This method ensures that the appropriate system property is defined
47 * to force the LogFactory class to use the AltHashtable class as its
48 * Hashtable implementation for storing factories in.
49 * <p>
50 * This does make the assumption that whatever JVM we are running in
51 * doesn't initialise classes until they are actually referenced (ie the
52 * LogFactory class hasn't been initialised before this method is called).
53 * This is true of all JVMs I know of; and if it isn't then this test will
54 * fail and someone will tell us.
55 */
56 public void setUp() {
57 System.setProperty(
58 "org.apache.commons.logging.LogFactory.HashtableImpl",
59 AltHashtable.class.getName());
60 }
61
62 /***
63 * Verify that initialising the LogFactory class will cause it
64 * to instantiate an object of type specified in system property
65 * "org.apache.commons.logging.LogFactory.HashtableImpl".
66 */
67 public void testType() {
68
69
70
71
72
73
74 assertTrue(LogFactory.factories instanceof AltHashtable);
75 }
76
77 /***
78 * Verify that when LogFactory sees a context-classloader for the
79 * first time that it creates a new entry in the LogFactory.factories
80 * hashmap. In particular, this checks that this process works ok when
81 * a system property has been used to specify an alternative Hashtable
82 * implementation for LogFactory to use.
83 */
84 public void testPutCalled() throws Exception {
85 AltHashtable.lastKey = null;
86 AltHashtable.lastValue = null;
87
88 LogFactory.getLog(AltHashtableTestCase.class);
89 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
90 assertEquals(contextLoader, AltHashtable.lastKey);
91 assertNotNull(AltHashtable.lastValue);
92 }
93 }