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.7 by dl, Sat Oct 25 16:02:13 2003 UTC vs.
Revision 1.37 by dl, Tue Aug 4 00:23:18 2009 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   import java.security.*;
15  
# Line 16 | Line 18 | import java.security.*;
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 doe not otherwise arrange).  The rules for creating such
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
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
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
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
36   * them. These methods are used to clear and check for thread
37   * assertion failures.</li>
# Line 43 | Line 45 | import java.security.*;
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
# Line 62 | Line 64 | import java.security.*;
64   * "normal" behaviors differ significantly. And sometimes testcases
65   * cover multiple methods when they cannot be tested in
66   * isolation.</li>
67 < *
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
# Line 87 | Line 89 | import java.security.*;
89   public class JSR166TestCase extends TestCase {
90      /**
91       * Runs all JSR166 unit tests using junit.textui.TestRunner
92 <     */
92 >     */
93      public static void main (String[] args) {
94 <        junit.textui.TestRunner.run (suite());
94 >        int iters = 1;
95 >        if (args.length > 0)
96 >            iters = Integer.parseInt(args[0]);
97 >        Test s = suite();
98 >        for (int i = 0; i < iters; ++i) {
99 >            junit.textui.TestRunner.run (s);
100 >            System.gc();
101 >            System.runFinalization();
102 >        }
103 >        System.exit(0);
104      }
105  
106      /**
107       * Collects all JSR166 unit tests as one suite
108 <     */
108 >     */
109      public static Test suite ( ) {
110          TestSuite suite = new TestSuite("JSR166 Unit Tests");
111 <        
111 >
112 >        suite.addTest(new TestSuite(ForkJoinPoolTest.class));
113 >        suite.addTest(new TestSuite(ForkJoinTaskTest.class));
114 >        suite.addTest(new TestSuite(RecursiveActionTest.class));
115 >        suite.addTest(new TestSuite(RecursiveTaskTest.class));
116 >        suite.addTest(new TestSuite(LinkedTransferQueueTest.class));
117 >        suite.addTest(new TestSuite(PhaserTest.class));
118 >        suite.addTest(new TestSuite(ThreadLocalRandomTest.class));
119 >        suite.addTest(new TestSuite(AbstractExecutorServiceTest.class));
120 >        suite.addTest(new TestSuite(AbstractQueueTest.class));
121 >        suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class));
122 >        suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class));
123          suite.addTest(new TestSuite(ArrayBlockingQueueTest.class));
124 <        suite.addTest(new TestSuite(AtomicBooleanTest.class));
125 <        suite.addTest(new TestSuite(AtomicIntegerArrayTest.class));
126 <        suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class));
127 <        suite.addTest(new TestSuite(AtomicIntegerTest.class));
128 <        suite.addTest(new TestSuite(AtomicLongArrayTest.class));
129 <        suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class));
130 <        suite.addTest(new TestSuite(AtomicLongTest.class));
131 <        suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class));
132 <        suite.addTest(new TestSuite(AtomicReferenceArrayTest.class));
133 <        suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class));
134 <        suite.addTest(new TestSuite(AtomicReferenceTest.class));
135 <        suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
136 <        suite.addTest(new TestSuite(CancellableTaskTest.class));
124 >        suite.addTest(new TestSuite(ArrayDequeTest.class));
125 >        suite.addTest(new TestSuite(AtomicBooleanTest.class));
126 >        suite.addTest(new TestSuite(AtomicIntegerArrayTest.class));
127 >        suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class));
128 >        suite.addTest(new TestSuite(AtomicIntegerTest.class));
129 >        suite.addTest(new TestSuite(AtomicLongArrayTest.class));
130 >        suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class));
131 >        suite.addTest(new TestSuite(AtomicLongTest.class));
132 >        suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class));
133 >        suite.addTest(new TestSuite(AtomicReferenceArrayTest.class));
134 >        suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class));
135 >        suite.addTest(new TestSuite(AtomicReferenceTest.class));
136 >        suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
137          suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
138          suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
139 +        suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class));
140 +        suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class));
141 +        suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class));
142 +        suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class));
143          suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class));
144          suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class));
145          suite.addTest(new TestSuite(CountDownLatchTest.class));
146          suite.addTest(new TestSuite(CyclicBarrierTest.class));
147          suite.addTest(new TestSuite(DelayQueueTest.class));
148 +        suite.addTest(new TestSuite(EntryTest.class));
149          suite.addTest(new TestSuite(ExchangerTest.class));
150          suite.addTest(new TestSuite(ExecutorsTest.class));
151 <        suite.addTest(new TestSuite(FairSemaphoreTest.class));
151 >        suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class));
152          suite.addTest(new TestSuite(FutureTaskTest.class));
153 +        suite.addTest(new TestSuite(LinkedBlockingDequeTest.class));
154          suite.addTest(new TestSuite(LinkedBlockingQueueTest.class));
155          suite.addTest(new TestSuite(LinkedListTest.class));
156          suite.addTest(new TestSuite(LockSupportTest.class));
157          suite.addTest(new TestSuite(PriorityBlockingQueueTest.class));
158          suite.addTest(new TestSuite(PriorityQueueTest.class));
131        suite.addTest(new TestSuite(PrivilegedFutureTaskTest.class));
159          suite.addTest(new TestSuite(ReentrantLockTest.class));
160          suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class));
161          suite.addTest(new TestSuite(ScheduledExecutorTest.class));
162 +        suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class));
163          suite.addTest(new TestSuite(SemaphoreTest.class));
164          suite.addTest(new TestSuite(SynchronousQueueTest.class));
165          suite.addTest(new TestSuite(SystemTest.class));
166          suite.addTest(new TestSuite(ThreadLocalTest.class));
167          suite.addTest(new TestSuite(ThreadPoolExecutorTest.class));
168 +        suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class));
169          suite.addTest(new TestSuite(ThreadTest.class));
170          suite.addTest(new TestSuite(TimeUnitTest.class));
171 <                
171 >        suite.addTest(new TestSuite(TreeMapTest.class));
172 >        suite.addTest(new TestSuite(TreeSetTest.class));
173 >        suite.addTest(new TestSuite(TreeSubMapTest.class));
174 >        suite.addTest(new TestSuite(TreeSubSetTest.class));
175 >
176          return suite;
177      }
178  
# Line 151 | Line 184 | public class JSR166TestCase extends Test
184  
185  
186      /**
187 <     * Return the shortest timed delay. This could
188 <     * be reimplmented to use for example a Property.
189 <     */
187 >     * Returns the shortest timed delay. This could
188 >     * be reimplemented to use for example a Property.
189 >     */
190      protected long getShortDelay() {
191 <        return 100;
191 >        return 50;
192      }
193  
194  
195      /**
196 <     * Set delays as multiples of SHORT_DELAY.
196 >     * Sets delays as multiples of SHORT_DELAY.
197       */
198      protected  void setDelays() {
199          SHORT_DELAY_MS = getShortDelay();
# Line 175 | Line 208 | public class JSR166TestCase extends Test
208      volatile boolean threadFailed;
209  
210      /**
211 <     * Initialize test to indicate that no thread assertions have failed
211 >     * Initializes test to indicate that no thread assertions have failed
212       */
213 <    public void setUp() {
213 >    public void setUp() {
214          setDelays();
215 <        threadFailed = false;  
215 >        threadFailed = false;
216      }
217  
218      /**
219 <     * Trigger test case failure if any thread assertions have failed
219 >     * Triggers test case failure if any thread assertions have failed
220       */
221 <    public void tearDown() {
222 <        assertFalse(threadFailed);  
221 >    public void tearDown() {
222 >        assertFalse(threadFailed);
223      }
224  
225      /**
226       * Fail, also setting status to indicate current testcase should fail
227 <     */
227 >     */
228      public void threadFail(String reason) {
229          threadFailed = true;
230          fail(reason);
# Line 200 | Line 233 | public class JSR166TestCase extends Test
233      /**
234       * If expression not true, set status to indicate current testcase
235       * should fail
236 <     */
236 >     */
237      public void threadAssertTrue(boolean b) {
238          if (!b) {
239              threadFailed = true;
# Line 211 | Line 244 | public class JSR166TestCase extends Test
244      /**
245       * If expression not false, set status to indicate current testcase
246       * should fail
247 <     */
247 >     */
248      public void threadAssertFalse(boolean b) {
249          if (b) {
250              threadFailed = true;
# Line 222 | Line 255 | public class JSR166TestCase extends Test
255      /**
256       * If argument not null, set status to indicate current testcase
257       * should fail
258 <     */
258 >     */
259      public void threadAssertNull(Object x) {
260          if (x != null) {
261              threadFailed = true;
# Line 233 | Line 266 | public class JSR166TestCase extends Test
266      /**
267       * If arguments not equal, set status to indicate current testcase
268       * should fail
269 <     */
269 >     */
270      public void threadAssertEquals(long x, long y) {
271          if (x != y) {
272              threadFailed = true;
# Line 244 | Line 277 | public class JSR166TestCase extends Test
277      /**
278       * If arguments not equal, set status to indicate current testcase
279       * should fail
280 <     */
280 >     */
281      public void threadAssertEquals(Object x, Object y) {
282          if (x != y && (x == null || !x.equals(y))) {
283              threadFailed = true;
# Line 254 | Line 287 | public class JSR166TestCase extends Test
287  
288      /**
289       * threadFail with message "should throw exception"
290 <     */
290 >     */
291      public void threadShouldThrow() {
292          threadFailed = true;
293          fail("should throw exception");
# Line 268 | Line 301 | public class JSR166TestCase extends Test
301          fail("Unexpected exception");
302      }
303  
304 +    /**
305 +     * threadFail with message "Unexpected exception", with argument
306 +     */
307 +    public void threadUnexpectedException(Throwable ex) {
308 +        threadFailed = true;
309 +        ex.printStackTrace();
310 +        fail("Unexpected exception: " + ex);
311 +    }
312  
313      /**
314       * Wait out termination of a thread pool or fail doing so
# Line 275 | Line 316 | public class JSR166TestCase extends Test
316      public void joinPool(ExecutorService exec) {
317          try {
318              exec.shutdown();
319 <            assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
320 <        } catch(InterruptedException ie) {
319 >            assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
320 >        } catch (SecurityException ok) {
321 >            // Allowed in case test doesn't have privs
322 >        } catch (InterruptedException ie) {
323              fail("Unexpected exception");
324          }
325      }
# Line 284 | Line 327 | public class JSR166TestCase extends Test
327  
328      /**
329       * fail with message "should throw exception"
330 <     */
330 >     */
331      public void shouldThrow() {
332          fail("Should throw exception");
333      }
# Line 296 | Line 339 | public class JSR166TestCase extends Test
339          fail("Unexpected exception");
340      }
341  
342 +    /**
343 +     * fail with message "Unexpected exception", with argument
344 +     */
345 +    public void unexpectedException(Throwable ex) {
346 +        ex.printStackTrace();
347 +        fail("Unexpected exception: " + ex);
348 +    }
349 +
350  
351      /**
352       * The number of elements to place in collections, arrays, etc.
# Line 319 | Line 370 | public class JSR166TestCase extends Test
370      static final Integer m3  = new Integer(-3);
371      static final Integer m4 = new Integer(-4);
372      static final Integer m5 = new Integer(-5);
373 +    static final Integer m6 = new Integer(-6);
374      static final Integer m10 = new Integer(-10);
375  
376  
# Line 346 | Line 398 | public class JSR166TestCase extends Test
398  
399      // Some convenient Runnable classes
400  
401 <    static class NoOpRunnable implements Runnable {
402 <        public void run() {}
351 <    }
401 >    abstract class CheckedRunnable implements Runnable {
402 >        abstract void realRun() throws Throwable;
403  
404 <    static class NoOpCallable implements Callable {
354 <        public Object call() { return Boolean.TRUE; }
355 <    }
356 <
357 <    class ShortRunnable implements Runnable {
358 <        public void run() {
404 >        public final void run() {
405              try {
406 <                Thread.sleep(SHORT_DELAY_MS);
407 <            }
408 <            catch(Exception e) {
363 <                threadUnexpectedException();
406 >                realRun();
407 >            } catch (Throwable t) {
408 >                threadUnexpectedException(t);
409              }
410          }
411      }
412  
413 <    class ShortInterruptedRunnable implements Runnable {
414 <        public void run() {
413 >    abstract class CheckedInterruptedRunnable implements Runnable {
414 >        abstract void realRun() throws Throwable;
415 >
416 >        public final void run() {
417              try {
418 <                Thread.sleep(SHORT_DELAY_MS);
418 >                realRun();
419                  threadShouldThrow();
420 <            }
421 <            catch(InterruptedException success) {
420 >            } catch (InterruptedException success) {
421 >            } catch (Throwable t) {
422 >                threadUnexpectedException(t);
423              }
424          }
425      }
426  
427 <    class SmallRunnable implements Runnable {
428 <        public void run() {
427 >    abstract class CheckedCallable<T> implements Callable<T> {
428 >        abstract T realCall() throws Throwable;
429 >
430 >        public final T call() {
431              try {
432 <                Thread.sleep(SMALL_DELAY_MS);
433 <            }
434 <            catch(Exception e) {
435 <                threadUnexpectedException();
432 >                return realCall();
433 >            } catch (Throwable t) {
434 >                threadUnexpectedException(t);
435 >                return null;
436              }
437          }
438      }
439  
440 <    class SmallPossiblyInterruptedRunnable implements Runnable {
441 <        public void run() {
442 <            try {
443 <                Thread.sleep(SMALL_DELAY_MS);
444 <            }
445 <            catch(Exception e) {
446 <            }
440 >    static class NoOpRunnable implements Runnable {
441 >        public void run() {}
442 >    }
443 >
444 >    static class NoOpCallable implements Callable {
445 >        public Object call() { return Boolean.TRUE; }
446 >    }
447 >
448 >    static final String TEST_STRING = "a test string";
449 >
450 >    static class StringTask implements Callable<String> {
451 >        public String call() { return TEST_STRING; }
452 >    }
453 >
454 >    static class NPETask implements Callable<String> {
455 >        public String call() { throw new NullPointerException(); }
456 >    }
457 >
458 >    static class CallableOne implements Callable<Integer> {
459 >        public Integer call() { return one; }
460 >    }
461 >
462 >    class ShortRunnable extends CheckedRunnable {
463 >        void realRun() throws Throwable {
464 >            Thread.sleep(SHORT_DELAY_MS);
465          }
466      }
467  
468 <    class SmallCallable implements Callable {
469 <        public Object call() {
468 >    class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
469 >        void realRun() throws InterruptedException {
470 >            Thread.sleep(SHORT_DELAY_MS);
471 >        }
472 >    }
473 >
474 >    class SmallRunnable extends CheckedRunnable {
475 >        void realRun() throws Throwable {
476 >            Thread.sleep(SMALL_DELAY_MS);
477 >        }
478 >    }
479 >
480 >    class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
481 >        void realRun() {
482              try {
483                  Thread.sleep(SMALL_DELAY_MS);
484              }
485 <            catch(Exception e) {
406 <                threadUnexpectedException();
485 >            catch (InterruptedException ok) {
486              }
487 +        }
488 +    }
489 +
490 +    class SmallCallable extends CheckedCallable {
491 +        Object realCall() throws Throwable {
492 +            Thread.sleep(SMALL_DELAY_MS);
493              return Boolean.TRUE;
494          }
495      }
496  
497 <    class SmallInterruptedRunnable implements Runnable {
498 <        public void run() {
499 <            try {
415 <                Thread.sleep(SMALL_DELAY_MS);
416 <                threadShouldThrow();
417 <            }
418 <            catch(InterruptedException success) {
419 <            }
497 >    class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
498 >        void realRun() throws InterruptedException {
499 >            Thread.sleep(SMALL_DELAY_MS);
500          }
501      }
502  
503 +    class MediumRunnable extends CheckedRunnable {
504 +        void realRun() throws Throwable {
505 +            Thread.sleep(MEDIUM_DELAY_MS);
506 +        }
507 +    }
508  
509 <    class MediumRunnable implements Runnable {
510 <        public void run() {
511 <            try {
427 <                Thread.sleep(MEDIUM_DELAY_MS);
428 <            }
429 <            catch(Exception e) {
430 <                threadUnexpectedException();
431 <            }
509 >    class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
510 >        void realRun() throws InterruptedException {
511 >            Thread.sleep(MEDIUM_DELAY_MS);
512          }
513      }
514  
515 <    class MediumInterruptedRunnable implements Runnable {
516 <        public void run() {
515 >    class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
516 >        void realRun() {
517              try {
518                  Thread.sleep(MEDIUM_DELAY_MS);
439                threadShouldThrow();
519              }
520 <            catch(InterruptedException success) {
520 >            catch (InterruptedException ok) {
521              }
522          }
523      }
524  
525 <    class MediumPossiblyInterruptedRunnable implements Runnable {
526 <        public void run() {
525 >    class LongPossiblyInterruptedRunnable extends CheckedRunnable {
526 >        void realRun() {
527              try {
528 <                Thread.sleep(MEDIUM_DELAY_MS);
528 >                Thread.sleep(LONG_DELAY_MS);
529              }
530 <            catch(InterruptedException success) {
530 >            catch (InterruptedException ok) {
531              }
532          }
533      }
# Line 456 | Line 535 | public class JSR166TestCase extends Test
535      /**
536       * For use as ThreadFactory in constructors
537       */
538 <    static class SimpleThreadFactory implements ThreadFactory{
539 <        public Thread newThread(Runnable r){
538 >    static class SimpleThreadFactory implements ThreadFactory {
539 >        public Thread newThread(Runnable r) {
540              return new Thread(r);
541 <        }  
541 >        }
542      }
543  
544      static class TrackedShortRunnable implements Runnable {
# Line 468 | Line 547 | public class JSR166TestCase extends Test
547              try {
548                  Thread.sleep(SMALL_DELAY_MS);
549                  done = true;
550 <            } catch(Exception e){
550 >            } catch (Exception e) {
551              }
552          }
553      }
# Line 479 | Line 558 | public class JSR166TestCase extends Test
558              try {
559                  Thread.sleep(MEDIUM_DELAY_MS);
560                  done = true;
561 <            } catch(Exception e){
561 >            } catch (Exception e) {
562              }
563          }
564      }
# Line 490 | Line 569 | public class JSR166TestCase extends Test
569              try {
570                  Thread.sleep(LONG_DELAY_MS);
571                  done = true;
572 <            } catch(Exception e){
572 >            } catch (Exception e) {
573              }
574          }
575      }
# Line 508 | Line 587 | public class JSR166TestCase extends Test
587              try {
588                  Thread.sleep(SMALL_DELAY_MS);
589                  done = true;
590 <            } catch(Exception e){
590 >            } catch (Exception e) {
591              }
592              return Boolean.TRUE;
593          }
594      }
595  
596 +
597      /**
598       * For use as RejectedExecutionHandler in constructors
599       */
600 <    static class NoOpREHandler implements RejectedExecutionHandler{
601 <        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){}
600 >    static class NoOpREHandler implements RejectedExecutionHandler {
601 >        public void rejectedExecution(Runnable r,
602 >                                      ThreadPoolExecutor executor) {}
603      }
604 <
524 <    
604 >
605   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines