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.40 by jsr166, Fri Nov 20 00:58:01 2009 UTC vs.
Revision 1.49 by jsr166, Tue Jan 5 02:08:37 2010 UTC

# Line 87 | Line 87 | import java.security.*;
87   * </ul>
88   */
89   public class JSR166TestCase extends TestCase {
90 +    private static final boolean useSecurityManager =
91 +        Boolean.getBoolean("jsr166.useSecurityManager");
92 +
93      /**
94       * Runs all JSR166 unit tests using junit.textui.TestRunner
95       */
96 <    public static void main (String[] args) {
96 >    public static void main(String[] args) {
97 >        if (useSecurityManager) {
98 >            System.err.println("Setting a permissive security manager");
99 >            Policy.setPolicy(permissivePolicy());
100 >            System.setSecurityManager(new SecurityManager());
101 >        }
102          int iters = 1;
103          if (args.length > 0)
104              iters = Integer.parseInt(args[0]);
105          Test s = suite();
106          for (int i = 0; i < iters; ++i) {
107 <            junit.textui.TestRunner.run (s);
107 >            junit.textui.TestRunner.run(s);
108              System.gc();
109              System.runFinalization();
110          }
# Line 106 | Line 114 | public class JSR166TestCase extends Test
114      /**
115       * Collects all JSR166 unit tests as one suite
116       */
117 <    public static Test suite ( ) {
117 >    public static Test suite() {
118          TestSuite suite = new TestSuite("JSR166 Unit Tests");
119  
120          suite.addTest(new TestSuite(ForkJoinPoolTest.class));
# Line 195 | Line 203 | public class JSR166TestCase extends Test
203      /**
204       * Sets delays as multiples of SHORT_DELAY.
205       */
206 <    protected  void setDelays() {
206 >    protected void setDelays() {
207          SHORT_DELAY_MS = getShortDelay();
208          SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
209          MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
# Line 328 | Line 336 | public class JSR166TestCase extends Test
336          } catch (SecurityException ok) {
337              // Allowed in case test doesn't have privs
338          } catch (InterruptedException ie) {
339 <            fail("Unexpected exception");
339 >            fail("Unexpected InterruptedException");
340          }
341      }
342  
# Line 366 | Line 374 | public class JSR166TestCase extends Test
374      /**
375       * The number of elements to place in collections, arrays, etc.
376       */
377 <    static final int SIZE = 20;
377 >    public static final int SIZE = 20;
378  
379      // Some convenient Integer constants
380  
381 <    static final Integer zero = new Integer(0);
382 <    static final Integer one = new Integer(1);
383 <    static final Integer two = new Integer(2);
384 <    static final Integer three  = new Integer(3);
385 <    static final Integer four  = new Integer(4);
386 <    static final Integer five  = new Integer(5);
387 <    static final Integer six = new Integer(6);
388 <    static final Integer seven = new Integer(7);
389 <    static final Integer eight = new Integer(8);
390 <    static final Integer nine = new Integer(9);
391 <    static final Integer m1  = new Integer(-1);
392 <    static final Integer m2  = new Integer(-2);
393 <    static final Integer m3  = new Integer(-3);
394 <    static final Integer m4 = new Integer(-4);
395 <    static final Integer m5 = new Integer(-5);
396 <    static final Integer m6 = new Integer(-6);
397 <    static final Integer m10 = new Integer(-10);
381 >    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 >    public static final Integer four  = new Integer(4);
386 >    public static final Integer five  = new Integer(5);
387 >    public static final Integer six   = new Integer(6);
388 >    public static final Integer seven = new Integer(7);
389 >    public static final Integer eight = new Integer(8);
390 >    public static final Integer nine  = new Integer(9);
391 >    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 >    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 >    public static final Integer m10 = new Integer(-10);
398  
399  
400      /**
401 +     * 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       * A security policy where new permissions can be dynamically added
443       * or all cleared.
444       */
445 <    static class AdjustablePolicy extends java.security.Policy {
445 >    public static class AdjustablePolicy extends java.security.Policy {
446          Permissions perms = new Permissions();
447 <        AdjustablePolicy() { }
447 >        AdjustablePolicy(Permission... permissions) {
448 >            for (Permission permission : permissions)
449 >                perms.add(permission);
450 >        }
451          void addPermission(Permission perm) { perms.add(perm); }
452          void clearPermissions() { perms = new Permissions(); }
453 <        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() {}
453 >        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 >    }
464 >
465 >    /**
466 >     * 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      /**
# Line 417 | Line 488 | public class JSR166TestCase extends Test
488      void sleepTillInterrupted(long timeoutMillis) {
489          try {
490              Thread.sleep(timeoutMillis);
491 <        } catch (InterruptedException wakeup) {
421 <        }
491 >        } catch (InterruptedException wakeup) {}
492      }
493  
494      /**
# Line 432 | Line 502 | public class JSR166TestCase extends Test
502  
503      // Some convenient Runnable classes
504  
505 <    abstract class CheckedRunnable implements Runnable {
506 <        abstract void realRun() throws Throwable;
505 >    public abstract class CheckedRunnable implements Runnable {
506 >        protected abstract void realRun() throws Throwable;
507  
508          public final void run() {
509              try {
# Line 444 | Line 514 | public class JSR166TestCase extends Test
514          }
515      }
516  
517 <    abstract class RunnableShouldThrow implements Runnable {
518 <        abstract void realRun() throws Throwable;
517 >    public abstract class RunnableShouldThrow implements Runnable {
518 >        protected abstract void realRun() throws Throwable;
519  
520          final Class<?> exceptionClass;
521  
# Line 457 | Line 527 | public class JSR166TestCase extends Test
527              try {
528                  realRun();
529                  threadShouldThrow(exceptionClass.getSimpleName());
460            } catch (InterruptedException success) {
530              } catch (Throwable t) {
531                  if (! exceptionClass.isInstance(t))
532                      threadUnexpectedException(t);
# Line 465 | Line 534 | public class JSR166TestCase extends Test
534          }
535      }
536  
537 <    abstract class ThreadShouldThrow extends Thread {
538 <        abstract void realRun() throws Throwable;
537 >    public abstract class ThreadShouldThrow extends Thread {
538 >        protected abstract void realRun() throws Throwable;
539  
540          final Class<?> exceptionClass;
541  
# Line 478 | Line 547 | public class JSR166TestCase extends Test
547              try {
548                  realRun();
549                  threadShouldThrow(exceptionClass.getSimpleName());
481            } catch (InterruptedException success) {
550              } catch (Throwable t) {
551                  if (! exceptionClass.isInstance(t))
552                      threadUnexpectedException(t);
# Line 486 | Line 554 | public class JSR166TestCase extends Test
554          }
555      }
556  
557 <    abstract class CheckedInterruptedRunnable implements Runnable {
558 <        abstract void realRun() throws Throwable;
557 >    public abstract class CheckedInterruptedRunnable implements Runnable {
558 >        protected abstract void realRun() throws Throwable;
559  
560          public final void run() {
561              try {
# Line 500 | Line 568 | public class JSR166TestCase extends Test
568          }
569      }
570  
571 <    abstract class CheckedCallable<T> implements Callable<T> {
572 <        abstract T realCall() throws Throwable;
571 >    public abstract class CheckedCallable<T> implements Callable<T> {
572 >        protected abstract T realCall() throws Throwable;
573  
574          public final T call() {
575              try {
# Line 513 | Line 581 | public class JSR166TestCase extends Test
581          }
582      }
583  
584 <    abstract class CheckedInterruptedCallable<T> implements Callable<T> {
585 <        abstract T realCall() throws Throwable;
584 >    public abstract class CheckedInterruptedCallable<T> implements Callable<T> {
585 >        protected abstract T realCall() throws Throwable;
586  
587          public final T call() {
588              try {
# Line 529 | Line 597 | public class JSR166TestCase extends Test
597          }
598      }
599  
600 <    static class NoOpRunnable implements Runnable {
600 >    public static class NoOpRunnable implements Runnable {
601          public void run() {}
602      }
603  
604 <    static class NoOpCallable implements Callable {
604 >    public static class NoOpCallable implements Callable {
605          public Object call() { return Boolean.TRUE; }
606      }
607  
608 <    static final String TEST_STRING = "a test string";
608 >    public static final String TEST_STRING = "a test string";
609  
610 <    static class StringTask implements Callable<String> {
610 >    public static class StringTask implements Callable<String> {
611          public String call() { return TEST_STRING; }
612      }
613  
614 <    static class NPETask implements Callable<String> {
614 >    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 >    public static class NPETask implements Callable<String> {
625          public String call() { throw new NullPointerException(); }
626      }
627  
628 <    static class CallableOne implements Callable<Integer> {
628 >    public static class CallableOne implements Callable<Integer> {
629          public Integer call() { return one; }
630      }
631  
632 <    class ShortRunnable extends CheckedRunnable {
633 <        void realRun() throws Throwable {
632 >    public class ShortRunnable extends CheckedRunnable {
633 >        protected void realRun() throws Throwable {
634              Thread.sleep(SHORT_DELAY_MS);
635          }
636      }
637  
638 <    class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
639 <        void realRun() throws InterruptedException {
638 >    public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
639 >        protected void realRun() throws InterruptedException {
640              Thread.sleep(SHORT_DELAY_MS);
641          }
642      }
643  
644 <    class SmallRunnable extends CheckedRunnable {
645 <        void realRun() throws Throwable {
644 >    public class SmallRunnable extends CheckedRunnable {
645 >        protected void realRun() throws Throwable {
646              Thread.sleep(SMALL_DELAY_MS);
647          }
648      }
649  
650 <    class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
651 <        void realRun() {
650 >    public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
651 >        protected void realRun() {
652              try {
653                  Thread.sleep(SMALL_DELAY_MS);
654 <            }
577 <            catch (InterruptedException ok) {
578 <            }
654 >            } catch (InterruptedException ok) {}
655          }
656      }
657  
658 <    class SmallCallable extends CheckedCallable {
659 <        Object realCall() throws Throwable {
658 >    public class SmallCallable extends CheckedCallable {
659 >        protected Object realCall() throws InterruptedException {
660              Thread.sleep(SMALL_DELAY_MS);
661              return Boolean.TRUE;
662          }
663      }
664  
665 <    class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
666 <        void realRun() throws InterruptedException {
665 >    public class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
666 >        protected void realRun() throws InterruptedException {
667              Thread.sleep(SMALL_DELAY_MS);
668          }
669      }
670  
671 <    class MediumRunnable extends CheckedRunnable {
672 <        void realRun() throws Throwable {
671 >    public class MediumRunnable extends CheckedRunnable {
672 >        protected void realRun() throws Throwable {
673              Thread.sleep(MEDIUM_DELAY_MS);
674          }
675      }
676  
677 <    class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
678 <        void realRun() throws InterruptedException {
677 >    public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
678 >        protected void realRun() throws InterruptedException {
679              Thread.sleep(MEDIUM_DELAY_MS);
680          }
681      }
682  
683 <    class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
684 <        void realRun() {
683 >    public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
684 >        protected void realRun() {
685              try {
686                  Thread.sleep(MEDIUM_DELAY_MS);
687 <            }
612 <            catch (InterruptedException ok) {
613 <            }
687 >            } catch (InterruptedException ok) {}
688          }
689      }
690  
691 <    class LongPossiblyInterruptedRunnable extends CheckedRunnable {
692 <        void realRun() {
691 >    public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
692 >        protected void realRun() {
693              try {
694                  Thread.sleep(LONG_DELAY_MS);
695 <            }
622 <            catch (InterruptedException ok) {
623 <            }
695 >            } catch (InterruptedException ok) {}
696          }
697      }
698  
699      /**
700       * For use as ThreadFactory in constructors
701       */
702 <    static class SimpleThreadFactory implements ThreadFactory {
702 >    public static class SimpleThreadFactory implements ThreadFactory {
703          public Thread newThread(Runnable r) {
704              return new Thread(r);
705          }
706      }
707  
708 <    static class TrackedShortRunnable implements Runnable {
709 <        volatile boolean done = false;
708 >    public static class TrackedShortRunnable implements Runnable {
709 >        public volatile boolean done = false;
710          public void run() {
711              try {
712                  Thread.sleep(SMALL_DELAY_MS);
713                  done = true;
714 <            } catch (InterruptedException ok) {
643 <            }
714 >            } catch (InterruptedException ok) {}
715          }
716      }
717  
718 <    static class TrackedMediumRunnable implements Runnable {
719 <        volatile boolean done = false;
718 >    public static class TrackedMediumRunnable implements Runnable {
719 >        public volatile boolean done = false;
720          public void run() {
721              try {
722                  Thread.sleep(MEDIUM_DELAY_MS);
723                  done = true;
724 <            } catch (InterruptedException ok) {
654 <            }
724 >            } catch (InterruptedException ok) {}
725          }
726      }
727  
728 <    static class TrackedLongRunnable implements Runnable {
729 <        volatile boolean done = false;
728 >    public static class TrackedLongRunnable implements Runnable {
729 >        public volatile boolean done = false;
730          public void run() {
731              try {
732                  Thread.sleep(LONG_DELAY_MS);
733                  done = true;
734 <            } catch (InterruptedException ok) {
665 <            }
734 >            } catch (InterruptedException ok) {}
735          }
736      }
737  
738 <    static class TrackedNoOpRunnable implements Runnable {
739 <        volatile boolean done = false;
738 >    public static class TrackedNoOpRunnable implements Runnable {
739 >        public volatile boolean done = false;
740          public void run() {
741              done = true;
742          }
743      }
744  
745 <    static class TrackedCallable implements Callable {
746 <        volatile boolean done = false;
745 >    public static class TrackedCallable implements Callable {
746 >        public volatile boolean done = false;
747          public Object call() {
748              try {
749                  Thread.sleep(SMALL_DELAY_MS);
750                  done = true;
751 <            } catch (InterruptedException ok) {
683 <            }
751 >            } catch (InterruptedException ok) {}
752              return Boolean.TRUE;
753          }
754      }
# Line 689 | Line 757 | public class JSR166TestCase extends Test
757      /**
758       * For use as RejectedExecutionHandler in constructors
759       */
760 <    static class NoOpREHandler implements RejectedExecutionHandler {
760 >    public static class NoOpREHandler implements RejectedExecutionHandler {
761          public void rejectedExecution(Runnable r,
762                                        ThreadPoolExecutor executor) {}
763      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines