001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */ 
017    
018    package org.apache.commons.logging;
019    
020    import junit.framework.*;
021    
022    /**
023     * Test the ability to force the LogFactory class to use some
024     * arbitrary Hashtable implementation to store its mapping from
025     * context-classloader -> LogFactory object.
026     * <p>
027     * This is done by 
028     */
029    public class AltHashtableTestCase extends TestCase {
030    
031        public static Test suite() throws Exception {
032            Class thisClass = AltHashtableTestCase.class;
033            ClassLoader thisClassLoader = thisClass.getClassLoader();
034    
035            PathableClassLoader loader = new PathableClassLoader(null);
036            loader.useExplicitLoader("junit.", thisClassLoader);
037            loader.addLogicalLib("testclasses");
038            loader.addLogicalLib("commons-logging");
039    
040            Class testClass = loader.loadClass(thisClass.getName());
041            return new PathableTestSuite(testClass, loader);
042        }
043    
044        /**
045         * Set up before each test.
046         * <p>
047         * This method ensures that the appropriate system property is defined
048         * to force the LogFactory class to use the AltHashtable class as its
049         * Hashtable implementation for storing factories in. 
050         * <p>
051         * This does make the assumption that whatever JVM we are running in
052         * doesn't initialise classes until they are actually referenced (ie the
053         * LogFactory class hasn't been initialised before this method is called).
054         * This is true of all JVMs I know of; and if it isn't then this test will
055         * fail and someone will tell us. 
056         */
057        public void setUp() {
058            System.setProperty(
059                    "org.apache.commons.logging.LogFactory.HashtableImpl",
060                    AltHashtable.class.getName());
061        }
062        
063        /**
064         * Verify that initialising the LogFactory class will cause it
065         * to instantiate an object of type specified in system property
066         * "org.apache.commons.logging.LogFactory.HashtableImpl".
067         */
068        public void testType() {
069            // Here, the reference to the LogFactory class should cause the
070            // class to be loaded and initialised. It will see the property
071            // set and use the AltHashtable class. If other tests in this
072            // class have already been run within the same classloader then
073            // LogFactory will already have been initialised, but that
074            // doesn't change the effectiveness of this test.
075            assertTrue(LogFactory.factories instanceof AltHashtable);
076        }
077        
078        /**
079         * Verify that when LogFactory sees a context-classloader for the
080         * first time that it creates a new entry in the LogFactory.factories
081         * hashmap. In particular, this checks that this process works ok when
082         * a system property has been used to specify an alternative Hashtable
083         * implementation for LogFactory to use.
084         */
085        public void testPutCalled() throws Exception {
086            AltHashtable.lastKey = null;
087            AltHashtable.lastValue = null;
088            
089            LogFactory.getLog(AltHashtableTestCase.class);
090            ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
091            assertEquals(contextLoader, AltHashtable.lastKey);
092            assertNotNull(AltHashtable.lastValue);
093        }
094    }