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.2 by dl, Sat Sep 20 00:31:57 2003 UTC vs.
Revision 1.52 by jsr166, Mon Sep 13 23:19:31 2010 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14 <
14 > import java.security.*;
15  
16   /**
17 < * Base class for JSR166 Junit TCK tests.  Defines some constants and
18 < * utility methods, as well as a simple framework for helping to make
19 < * sure that assertions failing in generated threads cause the
20 < * associated test that generated them to itself fail (which JUnit doe
21 < * not otherwise arrange).  The rules for creating such tests are:
17 > * Base class for JSR166 Junit TCK tests.  Defines some constants,
18 > * utility methods and classes, as well as a simple framework for
19 > * helping to make sure that assertions failing in generated threads
20 > * cause the associated test that generated them to itself fail (which
21 > * JUnit does not otherwise arrange).  The rules for creating such
22 > * tests are:
23   *
24   * <ol>
25   *
26   * <li> All assertions in code running in generated threads must use
27 < * the forms {@link threadFail} , {@link threadAssertTrue} {@link
28 < * threadAssertEquals}, or {@link threadAssertNull}, (not
29 < * <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not
27 > * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
28 > * #threadAssertEquals}, or {@link #threadAssertNull}, (not
29 > * {@code fail}, {@code assertTrue}, etc.) It is OK (but not
30   * particularly recommended) for other code to use these forms too.
31   * Only the most typically used JUnit assertion methods are defined
32   * this way, but enough to live with.</li>
33   *
34 < * <li> If you override {@link setUp} or {@link tearDown}, make sure
35 < * to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within
34 > * <li> If you override {@link #setUp} or {@link #tearDown}, make sure
35 > * to invoke {@code super.setUp} and {@code super.tearDown} within
36   * them. These methods are used to clear and check for thread
37   * assertion failures.</li>
38   *
39 < * <li>All delays and timeouts must use one of the constants {@link
40 < * SHORT_DELAY_MS}, {@link SMALL_DELAY_MS}, {@link MEDIUM_DELAY_MS},
41 < * {@link LONG_DELAY_MS}. The idea here is that a SHORT is always
42 < * discriminatable from zero time, and always allows enough time for
43 < * the small amounts of computation (creating a thread, calling a few
39 > * <li>All delays and timeouts must use one of the constants {@code
40 > * SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS},
41 > * {@code LONG_DELAY_MS}. The idea here is that a SHORT is always
42 > * discriminable from zero time, and always allows enough time for the
43 > * small amounts of computation (creating a thread, calling a few
44   * methods, etc) needed to reach a timeout point. Similarly, a SMALL
45   * is always discriminable as larger than SHORT and smaller than
46   * MEDIUM.  And so on. These constants are set to conservative values,
47   * but even so, if there is ever any doubt, they can all be increased
48 < * in one spot to rerun tests on slower platforms</li>
48 > * in one spot to rerun tests on slower platforms.</li>
49   *
50   * <li> All threads generated must be joined inside each test case
51 < * method (or <tt>fail</tt> to do so) before returning from the
52 < * method. The {@link joinPool} method can be used to do this when
51 > * method (or {@code fail} to do so) before returning from the
52 > * method. The {@code joinPool} method can be used to do this when
53   * using Executors.</li>
54   *
55   * </ol>
56 + *
57 + * <p> <b>Other notes</b>
58 + * <ul>
59 + *
60 + * <li> Usually, there is one testcase method per JSR166 method
61 + * covering "normal" operation, and then as many exception-testing
62 + * methods as there are exceptions the method can throw. Sometimes
63 + * there are multiple tests per JSR166 method when the different
64 + * "normal" behaviors differ significantly. And sometimes testcases
65 + * cover multiple methods when they cannot be tested in
66 + * isolation.</li>
67 + *
68 + * <li> The documentation style for testcases is to provide as javadoc
69 + * a simple sentence or two describing the property that the testcase
70 + * method purports to test. The javadocs do not say anything about how
71 + * the property is tested. To find out, read the code.</li>
72 + *
73 + * <li> These tests are "conformance tests", and do not attempt to
74 + * test throughput, latency, scalability or other performance factors
75 + * (see the separate "jtreg" tests for a set intended to check these
76 + * for the most central aspects of functionality.) So, most tests use
77 + * the smallest sensible numbers of threads, collection sizes, etc
78 + * needed to check basic conformance.</li>
79 + *
80 + * <li>The test classes currently do not declare inclusion in
81 + * any particular package to simplify things for people integrating
82 + * them in TCK test suites.</li>
83 + *
84 + * <li> As a convenience, the {@code main} of this class (JSR166TestCase)
85 + * runs all JSR166 unit tests.</li>
86 + *
87 + * </ul>
88   */
89   public class JSR166TestCase extends TestCase {
90 +    private static final boolean useSecurityManager =
91 +        Boolean.getBoolean("jsr166.useSecurityManager");
92 +
93 +    /**
94 +     * Runs all JSR166 unit tests using junit.textui.TestRunner
95 +     */
96 +    public static void main(String[] args) {
97 +        if (useSecurityManager) {
98 +            System.err.println("Setting a permissive security manager");
99 +            Policy.setPolicy(permissivePolicy());
100 +            System.setSecurityManager(new SecurityManager());
101 +        }
102 +        int iters = 1;
103 +        if (args.length > 0)
104 +            iters = Integer.parseInt(args[0]);
105 +        Test s = suite();
106 +        for (int i = 0; i < iters; ++i) {
107 +            junit.textui.TestRunner.run(s);
108 +            System.gc();
109 +            System.runFinalization();
110 +        }
111 +        System.exit(0);
112 +    }
113 +
114 +    /**
115 +     * Collects all JSR166 unit tests as one suite
116 +     */
117 +    public static Test suite() {
118 +        TestSuite suite = new TestSuite("JSR166 Unit Tests");
119 +
120 +        suite.addTest(new TestSuite(ForkJoinPoolTest.class));
121 +        suite.addTest(new TestSuite(ForkJoinTaskTest.class));
122 +        suite.addTest(new TestSuite(RecursiveActionTest.class));
123 +        suite.addTest(new TestSuite(RecursiveTaskTest.class));
124 +        suite.addTest(new TestSuite(LinkedTransferQueueTest.class));
125 +        suite.addTest(new TestSuite(PhaserTest.class));
126 +        suite.addTest(new TestSuite(ThreadLocalRandomTest.class));
127 +        suite.addTest(new TestSuite(AbstractExecutorServiceTest.class));
128 +        suite.addTest(new TestSuite(AbstractQueueTest.class));
129 +        suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class));
130 +        suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class));
131 +        suite.addTest(new TestSuite(ArrayBlockingQueueTest.class));
132 +        suite.addTest(new TestSuite(ArrayDequeTest.class));
133 +        suite.addTest(new TestSuite(AtomicBooleanTest.class));
134 +        suite.addTest(new TestSuite(AtomicIntegerArrayTest.class));
135 +        suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class));
136 +        suite.addTest(new TestSuite(AtomicIntegerTest.class));
137 +        suite.addTest(new TestSuite(AtomicLongArrayTest.class));
138 +        suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class));
139 +        suite.addTest(new TestSuite(AtomicLongTest.class));
140 +        suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class));
141 +        suite.addTest(new TestSuite(AtomicReferenceArrayTest.class));
142 +        suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class));
143 +        suite.addTest(new TestSuite(AtomicReferenceTest.class));
144 +        suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
145 +        suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
146 +        suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class));
147 +        suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
148 +        suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class));
149 +        suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class));
150 +        suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class));
151 +        suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class));
152 +        suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class));
153 +        suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class));
154 +        suite.addTest(new TestSuite(CountDownLatchTest.class));
155 +        suite.addTest(new TestSuite(CyclicBarrierTest.class));
156 +        suite.addTest(new TestSuite(DelayQueueTest.class));
157 +        suite.addTest(new TestSuite(EntryTest.class));
158 +        suite.addTest(new TestSuite(ExchangerTest.class));
159 +        suite.addTest(new TestSuite(ExecutorsTest.class));
160 +        suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class));
161 +        suite.addTest(new TestSuite(FutureTaskTest.class));
162 +        suite.addTest(new TestSuite(LinkedBlockingDequeTest.class));
163 +        suite.addTest(new TestSuite(LinkedBlockingQueueTest.class));
164 +        suite.addTest(new TestSuite(LinkedListTest.class));
165 +        suite.addTest(new TestSuite(LockSupportTest.class));
166 +        suite.addTest(new TestSuite(PriorityBlockingQueueTest.class));
167 +        suite.addTest(new TestSuite(PriorityQueueTest.class));
168 +        suite.addTest(new TestSuite(ReentrantLockTest.class));
169 +        suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class));
170 +        suite.addTest(new TestSuite(ScheduledExecutorTest.class));
171 +        suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class));
172 +        suite.addTest(new TestSuite(SemaphoreTest.class));
173 +        suite.addTest(new TestSuite(SynchronousQueueTest.class));
174 +        suite.addTest(new TestSuite(SystemTest.class));
175 +        suite.addTest(new TestSuite(ThreadLocalTest.class));
176 +        suite.addTest(new TestSuite(ThreadPoolExecutorTest.class));
177 +        suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class));
178 +        suite.addTest(new TestSuite(ThreadTest.class));
179 +        suite.addTest(new TestSuite(TimeUnitTest.class));
180 +        suite.addTest(new TestSuite(TreeMapTest.class));
181 +        suite.addTest(new TestSuite(TreeSetTest.class));
182 +        suite.addTest(new TestSuite(TreeSubMapTest.class));
183 +        suite.addTest(new TestSuite(TreeSubSetTest.class));
184 +
185 +        return suite;
186 +    }
187 +
188  
189      public static long SHORT_DELAY_MS;
190      public static long SMALL_DELAY_MS;
# Line 60 | Line 193 | public class JSR166TestCase extends Test
193  
194  
195      /**
196 <     * Return the shortest timed delay. This could
197 <     * be reimplmented to use for example a Property.
198 <     */
196 >     * Returns the shortest timed delay. This could
197 >     * be reimplemented to use for example a Property.
198 >     */
199      protected long getShortDelay() {
200          return 50;
201      }
202  
203  
204      /**
205 <     * Set delays as multiples fo SHORT_DELAY.
205 >     * Sets delays as multiples of SHORT_DELAY.
206       */
207 <    protected  void setDelays() {
207 >    protected void setDelays() {
208          SHORT_DELAY_MS = getShortDelay();
209          SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
210          MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
# Line 81 | Line 214 | public class JSR166TestCase extends Test
214      /**
215       * Flag set true if any threadAssert methods fail
216       */
217 <    protected volatile boolean threadFailed;
217 >    volatile boolean threadFailed;
218  
219      /**
220 <     * Initialize test to indicat that no thread assertions have failed
220 >     * Initializes test to indicate that no thread assertions have failed
221       */
222 <    public void setUp() {
222 >    public void setUp() {
223          setDelays();
224 <        threadFailed = false;  
224 >        threadFailed = false;
225      }
226  
227      /**
228 <     * Trigger test case failure if any thread assertions have failed
228 >     * Triggers test case failure if any thread assertions have failed
229       */
230 <    public void tearDown() {
231 <        assertFalse(threadFailed);  
230 >    public void tearDown() {
231 >        assertFalse(threadFailed);
232      }
233  
234 +    /**
235 +     * Fail, also setting status to indicate current testcase should fail
236 +     */
237      public void threadFail(String reason) {
238          threadFailed = true;
239          fail(reason);
240      }
241  
242 +    /**
243 +     * If expression not true, set status to indicate current testcase
244 +     * should fail
245 +     */
246      public void threadAssertTrue(boolean b) {
247          if (!b) {
248              threadFailed = true;
249              assertTrue(b);
250          }
251      }
252 +
253 +    /**
254 +     * If expression not false, set status to indicate current testcase
255 +     * should fail
256 +     */
257      public void threadAssertFalse(boolean b) {
258          if (b) {
259              threadFailed = true;
260              assertFalse(b);
261          }
262      }
263 +
264 +    /**
265 +     * If argument not null, set status to indicate current testcase
266 +     * should fail
267 +     */
268      public void threadAssertNull(Object x) {
269          if (x != null) {
270              threadFailed = true;
271              assertNull(x);
272          }
273      }
274 +
275 +    /**
276 +     * If arguments not equal, set status to indicate current testcase
277 +     * should fail
278 +     */
279      public void threadAssertEquals(long x, long y) {
280          if (x != y) {
281              threadFailed = true;
282              assertEquals(x, y);
283          }
284      }
285 +
286 +    /**
287 +     * If arguments not equal, set status to indicate current testcase
288 +     * should fail
289 +     */
290      public void threadAssertEquals(Object x, Object y) {
291          if (x != y && (x == null || !x.equals(y))) {
292              threadFailed = true;
# Line 135 | Line 295 | public class JSR166TestCase extends Test
295      }
296  
297      /**
298 +     * If arguments not identical, set status to indicate current testcase
299 +     * should fail
300 +     */
301 +    public void threadAssertSame(Object x, Object y) {
302 +        if (x != y) {
303 +            threadFailed = true;
304 +            assertSame(x, y);
305 +        }
306 +    }
307 +
308 +    /**
309 +     * threadFail with message "should throw exception"
310 +     */
311 +    public void threadShouldThrow() {
312 +        threadFailed = true;
313 +        fail("should throw exception");
314 +    }
315 +
316 +    /**
317 +     * threadFail with message "should throw" + exceptionName
318 +     */
319 +    public void threadShouldThrow(String exceptionName) {
320 +        threadFailed = true;
321 +        fail("should throw " + exceptionName);
322 +    }
323 +
324 +    /**
325 +     * threadFail with message "Unexpected exception"
326 +     */
327 +    public void threadUnexpectedException() {
328 +        threadFailed = true;
329 +        fail("Unexpected exception");
330 +    }
331 +
332 +    /**
333 +     * threadFail with message "Unexpected exception", with argument
334 +     */
335 +    public void threadUnexpectedException(Throwable ex) {
336 +        threadFailed = true;
337 +        ex.printStackTrace();
338 +        fail("Unexpected exception: " + ex);
339 +    }
340 +
341 +    /**
342       * Wait out termination of a thread pool or fail doing so
343       */
344      public void joinPool(ExecutorService exec) {
345          try {
346              exec.shutdown();
347 <            assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
348 <        } catch(InterruptedException ie) {
349 <            fail("unexpected exception");
347 >            assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
348 >        } catch (SecurityException ok) {
349 >            // Allowed in case test doesn't have privs
350 >        } catch (InterruptedException ie) {
351 >            fail("Unexpected InterruptedException");
352          }
353      }
354  
355  
356 +    /**
357 +     * fail with message "should throw exception"
358 +     */
359 +    public void shouldThrow() {
360 +        fail("Should throw exception");
361 +    }
362 +
363 +    /**
364 +     * fail with message "should throw " + exceptionName
365 +     */
366 +    public void shouldThrow(String exceptionName) {
367 +        fail("Should throw " + exceptionName);
368 +    }
369 +
370 +    /**
371 +     * fail with message "Unexpected exception"
372 +     */
373 +    public void unexpectedException() {
374 +        fail("Unexpected exception");
375 +    }
376 +
377 +    /**
378 +     * fail with message "Unexpected exception", with argument
379 +     */
380 +    public void unexpectedException(Throwable ex) {
381 +        ex.printStackTrace();
382 +        fail("Unexpected exception: " + ex);
383 +    }
384 +
385  
386      /**
387       * The number of elements to place in collections, arrays, etc.
# Line 155 | Line 390 | public class JSR166TestCase extends Test
390  
391      // Some convenient Integer constants
392  
393 <    public static final Integer zero = new Integer(0);
394 <    public static final Integer one = new Integer(1);
395 <    public static final Integer two = new Integer(2);
396 <    public static final Integer three  = new Integer(3);
393 >    public static final Integer zero  = new Integer(0);
394 >    public static final Integer one   = new Integer(1);
395 >    public static final Integer two   = new Integer(2);
396 >    public static final Integer three = new Integer(3);
397      public static final Integer four  = new Integer(4);
398      public static final Integer five  = new Integer(5);
399 <    public static final Integer six = new Integer(6);
399 >    public static final Integer six   = new Integer(6);
400      public static final Integer seven = new Integer(7);
401      public static final Integer eight = new Integer(8);
402 <    public static final Integer nine = new Integer(9);
402 >    public static final Integer nine  = new Integer(9);
403      public static final Integer m1  = new Integer(-1);
404      public static final Integer m2  = new Integer(-2);
405      public static final Integer m3  = new Integer(-3);
406 <    public static final Integer m4 = new Integer(-4);
407 <    public static final Integer m5 = new Integer(-5);
406 >    public static final Integer m4  = new Integer(-4);
407 >    public static final Integer m5  = new Integer(-5);
408 >    public static final Integer m6  = new Integer(-6);
409      public static final Integer m10 = new Integer(-10);
410  
411  
412 <    // Some convenient Runnable classes
412 >    /**
413 >     * Runs Runnable r with a security policy that permits precisely
414 >     * the specified permissions.  If there is no current security
415 >     * manager, the runnable is run twice, both with and without a
416 >     * security manager.  We require that any security manager permit
417 >     * getPolicy/setPolicy.
418 >     */
419 >    public void runWithPermissions(Runnable r, Permission... permissions) {
420 >        SecurityManager sm = System.getSecurityManager();
421 >        if (sm == null) {
422 >            r.run();
423 >            Policy savedPolicy = Policy.getPolicy();
424 >            try {
425 >                Policy.setPolicy(permissivePolicy());
426 >                System.setSecurityManager(new SecurityManager());
427 >                runWithPermissions(r, permissions);
428 >            } finally {
429 >                System.setSecurityManager(null);
430 >                Policy.setPolicy(savedPolicy);
431 >            }
432 >        } else {
433 >            Policy savedPolicy = Policy.getPolicy();
434 >            AdjustablePolicy policy = new AdjustablePolicy(permissions);
435 >            Policy.setPolicy(policy);
436  
437 <    public static class NoOpRunnable implements Runnable {
438 <        public void run() {}
437 >            try {
438 >                r.run();
439 >            } finally {
440 >                policy.addPermission(new SecurityPermission("setPolicy"));
441 >                Policy.setPolicy(savedPolicy);
442 >            }
443 >        }
444      }
445  
446 <    public static class NoOpCallable implements Callable {
447 <        public Object call() { return Boolean.TRUE; }
446 >    /**
447 >     * Runs a runnable without any permissions.
448 >     */
449 >    public void runWithoutPermissions(Runnable r) {
450 >        runWithPermissions(r);
451      }
452  
453 <    public class ShortRunnable implements Runnable {
454 <        public void run() {
453 >    /**
454 >     * A security policy where new permissions can be dynamically added
455 >     * or all cleared.
456 >     */
457 >    public static class AdjustablePolicy extends java.security.Policy {
458 >        Permissions perms = new Permissions();
459 >        AdjustablePolicy(Permission... permissions) {
460 >            for (Permission permission : permissions)
461 >                perms.add(permission);
462 >        }
463 >        void addPermission(Permission perm) { perms.add(perm); }
464 >        void clearPermissions() { perms = new Permissions(); }
465 >        public PermissionCollection getPermissions(CodeSource cs) {
466 >            return perms;
467 >        }
468 >        public PermissionCollection getPermissions(ProtectionDomain pd) {
469 >            return perms;
470 >        }
471 >        public boolean implies(ProtectionDomain pd, Permission p) {
472 >            return perms.implies(p);
473 >        }
474 >        public void refresh() {}
475 >    }
476 >
477 >    /**
478 >     * Returns a policy containing all the permissions we ever need.
479 >     */
480 >    public static Policy permissivePolicy() {
481 >        return new AdjustablePolicy
482 >            // Permissions j.u.c. needs directly
483 >            (new RuntimePermission("modifyThread"),
484 >             new RuntimePermission("getClassLoader"),
485 >             new RuntimePermission("setContextClassLoader"),
486 >             // Permissions needed to change permissions!
487 >             new SecurityPermission("getPolicy"),
488 >             new SecurityPermission("setPolicy"),
489 >             new RuntimePermission("setSecurityManager"),
490 >             // Permissions needed by the junit test harness
491 >             new RuntimePermission("accessDeclaredMembers"),
492 >             new PropertyPermission("*", "read"),
493 >             new java.io.FilePermission("<<ALL FILES>>", "read"));
494 >    }
495 >
496 >    /**
497 >     * Sleep until the timeout has elapsed, or interrupted.
498 >     * Does <em>NOT</em> throw InterruptedException.
499 >     */
500 >    void sleepTillInterrupted(long timeoutMillis) {
501 >        try {
502 >            Thread.sleep(timeoutMillis);
503 >        } catch (InterruptedException wakeup) {}
504 >    }
505 >
506 >    /**
507 >     * Returns a new started Thread running the given runnable.
508 >     */
509 >    Thread newStartedThread(Runnable runnable) {
510 >        Thread t = new Thread(runnable);
511 >        t.start();
512 >        return t;
513 >    }
514 >
515 >    // Some convenient Runnable classes
516 >
517 >    public abstract class CheckedRunnable implements Runnable {
518 >        protected abstract void realRun() throws Throwable;
519 >
520 >        public final void run() {
521              try {
522 <                Thread.sleep(SHORT_DELAY_MS);
522 >                realRun();
523 >            } catch (Throwable t) {
524 >                threadUnexpectedException(t);
525              }
526 <            catch(Exception e) {
527 <                threadFail("unexpectedException");
526 >        }
527 >    }
528 >
529 >    public abstract class RunnableShouldThrow implements Runnable {
530 >        protected abstract void realRun() throws Throwable;
531 >
532 >        final Class<?> exceptionClass;
533 >
534 >        <T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) {
535 >            this.exceptionClass = exceptionClass;
536 >        }
537 >
538 >        public final void run() {
539 >            try {
540 >                realRun();
541 >                threadShouldThrow(exceptionClass.getSimpleName());
542 >            } catch (Throwable t) {
543 >                if (! exceptionClass.isInstance(t))
544 >                    threadUnexpectedException(t);
545              }
546          }
547      }
548  
549 <    public class ShortInterruptedRunnable implements Runnable {
550 <        public void run() {
549 >    public abstract class ThreadShouldThrow extends Thread {
550 >        protected abstract void realRun() throws Throwable;
551 >
552 >        final Class<?> exceptionClass;
553 >
554 >        <T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) {
555 >            this.exceptionClass = exceptionClass;
556 >        }
557 >
558 >        public final void run() {
559              try {
560 <                Thread.sleep(SHORT_DELAY_MS);
561 <                threadFail("should throw IE");
560 >                realRun();
561 >                threadShouldThrow(exceptionClass.getSimpleName());
562 >            } catch (Throwable t) {
563 >                if (! exceptionClass.isInstance(t))
564 >                    threadUnexpectedException(t);
565              }
566 <            catch(InterruptedException success) {
566 >        }
567 >    }
568 >
569 >    public abstract class CheckedInterruptedRunnable implements Runnable {
570 >        protected abstract void realRun() throws Throwable;
571 >
572 >        public final void run() {
573 >            try {
574 >                realRun();
575 >                threadShouldThrow("InterruptedException");
576 >            } catch (InterruptedException success) {
577 >            } catch (Throwable t) {
578 >                threadUnexpectedException(t);
579              }
580          }
581      }
582  
583 <    public class SmallRunnable implements Runnable {
584 <        public void run() {
583 >    public abstract class CheckedCallable<T> implements Callable<T> {
584 >        protected abstract T realCall() throws Throwable;
585 >
586 >        public final T call() {
587              try {
588 <                Thread.sleep(SMALL_DELAY_MS);
588 >                return realCall();
589 >            } catch (Throwable t) {
590 >                threadUnexpectedException(t);
591              }
592 <            catch(Exception e) {
593 <                threadFail("unexpectedException");
592 >            return null;
593 >        }
594 >    }
595 >
596 >    public abstract class CheckedInterruptedCallable<T> implements Callable<T> {
597 >        protected abstract T realCall() throws Throwable;
598 >
599 >        public final T call() {
600 >            try {
601 >                T result = realCall();
602 >                threadShouldThrow("InterruptedException");
603 >                return result;
604 >            } catch (InterruptedException success) {
605 >            } catch (Throwable t) {
606 >                threadUnexpectedException(t);
607              }
608 +            return null;
609          }
610      }
611  
612 <    public class SmallCallable implements Callable {
613 <        public Object call() {
612 >    public static class NoOpRunnable implements Runnable {
613 >        public void run() {}
614 >    }
615 >
616 >    public static class NoOpCallable implements Callable {
617 >        public Object call() { return Boolean.TRUE; }
618 >    }
619 >
620 >    public static final String TEST_STRING = "a test string";
621 >
622 >    public static class StringTask implements Callable<String> {
623 >        public String call() { return TEST_STRING; }
624 >    }
625 >
626 >    public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
627 >        return new CheckedCallable<String>() {
628 >            public String realCall() {
629 >                try {
630 >                    latch.await();
631 >                } catch (InterruptedException quittingTime) {}
632 >                return TEST_STRING;
633 >            }};
634 >    }
635 >
636 >    public static class NPETask implements Callable<String> {
637 >        public String call() { throw new NullPointerException(); }
638 >    }
639 >
640 >    public static class CallableOne implements Callable<Integer> {
641 >        public Integer call() { return one; }
642 >    }
643 >
644 >    public class ShortRunnable extends CheckedRunnable {
645 >        protected void realRun() throws Throwable {
646 >            Thread.sleep(SHORT_DELAY_MS);
647 >        }
648 >    }
649 >
650 >    public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
651 >        protected void realRun() throws InterruptedException {
652 >            Thread.sleep(SHORT_DELAY_MS);
653 >        }
654 >    }
655 >
656 >    public class SmallRunnable extends CheckedRunnable {
657 >        protected void realRun() throws Throwable {
658 >            Thread.sleep(SMALL_DELAY_MS);
659 >        }
660 >    }
661 >
662 >    public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
663 >        protected void realRun() {
664              try {
665                  Thread.sleep(SMALL_DELAY_MS);
666 <            }
667 <            catch(Exception e) {
668 <                threadFail("unexpectedException");
669 <            }
666 >            } catch (InterruptedException ok) {}
667 >        }
668 >    }
669 >
670 >    public class SmallCallable extends CheckedCallable {
671 >        protected Object realCall() throws InterruptedException {
672 >            Thread.sleep(SMALL_DELAY_MS);
673              return Boolean.TRUE;
674          }
675      }
676  
677 <    public class SmallInterruptedRunnable implements Runnable {
677 >    public class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
678 >        protected void realRun() throws InterruptedException {
679 >            Thread.sleep(SMALL_DELAY_MS);
680 >        }
681 >    }
682 >
683 >    public class MediumRunnable extends CheckedRunnable {
684 >        protected void realRun() throws Throwable {
685 >            Thread.sleep(MEDIUM_DELAY_MS);
686 >        }
687 >    }
688 >
689 >    public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
690 >        protected void realRun() throws InterruptedException {
691 >            Thread.sleep(MEDIUM_DELAY_MS);
692 >        }
693 >    }
694 >
695 >    public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
696 >        protected void realRun() {
697 >            try {
698 >                Thread.sleep(MEDIUM_DELAY_MS);
699 >            } catch (InterruptedException ok) {}
700 >        }
701 >    }
702 >
703 >    public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
704 >        protected void realRun() {
705 >            try {
706 >                Thread.sleep(LONG_DELAY_MS);
707 >            } catch (InterruptedException ok) {}
708 >        }
709 >    }
710 >
711 >    /**
712 >     * For use as ThreadFactory in constructors
713 >     */
714 >    public static class SimpleThreadFactory implements ThreadFactory {
715 >        public Thread newThread(Runnable r) {
716 >            return new Thread(r);
717 >        }
718 >    }
719 >
720 >    public static class TrackedShortRunnable implements Runnable {
721 >        public volatile boolean done = false;
722          public void run() {
723              try {
724                  Thread.sleep(SMALL_DELAY_MS);
725 <                threadFail("should throw IE");
726 <            }
237 <            catch(InterruptedException success) {
238 <            }
725 >                done = true;
726 >            } catch (InterruptedException ok) {}
727          }
728      }
729  
730 <
731 <    public class MediumRunnable implements Runnable {
730 >    public static class TrackedMediumRunnable implements Runnable {
731 >        public volatile boolean done = false;
732          public void run() {
733              try {
734                  Thread.sleep(MEDIUM_DELAY_MS);
735 <            }
736 <            catch(Exception e) {
249 <                threadFail("unexpectedException");
250 <            }
735 >                done = true;
736 >            } catch (InterruptedException ok) {}
737          }
738      }
739  
740 <    public class MediumInterruptedRunnable implements Runnable {
740 >    public static class TrackedLongRunnable implements Runnable {
741 >        public volatile boolean done = false;
742          public void run() {
743              try {
744 <                Thread.sleep(MEDIUM_DELAY_MS);
745 <                threadFail("should throw IE");
746 <            }
260 <            catch(InterruptedException success) {
261 <            }
744 >                Thread.sleep(LONG_DELAY_MS);
745 >                done = true;
746 >            } catch (InterruptedException ok) {}
747          }
748      }
749  
750 <    public class MediumPossiblyInterruptedRunnable implements Runnable {
750 >    public static class TrackedNoOpRunnable implements Runnable {
751 >        public volatile boolean done = false;
752          public void run() {
753 +            done = true;
754 +        }
755 +    }
756 +
757 +    public static class TrackedCallable implements Callable {
758 +        public volatile boolean done = false;
759 +        public Object call() {
760              try {
761 <                Thread.sleep(MEDIUM_DELAY_MS);
762 <            }
763 <            catch(InterruptedException success) {
764 <            }
761 >                Thread.sleep(SMALL_DELAY_MS);
762 >                done = true;
763 >            } catch (InterruptedException ok) {}
764 >            return Boolean.TRUE;
765          }
766      }
767 <    
767 >
768 >
769 >    /**
770 >     * For use as RejectedExecutionHandler in constructors
771 >     */
772 >    public static class NoOpREHandler implements RejectedExecutionHandler {
773 >        public void rejectedExecution(Runnable r,
774 >                                      ThreadPoolExecutor executor) {}
775 >    }
776 >
777   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines