01: /*
02: * Copyright 2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
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 this Class = AltHashtableTestCase.class;
32: ClassLoader this ClassLoader = this Class.getClassLoader();
33:
34: PathableClassLoader loader = new PathableClassLoader(null);
35: loader.useExplicitLoader("junit.", this ClassLoader);
36: loader.addLogicalLib("testclasses");
37: loader.addLogicalLib("commons-logging");
38:
39: Class testClass = loader.loadClass(this Class.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: // Here, the reference to the LogFactory class should cause the
69: // class to be loaded and initialised. It will see the property
70: // set and use the AltHashtable class. If other tests in this
71: // class have already been run within the same classloader then
72: // LogFactory will already have been initialised, but that
73: // doesn't change the effectiveness of this test.
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()
90: .getContextClassLoader();
91: assertEquals(contextLoader, AltHashtable.lastKey);
92: assertNotNull(AltHashtable.lastValue);
93: }
94: }
|