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.52 by jsr166, Mon Sep 13 23:19:31 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 286 | Line 295 | public class JSR166TestCase extends Test
295      }
296  
297      /**
298 +     * If arguments not identical, set status to indicate current testcase
299 +     * should fail
300 +     */
301 +    public void threadAssertSame(Object x, Object y) {
302 +        if (x != y) {
303 +            threadFailed = true;
304 +            assertSame(x, y);
305 +        }
306 +    }
307 +
308 +    /**
309       * threadFail with message "should throw exception"
310       */
311      public void threadShouldThrow() {
# Line 370 | Line 390 | public class JSR166TestCase extends Test
390  
391      // Some convenient Integer constants
392  
393 <    public static final Integer zero = new Integer(0);
394 <    public static final Integer one = new Integer(1);
395 <    public static final Integer two = new Integer(2);
396 <    public static final Integer three  = new Integer(3);
393 >    public static final Integer zero  = new Integer(0);
394 >    public static final Integer one   = new Integer(1);
395 >    public static final Integer two   = new Integer(2);
396 >    public static final Integer three = new Integer(3);
397      public static final Integer four  = new Integer(4);
398      public static final Integer five  = new Integer(5);
399 <    public static final Integer six = new Integer(6);
399 >    public static final Integer six   = new Integer(6);
400      public static final Integer seven = new Integer(7);
401      public static final Integer eight = new Integer(8);
402 <    public static final Integer nine = new Integer(9);
402 >    public static final Integer nine  = new Integer(9);
403      public static final Integer m1  = new Integer(-1);
404      public static final Integer m2  = new Integer(-2);
405      public static final Integer m3  = new Integer(-3);
406 <    public static final Integer m4 = new Integer(-4);
407 <    public static final Integer m5 = new Integer(-5);
408 <    public static final Integer m6 = new Integer(-6);
406 >    public static final Integer m4  = new Integer(-4);
407 >    public static final Integer m5  = new Integer(-5);
408 >    public static final Integer m6  = new Integer(-6);
409      public static final Integer m10 = new Integer(-10);
410  
411  
412      /**
413 +     * Runs Runnable r with a security policy that permits precisely
414 +     * the specified permissions.  If there is no current security
415 +     * manager, the runnable is run twice, both with and without a
416 +     * security manager.  We require that any security manager permit
417 +     * getPolicy/setPolicy.
418 +     */
419 +    public void runWithPermissions(Runnable r, Permission... permissions) {
420 +        SecurityManager sm = System.getSecurityManager();
421 +        if (sm == null) {
422 +            r.run();
423 +            Policy savedPolicy = Policy.getPolicy();
424 +            try {
425 +                Policy.setPolicy(permissivePolicy());
426 +                System.setSecurityManager(new SecurityManager());
427 +                runWithPermissions(r, permissions);
428 +            } finally {
429 +                System.setSecurityManager(null);
430 +                Policy.setPolicy(savedPolicy);
431 +            }
432 +        } else {
433 +            Policy savedPolicy = Policy.getPolicy();
434 +            AdjustablePolicy policy = new AdjustablePolicy(permissions);
435 +            Policy.setPolicy(policy);
436 +
437 +            try {
438 +                r.run();
439 +            } finally {
440 +                policy.addPermission(new SecurityPermission("setPolicy"));
441 +                Policy.setPolicy(savedPolicy);
442 +            }
443 +        }
444 +    }
445 +
446 +    /**
447 +     * Runs a runnable without any permissions.
448 +     */
449 +    public void runWithoutPermissions(Runnable r) {
450 +        runWithPermissions(r);
451 +    }
452 +
453 +    /**
454       * A security policy where new permissions can be dynamically added
455       * or all cleared.
456       */
457      public static class AdjustablePolicy extends java.security.Policy {
458          Permissions perms = new Permissions();
459 <        AdjustablePolicy() { }
459 >        AdjustablePolicy(Permission... permissions) {
460 >            for (Permission permission : permissions)
461 >                perms.add(permission);
462 >        }
463          void addPermission(Permission perm) { perms.add(perm); }
464          void clearPermissions() { perms = new Permissions(); }
465          public PermissionCollection getPermissions(CodeSource cs) {
# Line 411 | Line 475 | public class JSR166TestCase extends Test
475      }
476  
477      /**
478 +     * Returns a policy containing all the permissions we ever need.
479 +     */
480 +    public static Policy permissivePolicy() {
481 +        return new AdjustablePolicy
482 +            // Permissions j.u.c. needs directly
483 +            (new RuntimePermission("modifyThread"),
484 +             new RuntimePermission("getClassLoader"),
485 +             new RuntimePermission("setContextClassLoader"),
486 +             // Permissions needed to change permissions!
487 +             new SecurityPermission("getPolicy"),
488 +             new SecurityPermission("setPolicy"),
489 +             new RuntimePermission("setSecurityManager"),
490 +             // Permissions needed by the junit test harness
491 +             new RuntimePermission("accessDeclaredMembers"),
492 +             new PropertyPermission("*", "read"),
493 +             new java.io.FilePermission("<<ALL FILES>>", "read"));
494 +    }
495 +
496 +    /**
497       * Sleep until the timeout has elapsed, or interrupted.
498       * Does <em>NOT</em> throw InterruptedException.
499       */
# Line 456 | Line 539 | public class JSR166TestCase extends Test
539              try {
540                  realRun();
541                  threadShouldThrow(exceptionClass.getSimpleName());
459            } catch (InterruptedException success) {
542              } catch (Throwable t) {
543                  if (! exceptionClass.isInstance(t))
544                      threadUnexpectedException(t);
# Line 477 | Line 559 | public class JSR166TestCase extends Test
559              try {
560                  realRun();
561                  threadShouldThrow(exceptionClass.getSimpleName());
480            } catch (InterruptedException success) {
562              } catch (Throwable t) {
563                  if (! exceptionClass.isInstance(t))
564                      threadUnexpectedException(t);
# Line 542 | Line 623 | public class JSR166TestCase extends Test
623          public String call() { return TEST_STRING; }
624      }
625  
626 +    public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
627 +        return new CheckedCallable<String>() {
628 +            public String realCall() {
629 +                try {
630 +                    latch.await();
631 +                } catch (InterruptedException quittingTime) {}
632 +                return TEST_STRING;
633 +            }};
634 +    }
635 +
636      public static class NPETask implements Callable<String> {
637          public String call() { throw new NullPointerException(); }
638      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines