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