1 | package de.ugoe.cs.util.console;
|
---|
2 |
|
---|
3 | import java.io.ByteArrayOutputStream;
|
---|
4 | import java.io.PrintStream;
|
---|
5 | import java.util.logging.Level;
|
---|
6 |
|
---|
7 | import org.junit.*;
|
---|
8 | import static org.junit.Assert.*;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * The class <code>TextConsoleTest</code> contains tests for the class
|
---|
12 | * <code>{@link TextConsole}</code>.
|
---|
13 | *
|
---|
14 | * @author Steffen Herbold
|
---|
15 | * @version 1.0
|
---|
16 | */
|
---|
17 | public class TextConsoleTest {
|
---|
18 |
|
---|
19 | private final static String ENDLINE = System.getProperty("line.separator");
|
---|
20 |
|
---|
21 | private PrintStream sysOut = null;
|
---|
22 | private PrintStream sysErr = null;
|
---|
23 |
|
---|
24 | private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
---|
25 | private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
|
---|
26 |
|
---|
27 | @Test
|
---|
28 | public void testTextConsole_1() throws Exception {
|
---|
29 |
|
---|
30 | Console.reset();
|
---|
31 | TextConsole result = new TextConsole();
|
---|
32 |
|
---|
33 | assertNotNull(result);
|
---|
34 | assertTrue(Console.getInstance().hasErrorListener(result));
|
---|
35 | assertTrue(Console.getInstance().hasExceptionListener(result));
|
---|
36 | assertTrue(Console.getInstance().hasOutputListener(result));
|
---|
37 | assertTrue(Console.getInstance().hasTraceListener(result));
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Test
|
---|
41 | public void testErrorMsg_1() throws Exception {
|
---|
42 | TextConsole fixture = new TextConsole();
|
---|
43 | String errMessage = "test";
|
---|
44 |
|
---|
45 | fixture.errorMsg(errMessage);
|
---|
46 |
|
---|
47 | assertEquals(errMessage, errContent.toString());
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Test
|
---|
51 | public void testLogException_1() throws Exception {
|
---|
52 | TextConsole fixture = new TextConsole();
|
---|
53 | Exception e = new Exception("test");
|
---|
54 | ;
|
---|
55 |
|
---|
56 | fixture.logException(e);
|
---|
57 | assertEquals(e.getMessage() + ENDLINE, errContent.toString());
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Test
|
---|
61 | public void testOutputMsg_1() throws Exception {
|
---|
62 | TextConsole fixture = new TextConsole();
|
---|
63 | String newMessage = "test";
|
---|
64 |
|
---|
65 | fixture.outputMsg(newMessage);
|
---|
66 |
|
---|
67 | assertEquals(newMessage, outContent.toString());
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Test
|
---|
71 | public void testTraceMsg_1() throws Exception {
|
---|
72 | TextConsole fixture = new TextConsole();
|
---|
73 | String traceMessage = "test";
|
---|
74 | Level traceLevel = Level.WARNING;
|
---|
75 | String expectedMessage = "[WARNING] test";
|
---|
76 |
|
---|
77 | fixture.traceMsg(traceMessage, traceLevel);
|
---|
78 |
|
---|
79 | assertEquals(expectedMessage, outContent.toString());
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Test
|
---|
83 | public void testTraceMsg_2() throws Exception {
|
---|
84 | TextConsole fixture = new TextConsole(Level.INFO);
|
---|
85 | String traceMessage = "test";
|
---|
86 | Level traceLevel = Level.INFO;
|
---|
87 | String expectedMessage = "[INFO] test";
|
---|
88 |
|
---|
89 | fixture.traceMsg(traceMessage, traceLevel);
|
---|
90 |
|
---|
91 | assertEquals(expectedMessage, outContent.toString());
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Test
|
---|
95 | public void testTraceMsg_3() throws Exception {
|
---|
96 | TextConsole fixture = new TextConsole(Level.WARNING);
|
---|
97 | String traceMessage = "[INFO] test";
|
---|
98 | Level traceLevel = Level.INFO;
|
---|
99 |
|
---|
100 |
|
---|
101 | fixture.traceMsg(traceMessage, traceLevel);
|
---|
102 |
|
---|
103 | assertEquals("", outContent.toString());
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Before
|
---|
107 | public void setUp() throws Exception {
|
---|
108 | sysOut = new PrintStream(System.out);
|
---|
109 | sysErr = new PrintStream(System.err);
|
---|
110 | System.setOut(new PrintStream(outContent));
|
---|
111 | System.setErr(new PrintStream(errContent));
|
---|
112 | }
|
---|
113 |
|
---|
114 | @After
|
---|
115 | public void tearDown() throws Exception {
|
---|
116 | System.setOut(sysOut);
|
---|
117 | System.setErr(sysErr);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public static void main(String[] args) {
|
---|
121 | new org.junit.runner.JUnitCore().run(TextConsoleTest.class);
|
---|
122 | }
|
---|
123 | } |
---|