KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > logging > simple > DefaultConfigTestCase


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.simple;
18
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
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 import org.apache.commons.logging.impl.SimpleLog;
32
33
34 /**
35  * <p>TestCase for simple logging when running with zero configuration
36  * other than selecting the SimpleLog implementation.</p>
37  *
38  * @author Craig R. McClanahan
39  * @version $Revision: 1.5 $ $Date: 2004/05/29 10:43:35 $
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 JavaDoc 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 JavaDoc {
80         setUpFactory();
81         setUpLog("TestLogger");
82     }
83
84
85     /**
86      * Return the tests included in this test suite.
87      */

88     public static Test suite() {
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 DecoratedSimpleLog instance
106
public void testPristineDecorated() {
107
108         setUpDecorated("DecoratedLogger");
109         checkDecorated();
110
111     }
112
113
114     // Test pristine Log instance
115
public void testPristineLog() {
116
117         checkStandard();
118
119     }
120
121
122     // Test pristine LogFactory instance
123
public void testPristineFactory() {
124
125         assertNotNull("LogFactory exists", factory);
126         assertEquals("LogFactory class",
127                      "org.apache.commons.logging.impl.LogFactoryImpl",
128                      factory.getClass().getName());
129
130         String JavaDoc names[] = factory.getAttributeNames();
131         assertNotNull("Names exists", names);
132         assertEquals("Names empty", 0, names.length);
133
134     }
135
136
137     // Test Serializability of standard instance
138
public void testSerializable() throws Exception JavaDoc {
139
140         // Serialize and deserialize the instance
141
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
142         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
143         oos.writeObject(log);
144         oos.close();
145         ByteArrayInputStream JavaDoc bais =
146             new ByteArrayInputStream JavaDoc(baos.toByteArray());
147         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
148         log = (Log) ois.readObject();
149         ois.close();
150
151         // Check the characteristics of the resulting object
152
checkStandard();
153
154     }
155
156
157     // -------------------------------------------------------- Support Methods
158

159
160
161     // Check the decorated log instance
162
protected void checkDecorated() {
163
164         assertNotNull("Log exists", log);
165         assertEquals("Log class",
166                      "org.apache.commons.logging.simple.DecoratedSimpleLog",
167                      log.getClass().getName());
168
169         // Can we call level checkers with no exceptions?
170
assertTrue(!log.isDebugEnabled());
171         assertTrue(log.isErrorEnabled());
172         assertTrue(log.isFatalEnabled());
173         assertTrue(log.isInfoEnabled());
174         assertTrue(!log.isTraceEnabled());
175         assertTrue(log.isWarnEnabled());
176
177         // Can we retrieve the current log level?
178
assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
179
180         // Can we validate the extra exposed properties?
181
assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
182                      ((DecoratedSimpleLog) log).getDateTimeFormat());
183         assertEquals("DecoratedLogger",
184                      ((DecoratedSimpleLog) log).getLogName());
185         assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
186         assertTrue(((DecoratedSimpleLog) log).getShowShortName());
187
188     }
189
190
191     // Check the standard log instance
192
protected void checkStandard() {
193
194         assertNotNull("Log exists", log);
195         assertEquals("Log class",
196                      "org.apache.commons.logging.impl.SimpleLog",
197                      log.getClass().getName());
198
199         // Can we call level checkers with no exceptions?
200
assertTrue(!log.isDebugEnabled());
201         assertTrue(log.isErrorEnabled());
202         assertTrue(log.isFatalEnabled());
203         assertTrue(log.isInfoEnabled());
204         assertTrue(!log.isTraceEnabled());
205         assertTrue(log.isWarnEnabled());
206
207         // Can we retrieve the current log level?
208
assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
209
210     }
211
212
213     // Set up decorated log instance
214
protected void setUpDecorated(String JavaDoc name) {
215         log = new DecoratedSimpleLog(name);
216     }
217
218
219     // Set up factory instance
220
protected void setUpFactory() throws Exception JavaDoc {
221         factory = LogFactory.getFactory();
222     }
223
224
225     // Set up log instance
226
protected void setUpLog(String JavaDoc name) throws Exception JavaDoc {
227         log = LogFactory.getLog(name);
228     }
229
230
231 }
232
Popular Tags