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.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import junit.framework.Test;
25
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.commons.logging.PathableClassLoader;
28 import org.apache.commons.logging.PathableTestSuite;
29 import org.apache.commons.logging.impl.SimpleLog;
30
31
32 /***
33 * <p>TestCase for simple logging when running with custom configuration
34 * properties.</p>
35 *
36 * @author Craig R. McClanahan
37 * @version $Revision: 370012 $ $Date: 2006-01-18 02:34:05 +0000 (Wed, 18 Jan 2006) $
38 */
39 public class CustomConfigTestCase extends DefaultConfigTestCase {
40
41
42
43
44
45 /***
46 * <p>The expected log records.</p>
47 */
48 protected List expected;
49
50
51 /***
52 * <p>The message levels that should have been logged.</p>
53 */
54
55
56
57
58
59
60 /***
61 * <p>The message strings that should have been logged.</p>
62 */
63 protected String testMessages[] =
64 { "debug", "info", "warn", "error", "fatal" };
65
66
67
68
69 /***
70 * Set system properties that will control the LogFactory/Log objects
71 * when they are created. Subclasses can override this method to
72 * define properties that suit them.
73 */
74 public void setProperties() {
75 System.setProperty(
76 "org.apache.commons.logging.Log",
77 "org.apache.commons.logging.simple.DecoratedSimpleLog");
78 System.setProperty(
79 "org.apache.commons.logging.simplelog.defaultlog",
80 "debug");
81 }
82
83 /***
84 * Set up instance variables required by this test case.
85 */
86 public void setUp() throws Exception {
87 LogFactory.releaseAll();
88 setProperties();
89 expected = new ArrayList();
90 setUpFactory();
91 setUpLog("DecoratedLogger");
92 }
93
94
95 /***
96 * Return the tests included in this test suite.
97 * <p>
98 * We need to use a PathableClassLoader here because the SimpleLog class
99 * is a pile of junk and chock-full of static variables. Any other test
100 * (like simple.CustomConfigTestCase) that has used the SimpleLog class
101 * will already have caused it to do once-only initialisation that we
102 * can't reset, even by calling LogFactory.releaseAll, because of those
103 * ugly statics. The only clean solution is to load a clean copy of
104 * commons-logging including SimpleLog via a nice clean classloader.
105 * Or we could fix SimpleLog to be sane...
106 */
107 public static Test suite() throws Exception {
108 Class thisClass = CustomConfigTestCase.class;
109
110 PathableClassLoader loader = new PathableClassLoader(null);
111 loader.useSystemLoader("junit.");
112 loader.addLogicalLib("testclasses");
113 loader.addLogicalLib("commons-logging");
114
115 Class testClass = loader.loadClass(thisClass.getName());
116 return new PathableTestSuite(testClass, loader);
117 }
118
119 /***
120 * Tear down instance variables required by this test case.
121 */
122 public void tearDown() {
123 super.tearDown();
124 expected = null;
125 }
126
127
128
129
130
131
132 public void testExceptionMessages() throws Exception {
133
134 ((DecoratedSimpleLog) log).clearCache();
135 logExceptionMessages();
136 checkExpected();
137
138 }
139
140
141
142 public void testPlainMessages() throws Exception {
143
144 ((DecoratedSimpleLog) log).clearCache();
145 logPlainMessages();
146 checkExpected();
147
148 }
149
150
151
152 public void testSerializable() throws Exception {
153
154 ((DecoratedSimpleLog) log).clearCache();
155 logPlainMessages();
156 super.testSerializable();
157 logExceptionMessages();
158 checkExpected();
159
160 }
161
162
163
164
165
166
167 protected void checkDecorated() {
168
169 assertNotNull("Log exists", log);
170 assertEquals("Log class",
171 "org.apache.commons.logging.simple.DecoratedSimpleLog",
172 log.getClass().getName());
173
174
175 assertTrue(log.isDebugEnabled());
176 assertTrue(log.isErrorEnabled());
177 assertTrue(log.isFatalEnabled());
178 assertTrue(log.isInfoEnabled());
179 assertTrue(!log.isTraceEnabled());
180 assertTrue(log.isWarnEnabled());
181
182
183 assertEquals(SimpleLog.LOG_LEVEL_DEBUG, ((SimpleLog) log).getLevel());
184
185
186 checkDecoratedDateTime();
187 assertEquals("DecoratedLogger",
188 ((DecoratedSimpleLog) log).getLogName());
189 checkShowDateTime();
190 assertTrue(((DecoratedSimpleLog) log).getShowShortName());
191
192 }
193
194 /*** Hook for subclassses */
195 protected void checkShowDateTime() {
196 assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
197 }
198
199 /*** Hook for subclasses */
200 protected void checkDecoratedDateTime() {
201 assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
202 ((DecoratedSimpleLog) log).getDateTimeFormat());
203 }
204
205
206
207
208 protected void checkExpected() {
209
210 List acts = ((DecoratedSimpleLog) log).getCache();
211 Iterator exps = expected.iterator();
212 int n = 0;
213 while (exps.hasNext()) {
214 LogRecord exp = (LogRecord) exps.next();
215 LogRecord act = (LogRecord) acts.get(n++);
216 assertEquals("Row " + n + " type", exp.type, act.type);
217 assertEquals("Row " + n + " message", exp.message, act.message);
218 assertEquals("Row " + n + " throwable", exp.t, act.t);
219 }
220
221 }
222
223
224
225 protected void checkStandard() {
226
227 checkDecorated();
228
229 }
230
231
232
233 protected void logExceptionMessages() {
234
235
236 Throwable t = new IndexOutOfBoundsException();
237 log.trace("trace", t);
238 log.debug("debug", t);
239 log.info("info", t);
240 log.warn("warn", t);
241 log.error("error", t);
242 log.fatal("fatal", t);
243
244
245 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", t));
246 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", t));
247 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", t));
248 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", t));
249 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", t));
250
251 }
252
253
254
255 protected void logPlainMessages() {
256
257
258 log.trace("trace");
259 log.debug("debug");
260 log.info("info");
261 log.warn("warn");
262 log.error("error");
263 log.fatal("fatal");
264
265
266 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", null));
267 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", null));
268 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", null));
269 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", null));
270 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", null));
271
272 }
273
274
275 }