1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.logging.simple;
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 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
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
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
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() {
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
103
104
105
106 public void testPristineDecorated() {
107
108 setUpDecorated("DecoratedLogger");
109 checkDecorated();
110
111 }
112
113
114
115 public void testPristineLog() {
116
117 checkStandard();
118
119 }
120
121
122
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 names[] = factory.getAttributeNames();
131 assertNotNull("Names exists", names);
132 assertEquals("Names empty", 0, names.length);
133
134 }
135
136
137
138 public void testSerializable() throws Exception {
139
140
141 ByteArrayOutputStream baos = new ByteArrayOutputStream();
142 ObjectOutputStream oos = new ObjectOutputStream(baos);
143 oos.writeObject(log);
144 oos.close();
145 ByteArrayInputStream bais =
146 new ByteArrayInputStream(baos.toByteArray());
147 ObjectInputStream ois = new ObjectInputStream(bais);
148 log = (Log) ois.readObject();
149 ois.close();
150
151
152 checkStandard();
153
154 }
155
156
157
158
159
160
161
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
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
178 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
179
180
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
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
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
208 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
209
210 }
211
212
213
214 protected void setUpDecorated(String name) {
215 log = new DecoratedSimpleLog(name);
216 }
217
218
219
220 protected void setUpFactory() throws Exception {
221 factory = LogFactory.getFactory();
222 }
223
224
225
226 protected void setUpLog(String name) throws Exception {
227 log = LogFactory.getLog(name);
228 }
229
230
231 }