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.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import junit.framework.Test;
26
27 import org.apache.commons.logging.DummyException;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.commons.logging.PathableClassLoader;
30 import org.apache.commons.logging.PathableTestSuite;
31 import org.apache.commons.logging.impl.SimpleLog;
32
33
34
35
36
37
38
39
40
41 public class CustomConfigTestCase extends DefaultConfigTestCase {
42
43
44
45
46
47
48
49
50 protected List expected;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 protected String testMessages[] =
66 { "debug", "info", "warn", "error", "fatal" };
67
68
69
70
71
72
73
74
75
76 public void setProperties() {
77 System.setProperty(
78 "org.apache.commons.logging.Log",
79 "org.apache.commons.logging.simple.DecoratedSimpleLog");
80 System.setProperty(
81 "org.apache.commons.logging.simplelog.defaultlog",
82 "debug");
83 }
84
85
86
87
88 public void setUp() throws Exception {
89 LogFactory.releaseAll();
90 setProperties();
91 expected = new ArrayList();
92 setUpFactory();
93 setUpLog("DecoratedLogger");
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public static Test suite() throws Exception {
110 Class thisClass = CustomConfigTestCase.class;
111
112 PathableClassLoader loader = new PathableClassLoader(null);
113 loader.useExplicitLoader("junit.", Test.class.getClassLoader());
114 loader.addLogicalLib("testclasses");
115 loader.addLogicalLib("commons-logging");
116
117 Class testClass = loader.loadClass(thisClass.getName());
118 return new PathableTestSuite(testClass, loader);
119 }
120
121
122
123
124 public void tearDown() {
125 super.tearDown();
126 expected = null;
127 }
128
129
130
131
132
133
134 public void testExceptionMessages() throws Exception {
135
136 ((DecoratedSimpleLog) log).clearCache();
137 logExceptionMessages();
138 checkExpected();
139
140 }
141
142
143
144 public void testPlainMessages() throws Exception {
145
146 ((DecoratedSimpleLog) log).clearCache();
147 logPlainMessages();
148 checkExpected();
149
150 }
151
152
153
154 public void testSerializable() throws Exception {
155
156 ((DecoratedSimpleLog) log).clearCache();
157 logPlainMessages();
158 super.testSerializable();
159 logExceptionMessages();
160 checkExpected();
161
162 }
163
164
165
166
167
168
169 protected void checkDecorated() {
170
171 assertNotNull("Log exists", log);
172 assertEquals("Log class",
173 "org.apache.commons.logging.simple.DecoratedSimpleLog",
174 log.getClass().getName());
175
176
177 assertTrue(log.isDebugEnabled());
178 assertTrue(log.isErrorEnabled());
179 assertTrue(log.isFatalEnabled());
180 assertTrue(log.isInfoEnabled());
181 assertTrue(!log.isTraceEnabled());
182 assertTrue(log.isWarnEnabled());
183
184
185 assertEquals(SimpleLog.LOG_LEVEL_DEBUG, ((SimpleLog) log).getLevel());
186
187
188 checkDecoratedDateTime();
189 assertEquals("DecoratedLogger",
190 ((DecoratedSimpleLog) log).getLogName());
191 checkShowDateTime();
192 assertTrue(((DecoratedSimpleLog) log).getShowShortName());
193
194 }
195
196
197 protected void checkShowDateTime() {
198 assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
199 }
200
201
202 protected void checkDecoratedDateTime() {
203 assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
204 ((DecoratedSimpleLog) log).getDateTimeFormat());
205 }
206
207
208
209
210 protected void checkExpected() {
211
212 List acts = ((DecoratedSimpleLog) log).getCache();
213 Iterator exps = expected.iterator();
214 int n = 0;
215 while (exps.hasNext()) {
216 LogRecord exp = (LogRecord) exps.next();
217 LogRecord act = (LogRecord) acts.get(n++);
218 assertEquals("Row " + n + " type", exp.type, act.type);
219 assertEquals("Row " + n + " message", exp.message, act.message);
220 assertEquals("Row " + n + " throwable", exp.t, act.t);
221 }
222
223 }
224
225
226
227 protected void checkStandard() {
228
229 checkDecorated();
230
231 }
232
233
234
235 protected void logExceptionMessages() {
236
237
238 Throwable t = new DummyException();
239 log.trace("trace", t);
240 log.debug("debug", t);
241 log.info("info", t);
242 log.warn("warn", t);
243 log.error("error", t);
244 log.fatal("fatal", t);
245
246
247 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", t));
248 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", t));
249 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", t));
250 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", t));
251 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", t));
252
253 }
254
255
256
257 protected void logPlainMessages() {
258
259
260 log.trace("trace");
261 log.debug("debug");
262 log.info("info");
263 log.warn("warn");
264 log.error("error");
265 log.fatal("fatal");
266
267
268 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", null));
269 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", null));
270 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", null));
271 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", null));
272 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", null));
273
274 }
275
276
277 }