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