[255] | 1 | package de.ugoe.cs.util.console;
|
---|
| 2 |
|
---|
[638] | 3 | import java.util.logging.Level;
|
---|
| 4 |
|
---|
[255] | 5 | import org.junit.*;
|
---|
[319] | 6 | import de.ugoe.cs.util.console.mock.MockCommandListener;
|
---|
| 7 | import de.ugoe.cs.util.console.mock.MockErrorListener;
|
---|
| 8 | import de.ugoe.cs.util.console.mock.MockExceptionListener;
|
---|
| 9 | import de.ugoe.cs.util.console.mock.MockObserver;
|
---|
| 10 | import de.ugoe.cs.util.console.mock.MockOutputListener;
|
---|
| 11 | import de.ugoe.cs.util.console.mock.MockTraceListener;
|
---|
[255] | 12 | import static org.junit.Assert.*;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * The class <code>ConsoleTest</code> contains tests for the class <code>{@link Console}</code>.
|
---|
| 16 | *
|
---|
| 17 | * @author Steffen Herbold
|
---|
| 18 | * @version 1.0
|
---|
| 19 | */
|
---|
| 20 | public class ConsoleTest {
|
---|
| 21 |
|
---|
| 22 | private static final String ENDLINE = System.getProperty("line.separator");
|
---|
| 23 |
|
---|
| 24 | @Test
|
---|
| 25 | public void testCommandNotification_1()
|
---|
| 26 | throws Exception {
|
---|
| 27 | String command = "test";
|
---|
| 28 |
|
---|
| 29 | MockCommandListener commandListener1 = new MockCommandListener();
|
---|
| 30 | MockCommandListener commandListener2 = new MockCommandListener();
|
---|
| 31 |
|
---|
| 32 | Console.getInstance().registerCommandListener(commandListener1);
|
---|
| 33 | Console.getInstance().registerCommandListener(commandListener2);
|
---|
| 34 | Console.commandNotification(command);
|
---|
| 35 |
|
---|
| 36 | assertEquals(command, commandListener1.getLastCommand());
|
---|
| 37 | assertEquals(command, commandListener2.getLastCommand());
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | @SuppressWarnings("deprecation")
|
---|
| 41 | @Test
|
---|
| 42 | public void testDeleteObserver_1()
|
---|
| 43 | throws Exception {
|
---|
| 44 | Console fixture = Console.getInstance();
|
---|
| 45 |
|
---|
| 46 | MockObserver mockObserver1 = new MockObserver();
|
---|
| 47 | MockObserver mockObserver2 = new MockObserver();
|
---|
| 48 | fixture.registerObserver(mockObserver1);
|
---|
| 49 | fixture.registerObserver(mockObserver2);
|
---|
| 50 |
|
---|
| 51 | fixture.deleteObserver(mockObserver1);
|
---|
| 52 |
|
---|
| 53 | assertFalse(fixture.hasCommandListener(mockObserver1));
|
---|
| 54 | assertFalse(fixture.hasErrorListener(mockObserver1));
|
---|
| 55 | assertFalse(fixture.hasExceptionListener(mockObserver1));
|
---|
| 56 | assertFalse(fixture.hasOutputListener(mockObserver1));
|
---|
| 57 | assertFalse(fixture.hasTraceListener(mockObserver1));
|
---|
| 58 |
|
---|
| 59 | assertTrue(fixture.hasCommandListener(mockObserver2));
|
---|
| 60 | assertTrue(fixture.hasErrorListener(mockObserver2));
|
---|
| 61 | assertTrue(fixture.hasExceptionListener(mockObserver2));
|
---|
| 62 | assertTrue(fixture.hasOutputListener(mockObserver2));
|
---|
| 63 | assertTrue(fixture.hasTraceListener(mockObserver2));
|
---|
| 64 |
|
---|
| 65 | // add additional test code here
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | @Test
|
---|
| 69 | public void testGetInstance()
|
---|
| 70 | throws Exception {
|
---|
| 71 |
|
---|
| 72 | Console result = Console.getInstance();
|
---|
| 73 | assertNotNull(result);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | @Test
|
---|
| 77 | public void testLogException()
|
---|
| 78 | throws Exception {
|
---|
| 79 | Exception e = new Exception();
|
---|
| 80 | MockExceptionListener mockExceptionListener1 = new MockExceptionListener();
|
---|
| 81 | MockExceptionListener mockExceptionListener2 = new MockExceptionListener();
|
---|
| 82 |
|
---|
| 83 | Console.getInstance().registerExceptionListener(mockExceptionListener1);
|
---|
| 84 | Console.getInstance().registerExceptionListener(mockExceptionListener2);
|
---|
| 85 |
|
---|
| 86 | Console.logException(e);
|
---|
| 87 |
|
---|
| 88 | assertEquals(e, mockExceptionListener1.getLastException());
|
---|
| 89 | assertEquals(e, mockExceptionListener2.getLastException());
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | @Test
|
---|
| 93 | public void testPrint_1()
|
---|
| 94 | throws Exception {
|
---|
| 95 | String msg = "test";
|
---|
| 96 |
|
---|
| 97 | MockOutputListener mockOutputListener1 = new MockOutputListener();
|
---|
| 98 | MockOutputListener mockOutputListener2 = new MockOutputListener();
|
---|
| 99 |
|
---|
| 100 | Console.getInstance().registerOutputListener(mockOutputListener1);
|
---|
| 101 | Console.getInstance().registerOutputListener(mockOutputListener2);
|
---|
| 102 |
|
---|
| 103 | Console.print(msg);
|
---|
| 104 |
|
---|
| 105 | assertEquals(msg, mockOutputListener1.getLastOutput());
|
---|
| 106 | assertEquals(msg, mockOutputListener2.getLastOutput());
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | @Test
|
---|
| 110 | public void testPrinterr_1()
|
---|
| 111 | throws Exception {
|
---|
| 112 | String errMsg = "test";
|
---|
| 113 |
|
---|
| 114 | MockErrorListener mockErrorListener1 = new MockErrorListener();
|
---|
| 115 | MockErrorListener mockErrorListener2 = new MockErrorListener();
|
---|
| 116 |
|
---|
| 117 | Console.getInstance().registerErrorListener(mockErrorListener1);
|
---|
| 118 | Console.getInstance().registerErrorListener(mockErrorListener2);
|
---|
| 119 |
|
---|
| 120 | Console.printerr(errMsg);
|
---|
| 121 |
|
---|
| 122 | assertEquals(errMsg, mockErrorListener1.getLastError());
|
---|
| 123 | assertEquals(errMsg, mockErrorListener2.getLastError());
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | @Test
|
---|
| 127 | public void testPrinterrln_1()
|
---|
| 128 | throws Exception {
|
---|
| 129 | String errMsg = "test";
|
---|
| 130 |
|
---|
| 131 | MockErrorListener mockErrorListener1 = new MockErrorListener();
|
---|
| 132 | MockErrorListener mockErrorListener2 = new MockErrorListener();
|
---|
| 133 |
|
---|
| 134 | Console.getInstance().registerErrorListener(mockErrorListener1);
|
---|
| 135 | Console.getInstance().registerErrorListener(mockErrorListener2);
|
---|
| 136 |
|
---|
| 137 | Console.printerrln(errMsg);
|
---|
| 138 |
|
---|
| 139 | assertEquals(errMsg+ENDLINE, mockErrorListener1.getLastError());
|
---|
| 140 | assertEquals(errMsg+ENDLINE, mockErrorListener2.getLastError());
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | @Test
|
---|
| 145 | public void testPrintln_1()
|
---|
| 146 | throws Exception {
|
---|
| 147 | String msg = "test";
|
---|
| 148 |
|
---|
| 149 | MockOutputListener mockOutputListener1 = new MockOutputListener();
|
---|
| 150 | MockOutputListener mockOutputListener2 = new MockOutputListener();
|
---|
| 151 |
|
---|
| 152 | Console.getInstance().registerOutputListener(mockOutputListener1);
|
---|
| 153 | Console.getInstance().registerOutputListener(mockOutputListener2);
|
---|
| 154 |
|
---|
| 155 | Console.println(msg);
|
---|
| 156 |
|
---|
| 157 | assertEquals(msg+ENDLINE, mockOutputListener1.getLastOutput());
|
---|
| 158 | assertEquals(msg+ENDLINE, mockOutputListener2.getLastOutput());
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | @Test
|
---|
| 163 | public void testRegisterCommandListener_1()
|
---|
| 164 | throws Exception {
|
---|
| 165 | Console fixture = Console.getInstance();
|
---|
| 166 | MockCommandListener mockCommandListener = new MockCommandListener();
|
---|
| 167 |
|
---|
| 168 | fixture.registerCommandListener(mockCommandListener);
|
---|
| 169 |
|
---|
| 170 | assertTrue(fixture.hasCommandListener(mockCommandListener));
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | @Test
|
---|
| 174 | public void testRegisterErrorListener_1()
|
---|
| 175 | throws Exception {
|
---|
| 176 | Console fixture = Console.getInstance();
|
---|
| 177 |
|
---|
| 178 | MockErrorListener mockErrorListener = new MockErrorListener();
|
---|
| 179 |
|
---|
| 180 | fixture.registerErrorListener(mockErrorListener);
|
---|
| 181 |
|
---|
| 182 | assertTrue(fixture.hasErrorListener(mockErrorListener));
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | @Test
|
---|
| 186 | public void testRegisterExceptionListener_1()
|
---|
| 187 | throws Exception {
|
---|
| 188 | Console fixture = Console.getInstance();
|
---|
| 189 |
|
---|
| 190 | MockExceptionListener mockExceptionListener = new MockExceptionListener();
|
---|
| 191 |
|
---|
| 192 | fixture.registerExceptionListener(mockExceptionListener);
|
---|
| 193 |
|
---|
| 194 | assertTrue(fixture.hasExceptionListener(mockExceptionListener));
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | @SuppressWarnings("deprecation")
|
---|
| 198 | @Test
|
---|
| 199 | public void testRegisterObserver_1()
|
---|
| 200 | throws Exception {
|
---|
| 201 | Console fixture = Console.getInstance();
|
---|
| 202 |
|
---|
| 203 | MockObserver mockObserver = new MockObserver();
|
---|
| 204 |
|
---|
| 205 | fixture.registerObserver(mockObserver);
|
---|
| 206 |
|
---|
| 207 | assertTrue(fixture.hasCommandListener(mockObserver));
|
---|
| 208 | assertTrue(fixture.hasErrorListener(mockObserver));
|
---|
| 209 | assertTrue(fixture.hasExceptionListener(mockObserver));
|
---|
| 210 | assertTrue(fixture.hasOutputListener(mockObserver));
|
---|
| 211 | assertTrue(fixture.hasTraceListener(mockObserver));
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | @Test
|
---|
| 215 | public void testRegisterOutputListener_1()
|
---|
| 216 | throws Exception {
|
---|
| 217 | Console fixture = Console.getInstance();
|
---|
| 218 |
|
---|
| 219 | MockOutputListener mockOutputListener = new MockOutputListener();
|
---|
| 220 |
|
---|
| 221 | fixture.registerOutputListener(mockOutputListener);
|
---|
| 222 |
|
---|
| 223 | assertTrue(fixture.hasOutputListener(mockOutputListener));
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | @Test
|
---|
| 227 | public void testRegisterTraceListener_1()
|
---|
| 228 | throws Exception {
|
---|
| 229 | Console fixture = Console.getInstance();
|
---|
| 230 |
|
---|
| 231 | MockTraceListener mockTraceListener = new MockTraceListener();
|
---|
| 232 |
|
---|
| 233 | fixture.registerTraceListener(mockTraceListener);
|
---|
| 234 |
|
---|
| 235 | assertTrue(fixture.hasTraceListener(mockTraceListener));
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | @Test
|
---|
| 239 | public void testRemoveCommandListener_1()
|
---|
| 240 | throws Exception {
|
---|
| 241 | Console fixture = Console.getInstance();
|
---|
| 242 |
|
---|
| 243 | MockCommandListener mockCommandListener1 = new MockCommandListener();
|
---|
| 244 | MockCommandListener mockCommandListener2 = new MockCommandListener();
|
---|
| 245 | fixture.registerCommandListener(mockCommandListener1);
|
---|
| 246 | fixture.registerCommandListener(mockCommandListener2);
|
---|
| 247 |
|
---|
| 248 | fixture.removeCommandListener(mockCommandListener1);
|
---|
| 249 |
|
---|
| 250 | assertFalse(fixture.hasCommandListener(mockCommandListener1));
|
---|
| 251 | assertTrue(fixture.hasCommandListener(mockCommandListener2));
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | @Test
|
---|
| 255 | public void testRemoveErrorListener_1()
|
---|
| 256 | throws Exception {
|
---|
| 257 | Console fixture = Console.getInstance();
|
---|
| 258 |
|
---|
| 259 | MockErrorListener mockErrorListener1 = new MockErrorListener();
|
---|
| 260 | MockErrorListener mockErrorListener2 = new MockErrorListener();
|
---|
| 261 | fixture.registerErrorListener(mockErrorListener1);
|
---|
| 262 | fixture.registerErrorListener(mockErrorListener2);
|
---|
| 263 |
|
---|
| 264 | fixture.removeErrorListener(mockErrorListener1);
|
---|
| 265 |
|
---|
| 266 | assertFalse(fixture.hasErrorListener(mockErrorListener1));
|
---|
| 267 | assertTrue(fixture.hasErrorListener(mockErrorListener2));
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | @Test
|
---|
| 271 | public void testRemoveExceptionListener_1()
|
---|
| 272 | throws Exception {
|
---|
| 273 | Console fixture = Console.getInstance();
|
---|
| 274 |
|
---|
| 275 | MockExceptionListener mockExceptionListener1 = new MockExceptionListener();
|
---|
| 276 | MockExceptionListener mockExceptionListener2 = new MockExceptionListener();
|
---|
| 277 |
|
---|
| 278 | fixture.registerExceptionListener(mockExceptionListener1);
|
---|
| 279 | fixture.registerExceptionListener(mockExceptionListener2);
|
---|
| 280 |
|
---|
| 281 | fixture.removeExceptionListener(mockExceptionListener1);
|
---|
| 282 |
|
---|
| 283 | assertFalse(fixture.hasExceptionListener(mockExceptionListener1));
|
---|
| 284 | assertTrue(fixture.hasExceptionListener(mockExceptionListener2));
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | @Test
|
---|
| 288 | public void testRemoveOutputListener_1()
|
---|
| 289 | throws Exception {
|
---|
| 290 | Console fixture = Console.getInstance();
|
---|
| 291 |
|
---|
| 292 | MockOutputListener mockOutputListener1 = new MockOutputListener();
|
---|
| 293 | MockOutputListener mockOutputListener2 = new MockOutputListener();
|
---|
| 294 |
|
---|
| 295 | fixture.registerOutputListener(mockOutputListener1);
|
---|
| 296 | fixture.registerOutputListener(mockOutputListener2);
|
---|
| 297 |
|
---|
| 298 | fixture.removeOutputListener(mockOutputListener1);
|
---|
| 299 |
|
---|
| 300 | assertFalse(fixture.hasOutputListener(mockOutputListener1));
|
---|
| 301 | assertTrue(fixture.hasOutputListener(mockOutputListener2));
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | @Test
|
---|
| 305 | public void testRemoveTraceListener_1()
|
---|
| 306 | throws Exception {
|
---|
| 307 | Console fixture = Console.getInstance();
|
---|
| 308 |
|
---|
| 309 | MockTraceListener mockTraceListener1 = new MockTraceListener();
|
---|
| 310 | MockTraceListener mockTraceListener2 = new MockTraceListener();
|
---|
| 311 |
|
---|
| 312 | fixture.registerTraceListener(mockTraceListener1);
|
---|
| 313 | fixture.registerTraceListener(mockTraceListener2);
|
---|
| 314 |
|
---|
| 315 | fixture.removeTraceListener(mockTraceListener1);
|
---|
| 316 |
|
---|
| 317 | assertFalse(fixture.hasTraceListener(mockTraceListener1));
|
---|
| 318 | assertTrue(fixture.hasTraceListener(mockTraceListener2));
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[638] | 321 | @SuppressWarnings("deprecation")
|
---|
| 322 | @Test
|
---|
[255] | 323 | public void testTrace_1()
|
---|
| 324 | throws Exception {
|
---|
| 325 | String traceMsg = "test";
|
---|
| 326 |
|
---|
| 327 | MockTraceListener mockTraceListener1 = new MockTraceListener();
|
---|
| 328 | MockTraceListener mockTraceListener2 = new MockTraceListener();
|
---|
| 329 |
|
---|
| 330 | Console.getInstance().registerTraceListener(mockTraceListener1);
|
---|
| 331 | Console.getInstance().registerTraceListener(mockTraceListener2);
|
---|
| 332 |
|
---|
| 333 | Console.trace(traceMsg);
|
---|
| 334 |
|
---|
| 335 | assertEquals(traceMsg, mockTraceListener1.getLastTrace());
|
---|
[638] | 336 | assertEquals(Level.INFO, mockTraceListener1.getLastLevel());
|
---|
[255] | 337 | assertEquals(traceMsg, mockTraceListener2.getLastTrace());
|
---|
[638] | 338 | assertEquals(Level.INFO, mockTraceListener2.getLastLevel());
|
---|
[255] | 339 | }
|
---|
| 340 |
|
---|
[638] | 341 | @Test
|
---|
| 342 | public void testTrace_2()
|
---|
| 343 | throws Exception {
|
---|
| 344 | String traceMsg = "test";
|
---|
| 345 | Level traceLevel = Level.SEVERE;
|
---|
| 346 |
|
---|
| 347 | MockTraceListener mockTraceListener1 = new MockTraceListener();
|
---|
| 348 | MockTraceListener mockTraceListener2 = new MockTraceListener();
|
---|
| 349 |
|
---|
| 350 | Console.getInstance().registerTraceListener(mockTraceListener1);
|
---|
| 351 | Console.getInstance().registerTraceListener(mockTraceListener2);
|
---|
| 352 |
|
---|
| 353 | Console.trace(traceLevel, traceMsg);
|
---|
| 354 |
|
---|
| 355 | assertEquals(traceMsg, mockTraceListener1.getLastTrace());
|
---|
| 356 | assertEquals(traceLevel, mockTraceListener1.getLastLevel());
|
---|
| 357 | assertEquals(traceMsg, mockTraceListener2.getLastTrace());
|
---|
| 358 | assertEquals(traceLevel, mockTraceListener2.getLastLevel());
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | @SuppressWarnings("deprecation")
|
---|
| 362 | @Test
|
---|
[255] | 363 | public void testTraceln_1()
|
---|
| 364 | throws Exception {
|
---|
| 365 | String traceMsg = "test";
|
---|
| 366 |
|
---|
| 367 | MockTraceListener mockTraceListener1 = new MockTraceListener();
|
---|
| 368 | MockTraceListener mockTraceListener2 = new MockTraceListener();
|
---|
| 369 |
|
---|
| 370 | Console.getInstance().registerTraceListener(mockTraceListener1);
|
---|
| 371 | Console.getInstance().registerTraceListener(mockTraceListener2);
|
---|
| 372 |
|
---|
| 373 | Console.traceln(traceMsg);
|
---|
| 374 |
|
---|
| 375 | assertEquals(traceMsg+ENDLINE, mockTraceListener1.getLastTrace());
|
---|
[638] | 376 | assertEquals(Level.INFO, mockTraceListener1.getLastLevel());
|
---|
[255] | 377 | assertEquals(traceMsg+ENDLINE, mockTraceListener2.getLastTrace());
|
---|
[638] | 378 | assertEquals(Level.INFO, mockTraceListener1.getLastLevel());
|
---|
[255] | 379 | }
|
---|
[638] | 380 |
|
---|
| 381 | @Test
|
---|
| 382 | public void testTraceln_2()
|
---|
| 383 | throws Exception {
|
---|
| 384 | String traceMsg = "test";
|
---|
| 385 | Level traceLevel = Level.SEVERE;
|
---|
[255] | 386 |
|
---|
[638] | 387 | MockTraceListener mockTraceListener1 = new MockTraceListener();
|
---|
| 388 | MockTraceListener mockTraceListener2 = new MockTraceListener();
|
---|
| 389 |
|
---|
| 390 | Console.getInstance().registerTraceListener(mockTraceListener1);
|
---|
| 391 | Console.getInstance().registerTraceListener(mockTraceListener2);
|
---|
| 392 |
|
---|
| 393 | Console.traceln(traceLevel, traceMsg);
|
---|
| 394 |
|
---|
| 395 | assertEquals(traceMsg+ENDLINE, mockTraceListener1.getLastTrace());
|
---|
| 396 | assertEquals(traceLevel, mockTraceListener1.getLastLevel());
|
---|
| 397 | assertEquals(traceMsg+ENDLINE, mockTraceListener2.getLastTrace());
|
---|
| 398 | assertEquals(traceLevel, mockTraceListener2.getLastLevel());
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[255] | 401 | @Before
|
---|
| 402 | public void setUp()
|
---|
| 403 | throws Exception {
|
---|
| 404 | Console.reset();
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | public static void main(String[] args) {
|
---|
| 408 | new org.junit.runner.JUnitCore().run(ConsoleTest.class);
|
---|
| 409 | }
|
---|
| 410 | } |
---|