1   /*
2    * Copyright 2001-2004 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.jdk14;
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  import junit.framework.TestSuite;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
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   * @author Craig R. McClanahan
39   * @version $Revision: 202471 $ $Date: 2005-06-30 04:21:03 +0100 (Thu, 30 Jun 2005) $
40   */
41  
42  public class DefaultConfigTestCase extends TestCase {
43  
44  
45      // ----------------------------------------------------------- Constructors
46  
47  
48      /***
49       * <p>Construct a new instance of this test case.</p>
50       *
51       * @param name Name of the test case
52       */
53      public DefaultConfigTestCase(String name) {
54          super(name);
55      }
56  
57  
58      // ----------------------------------------------------- Instance Variables
59  
60  
61      /***
62       * <p>The {@link LogFactory} implementation we have selected.</p>
63       */
64      protected LogFactory factory = null;
65  
66  
67      /***
68       * <p>The {@link Log} implementation we have selected.</p>
69       */
70      protected Log log = null;
71  
72  
73      // ------------------------------------------- JUnit Infrastructure Methods
74  
75  
76      /***
77       * Set up instance variables required by this test case.
78       */
79      public void setUp() throws Exception {
80          setUpFactory();
81          setUpLog("TestLogger");
82      }
83  
84  
85      /***
86       * Return the tests included in this test suite.
87       */
88      public static Test suite() throws Exception {
89          return (new TestSuite(DefaultConfigTestCase.class));
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 
102     // ----------------------------------------------------------- Test Methods
103 
104 
105     // Test pristine Log instance
106     public void testPristineLog() {
107 
108         checkLog();
109 
110     }
111 
112 
113     // Test pristine LogFactory instance
114     public void testPristineFactory() {
115 
116         assertNotNull("LogFactory exists", factory);
117         assertEquals("LogFactory class",
118                      "org.apache.commons.logging.impl.LogFactoryImpl",
119                      factory.getClass().getName());
120 
121         String names[] = factory.getAttributeNames();
122         assertNotNull("Names exists", names);
123         assertEquals("Names empty", 0, names.length);
124 
125     }
126 
127 
128     // Test Serializability of Log instance
129     public void testSerializable() throws Exception {
130 
131         // Serialize and deserialize the instance
132         ByteArrayOutputStream baos = new ByteArrayOutputStream();
133         ObjectOutputStream oos = new ObjectOutputStream(baos);
134         oos.writeObject(log);
135         oos.close();
136         ByteArrayInputStream bais =
137             new ByteArrayInputStream(baos.toByteArray());
138         ObjectInputStream ois = new ObjectInputStream(bais);
139         log = (Log) ois.readObject();
140         ois.close();
141 
142         // Check the characteristics of the resulting object
143         checkLog();
144 
145     }
146 
147 
148     // -------------------------------------------------------- Support Methods
149 
150 
151 
152     // Check the log instance
153     protected void checkLog() {
154 
155         assertNotNull("Log exists", log);
156         assertEquals("Log class",
157                      "org.apache.commons.logging.impl.Jdk14Logger",
158                      log.getClass().getName());
159 
160         // Can we call level checkers with no exceptions?
161         log.isDebugEnabled();
162         log.isErrorEnabled();
163         log.isFatalEnabled();
164         log.isInfoEnabled();
165         log.isTraceEnabled();
166         log.isWarnEnabled();
167 
168     }
169 
170 
171     // Set up factory instance
172     protected void setUpFactory() throws Exception {
173         factory = LogFactory.getFactory();
174     }
175 
176 
177     // Set up log instance
178     protected void setUpLog(String name) throws Exception {
179         log = LogFactory.getLog(name);
180     }
181 
182 
183 }