[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[255] | 15 | package de.ugoe.cs.util.console;
|
---|
| 16 |
|
---|
| 17 | import java.io.ByteArrayOutputStream;
|
---|
| 18 | import java.io.PrintStream;
|
---|
| 19 |
|
---|
| 20 | import org.junit.*;
|
---|
| 21 | import static org.junit.Assert.*;
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * The class <code>TextConsoleTest</code> contains tests for the class
|
---|
| 25 | * <code>{@link TextConsole}</code>.
|
---|
| 26 | *
|
---|
| 27 | * @author Steffen Herbold
|
---|
| 28 | * @version 1.0
|
---|
| 29 | */
|
---|
| 30 | public class TextConsoleTest {
|
---|
| 31 |
|
---|
| 32 | private final static String ENDLINE = System.getProperty("line.separator");
|
---|
[670] | 33 |
|
---|
| 34 | private PrintStream sysOut = null;
|
---|
| 35 | private PrintStream sysErr = null;
|
---|
[255] | 36 |
|
---|
| 37 | private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
---|
| 38 | private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
|
---|
| 39 |
|
---|
| 40 | @Test
|
---|
| 41 | public void testTextConsole_1() throws Exception {
|
---|
| 42 |
|
---|
| 43 | Console.reset();
|
---|
| 44 | TextConsole result = new TextConsole();
|
---|
| 45 |
|
---|
| 46 | assertNotNull(result);
|
---|
| 47 | assertTrue(Console.getInstance().hasErrorListener(result));
|
---|
| 48 | assertTrue(Console.getInstance().hasExceptionListener(result));
|
---|
| 49 | assertTrue(Console.getInstance().hasOutputListener(result));
|
---|
| 50 | assertTrue(Console.getInstance().hasTraceListener(result));
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | @Test
|
---|
| 54 | public void testErrorMsg_1() throws Exception {
|
---|
| 55 | TextConsole fixture = new TextConsole();
|
---|
| 56 | String errMessage = "test";
|
---|
| 57 |
|
---|
| 58 | fixture.errorMsg(errMessage);
|
---|
| 59 |
|
---|
| 60 | assertEquals(errMessage, errContent.toString());
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Test
|
---|
| 64 | public void testLogException_1() throws Exception {
|
---|
| 65 | TextConsole fixture = new TextConsole();
|
---|
| 66 | Exception e = new Exception("test");
|
---|
| 67 | ;
|
---|
| 68 |
|
---|
| 69 | fixture.logException(e);
|
---|
| 70 | assertEquals(e.getMessage() + ENDLINE, errContent.toString());
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | @Test
|
---|
| 74 | public void testOutputMsg_1() throws Exception {
|
---|
| 75 | TextConsole fixture = new TextConsole();
|
---|
| 76 | String newMessage = "test";
|
---|
| 77 |
|
---|
| 78 | fixture.outputMsg(newMessage);
|
---|
| 79 |
|
---|
| 80 | assertEquals(newMessage, outContent.toString());
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[1318] | 83 | // @Test
|
---|
| 84 | // public void testTraceMsg_1() throws Exception {
|
---|
| 85 | // TextConsole fixture = new TextConsole();
|
---|
| 86 | // String traceMessage = "test";
|
---|
| 87 | // Level traceLevel = Level.WARNING;
|
---|
| 88 | // String expectedMessage = "[WARNING] test";
|
---|
| 89 | //
|
---|
| 90 | // fixture.traceMsg(traceMessage, traceLevel);
|
---|
| 91 | //
|
---|
| 92 | // assertEquals(expectedMessage, outContent.toString());
|
---|
| 93 | // }
|
---|
| 94 | //
|
---|
| 95 | // @Test
|
---|
| 96 | // public void testTraceMsg_2() throws Exception {
|
---|
| 97 | // TextConsole fixture = new TextConsole(Level.INFO);
|
---|
| 98 | // String traceMessage = "test";
|
---|
| 99 | // Level traceLevel = Level.INFO;
|
---|
| 100 | // String expectedMessage = "[INFO] test";
|
---|
| 101 | //
|
---|
| 102 | // fixture.traceMsg(traceMessage, traceLevel);
|
---|
| 103 | //
|
---|
| 104 | // assertEquals(expectedMessage, outContent.toString());
|
---|
| 105 | // }
|
---|
| 106 | //
|
---|
| 107 | // @Test
|
---|
| 108 | // public void testTraceMsg_3() throws Exception {
|
---|
| 109 | // TextConsole fixture = new TextConsole(Level.WARNING);
|
---|
| 110 | // String traceMessage = "[INFO] test";
|
---|
| 111 | // Level traceLevel = Level.INFO;
|
---|
| 112 | //
|
---|
| 113 | //
|
---|
| 114 | // fixture.traceMsg(traceMessage, traceLevel);
|
---|
| 115 | //
|
---|
| 116 | // assertEquals("", outContent.toString());
|
---|
| 117 | // }
|
---|
[255] | 118 |
|
---|
| 119 | @Before
|
---|
| 120 | public void setUp() throws Exception {
|
---|
[670] | 121 | sysOut = new PrintStream(System.out);
|
---|
| 122 | sysErr = new PrintStream(System.err);
|
---|
[255] | 123 | System.setOut(new PrintStream(outContent));
|
---|
| 124 | System.setErr(new PrintStream(errContent));
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | @After
|
---|
| 128 | public void tearDown() throws Exception {
|
---|
[670] | 129 | System.setOut(sysOut);
|
---|
| 130 | System.setErr(sysErr);
|
---|
[255] | 131 | }
|
---|
| 132 |
|
---|
| 133 | public static void main(String[] args) {
|
---|
| 134 | new org.junit.runner.JUnitCore().run(TextConsoleTest.class);
|
---|
| 135 | }
|
---|
| 136 | } |
---|