1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }