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.49 by jsr166, Tue Jan 5 02:08:37 2010 UTC vs.
Revision 1.56 by jsr166, Sun Oct 3 23:59:05 2010 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
10 < import java.util.*;
10 > import java.util.PropertyPermission;
11   import java.util.concurrent.*;
12 + import java.util.concurrent.atomic.AtomicReference;
13   import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 < import java.io.*;
15 < import java.security.*;
14 > import java.security.CodeSource;
15 > import java.security.Permission;
16 > import java.security.PermissionCollection;
17 > import java.security.Permissions;
18 > import java.security.Policy;
19 > import java.security.ProtectionDomain;
20 > import java.security.SecurityPermission;
21  
22   /**
23   * Base class for JSR166 Junit TCK tests.  Defines some constants,
# Line 26 | Line 32 | import java.security.*;
32   * <li> All assertions in code running in generated threads must use
33   * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
34   * #threadAssertEquals}, or {@link #threadAssertNull}, (not
35 < * <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not
35 > * {@code fail}, {@code assertTrue}, etc.) It is OK (but not
36   * particularly recommended) for other code to use these forms too.
37   * Only the most typically used JUnit assertion methods are defined
38   * this way, but enough to live with.</li>
39   *
40   * <li> If you override {@link #setUp} or {@link #tearDown}, make sure
41 < * to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within
41 > * to invoke {@code super.setUp} and {@code super.tearDown} within
42   * them. These methods are used to clear and check for thread
43   * assertion failures.</li>
44   *
45 < * <li>All delays and timeouts must use one of the constants <tt>
46 < * SHORT_DELAY_MS</tt>, <tt> SMALL_DELAY_MS</tt>, <tt> MEDIUM_DELAY_MS</tt>,
47 < * <tt> LONG_DELAY_MS</tt>. The idea here is that a SHORT is always
45 > * <li>All delays and timeouts must use one of the constants {@code
46 > * SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS},
47 > * {@code LONG_DELAY_MS}. The idea here is that a SHORT is always
48   * discriminable from zero time, and always allows enough time for the
49   * small amounts of computation (creating a thread, calling a few
50   * methods, etc) needed to reach a timeout point. Similarly, a SMALL
# Line 48 | Line 54 | import java.security.*;
54   * in one spot to rerun tests on slower platforms.</li>
55   *
56   * <li> All threads generated must be joined inside each test case
57 < * method (or <tt>fail</tt> to do so) before returning from the
58 < * method. The <tt> joinPool</tt> method can be used to do this when
57 > * method (or {@code fail} to do so) before returning from the
58 > * method. The {@code joinPool} method can be used to do this when
59   * using Executors.</li>
60   *
61   * </ol>
# Line 81 | Line 87 | import java.security.*;
87   * any particular package to simplify things for people integrating
88   * them in TCK test suites.</li>
89   *
90 < * <li> As a convenience, the <tt>main</tt> of this class (JSR166TestCase)
90 > * <li> As a convenience, the {@code main} of this class (JSR166TestCase)
91   * runs all JSR166 unit tests.</li>
92   *
93   * </ul>
# Line 99 | Line 105 | public class JSR166TestCase extends Test
105              Policy.setPolicy(permissivePolicy());
106              System.setSecurityManager(new SecurityManager());
107          }
108 <        int iters = 1;
109 <        if (args.length > 0)
104 <            iters = Integer.parseInt(args[0]);
108 >        int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]);
109 >
110          Test s = suite();
111          for (int i = 0; i < iters; ++i) {
112              junit.textui.TestRunner.run(s);
# Line 117 | Line 122 | public class JSR166TestCase extends Test
122      public static Test suite() {
123          TestSuite suite = new TestSuite("JSR166 Unit Tests");
124  
125 <        suite.addTest(new TestSuite(ForkJoinPoolTest.class));
126 <        suite.addTest(new TestSuite(ForkJoinTaskTest.class));
127 <        suite.addTest(new TestSuite(RecursiveActionTest.class));
128 <        suite.addTest(new TestSuite(RecursiveTaskTest.class));
129 <        suite.addTest(new TestSuite(LinkedTransferQueueTest.class));
130 <        suite.addTest(new TestSuite(PhaserTest.class));
131 <        suite.addTest(new TestSuite(ThreadLocalRandomTest.class));
132 <        suite.addTest(new TestSuite(AbstractExecutorServiceTest.class));
133 <        suite.addTest(new TestSuite(AbstractQueueTest.class));
134 <        suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class));
135 <        suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class));
136 <        suite.addTest(new TestSuite(ArrayBlockingQueueTest.class));
137 <        suite.addTest(new TestSuite(ArrayDequeTest.class));
138 <        suite.addTest(new TestSuite(AtomicBooleanTest.class));
139 <        suite.addTest(new TestSuite(AtomicIntegerArrayTest.class));
140 <        suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class));
141 <        suite.addTest(new TestSuite(AtomicIntegerTest.class));
142 <        suite.addTest(new TestSuite(AtomicLongArrayTest.class));
143 <        suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class));
144 <        suite.addTest(new TestSuite(AtomicLongTest.class));
145 <        suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class));
146 <        suite.addTest(new TestSuite(AtomicReferenceArrayTest.class));
147 <        suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class));
148 <        suite.addTest(new TestSuite(AtomicReferenceTest.class));
149 <        suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
150 <        suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
151 <        suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
152 <        suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class));
153 <        suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class));
154 <        suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class));
155 <        suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class));
156 <        suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class));
157 <        suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class));
158 <        suite.addTest(new TestSuite(CountDownLatchTest.class));
159 <        suite.addTest(new TestSuite(CyclicBarrierTest.class));
160 <        suite.addTest(new TestSuite(DelayQueueTest.class));
161 <        suite.addTest(new TestSuite(EntryTest.class));
162 <        suite.addTest(new TestSuite(ExchangerTest.class));
163 <        suite.addTest(new TestSuite(ExecutorsTest.class));
164 <        suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class));
165 <        suite.addTest(new TestSuite(FutureTaskTest.class));
166 <        suite.addTest(new TestSuite(LinkedBlockingDequeTest.class));
167 <        suite.addTest(new TestSuite(LinkedBlockingQueueTest.class));
168 <        suite.addTest(new TestSuite(LinkedListTest.class));
169 <        suite.addTest(new TestSuite(LockSupportTest.class));
170 <        suite.addTest(new TestSuite(PriorityBlockingQueueTest.class));
171 <        suite.addTest(new TestSuite(PriorityQueueTest.class));
172 <        suite.addTest(new TestSuite(ReentrantLockTest.class));
173 <        suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class));
174 <        suite.addTest(new TestSuite(ScheduledExecutorTest.class));
175 <        suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class));
176 <        suite.addTest(new TestSuite(SemaphoreTest.class));
177 <        suite.addTest(new TestSuite(SynchronousQueueTest.class));
178 <        suite.addTest(new TestSuite(SystemTest.class));
179 <        suite.addTest(new TestSuite(ThreadLocalTest.class));
180 <        suite.addTest(new TestSuite(ThreadPoolExecutorTest.class));
181 <        suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class));
182 <        suite.addTest(new TestSuite(ThreadTest.class));
183 <        suite.addTest(new TestSuite(TimeUnitTest.class));
184 <        suite.addTest(new TestSuite(TreeMapTest.class));
185 <        suite.addTest(new TestSuite(TreeSetTest.class));
186 <        suite.addTest(new TestSuite(TreeSubMapTest.class));
187 <        suite.addTest(new TestSuite(TreeSubSetTest.class));
125 >        suite.addTest(ForkJoinPoolTest.suite());
126 >        suite.addTest(ForkJoinTaskTest.suite());
127 >        suite.addTest(RecursiveActionTest.suite());
128 >        suite.addTest(RecursiveTaskTest.suite());
129 >        suite.addTest(LinkedTransferQueueTest.suite());
130 >        suite.addTest(PhaserTest.suite());
131 >        suite.addTest(ThreadLocalRandomTest.suite());
132 >        suite.addTest(AbstractExecutorServiceTest.suite());
133 >        suite.addTest(AbstractQueueTest.suite());
134 >        suite.addTest(AbstractQueuedSynchronizerTest.suite());
135 >        suite.addTest(AbstractQueuedLongSynchronizerTest.suite());
136 >        suite.addTest(ArrayBlockingQueueTest.suite());
137 >        suite.addTest(ArrayDequeTest.suite());
138 >        suite.addTest(AtomicBooleanTest.suite());
139 >        suite.addTest(AtomicIntegerArrayTest.suite());
140 >        suite.addTest(AtomicIntegerFieldUpdaterTest.suite());
141 >        suite.addTest(AtomicIntegerTest.suite());
142 >        suite.addTest(AtomicLongArrayTest.suite());
143 >        suite.addTest(AtomicLongFieldUpdaterTest.suite());
144 >        suite.addTest(AtomicLongTest.suite());
145 >        suite.addTest(AtomicMarkableReferenceTest.suite());
146 >        suite.addTest(AtomicReferenceArrayTest.suite());
147 >        suite.addTest(AtomicReferenceFieldUpdaterTest.suite());
148 >        suite.addTest(AtomicReferenceTest.suite());
149 >        suite.addTest(AtomicStampedReferenceTest.suite());
150 >        suite.addTest(ConcurrentHashMapTest.suite());
151 >        suite.addTest(ConcurrentLinkedDequeTest.suite());
152 >        suite.addTest(ConcurrentLinkedQueueTest.suite());
153 >        suite.addTest(ConcurrentSkipListMapTest.suite());
154 >        suite.addTest(ConcurrentSkipListSubMapTest.suite());
155 >        suite.addTest(ConcurrentSkipListSetTest.suite());
156 >        suite.addTest(ConcurrentSkipListSubSetTest.suite());
157 >        suite.addTest(CopyOnWriteArrayListTest.suite());
158 >        suite.addTest(CopyOnWriteArraySetTest.suite());
159 >        suite.addTest(CountDownLatchTest.suite());
160 >        suite.addTest(CyclicBarrierTest.suite());
161 >        suite.addTest(DelayQueueTest.suite());
162 >        suite.addTest(EntryTest.suite());
163 >        suite.addTest(ExchangerTest.suite());
164 >        suite.addTest(ExecutorsTest.suite());
165 >        suite.addTest(ExecutorCompletionServiceTest.suite());
166 >        suite.addTest(FutureTaskTest.suite());
167 >        suite.addTest(LinkedBlockingDequeTest.suite());
168 >        suite.addTest(LinkedBlockingQueueTest.suite());
169 >        suite.addTest(LinkedListTest.suite());
170 >        suite.addTest(LockSupportTest.suite());
171 >        suite.addTest(PriorityBlockingQueueTest.suite());
172 >        suite.addTest(PriorityQueueTest.suite());
173 >        suite.addTest(ReentrantLockTest.suite());
174 >        suite.addTest(ReentrantReadWriteLockTest.suite());
175 >        suite.addTest(ScheduledExecutorTest.suite());
176 >        suite.addTest(ScheduledExecutorSubclassTest.suite());
177 >        suite.addTest(SemaphoreTest.suite());
178 >        suite.addTest(SynchronousQueueTest.suite());
179 >        suite.addTest(SystemTest.suite());
180 >        suite.addTest(ThreadLocalTest.suite());
181 >        suite.addTest(ThreadPoolExecutorTest.suite());
182 >        suite.addTest(ThreadPoolExecutorSubclassTest.suite());
183 >        suite.addTest(ThreadTest.suite());
184 >        suite.addTest(TimeUnitTest.suite());
185 >        suite.addTest(TreeMapTest.suite());
186 >        suite.addTest(TreeSetTest.suite());
187 >        suite.addTest(TreeSubMapTest.suite());
188 >        suite.addTest(TreeSubSetTest.suite());
189  
190          return suite;
191      }
# Line 205 | Line 211 | public class JSR166TestCase extends Test
211       */
212      protected void setDelays() {
213          SHORT_DELAY_MS = getShortDelay();
214 <        SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
214 >        SMALL_DELAY_MS  = SHORT_DELAY_MS * 5;
215          MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
216 <        LONG_DELAY_MS = SHORT_DELAY_MS * 50;
216 >        LONG_DELAY_MS   = SHORT_DELAY_MS * 50;
217      }
218  
219      /**
220 <     * Flag set true if any threadAssert methods fail
220 >     * The first exception encountered if any threadAssertXXX method fails.
221       */
222 <    volatile boolean threadFailed;
222 >    private final AtomicReference<Throwable> threadFailure
223 >        = new AtomicReference<Throwable>(null);
224  
225      /**
226 <     * Initializes test to indicate that no thread assertions have failed
226 >     * Records an exception so that it can be rethrown later in the test
227 >     * harness thread, triggering a test case failure.  Only the first
228 >     * failure is recorded; subsequent calls to this method from within
229 >     * the same test have no effect.
230       */
231 +    public void threadRecordFailure(Throwable t) {
232 +        threadFailure.compareAndSet(null, t);
233 +    }
234 +
235      public void setUp() {
236          setDelays();
223        threadFailed = false;
237      }
238  
239      /**
240 <     * Triggers test case failure if any thread assertions have failed
241 <     */
242 <    public void tearDown() {
243 <        assertFalse(threadFailed);
240 >     * Triggers test case failure if any thread assertions have failed,
241 >     * by rethrowing, in the test harness thread, any exception recorded
242 >     * earlier by threadRecordFailure.
243 >     */
244 >    public void tearDown() throws Exception {
245 >        Throwable t = threadFailure.get();
246 >        if (t != null) {
247 >            if (t instanceof Error)
248 >                throw (Error) t;
249 >            else if (t instanceof RuntimeException)
250 >                throw (RuntimeException) t;
251 >            else if (t instanceof Exception)
252 >                throw (Exception) t;
253 >            else
254 >                throw new AssertionError(t);
255 >        }
256      }
257  
258      /**
259 <     * Fail, also setting status to indicate current testcase should fail
259 >     * Just like fail(reason), but additionally recording (using
260 >     * threadRecordFailure) any AssertionError thrown, so that the current
261 >     * testcase will fail.
262       */
263      public void threadFail(String reason) {
264 <        threadFailed = true;
265 <        fail(reason);
264 >        try {
265 >            fail(reason);
266 >        } catch (Throwable t) {
267 >            threadRecordFailure(t);
268 >            fail(reason);
269 >        }
270      }
271  
272      /**
273 <     * If expression not true, set status to indicate current testcase
274 <     * should fail
273 >     * Just like assertTrue(b), but additionally recording (using
274 >     * threadRecordFailure) any AssertionError thrown, so that the current
275 >     * testcase will fail.
276       */
277      public void threadAssertTrue(boolean b) {
278 <        if (!b) {
247 <            threadFailed = true;
278 >        try {
279              assertTrue(b);
280 +        } catch (AssertionError t) {
281 +            threadRecordFailure(t);
282 +            throw t;
283          }
284      }
285  
286      /**
287 <     * If expression not false, set status to indicate current testcase
288 <     * should fail
287 >     * Just like assertFalse(b), but additionally recording (using
288 >     * threadRecordFailure) any AssertionError thrown, so that the
289 >     * current testcase will fail.
290       */
291      public void threadAssertFalse(boolean b) {
292 <        if (b) {
258 <            threadFailed = true;
292 >        try {
293              assertFalse(b);
294 +        } catch (AssertionError t) {
295 +            threadRecordFailure(t);
296 +            throw t;
297          }
298      }
299  
300      /**
301 <     * If argument not null, set status to indicate current testcase
302 <     * should fail
301 >     * Just like assertNull(x), but additionally recording (using
302 >     * threadRecordFailure) any AssertionError thrown, so that the
303 >     * current testcase will fail.
304       */
305      public void threadAssertNull(Object x) {
306 <        if (x != null) {
269 <            threadFailed = true;
306 >        try {
307              assertNull(x);
308 +        } catch (AssertionError t) {
309 +            threadRecordFailure(t);
310 +            throw t;
311          }
312      }
313  
314      /**
315 <     * If arguments not equal, set status to indicate current testcase
316 <     * should fail
315 >     * Just like assertEquals(x, y), but additionally recording (using
316 >     * threadRecordFailure) any AssertionError thrown, so that the
317 >     * current testcase will fail.
318       */
319      public void threadAssertEquals(long x, long y) {
320 <        if (x != y) {
280 <            threadFailed = true;
320 >        try {
321              assertEquals(x, y);
322 +        } catch (AssertionError t) {
323 +            threadRecordFailure(t);
324 +            throw t;
325          }
326      }
327  
328      /**
329 <     * If arguments not equal, set status to indicate current testcase
330 <     * should fail
329 >     * Just like assertEquals(x, y), but additionally recording (using
330 >     * threadRecordFailure) any AssertionError thrown, so that the
331 >     * current testcase will fail.
332       */
333      public void threadAssertEquals(Object x, Object y) {
334 <        if (x != y && (x == null || !x.equals(y))) {
291 <            threadFailed = true;
334 >        try {
335              assertEquals(x, y);
336 +        } catch (AssertionError t) {
337 +            threadRecordFailure(t);
338 +            throw t;
339          }
340      }
341  
342      /**
343 <     * threadFail with message "should throw exception"
343 >     * Just like assertSame(x, y), but additionally recording (using
344 >     * threadRecordFailure) any AssertionError thrown, so that the
345 >     * current testcase will fail.
346       */
347 <    public void threadShouldThrow() {
348 <        threadFailed = true;
349 <        fail("should throw exception");
347 >    public void threadAssertSame(Object x, Object y) {
348 >        try {
349 >            assertSame(x, y);
350 >        } catch (AssertionError t) {
351 >            threadRecordFailure(t);
352 >            throw t;
353 >        }
354      }
355  
356      /**
357 <     * threadFail with message "should throw" + exceptionName
357 >     * Calls threadFail with message "should throw exception".
358       */
359 <    public void threadShouldThrow(String exceptionName) {
360 <        threadFailed = true;
309 <        fail("should throw " + exceptionName);
359 >    public void threadShouldThrow() {
360 >        threadFail("should throw exception");
361      }
362  
363      /**
364 <     * threadFail with message "Unexpected exception"
364 >     * Calls threadFail with message "should throw" + exceptionName.
365       */
366 <    public void threadUnexpectedException() {
367 <        threadFailed = true;
317 <        fail("Unexpected exception");
366 >    public void threadShouldThrow(String exceptionName) {
367 >        threadFail("should throw " + exceptionName);
368      }
369  
370      /**
371 <     * threadFail with message "Unexpected exception", with argument
371 >     * Calls threadFail with message "Unexpected exception" + ex.
372       */
373 <    public void threadUnexpectedException(Throwable ex) {
374 <        threadFailed = true;
375 <        ex.printStackTrace();
376 <        fail("Unexpected exception: " + ex);
373 >    public void threadUnexpectedException(Throwable t) {
374 >        threadRecordFailure(t);
375 >        t.printStackTrace();
376 >        // Rethrow, wrapping in an AssertionError if necessary
377 >        if (t instanceof RuntimeException)
378 >            throw (RuntimeException) t;
379 >        else if (t instanceof Error)
380 >            throw (Error) t;
381 >        else {
382 >            AssertionError ae = new AssertionError("unexpected exception: " + t);
383 >            t.initCause(t);
384 >            throw ae;
385 >        }
386      }
387  
388      /**
389 <     * Wait out termination of a thread pool or fail doing so
389 >     * Waits out termination of a thread pool or fails doing so.
390       */
391      public void joinPool(ExecutorService exec) {
392          try {
393              exec.shutdown();
394 <            assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
394 >            assertTrue("ExecutorService did not terminate in a timely manner",
395 >                       exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
396          } catch (SecurityException ok) {
397              // Allowed in case test doesn't have privs
398          } catch (InterruptedException ie) {
# Line 342 | Line 402 | public class JSR166TestCase extends Test
402  
403  
404      /**
405 <     * fail with message "should throw exception"
405 >     * Fails with message "should throw exception".
406       */
407      public void shouldThrow() {
408          fail("Should throw exception");
409      }
410  
411      /**
412 <     * fail with message "should throw " + exceptionName
412 >     * Fails with message "should throw " + exceptionName.
413       */
414      public void shouldThrow(String exceptionName) {
415          fail("Should throw " + exceptionName);
416      }
417  
418      /**
419 <     * fail with message "Unexpected exception"
360 <     */
361 <    public void unexpectedException() {
362 <        fail("Unexpected exception");
363 <    }
364 <
365 <    /**
366 <     * fail with message "Unexpected exception", with argument
419 >     * Fails with message "Unexpected exception: " + ex.
420       */
421      public void unexpectedException(Throwable ex) {
422          ex.printStackTrace();
# Line 576 | Line 629 | public class JSR166TestCase extends Test
629                  return realCall();
630              } catch (Throwable t) {
631                  threadUnexpectedException(t);
632 +                return null;
633              }
580            return null;
634          }
635      }
636  
637 <    public abstract class CheckedInterruptedCallable<T> implements Callable<T> {
637 >    public abstract class CheckedInterruptedCallable<T>
638 >        implements Callable<T> {
639          protected abstract T realCall() throws Throwable;
640  
641          public final T call() {
# Line 753 | Line 807 | public class JSR166TestCase extends Test
807          }
808      }
809  
810 +    /**
811 +     * Analog of CheckedRunnable for RecursiveAction
812 +     */
813 +    public abstract class CheckedRecursiveAction extends RecursiveAction {
814 +        protected abstract void realCompute() throws Throwable;
815 +
816 +        public final void compute() {
817 +            try {
818 +                realCompute();
819 +            } catch (Throwable t) {
820 +                threadUnexpectedException(t);
821 +            }
822 +        }
823 +    }
824 +
825 +    /**
826 +     * Analog of CheckedCallable for RecursiveTask
827 +     */
828 +    public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> {
829 +        protected abstract T realCompute() throws Throwable;
830 +
831 +        public final T compute() {
832 +            try {
833 +                return realCompute();
834 +            } catch (Throwable t) {
835 +                threadUnexpectedException(t);
836 +                return null;
837 +            }
838 +        }
839 +    }
840  
841      /**
842       * For use as RejectedExecutionHandler in constructors

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines