1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.logging;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.PrintStream;
20  
21  import org.xulux.utils.io.IOUtils;
22  
23  import junit.framework.TestCase;
24  
25  public class SystemOutLoggerTest extends TestCase {
26  
27      public void testLogMessage() {
28          System.out.println("testLogMessage");
29          SystemOutLogger logger = new SystemOutLogger();
30          logger.init();
31          ByteArrayOutputStream out = new ByteArrayOutputStream();
32          PrintStream catchStream = new PrintStream(out);
33          PrintStream origStream = System.out;
34          System.setOut(catchStream);
35          logger.log(0, "name", "message");
36          assertEquals("[name] message"+IOUtils.getLineSeperator(), out.toString());
37          out = new ByteArrayOutputStream();
38          catchStream = new PrintStream(out);
39          System.setOut(catchStream);
40          logger.log(0, null, null);
41          assertEquals("[null] null"+IOUtils.getLineSeperator(), out.toString());
42          System.setOut(origStream);
43          logger.destroy();
44      }
45      
46      public void testLogException() {
47          System.out.println("testLogException");
48          SystemOutLogger logger = new SystemOutLogger();
49          logger.init();
50          ByteArrayOutputStream out = new ByteArrayOutputStream();
51          PrintStream catchStream = new PrintStream(out);
52          PrintStream origStream = System.out;
53          System.setOut(catchStream);
54          logger.log(0, "name", "message", new Exception());
55          String message = out.toString();
56          assertEquals(true, message.startsWith("[name] message"+System.getProperty("line.separator")+"[name] java.lang.Exception"));
57          out = new ByteArrayOutputStream();
58          catchStream = new PrintStream(out);
59          System.setOut(catchStream);
60          logger.log(0, null, null, null);
61          assertEquals("[null] null"+IOUtils.getLineSeperator()+"[null] ", out.toString());
62          System.setOut(origStream);
63          logger.destroy();
64      }
65  }