--- jsr166/src/test/tck/JSR166TestCase.java 2009/08/03 22:08:45 1.36 +++ jsr166/src/test/tck/JSR166TestCase.java 2009/11/20 06:27:39 1.41 @@ -90,13 +90,13 @@ public class JSR166TestCase extends Test /** * Runs all JSR166 unit tests using junit.textui.TestRunner */ - public static void main (String[] args) { + public static void main(String[] args) { int iters = 1; if (args.length > 0) iters = Integer.parseInt(args[0]); Test s = suite(); for (int i = 0; i < iters; ++i) { - junit.textui.TestRunner.run (s); + junit.textui.TestRunner.run(s); System.gc(); System.runFinalization(); } @@ -106,7 +106,7 @@ public class JSR166TestCase extends Test /** * Collects all JSR166 unit tests as one suite */ - public static Test suite ( ) { + public static Test suite() { TestSuite suite = new TestSuite("JSR166 Unit Tests"); suite.addTest(new TestSuite(ForkJoinPoolTest.class)); @@ -289,13 +289,16 @@ public class JSR166TestCase extends Test * threadFail with message "should throw exception" */ public void threadShouldThrow() { - try { - threadFailed = true; - fail("should throw exception"); - } catch (AssertionFailedError e) { - e.printStackTrace(); - throw e; - } + threadFailed = true; + fail("should throw exception"); + } + + /** + * threadFail with message "should throw" + exceptionName + */ + public void threadShouldThrow(String exceptionName) { + threadFailed = true; + fail("should throw " + exceptionName); } /** @@ -338,6 +341,13 @@ public class JSR166TestCase extends Test } /** + * fail with message "should throw " + exceptionName + */ + public void shouldThrow(String exceptionName) { + fail("Should throw " + exceptionName); + } + + /** * fail with message "Unexpected exception" */ public void unexpectedException() { @@ -400,6 +410,25 @@ public class JSR166TestCase extends Test public void refresh() {} } + /** + * Sleep until the timeout has elapsed, or interrupted. + * Does NOT throw InterruptedException. + */ + void sleepTillInterrupted(long timeoutMillis) { + try { + Thread.sleep(timeoutMillis); + } catch (InterruptedException wakeup) { + } + } + + /** + * Returns a new started Thread running the given runnable. + */ + Thread newStartedThread(Runnable runnable) { + Thread t = new Thread(runnable); + t.start(); + return t; + } // Some convenient Runnable classes @@ -415,13 +444,55 @@ public class JSR166TestCase extends Test } } + abstract class RunnableShouldThrow implements Runnable { + abstract void realRun() throws Throwable; + + final Class exceptionClass; + + RunnableShouldThrow(Class exceptionClass) { + this.exceptionClass = exceptionClass; + } + + public final void run() { + try { + realRun(); + threadShouldThrow(exceptionClass.getSimpleName()); + } catch (InterruptedException success) { + } catch (Throwable t) { + if (! exceptionClass.isInstance(t)) + threadUnexpectedException(t); + } + } + } + + abstract class ThreadShouldThrow extends Thread { + abstract void realRun() throws Throwable; + + final Class exceptionClass; + + ThreadShouldThrow(Class exceptionClass) { + this.exceptionClass = exceptionClass; + } + + public final void run() { + try { + realRun(); + threadShouldThrow(exceptionClass.getSimpleName()); + } catch (InterruptedException success) { + } catch (Throwable t) { + if (! exceptionClass.isInstance(t)) + threadUnexpectedException(t); + } + } + } + abstract class CheckedInterruptedRunnable implements Runnable { abstract void realRun() throws Throwable; public final void run() { try { realRun(); - threadShouldThrow(); + threadShouldThrow("InterruptedException"); } catch (InterruptedException success) { } catch (Throwable t) { threadUnexpectedException(t); @@ -437,8 +508,24 @@ public class JSR166TestCase extends Test return realCall(); } catch (Throwable t) { threadUnexpectedException(t); - return null; } + return null; + } + } + + abstract class CheckedInterruptedCallable implements Callable { + abstract T realCall() throws Throwable; + + public final T call() { + try { + T result = realCall(); + threadShouldThrow("InterruptedException"); + return result; + } catch (InterruptedException success) { + } catch (Throwable t) { + threadUnexpectedException(t); + } + return null; } } @@ -552,7 +639,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(SMALL_DELAY_MS); done = true; - } catch (Exception e) { + } catch (InterruptedException ok) { } } } @@ -563,7 +650,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(MEDIUM_DELAY_MS); done = true; - } catch (Exception e) { + } catch (InterruptedException ok) { } } } @@ -574,7 +661,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(LONG_DELAY_MS); done = true; - } catch (Exception e) { + } catch (InterruptedException ok) { } } } @@ -592,7 +679,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(SMALL_DELAY_MS); done = true; - } catch (Exception e) { + } catch (InterruptedException ok) { } return Boolean.TRUE; }