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.45 by jsr166, Thu Nov 26 15:42:15 2009 UTC vs.
Revision 1.51 by jsr166, Wed Sep 1 06:41:55 2010 UTC

# Line 26 | Line 26 | import java.security.*;
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
29 < * <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not
29 > * {@code fail}, {@code assertTrue}, 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
35 < * to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within
35 > * to invoke {@code super.setUp} and {@code super.tearDown} within
36   * them. These methods are used to clear and check for thread
37   * assertion failures.</li>
38   *
39 < * <li>All delays and timeouts must use one of the constants <tt>
40 < * SHORT_DELAY_MS</tt>, <tt> SMALL_DELAY_MS</tt>, <tt> MEDIUM_DELAY_MS</tt>,
41 < * <tt> LONG_DELAY_MS</tt>. The idea here is that a SHORT is always
39 > * <li>All delays and timeouts must use one of the constants {@code
40 > * SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS},
41 > * {@code LONG_DELAY_MS}. The idea here is that a SHORT is always
42   * discriminable from zero time, and always allows enough time for the
43   * small amounts of computation (creating a thread, calling a few
44   * methods, etc) needed to reach a timeout point. Similarly, a SMALL
# Line 48 | Line 48 | import java.security.*;
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
52 < * method. The <tt> joinPool</tt> method can be used to do this when
51 > * method (or {@code fail} to do so) before returning from the
52 > * method. The {@code joinPool} method can be used to do this when
53   * using Executors.</li>
54   *
55   * </ol>
# Line 81 | Line 81 | import java.security.*;
81   * any particular package to simplify things for people integrating
82   * them in TCK test suites.</li>
83   *
84 < * <li> As a convenience, the <tt>main</tt> of this class (JSR166TestCase)
84 > * <li> As a convenience, the {@code main} of this class (JSR166TestCase)
85   * runs all JSR166 unit tests.</li>
86   *
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) {
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]);
# Line 135 | Line 143 | public class JSR166TestCase extends Test
143          suite.addTest(new TestSuite(AtomicReferenceTest.class));
144          suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
145          suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
146 +        suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class));
147          suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
148          suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class));
149          suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class));
# Line 370 | Line 379 | public class JSR166TestCase extends Test
379  
380      // Some convenient Integer constants
381  
382 <    public static final Integer zero = new Integer(0);
383 <    public static final Integer one = new Integer(1);
384 <    public static final Integer two = new Integer(2);
385 <    public static final Integer three  = new Integer(3);
382 >    public static final Integer zero  = new Integer(0);
383 >    public static final Integer one   = new Integer(1);
384 >    public static final Integer two   = new Integer(2);
385 >    public static final Integer three = new Integer(3);
386      public static final Integer four  = new Integer(4);
387      public static final Integer five  = new Integer(5);
388 <    public static final Integer six = new Integer(6);
388 >    public static final Integer six   = new Integer(6);
389      public static final Integer seven = new Integer(7);
390      public static final Integer eight = new Integer(8);
391 <    public static final Integer nine = new Integer(9);
391 >    public static final Integer nine  = new Integer(9);
392      public static final Integer m1  = new Integer(-1);
393      public static final Integer m2  = new Integer(-2);
394      public static final Integer m3  = new Integer(-3);
395 <    public static final Integer m4 = new Integer(-4);
396 <    public static final Integer m5 = new Integer(-5);
397 <    public static final Integer m6 = new Integer(-6);
395 >    public static final Integer m4  = new Integer(-4);
396 >    public static final Integer m5  = new Integer(-5);
397 >    public static final Integer m6  = new Integer(-6);
398      public static final Integer m10 = new Integer(-10);
399  
400  
401      /**
402 +     * Runs Runnable r with a security policy that permits precisely
403 +     * the specified permissions.  If there is no current security
404 +     * manager, the runnable is run twice, both with and without a
405 +     * security manager.  We require that any security manager permit
406 +     * getPolicy/setPolicy.
407 +     */
408 +    public void runWithPermissions(Runnable r, Permission... permissions) {
409 +        SecurityManager sm = System.getSecurityManager();
410 +        if (sm == null) {
411 +            r.run();
412 +            Policy savedPolicy = Policy.getPolicy();
413 +            try {
414 +                Policy.setPolicy(permissivePolicy());
415 +                System.setSecurityManager(new SecurityManager());
416 +                runWithPermissions(r, permissions);
417 +            } finally {
418 +                System.setSecurityManager(null);
419 +                Policy.setPolicy(savedPolicy);
420 +            }
421 +        } else {
422 +            Policy savedPolicy = Policy.getPolicy();
423 +            AdjustablePolicy policy = new AdjustablePolicy(permissions);
424 +            Policy.setPolicy(policy);
425 +
426 +            try {
427 +                r.run();
428 +            } finally {
429 +                policy.addPermission(new SecurityPermission("setPolicy"));
430 +                Policy.setPolicy(savedPolicy);
431 +            }
432 +        }
433 +    }
434 +
435 +    /**
436 +     * Runs a runnable without any permissions.
437 +     */
438 +    public void runWithoutPermissions(Runnable r) {
439 +        runWithPermissions(r);
440 +    }
441 +
442 +    /**
443       * A security policy where new permissions can be dynamically added
444       * or all cleared.
445       */
446      public static class AdjustablePolicy extends java.security.Policy {
447          Permissions perms = new Permissions();
448 <        AdjustablePolicy() { }
448 >        AdjustablePolicy(Permission... permissions) {
449 >            for (Permission permission : permissions)
450 >                perms.add(permission);
451 >        }
452          void addPermission(Permission perm) { perms.add(perm); }
453          void clearPermissions() { perms = new Permissions(); }
454          public PermissionCollection getPermissions(CodeSource cs) {
# Line 411 | Line 464 | public class JSR166TestCase extends Test
464      }
465  
466      /**
467 +     * Returns a policy containing all the permissions we ever need.
468 +     */
469 +    public static Policy permissivePolicy() {
470 +        return new AdjustablePolicy
471 +            // Permissions j.u.c. needs directly
472 +            (new RuntimePermission("modifyThread"),
473 +             new RuntimePermission("getClassLoader"),
474 +             new RuntimePermission("setContextClassLoader"),
475 +             // Permissions needed to change permissions!
476 +             new SecurityPermission("getPolicy"),
477 +             new SecurityPermission("setPolicy"),
478 +             new RuntimePermission("setSecurityManager"),
479 +             // Permissions needed by the junit test harness
480 +             new RuntimePermission("accessDeclaredMembers"),
481 +             new PropertyPermission("*", "read"),
482 +             new java.io.FilePermission("<<ALL FILES>>", "read"));
483 +    }
484 +
485 +    /**
486       * Sleep until the timeout has elapsed, or interrupted.
487       * Does <em>NOT</em> throw InterruptedException.
488       */
# Line 456 | Line 528 | public class JSR166TestCase extends Test
528              try {
529                  realRun();
530                  threadShouldThrow(exceptionClass.getSimpleName());
459            } catch (InterruptedException success) {
531              } catch (Throwable t) {
532                  if (! exceptionClass.isInstance(t))
533                      threadUnexpectedException(t);
# Line 477 | Line 548 | public class JSR166TestCase extends Test
548              try {
549                  realRun();
550                  threadShouldThrow(exceptionClass.getSimpleName());
480            } catch (InterruptedException success) {
551              } catch (Throwable t) {
552                  if (! exceptionClass.isInstance(t))
553                      threadUnexpectedException(t);
# Line 542 | Line 612 | public class JSR166TestCase extends Test
612          public String call() { return TEST_STRING; }
613      }
614  
615 +    public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
616 +        return new CheckedCallable<String>() {
617 +            public String realCall() {
618 +                try {
619 +                    latch.await();
620 +                } catch (InterruptedException quittingTime) {}
621 +                return TEST_STRING;
622 +            }};
623 +    }
624 +
625      public static class NPETask implements Callable<String> {
626          public String call() { throw new NullPointerException(); }
627      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines