ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
Revision: 1.49
Committed: Tue Jan 5 02:08:37 2010 UTC (14 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.48: +72 -1 lines
Log Message:
Make tests security-manager-aware

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