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 |
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> |
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]); |
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)); |
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() { |
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) { |
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 |
|
*/ |
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 |
|
} |