StandardTestCase 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.logkit;
    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
    28  import org.apache.commons.logging.AbstractLogTest;
    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.LogKitLogger;
    34
    35  /**
    36   * Basic tests for Avalon LogKit logger adapter.
    37   */
    38
    39  public class StandardTestCase extends AbstractLogTest {
    40
    41
    42      // ----------------------------------------------------- Instance Variables
    43
    44
    45      /**
    46       * <p>The {@link LogFactory} implementation we have selected.</p>
    47       */
    48      protected LogFactory factory = null;
    49
    50
    51      /**
    52       * <p>The {@link Log} implementation we have selected.</p>
    53       */
    54      protected Log log = null;
    55
    56
    57      // ------------------------------------------- JUnit Infrastructure Methods
    58
    59
    60      /**
    61       * Return the tests included in this test suite.
    62       */
    63      public static Test suite() throws Exception {
    64          Class thisClass = StandardTestCase.class;
    65
    66          PathableClassLoader loader = new PathableClassLoader(null);
    67          loader.useExplicitLoader("junit.", Test.class.getClassLoader());
    68          loader.addLogicalLib("testclasses");
    69          loader.addLogicalLib("commons-logging");
    70          loader.addLogicalLib("logkit");
    71
    72          Class testClass = loader.loadClass(thisClass.getName());
    73          return new PathableTestSuite(testClass, loader);
    74      }
    75
    76      /**
    77       * Set up instance variables required by this test case.
    78       */
    79      public void setUp() throws Exception {
    80          LogFactory.releaseAll();
    81
    82          System.setProperty(
    83                  "org.apache.commons.logging.Log",
    84                  "org.apache.commons.logging.impl.LogKitLogger");
    85
    86          factory = LogFactory.getFactory();
    87          log = LogFactory.getLog("TestLogger");
    88      }
    89
    90      /**
    91       * Tear down instance variables required by this test case.
    92       */
    93      public void tearDown() {
    94          log = null;
    95          factory = null;
    96          LogFactory.releaseAll();
    97      }
    98
    99      // ----------------------------------------------------------- Test Methods
    100
    101     /**
    102      * Override the abstract method from the parent class so that the
    103      * inherited tests can access the right Log object type.
    104      */
    105     public Log getLogObject()
    106     {
    107         return new LogKitLogger(this.getClass().getName());
    108     }
    109
    110     // Test pristine LogFactory instance
    111     public void testPristineFactory() {
    112
    113         assertNotNull("LogFactory exists", factory);
    114         assertEquals("LogFactory class",
    115                      "org.apache.commons.logging.impl.LogFactoryImpl",
    116                      factory.getClass().getName());
    117
    118         String names[] = factory.getAttributeNames();
    119         assertNotNull("Names exists", names);
    120         assertEquals("Names empty", 0, names.length);
    121     }
    122
    123     // Test pristine Log instance
    124     public void testPristineLog() {
    125         checkStandard();
    126     }
    127
    128     // Test Serializability of standard instance
    129     public void testSerializable() throws Exception {
    130         checkStandard();
    131
    132         // Serialize and deserialize the instance
    133         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    134         ObjectOutputStream oos = new ObjectOutputStream(baos);
    135         oos.writeObject(log);
    136         oos.close();
    137         ByteArrayInputStream bais =
    138             new ByteArrayInputStream(baos.toByteArray());
    139         ObjectInputStream ois = new ObjectInputStream(bais);
    140         log = (Log) ois.readObject();
    141         ois.close();
    142
    143         checkStandard();
    144     }
    145
    146
    147     // -------------------------------------------------------- Support Methods
    148
    149     // Check the standard log instance
    150     protected void checkStandard() {
    151
    152         assertNotNull("Log exists", log);
    153         assertEquals("Log class",
    154                      "org.apache.commons.logging.impl.LogKitLogger",
    155                      log.getClass().getName());
    156
    157         // Can we call level checkers with no exceptions?
    158         // Note that by default *everything* is enabled for LogKit
    159         assertTrue(log.isTraceEnabled());
    160         assertTrue(log.isDebugEnabled());
    161         assertTrue(log.isInfoEnabled());
    162         assertTrue(log.isWarnEnabled());
    163         assertTrue(log.isErrorEnabled());
    164         assertTrue(log.isFatalEnabled());
    165     }
    166 }