1
2
3
4
5
6
7
8 package org.codehaus.dna.impl;
9
10 import junit.framework.TestCase;
11 import org.apache.log.Hierarchy;
12 import org.apache.log.LogTarget;
13 import org.apache.log.Logger;
14 import org.apache.log.Priority;
15 import org.codehaus.dna.impl.LogkitLogger;
16
17 public class LogkitLoggerTestCase
18 extends TestCase
19 {
20 public void testLogkitLoggerEmptyCtor()
21 throws Exception
22 {
23 try
24 {
25 new LogkitLogger( null );
26 }
27 catch( NullPointerException npe )
28 {
29 assertEquals( "npe.getMessage()", "logger", npe.getMessage() );
30 }
31 }
32
33 public void testLogkitLoggerGetChildLogger()
34 throws Exception
35 {
36 final MockLogTarget target = new MockLogTarget();
37 final LogkitLogger logger = createLogger( target, Priority.DEBUG );
38
39 assertNotSame( "logger.getChildLogger == logger",
40 logger,
41 logger.getChildLogger( "whatever" ) );
42 }
43
44 public void testLogkitLoggerTraceEnabled()
45 throws Exception
46 {
47 final Priority level = Priority.DEBUG;
48 final Priority type = Priority.DEBUG;
49 final String message = "Meep!";
50 final Throwable throwable = null;
51 final boolean output = true;
52
53 final MockLogTarget target = new MockLogTarget();
54 final LogkitLogger logger = createLogger( target, level );
55 logger.trace( message );
56 checkLogger( target, output, message, throwable, type );
57 }
58
59 public void testLogkitLoggerTraceDisabled()
60 throws Exception
61 {
62 final Priority level = Priority.ERROR;
63 final String message = "Meep!";
64
65 final MockLogTarget target = new MockLogTarget();
66 final LogkitLogger logger = createLogger( target, level );
67 logger.trace( message );
68 checkLogger( target, false, null, null, null );
69 }
70
71 public void testLogkitLoggerTraceWithExceptionEnabled()
72 throws Exception
73 {
74 final Priority level = Priority.DEBUG;
75 final Priority type = Priority.DEBUG;
76 final String message = "Meep!";
77 final Throwable throwable = new Throwable();
78 final boolean output = true;
79
80 final MockLogTarget target = new MockLogTarget();
81 final LogkitLogger logger = createLogger( target, level );
82
83 logger.trace( message, throwable );
84 checkLogger( target, output, message, throwable, type );
85 }
86
87 public void testLogkitLoggerTraceWithExceptionDisabled()
88 throws Exception
89 {
90 final Priority level = Priority.ERROR;
91 final String message = "Meep!";
92 final Throwable throwable = new Throwable();
93
94 final MockLogTarget target = new MockLogTarget();
95 final LogkitLogger logger = createLogger( target, level );
96
97 logger.trace( message, throwable );
98 checkLogger( target, false, null, null, null );
99 }
100
101 public void testLogkitLoggerDebugEnabled()
102 throws Exception
103 {
104 final Priority level = Priority.DEBUG;
105 final Priority type = Priority.DEBUG;
106 final String message = "Meep!";
107 final Throwable throwable = null;
108 final boolean output = true;
109
110 final MockLogTarget target = new MockLogTarget();
111 final LogkitLogger logger = createLogger( target, level );
112 logger.debug( message );
113 checkLogger( target, output, message, throwable, type );
114 }
115
116 public void testLogkitLoggerDebugDisabled()
117 throws Exception
118 {
119 final Priority level = Priority.ERROR;
120 final String message = "Meep!";
121
122 final MockLogTarget target = new MockLogTarget();
123 final LogkitLogger logger = createLogger( target, level );
124 logger.debug( message );
125 checkLogger( target, false, null, null, null );
126 }
127
128 public void testLogkitLoggerDebugWithExceptionEnabled()
129 throws Exception
130 {
131 final Priority level = Priority.DEBUG;
132 final Priority type = Priority.DEBUG;
133 final String message = "Meep!";
134 final Throwable throwable = new Throwable();
135 final boolean output = true;
136
137 final MockLogTarget target = new MockLogTarget();
138 final LogkitLogger logger = createLogger( target, level );
139 logger.debug( message, throwable );
140 checkLogger( target, output, message, throwable, type );
141 }
142
143 public void testLogkitLoggerDebugWithExceptionDisabled()
144 throws Exception
145 {
146 final Priority level = Priority.ERROR;
147 final String message = "Meep!";
148 final Throwable throwable = new Throwable();
149
150 final MockLogTarget target = new MockLogTarget();
151 final LogkitLogger logger = createLogger( target, level );
152 logger.debug( message, throwable );
153 checkLogger( target, false, null, null, null );
154 }
155
156 public void testLogkitLoggerInfoEnabled()
157 throws Exception
158 {
159 final Priority level = Priority.DEBUG;
160 final Priority type = Priority.INFO;
161 final String message = "Meep!";
162 final Throwable throwable = null;
163 final boolean output = true;
164
165 final MockLogTarget target = new MockLogTarget();
166 final LogkitLogger logger = createLogger( target, level );
167 logger.info( message );
168 checkLogger( target, output, message, throwable, type );
169 }
170
171 public void testLogkitLoggerInfoDisabled()
172 throws Exception
173 {
174 final Priority level = Priority.ERROR;
175 final String message = "Meep!";
176
177 final MockLogTarget target = new MockLogTarget();
178 final LogkitLogger logger = createLogger( target, level );
179 logger.info( message );
180 checkLogger( target, false, null, null, null );
181 }
182
183 public void testLogkitLoggerInfoWithExceptionEnabled()
184 throws Exception
185 {
186 final Priority level = Priority.DEBUG;
187 final Priority type = Priority.INFO;
188 final String message = "Meep!";
189 final Throwable throwable = new Throwable();
190 final boolean output = true;
191
192 final MockLogTarget target = new MockLogTarget();
193 final LogkitLogger logger = createLogger( target, level );
194 logger.info( message, throwable );
195 checkLogger( target, output, message, throwable, type );
196 }
197
198 public void testLogkitLoggerInfoWithExceptionDisabled()
199 throws Exception
200 {
201 final Priority level = Priority.ERROR;
202 final String message = "Meep!";
203 final Throwable throwable = new Throwable();
204
205 final MockLogTarget target = new MockLogTarget();
206 final LogkitLogger logger = createLogger( target, level );
207 logger.info( message, throwable );
208 checkLogger( target, false, null, null, null );
209 }
210
211 public void testLogkitLoggerWarnEnabled()
212 throws Exception
213 {
214 final Priority level = Priority.DEBUG;
215 final Priority type = Priority.WARN;
216 final String message = "Meep!";
217 final Throwable throwable = null;
218 final boolean output = true;
219
220 final MockLogTarget target = new MockLogTarget();
221 final LogkitLogger logger = createLogger( target, level );
222 logger.warn( message );
223 checkLogger( target, output, message, throwable, type );
224 }
225
226 public void testLogkitLoggerWarnDisabled()
227 throws Exception
228 {
229 final Priority level = Priority.ERROR;
230 final String message = "Meep!";
231
232 final MockLogTarget target = new MockLogTarget();
233 final LogkitLogger logger = createLogger( target, level );
234 logger.warn( message );
235 checkLogger( target, false, null, null, null );
236 }
237
238 public void testLogkitLoggerWarnWithExceptionEnabled()
239 throws Exception
240 {
241 final Priority level = Priority.DEBUG;
242 final Priority type = Priority.WARN;
243 final String message = "Meep!";
244 final Throwable throwable = new Throwable();
245 final boolean output = true;
246
247 final MockLogTarget target = new MockLogTarget();
248 final LogkitLogger logger = createLogger( target, level );
249 logger.warn( message, throwable );
250 checkLogger( target, output, message, throwable, type );
251 }
252
253 public void testLogkitLoggerWarnWithExceptionDisabled()
254 throws Exception
255 {
256 final Priority level = Priority.ERROR;
257 final String message = "Meep!";
258 final Throwable throwable = new Throwable();
259
260 final MockLogTarget target = new MockLogTarget();
261 final LogkitLogger logger = createLogger( target, level );
262 logger.warn( message, throwable );
263 checkLogger( target, false, null, null, null );
264 }
265
266 public void testLogkitLoggerErrorEnabled()
267 throws Exception
268 {
269 final Priority level = Priority.DEBUG;
270 final Priority type = Priority.ERROR;
271 final String message = "Meep!";
272 final Throwable throwable = null;
273 final boolean output = true;
274
275 final MockLogTarget target = new MockLogTarget();
276 final LogkitLogger logger = createLogger( target, level );
277 logger.error( message );
278 checkLogger( target, output, message, throwable, type );
279 }
280
281 public void testLogkitLoggerErrorWithExceptionEnabled()
282 throws Exception
283 {
284 final Priority level = Priority.DEBUG;
285 final Priority type = Priority.ERROR;
286 final String message = "Meep!";
287 final Throwable throwable = new Throwable();
288 final boolean output = true;
289
290 final MockLogTarget target = new MockLogTarget();
291 final LogkitLogger logger = createLogger( target, level );
292 logger.error( message, throwable );
293 checkLogger( target, output, message, throwable, type );
294 }
295
296 public void testConsoleLevelComparisonWithDebugEnabled()
297 throws Exception
298 {
299 final MockLogTarget target = new MockLogTarget();
300 final LogkitLogger logger = createLogger( target, Priority.DEBUG );
301
302 assertEquals( "logger.isTraceEnabled()", true, logger.isTraceEnabled() );
303 assertEquals( "logger.isDebugEnabled()", true, logger.isDebugEnabled() );
304 assertEquals( "logger.isInfoEnabled()", true, logger.isInfoEnabled() );
305 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() );
306 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() );
307 }
308
309 public void testConsoleLevelComparisonWithInfoEnabled()
310 throws Exception
311 {
312 final MockLogTarget target = new MockLogTarget();
313 final LogkitLogger logger = createLogger( target, Priority.INFO );
314
315 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() );
316 assertEquals( "logger.isDebugEnabled()", false, logger.isDebugEnabled() );
317 assertEquals( "logger.isInfoEnabled()", true, logger.isInfoEnabled() );
318 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() );
319 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() );
320 }
321
322 public void testConsoleLevelComparisonWithWarnEnabled()
323 throws Exception
324 {
325 final MockLogTarget target = new MockLogTarget();
326 final LogkitLogger logger = createLogger( target, Priority.WARN );
327
328 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() );
329 assertEquals( "logger.isDebugEnabled()", false, logger.isDebugEnabled() );
330 assertEquals( "logger.isInfoEnabled()", false, logger.isInfoEnabled() );
331 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() );
332 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() );
333 }
334
335 public void testConsoleLevelComparisonWithErrorEnabled()
336 throws Exception
337 {
338 final MockLogTarget target = new MockLogTarget();
339 final LogkitLogger logger = createLogger( target, Priority.ERROR );
340
341 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() );
342 assertEquals( "logger.isDebugEnabled()", false, logger.isDebugEnabled() );
343 assertEquals( "logger.isInfoEnabled()", false, logger.isInfoEnabled() );
344 assertEquals( "logger.isWarnEnabled()", false, logger.isWarnEnabled() );
345 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() );
346 }
347
348 private LogkitLogger createLogger( final MockLogTarget target,
349 final Priority priority )
350 {
351 final Hierarchy hierarchy = new Hierarchy();
352 final Logger logkitLogger = hierarchy.getLoggerFor( "test" );
353 logkitLogger.setLogTargets( new LogTarget[]{target} );
354 logkitLogger.setPriority( priority );
355 final LogkitLogger logger = new LogkitLogger( logkitLogger );
356 return logger;
357 }
358
359 private void checkLogger( final MockLogTarget target,
360 final boolean output,
361 final String message,
362 final Throwable throwable,
363 final Priority priority )
364 {
365 assertEquals( "logger.m_message == message", message, target.m_message );
366 assertEquals( "logger.m_output == output", output, target.m_output );
367 assertEquals( "logger.m_throwable == null", throwable, target.m_throwable );
368 assertEquals( "logger.m_priority == null", priority, target.m_priority );
369 }
370 }