    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.jdk14;
    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  import junit.framework.TestSuite;
    29
    30  import org.apache.commons.logging.Log;
    31  import org.apache.commons.logging.LogFactory;
    32  import org.apache.commons.logging.PathableClassLoader;
    33  import org.apache.commons.logging.PathableTestSuite;
    34
    35
    36  /**
    37   * <p>TestCase for JDK 1.4 logging when running on a JDK 1.4 system with
    38   * zero configuration, and with Log4J not present (so JDK 1.4 logging
    39   * should be automatically configured.</p>
    40   *
    41   * @author Craig R. McClanahan
    42   * @version $Revision: 427808 $ $Date: 2006-08-02 02:08:20 +0200 (on, 02 aug 2006) $
    43   */
    44
    45  public class DefaultConfigTestCase extends TestCase {
    46
    47
    48      // ----------------------------------------------------------- Constructors
    49
    50
    51      /**
    52       * <p>Construct a new instance of this test case.</p>
    53       *
    54       * @param name Name of the test case
    55       */
    56      public DefaultConfigTestCase(String name) {
    57          super(name);
    58      }
    59
    60
    61      // ----------------------------------------------------- Instance Variables
    62
    63
    64      /**
    65       * <p>The {@link LogFactory} implementation we have selected.</p>
    66       */
    67      protected LogFactory factory = null;
    68
    69
    70      /**
    71       * <p>The {@link Log} implementation we have selected.</p>
    72       */
    73      protected Log log = null;
    74
    75
    76      // ------------------------------------------- JUnit Infrastructure Methods
    77
    78
    79      /**
    80       * Set up instance variables required by this test case.
    81       */
    82      public void setUp() throws Exception {
    83          setUpFactory();
    84          setUpLog("TestLogger");
    85      }
    86
    87
    88      /**
    89       * Return the tests included in this test suite.
    90       */
    91      public static Test suite() throws Exception {
    92          PathableClassLoader loader = new PathableClassLoader(null);
    93          loader.useExplicitLoader("junit.", Test.class.getClassLoader());
    94          loader.addLogicalLib("testclasses");
    95          loader.addLogicalLib("commons-logging");
    96
    97          Class testClass = loader.loadClass(DefaultConfigTestCase.class.getName());
    98          return new PathableTestSuite(testClass, loader);
    99      }
    100
    101     /**
    102      * Tear down instance variables required by this test case.
    103      */
    104     public void tearDown() {
    105         log = null;
    106         factory = null;
    107         LogFactory.releaseAll();
    108     }
    109
    110
    111     // ----------------------------------------------------------- Test Methods
    112
    113
    114     // Test pristine Log instance
    115     public void testPristineLog() {
    116
    117         checkLog();
    118
    119     }
    120
    121
    122     // Test pristine LogFactory instance
    123     public void testPristineFactory() {
    124
    125         assertNotNull("LogFactory exists", factory);
    126         assertEquals("LogFactory class",
    127                      "org.apache.commons.logging.impl.LogFactoryImpl",
    128                      factory.getClass().getName());
    129
    130         String names[] = factory.getAttributeNames();
    131         assertNotNull("Names exists", names);
    132         assertEquals("Names empty", 0, names.length);
    133
    134     }
    135
    136
    137     // Test Serializability of Log instance
    138     public void testSerializable() throws Exception {
    139
    140         // Serialize and deserialize the instance
    141         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    142         ObjectOutputStream oos = new ObjectOutputStream(baos);
    143         oos.writeObject(log);
    144         oos.close();
    145         ByteArrayInputStream bais =
    146             new ByteArrayInputStream(baos.toByteArray());
    147         ObjectInputStream ois = new ObjectInputStream(bais);
    148         log = (Log) ois.readObject();
    149         ois.close();
    150
    151         // Check the characteristics of the resulting object
    152         checkLog();
    153
    154     }
    155
    156
    157     // -------------------------------------------------------- Support Methods
    158
    159
    160
    161     // Check the log instance
    162     protected void checkLog() {
    163
    164         assertNotNull("Log exists", log);
    165         assertEquals("Log class",
    166                      "org.apache.commons.logging.impl.Jdk14Logger",
    167                      log.getClass().getName());
    168
    169         // Can we call level checkers with no exceptions?
    170         log.isDebugEnabled();
    171         log.isErrorEnabled();
    172         log.isFatalEnabled();
    173         log.isInfoEnabled();
    174         log.isTraceEnabled();
    175         log.isWarnEnabled();
    176
    177     }
    178
    179
    180     // Set up factory instance
    181     protected void setUpFactory() throws Exception {
    182         factory = LogFactory.getFactory();
    183     }
    184
    185
    186     // Set up log instance
    187     protected void setUpLog(String name) throws Exception {
    188         log = LogFactory.getLog(name);
    189     }
    190
    191
    192 }