1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.logging; 19 20 import junit.framework.Test; 21 import junit.framework.TestCase; 22 23 /** 24 * Test the ability to force the LogFactory class to use some 25 * arbitrary Hashtable implementation to store its mapping from 26 * context class loader -> LogFactory object. 27 */ 28 public class AltHashtableTestCase extends TestCase { 29 30 public static Test suite() throws Exception { 31 final Class thisClass = AltHashtableTestCase.class; 32 final ClassLoader thisClassLoader = thisClass.getClassLoader(); 33 34 final PathableClassLoader loader = new PathableClassLoader(null); 35 loader.useExplicitLoader("junit.", thisClassLoader); 36 loader.addLogicalLib("testclasses"); 37 loader.addLogicalLib("commons-logging"); 38 39 final Class testClass = loader.loadClass(thisClass.getName()); 40 return new PathableTestSuite(testClass, loader); 41 } 42 43 /** 44 * Sets 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 initialize classes until they are actually referenced (ie the 52 * LogFactory class hasn't been initialized 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 @Override 57 public void setUp() { 58 System.setProperty( 59 "org.apache.commons.logging.LogFactory.HashtableImpl", 60 AltHashtable.class.getName()); 61 } 62 63 /** 64 * Verify that when LogFactory sees a context class loader for the 65 * first time that it creates a new entry in the LogFactory.factories 66 * hashmap. In particular, this checks that this process works ok when 67 * a system property has been used to specify an alternative Hashtable 68 * implementation for LogFactory to use. 69 */ 70 public void testPutCalled() throws Exception { 71 AltHashtable.lastKey = null; 72 AltHashtable.lastValue = null; 73 74 LogFactory.getLog(AltHashtableTestCase.class); 75 final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); 76 assertEquals(contextLoader, AltHashtable.lastKey); 77 assertNotNull(AltHashtable.lastValue); 78 } 79 80 /** 81 * Verify that initialising the LogFactory class will cause it 82 * to instantiate an object of type specified in system property 83 * "org.apache.commons.logging.LogFactory.HashtableImpl". 84 */ 85 public void testType() { 86 // Here, the reference to the LogFactory class should cause the 87 // class to be loaded and initialized. It will see the property 88 // set and use the AltHashtable class. If other tests in this 89 // class have already been run within the same class loader then 90 // LogFactory will already have been initialized, but that 91 // doesn't change the effectiveness of this test. 92 assertTrue(LogFactory.factories instanceof AltHashtable); 93 } 94 }