--- jsr166/src/test/tck/JSR166TestCase.java 2009/07/31 23:53:23 1.34 +++ jsr166/src/test/tck/JSR166TestCase.java 2009/12/01 06:47:14 1.47 @@ -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,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)); @@ -194,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; @@ -288,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); } /** @@ -320,11 +324,11 @@ public class JSR166TestCase extends Test public void joinPool(ExecutorService exec) { try { exec.shutdown(); - assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); + assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); } catch (SecurityException ok) { // Allowed in case test doesn't have privs } catch (InterruptedException ie) { - fail("Unexpected exception"); + fail("Unexpected InterruptedException"); } } @@ -337,251 +341,333 @@ 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. */ - static final int SIZE = 20; + public static final int SIZE = 20; // Some convenient Integer constants - static final Integer zero = new Integer(0); - static final Integer one = new Integer(1); - static final Integer two = new Integer(2); - static final Integer three = new Integer(3); - static final Integer four = new Integer(4); - static final Integer five = new Integer(5); - static final Integer six = new Integer(6); - static final Integer seven = new Integer(7); - static final Integer eight = new Integer(8); - static final Integer nine = new Integer(9); - static final Integer m1 = new Integer(-1); - static final Integer m2 = new Integer(-2); - static final Integer m3 = new Integer(-3); - static final Integer m4 = new Integer(-4); - static final Integer m5 = new Integer(-5); - static final Integer m6 = new Integer(-6); - static final Integer m10 = new Integer(-10); + public static final Integer zero = new Integer(0); + public static final Integer one = new Integer(1); + public static final Integer two = new Integer(2); + public static final Integer three = new Integer(3); + public static final Integer four = new Integer(4); + public static final Integer five = new Integer(5); + public static final Integer six = new Integer(6); + public static final Integer seven = new Integer(7); + public static final Integer eight = new Integer(8); + public static final Integer nine = new Integer(9); + public static final Integer m1 = new Integer(-1); + public static final Integer m2 = new Integer(-2); + public static final Integer m3 = new Integer(-3); + public static final Integer m4 = new Integer(-4); + public static final Integer m5 = new Integer(-5); + public static final Integer m6 = new Integer(-6); + public static final Integer m10 = new Integer(-10); /** * A security policy where new permissions can be dynamically added * or all cleared. */ - static class AdjustablePolicy extends java.security.Policy { + public static class AdjustablePolicy extends java.security.Policy { Permissions perms = new Permissions(); 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() {} } - - // Some convenient Runnable classes - - static class NoOpRunnable implements Runnable { - public void run() {} + /** + * Sleep until the timeout has elapsed, or interrupted. + * Does NOT throw InterruptedException. + */ + void sleepTillInterrupted(long timeoutMillis) { + try { + Thread.sleep(timeoutMillis); + } catch (InterruptedException wakeup) {} } - static class NoOpCallable implements Callable { - public Object call() { return Boolean.TRUE; } + /** + * Returns a new started Thread running the given runnable. + */ + Thread newStartedThread(Runnable runnable) { + Thread t = new Thread(runnable); + t.start(); + return t; } - static final String TEST_STRING = "a test string"; + // Some convenient Runnable classes - static class StringTask implements Callable { - public String call() { return TEST_STRING; } - } + public abstract class CheckedRunnable implements Runnable { + protected abstract void realRun() throws Throwable; - static class NPETask implements Callable { - public String call() { throw new NullPointerException(); } + public final void run() { + try { + realRun(); + } catch (Throwable t) { + threadUnexpectedException(t); + } + } } - static class CallableOne implements Callable { - public Integer call() { return one; } - } + public abstract class RunnableShouldThrow implements Runnable { + protected abstract void realRun() throws Throwable; - class ShortRunnable implements Runnable { - public void run() { + final Class exceptionClass; + + RunnableShouldThrow(Class exceptionClass) { + this.exceptionClass = exceptionClass; + } + + public final void run() { try { - Thread.sleep(SHORT_DELAY_MS); - } - catch (Exception e) { - threadUnexpectedException(e); + realRun(); + threadShouldThrow(exceptionClass.getSimpleName()); + } catch (Throwable t) { + if (! exceptionClass.isInstance(t)) + threadUnexpectedException(t); } } } - class ShortInterruptedRunnable implements Runnable { - public void run() { + public abstract class ThreadShouldThrow extends Thread { + protected abstract void realRun() throws Throwable; + + final Class exceptionClass; + + ThreadShouldThrow(Class exceptionClass) { + this.exceptionClass = exceptionClass; + } + + public final void run() { try { - Thread.sleep(SHORT_DELAY_MS); - threadShouldThrow(); - } - catch (InterruptedException success) { + realRun(); + threadShouldThrow(exceptionClass.getSimpleName()); + } catch (Throwable t) { + if (! exceptionClass.isInstance(t)) + threadUnexpectedException(t); } } } - class SmallRunnable implements Runnable { - public void run() { + public abstract class CheckedInterruptedRunnable implements Runnable { + protected abstract void realRun() throws Throwable; + + public final void run() { try { - Thread.sleep(SMALL_DELAY_MS); - } - catch (Exception e) { - threadUnexpectedException(e); + realRun(); + threadShouldThrow("InterruptedException"); + } catch (InterruptedException success) { + } catch (Throwable t) { + threadUnexpectedException(t); } } } - class SmallPossiblyInterruptedRunnable implements Runnable { - public void run() { + public abstract class CheckedCallable implements Callable { + protected abstract T realCall() throws Throwable; + + public final T call() { try { - Thread.sleep(SMALL_DELAY_MS); - } - catch (Exception e) { + return realCall(); + } catch (Throwable t) { + threadUnexpectedException(t); } + return null; } } - class SmallCallable implements Callable { - public Object call() { + public abstract class CheckedInterruptedCallable implements Callable { + protected abstract T realCall() throws Throwable; + + public final T call() { try { - Thread.sleep(SMALL_DELAY_MS); - } - catch (Exception e) { - threadUnexpectedException(e); + T result = realCall(); + threadShouldThrow("InterruptedException"); + return result; + } catch (InterruptedException success) { + } catch (Throwable t) { + threadUnexpectedException(t); } - return Boolean.TRUE; + return null; } } - class SmallInterruptedRunnable implements Runnable { - public void run() { + public static class NoOpRunnable implements Runnable { + public void run() {} + } + + public static class NoOpCallable implements Callable { + public Object call() { return Boolean.TRUE; } + } + + public static final String TEST_STRING = "a test string"; + + public static class StringTask implements Callable { + public String call() { return TEST_STRING; } + } + + public static class NPETask implements Callable { + public String call() { throw new NullPointerException(); } + } + + public static class CallableOne implements Callable { + public Integer call() { return one; } + } + + public class ShortRunnable extends CheckedRunnable { + protected void realRun() throws Throwable { + Thread.sleep(SHORT_DELAY_MS); + } + } + + public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { + protected void realRun() throws InterruptedException { + Thread.sleep(SHORT_DELAY_MS); + } + } + + public class SmallRunnable extends CheckedRunnable { + protected void realRun() throws Throwable { + Thread.sleep(SMALL_DELAY_MS); + } + } + + public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { + protected void realRun() { try { Thread.sleep(SMALL_DELAY_MS); - threadShouldThrow(); - } - catch (InterruptedException success) { - } + } catch (InterruptedException ok) {} } } + public class SmallCallable extends CheckedCallable { + protected Object realCall() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); + return Boolean.TRUE; + } + } - class MediumRunnable implements Runnable { - public void run() { - try { - Thread.sleep(MEDIUM_DELAY_MS); - } - catch (Exception e) { - threadUnexpectedException(e); - } + public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { + protected void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); } } - class MediumInterruptedRunnable implements Runnable { - public void run() { - try { - Thread.sleep(MEDIUM_DELAY_MS); - threadShouldThrow(); - } - catch (InterruptedException success) { - } + public class MediumRunnable extends CheckedRunnable { + protected void realRun() throws Throwable { + Thread.sleep(MEDIUM_DELAY_MS); } } - class MediumPossiblyInterruptedRunnable implements Runnable { - public void run() { + public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { + protected void realRun() throws InterruptedException { + Thread.sleep(MEDIUM_DELAY_MS); + } + } + + public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { + protected void realRun() { try { Thread.sleep(MEDIUM_DELAY_MS); - } - catch (InterruptedException success) { - } + } catch (InterruptedException ok) {} } } - class LongPossiblyInterruptedRunnable implements Runnable { - public void run() { + public class LongPossiblyInterruptedRunnable extends CheckedRunnable { + protected void realRun() { try { Thread.sleep(LONG_DELAY_MS); - } - catch (InterruptedException success) { - } + } catch (InterruptedException ok) {} } } /** * For use as ThreadFactory in constructors */ - static class SimpleThreadFactory implements ThreadFactory { + public static class SimpleThreadFactory implements ThreadFactory { public Thread newThread(Runnable r) { return new Thread(r); } } - static class TrackedShortRunnable implements Runnable { - volatile boolean done = false; + public static class TrackedShortRunnable implements Runnable { + public volatile boolean done = false; public void run() { try { Thread.sleep(SMALL_DELAY_MS); done = true; - } catch (Exception e) { - } + } catch (InterruptedException ok) {} } } - static class TrackedMediumRunnable implements Runnable { - volatile boolean done = false; + public static class TrackedMediumRunnable implements Runnable { + public volatile boolean done = false; public void run() { try { Thread.sleep(MEDIUM_DELAY_MS); done = true; - } catch (Exception e) { - } + } catch (InterruptedException ok) {} } } - static class TrackedLongRunnable implements Runnable { - volatile boolean done = false; + public static class TrackedLongRunnable implements Runnable { + public volatile boolean done = false; public void run() { try { Thread.sleep(LONG_DELAY_MS); done = true; - } catch (Exception e) { - } + } catch (InterruptedException ok) {} } } - static class TrackedNoOpRunnable implements Runnable { - volatile boolean done = false; + public static class TrackedNoOpRunnable implements Runnable { + public volatile boolean done = false; public void run() { done = true; } } - static class TrackedCallable implements Callable { - volatile boolean done = false; + public static class TrackedCallable implements Callable { + public volatile boolean done = false; public Object call() { try { Thread.sleep(SMALL_DELAY_MS); done = true; - } catch (Exception e) { - } + } catch (InterruptedException ok) {} return Boolean.TRUE; } } @@ -590,9 +676,9 @@ public class JSR166TestCase extends Test /** * For use as RejectedExecutionHandler in constructors */ - static class NoOpREHandler implements RejectedExecutionHandler { - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} + public static class NoOpREHandler implements RejectedExecutionHandler { + public void rejectedExecution(Runnable r, + ThreadPoolExecutor executor) {} } - }