DefaultConfigTestCase xref

    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.simple;
    19
    20
    21  import java.io.ByteArrayInputStream;
    22  import java.io.ByteArrayOutputStream;
    23  import java.io.ObjectInputStream;
    24  import java.io.ObjectOutputStream;
    25
    26  import junit.framework.Test;
    27  import junit.framework.TestCase;
    28
    29  import org.apache.commons.logging.Log;
    30  import org.apache.commons.logging.LogFactory;
    31  import org.apache.commons.logging.PathableClassLoader;
    32  import org.apache.commons.logging.PathableTestSuite;
    33  import org.apache.commons.logging.impl.SimpleLog;
    34
    35
    36  /**
    37   * <p>TestCase for simple logging when running with zero configuration
    38   * other than selecting the SimpleLog implementation.</p>
    39   *
    40   * @author Craig R. McClanahan
    41   * @version $Revision: 425249 $ $Date: 2006-07-25 03:30:16 +0200 (ti, 25 jul 2006) $
    42   */
    43
    44  public class DefaultConfigTestCase extends TestCase {
    45
    46
    47      // ----------------------------------------------------- Instance Variables
    48
    49
    50      /**
    51       * <p>The {@link LogFactory} implementation we have selected.</p>
    52       */
    53      protected LogFactory factory = null;
    54
    55
    56      /**
    57       * <p>The {@link Log} implementation we have selected.</p>
    58       */
    59      protected Log log = null;
    60
    61
    62      // ------------------------------------------- JUnit Infrastructure Methods
    63
    64
    65      /**
    66       * Return the tests included in this test suite.
    67       * <p>
    68       * We need to use a PathableClassLoader here because the SimpleLog class
    69       * is a pile of junk and chock-full of static variables. Any other test
    70       * (like simple.CustomConfigTestCase) that has used the SimpleLog class
    71       * will already have caused it to do once-only initialisation that we
    72       * can't reset, even by calling LogFactory.releaseAll, because of those
    73       * ugly statics. The only clean solution is to load a clean copy of
    74       * commons-logging including SimpleLog via a nice clean classloader.
    75       * Or we could fix SimpleLog to be sane...
    76       */
    77      public static Test suite() throws Exception {
    78          Class thisClass = DefaultConfigTestCase.class;
    79
    80          PathableClassLoader loader = new PathableClassLoader(null);
    81          loader.useExplicitLoader("junit.", Test.class.getClassLoader());
    82          loader.addLogicalLib("testclasses");
    83          loader.addLogicalLib("commons-logging");
    84
    85          Class testClass = loader.loadClass(thisClass.getName());
    86          return new PathableTestSuite(testClass, loader);
    87      }
    88
    89      /**
    90       * Set system properties that will control the LogFactory/Log objects
    91       * when they are created. Subclasses can override this method to
    92       * define properties that suit them.
    93       */
    94      public void setProperties() {
    95          System.setProperty(
    96              "org.apache.commons.logging.Log",
    97              "org.apache.commons.logging.impl.SimpleLog");
    98      }
    99
    100     /**
    101      * Set up instance variables required by this test case.
    102      */
    103     public void setUp() throws Exception {
    104         LogFactory.releaseAll();
    105         setProperties();
    106         setUpFactory();
    107         setUpLog("TestLogger");
    108     }
    109
    110     /**
    111      * Tear down instance variables required by this test case.
    112      */
    113     public void tearDown() {
    114         log = null;
    115         factory = null;
    116         LogFactory.releaseAll();
    117     }
    118
    119
    120     // ----------------------------------------------------------- Test Methods
    121
    122
    123     // Test pristine DecoratedSimpleLog instance
    124     public void testPristineDecorated() {
    125
    126         setUpDecorated("DecoratedLogger");
    127         checkDecorated();
    128
    129     }
    130
    131
    132     // Test pristine Log instance
    133     public void testPristineLog() {
    134
    135         checkStandard();
    136
    137     }
    138
    139
    140     // Test pristine LogFactory instance
    141     public void testPristineFactory() {
    142
    143         assertNotNull("LogFactory exists", factory);
    144         assertEquals("LogFactory class",
    145                      "org.apache.commons.logging.impl.LogFactoryImpl",
    146                      factory.getClass().getName());
    147
    148         String names[] = factory.getAttributeNames();
    149         assertNotNull("Names exists", names);
    150         assertEquals("Names empty", 0, names.length);
    151
    152     }
    153
    154
    155     // Test Serializability of standard instance
    156     public void testSerializable() throws Exception {
    157
    158         // Serialize and deserialize the instance
    159         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    160         ObjectOutputStream oos = new ObjectOutputStream(baos);
    161         oos.writeObject(log);
    162         oos.close();
    163         ByteArrayInputStream bais =
    164             new ByteArrayInputStream(baos.toByteArray());
    165         ObjectInputStream ois = new ObjectInputStream(bais);
    166         log = (Log) ois.readObject();
    167         ois.close();
    168
    169         // Check the characteristics of the resulting object
    170         checkStandard();
    171
    172     }
    173
    174
    175     // -------------------------------------------------------- Support Methods
    176
    177
    178
    179     // Check the decorated log instance
    180     protected void checkDecorated() {
    181
    182         assertNotNull("Log exists", log);
    183         assertEquals("Log class",
    184                      "org.apache.commons.logging.simple.DecoratedSimpleLog",
    185                      log.getClass().getName());
    186
    187         // Can we call level checkers with no exceptions?
    188         assertTrue(!log.isDebugEnabled());
    189         assertTrue(log.isErrorEnabled());
    190         assertTrue(log.isFatalEnabled());
    191         assertTrue(log.isInfoEnabled());
    192         assertTrue(!log.isTraceEnabled());
    193         assertTrue(log.isWarnEnabled());
    194
    195         // Can we retrieve the current log level?
    196         assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
    197
    198         // Can we validate the extra exposed properties?
    199         assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
    200                      ((DecoratedSimpleLog) log).getDateTimeFormat());
    201         assertEquals("DecoratedLogger",
    202                      ((DecoratedSimpleLog) log).getLogName());
    203         assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
    204         assertTrue(((DecoratedSimpleLog) log).getShowShortName());
    205
    206     }
    207
    208
    209     // Check the standard log instance
    210     protected void checkStandard() {
    211
    212         assertNotNull("Log exists", log);
    213         assertEquals("Log class",
    214                      "org.apache.commons.logging.impl.SimpleLog",
    215                      log.getClass().getName());
    216
    217         // Can we call level checkers with no exceptions?
    218         assertTrue(!log.isDebugEnabled());
    219         assertTrue(log.isErrorEnabled());
    220         assertTrue(log.isFatalEnabled());
    221         assertTrue(log.isInfoEnabled());
    222         assertTrue(!log.isTraceEnabled());
    223         assertTrue(log.isWarnEnabled());
    224
    225         // Can we retrieve the current log level?
    226         assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
    227
    228     }
    229
    230
    231     // Set up decorated log instance
    232     protected void setUpDecorated(String name) {
    233         log = new DecoratedSimpleLog(name);
    234     }
    235
    236
    237     // Set up factory instance
    238     protected void setUpFactory() throws Exception {
    239         factory = LogFactory.getFactory();
    240     }
    241
    242
    243     // Set up log instance
    244     protected void setUpLog(String name) throws Exception {
    245         log = LogFactory.getLog(name);
    246     }
    247
    248
    249 }