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