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