--- jsr166/src/test/tck/JSR166TestCase.java 2009/08/04 00:23:18 1.37
+++ jsr166/src/test/tck/JSR166TestCase.java 2009/11/21 10:25:05 1.43
@@ -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() {
@@ -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,37 @@ 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 +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);
@@ -432,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;
}
}
@@ -547,7 +639,7 @@ public class JSR166TestCase extends Test
try {
Thread.sleep(SMALL_DELAY_MS);
done = true;
- } catch (Exception e) {
+ } catch (InterruptedException ok) {
}
}
}
@@ -558,7 +650,7 @@ public class JSR166TestCase extends Test
try {
Thread.sleep(MEDIUM_DELAY_MS);
done = true;
- } catch (Exception e) {
+ } catch (InterruptedException ok) {
}
}
}
@@ -569,7 +661,7 @@ public class JSR166TestCase extends Test
try {
Thread.sleep(LONG_DELAY_MS);
done = true;
- } catch (Exception e) {
+ } catch (InterruptedException ok) {
}
}
}
@@ -587,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;
}