--- jsr166/src/test/tck/JSR166TestCase.java 2006/04/20 20:35:00 1.30 +++ jsr166/src/test/tck/JSR166TestCase.java 2009/11/21 02:07:26 1.42 @@ -9,6 +9,7 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.io.*; import java.security.*; @@ -89,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(); } @@ -105,9 +106,16 @@ 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)); + suite.addTest(new TestSuite(ForkJoinTaskTest.class)); + suite.addTest(new TestSuite(RecursiveActionTest.class)); + suite.addTest(new TestSuite(RecursiveTaskTest.class)); + suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); + suite.addTest(new TestSuite(PhaserTest.class)); + suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); suite.addTest(new TestSuite(AbstractQueueTest.class)); suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); @@ -286,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() { @@ -293,6 +309,14 @@ public class JSR166TestCase extends Test fail("Unexpected exception"); } + /** + * threadFail with message "Unexpected exception", with argument + */ + public void threadUnexpectedException(Throwable ex) { + threadFailed = true; + ex.printStackTrace(); + fail("Unexpected exception: " + ex); + } /** * Wait out termination of a thread pool or fail doing so @@ -300,10 +324,10 @@ public class JSR166TestCase extends Test public void joinPool(ExecutorService exec) { try { exec.shutdown(); - assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); - } catch(SecurityException ok) { + assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); + } catch (SecurityException ok) { // Allowed in case test doesn't have privs - } catch(InterruptedException ie) { + } catch (InterruptedException ie) { fail("Unexpected exception"); } } @@ -317,12 +341,27 @@ 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() { fail("Unexpected exception"); } + /** + * fail with message "Unexpected exception", with argument + */ + public void unexpectedException(Throwable ex) { + ex.printStackTrace(); + fail("Unexpected exception: " + ex); + } + /** * The number of elements to place in collections, arrays, etc. @@ -359,21 +398,137 @@ 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 + abstract class CheckedRunnable implements Runnable { + abstract void realRun() throws Throwable; + + public final void run() { + try { + realRun(); + } catch (Throwable t) { + threadUnexpectedException(t); + } + } + } + + 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("InterruptedException"); + } catch (InterruptedException success) { + } catch (Throwable t) { + threadUnexpectedException(t); + } + } + } + + abstract class CheckedCallable implements Callable { + abstract T realCall() throws Throwable; + + public final T call() { + try { + return realCall(); + } catch (Throwable t) { + threadUnexpectedException(t); + } + 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; + } + } + static class NoOpRunnable implements Runnable { public void run() {} } @@ -396,111 +551,75 @@ public class JSR166TestCase extends Test public Integer call() { return one; } } - class ShortRunnable implements Runnable { - public void run() { - try { - Thread.sleep(SHORT_DELAY_MS); - } - catch(Exception e) { - threadUnexpectedException(); - } + class ShortRunnable extends CheckedRunnable { + void realRun() throws Throwable { + Thread.sleep(SHORT_DELAY_MS); } } - class ShortInterruptedRunnable implements Runnable { - public void run() { - try { - Thread.sleep(SHORT_DELAY_MS); - threadShouldThrow(); - } - catch(InterruptedException success) { - } + class ShortInterruptedRunnable extends CheckedInterruptedRunnable { + void realRun() throws InterruptedException { + Thread.sleep(SHORT_DELAY_MS); } } - class SmallRunnable implements Runnable { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - } - catch(Exception e) { - threadUnexpectedException(); - } + class SmallRunnable extends CheckedRunnable { + void realRun() throws Throwable { + Thread.sleep(SMALL_DELAY_MS); } } - class SmallPossiblyInterruptedRunnable implements Runnable { - public void run() { + class SmallPossiblyInterruptedRunnable extends CheckedRunnable { + void realRun() { try { Thread.sleep(SMALL_DELAY_MS); } - catch(Exception e) { + catch (InterruptedException ok) { } } } - class SmallCallable implements Callable { - public Object call() { - try { - Thread.sleep(SMALL_DELAY_MS); - } - catch(Exception e) { - threadUnexpectedException(); - } + class SmallCallable extends CheckedCallable { + Object realCall() throws Throwable { + Thread.sleep(SMALL_DELAY_MS); return Boolean.TRUE; } } - class SmallInterruptedRunnable implements Runnable { - public void run() { - try { - Thread.sleep(SMALL_DELAY_MS); - threadShouldThrow(); - } - catch(InterruptedException success) { - } + class SmallInterruptedRunnable extends CheckedInterruptedRunnable { + void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); } } - - class MediumRunnable implements Runnable { - public void run() { - try { - Thread.sleep(MEDIUM_DELAY_MS); - } - catch(Exception e) { - threadUnexpectedException(); - } + class MediumRunnable extends CheckedRunnable { + void realRun() throws Throwable { + Thread.sleep(MEDIUM_DELAY_MS); } } - class MediumInterruptedRunnable implements Runnable { - public void run() { - try { - Thread.sleep(MEDIUM_DELAY_MS); - threadShouldThrow(); - } - catch(InterruptedException success) { - } + class MediumInterruptedRunnable extends CheckedInterruptedRunnable { + void realRun() throws InterruptedException { + Thread.sleep(MEDIUM_DELAY_MS); } } - class MediumPossiblyInterruptedRunnable implements Runnable { - public void run() { + class MediumPossiblyInterruptedRunnable extends CheckedRunnable { + void realRun() { try { Thread.sleep(MEDIUM_DELAY_MS); } - catch(InterruptedException success) { + catch (InterruptedException ok) { } } } - class LongPossiblyInterruptedRunnable implements Runnable { - public void run() { + class LongPossiblyInterruptedRunnable extends CheckedRunnable { + void realRun() { try { Thread.sleep(LONG_DELAY_MS); } - catch(InterruptedException success) { + catch (InterruptedException ok) { } } } @@ -508,8 +627,8 @@ public class JSR166TestCase extends Test /** * For use as ThreadFactory in constructors */ - static class SimpleThreadFactory implements ThreadFactory{ - public Thread newThread(Runnable r){ + static class SimpleThreadFactory implements ThreadFactory { + public Thread newThread(Runnable r) { return new Thread(r); } } @@ -520,7 +639,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(SMALL_DELAY_MS); done = true; - } catch(Exception e){ + } catch (InterruptedException ok) { } } } @@ -531,7 +650,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(MEDIUM_DELAY_MS); done = true; - } catch(Exception e){ + } catch (InterruptedException ok) { } } } @@ -542,7 +661,7 @@ public class JSR166TestCase extends Test try { Thread.sleep(LONG_DELAY_MS); done = true; - } catch(Exception e){ + } catch (InterruptedException ok) { } } } @@ -560,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; } @@ -570,9 +689,9 @@ public class JSR166TestCase extends Test /** * For use as RejectedExecutionHandler in constructors */ - static class NoOpREHandler implements RejectedExecutionHandler{ - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} + static class NoOpREHandler implements RejectedExecutionHandler { + public void rejectedExecution(Runnable r, + ThreadPoolExecutor executor) {} } - }