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.52 by jsr166, Mon Sep 13 23:19:31 2010 UTC vs.
Revision 1.53 by jsr166, Thu Sep 16 00:52:49 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 206 | Line 212 | public class JSR166TestCase extends Test
212       */
213      protected void setDelays() {
214          SHORT_DELAY_MS = getShortDelay();
215 <        SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
215 >        SMALL_DELAY_MS  = SHORT_DELAY_MS * 5;
216          MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
217 <        LONG_DELAY_MS = SHORT_DELAY_MS * 50;
217 >        LONG_DELAY_MS   = SHORT_DELAY_MS * 50;
218      }
219  
220      /**
221 <     * Flag set true if any threadAssert methods fail
221 >     * The first exception encountered if any threadAssertXXX method fails.
222       */
223 <    volatile boolean threadFailed;
223 >    private final AtomicReference<Throwable> threadFailure
224 >        = new AtomicReference<Throwable>(null);
225  
226      /**
227 <     * Initializes test to indicate that no thread assertions have failed
227 >     * Records an exception so that it can be rethrown later in the test
228 >     * harness thread, triggering a test case failure.  Only the first
229 >     * failure is recorded; subsequent calls to this method from within
230 >     * the same test have no effect.
231       */
232 +    public void threadRecordFailure(Throwable t) {
233 +        threadFailure.compareAndSet(null, t);
234 +    }
235 +
236      public void setUp() {
237          setDelays();
224        threadFailed = false;
238      }
239  
240      /**
241 <     * Triggers test case failure if any thread assertions have failed
242 <     */
243 <    public void tearDown() {
244 <        assertFalse(threadFailed);
241 >     * Triggers test case failure if any thread assertions have failed,
242 >     * by rethrowing, in the test harness thread, any exception recorded
243 >     * earlier by threadRecordFailure.
244 >     */
245 >    public void tearDown() throws Exception {
246 >        Throwable t = threadFailure.get();
247 >        if (t != null) {
248 >            if (t instanceof Error)
249 >                throw (Error) t;
250 >            else if (t instanceof RuntimeException)
251 >                throw (RuntimeException) t;
252 >            else if (t instanceof Exception)
253 >                throw (Exception) t;
254 >            else
255 >                throw new AssertionError(t);
256 >        }
257      }
258  
259      /**
260 <     * Fail, also setting status to indicate current testcase should fail
260 >     * Just like fail(reason), but additionally recording (using
261 >     * threadRecordFailure) any AssertionError thrown, so that the current
262 >     * testcase will fail.
263       */
264      public void threadFail(String reason) {
265 <        threadFailed = true;
266 <        fail(reason);
265 >        try {
266 >            fail(reason);
267 >        } catch (Throwable t) {
268 >            threadRecordFailure(t);
269 >            fail(reason);
270 >        }
271      }
272  
273      /**
274 <     * If expression not true, set status to indicate current testcase
275 <     * should fail
274 >     * Just like assertTrue(b), but additionally recording (using
275 >     * threadRecordFailure) any AssertionError thrown, so that the current
276 >     * testcase will fail.
277       */
278      public void threadAssertTrue(boolean b) {
279 <        if (!b) {
248 <            threadFailed = true;
279 >        try {
280              assertTrue(b);
281 +        } catch (AssertionError t) {
282 +            threadRecordFailure(t);
283 +            throw t;
284          }
285      }
286  
287      /**
288 <     * If expression not false, set status to indicate current testcase
289 <     * should fail
288 >     * Just like assertFalse(b), but additionally recording (using
289 >     * threadRecordFailure) any AssertionError thrown, so that the
290 >     * current testcase will fail.
291       */
292      public void threadAssertFalse(boolean b) {
293 <        if (b) {
259 <            threadFailed = true;
293 >        try {
294              assertFalse(b);
295 +        } catch (AssertionError t) {
296 +            threadRecordFailure(t);
297 +            throw t;
298          }
299      }
300  
301      /**
302 <     * If argument not null, set status to indicate current testcase
303 <     * should fail
302 >     * Just like assertNull(x), but additionally recording (using
303 >     * threadRecordFailure) any AssertionError thrown, so that the
304 >     * current testcase will fail.
305       */
306      public void threadAssertNull(Object x) {
307 <        if (x != null) {
270 <            threadFailed = true;
307 >        try {
308              assertNull(x);
309 +        } catch (AssertionError t) {
310 +            threadRecordFailure(t);
311 +            throw t;
312          }
313      }
314  
315      /**
316 <     * If arguments not equal, set status to indicate current testcase
317 <     * should fail
316 >     * Just like assertEquals(x, y), but additionally recording (using
317 >     * threadRecordFailure) any AssertionError thrown, so that the
318 >     * current testcase will fail.
319       */
320      public void threadAssertEquals(long x, long y) {
321 <        if (x != y) {
281 <            threadFailed = true;
321 >        try {
322              assertEquals(x, y);
323 +        } catch (AssertionError t) {
324 +            threadRecordFailure(t);
325 +            throw t;
326          }
327      }
328  
329      /**
330 <     * If arguments not equal, set status to indicate current testcase
331 <     * should fail
330 >     * Just like assertEquals(x, y), but additionally recording (using
331 >     * threadRecordFailure) any AssertionError thrown, so that the
332 >     * current testcase will fail.
333       */
334      public void threadAssertEquals(Object x, Object y) {
335 <        if (x != y && (x == null || !x.equals(y))) {
292 <            threadFailed = true;
335 >        try {
336              assertEquals(x, y);
337 +        } catch (AssertionError t) {
338 +            threadRecordFailure(t);
339 +            throw t;
340          }
341      }
342  
343      /**
344 <     * If arguments not identical, set status to indicate current testcase
345 <     * should fail
344 >     * Just like assertSame(x, y), but additionally recording (using
345 >     * threadRecordFailure) any AssertionError thrown, so that the
346 >     * current testcase will fail.
347       */
348      public void threadAssertSame(Object x, Object y) {
349 <        if (x != y) {
303 <            threadFailed = true;
349 >        try {
350              assertSame(x, y);
351 +        } catch (AssertionError t) {
352 +            threadRecordFailure(t);
353 +            throw t;
354          }
355      }
356  
357      /**
358 <     * threadFail with message "should throw exception"
358 >     * Calls threadFail with message "should throw exception".
359       */
360      public void threadShouldThrow() {
361 <        threadFailed = true;
313 <        fail("should throw exception");
361 >        threadFail("should throw exception");
362      }
363  
364      /**
365 <     * threadFail with message "should throw" + exceptionName
365 >     * Calls threadFail with message "should throw" + exceptionName.
366       */
367      public void threadShouldThrow(String exceptionName) {
368 <        threadFailed = true;
321 <        fail("should throw " + exceptionName);
322 <    }
323 <
324 <    /**
325 <     * threadFail with message "Unexpected exception"
326 <     */
327 <    public void threadUnexpectedException() {
328 <        threadFailed = true;
329 <        fail("Unexpected exception");
368 >        threadFail("should throw " + exceptionName);
369      }
370  
371      /**
372 <     * threadFail with message "Unexpected exception", with argument
372 >     * Calls threadFail with message "Unexpected exception" + ex.
373       */
374 <    public void threadUnexpectedException(Throwable ex) {
375 <        threadFailed = true;
376 <        ex.printStackTrace();
377 <        fail("Unexpected exception: " + ex);
374 >    public void threadUnexpectedException(Throwable t) {
375 >        threadRecordFailure(t);
376 >        t.printStackTrace();
377 >        // Rethrow, wrapping in an AssertionError if necessary
378 >        if (t instanceof RuntimeException)
379 >            throw (RuntimeException) t;
380 >        else if (t instanceof Error)
381 >            throw (Error) t;
382 >        else {
383 >            AssertionError ae = new AssertionError("unexpected exception: " + t);
384 >            t.initCause(t);
385 >            throw ae;
386 >        }            
387      }
388  
389      /**
390 <     * Wait out termination of a thread pool or fail doing so
390 >     * Waits out termination of a thread pool or fails doing so.
391       */
392      public void joinPool(ExecutorService exec) {
393          try {
# Line 354 | 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"
372 <     */
373 <    public void unexpectedException() {
374 <        fail("Unexpected exception");
375 <    }
376 <
377 <    /**
378 <     * 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 588 | Line 629 | public class JSR166TestCase extends Test
629                  return realCall();
630              } catch (Throwable t) {
631                  threadUnexpectedException(t);
632 +                return null;
633              }
592            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 765 | 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