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.jdk14;
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.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.commons.logging.PathableClassLoader;
28  import org.apache.commons.logging.PathableTestSuite;
29  
30  import junit.framework.Test;
31  import junit.framework.TestCase;
32  
33  /**
34   * <p>TestCase for JDK 1.4 logging when running on a JDK 1.4 system with
35   * zero configuration, and with Log4J not present (so JDK 1.4 logging
36   * should be automatically configured.</p>
37   */
38  public class DefaultConfigTestCase extends TestCase {
39  
40      /**
41       * Return the tests included in this test suite.
42       */
43      public static Test suite() throws Exception {
44          final PathableClassLoader loader = new PathableClassLoader(null);
45          loader.useExplicitLoader("junit.", Test.class.getClassLoader());
46          loader.addLogicalLib("testclasses");
47          loader.addLogicalLib("commons-logging");
48  
49          final Class testClass = loader.loadClass(DefaultConfigTestCase.class.getName());
50          return new PathableTestSuite(testClass, loader);
51      }
52  
53      /**
54       * <p>The {@link LogFactory} implementation we have selected.</p>
55       */
56      protected LogFactory factory;
57  
58      /**
59       * <p>The {@link Log} implementation we have selected.</p>
60       */
61      protected Log log;
62  
63      /**
64       * <p>Construct a new instance of this test case.</p>
65       *
66       * @param name Name of the test case
67       */
68      public DefaultConfigTestCase(final String name) {
69          super(name);
70      }
71  
72      // Check the log instance
73      protected void checkLog() {
74  
75          assertNotNull("Log exists", log);
76          assertEquals("Log class",
77                       "org.apache.commons.logging.impl.Jdk14Logger",
78                       log.getClass().getName());
79  
80          // Can we call level checkers with no exceptions?
81          log.isDebugEnabled();
82          log.isErrorEnabled();
83          log.isFatalEnabled();
84          log.isInfoEnabled();
85          log.isTraceEnabled();
86          log.isWarnEnabled();
87  
88      }
89  
90      /**
91       * Sets up instance variables required by this test case.
92       */
93      @Override
94      public void setUp() throws Exception {
95          setUpFactory();
96          setUpLog("TestLogger");
97      }
98  
99      // Set up factory instance
100     protected void setUpFactory() throws Exception {
101         factory = LogFactory.getFactory();
102     }
103 
104     // Set up log instance
105     protected void setUpLog(final String name) throws Exception {
106         log = LogFactory.getLog(name);
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         assertNotNull("LogFactory exists", factory);
122         assertEquals("LogFactory class",
123                      "org.apache.commons.logging.impl.LogFactoryImpl",
124                      factory.getClass().getName());
125 
126         final String[] names = factory.getAttributeNames();
127         assertNotNull("Names exists", names);
128         assertEquals("Names empty", 0, names.length);
129     }
130 
131     // Test pristine Log instance
132     public void testPristineLog() {
133         checkLog();
134     }
135 
136     // Test Serializability of Log instance
137     public void testSerializable() throws Exception {
138 
139         // Serialize and deserialize the instance
140         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
141         final ObjectOutputStream oos = new ObjectOutputStream(baos);
142         oos.writeObject(log);
143         oos.close();
144         final ByteArrayInputStream bais =
145             new ByteArrayInputStream(baos.toByteArray());
146         final ObjectInputStream ois = new ObjectInputStream(bais);
147         log = (Log) ois.readObject();
148         ois.close();
149 
150         // Check the characteristics of the resulting object
151         checkLog();
152 
153     }
154 
155 }