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 java.io.ByteArrayOutputStream;
20  import java.io.PrintStream;
21  
22  import junit.framework.TestCase;
23  import org.codehaus.plexus.util.StringUtils;
24  
25  /**
26   * @author Jason van Zyl
27   *
28   */
29  public class ConsoleLoggerTest extends TestCase {
30      public void testConsoleLogger() {
31          ConsoleLogger logger = new ConsoleLogger(ConsoleLogger.LEVEL_DEBUG, "test");
32  
33          assertTrue(logger.isDebugEnabled());
34  
35          assertTrue(logger.isInfoEnabled());
36  
37          assertTrue(logger.isWarnEnabled());
38  
39          assertTrue(logger.isErrorEnabled());
40  
41          assertTrue(logger.isFatalErrorEnabled());
42  
43          // Save the original print stream.
44          PrintStream original = System.out;
45  
46          Throwable t = new Throwable("throwable");
47  
48          ByteArrayOutputStream os = new ByteArrayOutputStream();
49  
50          PrintStream consoleStream = new PrintStream(os);
51  
52          System.setOut(consoleStream);
53  
54          logger.debug("debug");
55  
56          assertEquals("[DEBUG] debug", getMessage(consoleStream, os));
57  
58          logger.debug("debug", t);
59  
60          assertEquals("[DEBUG] debug", getMessage(consoleStream, os));
61  
62          os = new ByteArrayOutputStream();
63  
64          consoleStream = new PrintStream(os);
65  
66          System.setOut(consoleStream);
67  
68          logger.info("info");
69  
70          assertEquals("[INFO] info", getMessage(consoleStream, os));
71  
72          logger.info("info", t);
73  
74          assertEquals("[INFO] info", getMessage(consoleStream, os));
75  
76          os = new ByteArrayOutputStream();
77  
78          consoleStream = new PrintStream(os);
79  
80          System.setOut(consoleStream);
81  
82          logger.warn("warn");
83  
84          assertEquals("[WARNING] warn", getMessage(consoleStream, os));
85  
86          logger.warn("warn", t);
87  
88          assertEquals("[WARNING] warn", getMessage(consoleStream, os));
89  
90          os = new ByteArrayOutputStream();
91  
92          consoleStream = new PrintStream(os);
93  
94          System.setOut(consoleStream);
95  
96          logger.error("error");
97  
98          assertEquals("[ERROR] error", getMessage(consoleStream, os));
99  
100         logger.error("error", t);
101 
102         assertEquals("[ERROR] error", getMessage(consoleStream, os));
103 
104         os = new ByteArrayOutputStream();
105 
106         consoleStream = new PrintStream(os);
107 
108         System.setOut(consoleStream);
109 
110         logger.fatalError("error");
111 
112         assertEquals("[FATAL ERROR] error", getMessage(consoleStream, os));
113 
114         logger.fatalError("error", t);
115 
116         assertEquals("[FATAL ERROR] error", getMessage(consoleStream, os));
117 
118         // Set the original print stream.
119         System.setOut(original);
120     }
121 
122     private String getMessage(PrintStream consoleStream, ByteArrayOutputStream os) {
123         consoleStream.flush();
124 
125         consoleStream.close();
126 
127         return StringUtils.chopNewline(os.toString());
128     }
129 }