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