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 import junit.framework.TestSuite;
26
27 import org.apache.commons.logging.impl.SimpleLog;
28
29
30 /***
31 * <p>TestCase for simple logging when running with custom configuration
32 * properties.</p>
33 *
34 * @author Craig R. McClanahan
35 * @version $Revision: 1.6 $ $Date: 2004/05/30 10:32:25 $
36 */
37 public class CustomConfigTestCase extends DefaultConfigTestCase {
38
39
40
41
42
43 /***
44 * <p>Construct a new instance of this test case.</p>
45 *
46 * @param name Name of the test case
47 */
48 public CustomConfigTestCase(String name) {
49 super(name);
50 }
51
52
53
54
55
56 /***
57 * <p>The expected log records.</p>
58 */
59 protected List expected;
60
61
62 /***
63 * <p>The message levels that should have been logged.</p>
64 */
65
66
67
68
69
70
71 /***
72 * <p>The message strings that should have been logged.</p>
73 */
74 protected String testMessages[] =
75 { "debug", "info", "warn", "error", "fatal" };
76
77
78
79
80
81 /***
82 * Set up instance variables required by this test case.
83 */
84 public void setUp() throws Exception {
85 expected = new ArrayList();
86 setUpFactory();
87 setUpLog("DecoratedLogger");
88 }
89
90
91 /***
92 * Return the tests included in this test suite.
93 */
94 public static Test suite() {
95 return (new TestSuite(CustomConfigTestCase.class));
96 }
97
98 /***
99 * Tear down instance variables required by this test case.
100 */
101 public void tearDown() {
102 super.tearDown();
103 expected = null;
104 }
105
106
107
108
109
110
111 public void testExceptionMessages() throws Exception {
112
113 ((DecoratedSimpleLog) log).clearCache();
114 logExceptionMessages();
115 checkExpected();
116
117 }
118
119
120
121 public void testPlainMessages() throws Exception {
122
123 ((DecoratedSimpleLog) log).clearCache();
124 logPlainMessages();
125 checkExpected();
126
127 }
128
129
130
131 public void testSerializable() throws Exception {
132
133 ((DecoratedSimpleLog) log).clearCache();
134 logPlainMessages();
135 super.testSerializable();
136 logExceptionMessages();
137 checkExpected();
138
139 }
140
141
142
143
144
145
146 protected void checkDecorated() {
147
148 assertNotNull("Log exists", log);
149 assertEquals("Log class",
150 "org.apache.commons.logging.simple.DecoratedSimpleLog",
151 log.getClass().getName());
152
153
154 assertTrue(log.isDebugEnabled());
155 assertTrue(log.isErrorEnabled());
156 assertTrue(log.isFatalEnabled());
157 assertTrue(log.isInfoEnabled());
158 assertTrue(!log.isTraceEnabled());
159 assertTrue(log.isWarnEnabled());
160
161
162 assertEquals(SimpleLog.LOG_LEVEL_DEBUG, ((SimpleLog) log).getLevel());
163
164
165 checkDecoratedDateTime();
166 assertEquals("DecoratedLogger",
167 ((DecoratedSimpleLog) log).getLogName());
168 checkShowDateTime();
169 assertTrue(((DecoratedSimpleLog) log).getShowShortName());
170
171 }
172
173 /*** Hook for subclassses */
174 protected void checkShowDateTime() {
175 assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());
176 }
177
178 /*** Hook for subclasses */
179 protected void checkDecoratedDateTime() {
180 assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz",
181 ((DecoratedSimpleLog) log).getDateTimeFormat());
182 }
183
184
185
186
187 protected void checkExpected() {
188
189 List acts = ((DecoratedSimpleLog) log).getCache();
190 Iterator exps = expected.iterator();
191 int n = 0;
192 while (exps.hasNext()) {
193 LogRecord exp = (LogRecord) exps.next();
194 LogRecord act = (LogRecord) acts.get(n++);
195 assertEquals("Row " + n + " type", exp.type, act.type);
196 assertEquals("Row " + n + " message", exp.message, act.message);
197 assertEquals("Row " + n + " throwable", exp.t, act.t);
198 }
199
200 }
201
202
203
204 protected void checkStandard() {
205
206 checkDecorated();
207
208 }
209
210
211
212 protected void logExceptionMessages() {
213
214
215 Throwable t = new IndexOutOfBoundsException();
216 log.trace("trace", t);
217 log.debug("debug", t);
218 log.info("info", t);
219 log.warn("warn", t);
220 log.error("error", t);
221 log.fatal("fatal", t);
222
223
224 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", t));
225 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", t));
226 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", t));
227 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", t));
228 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", t));
229
230 }
231
232
233
234 protected void logPlainMessages() {
235
236
237 log.trace("trace");
238 log.debug("debug");
239 log.info("info");
240 log.warn("warn");
241 log.error("error");
242 log.fatal("fatal");
243
244
245 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", null));
246 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", null));
247 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", null));
248 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", null));
249 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", null));
250
251 }
252
253
254 }