[255] | 1 | package de.ugoe.cs.util.console;
|
---|
| 2 |
|
---|
| 3 | import java.io.ByteArrayOutputStream;
|
---|
| 4 | import java.io.PrintStream;
|
---|
[636] | 5 | import java.util.logging.Level;
|
---|
[255] | 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");
|
---|
[670] | 20 |
|
---|
| 21 | private PrintStream sysOut = null;
|
---|
| 22 | private PrintStream sysErr = null;
|
---|
[255] | 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 | fixture.setDebug(true);
|
---|
| 74 | String traceMessage = "test";
|
---|
[636] | 75 | Level traceLevel = Level.WARNING;
|
---|
| 76 | String expectedMessage = "[WARNING] test";
|
---|
[255] | 77 |
|
---|
[636] | 78 | fixture.traceMsg(traceMessage, traceLevel);
|
---|
[255] | 79 |
|
---|
[636] | 80 | assertEquals(expectedMessage, outContent.toString());
|
---|
[255] | 81 | }
|
---|
| 82 |
|
---|
| 83 | @Test
|
---|
| 84 | public void testTraceMsg_2() throws Exception {
|
---|
| 85 | TextConsole fixture = new TextConsole();
|
---|
[636] | 86 | fixture.setDebug(true);
|
---|
[255] | 87 | String traceMessage = "test";
|
---|
[636] | 88 | Level traceLevel = Level.INFO;
|
---|
| 89 | String expectedMessage = "[INFO] test";
|
---|
[255] | 90 |
|
---|
[636] | 91 | fixture.traceMsg(traceMessage, traceLevel);
|
---|
[255] | 92 |
|
---|
[636] | 93 | assertEquals(expectedMessage, outContent.toString());
|
---|
[255] | 94 | }
|
---|
[636] | 95 |
|
---|
| 96 | @Test
|
---|
| 97 | public void testTraceMsg_3() throws Exception {
|
---|
| 98 | TextConsole fixture = new TextConsole();
|
---|
| 99 | fixture.setDebug(false);
|
---|
| 100 | String traceMessage = "[INFO] test";
|
---|
| 101 | Level traceLevel = Level.INFO;
|
---|
| 102 |
|
---|
[255] | 103 |
|
---|
[636] | 104 | fixture.traceMsg(traceMessage, traceLevel);
|
---|
| 105 |
|
---|
| 106 | assertEquals("", outContent.toString());
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[255] | 109 | @Before
|
---|
| 110 | public void setUp() throws Exception {
|
---|
[670] | 111 | sysOut = new PrintStream(System.out);
|
---|
| 112 | sysErr = new PrintStream(System.err);
|
---|
[255] | 113 | System.setOut(new PrintStream(outContent));
|
---|
| 114 | System.setErr(new PrintStream(errContent));
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | @After
|
---|
| 118 | public void tearDown() throws Exception {
|
---|
[670] | 119 | System.setOut(sysOut);
|
---|
| 120 | System.setErr(sysErr);
|
---|
[255] | 121 | }
|
---|
| 122 |
|
---|
| 123 | public static void main(String[] args) {
|
---|
| 124 | new org.junit.runner.JUnitCore().run(TextConsoleTest.class);
|
---|
| 125 | }
|
---|
| 126 | } |
---|