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