ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
Revision: 1.51
Committed: Wed Sep 1 06:41:55 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.50: +1 -1 lines
Log Message:
trailing whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.13 * 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 jsr166 1.27 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12 jsr166 1.36 import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 dl 1.1 import java.io.*;
14 dl 1.7 import java.security.*;
15 dl 1.1
16     /**
17 dl 1.5 * 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 jsr166 1.27 * JUnit does not otherwise arrange). The rules for creating such
22 dl 1.5 * tests are:
23 dl 1.1 *
24     * <ol>
25     *
26     * <li> All assertions in code running in generated threads must use
27 jsr166 1.27 * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
28 dl 1.18 * #threadAssertEquals}, or {@link #threadAssertNull}, (not
29 jsr166 1.50 * {@code fail}, {@code assertTrue}, etc.) It is OK (but not
30 dl 1.1 * 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 dl 1.18 * <li> If you override {@link #setUp} or {@link #tearDown}, make sure
35 jsr166 1.50 * to invoke {@code super.setUp} and {@code super.tearDown} within
36 dl 1.1 * them. These methods are used to clear and check for thread
37     * assertion failures.</li>
38     *
39 jsr166 1.51 * <li>All delays and timeouts must use one of the constants {@code
40 jsr166 1.50 * 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 dl 1.5 * discriminable from zero time, and always allows enough time for the
43     * small amounts of computation (creating a thread, calling a few
44 dl 1.1 * 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 dl 1.2 * but even so, if there is ever any doubt, they can all be increased
48 jsr166 1.27 * in one spot to rerun tests on slower platforms.</li>
49 dl 1.1 *
50     * <li> All threads generated must be joined inside each test case
51 jsr166 1.50 * 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 dl 1.1 * using Executors.</li>
54     *
55     * </ol>
56 dl 1.6 *
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 jsr166 1.27 *
68 dl 1.6 * <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 jsr166 1.50 * <li> As a convenience, the {@code main} of this class (JSR166TestCase)
85 dl 1.6 * runs all JSR166 unit tests.</li>
86     *
87     * </ul>
88 dl 1.1 */
89     public class JSR166TestCase extends TestCase {
90 jsr166 1.49 private static final boolean useSecurityManager =
91     Boolean.getBoolean("jsr166.useSecurityManager");
92    
93 dl 1.6 /**
94     * Runs all JSR166 unit tests using junit.textui.TestRunner
95 jsr166 1.27 */
96 jsr166 1.41 public static void main(String[] args) {
97 jsr166 1.49 if (useSecurityManager) {
98     System.err.println("Setting a permissive security manager");
99     Policy.setPolicy(permissivePolicy());
100     System.setSecurityManager(new SecurityManager());
101     }
102 dl 1.16 int iters = 1;
103 jsr166 1.27 if (args.length > 0)
104 dl 1.16 iters = Integer.parseInt(args[0]);
105     Test s = suite();
106 dl 1.22 for (int i = 0; i < iters; ++i) {
107 jsr166 1.41 junit.textui.TestRunner.run(s);
108 dl 1.22 System.gc();
109     System.runFinalization();
110     }
111     System.exit(0);
112 dl 1.6 }
113    
114     /**
115     * Collects all JSR166 unit tests as one suite
116 jsr166 1.27 */
117 jsr166 1.41 public static Test suite() {
118 dl 1.6 TestSuite suite = new TestSuite("JSR166 Unit Tests");
119 jsr166 1.27
120 dl 1.32 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 dl 1.10 suite.addTest(new TestSuite(AbstractExecutorServiceTest.class));
128 dl 1.19 suite.addTest(new TestSuite(AbstractQueueTest.class));
129 dl 1.14 suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class));
130 dl 1.25 suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class));
131 dl 1.6 suite.addTest(new TestSuite(ArrayBlockingQueueTest.class));
132 dl 1.23 suite.addTest(new TestSuite(ArrayDequeTest.class));
133 jsr166 1.27 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 dl 1.6 suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
146 jsr166 1.50 suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class));
147 dl 1.6 suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
148 dl 1.23 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 dl 1.6 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 dl 1.29 suite.addTest(new TestSuite(EntryTest.class));
158 dl 1.6 suite.addTest(new TestSuite(ExchangerTest.class));
159     suite.addTest(new TestSuite(ExecutorsTest.class));
160 dl 1.11 suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class));
161 dl 1.6 suite.addTest(new TestSuite(FutureTaskTest.class));
162 dl 1.23 suite.addTest(new TestSuite(LinkedBlockingDequeTest.class));
163 dl 1.6 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 dl 1.28 suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class));
172 dl 1.6 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 dl 1.28 suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class));
178 dl 1.6 suite.addTest(new TestSuite(ThreadTest.class));
179 dl 1.24 suite.addTest(new TestSuite(TimeUnitTest.class));
180 dl 1.23 suite.addTest(new TestSuite(TreeMapTest.class));
181     suite.addTest(new TestSuite(TreeSetTest.class));
182 dl 1.26 suite.addTest(new TestSuite(TreeSubMapTest.class));
183     suite.addTest(new TestSuite(TreeSubSetTest.class));
184 jsr166 1.27
185 dl 1.6 return suite;
186     }
187    
188 dl 1.1
189 dl 1.2 public static long SHORT_DELAY_MS;
190     public static long SMALL_DELAY_MS;
191     public static long MEDIUM_DELAY_MS;
192     public static long LONG_DELAY_MS;
193    
194    
195     /**
196 jsr166 1.27 * Returns the shortest timed delay. This could
197 dl 1.15 * be reimplemented to use for example a Property.
198 jsr166 1.27 */
199 dl 1.2 protected long getShortDelay() {
200 dl 1.21 return 50;
201 dl 1.2 }
202    
203    
204     /**
205 jsr166 1.27 * Sets delays as multiples of SHORT_DELAY.
206 dl 1.2 */
207 jsr166 1.43 protected void setDelays() {
208 dl 1.2 SHORT_DELAY_MS = getShortDelay();
209     SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
210     MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
211     LONG_DELAY_MS = SHORT_DELAY_MS * 50;
212     }
213    
214 dl 1.1 /**
215     * Flag set true if any threadAssert methods fail
216     */
217 dl 1.5 volatile boolean threadFailed;
218 dl 1.1
219     /**
220 jsr166 1.27 * Initializes test to indicate that no thread assertions have failed
221 dl 1.1 */
222 jsr166 1.27 public void setUp() {
223 dl 1.2 setDelays();
224 jsr166 1.27 threadFailed = false;
225 dl 1.1 }
226    
227     /**
228 jsr166 1.27 * Triggers test case failure if any thread assertions have failed
229 dl 1.1 */
230 jsr166 1.27 public void tearDown() {
231     assertFalse(threadFailed);
232 dl 1.1 }
233    
234 dl 1.5 /**
235     * Fail, also setting status to indicate current testcase should fail
236 jsr166 1.27 */
237 dl 1.1 public void threadFail(String reason) {
238     threadFailed = true;
239     fail(reason);
240     }
241    
242 dl 1.5 /**
243     * If expression not true, set status to indicate current testcase
244     * should fail
245 jsr166 1.27 */
246 dl 1.1 public void threadAssertTrue(boolean b) {
247     if (!b) {
248     threadFailed = true;
249     assertTrue(b);
250     }
251     }
252 dl 1.5
253     /**
254     * If expression not false, set status to indicate current testcase
255     * should fail
256 jsr166 1.27 */
257 dl 1.1 public void threadAssertFalse(boolean b) {
258     if (b) {
259     threadFailed = true;
260     assertFalse(b);
261     }
262     }
263 dl 1.5
264     /**
265     * If argument not null, set status to indicate current testcase
266     * should fail
267 jsr166 1.27 */
268 dl 1.1 public void threadAssertNull(Object x) {
269     if (x != null) {
270     threadFailed = true;
271     assertNull(x);
272     }
273     }
274 dl 1.5
275     /**
276     * If arguments not equal, set status to indicate current testcase
277     * should fail
278 jsr166 1.27 */
279 dl 1.1 public void threadAssertEquals(long x, long y) {
280     if (x != y) {
281     threadFailed = true;
282     assertEquals(x, y);
283     }
284     }
285 dl 1.5
286     /**
287     * If arguments not equal, set status to indicate current testcase
288     * should fail
289 jsr166 1.27 */
290 dl 1.1 public void threadAssertEquals(Object x, Object y) {
291     if (x != y && (x == null || !x.equals(y))) {
292     threadFailed = true;
293     assertEquals(x, y);
294     }
295     }
296    
297 dl 1.5 /**
298     * threadFail with message "should throw exception"
299 jsr166 1.33 */
300 dl 1.3 public void threadShouldThrow() {
301 dl 1.37 threadFailed = true;
302     fail("should throw exception");
303 dl 1.3 }
304    
305 dl 1.5 /**
306 jsr166 1.40 * threadFail with message "should throw" + exceptionName
307     */
308     public void threadShouldThrow(String exceptionName) {
309     threadFailed = true;
310     fail("should throw " + exceptionName);
311     }
312    
313     /**
314 dl 1.5 * threadFail with message "Unexpected exception"
315     */
316 dl 1.3 public void threadUnexpectedException() {
317     threadFailed = true;
318     fail("Unexpected exception");
319     }
320    
321 dl 1.31 /**
322     * threadFail with message "Unexpected exception", with argument
323     */
324     public void threadUnexpectedException(Throwable ex) {
325     threadFailed = true;
326     ex.printStackTrace();
327     fail("Unexpected exception: " + ex);
328     }
329 dl 1.3
330 dl 1.1 /**
331     * Wait out termination of a thread pool or fail doing so
332     */
333     public void joinPool(ExecutorService exec) {
334     try {
335     exec.shutdown();
336 jsr166 1.36 assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
337 jsr166 1.33 } catch (SecurityException ok) {
338 dl 1.22 // Allowed in case test doesn't have privs
339 jsr166 1.33 } catch (InterruptedException ie) {
340 jsr166 1.44 fail("Unexpected InterruptedException");
341 dl 1.1 }
342     }
343    
344 dl 1.5
345     /**
346     * fail with message "should throw exception"
347 jsr166 1.27 */
348 dl 1.3 public void shouldThrow() {
349     fail("Should throw exception");
350     }
351    
352 dl 1.5 /**
353 jsr166 1.40 * fail with message "should throw " + exceptionName
354     */
355     public void shouldThrow(String exceptionName) {
356     fail("Should throw " + exceptionName);
357     }
358    
359     /**
360 dl 1.5 * fail with message "Unexpected exception"
361     */
362 dl 1.3 public void unexpectedException() {
363     fail("Unexpected exception");
364     }
365 dl 1.1
366 jsr166 1.36 /**
367     * fail with message "Unexpected exception", with argument
368     */
369     public void unexpectedException(Throwable ex) {
370     ex.printStackTrace();
371     fail("Unexpected exception: " + ex);
372     }
373    
374 dl 1.1
375     /**
376     * The number of elements to place in collections, arrays, etc.
377     */
378 jsr166 1.45 public static final int SIZE = 20;
379 dl 1.1
380     // Some convenient Integer constants
381    
382 jsr166 1.47 public static final Integer zero = new Integer(0);
383     public static final Integer one = new Integer(1);
384     public static final Integer two = new Integer(2);
385     public static final Integer three = new Integer(3);
386 jsr166 1.45 public static final Integer four = new Integer(4);
387     public static final Integer five = new Integer(5);
388 jsr166 1.47 public static final Integer six = new Integer(6);
389 jsr166 1.45 public static final Integer seven = new Integer(7);
390     public static final Integer eight = new Integer(8);
391 jsr166 1.47 public static final Integer nine = new Integer(9);
392 jsr166 1.45 public static final Integer m1 = new Integer(-1);
393     public static final Integer m2 = new Integer(-2);
394     public static final Integer m3 = new Integer(-3);
395 jsr166 1.47 public static final Integer m4 = new Integer(-4);
396     public static final Integer m5 = new Integer(-5);
397     public static final Integer m6 = new Integer(-6);
398 jsr166 1.45 public static final Integer m10 = new Integer(-10);
399 dl 1.7
400    
401     /**
402 jsr166 1.49 * Runs Runnable r with a security policy that permits precisely
403     * the specified permissions. If there is no current security
404     * manager, the runnable is run twice, both with and without a
405     * security manager. We require that any security manager permit
406     * getPolicy/setPolicy.
407     */
408     public void runWithPermissions(Runnable r, Permission... permissions) {
409     SecurityManager sm = System.getSecurityManager();
410     if (sm == null) {
411     r.run();
412     Policy savedPolicy = Policy.getPolicy();
413     try {
414     Policy.setPolicy(permissivePolicy());
415     System.setSecurityManager(new SecurityManager());
416     runWithPermissions(r, permissions);
417     } finally {
418     System.setSecurityManager(null);
419     Policy.setPolicy(savedPolicy);
420     }
421     } else {
422     Policy savedPolicy = Policy.getPolicy();
423     AdjustablePolicy policy = new AdjustablePolicy(permissions);
424     Policy.setPolicy(policy);
425    
426     try {
427     r.run();
428     } finally {
429     policy.addPermission(new SecurityPermission("setPolicy"));
430     Policy.setPolicy(savedPolicy);
431     }
432     }
433     }
434    
435     /**
436     * Runs a runnable without any permissions.
437     */
438     public void runWithoutPermissions(Runnable r) {
439     runWithPermissions(r);
440     }
441    
442     /**
443 dl 1.7 * A security policy where new permissions can be dynamically added
444     * or all cleared.
445     */
446 jsr166 1.45 public static class AdjustablePolicy extends java.security.Policy {
447 dl 1.7 Permissions perms = new Permissions();
448 jsr166 1.49 AdjustablePolicy(Permission... permissions) {
449     for (Permission permission : permissions)
450     perms.add(permission);
451     }
452 dl 1.7 void addPermission(Permission perm) { perms.add(perm); }
453     void clearPermissions() { perms = new Permissions(); }
454 jsr166 1.42 public PermissionCollection getPermissions(CodeSource cs) {
455     return perms;
456     }
457     public PermissionCollection getPermissions(ProtectionDomain pd) {
458     return perms;
459     }
460     public boolean implies(ProtectionDomain pd, Permission p) {
461     return perms.implies(p);
462     }
463     public void refresh() {}
464 dl 1.7 }
465 dl 1.1
466 jsr166 1.38 /**
467 jsr166 1.49 * Returns a policy containing all the permissions we ever need.
468     */
469     public static Policy permissivePolicy() {
470     return new AdjustablePolicy
471     // Permissions j.u.c. needs directly
472     (new RuntimePermission("modifyThread"),
473     new RuntimePermission("getClassLoader"),
474     new RuntimePermission("setContextClassLoader"),
475     // Permissions needed to change permissions!
476     new SecurityPermission("getPolicy"),
477     new SecurityPermission("setPolicy"),
478     new RuntimePermission("setSecurityManager"),
479     // Permissions needed by the junit test harness
480     new RuntimePermission("accessDeclaredMembers"),
481     new PropertyPermission("*", "read"),
482     new java.io.FilePermission("<<ALL FILES>>", "read"));
483     }
484    
485     /**
486 jsr166 1.38 * Sleep until the timeout has elapsed, or interrupted.
487     * Does <em>NOT</em> throw InterruptedException.
488     */
489     void sleepTillInterrupted(long timeoutMillis) {
490     try {
491     Thread.sleep(timeoutMillis);
492 jsr166 1.44 } catch (InterruptedException wakeup) {}
493 jsr166 1.38 }
494    
495     /**
496     * Returns a new started Thread running the given runnable.
497     */
498     Thread newStartedThread(Runnable runnable) {
499     Thread t = new Thread(runnable);
500     t.start();
501     return t;
502     }
503 dl 1.1
504     // Some convenient Runnable classes
505    
506 jsr166 1.45 public abstract class CheckedRunnable implements Runnable {
507     protected abstract void realRun() throws Throwable;
508 jsr166 1.35
509     public final void run() {
510     try {
511     realRun();
512     } catch (Throwable t) {
513     threadUnexpectedException(t);
514     }
515     }
516     }
517    
518 jsr166 1.45 public abstract class RunnableShouldThrow implements Runnable {
519     protected abstract void realRun() throws Throwable;
520 jsr166 1.40
521     final Class<?> exceptionClass;
522    
523     <T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) {
524     this.exceptionClass = exceptionClass;
525     }
526    
527     public final void run() {
528     try {
529     realRun();
530     threadShouldThrow(exceptionClass.getSimpleName());
531     } catch (Throwable t) {
532     if (! exceptionClass.isInstance(t))
533     threadUnexpectedException(t);
534     }
535     }
536     }
537    
538 jsr166 1.45 public abstract class ThreadShouldThrow extends Thread {
539     protected abstract void realRun() throws Throwable;
540 jsr166 1.40
541     final Class<?> exceptionClass;
542    
543     <T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) {
544     this.exceptionClass = exceptionClass;
545     }
546    
547     public final void run() {
548     try {
549     realRun();
550     threadShouldThrow(exceptionClass.getSimpleName());
551     } catch (Throwable t) {
552     if (! exceptionClass.isInstance(t))
553     threadUnexpectedException(t);
554     }
555     }
556     }
557    
558 jsr166 1.45 public abstract class CheckedInterruptedRunnable implements Runnable {
559     protected abstract void realRun() throws Throwable;
560 jsr166 1.35
561     public final void run() {
562     try {
563     realRun();
564 jsr166 1.40 threadShouldThrow("InterruptedException");
565 jsr166 1.35 } catch (InterruptedException success) {
566     } catch (Throwable t) {
567     threadUnexpectedException(t);
568     }
569     }
570     }
571    
572 jsr166 1.45 public abstract class CheckedCallable<T> implements Callable<T> {
573     protected abstract T realCall() throws Throwable;
574 jsr166 1.35
575     public final T call() {
576     try {
577     return realCall();
578     } catch (Throwable t) {
579     threadUnexpectedException(t);
580     }
581 jsr166 1.40 return null;
582     }
583     }
584    
585 jsr166 1.45 public abstract class CheckedInterruptedCallable<T> implements Callable<T> {
586     protected abstract T realCall() throws Throwable;
587 jsr166 1.40
588     public final T call() {
589     try {
590     T result = realCall();
591     threadShouldThrow("InterruptedException");
592     return result;
593     } catch (InterruptedException success) {
594     } catch (Throwable t) {
595     threadUnexpectedException(t);
596     }
597     return null;
598 jsr166 1.35 }
599     }
600    
601 jsr166 1.45 public static class NoOpRunnable implements Runnable {
602 dl 1.1 public void run() {}
603     }
604    
605 jsr166 1.45 public static class NoOpCallable implements Callable {
606 dl 1.1 public Object call() { return Boolean.TRUE; }
607 dl 1.10 }
608    
609 jsr166 1.45 public static final String TEST_STRING = "a test string";
610 dl 1.10
611 jsr166 1.45 public static class StringTask implements Callable<String> {
612 dl 1.10 public String call() { return TEST_STRING; }
613     }
614    
615 jsr166 1.48 public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
616     return new CheckedCallable<String>() {
617     public String realCall() {
618     try {
619     latch.await();
620     } catch (InterruptedException quittingTime) {}
621     return TEST_STRING;
622     }};
623     }
624    
625 jsr166 1.45 public static class NPETask implements Callable<String> {
626 dl 1.10 public String call() { throw new NullPointerException(); }
627     }
628    
629 jsr166 1.45 public static class CallableOne implements Callable<Integer> {
630 dl 1.10 public Integer call() { return one; }
631 dl 1.1 }
632    
633 jsr166 1.45 public class ShortRunnable extends CheckedRunnable {
634     protected void realRun() throws Throwable {
635 jsr166 1.35 Thread.sleep(SHORT_DELAY_MS);
636 dl 1.1 }
637     }
638    
639 jsr166 1.45 public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
640     protected void realRun() throws InterruptedException {
641 jsr166 1.35 Thread.sleep(SHORT_DELAY_MS);
642 dl 1.1 }
643     }
644    
645 jsr166 1.45 public class SmallRunnable extends CheckedRunnable {
646     protected void realRun() throws Throwable {
647 jsr166 1.35 Thread.sleep(SMALL_DELAY_MS);
648 dl 1.1 }
649     }
650    
651 jsr166 1.45 public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
652     protected void realRun() {
653 dl 1.6 try {
654     Thread.sleep(SMALL_DELAY_MS);
655 jsr166 1.44 } catch (InterruptedException ok) {}
656 dl 1.6 }
657     }
658    
659 jsr166 1.45 public class SmallCallable extends CheckedCallable {
660     protected Object realCall() throws InterruptedException {
661 jsr166 1.35 Thread.sleep(SMALL_DELAY_MS);
662 dl 1.1 return Boolean.TRUE;
663     }
664     }
665    
666 jsr166 1.45 public class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
667     protected void realRun() throws InterruptedException {
668 jsr166 1.35 Thread.sleep(SMALL_DELAY_MS);
669 dl 1.1 }
670     }
671    
672 jsr166 1.45 public class MediumRunnable extends CheckedRunnable {
673     protected void realRun() throws Throwable {
674 jsr166 1.35 Thread.sleep(MEDIUM_DELAY_MS);
675 dl 1.1 }
676     }
677    
678 jsr166 1.45 public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
679     protected void realRun() throws InterruptedException {
680 jsr166 1.35 Thread.sleep(MEDIUM_DELAY_MS);
681 dl 1.1 }
682     }
683    
684 jsr166 1.45 public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
685     protected void realRun() {
686 dl 1.1 try {
687     Thread.sleep(MEDIUM_DELAY_MS);
688 jsr166 1.44 } catch (InterruptedException ok) {}
689 dl 1.1 }
690     }
691 dl 1.5
692 jsr166 1.45 public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
693     protected void realRun() {
694 dl 1.12 try {
695     Thread.sleep(LONG_DELAY_MS);
696 jsr166 1.44 } catch (InterruptedException ok) {}
697 dl 1.12 }
698     }
699    
700 dl 1.5 /**
701     * For use as ThreadFactory in constructors
702     */
703 jsr166 1.45 public static class SimpleThreadFactory implements ThreadFactory {
704 jsr166 1.33 public Thread newThread(Runnable r) {
705 dl 1.5 return new Thread(r);
706 jsr166 1.27 }
707 dl 1.5 }
708    
709 jsr166 1.45 public static class TrackedShortRunnable implements Runnable {
710     public volatile boolean done = false;
711 dl 1.5 public void run() {
712     try {
713     Thread.sleep(SMALL_DELAY_MS);
714     done = true;
715 jsr166 1.44 } catch (InterruptedException ok) {}
716 dl 1.6 }
717     }
718    
719 jsr166 1.45 public static class TrackedMediumRunnable implements Runnable {
720     public volatile boolean done = false;
721 dl 1.6 public void run() {
722     try {
723     Thread.sleep(MEDIUM_DELAY_MS);
724     done = true;
725 jsr166 1.44 } catch (InterruptedException ok) {}
726 dl 1.6 }
727     }
728    
729 jsr166 1.45 public static class TrackedLongRunnable implements Runnable {
730     public volatile boolean done = false;
731 dl 1.6 public void run() {
732     try {
733     Thread.sleep(LONG_DELAY_MS);
734     done = true;
735 jsr166 1.44 } catch (InterruptedException ok) {}
736 dl 1.6 }
737     }
738    
739 jsr166 1.45 public static class TrackedNoOpRunnable implements Runnable {
740     public volatile boolean done = false;
741 dl 1.6 public void run() {
742     done = true;
743 dl 1.5 }
744     }
745    
746 jsr166 1.45 public static class TrackedCallable implements Callable {
747     public volatile boolean done = false;
748 dl 1.5 public Object call() {
749     try {
750     Thread.sleep(SMALL_DELAY_MS);
751     done = true;
752 jsr166 1.44 } catch (InterruptedException ok) {}
753 dl 1.5 return Boolean.TRUE;
754     }
755     }
756 dl 1.14
757 dl 1.5
758     /**
759     * For use as RejectedExecutionHandler in constructors
760     */
761 jsr166 1.45 public static class NoOpREHandler implements RejectedExecutionHandler {
762 jsr166 1.35 public void rejectedExecution(Runnable r,
763     ThreadPoolExecutor executor) {}
764 dl 1.5 }
765 jsr166 1.27
766 dl 1.1 }