1   /*
2    * Copyright 2006 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.logging.logkit;
18  
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.commons.logging.PathableClassLoader;
31  import org.apache.commons.logging.PathableTestSuite;
32  import org.apache.commons.logging.impl.LogKitLogger;
33  import org.apache.commons.logging.impl.NoOpLog;
34  
35  import org.apache.commons.logging.AbstractLogTest;
36  
37  /***
38   * Basic tests for Avalon LogKit logger adapter.
39   */
40  
41  public class StandardTestCase extends AbstractLogTest {
42  
43  
44      // ----------------------------------------------------- Instance Variables
45  
46  
47      /***
48       * <p>The {@link LogFactory} implementation we have selected.</p>
49       */
50      protected LogFactory factory = null;
51  
52  
53      /***
54       * <p>The {@link Log} implementation we have selected.</p>
55       */
56      protected Log log = null;
57  
58  
59      // ------------------------------------------- JUnit Infrastructure Methods
60  
61  
62      /***
63       * Return the tests included in this test suite.
64       */
65      public static Test suite() throws Exception {
66          Class thisClass = StandardTestCase.class;
67  
68          PathableClassLoader loader = new PathableClassLoader(null);
69          loader.useSystemLoader("junit.");
70          loader.addLogicalLib("testclasses");
71          loader.addLogicalLib("commons-logging");
72          loader.addLogicalLib("logkit");
73  
74          Class testClass = loader.loadClass(thisClass.getName());
75          return new PathableTestSuite(testClass, loader);
76      }
77  
78      /***
79       * Set up instance variables required by this test case.
80       */
81      public void setUp() throws Exception {
82          LogFactory.releaseAll();
83  
84          System.setProperty(
85                  "org.apache.commons.logging.Log",
86                  "org.apache.commons.logging.impl.LogKitLogger");
87  
88          factory = LogFactory.getFactory();
89          log = LogFactory.getLog("TestLogger");
90      }
91  
92      /***
93       * Tear down instance variables required by this test case.
94       */
95      public void tearDown() {
96          log = null;
97          factory = null;
98          LogFactory.releaseAll();
99      }
100 
101     // ----------------------------------------------------------- Test Methods
102 
103     /***
104      * Override the abstract method from the parent class so that the
105      * inherited tests can access the right Log object type. 
106      */
107     public Log getLogObject()
108     {
109         return new LogKitLogger(this.getClass().getName());
110     }
111 
112     // Test pristine LogFactory instance
113     public void testPristineFactory() {
114 
115         assertNotNull("LogFactory exists", factory);
116         assertEquals("LogFactory class",
117                      "org.apache.commons.logging.impl.LogFactoryImpl",
118                      factory.getClass().getName());
119 
120         String names[] = factory.getAttributeNames();
121         assertNotNull("Names exists", names);
122         assertEquals("Names empty", 0, names.length);
123     }
124 
125     // Test pristine Log instance
126     public void testPristineLog() {
127         checkStandard();
128     }
129 
130     // Test Serializability of standard instance
131     public void testSerializable() throws Exception {
132         checkStandard();
133 
134         // Serialize and deserialize the instance
135         ByteArrayOutputStream baos = new ByteArrayOutputStream();
136         ObjectOutputStream oos = new ObjectOutputStream(baos);
137         oos.writeObject(log);
138         oos.close();
139         ByteArrayInputStream bais =
140             new ByteArrayInputStream(baos.toByteArray());
141         ObjectInputStream ois = new ObjectInputStream(bais);
142         log = (Log) ois.readObject();
143         ois.close();
144 
145         checkStandard();
146     }
147 
148 
149     // -------------------------------------------------------- Support Methods
150 
151     // Check the standard log instance
152     protected void checkStandard() {
153 
154         assertNotNull("Log exists", log);
155         assertEquals("Log class",
156                      "org.apache.commons.logging.impl.LogKitLogger",
157                      log.getClass().getName());
158 
159         // Can we call level checkers with no exceptions?
160         // Note that by default *everything* is enabled for LogKit
161         assertTrue(log.isTraceEnabled());
162         assertTrue(log.isDebugEnabled());
163         assertTrue(log.isInfoEnabled());
164         assertTrue(log.isWarnEnabled());
165         assertTrue(log.isErrorEnabled());
166         assertTrue(log.isFatalEnabled());
167     }
168 }