1 26 27 package net.sourceforge.groboutils.junit.v1; 28 29 import net.sourceforge.groboutils.autodoc.v1.AutoDoc; 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 import java.io.IOException ; 35 36 37 44 public class MultiThreadedTestRunnerUTest extends TestCase 45 { 46 49 private static final Class THIS_CLASS = MultiThreadedTestRunnerUTest.class; 50 private static final AutoDoc DOC = new AutoDoc( THIS_CLASS ); 51 52 public MultiThreadedTestRunnerUTest( String name ) 53 { 54 super( name ); 55 } 56 57 58 59 60 63 64 public void testConstructor1() 65 { 66 DOC.getLog().debug("testConstructor1"); 67 try 68 { 69 new MultiThreadedTestRunner( null ); 70 fail("did not throw an IllegalArgumentException."); 71 } 72 catch (IllegalArgumentException e) 73 { 74 } 76 } 77 78 79 public void testConstructor2() 80 { 81 DOC.getLog().debug("testConstructor2"); 82 try 83 { 84 new MultiThreadedTestRunner( new TestRunnable[0] ); 85 fail("did not throw an IllegalArgumentException."); 86 } 87 catch (IllegalArgumentException e) 88 { 89 } 91 } 92 93 94 static class RunValue1 95 { 96 public int value; 97 } 98 99 public synchronized void testRun1() 100 throws Throwable 101 { 102 DOC.getIT().testsIssue( 526710 ); 103 DOC.getLog().debug("testRun1"); 104 105 final RunValue1 runCount = new RunValue1(); 106 TestRunnable tr1 = new TestRunnable() { 107 public void runTest() throws Throwable 108 { 109 DOC.getLog().debug("Running test testRun1"); 110 ++runCount.value; 111 } 112 }; 113 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 114 new TestRunnable[] { tr1 } ); 115 mttr.runTestRunnables(); 116 117 assertEquals( 118 "Did not run the runTest method enough times.", 119 1, 120 runCount.value ); 121 } 122 123 124 public synchronized void testRun2() 125 throws Throwable 126 { 127 DOC.getIT().testsIssue( 526710 ); 128 DOC.getLog().debug("testRun2"); 129 130 final RunValue1 runCount = new RunValue1(); 131 TestRunnable tr1 = new TestRunnable() { 132 public void runTest() throws Throwable 133 { 134 ++runCount.value; 135 } 136 }; 137 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 138 new TestRunnable[] { tr1 } ); 139 mttr.runTestRunnables( 1000 ); 140 141 assertEquals( 142 "Did not run the runTest method enough times.", 143 1, 144 runCount.value ); 145 } 146 147 148 public synchronized void testRun3a() 149 throws Throwable 150 { 151 DOC.getIT().testsIssue( 526710 ); 152 DOC.getLog().debug("testRun3a"); 153 DOC.getLog().warn( 154 "****************\n"+ 155 "This test may expose timing issues, where only one test is run.\n"+ 156 "I've only seen this with JDK 1.1, but that may expose an underlying\n"+ 157 "problem. This will require further investigation.\n"+ 158 "****************" ); 159 160 final RunValue1 runCount = new RunValue1(); 161 TestRunnable tr1 = new TestRunnable() { 162 public synchronized void runTest() throws Throwable 163 { 164 ++runCount.value; 165 } 166 }; 167 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 168 new TestRunnable[] { tr1, tr1 } ); 169 mttr.runTestRunnables(); 170 171 assertEquals( 172 "Did not run the runTest method enough times.", 173 2, 174 runCount.value ); 175 } 176 177 178 public synchronized void testRun3b() 179 throws Throwable 180 { 181 DOC.getLog().debug("testRun3b"); 182 final RunValue1 runCount = new RunValue1(); 183 TestRunnable tr1 = new TestRunnable() { 184 public void runTest() throws Throwable 185 { 186 ++runCount.value; 187 } 188 }; 189 int totalCount = 15; 190 TestRunnable trList[] = new TestRunnable[ totalCount ]; 191 for (int i = 0; i < totalCount; ++i) 192 { 193 trList[ i ] = tr1; 194 } 195 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( trList ); 196 mttr.runTestRunnables(); 197 198 assertEquals( 199 "Did not run the runTest method enough times.", 200 totalCount, 201 runCount.value ); 202 } 203 204 205 public void testRun4() 206 throws Throwable 207 { 208 DOC.getLog().debug("testRun4"); 209 TestRunnable tr1 = new TestRunnable() { 210 public void runTest() throws Throwable 211 { 212 throw new IOException ("My exception"); 213 } 214 }; 215 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 216 new TestRunnable[] { tr1 } ); 217 try 218 { 219 mttr.runTestRunnables(); 220 fail("did not throw IOException."); 221 } 222 catch (IOException ioe) 223 { 224 assertEquals( 226 "IOException not the one we threw.", 227 "My exception", 228 ioe.getMessage() ); 229 } 230 } 231 232 233 public void testRun5() 234 throws Throwable 235 { 236 DOC.getLog().debug("testRun5"); 237 TestRunnable tr1 = new TestRunnable() { 238 public void runTest() throws Throwable 239 { 240 Object o = new Object (); 241 synchronized( o ) 242 { 243 o.wait(); 244 } 245 } 246 }; 247 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 248 new TestRunnable[] { tr1 } ); 249 boolean fail = false; 250 try 251 { 252 mttr.runTestRunnables( 10 ); 254 fail = true; 255 } 256 catch (junit.framework.AssertionFailedError e) 257 { 258 } 260 if (fail) 261 { 262 fail("Did not throw an assertion failed error."); 263 } 264 } 265 266 267 public void testRun5a() 268 throws Throwable 269 { 270 DOC.getLog().debug("testRun5a"); 271 TestRunnable tr1 = new TestRunnable() { 272 public void runTest() throws Throwable 273 { 274 Object o = new Object (); 275 synchronized( o ) 276 { 277 o.wait(); 278 } 279 } 280 }; 281 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 282 new TestRunnable[] { tr1 } ); 283 boolean fail = false; 284 try 285 { 286 mttr.runTestRunnables( 1000 ); 288 fail = true; 289 } 290 catch (junit.framework.AssertionFailedError e) 291 { 292 } 294 if (fail) 295 { 296 fail("Did not throw an assertion failed error."); 297 } 298 } 299 300 301 private static final int MAX_LOOP_COUNT = 20000; 302 303 static abstract class TestRunnable6 extends TestRunnable 304 { 305 public Thread runningThread = null; 306 public boolean wasInterrupted = false; 307 public void runTest() throws Throwable 308 { 309 this.runningThread = Thread.currentThread(); 310 runMyTest(); 311 } 312 313 314 public abstract void runMyTest() throws Throwable ; 315 } 316 317 318 public void testRun6() 319 throws Throwable 320 { 321 DOC.getIT().testsIssue( 771000 ); 322 DOC.getLog().debug("testRun6"); 323 TestRunnable6 tr1 = new TestRunnable6() { 324 public void runMyTest() throws Throwable 325 { 326 DOC.getLog().warn("runMyTest 6 running forever"); 327 for (int i = 0; i < MAX_LOOP_COUNT; ++i) 329 { 330 try { 332 delay( 100 ); 333 } catch (InterruptedException ie) { 334 this.wasInterrupted = true; 335 DOC.getLog().warn("runMyTest 6: interrupted!"); 336 } 337 DOC.getLog().warn("runMyTest 6: Loop!"); 338 if (Thread.interrupted()) 339 { 340 this.wasInterrupted = true; 341 DOC.getLog().warn("runMyTest 6: inerrupted!"); 342 } 343 } 344 fail( "This line should NEVER be reached!" ); 345 } 346 }; 347 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 348 new TestRunnable[] { tr1 } ); 349 boolean fail = false; 350 try 351 { 352 mttr.runTestRunnables( 10 ); 353 fail = true; 354 } 355 catch (junit.framework.AssertionFailedError e) 356 { 357 if (e.getMessage() != null && 359 e.getMessage().indexOf( "This line should NEVER be reached!" ) 360 >= 0) 361 { 362 fail("Did not kill the runnable." ); 363 } 364 } 365 if (fail) 366 { 367 fail("Did not throw an assertion failed error."); 368 } 369 370 assertTrue( 371 "Test was not interrupted.", 372 tr1.wasInterrupted ); 373 assertFalse( 374 "Did not stop the test runnable.", 375 tr1.runningThread.isAlive() ); 376 } 377 378 379 public void testRun7() 380 throws Throwable 381 { 382 DOC.getIT().testsIssue( 771001 ); 383 DOC.getLog().debug("testRun7"); 384 TestRunnable6 tr1 = new TestRunnable6() { 385 public void runMyTest() throws Throwable 386 { 387 assertTrue( 388 "Did not create the thread as daemon.", 389 this.runningThread.isDaemon() ); 390 } 391 }; 392 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 393 new TestRunnable[] { tr1 } ); 394 mttr.runTestRunnables(); 395 } 396 397 398 399 static class TesterClass 400 { 401 public int value; 402 } 403 404 public void testRun8() 405 throws Throwable 406 { 407 DOC.getIT().testsIssue( 771008 ); 408 409 411 DOC.getLog().debug("testRun8"); 412 final TesterClass tc = new TesterClass(); 413 TestRunnable tr1 = new TestRunnable() { 414 public void runTest() throws Throwable 415 { 416 for (int i = 0; i < 30; ++i) 417 { 418 tc.value = i; 419 delay( 100 ); 420 } 421 tc.value = -1; 422 } 423 }; 424 TestMonitorRunnable tm1 = new TestMonitorRunnable() { 425 public void runMonitor() throws Throwable 426 { 427 assertTrue( 428 "Value is < 0.", 429 tc.value >= 0 ); 430 } 431 }; 432 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 433 new TestRunnable[] { tr1 }, new TestRunnable[] { tm1 } ); 434 boolean fail = false; 435 try 436 { 437 mttr.runTestRunnables(); 438 fail = true; 439 } 440 catch (junit.framework.AssertionFailedError e) 441 { 442 assertTrue( 443 "Did not throw correct exception from monitor.", 444 e.getMessage().indexOf( "Value is < 0." ) >= 0 ); 445 } 446 if (fail) 447 { 448 fail("Did not throw an assertion failed error."); 449 } 450 } 451 452 453 454 static abstract class TestMonitorRunnable9 extends TestMonitorRunnable 455 { 456 public Thread runningThread = null; 457 public void runTest() throws Throwable 458 { 459 this.runningThread = Thread.currentThread(); 460 super.runTest(); 461 } 462 } 463 464 465 public void testRun9() 466 throws Throwable 467 { 468 DOC.getIT().testsIssue( 771008 ); 469 470 472 DOC.getLog().debug("testRun9"); 473 TestRunnable tr1 = new TestRunnable() { 474 public void runTest() throws Throwable 475 { 476 delay( 100 ); 477 } 478 }; 479 TestMonitorRunnable9 tm1 = new TestMonitorRunnable9() { 480 public void runMonitor() throws Throwable 481 { 482 } 484 }; 485 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 486 new TestRunnable[] { tr1 }, new TestRunnable[] { tm1 } ); 487 mttr.runTestRunnables(); 488 assertFalse( 489 "Did not stop the test monitor.", 490 tm1.runningThread.isAlive() ); 491 } 492 493 494 public void testRun10() 495 throws Throwable 496 { 497 DOC.getIT().testsIssue( 771008 ); 498 499 501 DOC.getLog().debug("testRun10"); 502 TestRunnable tr1 = new TestRunnable() { 503 public void runTest() throws Throwable 504 { 505 delay( 100 ); 506 } 507 }; 508 TestRunnable6 tm1 = new TestRunnable6() { 509 public void runMyTest() throws Throwable 510 { 511 DOC.getLog().warn("runMyTest 10 running forever"); 512 for (int i = 0; i < MAX_LOOP_COUNT; ++i) 513 { 514 try { 516 delay( 100 ); 517 } catch (InterruptedException ie) { 518 this.wasInterrupted = true; 519 DOC.getLog().warn("runMyTest 10: interrupted!"); 520 } 521 DOC.getLog().warn("runMyTest 10: Loop!"); 522 if (Thread.interrupted()) 523 { 524 this.wasInterrupted = true; 525 DOC.getLog().warn("runMyTest 10: interrupted!"); 526 } 527 } 528 fail( "This line should NEVER be reached!" ); 529 } 530 }; 531 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 532 new TestRunnable[] { tr1 }, new TestRunnable[] { tm1 } ); 533 534 mttr.runTestRunnables(); 536 537 assertTrue( 538 "monitor was not interrupted.", 539 tm1.wasInterrupted ); 540 Thread.sleep( 100 ); 541 assertFalse( 542 "Did not stop the test monitor.", 543 tm1.runningThread.isAlive() ); 544 } 545 546 public void testRun11() 547 throws Throwable 548 { 549 DOC.getIT().testsIssue( 771008 ); 550 551 553 DOC.getLog().debug("testRun11"); 554 final TesterClass tc = new TesterClass(); 555 TestRunnable tr1 = new TestRunnable() { 556 public void runTest() throws Throwable 557 { 558 tc.value = -1; 559 for (int i = 0; i < 30; ++i) 560 { 561 delay( 100 ); 562 tc.value = i; 563 } 564 } 565 }; 566 TestMonitorRunnable tm1 = new TestMonitorRunnable() { 567 public void runMonitor() throws Throwable 568 { 569 assertTrue( 570 "Value is < 0.", 571 tc.value >= 0 ); 572 } 573 }; 574 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner( 575 new TestRunnable[] { tr1 }, new TestRunnable[] { tm1 } ); 576 boolean fail = false; 577 try 578 { 579 mttr.runTestRunnables(); 580 fail = true; 581 } 582 catch (junit.framework.AssertionFailedError e) 583 { 584 assertTrue( 585 "Did not throw correct exception from monitor.", 586 e.getMessage().indexOf( "Value is < 0." ) >= 0 ); 587 } 588 if (fail) 589 { 590 fail("Did not throw an assertion failed error."); 591 } 592 } 593 594 595 598 599 public static Test suite() 600 { 601 TestSuite suite = new TestSuite( THIS_CLASS ); 602 603 return suite; 604 } 605 606 public static void main( String [] args ) 607 { 608 String [] name = { THIS_CLASS.getName() }; 609 610 613 junit.textui.TestRunner.main( name ); 614 } 615 616 617 621 protected void setUp() throws Exception 622 { 623 super.setUp(); 624 625 } 627 628 629 633 protected void tearDown() throws Exception 634 { 635 637 638 super.tearDown(); 639 } 640 } 641 642 | Popular Tags |