View Javadoc
1   package org.codehaus.plexus.logging.console;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.TestCase;
20  import org.codehaus.plexus.util.StringUtils;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.PrintStream;
24  
25  /**
26   * @author Jason van Zyl
27   *
28   * @version $Id$
29   */
30  public class ConsoleLoggerTest
31      extends TestCase
32  {
33      public void testConsoleLogger()
34      {
35          ConsoleLogger logger = new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG, "test" );
36  
37          assertTrue( logger.isDebugEnabled() );
38  
39          assertTrue( logger.isInfoEnabled() );
40  
41          assertTrue( logger.isWarnEnabled() );
42  
43          assertTrue( logger.isErrorEnabled() );
44  
45          assertTrue( logger.isFatalErrorEnabled() );
46  
47          // Save the original print stream.
48          PrintStream original = System.out;
49  
50          Throwable t = new Throwable( "throwable" );
51  
52          ByteArrayOutputStream os = new ByteArrayOutputStream();
53  
54          PrintStream consoleStream = new PrintStream( os );
55  
56          System.setOut( consoleStream );
57  
58  
59          logger.debug( "debug" );
60  
61          assertEquals( "[DEBUG] debug", getMessage( consoleStream, os ) );
62  
63          logger.debug( "debug", t );
64  
65          assertEquals( "[DEBUG] debug", getMessage( consoleStream, os ) );
66  
67  
68          os = new ByteArrayOutputStream();
69  
70          consoleStream = new PrintStream( os );
71  
72          System.setOut( consoleStream );
73  
74          logger.info( "info" );
75  
76          assertEquals( "[INFO] info", getMessage( consoleStream, os ) );
77  
78          logger.info( "info", t );
79  
80          assertEquals( "[INFO] info", getMessage( consoleStream, os ) );
81  
82  
83          os = new ByteArrayOutputStream();
84  
85          consoleStream = new PrintStream( os );
86  
87          System.setOut( consoleStream );
88  
89          logger.warn( "warn" );
90  
91          assertEquals( "[WARNING] warn", getMessage( consoleStream, os ) );
92  
93          logger.warn( "warn", t );
94  
95          assertEquals( "[WARNING] warn", getMessage( consoleStream, os ) );
96  
97  
98          os = new ByteArrayOutputStream();
99  
100         consoleStream = new PrintStream( os );
101 
102         System.setOut( consoleStream );
103 
104         logger.error( "error" );
105 
106         assertEquals( "[ERROR] error", getMessage( consoleStream, os ) );
107 
108         logger.error( "error", t );
109 
110         assertEquals( "[ERROR] error", getMessage( consoleStream, os ) );
111 
112 
113         os = new ByteArrayOutputStream();
114 
115         consoleStream = new PrintStream( os );
116 
117         System.setOut( consoleStream );
118 
119         logger.fatalError( "error" );
120 
121         assertEquals( "[FATAL ERROR] error", getMessage( consoleStream, os ) );
122 
123         logger.fatalError( "error", t );
124 
125         assertEquals( "[FATAL ERROR] error", getMessage( consoleStream, os ) );
126 
127         // Set the original print stream.
128         System.setOut( original );
129     }
130 
131     private String getMessage( PrintStream consoleStream, ByteArrayOutputStream os )
132     {
133         consoleStream.flush();
134 
135         consoleStream.close();
136 
137         return StringUtils.chopNewline( os.toString() );
138     }
139 }