[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");
|
---|
| 20 |
|
---|
| 21 | private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
---|
| 22 | private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
|
---|
| 23 |
|
---|
| 24 | @Test
|
---|
| 25 | public void testTextConsole_1() throws Exception {
|
---|
| 26 |
|
---|
| 27 | Console.reset();
|
---|
| 28 | TextConsole result = new TextConsole();
|
---|
| 29 |
|
---|
| 30 | assertNotNull(result);
|
---|
| 31 | assertTrue(Console.getInstance().hasErrorListener(result));
|
---|
| 32 | assertTrue(Console.getInstance().hasExceptionListener(result));
|
---|
| 33 | assertTrue(Console.getInstance().hasOutputListener(result));
|
---|
| 34 | assertTrue(Console.getInstance().hasTraceListener(result));
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | @Test
|
---|
| 38 | public void testErrorMsg_1() throws Exception {
|
---|
| 39 | TextConsole fixture = new TextConsole();
|
---|
| 40 | String errMessage = "test";
|
---|
| 41 |
|
---|
| 42 | fixture.errorMsg(errMessage);
|
---|
| 43 |
|
---|
| 44 | assertEquals(errMessage, errContent.toString());
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | @Test
|
---|
| 48 | public void testLogException_1() throws Exception {
|
---|
| 49 | TextConsole fixture = new TextConsole();
|
---|
| 50 | Exception e = new Exception("test");
|
---|
| 51 | ;
|
---|
| 52 |
|
---|
| 53 | fixture.logException(e);
|
---|
| 54 | assertEquals(e.getMessage() + ENDLINE, errContent.toString());
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | @Test
|
---|
| 58 | public void testOutputMsg_1() throws Exception {
|
---|
| 59 | TextConsole fixture = new TextConsole();
|
---|
| 60 | String newMessage = "test";
|
---|
| 61 |
|
---|
| 62 | fixture.outputMsg(newMessage);
|
---|
| 63 |
|
---|
| 64 | assertEquals(newMessage, outContent.toString());
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | @Test
|
---|
| 68 | public void testTraceMsg_1() throws Exception {
|
---|
| 69 | TextConsole fixture = new TextConsole();
|
---|
| 70 | fixture.setDebug(true);
|
---|
| 71 | String traceMessage = "test";
|
---|
[636] | 72 | Level traceLevel = Level.WARNING;
|
---|
| 73 | String expectedMessage = "[WARNING] test";
|
---|
[255] | 74 |
|
---|
[636] | 75 | fixture.traceMsg(traceMessage, traceLevel);
|
---|
[255] | 76 |
|
---|
[636] | 77 | assertEquals(expectedMessage, outContent.toString());
|
---|
[255] | 78 | }
|
---|
| 79 |
|
---|
| 80 | @Test
|
---|
| 81 | public void testTraceMsg_2() throws Exception {
|
---|
| 82 | TextConsole fixture = new TextConsole();
|
---|
[636] | 83 | fixture.setDebug(true);
|
---|
[255] | 84 | String traceMessage = "test";
|
---|
[636] | 85 | Level traceLevel = Level.INFO;
|
---|
| 86 | String expectedMessage = "[INFO] test";
|
---|
[255] | 87 |
|
---|
[636] | 88 | fixture.traceMsg(traceMessage, traceLevel);
|
---|
[255] | 89 |
|
---|
[636] | 90 | assertEquals(expectedMessage, outContent.toString());
|
---|
[255] | 91 | }
|
---|
[636] | 92 |
|
---|
| 93 | @Test
|
---|
| 94 | public void testTraceMsg_3() throws Exception {
|
---|
| 95 | TextConsole fixture = new TextConsole();
|
---|
| 96 | fixture.setDebug(false);
|
---|
| 97 | String traceMessage = "[INFO] test";
|
---|
| 98 | Level traceLevel = Level.INFO;
|
---|
| 99 |
|
---|
[255] | 100 |
|
---|
[636] | 101 | fixture.traceMsg(traceMessage, traceLevel);
|
---|
| 102 |
|
---|
| 103 | assertEquals("", outContent.toString());
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[255] | 106 | @Before
|
---|
| 107 | public void setUp() throws Exception {
|
---|
| 108 | System.setOut(new PrintStream(outContent));
|
---|
| 109 | System.setErr(new PrintStream(errContent));
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | @After
|
---|
| 113 | public void tearDown() throws Exception {
|
---|
| 114 | System.setOut(null);
|
---|
| 115 | System.setErr(null);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | public static void main(String[] args) {
|
---|
| 119 | new org.junit.runner.JUnitCore().run(TextConsoleTest.class);
|
---|
| 120 | }
|
---|
| 121 | } |
---|