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.35 by jsr166, Mon Aug 3 19:07:51 2009 UTC vs.
Revision 1.42 by jsr166, Sat Nov 21 02:07:26 2009 UTC

# Line 9 | Line 9
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 89 | Line 90 | public class JSR166TestCase extends Test
90      /**
91       * Runs all JSR166 unit tests using junit.textui.TestRunner
92       */
93 <    public static void main (String[] args) {
93 >    public static void main(String[] args) {
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);
99 >            junit.textui.TestRunner.run(s);
100              System.gc();
101              System.runFinalization();
102          }
# Line 105 | Line 106 | public class JSR166TestCase extends Test
106      /**
107       * Collects all JSR166 unit tests as one suite
108       */
109 <    public static Test suite ( ) {
109 >    public static Test suite() {
110          TestSuite suite = new TestSuite("JSR166 Unit Tests");
111  
112          suite.addTest(new TestSuite(ForkJoinPoolTest.class));
# Line 288 | Line 289 | public class JSR166TestCase extends Test
289       * threadFail with message "should throw exception"
290       */
291      public void threadShouldThrow() {
292 <        try {
293 <            threadFailed = true;
294 <            fail("should throw exception");
295 <        } catch (AssertionFailedError e) {
296 <            e.printStackTrace();
297 <            throw e;
298 <        }
292 >        threadFailed = true;
293 >        fail("should throw exception");
294 >    }
295 >
296 >    /**
297 >     * threadFail with message "should throw" + exceptionName
298 >     */
299 >    public void threadShouldThrow(String exceptionName) {
300 >        threadFailed = true;
301 >        fail("should throw " + exceptionName);
302      }
303  
304      /**
# Line 320 | Line 324 | public class JSR166TestCase extends Test
324      public void joinPool(ExecutorService exec) {
325          try {
326              exec.shutdown();
327 <            assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
327 >            assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
328          } catch (SecurityException ok) {
329              // Allowed in case test doesn't have privs
330          } catch (InterruptedException ie) {
# Line 337 | Line 341 | public class JSR166TestCase extends Test
341      }
342  
343      /**
344 +     * fail with message "should throw " + exceptionName
345 +     */
346 +    public void shouldThrow(String exceptionName) {
347 +        fail("Should throw " + exceptionName);
348 +    }
349 +
350 +    /**
351       * fail with message "Unexpected exception"
352       */
353      public void unexpectedException() {
354          fail("Unexpected exception");
355      }
356  
357 +    /**
358 +     * fail with message "Unexpected exception", with argument
359 +     */
360 +    public void unexpectedException(Throwable ex) {
361 +        ex.printStackTrace();
362 +        fail("Unexpected exception: " + ex);
363 +    }
364 +
365  
366      /**
367       * The number of elements to place in collections, arrays, etc.
# Line 379 | Line 398 | public class JSR166TestCase extends Test
398          AdjustablePolicy() { }
399          void addPermission(Permission perm) { perms.add(perm); }
400          void clearPermissions() { perms = new Permissions(); }
401 <        public PermissionCollection getPermissions(CodeSource cs) {
402 <            return perms;
403 <        }
404 <        public PermissionCollection getPermissions(ProtectionDomain pd) {
405 <            return perms;
406 <        }
407 <        public boolean implies(ProtectionDomain pd, Permission p) {
408 <            return perms.implies(p);
409 <        }
410 <        public void refresh() {}
401 >        public PermissionCollection getPermissions(CodeSource cs) {
402 >            return perms;
403 >        }
404 >        public PermissionCollection getPermissions(ProtectionDomain pd) {
405 >            return perms;
406 >        }
407 >        public boolean implies(ProtectionDomain pd, Permission p) {
408 >            return perms.implies(p);
409 >        }
410 >        public void refresh() {}
411      }
412  
413 +    /**
414 +     * Sleep until the timeout has elapsed, or interrupted.
415 +     * Does <em>NOT</em> throw InterruptedException.
416 +     */
417 +    void sleepTillInterrupted(long timeoutMillis) {
418 +        try {
419 +            Thread.sleep(timeoutMillis);
420 +        } catch (InterruptedException wakeup) {
421 +        }
422 +    }
423 +
424 +    /**
425 +     * Returns a new started Thread running the given runnable.
426 +     */
427 +    Thread newStartedThread(Runnable runnable) {
428 +        Thread t = new Thread(runnable);
429 +        t.start();
430 +        return t;
431 +    }
432  
433      // Some convenient Runnable classes
434  
# Line 406 | Line 444 | public class JSR166TestCase extends Test
444          }
445      }
446  
447 +    abstract class RunnableShouldThrow implements Runnable {
448 +        abstract void realRun() throws Throwable;
449 +
450 +        final Class<?> exceptionClass;
451 +
452 +        <T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) {
453 +            this.exceptionClass = exceptionClass;
454 +        }
455 +
456 +        public final void run() {
457 +            try {
458 +                realRun();
459 +                threadShouldThrow(exceptionClass.getSimpleName());
460 +            } catch (InterruptedException success) {
461 +            } catch (Throwable t) {
462 +                if (! exceptionClass.isInstance(t))
463 +                    threadUnexpectedException(t);
464 +            }
465 +        }
466 +    }
467 +
468 +    abstract class ThreadShouldThrow extends Thread {
469 +        abstract void realRun() throws Throwable;
470 +
471 +        final Class<?> exceptionClass;
472 +
473 +        <T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) {
474 +            this.exceptionClass = exceptionClass;
475 +        }
476 +
477 +        public final void run() {
478 +            try {
479 +                realRun();
480 +                threadShouldThrow(exceptionClass.getSimpleName());
481 +            } catch (InterruptedException success) {
482 +            } catch (Throwable t) {
483 +                if (! exceptionClass.isInstance(t))
484 +                    threadUnexpectedException(t);
485 +            }
486 +        }
487 +    }
488 +
489      abstract class CheckedInterruptedRunnable implements Runnable {
490          abstract void realRun() throws Throwable;
491  
492          public final void run() {
493              try {
494                  realRun();
495 <                threadShouldThrow();
495 >                threadShouldThrow("InterruptedException");
496              } catch (InterruptedException success) {
497              } catch (Throwable t) {
498                  threadUnexpectedException(t);
# Line 428 | Line 508 | public class JSR166TestCase extends Test
508                  return realCall();
509              } catch (Throwable t) {
510                  threadUnexpectedException(t);
431                return null;
511              }
512 +            return null;
513 +        }
514 +    }
515 +
516 +    abstract class CheckedInterruptedCallable<T> implements Callable<T> {
517 +        abstract T realCall() throws Throwable;
518 +
519 +        public final T call() {
520 +            try {
521 +                T result = realCall();
522 +                threadShouldThrow("InterruptedException");
523 +                return result;
524 +            } catch (InterruptedException success) {
525 +            } catch (Throwable t) {
526 +                threadUnexpectedException(t);
527 +            }
528 +            return null;
529          }
530      }
531  
# Line 543 | Line 639 | public class JSR166TestCase extends Test
639              try {
640                  Thread.sleep(SMALL_DELAY_MS);
641                  done = true;
642 <            } catch (Exception e) {
642 >            } catch (InterruptedException ok) {
643              }
644          }
645      }
# Line 554 | Line 650 | public class JSR166TestCase extends Test
650              try {
651                  Thread.sleep(MEDIUM_DELAY_MS);
652                  done = true;
653 <            } catch (Exception e) {
653 >            } catch (InterruptedException ok) {
654              }
655          }
656      }
# Line 565 | Line 661 | public class JSR166TestCase extends Test
661              try {
662                  Thread.sleep(LONG_DELAY_MS);
663                  done = true;
664 <            } catch (Exception e) {
664 >            } catch (InterruptedException ok) {
665              }
666          }
667      }
# Line 583 | Line 679 | public class JSR166TestCase extends Test
679              try {
680                  Thread.sleep(SMALL_DELAY_MS);
681                  done = true;
682 <            } catch (Exception e) {
682 >            } catch (InterruptedException ok) {
683              }
684              return Boolean.TRUE;
685          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines