ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
(Generate patch)

Comparing jsr166/src/test/tck/JSR166TestCase.java (file contents):
Revision 1.30 by dl, Thu Apr 20 20:35:00 2006 UTC vs.
Revision 1.53 by jsr166, Thu Sep 16 00:52:49 2010 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
10 < import java.util.*;
10 > import java.util.PropertyPermission;
11   import java.util.concurrent.*;
12 < import java.io.*;
13 < import java.security.*;
12 > import java.util.concurrent.atomic.AtomicReference;
13 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 > import java.security.CodeSource;
15 > import java.security.Permission;
16 > import java.security.PermissionCollection;
17 > import java.security.Permissions;
18 > import java.security.Policy;
19 > import java.security.ProtectionDomain;
20 > import java.security.SecurityPermission;
21  
22   /**
23   * Base class for JSR166 Junit TCK tests.  Defines some constants,
# Line 25 | Line 32 | import java.security.*;
32   * <li> All assertions in code running in generated threads must use
33   * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
34   * #threadAssertEquals}, or {@link #threadAssertNull}, (not
35 < * <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not
35 > * {@code fail}, {@code assertTrue}, etc.) It is OK (but not
36   * particularly recommended) for other code to use these forms too.
37   * Only the most typically used JUnit assertion methods are defined
38   * this way, but enough to live with.</li>
39   *
40   * <li> If you override {@link #setUp} or {@link #tearDown}, make sure
41 < * to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within
41 > * to invoke {@code super.setUp} and {@code super.tearDown} within
42   * them. These methods are used to clear and check for thread
43   * assertion failures.</li>
44   *
45 < * <li>All delays and timeouts must use one of the constants <tt>
46 < * SHORT_DELAY_MS</tt>, <tt> SMALL_DELAY_MS</tt>, <tt> MEDIUM_DELAY_MS</tt>,
47 < * <tt> LONG_DELAY_MS</tt>. The idea here is that a SHORT is always
45 > * <li>All delays and timeouts must use one of the constants {@code
46 > * SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS},
47 > * {@code LONG_DELAY_MS}. The idea here is that a SHORT is always
48   * discriminable from zero time, and always allows enough time for the
49   * small amounts of computation (creating a thread, calling a few
50   * methods, etc) needed to reach a timeout point. Similarly, a SMALL
# Line 47 | Line 54 | import java.security.*;
54   * in one spot to rerun tests on slower platforms.</li>
55   *
56   * <li> All threads generated must be joined inside each test case
57 < * method (or <tt>fail</tt> to do so) before returning from the
58 < * method. The <tt> joinPool</tt> method can be used to do this when
57 > * method (or {@code fail} to do so) before returning from the
58 > * method. The {@code joinPool} method can be used to do this when
59   * using Executors.</li>
60   *
61   * </ol>
# Line 80 | Line 87 | import java.security.*;
87   * any particular package to simplify things for people integrating
88   * them in TCK test suites.</li>
89   *
90 < * <li> As a convenience, the <tt>main</tt> of this class (JSR166TestCase)
90 > * <li> As a convenience, the {@code main} of this class (JSR166TestCase)
91   * runs all JSR166 unit tests.</li>
92   *
93   * </ul>
94   */
95   public class JSR166TestCase extends TestCase {
96 +    private static final boolean useSecurityManager =
97 +        Boolean.getBoolean("jsr166.useSecurityManager");
98 +
99      /**
100       * Runs all JSR166 unit tests using junit.textui.TestRunner
101       */
102 <    public static void main (String[] args) {
102 >    public static void main(String[] args) {
103 >        if (useSecurityManager) {
104 >            System.err.println("Setting a permissive security manager");
105 >            Policy.setPolicy(permissivePolicy());
106 >            System.setSecurityManager(new SecurityManager());
107 >        }
108          int iters = 1;
109          if (args.length > 0)
110              iters = Integer.parseInt(args[0]);
111          Test s = suite();
112          for (int i = 0; i < iters; ++i) {
113 <            junit.textui.TestRunner.run (s);
113 >            junit.textui.TestRunner.run(s);
114              System.gc();
115              System.runFinalization();
116          }
# Line 105 | Line 120 | public class JSR166TestCase extends Test
120      /**
121       * Collects all JSR166 unit tests as one suite
122       */
123 <    public static Test suite ( ) {
123 >    public static Test suite() {
124          TestSuite suite = new TestSuite("JSR166 Unit Tests");
125  
126 +        suite.addTest(new TestSuite(ForkJoinPoolTest.class));
127 +        suite.addTest(new TestSuite(ForkJoinTaskTest.class));
128 +        suite.addTest(new TestSuite(RecursiveActionTest.class));
129 +        suite.addTest(new TestSuite(RecursiveTaskTest.class));
130 +        suite.addTest(new TestSuite(LinkedTransferQueueTest.class));
131 +        suite.addTest(new TestSuite(PhaserTest.class));
132 +        suite.addTest(new TestSuite(ThreadLocalRandomTest.class));
133          suite.addTest(new TestSuite(AbstractExecutorServiceTest.class));
134          suite.addTest(new TestSuite(AbstractQueueTest.class));
135          suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class));
# Line 127 | Line 149 | public class JSR166TestCase extends Test
149          suite.addTest(new TestSuite(AtomicReferenceTest.class));
150          suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
151          suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
152 +        suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class));
153          suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
154          suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class));
155          suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class));
# Line 187 | Line 210 | public class JSR166TestCase extends Test
210      /**
211       * Sets delays as multiples of SHORT_DELAY.
212       */
213 <    protected  void setDelays() {
213 >    protected void setDelays() {
214          SHORT_DELAY_MS = getShortDelay();
215 <        SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
215 >        SMALL_DELAY_MS  = SHORT_DELAY_MS * 5;
216          MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
217 <        LONG_DELAY_MS = SHORT_DELAY_MS * 50;
217 >        LONG_DELAY_MS   = SHORT_DELAY_MS * 50;
218      }
219  
220      /**
221 <     * Flag set true if any threadAssert methods fail
221 >     * The first exception encountered if any threadAssertXXX method fails.
222       */
223 <    volatile boolean threadFailed;
223 >    private final AtomicReference<Throwable> threadFailure
224 >        = new AtomicReference<Throwable>(null);
225  
226      /**
227 <     * Initializes test to indicate that no thread assertions have failed
227 >     * Records an exception so that it can be rethrown later in the test
228 >     * harness thread, triggering a test case failure.  Only the first
229 >     * failure is recorded; subsequent calls to this method from within
230 >     * the same test have no effect.
231       */
232 +    public void threadRecordFailure(Throwable t) {
233 +        threadFailure.compareAndSet(null, t);
234 +    }
235 +
236      public void setUp() {
237          setDelays();
207        threadFailed = false;
238      }
239  
240      /**
241 <     * Triggers test case failure if any thread assertions have failed
242 <     */
243 <    public void tearDown() {
244 <        assertFalse(threadFailed);
241 >     * Triggers test case failure if any thread assertions have failed,
242 >     * by rethrowing, in the test harness thread, any exception recorded
243 >     * earlier by threadRecordFailure.
244 >     */
245 >    public void tearDown() throws Exception {
246 >        Throwable t = threadFailure.get();
247 >        if (t != null) {
248 >            if (t instanceof Error)
249 >                throw (Error) t;
250 >            else if (t instanceof RuntimeException)
251 >                throw (RuntimeException) t;
252 >            else if (t instanceof Exception)
253 >                throw (Exception) t;
254 >            else
255 >                throw new AssertionError(t);
256 >        }
257      }
258  
259      /**
260 <     * Fail, also setting status to indicate current testcase should fail
260 >     * Just like fail(reason), but additionally recording (using
261 >     * threadRecordFailure) any AssertionError thrown, so that the current
262 >     * testcase will fail.
263       */
264      public void threadFail(String reason) {
265 <        threadFailed = true;
266 <        fail(reason);
265 >        try {
266 >            fail(reason);
267 >        } catch (Throwable t) {
268 >            threadRecordFailure(t);
269 >            fail(reason);
270 >        }
271      }
272  
273      /**
274 <     * If expression not true, set status to indicate current testcase
275 <     * should fail
274 >     * Just like assertTrue(b), but additionally recording (using
275 >     * threadRecordFailure) any AssertionError thrown, so that the current
276 >     * testcase will fail.
277       */
278      public void threadAssertTrue(boolean b) {
279 <        if (!b) {
231 <            threadFailed = true;
279 >        try {
280              assertTrue(b);
281 +        } catch (AssertionError t) {
282 +            threadRecordFailure(t);
283 +            throw t;
284          }
285      }
286  
287      /**
288 <     * If expression not false, set status to indicate current testcase
289 <     * should fail
288 >     * Just like assertFalse(b), but additionally recording (using
289 >     * threadRecordFailure) any AssertionError thrown, so that the
290 >     * current testcase will fail.
291       */
292      public void threadAssertFalse(boolean b) {
293 <        if (b) {
242 <            threadFailed = true;
293 >        try {
294              assertFalse(b);
295 +        } catch (AssertionError t) {
296 +            threadRecordFailure(t);
297 +            throw t;
298          }
299      }
300  
301      /**
302 <     * If argument not null, set status to indicate current testcase
303 <     * should fail
302 >     * Just like assertNull(x), but additionally recording (using
303 >     * threadRecordFailure) any AssertionError thrown, so that the
304 >     * current testcase will fail.
305       */
306      public void threadAssertNull(Object x) {
307 <        if (x != null) {
253 <            threadFailed = true;
307 >        try {
308              assertNull(x);
309 +        } catch (AssertionError t) {
310 +            threadRecordFailure(t);
311 +            throw t;
312          }
313      }
314  
315      /**
316 <     * If arguments not equal, set status to indicate current testcase
317 <     * should fail
316 >     * Just like assertEquals(x, y), but additionally recording (using
317 >     * threadRecordFailure) any AssertionError thrown, so that the
318 >     * current testcase will fail.
319       */
320      public void threadAssertEquals(long x, long y) {
321 <        if (x != y) {
264 <            threadFailed = true;
321 >        try {
322              assertEquals(x, y);
323 +        } catch (AssertionError t) {
324 +            threadRecordFailure(t);
325 +            throw t;
326          }
327      }
328  
329      /**
330 <     * If arguments not equal, set status to indicate current testcase
331 <     * should fail
330 >     * Just like assertEquals(x, y), but additionally recording (using
331 >     * threadRecordFailure) any AssertionError thrown, so that the
332 >     * current testcase will fail.
333       */
334      public void threadAssertEquals(Object x, Object y) {
335 <        if (x != y && (x == null || !x.equals(y))) {
275 <            threadFailed = true;
335 >        try {
336              assertEquals(x, y);
337 +        } catch (AssertionError t) {
338 +            threadRecordFailure(t);
339 +            throw t;
340 +        }
341 +    }
342 +
343 +    /**
344 +     * Just like assertSame(x, y), but additionally recording (using
345 +     * threadRecordFailure) any AssertionError thrown, so that the
346 +     * current testcase will fail.
347 +     */
348 +    public void threadAssertSame(Object x, Object y) {
349 +        try {
350 +            assertSame(x, y);
351 +        } catch (AssertionError t) {
352 +            threadRecordFailure(t);
353 +            throw t;
354          }
355      }
356  
357      /**
358 <     * threadFail with message "should throw exception"
358 >     * Calls threadFail with message "should throw exception".
359       */
360      public void threadShouldThrow() {
361 <        threadFailed = true;
285 <        fail("should throw exception");
361 >        threadFail("should throw exception");
362      }
363  
364      /**
365 <     * threadFail with message "Unexpected exception"
365 >     * Calls threadFail with message "should throw" + exceptionName.
366       */
367 <    public void threadUnexpectedException() {
368 <        threadFailed = true;
293 <        fail("Unexpected exception");
367 >    public void threadShouldThrow(String exceptionName) {
368 >        threadFail("should throw " + exceptionName);
369      }
370  
371 +    /**
372 +     * Calls threadFail with message "Unexpected exception" + ex.
373 +     */
374 +    public void threadUnexpectedException(Throwable t) {
375 +        threadRecordFailure(t);
376 +        t.printStackTrace();
377 +        // Rethrow, wrapping in an AssertionError if necessary
378 +        if (t instanceof RuntimeException)
379 +            throw (RuntimeException) t;
380 +        else if (t instanceof Error)
381 +            throw (Error) t;
382 +        else {
383 +            AssertionError ae = new AssertionError("unexpected exception: " + t);
384 +            t.initCause(t);
385 +            throw ae;
386 +        }            
387 +    }
388  
389      /**
390 <     * Wait out termination of a thread pool or fail doing so
390 >     * Waits out termination of a thread pool or fails doing so.
391       */
392      public void joinPool(ExecutorService exec) {
393          try {
394              exec.shutdown();
395 <            assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
396 <        } catch(SecurityException ok) {
395 >            assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
396 >        } catch (SecurityException ok) {
397              // Allowed in case test doesn't have privs
398 <        } catch(InterruptedException ie) {
399 <            fail("Unexpected exception");
398 >        } catch (InterruptedException ie) {
399 >            fail("Unexpected InterruptedException");
400          }
401      }
402  
403  
404      /**
405 <     * fail with message "should throw exception"
405 >     * Fails with message "should throw exception".
406       */
407      public void shouldThrow() {
408          fail("Should throw exception");
409      }
410  
411      /**
412 <     * fail with message "Unexpected exception"
412 >     * Fails with message "should throw " + exceptionName.
413 >     */
414 >    public void shouldThrow(String exceptionName) {
415 >        fail("Should throw " + exceptionName);
416 >    }
417 >
418 >    /**
419 >     * Fails with message "Unexpected exception: " + ex.
420       */
421 <    public void unexpectedException() {
422 <        fail("Unexpected exception");
421 >    public void unexpectedException(Throwable ex) {
422 >        ex.printStackTrace();
423 >        fail("Unexpected exception: " + ex);
424      }
425  
426  
427      /**
428       * The number of elements to place in collections, arrays, etc.
429       */
430 <    static final int SIZE = 20;
430 >    public static final int SIZE = 20;
431  
432      // Some convenient Integer constants
433  
434 <    static final Integer zero = new Integer(0);
435 <    static final Integer one = new Integer(1);
436 <    static final Integer two = new Integer(2);
437 <    static final Integer three  = new Integer(3);
438 <    static final Integer four  = new Integer(4);
439 <    static final Integer five  = new Integer(5);
440 <    static final Integer six = new Integer(6);
441 <    static final Integer seven = new Integer(7);
442 <    static final Integer eight = new Integer(8);
443 <    static final Integer nine = new Integer(9);
444 <    static final Integer m1  = new Integer(-1);
445 <    static final Integer m2  = new Integer(-2);
446 <    static final Integer m3  = new Integer(-3);
447 <    static final Integer m4 = new Integer(-4);
448 <    static final Integer m5 = new Integer(-5);
449 <    static final Integer m6 = new Integer(-6);
450 <    static final Integer m10 = new Integer(-10);
434 >    public static final Integer zero  = new Integer(0);
435 >    public static final Integer one   = new Integer(1);
436 >    public static final Integer two   = new Integer(2);
437 >    public static final Integer three = new Integer(3);
438 >    public static final Integer four  = new Integer(4);
439 >    public static final Integer five  = new Integer(5);
440 >    public static final Integer six   = new Integer(6);
441 >    public static final Integer seven = new Integer(7);
442 >    public static final Integer eight = new Integer(8);
443 >    public static final Integer nine  = new Integer(9);
444 >    public static final Integer m1  = new Integer(-1);
445 >    public static final Integer m2  = new Integer(-2);
446 >    public static final Integer m3  = new Integer(-3);
447 >    public static final Integer m4  = new Integer(-4);
448 >    public static final Integer m5  = new Integer(-5);
449 >    public static final Integer m6  = new Integer(-6);
450 >    public static final Integer m10 = new Integer(-10);
451  
452  
453      /**
454 +     * Runs Runnable r with a security policy that permits precisely
455 +     * the specified permissions.  If there is no current security
456 +     * manager, the runnable is run twice, both with and without a
457 +     * security manager.  We require that any security manager permit
458 +     * getPolicy/setPolicy.
459 +     */
460 +    public void runWithPermissions(Runnable r, Permission... permissions) {
461 +        SecurityManager sm = System.getSecurityManager();
462 +        if (sm == null) {
463 +            r.run();
464 +            Policy savedPolicy = Policy.getPolicy();
465 +            try {
466 +                Policy.setPolicy(permissivePolicy());
467 +                System.setSecurityManager(new SecurityManager());
468 +                runWithPermissions(r, permissions);
469 +            } finally {
470 +                System.setSecurityManager(null);
471 +                Policy.setPolicy(savedPolicy);
472 +            }
473 +        } else {
474 +            Policy savedPolicy = Policy.getPolicy();
475 +            AdjustablePolicy policy = new AdjustablePolicy(permissions);
476 +            Policy.setPolicy(policy);
477 +
478 +            try {
479 +                r.run();
480 +            } finally {
481 +                policy.addPermission(new SecurityPermission("setPolicy"));
482 +                Policy.setPolicy(savedPolicy);
483 +            }
484 +        }
485 +    }
486 +
487 +    /**
488 +     * Runs a runnable without any permissions.
489 +     */
490 +    public void runWithoutPermissions(Runnable r) {
491 +        runWithPermissions(r);
492 +    }
493 +
494 +    /**
495       * A security policy where new permissions can be dynamically added
496       * or all cleared.
497       */
498 <    static class AdjustablePolicy extends java.security.Policy {
498 >    public static class AdjustablePolicy extends java.security.Policy {
499          Permissions perms = new Permissions();
500 <        AdjustablePolicy() { }
500 >        AdjustablePolicy(Permission... permissions) {
501 >            for (Permission permission : permissions)
502 >                perms.add(permission);
503 >        }
504          void addPermission(Permission perm) { perms.add(perm); }
505          void clearPermissions() { perms = new Permissions(); }
506 <        public PermissionCollection getPermissions(CodeSource cs) {
507 <            return perms;
508 <        }
509 <        public PermissionCollection getPermissions(ProtectionDomain pd) {
510 <            return perms;
511 <        }
512 <        public boolean implies(ProtectionDomain pd, Permission p) {
513 <            return perms.implies(p);
514 <        }
515 <        public void refresh() {}
506 >        public PermissionCollection getPermissions(CodeSource cs) {
507 >            return perms;
508 >        }
509 >        public PermissionCollection getPermissions(ProtectionDomain pd) {
510 >            return perms;
511 >        }
512 >        public boolean implies(ProtectionDomain pd, Permission p) {
513 >            return perms.implies(p);
514 >        }
515 >        public void refresh() {}
516      }
517  
518 +    /**
519 +     * Returns a policy containing all the permissions we ever need.
520 +     */
521 +    public static Policy permissivePolicy() {
522 +        return new AdjustablePolicy
523 +            // Permissions j.u.c. needs directly
524 +            (new RuntimePermission("modifyThread"),
525 +             new RuntimePermission("getClassLoader"),
526 +             new RuntimePermission("setContextClassLoader"),
527 +             // Permissions needed to change permissions!
528 +             new SecurityPermission("getPolicy"),
529 +             new SecurityPermission("setPolicy"),
530 +             new RuntimePermission("setSecurityManager"),
531 +             // Permissions needed by the junit test harness
532 +             new RuntimePermission("accessDeclaredMembers"),
533 +             new PropertyPermission("*", "read"),
534 +             new java.io.FilePermission("<<ALL FILES>>", "read"));
535 +    }
536  
537 <    // Some convenient Runnable classes
538 <
539 <    static class NoOpRunnable implements Runnable {
540 <        public void run() {}
537 >    /**
538 >     * Sleep until the timeout has elapsed, or interrupted.
539 >     * Does <em>NOT</em> throw InterruptedException.
540 >     */
541 >    void sleepTillInterrupted(long timeoutMillis) {
542 >        try {
543 >            Thread.sleep(timeoutMillis);
544 >        } catch (InterruptedException wakeup) {}
545      }
546  
547 <    static class NoOpCallable implements Callable {
548 <        public Object call() { return Boolean.TRUE; }
547 >    /**
548 >     * Returns a new started Thread running the given runnable.
549 >     */
550 >    Thread newStartedThread(Runnable runnable) {
551 >        Thread t = new Thread(runnable);
552 >        t.start();
553 >        return t;
554      }
555  
556 <    static final String TEST_STRING = "a test string";
556 >    // Some convenient Runnable classes
557  
558 <    static class StringTask implements Callable<String> {
559 <        public String call() { return TEST_STRING; }
389 <    }
558 >    public abstract class CheckedRunnable implements Runnable {
559 >        protected abstract void realRun() throws Throwable;
560  
561 <    static class NPETask implements Callable<String> {
562 <        public String call() { throw new NullPointerException(); }
561 >        public final void run() {
562 >            try {
563 >                realRun();
564 >            } catch (Throwable t) {
565 >                threadUnexpectedException(t);
566 >            }
567 >        }
568      }
569  
570 <    static class CallableOne implements Callable<Integer> {
571 <        public Integer call() { return one; }
397 <    }
570 >    public abstract class RunnableShouldThrow implements Runnable {
571 >        protected abstract void realRun() throws Throwable;
572  
573 <    class ShortRunnable implements Runnable {
574 <        public void run() {
573 >        final Class<?> exceptionClass;
574 >
575 >        <T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) {
576 >            this.exceptionClass = exceptionClass;
577 >        }
578 >
579 >        public final void run() {
580              try {
581 <                Thread.sleep(SHORT_DELAY_MS);
582 <            }
583 <            catch(Exception e) {
584 <                threadUnexpectedException();
581 >                realRun();
582 >                threadShouldThrow(exceptionClass.getSimpleName());
583 >            } catch (Throwable t) {
584 >                if (! exceptionClass.isInstance(t))
585 >                    threadUnexpectedException(t);
586              }
587          }
588      }
589  
590 <    class ShortInterruptedRunnable implements Runnable {
591 <        public void run() {
590 >    public abstract class ThreadShouldThrow extends Thread {
591 >        protected abstract void realRun() throws Throwable;
592 >
593 >        final Class<?> exceptionClass;
594 >
595 >        <T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) {
596 >            this.exceptionClass = exceptionClass;
597 >        }
598 >
599 >        public final void run() {
600              try {
601 <                Thread.sleep(SHORT_DELAY_MS);
602 <                threadShouldThrow();
603 <            }
604 <            catch(InterruptedException success) {
601 >                realRun();
602 >                threadShouldThrow(exceptionClass.getSimpleName());
603 >            } catch (Throwable t) {
604 >                if (! exceptionClass.isInstance(t))
605 >                    threadUnexpectedException(t);
606              }
607          }
608      }
609  
610 <    class SmallRunnable implements Runnable {
611 <        public void run() {
610 >    public abstract class CheckedInterruptedRunnable implements Runnable {
611 >        protected abstract void realRun() throws Throwable;
612 >
613 >        public final void run() {
614              try {
615 <                Thread.sleep(SMALL_DELAY_MS);
616 <            }
617 <            catch(Exception e) {
618 <                threadUnexpectedException();
615 >                realRun();
616 >                threadShouldThrow("InterruptedException");
617 >            } catch (InterruptedException success) {
618 >            } catch (Throwable t) {
619 >                threadUnexpectedException(t);
620              }
621          }
622      }
623  
624 <    class SmallPossiblyInterruptedRunnable implements Runnable {
625 <        public void run() {
624 >    public abstract class CheckedCallable<T> implements Callable<T> {
625 >        protected abstract T realCall() throws Throwable;
626 >
627 >        public final T call() {
628              try {
629 <                Thread.sleep(SMALL_DELAY_MS);
630 <            }
631 <            catch(Exception e) {
629 >                return realCall();
630 >            } catch (Throwable t) {
631 >                threadUnexpectedException(t);
632 >                return null;
633              }
634          }
635      }
636  
637 <    class SmallCallable implements Callable {
638 <        public Object call() {
637 >    public abstract class CheckedInterruptedCallable<T>
638 >        implements Callable<T> {
639 >        protected abstract T realCall() throws Throwable;
640 >
641 >        public final T call() {
642              try {
643 <                Thread.sleep(SMALL_DELAY_MS);
644 <            }
645 <            catch(Exception e) {
646 <                threadUnexpectedException();
643 >                T result = realCall();
644 >                threadShouldThrow("InterruptedException");
645 >                return result;
646 >            } catch (InterruptedException success) {
647 >            } catch (Throwable t) {
648 >                threadUnexpectedException(t);
649              }
650 <            return Boolean.TRUE;
650 >            return null;
651          }
652      }
653  
654 <    class SmallInterruptedRunnable implements Runnable {
655 <        public void run() {
654 >    public static class NoOpRunnable implements Runnable {
655 >        public void run() {}
656 >    }
657 >
658 >    public static class NoOpCallable implements Callable {
659 >        public Object call() { return Boolean.TRUE; }
660 >    }
661 >
662 >    public static final String TEST_STRING = "a test string";
663 >
664 >    public static class StringTask implements Callable<String> {
665 >        public String call() { return TEST_STRING; }
666 >    }
667 >
668 >    public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
669 >        return new CheckedCallable<String>() {
670 >            public String realCall() {
671 >                try {
672 >                    latch.await();
673 >                } catch (InterruptedException quittingTime) {}
674 >                return TEST_STRING;
675 >            }};
676 >    }
677 >
678 >    public static class NPETask implements Callable<String> {
679 >        public String call() { throw new NullPointerException(); }
680 >    }
681 >
682 >    public static class CallableOne implements Callable<Integer> {
683 >        public Integer call() { return one; }
684 >    }
685 >
686 >    public class ShortRunnable extends CheckedRunnable {
687 >        protected void realRun() throws Throwable {
688 >            Thread.sleep(SHORT_DELAY_MS);
689 >        }
690 >    }
691 >
692 >    public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
693 >        protected void realRun() throws InterruptedException {
694 >            Thread.sleep(SHORT_DELAY_MS);
695 >        }
696 >    }
697 >
698 >    public class SmallRunnable extends CheckedRunnable {
699 >        protected void realRun() throws Throwable {
700 >            Thread.sleep(SMALL_DELAY_MS);
701 >        }
702 >    }
703 >
704 >    public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
705 >        protected void realRun() {
706              try {
707                  Thread.sleep(SMALL_DELAY_MS);
708 <                threadShouldThrow();
709 <            }
710 <            catch(InterruptedException success) {
711 <            }
708 >            } catch (InterruptedException ok) {}
709 >        }
710 >    }
711 >
712 >    public class SmallCallable extends CheckedCallable {
713 >        protected Object realCall() throws InterruptedException {
714 >            Thread.sleep(SMALL_DELAY_MS);
715 >            return Boolean.TRUE;
716          }
717      }
718  
719 +    public class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
720 +        protected void realRun() throws InterruptedException {
721 +            Thread.sleep(SMALL_DELAY_MS);
722 +        }
723 +    }
724  
725 <    class MediumRunnable implements Runnable {
726 <        public void run() {
727 <            try {
469 <                Thread.sleep(MEDIUM_DELAY_MS);
470 <            }
471 <            catch(Exception e) {
472 <                threadUnexpectedException();
473 <            }
725 >    public class MediumRunnable extends CheckedRunnable {
726 >        protected void realRun() throws Throwable {
727 >            Thread.sleep(MEDIUM_DELAY_MS);
728          }
729      }
730  
731 <    class MediumInterruptedRunnable implements Runnable {
732 <        public void run() {
733 <            try {
480 <                Thread.sleep(MEDIUM_DELAY_MS);
481 <                threadShouldThrow();
482 <            }
483 <            catch(InterruptedException success) {
484 <            }
731 >    public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
732 >        protected void realRun() throws InterruptedException {
733 >            Thread.sleep(MEDIUM_DELAY_MS);
734          }
735      }
736  
737 <    class MediumPossiblyInterruptedRunnable implements Runnable {
738 <        public void run() {
737 >    public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
738 >        protected void realRun() {
739              try {
740                  Thread.sleep(MEDIUM_DELAY_MS);
741 <            }
493 <            catch(InterruptedException success) {
494 <            }
741 >            } catch (InterruptedException ok) {}
742          }
743      }
744  
745 <    class LongPossiblyInterruptedRunnable implements Runnable {
746 <        public void run() {
745 >    public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
746 >        protected void realRun() {
747              try {
748                  Thread.sleep(LONG_DELAY_MS);
749 <            }
503 <            catch(InterruptedException success) {
504 <            }
749 >            } catch (InterruptedException ok) {}
750          }
751      }
752  
753      /**
754       * For use as ThreadFactory in constructors
755       */
756 <    static class SimpleThreadFactory implements ThreadFactory{
757 <        public Thread newThread(Runnable r){
756 >    public static class SimpleThreadFactory implements ThreadFactory {
757 >        public Thread newThread(Runnable r) {
758              return new Thread(r);
759          }
760      }
761  
762 <    static class TrackedShortRunnable implements Runnable {
763 <        volatile boolean done = false;
762 >    public static class TrackedShortRunnable implements Runnable {
763 >        public volatile boolean done = false;
764          public void run() {
765              try {
766                  Thread.sleep(SMALL_DELAY_MS);
767                  done = true;
768 <            } catch(Exception e){
524 <            }
768 >            } catch (InterruptedException ok) {}
769          }
770      }
771  
772 <    static class TrackedMediumRunnable implements Runnable {
773 <        volatile boolean done = false;
772 >    public static class TrackedMediumRunnable implements Runnable {
773 >        public volatile boolean done = false;
774          public void run() {
775              try {
776                  Thread.sleep(MEDIUM_DELAY_MS);
777                  done = true;
778 <            } catch(Exception e){
535 <            }
778 >            } catch (InterruptedException ok) {}
779          }
780      }
781  
782 <    static class TrackedLongRunnable implements Runnable {
783 <        volatile boolean done = false;
782 >    public static class TrackedLongRunnable implements Runnable {
783 >        public volatile boolean done = false;
784          public void run() {
785              try {
786                  Thread.sleep(LONG_DELAY_MS);
787                  done = true;
788 <            } catch(Exception e){
546 <            }
788 >            } catch (InterruptedException ok) {}
789          }
790      }
791  
792 <    static class TrackedNoOpRunnable implements Runnable {
793 <        volatile boolean done = false;
792 >    public static class TrackedNoOpRunnable implements Runnable {
793 >        public volatile boolean done = false;
794          public void run() {
795              done = true;
796          }
797      }
798  
799 <    static class TrackedCallable implements Callable {
800 <        volatile boolean done = false;
799 >    public static class TrackedCallable implements Callable {
800 >        public volatile boolean done = false;
801          public Object call() {
802              try {
803                  Thread.sleep(SMALL_DELAY_MS);
804                  done = true;
805 <            } catch(Exception e){
564 <            }
805 >            } catch (InterruptedException ok) {}
806              return Boolean.TRUE;
807          }
808      }
809  
810 +    /**
811 +     * Analog of CheckedRunnable for RecursiveAction
812 +     */
813 +    public abstract class CheckedRecursiveAction extends RecursiveAction {
814 +        protected abstract void realCompute() throws Throwable;
815 +
816 +        public final void compute() {
817 +            try {
818 +                realCompute();
819 +            } catch (Throwable t) {
820 +                threadUnexpectedException(t);
821 +            }
822 +        }
823 +    }
824  
825      /**
826 <     * For use as RejectedExecutionHandler in constructors
826 >     * Analog of CheckedCallable for RecursiveTask
827       */
828 <    static class NoOpREHandler implements RejectedExecutionHandler{
829 <        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){}
828 >    public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> {
829 >        protected abstract T realCompute() throws Throwable;
830 >
831 >        public final T compute() {
832 >            try {
833 >                return realCompute();
834 >            } catch (Throwable t) {
835 >                threadUnexpectedException(t);
836 >                return null;
837 >            }
838 >        }
839      }
840  
841 +    /**
842 +     * For use as RejectedExecutionHandler in constructors
843 +     */
844 +    public static class NoOpREHandler implements RejectedExecutionHandler {
845 +        public void rejectedExecution(Runnable r,
846 +                                      ThreadPoolExecutor executor) {}
847 +    }
848  
849   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines