1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.logging.simple;
19
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.commons.logging.PathableClassLoader;
32 import org.apache.commons.logging.PathableTestSuite;
33 import org.apache.commons.logging.impl.SimpleLog;
34
35
36
37
38
39
40
41
42
43
44 public class DefaultConfigTestCase extends TestCase {
45
46
47
48
49
50
51
52
53 protected LogFactory factory = null;
54
55
56
57
58
59 protected Log log = null;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public static Test suite() throws Exception {
78 Class thisClass = DefaultConfigTestCase.class;
79
80 PathableClassLoader loader = new PathableClassLoader(null);
81 loader.useExplicitLoader("junit.", Test.class.getClassLoader());
82 loader.addLogicalLib("testclasses");
83 loader.addLogicalLib("commons-logging");
84
85 Class testClass = loader.loadClass(thisClass.getName());
86 return new PathableTestSuite(testClass, loader);
87 }
88
89
90
91
92
93
94 public void setProperties() {
95 System.setProperty(
96 "org.apache.commons.logging.Log",
97 "org.apache.commons.logging.impl.SimpleLog");
98 }
99
100
101
102
103 public void setUp() throws Exception {
104 LogFactory.releaseAll();
105 setProperties();
106 setUpFactory();
107 setUpLog("TestLogger");
108 }
109
110
111
112
113 public void tearDown() {
114 log = null;
115 factory = null;
116 LogFactory.releaseAll();
117 }
118
119
120
121
122
123
124 public void testPristineDecorated() {
125
126 setUpDecorated("DecoratedLogger");
127 checkDecorated();
128
129 }
130
131
132
133 public void testPristineLog() {
134
135 checkStandard();
136
137 }
138
139
140
141 public void testPristineFactory() {
142
143 assertNotNull("LogFactory exists", factory);
144 assertEquals("LogFactory class",
145 "org.apache.commons.logging.impl.LogFactoryImpl",
146 factory.getClass().getName());
147
148 String names[] = factory.getAttributeNames();
149 assertNotNull("Names exists", names);
150 assertEquals("Names empty", 0, names.length);
151
152 }
153
154
155
156 public void testSerializable() throws Exception {
157
158
159 ByteArrayOutputStream baos = new ByteArrayOutputStream();
160 ObjectOutputStream oos = new ObjectOutputStream(baos);
161 oos.writeObject(log);
162 oos.close();
163 ByteArrayInputStream bais =
164 new ByteArrayInputStream(baos.toByteArray());
165 ObjectInputStream ois = new ObjectInputStream(bais);
166 log = (Log) ois.readObject();
167 ois.close();
168
169
170 checkStandard();
171
172 }
173
174
175
176
177
178
179
180 protected void checkDecorated() {
181
182 assertNotNull("Log exists", log);
183 assertEquals("Log class",
184 "org.apache.commons.logging.simple.DecoratedSimpleLog",
185 log.getClass().getName());
186
187
188 assertTrue(!log.isDebugEnabled());
189 assertTrue(log.isErrorEnabled());
190 assertTrue(log.isFatalEnabled());
191 assertTrue(log.isInfoEnabled());
192 assertTrue(!log.isTraceEnabled());
193 assertTrue(log.isWarnEnabled());
194
195
196 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
197
198
199 assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
200 ((DecoratedSimpleLog) log).getDateTimeFormat());
201 assertEquals("DecoratedLogger",
202 ((DecoratedSimpleLog) log).getLogName());
203 assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
204 assertTrue(((DecoratedSimpleLog) log).getShowShortName());
205
206 }
207
208
209
210 protected void checkStandard() {
211
212 assertNotNull("Log exists", log);
213 assertEquals("Log class",
214 "org.apache.commons.logging.impl.SimpleLog",
215 log.getClass().getName());
216
217
218 assertTrue(!log.isDebugEnabled());
219 assertTrue(log.isErrorEnabled());
220 assertTrue(log.isFatalEnabled());
221 assertTrue(log.isInfoEnabled());
222 assertTrue(!log.isTraceEnabled());
223 assertTrue(log.isWarnEnabled());
224
225
226 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel());
227
228 }
229
230
231
232 protected void setUpDecorated(String name) {
233 log = new DecoratedSimpleLog(name);
234 }
235
236
237
238 protected void setUpFactory() throws Exception {
239 factory = LogFactory.getFactory();
240 }
241
242
243
244 protected void setUpLog(String name) throws Exception {
245 log = LogFactory.getLog(name);
246 }
247
248
249 }