1 /*
2 * Copyright (C) The JContainer Group. All rights reserved.
3 *
4 * This software is published under the terms of the JContainer
5 * Software License version 1.1, a copy of which has been included
6 * with this distribution in the LICENSE.txt file.
7 */
8 package org.jcontainer.dna.impl;
9
10 import junit.framework.TestCase;
11 import java.io.PrintStream;
12 import java.io.ByteArrayOutputStream;
13
14 public class ConsoleLoggerTestCase
15 extends TestCase
16 {
17 public void testConsoleWithEmptyOutput()
18 throws Exception
19 {
20 try
21 {
22 new ConsoleLogger( MockConsoleLogger.LEVEL_ALL, null );
23 }
24 catch( final NullPointerException npe )
25 {
26 assertEquals( "npe.message", "output", npe.getMessage() );
27 return;
28 }
29 fail( "Expected to fail due to NPE in ctor" );
30 }
31
32 public void testMockConsoleEmptyCtor()
33 throws Exception
34 {
35 final MockConsoleLogger logger = new MockConsoleLogger();
36 assertEquals( "logger.level",
37 MockConsoleLogger.LEVEL_ALL,
38 logger.getLevel() );
39 }
40
41 public void testMockConsoleGetChildLogger()
42 throws Exception
43 {
44 final MockConsoleLogger logger = new MockConsoleLogger();
45 assertEquals( "logger.getChildLogger == logger",
46 logger,
47 logger.getChildLogger( "whatever" ) );
48 }
49
50 public void testMockConsoleOutputToConsole()
51 throws Exception
52 {
53 final ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream();
54 final PrintStream output = new PrintStream( arrayOutput );
55 final ConsoleLogger logger = new ConsoleLogger( MockConsoleLogger.LEVEL_ALL, output );
56 logger.debug( "ignore me!", null );
57 final String message = arrayOutput.toString();
58 final String expectedMessage =
59 "[DEBUG] ignore me!" + System.getProperty( "line.separator" );
60 assertEquals( "message", expectedMessage, message );
61 }
62
63 public void testMockConsoleOutputToConsoleWithException()
64 throws Exception
65 {
66 final ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream();
67 final PrintStream output = new PrintStream( arrayOutput );
68 final ConsoleLogger logger = new ConsoleLogger( MockConsoleLogger.LEVEL_ALL, output );
69 logger.debug( "ignore me!", new Throwable( "Ignore me aswell!" ) );
70 final String message = arrayOutput.toString();
71 final String expectedMessage =
72 "[DEBUG] ignore me!" + System.getProperty( "line.separator" );
73 assertTrue( "message", message.startsWith( expectedMessage ) );
74 assertTrue( "throwable message", -1 != message.indexOf( "Ignore me aswell!" ) );
75 assertTrue( "throwable", -1 != message.indexOf( Throwable.class.getName() ) );
76 }
77
78 public void testMockConsoleTraceEnabled()
79 throws Exception
80 {
81 final int level = MockConsoleLogger.LEVEL_ALL;
82 final String message = "Meep!";
83 final String type = "TRACE";
84 final Throwable throwable = null;
85 final boolean output = true;
86
87 final MockConsoleLogger logger = new MockConsoleLogger( level );
88 logger.trace( message );
89 checkLogger( logger, output, message, throwable, type );
90 }
91
92 public void testMockConsoleTraceDisabled()
93 throws Exception
94 {
95 final int level = MockConsoleLogger.LEVEL_NONE;
96 final String message = "Meep!";
97 final MockConsoleLogger logger = new MockConsoleLogger( level );
98 logger.trace( message );
99 checkLogger( logger, false, null, null, null );
100 }
101
102 public void testMockConsoleTraceWithExceptionEnabled()
103 throws Exception
104 {
105 final int level = MockConsoleLogger.LEVEL_ALL;
106 final String message = "Meep!";
107 final String type = "TRACE";
108 final Throwable throwable = new Throwable();
109 final boolean output = true;
110
111 final MockConsoleLogger logger = new MockConsoleLogger( level );
112 logger.trace( message, throwable );
113 checkLogger( logger, output, message, throwable, type );
114 }
115
116 public void testMockConsoleTraceWithExceptionDisabled()
117 throws Exception
118 {
119 final int level = MockConsoleLogger.LEVEL_NONE;
120 final String message = "Meep!";
121 final Throwable throwable = new Throwable();
122 final MockConsoleLogger logger = new MockConsoleLogger( level );
123 logger.trace( message, throwable );
124 checkLogger( logger, false, null, null, null );
125 }
126
127 public void testMockConsoleDebugEnabled()
128 throws Exception
129 {
130 final int level = MockConsoleLogger.LEVEL_ALL;
131 final String message = "Meep!";
132 final String type = "DEBUG";
133 final Throwable throwable = null;
134 final boolean output = true;
135
136 final MockConsoleLogger logger = new MockConsoleLogger( level );
137 logger.debug( message );
138 checkLogger( logger, output, message, throwable, type );
139 }
140
141 public void testMockConsoleDebugDisabled()
142 throws Exception
143 {
144 final int level = MockConsoleLogger.LEVEL_NONE;
145 final String message = "Meep!";
146 final MockConsoleLogger logger = new MockConsoleLogger( level );
147 logger.debug( message );
148 checkLogger( logger, false, null, null, null );
149 }
150
151 public void testMockConsoleDebugWithExceptionEnabled()
152 throws Exception
153 {
154 final int level = MockConsoleLogger.LEVEL_ALL;
155 final String message = "Meep!";
156 final String type = "DEBUG";
157 final Throwable throwable = new Throwable();
158 final boolean output = true;
159
160 final MockConsoleLogger logger = new MockConsoleLogger( level );
161 logger.debug( message, throwable );
162 checkLogger( logger, output, message, throwable, type );
163 }
164
165 public void testMockConsoleDebugWithExceptionDisabled()
166 throws Exception
167 {
168 final int level = MockConsoleLogger.LEVEL_NONE;
169 final String message = "Meep!";
170 final Throwable throwable = new Throwable();
171 final MockConsoleLogger logger = new MockConsoleLogger( level );
172 logger.debug( message, throwable );
173 checkLogger( logger, false, null, null, null );
174 }
175
176 public void testMockConsoleInfoEnabled()
177 throws Exception
178 {
179 final int level = MockConsoleLogger.LEVEL_ALL;
180 final String message = "Meep!";
181 final String type = "INFO";
182 final Throwable throwable = null;
183 final boolean output = true;
184
185 final MockConsoleLogger logger = new MockConsoleLogger( level );
186 logger.info( message );
187 checkLogger( logger, output, message, throwable, type );
188 }
189
190 public void testMockConsoleInfoDisabled()
191 throws Exception
192 {
193 final int level = MockConsoleLogger.LEVEL_NONE;
194 final String message = "Meep!";
195 final MockConsoleLogger logger = new MockConsoleLogger( level );
196 logger.info( message );
197 checkLogger( logger, false, null, null, null );
198 }
199
200 public void testMockConsoleInfoWithExceptionEnabled()
201 throws Exception
202 {
203 final int level = MockConsoleLogger.LEVEL_ALL;
204 final String message = "Meep!";
205 final String type = "INFO";
206 final Throwable throwable = new Throwable();
207 final boolean output = true;
208
209 final MockConsoleLogger logger = new MockConsoleLogger( level );
210 logger.info( message, throwable );
211 checkLogger( logger, output, message, throwable, type );
212 }
213
214 public void testMockConsoleInfoWithExceptionDisabled()
215 throws Exception
216 {
217 final int level = MockConsoleLogger.LEVEL_NONE;
218 final String message = "Meep!";
219 final Throwable throwable = new Throwable();
220 final MockConsoleLogger logger = new MockConsoleLogger( level );
221 logger.info( message, throwable );
222 checkLogger( logger, false, null, null, null );
223 }
224
225 public void testMockConsoleWarnEnabled()
226 throws Exception
227 {
228 final int level = MockConsoleLogger.LEVEL_ALL;
229 final String message = "Meep!";
230 final String type = "WARN";
231 final Throwable throwable = null;
232 final boolean output = true;
233
234 final MockConsoleLogger logger = new MockConsoleLogger( level );
235 logger.warn( message );
236 checkLogger( logger, output, message, throwable, type );
237 }
238
239 public void testMockConsoleWarnDisabled()
240 throws Exception
241 {
242 final int level = MockConsoleLogger.LEVEL_NONE;
243 final String message = "Meep!";
244 final MockConsoleLogger logger = new MockConsoleLogger( level );
245 logger.warn( message );
246 checkLogger( logger, false, null, null, null );
247 }
248
249 public void testMockConsoleWarnWithExceptionEnabled()
250 throws Exception
251 {
252 final int level = MockConsoleLogger.LEVEL_ALL;
253 final String message = "Meep!";
254 final String type = "WARN";
255 final Throwable throwable = new Throwable();
256 final boolean output = true;
257
258 final MockConsoleLogger logger = new MockConsoleLogger( level );
259 logger.warn( message, throwable );
260 checkLogger( logger, output, message, throwable, type );
261 }
262
263 public void testMockConsoleWarnWithExceptionDisabled()
264 throws Exception
265 {
266 final int level = MockConsoleLogger.LEVEL_NONE;
267 final String message = "Meep!";
268 final Throwable throwable = new Throwable();
269 final MockConsoleLogger logger = new MockConsoleLogger( level );
270 logger.warn( message, throwable );
271 checkLogger( logger, false, null, null, null );
272 }
273
274 public void testMockConsoleErrorEnabled()
275 throws Exception
276 {
277 final int level = MockConsoleLogger.LEVEL_ALL;
278 final String message = "Meep!";
279 final String type = "ERROR";
280 final Throwable throwable = null;
281 final boolean output = true;
282
283 final MockConsoleLogger logger = new MockConsoleLogger( level );
284 logger.error( message );
285 checkLogger( logger, output, message, throwable, type );
286 }
287
288 public void testMockConsoleErrorDisabled()
289 throws Exception
290 {
291 final int level = MockConsoleLogger.LEVEL_NONE;
292 final String message = "Meep!";
293 final MockConsoleLogger logger = new MockConsoleLogger( level );
294 logger.error( message );
295 checkLogger( logger, false, null, null, null );
296 }
297
298 public void testMockConsoleErrorWithExceptionEnabled()
299 throws Exception
300 {
301 final int level = MockConsoleLogger.LEVEL_ALL;
302 final String message = "Meep!";
303 final String type = "ERROR";
304 final Throwable throwable = new Throwable();
305 final boolean output = true;
306
307 final MockConsoleLogger logger = new MockConsoleLogger( level );
308 logger.error( message, throwable );
309 checkLogger( logger, output, message, throwable, type );
310 }
311
312 public void testMockConsoleErrorWithExceptionDisabled()
313 throws Exception
314 {
315 final int level = MockConsoleLogger.LEVEL_NONE;
316 final String message = "Meep!";
317 final Throwable throwable = new Throwable();
318 final MockConsoleLogger logger = new MockConsoleLogger( level );
319 logger.error( message, throwable );
320 checkLogger( logger, false, null, null, null );
321 }
322
323 public void testConsoleLevelComparisonWithAll()
324 throws Exception
325 {
326 final MockConsoleLogger logger = new MockConsoleLogger( MockConsoleLogger.LEVEL_ALL );
327 assertEquals( "logger.isTraceEnabled()", true, logger.isTraceEnabled() );
328 assertEquals( "logger.isDebugEnabled()", true, logger.isDebugEnabled() );
329 assertEquals( "logger.isInfoEnabled()", true, logger.isInfoEnabled() );
330 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() );
331 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() );
332 }
333
334 public void testConsoleLevelComparisonWithNone()
335 throws Exception
336 {
337 final MockConsoleLogger logger = new MockConsoleLogger( MockConsoleLogger.LEVEL_NONE );
338 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() );
339 assertEquals( "logger.isDebugEnabled()", false, logger.isDebugEnabled() );
340 assertEquals( "logger.isInfoEnabled()", false, logger.isInfoEnabled() );
341 assertEquals( "logger.isWarnEnabled()", false, logger.isWarnEnabled() );
342 assertEquals( "logger.isErrorEnabled()", false, logger.isErrorEnabled() );
343 }
344
345 public void testConsoleLevelComparisonWithTraceEnabled()
346 throws Exception
347 {
348 final MockConsoleLogger logger = new MockConsoleLogger( MockConsoleLogger.LEVEL_TRACE );
349 assertEquals( "logger.isTraceEnabled()", true, logger.isTraceEnabled() );
350 assertEquals( "logger.isDebugEnabled()", true, logger.isDebugEnabled() );
351 assertEquals( "logger.isInfoEnabled()", true, logger.isInfoEnabled() );
352 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() );
353 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() );
354 }
355
356 public void testConsoleLevelComparisonWithDebugEnabled()
357 throws Exception
358 {
359 final MockConsoleLogger logger = new MockConsoleLogger( MockConsoleLogger.LEVEL_DEBUG );
360 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() );
361 assertEquals( "logger.isDebugEnabled()", true, logger.isDebugEnabled() );
362 assertEquals( "logger.isInfoEnabled()", true, logger.isInfoEnabled() );
363 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() );
364 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() );
365 }
366
367 public void testConsoleLevelComparisonWithInfoEnabled()
368 throws Exception
369 {
370 final MockConsoleLogger logger = new MockConsoleLogger( MockConsoleLogger.LEVEL_INFO );
371 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() );
372 assertEquals( "logger.isDebugEnabled()", false, logger.isDebugEnabled() );
373 assertEquals( "logger.isInfoEnabled()", true, logger.isInfoEnabled() );
374 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() );
375 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() );
376 }
377
378 public void testConsoleLevelComparisonWithWarnEnabled()
379 throws Exception
380 {
381 final MockConsoleLogger logger = new MockConsoleLogger( MockConsoleLogger.LEVEL_WARN );
382 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() );
383 assertEquals( "logger.isDebugEnabled()", false, logger.isDebugEnabled() );
384 assertEquals( "logger.isInfoEnabled()", false, logger.isInfoEnabled() );
385 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() );
386 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() );
387 }
388
389 public void testConsoleLevelComparisonWithErrorEnabled()
390 throws Exception
391 {
392 final MockConsoleLogger logger = new MockConsoleLogger( MockConsoleLogger.LEVEL_ERROR );
393 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() );
394 assertEquals( "logger.isDebugEnabled()", false, logger.isDebugEnabled() );
395 assertEquals( "logger.isInfoEnabled()", false, logger.isInfoEnabled() );
396 assertEquals( "logger.isWarnEnabled()", false, logger.isWarnEnabled() );
397 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() );
398 }
399
400 private void checkLogger( final MockConsoleLogger logger,
401 final boolean output,
402 final String message,
403 final Throwable throwable,
404 final String type )
405 {
406 assertEquals( "logger.m_message == message", message, logger.m_message );
407 assertEquals( "logger.m_output == true", output, logger.m_output );
408 assertEquals( "logger.m_throwable == null", throwable, logger.m_throwable );
409 assertEquals( "logger.m_type == null", type, logger.m_type );
410 }
411 }
This page was automatically generated by Maven