--- jsr166/src/test/tck/JSR166TestCase.java 2009/08/04 00:23:18 1.37 +++ jsr166/src/test/tck/JSR166TestCase.java 2009/11/21 17:38:05 1.44 @@ -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)); @@ -195,7 +195,7 @@ public class JSR166TestCase extends Test /** * Sets delays as multiples of SHORT_DELAY. */ - protected void setDelays() { + protected void setDelays() { SHORT_DELAY_MS = getShortDelay(); SMALL_DELAY_MS = SHORT_DELAY_MS * 5; MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; @@ -294,6 +294,14 @@ public class JSR166TestCase extends Test } /** + * threadFail with message "should throw" + exceptionName + */ + public void threadShouldThrow(String exceptionName) { + threadFailed = true; + fail("should throw " + exceptionName); + } + + /** * threadFail with message "Unexpected exception" */ public void threadUnexpectedException() { @@ -320,7 +328,7 @@ public class JSR166TestCase extends Test } catch (SecurityException ok) { // Allowed in case test doesn't have privs } catch (InterruptedException ie) { - fail("Unexpected exception"); + fail("Unexpected InterruptedException"); } } @@ -333,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() { @@ -383,18 +398,36 @@ public class JSR166TestCase extends Test AdjustablePolicy() { } void addPermission(Permission perm) { perms.add(perm); } void clearPermissions() { perms = new Permissions(); } - public PermissionCollection getPermissions(CodeSource cs) { - return perms; - } - public PermissionCollection getPermissions(ProtectionDomain pd) { - return perms; - } - public boolean implies(ProtectionDomain pd, Permission p) { - return perms.implies(p); - } - public void refresh() {} + public PermissionCollection getPermissions(CodeSource cs) { + return perms; + } + public PermissionCollection getPermissions(ProtectionDomain pd) { + return perms; + } + public boolean implies(ProtectionDomain pd, Permission p) { + return perms.implies(p); + } + 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 @@ -410,13 +443,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); @@ -432,8 +507,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; } } @@ -481,14 +572,12 @@ public class JSR166TestCase extends Test void realRun() { try { Thread.sleep(SMALL_DELAY_MS); - } - catch (InterruptedException ok) { - } + } catch (InterruptedException ok) {} } } class SmallCallable extends CheckedCallable { - Object realCall() throws Throwable { + Object realCall() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); return Boolean.TRUE; } @@ -516,9 +605,7 @@ public class JSR166TestCase extends Test void realRun() { try { Thread.sleep(MEDIUM_DELAY_MS); - } - catch (InterruptedException ok) { - } + } catch (InterruptedException ok) {} } } @@ -526,9 +613,7 @@ public class JSR166TestCase extends Test void realRun() { try { Thread.sleep(LONG_DELAY_MS); - } - catch (InterruptedException ok) { - } + } catch (InterruptedException ok) {} } } @@ -547,8 +632,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(SMALL_DELAY_MS); done = true; - } catch (Exception e) { - } + } catch (InterruptedException ok) {} } } @@ -558,8 +642,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(MEDIUM_DELAY_MS); done = true; - } catch (Exception e) { - } + } catch (InterruptedException ok) {} } } @@ -569,8 +652,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(LONG_DELAY_MS); done = true; - } catch (Exception e) { - } + } catch (InterruptedException ok) {} } } @@ -587,8 +669,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(SMALL_DELAY_MS); done = true; - } catch (Exception e) { - } + } catch (InterruptedException ok) {} return Boolean.TRUE; } }