289 |
|
* threadFail with message "should throw exception" |
290 |
|
*/ |
291 |
|
public void threadShouldThrow() { |
292 |
< |
try { |
293 |
< |
threadFailed = true; |
294 |
< |
fail("should throw exception"); |
295 |
< |
} catch (AssertionFailedError e) { |
296 |
< |
e.printStackTrace(); |
297 |
< |
throw e; |
298 |
< |
} |
292 |
> |
threadFailed = true; |
293 |
> |
fail("should throw exception"); |
294 |
> |
} |
295 |
> |
|
296 |
> |
/** |
297 |
> |
* threadFail with message "should throw" + exceptionName |
298 |
> |
*/ |
299 |
> |
public void threadShouldThrow(String exceptionName) { |
300 |
> |
threadFailed = true; |
301 |
> |
fail("should throw " + exceptionName); |
302 |
|
} |
303 |
|
|
304 |
|
/** |
341 |
|
} |
342 |
|
|
343 |
|
/** |
344 |
+ |
* fail with message "should throw " + exceptionName |
345 |
+ |
*/ |
346 |
+ |
public void shouldThrow(String exceptionName) { |
347 |
+ |
fail("Should throw " + exceptionName); |
348 |
+ |
} |
349 |
+ |
|
350 |
+ |
/** |
351 |
|
* fail with message "Unexpected exception" |
352 |
|
*/ |
353 |
|
public void unexpectedException() { |
410 |
|
public void refresh() {} |
411 |
|
} |
412 |
|
|
413 |
+ |
/** |
414 |
+ |
* Sleep until the timeout has elapsed, or interrupted. |
415 |
+ |
* Does <em>NOT</em> throw InterruptedException. |
416 |
+ |
*/ |
417 |
+ |
void sleepTillInterrupted(long timeoutMillis) { |
418 |
+ |
try { |
419 |
+ |
Thread.sleep(timeoutMillis); |
420 |
+ |
} catch (InterruptedException wakeup) { |
421 |
+ |
} |
422 |
+ |
} |
423 |
+ |
|
424 |
+ |
/** |
425 |
+ |
* Returns a new started Thread running the given runnable. |
426 |
+ |
*/ |
427 |
+ |
Thread newStartedThread(Runnable runnable) { |
428 |
+ |
Thread t = new Thread(runnable); |
429 |
+ |
t.start(); |
430 |
+ |
return t; |
431 |
+ |
} |
432 |
|
|
433 |
|
// Some convenient Runnable classes |
434 |
|
|
444 |
|
} |
445 |
|
} |
446 |
|
|
447 |
+ |
abstract class RunnableShouldThrow implements Runnable { |
448 |
+ |
abstract void realRun() throws Throwable; |
449 |
+ |
|
450 |
+ |
final Class<?> exceptionClass; |
451 |
+ |
|
452 |
+ |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
453 |
+ |
this.exceptionClass = exceptionClass; |
454 |
+ |
} |
455 |
+ |
|
456 |
+ |
public final void run() { |
457 |
+ |
try { |
458 |
+ |
realRun(); |
459 |
+ |
threadShouldThrow(exceptionClass.getSimpleName()); |
460 |
+ |
} catch (InterruptedException success) { |
461 |
+ |
} catch (Throwable t) { |
462 |
+ |
if (! exceptionClass.isInstance(t)) |
463 |
+ |
threadUnexpectedException(t); |
464 |
+ |
} |
465 |
+ |
} |
466 |
+ |
} |
467 |
+ |
|
468 |
+ |
abstract class ThreadShouldThrow extends Thread { |
469 |
+ |
abstract void realRun() throws Throwable; |
470 |
+ |
|
471 |
+ |
final Class<?> exceptionClass; |
472 |
+ |
|
473 |
+ |
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
474 |
+ |
this.exceptionClass = exceptionClass; |
475 |
+ |
} |
476 |
+ |
|
477 |
+ |
public final void run() { |
478 |
+ |
try { |
479 |
+ |
realRun(); |
480 |
+ |
threadShouldThrow(exceptionClass.getSimpleName()); |
481 |
+ |
} catch (InterruptedException success) { |
482 |
+ |
} catch (Throwable t) { |
483 |
+ |
if (! exceptionClass.isInstance(t)) |
484 |
+ |
threadUnexpectedException(t); |
485 |
+ |
} |
486 |
+ |
} |
487 |
+ |
} |
488 |
+ |
|
489 |
|
abstract class CheckedInterruptedRunnable implements Runnable { |
490 |
|
abstract void realRun() throws Throwable; |
491 |
|
|
492 |
|
public final void run() { |
493 |
|
try { |
494 |
|
realRun(); |
495 |
< |
threadShouldThrow(); |
495 |
> |
threadShouldThrow("InterruptedException"); |
496 |
|
} catch (InterruptedException success) { |
497 |
|
} catch (Throwable t) { |
498 |
|
threadUnexpectedException(t); |
508 |
|
return realCall(); |
509 |
|
} catch (Throwable t) { |
510 |
|
threadUnexpectedException(t); |
440 |
– |
return null; |
511 |
|
} |
512 |
+ |
return null; |
513 |
+ |
} |
514 |
+ |
} |
515 |
+ |
|
516 |
+ |
abstract class CheckedInterruptedCallable<T> implements Callable<T> { |
517 |
+ |
abstract T realCall() throws Throwable; |
518 |
+ |
|
519 |
+ |
public final T call() { |
520 |
+ |
try { |
521 |
+ |
T result = realCall(); |
522 |
+ |
threadShouldThrow("InterruptedException"); |
523 |
+ |
return result; |
524 |
+ |
} catch (InterruptedException success) { |
525 |
+ |
} catch (Throwable t) { |
526 |
+ |
threadUnexpectedException(t); |
527 |
+ |
} |
528 |
+ |
return null; |
529 |
|
} |
530 |
|
} |
531 |
|
|
639 |
|
try { |
640 |
|
Thread.sleep(SMALL_DELAY_MS); |
641 |
|
done = true; |
642 |
< |
} catch (Exception e) { |
642 |
> |
} catch (InterruptedException ok) { |
643 |
|
} |
644 |
|
} |
645 |
|
} |
650 |
|
try { |
651 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
652 |
|
done = true; |
653 |
< |
} catch (Exception e) { |
653 |
> |
} catch (InterruptedException ok) { |
654 |
|
} |
655 |
|
} |
656 |
|
} |
661 |
|
try { |
662 |
|
Thread.sleep(LONG_DELAY_MS); |
663 |
|
done = true; |
664 |
< |
} catch (Exception e) { |
664 |
> |
} catch (InterruptedException ok) { |
665 |
|
} |
666 |
|
} |
667 |
|
} |
679 |
|
try { |
680 |
|
Thread.sleep(SMALL_DELAY_MS); |
681 |
|
done = true; |
682 |
< |
} catch (Exception e) { |
682 |
> |
} catch (InterruptedException ok) { |
683 |
|
} |
684 |
|
return Boolean.TRUE; |
685 |
|
} |