source: trunk/java-utils-test/src/test/java/de/ugoe/cs/util/console/TextConsoleTest.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 3.9 KB
Line 
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
15package de.ugoe.cs.util.console;
16
17import java.io.ByteArrayOutputStream;
18import java.io.PrintStream;
19import java.util.logging.Level;
20
21import org.junit.*;
22import 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 */
31public class TextConsoleTest {
32
33        private final static String ENDLINE = System.getProperty("line.separator");
34       
35        private PrintStream sysOut = null;
36        private PrintStream sysErr = null;
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";
88                Level traceLevel = Level.WARNING;
89                String expectedMessage = "[WARNING] test";
90
91                fixture.traceMsg(traceMessage, traceLevel);
92
93                assertEquals(expectedMessage, outContent.toString());
94        }
95
96        @Test
97        public void testTraceMsg_2() throws Exception {
98                TextConsole fixture = new TextConsole(Level.INFO);
99                String traceMessage = "test";
100                Level traceLevel = Level.INFO;
101                String expectedMessage = "[INFO] test";
102
103                fixture.traceMsg(traceMessage, traceLevel);
104
105                assertEquals(expectedMessage, outContent.toString());
106        }
107       
108               @Test
109                public void testTraceMsg_3() throws Exception {
110                        TextConsole fixture = new TextConsole(Level.WARNING);
111                        String traceMessage = "[INFO] test";
112                        Level traceLevel = Level.INFO;
113                       
114
115                        fixture.traceMsg(traceMessage, traceLevel);
116
117                        assertEquals("", outContent.toString());
118                }
119
120        @Before
121        public void setUp() throws Exception {
122            sysOut = new PrintStream(System.out);
123            sysErr = new PrintStream(System.err);
124                System.setOut(new PrintStream(outContent));
125                System.setErr(new PrintStream(errContent));
126        }
127
128        @After
129        public void tearDown() throws Exception {
130                System.setOut(sysOut);
131                System.setErr(sysErr);
132        }
133
134        public static void main(String[] args) {
135                new org.junit.runner.JUnitCore().run(TextConsoleTest.class);
136        }
137}
Note: See TracBrowser for help on using the repository browser.