7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.PropertyPermission; |
11 |
|
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
13 |
< |
import java.security.*; |
12 |
> |
import java.util.concurrent.atomic.AtomicReference; |
13 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
> |
import java.security.CodeSource; |
15 |
> |
import java.security.Permission; |
16 |
> |
import java.security.PermissionCollection; |
17 |
> |
import java.security.Permissions; |
18 |
> |
import java.security.Policy; |
19 |
> |
import java.security.ProtectionDomain; |
20 |
> |
import java.security.SecurityPermission; |
21 |
|
|
22 |
|
/** |
23 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
32 |
|
* <li> All assertions in code running in generated threads must use |
33 |
|
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
34 |
|
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
35 |
< |
* <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not |
35 |
> |
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
36 |
|
* particularly recommended) for other code to use these forms too. |
37 |
|
* Only the most typically used JUnit assertion methods are defined |
38 |
|
* this way, but enough to live with.</li> |
39 |
|
* |
40 |
|
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
41 |
< |
* to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within |
41 |
> |
* to invoke {@code super.setUp} and {@code super.tearDown} within |
42 |
|
* them. These methods are used to clear and check for thread |
43 |
|
* assertion failures.</li> |
44 |
|
* |
45 |
< |
* <li>All delays and timeouts must use one of the constants <tt> |
46 |
< |
* SHORT_DELAY_MS</tt>, <tt> SMALL_DELAY_MS</tt>, <tt> MEDIUM_DELAY_MS</tt>, |
47 |
< |
* <tt> LONG_DELAY_MS</tt>. The idea here is that a SHORT is always |
45 |
> |
* <li>All delays and timeouts must use one of the constants {@code |
46 |
> |
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
47 |
> |
* {@code LONG_DELAY_MS}. The idea here is that a SHORT is always |
48 |
|
* discriminable from zero time, and always allows enough time for the |
49 |
|
* small amounts of computation (creating a thread, calling a few |
50 |
|
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
54 |
|
* in one spot to rerun tests on slower platforms.</li> |
55 |
|
* |
56 |
|
* <li> All threads generated must be joined inside each test case |
57 |
< |
* method (or <tt>fail</tt> to do so) before returning from the |
58 |
< |
* method. The <tt> joinPool</tt> method can be used to do this when |
57 |
> |
* method (or {@code fail} to do so) before returning from the |
58 |
> |
* method. The {@code joinPool} method can be used to do this when |
59 |
|
* using Executors.</li> |
60 |
|
* |
61 |
|
* </ol> |
87 |
|
* any particular package to simplify things for people integrating |
88 |
|
* them in TCK test suites.</li> |
89 |
|
* |
90 |
< |
* <li> As a convenience, the <tt>main</tt> of this class (JSR166TestCase) |
90 |
> |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
91 |
|
* runs all JSR166 unit tests.</li> |
92 |
|
* |
93 |
|
* </ul> |
94 |
|
*/ |
95 |
|
public class JSR166TestCase extends TestCase { |
96 |
+ |
private static final boolean useSecurityManager = |
97 |
+ |
Boolean.getBoolean("jsr166.useSecurityManager"); |
98 |
+ |
|
99 |
|
/** |
100 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
101 |
|
*/ |
102 |
< |
public static void main (String[] args) { |
102 |
> |
public static void main(String[] args) { |
103 |
> |
if (useSecurityManager) { |
104 |
> |
System.err.println("Setting a permissive security manager"); |
105 |
> |
Policy.setPolicy(permissivePolicy()); |
106 |
> |
System.setSecurityManager(new SecurityManager()); |
107 |
> |
} |
108 |
|
int iters = 1; |
109 |
|
if (args.length > 0) |
110 |
|
iters = Integer.parseInt(args[0]); |
111 |
|
Test s = suite(); |
112 |
|
for (int i = 0; i < iters; ++i) { |
113 |
< |
junit.textui.TestRunner.run (s); |
113 |
> |
junit.textui.TestRunner.run(s); |
114 |
|
System.gc(); |
115 |
|
System.runFinalization(); |
116 |
|
} |
120 |
|
/** |
121 |
|
* Collects all JSR166 unit tests as one suite |
122 |
|
*/ |
123 |
< |
public static Test suite ( ) { |
123 |
> |
public static Test suite() { |
124 |
|
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
125 |
|
|
126 |
+ |
suite.addTest(new TestSuite(ForkJoinPoolTest.class)); |
127 |
+ |
suite.addTest(new TestSuite(ForkJoinTaskTest.class)); |
128 |
+ |
suite.addTest(new TestSuite(RecursiveActionTest.class)); |
129 |
+ |
suite.addTest(new TestSuite(RecursiveTaskTest.class)); |
130 |
+ |
suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); |
131 |
+ |
suite.addTest(new TestSuite(PhaserTest.class)); |
132 |
+ |
suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); |
133 |
|
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
134 |
|
suite.addTest(new TestSuite(AbstractQueueTest.class)); |
135 |
|
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
149 |
|
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
150 |
|
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
151 |
|
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
152 |
+ |
suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class)); |
153 |
|
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
154 |
|
suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class)); |
155 |
|
suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class)); |
160 |
|
suite.addTest(new TestSuite(CountDownLatchTest.class)); |
161 |
|
suite.addTest(new TestSuite(CyclicBarrierTest.class)); |
162 |
|
suite.addTest(new TestSuite(DelayQueueTest.class)); |
163 |
+ |
suite.addTest(new TestSuite(EntryTest.class)); |
164 |
|
suite.addTest(new TestSuite(ExchangerTest.class)); |
165 |
|
suite.addTest(new TestSuite(ExecutorsTest.class)); |
166 |
|
suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class)); |
174 |
|
suite.addTest(new TestSuite(ReentrantLockTest.class)); |
175 |
|
suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class)); |
176 |
|
suite.addTest(new TestSuite(ScheduledExecutorTest.class)); |
177 |
+ |
suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class)); |
178 |
|
suite.addTest(new TestSuite(SemaphoreTest.class)); |
179 |
|
suite.addTest(new TestSuite(SynchronousQueueTest.class)); |
180 |
|
suite.addTest(new TestSuite(SystemTest.class)); |
181 |
|
suite.addTest(new TestSuite(ThreadLocalTest.class)); |
182 |
|
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class)); |
183 |
+ |
suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class)); |
184 |
|
suite.addTest(new TestSuite(ThreadTest.class)); |
185 |
|
suite.addTest(new TestSuite(TimeUnitTest.class)); |
186 |
|
suite.addTest(new TestSuite(TreeMapTest.class)); |
210 |
|
/** |
211 |
|
* Sets delays as multiples of SHORT_DELAY. |
212 |
|
*/ |
213 |
< |
protected void setDelays() { |
213 |
> |
protected void setDelays() { |
214 |
|
SHORT_DELAY_MS = getShortDelay(); |
215 |
< |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
215 |
> |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
216 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
217 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
217 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
< |
* Flag set true if any threadAssert methods fail |
221 |
> |
* The first exception encountered if any threadAssertXXX method fails. |
222 |
|
*/ |
223 |
< |
volatile boolean threadFailed; |
223 |
> |
private final AtomicReference<Throwable> threadFailure |
224 |
> |
= new AtomicReference<Throwable>(null); |
225 |
|
|
226 |
|
/** |
227 |
< |
* Initializes test to indicate that no thread assertions have failed |
227 |
> |
* Records an exception so that it can be rethrown later in the test |
228 |
> |
* harness thread, triggering a test case failure. Only the first |
229 |
> |
* failure is recorded; subsequent calls to this method from within |
230 |
> |
* the same test have no effect. |
231 |
|
*/ |
232 |
+ |
public void threadRecordFailure(Throwable t) { |
233 |
+ |
threadFailure.compareAndSet(null, t); |
234 |
+ |
} |
235 |
+ |
|
236 |
|
public void setUp() { |
237 |
|
setDelays(); |
204 |
– |
threadFailed = false; |
238 |
|
} |
239 |
|
|
240 |
|
/** |
241 |
< |
* Triggers test case failure if any thread assertions have failed |
242 |
< |
*/ |
243 |
< |
public void tearDown() { |
244 |
< |
assertFalse(threadFailed); |
241 |
> |
* Triggers test case failure if any thread assertions have failed, |
242 |
> |
* by rethrowing, in the test harness thread, any exception recorded |
243 |
> |
* earlier by threadRecordFailure. |
244 |
> |
*/ |
245 |
> |
public void tearDown() throws Exception { |
246 |
> |
Throwable t = threadFailure.get(); |
247 |
> |
if (t != null) { |
248 |
> |
if (t instanceof Error) |
249 |
> |
throw (Error) t; |
250 |
> |
else if (t instanceof RuntimeException) |
251 |
> |
throw (RuntimeException) t; |
252 |
> |
else if (t instanceof Exception) |
253 |
> |
throw (Exception) t; |
254 |
> |
else |
255 |
> |
throw new AssertionError(t); |
256 |
> |
} |
257 |
|
} |
258 |
|
|
259 |
|
/** |
260 |
< |
* Fail, also setting status to indicate current testcase should fail |
260 |
> |
* Just like fail(reason), but additionally recording (using |
261 |
> |
* threadRecordFailure) any AssertionError thrown, so that the current |
262 |
> |
* testcase will fail. |
263 |
|
*/ |
264 |
|
public void threadFail(String reason) { |
265 |
< |
threadFailed = true; |
266 |
< |
fail(reason); |
265 |
> |
try { |
266 |
> |
fail(reason); |
267 |
> |
} catch (Throwable t) { |
268 |
> |
threadRecordFailure(t); |
269 |
> |
fail(reason); |
270 |
> |
} |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
< |
* If expression not true, set status to indicate current testcase |
275 |
< |
* should fail |
274 |
> |
* Just like assertTrue(b), but additionally recording (using |
275 |
> |
* threadRecordFailure) any AssertionError thrown, so that the current |
276 |
> |
* testcase will fail. |
277 |
|
*/ |
278 |
|
public void threadAssertTrue(boolean b) { |
279 |
< |
if (!b) { |
228 |
< |
threadFailed = true; |
279 |
> |
try { |
280 |
|
assertTrue(b); |
281 |
+ |
} catch (AssertionError t) { |
282 |
+ |
threadRecordFailure(t); |
283 |
+ |
throw t; |
284 |
|
} |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
< |
* If expression not false, set status to indicate current testcase |
289 |
< |
* should fail |
288 |
> |
* Just like assertFalse(b), but additionally recording (using |
289 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
290 |
> |
* current testcase will fail. |
291 |
|
*/ |
292 |
|
public void threadAssertFalse(boolean b) { |
293 |
< |
if (b) { |
239 |
< |
threadFailed = true; |
293 |
> |
try { |
294 |
|
assertFalse(b); |
295 |
+ |
} catch (AssertionError t) { |
296 |
+ |
threadRecordFailure(t); |
297 |
+ |
throw t; |
298 |
|
} |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
< |
* If argument not null, set status to indicate current testcase |
303 |
< |
* should fail |
302 |
> |
* Just like assertNull(x), but additionally recording (using |
303 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
304 |
> |
* current testcase will fail. |
305 |
|
*/ |
306 |
|
public void threadAssertNull(Object x) { |
307 |
< |
if (x != null) { |
250 |
< |
threadFailed = true; |
307 |
> |
try { |
308 |
|
assertNull(x); |
309 |
+ |
} catch (AssertionError t) { |
310 |
+ |
threadRecordFailure(t); |
311 |
+ |
throw t; |
312 |
|
} |
313 |
|
} |
314 |
|
|
315 |
|
/** |
316 |
< |
* If arguments not equal, set status to indicate current testcase |
317 |
< |
* should fail |
316 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
317 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
318 |
> |
* current testcase will fail. |
319 |
|
*/ |
320 |
|
public void threadAssertEquals(long x, long y) { |
321 |
< |
if (x != y) { |
261 |
< |
threadFailed = true; |
321 |
> |
try { |
322 |
|
assertEquals(x, y); |
323 |
+ |
} catch (AssertionError t) { |
324 |
+ |
threadRecordFailure(t); |
325 |
+ |
throw t; |
326 |
|
} |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
< |
* If arguments not equal, set status to indicate current testcase |
331 |
< |
* should fail |
330 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
331 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
332 |
> |
* current testcase will fail. |
333 |
|
*/ |
334 |
|
public void threadAssertEquals(Object x, Object y) { |
335 |
< |
if (x != y && (x == null || !x.equals(y))) { |
272 |
< |
threadFailed = true; |
335 |
> |
try { |
336 |
|
assertEquals(x, y); |
337 |
+ |
} catch (AssertionError t) { |
338 |
+ |
threadRecordFailure(t); |
339 |
+ |
throw t; |
340 |
+ |
} |
341 |
+ |
} |
342 |
+ |
|
343 |
+ |
/** |
344 |
+ |
* Just like assertSame(x, y), but additionally recording (using |
345 |
+ |
* threadRecordFailure) any AssertionError thrown, so that the |
346 |
+ |
* current testcase will fail. |
347 |
+ |
*/ |
348 |
+ |
public void threadAssertSame(Object x, Object y) { |
349 |
+ |
try { |
350 |
+ |
assertSame(x, y); |
351 |
+ |
} catch (AssertionError t) { |
352 |
+ |
threadRecordFailure(t); |
353 |
+ |
throw t; |
354 |
|
} |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
< |
* threadFail with message "should throw exception" |
358 |
> |
* Calls threadFail with message "should throw exception". |
359 |
|
*/ |
360 |
|
public void threadShouldThrow() { |
361 |
< |
threadFailed = true; |
282 |
< |
fail("should throw exception"); |
361 |
> |
threadFail("should throw exception"); |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
< |
* threadFail with message "Unexpected exception" |
365 |
> |
* Calls threadFail with message "should throw" + exceptionName. |
366 |
|
*/ |
367 |
< |
public void threadUnexpectedException() { |
368 |
< |
threadFailed = true; |
290 |
< |
fail("Unexpected exception"); |
367 |
> |
public void threadShouldThrow(String exceptionName) { |
368 |
> |
threadFail("should throw " + exceptionName); |
369 |
|
} |
370 |
|
|
371 |
+ |
/** |
372 |
+ |
* Calls threadFail with message "Unexpected exception" + ex. |
373 |
+ |
*/ |
374 |
+ |
public void threadUnexpectedException(Throwable t) { |
375 |
+ |
threadRecordFailure(t); |
376 |
+ |
t.printStackTrace(); |
377 |
+ |
// Rethrow, wrapping in an AssertionError if necessary |
378 |
+ |
if (t instanceof RuntimeException) |
379 |
+ |
throw (RuntimeException) t; |
380 |
+ |
else if (t instanceof Error) |
381 |
+ |
throw (Error) t; |
382 |
+ |
else { |
383 |
+ |
AssertionError ae = new AssertionError("unexpected exception: " + t); |
384 |
+ |
t.initCause(t); |
385 |
+ |
throw ae; |
386 |
+ |
} |
387 |
+ |
} |
388 |
|
|
389 |
|
/** |
390 |
< |
* Wait out termination of a thread pool or fail doing so |
390 |
> |
* Waits out termination of a thread pool or fails doing so. |
391 |
|
*/ |
392 |
|
public void joinPool(ExecutorService exec) { |
393 |
|
try { |
394 |
|
exec.shutdown(); |
395 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
396 |
< |
} catch(SecurityException ok) { |
395 |
> |
assertTrue("ExecutorService did not terminate in a timely manner", |
396 |
> |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
397 |
> |
} catch (SecurityException ok) { |
398 |
|
// Allowed in case test doesn't have privs |
399 |
< |
} catch(InterruptedException ie) { |
400 |
< |
fail("Unexpected exception"); |
399 |
> |
} catch (InterruptedException ie) { |
400 |
> |
fail("Unexpected InterruptedException"); |
401 |
|
} |
402 |
|
} |
403 |
|
|
404 |
|
|
405 |
|
/** |
406 |
< |
* fail with message "should throw exception" |
406 |
> |
* Fails with message "should throw exception". |
407 |
|
*/ |
408 |
|
public void shouldThrow() { |
409 |
|
fail("Should throw exception"); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* fail with message "Unexpected exception" |
413 |
> |
* Fails with message "should throw " + exceptionName. |
414 |
> |
*/ |
415 |
> |
public void shouldThrow(String exceptionName) { |
416 |
> |
fail("Should throw " + exceptionName); |
417 |
> |
} |
418 |
> |
|
419 |
> |
/** |
420 |
> |
* Fails with message "Unexpected exception: " + ex. |
421 |
|
*/ |
422 |
< |
public void unexpectedException() { |
423 |
< |
fail("Unexpected exception"); |
422 |
> |
public void unexpectedException(Throwable ex) { |
423 |
> |
ex.printStackTrace(); |
424 |
> |
fail("Unexpected exception: " + ex); |
425 |
|
} |
426 |
|
|
427 |
|
|
428 |
|
/** |
429 |
|
* The number of elements to place in collections, arrays, etc. |
430 |
|
*/ |
431 |
< |
static final int SIZE = 20; |
431 |
> |
public static final int SIZE = 20; |
432 |
|
|
433 |
|
// Some convenient Integer constants |
434 |
|
|
435 |
< |
static final Integer zero = new Integer(0); |
436 |
< |
static final Integer one = new Integer(1); |
437 |
< |
static final Integer two = new Integer(2); |
438 |
< |
static final Integer three = new Integer(3); |
439 |
< |
static final Integer four = new Integer(4); |
440 |
< |
static final Integer five = new Integer(5); |
441 |
< |
static final Integer six = new Integer(6); |
442 |
< |
static final Integer seven = new Integer(7); |
443 |
< |
static final Integer eight = new Integer(8); |
444 |
< |
static final Integer nine = new Integer(9); |
445 |
< |
static final Integer m1 = new Integer(-1); |
446 |
< |
static final Integer m2 = new Integer(-2); |
447 |
< |
static final Integer m3 = new Integer(-3); |
448 |
< |
static final Integer m4 = new Integer(-4); |
449 |
< |
static final Integer m5 = new Integer(-5); |
450 |
< |
static final Integer m10 = new Integer(-10); |
435 |
> |
public static final Integer zero = new Integer(0); |
436 |
> |
public static final Integer one = new Integer(1); |
437 |
> |
public static final Integer two = new Integer(2); |
438 |
> |
public static final Integer three = new Integer(3); |
439 |
> |
public static final Integer four = new Integer(4); |
440 |
> |
public static final Integer five = new Integer(5); |
441 |
> |
public static final Integer six = new Integer(6); |
442 |
> |
public static final Integer seven = new Integer(7); |
443 |
> |
public static final Integer eight = new Integer(8); |
444 |
> |
public static final Integer nine = new Integer(9); |
445 |
> |
public static final Integer m1 = new Integer(-1); |
446 |
> |
public static final Integer m2 = new Integer(-2); |
447 |
> |
public static final Integer m3 = new Integer(-3); |
448 |
> |
public static final Integer m4 = new Integer(-4); |
449 |
> |
public static final Integer m5 = new Integer(-5); |
450 |
> |
public static final Integer m6 = new Integer(-6); |
451 |
> |
public static final Integer m10 = new Integer(-10); |
452 |
|
|
453 |
|
|
454 |
|
/** |
455 |
+ |
* Runs Runnable r with a security policy that permits precisely |
456 |
+ |
* the specified permissions. If there is no current security |
457 |
+ |
* manager, the runnable is run twice, both with and without a |
458 |
+ |
* security manager. We require that any security manager permit |
459 |
+ |
* getPolicy/setPolicy. |
460 |
+ |
*/ |
461 |
+ |
public void runWithPermissions(Runnable r, Permission... permissions) { |
462 |
+ |
SecurityManager sm = System.getSecurityManager(); |
463 |
+ |
if (sm == null) { |
464 |
+ |
r.run(); |
465 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
466 |
+ |
try { |
467 |
+ |
Policy.setPolicy(permissivePolicy()); |
468 |
+ |
System.setSecurityManager(new SecurityManager()); |
469 |
+ |
runWithPermissions(r, permissions); |
470 |
+ |
} finally { |
471 |
+ |
System.setSecurityManager(null); |
472 |
+ |
Policy.setPolicy(savedPolicy); |
473 |
+ |
} |
474 |
+ |
} else { |
475 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
476 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(permissions); |
477 |
+ |
Policy.setPolicy(policy); |
478 |
+ |
|
479 |
+ |
try { |
480 |
+ |
r.run(); |
481 |
+ |
} finally { |
482 |
+ |
policy.addPermission(new SecurityPermission("setPolicy")); |
483 |
+ |
Policy.setPolicy(savedPolicy); |
484 |
+ |
} |
485 |
+ |
} |
486 |
+ |
} |
487 |
+ |
|
488 |
+ |
/** |
489 |
+ |
* Runs a runnable without any permissions. |
490 |
+ |
*/ |
491 |
+ |
public void runWithoutPermissions(Runnable r) { |
492 |
+ |
runWithPermissions(r); |
493 |
+ |
} |
494 |
+ |
|
495 |
+ |
/** |
496 |
|
* A security policy where new permissions can be dynamically added |
497 |
|
* or all cleared. |
498 |
|
*/ |
499 |
< |
static class AdjustablePolicy extends java.security.Policy { |
499 |
> |
public static class AdjustablePolicy extends java.security.Policy { |
500 |
|
Permissions perms = new Permissions(); |
501 |
< |
AdjustablePolicy() { } |
501 |
> |
AdjustablePolicy(Permission... permissions) { |
502 |
> |
for (Permission permission : permissions) |
503 |
> |
perms.add(permission); |
504 |
> |
} |
505 |
|
void addPermission(Permission perm) { perms.add(perm); } |
506 |
|
void clearPermissions() { perms = new Permissions(); } |
507 |
< |
public PermissionCollection getPermissions(CodeSource cs) { |
508 |
< |
return perms; |
509 |
< |
} |
510 |
< |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
511 |
< |
return perms; |
512 |
< |
} |
513 |
< |
public boolean implies(ProtectionDomain pd, Permission p) { |
514 |
< |
return perms.implies(p); |
515 |
< |
} |
516 |
< |
public void refresh() {} |
507 |
> |
public PermissionCollection getPermissions(CodeSource cs) { |
508 |
> |
return perms; |
509 |
> |
} |
510 |
> |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
511 |
> |
return perms; |
512 |
> |
} |
513 |
> |
public boolean implies(ProtectionDomain pd, Permission p) { |
514 |
> |
return perms.implies(p); |
515 |
> |
} |
516 |
> |
public void refresh() {} |
517 |
|
} |
518 |
|
|
519 |
+ |
/** |
520 |
+ |
* Returns a policy containing all the permissions we ever need. |
521 |
+ |
*/ |
522 |
+ |
public static Policy permissivePolicy() { |
523 |
+ |
return new AdjustablePolicy |
524 |
+ |
// Permissions j.u.c. needs directly |
525 |
+ |
(new RuntimePermission("modifyThread"), |
526 |
+ |
new RuntimePermission("getClassLoader"), |
527 |
+ |
new RuntimePermission("setContextClassLoader"), |
528 |
+ |
// Permissions needed to change permissions! |
529 |
+ |
new SecurityPermission("getPolicy"), |
530 |
+ |
new SecurityPermission("setPolicy"), |
531 |
+ |
new RuntimePermission("setSecurityManager"), |
532 |
+ |
// Permissions needed by the junit test harness |
533 |
+ |
new RuntimePermission("accessDeclaredMembers"), |
534 |
+ |
new PropertyPermission("*", "read"), |
535 |
+ |
new java.io.FilePermission("<<ALL FILES>>", "read")); |
536 |
+ |
} |
537 |
|
|
538 |
< |
// Some convenient Runnable classes |
539 |
< |
|
540 |
< |
static class NoOpRunnable implements Runnable { |
541 |
< |
public void run() {} |
538 |
> |
/** |
539 |
> |
* Sleep until the timeout has elapsed, or interrupted. |
540 |
> |
* Does <em>NOT</em> throw InterruptedException. |
541 |
> |
*/ |
542 |
> |
void sleepTillInterrupted(long timeoutMillis) { |
543 |
> |
try { |
544 |
> |
Thread.sleep(timeoutMillis); |
545 |
> |
} catch (InterruptedException wakeup) {} |
546 |
|
} |
547 |
|
|
548 |
< |
static class NoOpCallable implements Callable { |
549 |
< |
public Object call() { return Boolean.TRUE; } |
548 |
> |
/** |
549 |
> |
* Returns a new started Thread running the given runnable. |
550 |
> |
*/ |
551 |
> |
Thread newStartedThread(Runnable runnable) { |
552 |
> |
Thread t = new Thread(runnable); |
553 |
> |
t.start(); |
554 |
> |
return t; |
555 |
|
} |
556 |
|
|
557 |
< |
static final String TEST_STRING = "a test string"; |
557 |
> |
// Some convenient Runnable classes |
558 |
|
|
559 |
< |
static class StringTask implements Callable<String> { |
560 |
< |
public String call() { return TEST_STRING; } |
385 |
< |
} |
559 |
> |
public abstract class CheckedRunnable implements Runnable { |
560 |
> |
protected abstract void realRun() throws Throwable; |
561 |
|
|
562 |
< |
static class NPETask implements Callable<String> { |
563 |
< |
public String call() { throw new NullPointerException(); } |
562 |
> |
public final void run() { |
563 |
> |
try { |
564 |
> |
realRun(); |
565 |
> |
} catch (Throwable t) { |
566 |
> |
threadUnexpectedException(t); |
567 |
> |
} |
568 |
> |
} |
569 |
|
} |
570 |
|
|
571 |
< |
static class CallableOne implements Callable<Integer> { |
572 |
< |
public Integer call() { return one; } |
393 |
< |
} |
571 |
> |
public abstract class RunnableShouldThrow implements Runnable { |
572 |
> |
protected abstract void realRun() throws Throwable; |
573 |
|
|
574 |
< |
class ShortRunnable implements Runnable { |
575 |
< |
public void run() { |
574 |
> |
final Class<?> exceptionClass; |
575 |
> |
|
576 |
> |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
577 |
> |
this.exceptionClass = exceptionClass; |
578 |
> |
} |
579 |
> |
|
580 |
> |
public final void run() { |
581 |
|
try { |
582 |
< |
Thread.sleep(SHORT_DELAY_MS); |
583 |
< |
} |
584 |
< |
catch(Exception e) { |
585 |
< |
threadUnexpectedException(); |
582 |
> |
realRun(); |
583 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
584 |
> |
} catch (Throwable t) { |
585 |
> |
if (! exceptionClass.isInstance(t)) |
586 |
> |
threadUnexpectedException(t); |
587 |
|
} |
588 |
|
} |
589 |
|
} |
590 |
|
|
591 |
< |
class ShortInterruptedRunnable implements Runnable { |
592 |
< |
public void run() { |
591 |
> |
public abstract class ThreadShouldThrow extends Thread { |
592 |
> |
protected abstract void realRun() throws Throwable; |
593 |
> |
|
594 |
> |
final Class<?> exceptionClass; |
595 |
> |
|
596 |
> |
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
597 |
> |
this.exceptionClass = exceptionClass; |
598 |
> |
} |
599 |
> |
|
600 |
> |
public final void run() { |
601 |
|
try { |
602 |
< |
Thread.sleep(SHORT_DELAY_MS); |
603 |
< |
threadShouldThrow(); |
604 |
< |
} |
605 |
< |
catch(InterruptedException success) { |
602 |
> |
realRun(); |
603 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
604 |
> |
} catch (Throwable t) { |
605 |
> |
if (! exceptionClass.isInstance(t)) |
606 |
> |
threadUnexpectedException(t); |
607 |
|
} |
608 |
|
} |
609 |
|
} |
610 |
|
|
611 |
< |
class SmallRunnable implements Runnable { |
612 |
< |
public void run() { |
611 |
> |
public abstract class CheckedInterruptedRunnable implements Runnable { |
612 |
> |
protected abstract void realRun() throws Throwable; |
613 |
> |
|
614 |
> |
public final void run() { |
615 |
|
try { |
616 |
< |
Thread.sleep(SMALL_DELAY_MS); |
617 |
< |
} |
618 |
< |
catch(Exception e) { |
619 |
< |
threadUnexpectedException(); |
616 |
> |
realRun(); |
617 |
> |
threadShouldThrow("InterruptedException"); |
618 |
> |
} catch (InterruptedException success) { |
619 |
> |
} catch (Throwable t) { |
620 |
> |
threadUnexpectedException(t); |
621 |
|
} |
622 |
|
} |
623 |
|
} |
624 |
|
|
625 |
< |
class SmallPossiblyInterruptedRunnable implements Runnable { |
626 |
< |
public void run() { |
625 |
> |
public abstract class CheckedCallable<T> implements Callable<T> { |
626 |
> |
protected abstract T realCall() throws Throwable; |
627 |
> |
|
628 |
> |
public final T call() { |
629 |
|
try { |
630 |
< |
Thread.sleep(SMALL_DELAY_MS); |
631 |
< |
} |
632 |
< |
catch(Exception e) { |
630 |
> |
return realCall(); |
631 |
> |
} catch (Throwable t) { |
632 |
> |
threadUnexpectedException(t); |
633 |
> |
return null; |
634 |
|
} |
635 |
|
} |
636 |
|
} |
637 |
|
|
638 |
< |
class SmallCallable implements Callable { |
639 |
< |
public Object call() { |
638 |
> |
public abstract class CheckedInterruptedCallable<T> |
639 |
> |
implements Callable<T> { |
640 |
> |
protected abstract T realCall() throws Throwable; |
641 |
> |
|
642 |
> |
public final T call() { |
643 |
|
try { |
644 |
< |
Thread.sleep(SMALL_DELAY_MS); |
645 |
< |
} |
646 |
< |
catch(Exception e) { |
647 |
< |
threadUnexpectedException(); |
644 |
> |
T result = realCall(); |
645 |
> |
threadShouldThrow("InterruptedException"); |
646 |
> |
return result; |
647 |
> |
} catch (InterruptedException success) { |
648 |
> |
} catch (Throwable t) { |
649 |
> |
threadUnexpectedException(t); |
650 |
|
} |
651 |
< |
return Boolean.TRUE; |
651 |
> |
return null; |
652 |
|
} |
653 |
|
} |
654 |
|
|
655 |
< |
class SmallInterruptedRunnable implements Runnable { |
656 |
< |
public void run() { |
655 |
> |
public static class NoOpRunnable implements Runnable { |
656 |
> |
public void run() {} |
657 |
> |
} |
658 |
> |
|
659 |
> |
public static class NoOpCallable implements Callable { |
660 |
> |
public Object call() { return Boolean.TRUE; } |
661 |
> |
} |
662 |
> |
|
663 |
> |
public static final String TEST_STRING = "a test string"; |
664 |
> |
|
665 |
> |
public static class StringTask implements Callable<String> { |
666 |
> |
public String call() { return TEST_STRING; } |
667 |
> |
} |
668 |
> |
|
669 |
> |
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
670 |
> |
return new CheckedCallable<String>() { |
671 |
> |
public String realCall() { |
672 |
> |
try { |
673 |
> |
latch.await(); |
674 |
> |
} catch (InterruptedException quittingTime) {} |
675 |
> |
return TEST_STRING; |
676 |
> |
}}; |
677 |
> |
} |
678 |
> |
|
679 |
> |
public static class NPETask implements Callable<String> { |
680 |
> |
public String call() { throw new NullPointerException(); } |
681 |
> |
} |
682 |
> |
|
683 |
> |
public static class CallableOne implements Callable<Integer> { |
684 |
> |
public Integer call() { return one; } |
685 |
> |
} |
686 |
> |
|
687 |
> |
public class ShortRunnable extends CheckedRunnable { |
688 |
> |
protected void realRun() throws Throwable { |
689 |
> |
Thread.sleep(SHORT_DELAY_MS); |
690 |
> |
} |
691 |
> |
} |
692 |
> |
|
693 |
> |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
694 |
> |
protected void realRun() throws InterruptedException { |
695 |
> |
Thread.sleep(SHORT_DELAY_MS); |
696 |
> |
} |
697 |
> |
} |
698 |
> |
|
699 |
> |
public class SmallRunnable extends CheckedRunnable { |
700 |
> |
protected void realRun() throws Throwable { |
701 |
> |
Thread.sleep(SMALL_DELAY_MS); |
702 |
> |
} |
703 |
> |
} |
704 |
> |
|
705 |
> |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
706 |
> |
protected void realRun() { |
707 |
|
try { |
708 |
|
Thread.sleep(SMALL_DELAY_MS); |
709 |
< |
threadShouldThrow(); |
710 |
< |
} |
711 |
< |
catch(InterruptedException success) { |
712 |
< |
} |
709 |
> |
} catch (InterruptedException ok) {} |
710 |
> |
} |
711 |
> |
} |
712 |
> |
|
713 |
> |
public class SmallCallable extends CheckedCallable { |
714 |
> |
protected Object realCall() throws InterruptedException { |
715 |
> |
Thread.sleep(SMALL_DELAY_MS); |
716 |
> |
return Boolean.TRUE; |
717 |
|
} |
718 |
|
} |
719 |
|
|
720 |
+ |
public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
721 |
+ |
protected void realRun() throws InterruptedException { |
722 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
723 |
+ |
} |
724 |
+ |
} |
725 |
|
|
726 |
< |
class MediumRunnable implements Runnable { |
727 |
< |
public void run() { |
728 |
< |
try { |
465 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
466 |
< |
} |
467 |
< |
catch(Exception e) { |
468 |
< |
threadUnexpectedException(); |
469 |
< |
} |
726 |
> |
public class MediumRunnable extends CheckedRunnable { |
727 |
> |
protected void realRun() throws Throwable { |
728 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
729 |
|
} |
730 |
|
} |
731 |
|
|
732 |
< |
class MediumInterruptedRunnable implements Runnable { |
733 |
< |
public void run() { |
734 |
< |
try { |
476 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
477 |
< |
threadShouldThrow(); |
478 |
< |
} |
479 |
< |
catch(InterruptedException success) { |
480 |
< |
} |
732 |
> |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
733 |
> |
protected void realRun() throws InterruptedException { |
734 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
735 |
|
} |
736 |
|
} |
737 |
|
|
738 |
< |
class MediumPossiblyInterruptedRunnable implements Runnable { |
739 |
< |
public void run() { |
738 |
> |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
739 |
> |
protected void realRun() { |
740 |
|
try { |
741 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
742 |
< |
} |
489 |
< |
catch(InterruptedException success) { |
490 |
< |
} |
742 |
> |
} catch (InterruptedException ok) {} |
743 |
|
} |
744 |
|
} |
745 |
|
|
746 |
< |
class LongPossiblyInterruptedRunnable implements Runnable { |
747 |
< |
public void run() { |
746 |
> |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
747 |
> |
protected void realRun() { |
748 |
|
try { |
749 |
|
Thread.sleep(LONG_DELAY_MS); |
750 |
< |
} |
499 |
< |
catch(InterruptedException success) { |
500 |
< |
} |
750 |
> |
} catch (InterruptedException ok) {} |
751 |
|
} |
752 |
|
} |
753 |
|
|
754 |
|
/** |
755 |
|
* For use as ThreadFactory in constructors |
756 |
|
*/ |
757 |
< |
static class SimpleThreadFactory implements ThreadFactory{ |
758 |
< |
public Thread newThread(Runnable r){ |
757 |
> |
public static class SimpleThreadFactory implements ThreadFactory { |
758 |
> |
public Thread newThread(Runnable r) { |
759 |
|
return new Thread(r); |
760 |
|
} |
761 |
|
} |
762 |
|
|
763 |
< |
static class TrackedShortRunnable implements Runnable { |
764 |
< |
volatile boolean done = false; |
763 |
> |
public static class TrackedShortRunnable implements Runnable { |
764 |
> |
public volatile boolean done = false; |
765 |
|
public void run() { |
766 |
|
try { |
767 |
|
Thread.sleep(SMALL_DELAY_MS); |
768 |
|
done = true; |
769 |
< |
} catch(Exception e){ |
520 |
< |
} |
769 |
> |
} catch (InterruptedException ok) {} |
770 |
|
} |
771 |
|
} |
772 |
|
|
773 |
< |
static class TrackedMediumRunnable implements Runnable { |
774 |
< |
volatile boolean done = false; |
773 |
> |
public static class TrackedMediumRunnable implements Runnable { |
774 |
> |
public volatile boolean done = false; |
775 |
|
public void run() { |
776 |
|
try { |
777 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
778 |
|
done = true; |
779 |
< |
} catch(Exception e){ |
531 |
< |
} |
779 |
> |
} catch (InterruptedException ok) {} |
780 |
|
} |
781 |
|
} |
782 |
|
|
783 |
< |
static class TrackedLongRunnable implements Runnable { |
784 |
< |
volatile boolean done = false; |
783 |
> |
public static class TrackedLongRunnable implements Runnable { |
784 |
> |
public volatile boolean done = false; |
785 |
|
public void run() { |
786 |
|
try { |
787 |
|
Thread.sleep(LONG_DELAY_MS); |
788 |
|
done = true; |
789 |
< |
} catch(Exception e){ |
542 |
< |
} |
789 |
> |
} catch (InterruptedException ok) {} |
790 |
|
} |
791 |
|
} |
792 |
|
|
793 |
< |
static class TrackedNoOpRunnable implements Runnable { |
794 |
< |
volatile boolean done = false; |
793 |
> |
public static class TrackedNoOpRunnable implements Runnable { |
794 |
> |
public volatile boolean done = false; |
795 |
|
public void run() { |
796 |
|
done = true; |
797 |
|
} |
798 |
|
} |
799 |
|
|
800 |
< |
static class TrackedCallable implements Callable { |
801 |
< |
volatile boolean done = false; |
800 |
> |
public static class TrackedCallable implements Callable { |
801 |
> |
public volatile boolean done = false; |
802 |
|
public Object call() { |
803 |
|
try { |
804 |
|
Thread.sleep(SMALL_DELAY_MS); |
805 |
|
done = true; |
806 |
< |
} catch(Exception e){ |
560 |
< |
} |
806 |
> |
} catch (InterruptedException ok) {} |
807 |
|
return Boolean.TRUE; |
808 |
|
} |
809 |
|
} |
810 |
|
|
811 |
+ |
/** |
812 |
+ |
* Analog of CheckedRunnable for RecursiveAction |
813 |
+ |
*/ |
814 |
+ |
public abstract class CheckedRecursiveAction extends RecursiveAction { |
815 |
+ |
protected abstract void realCompute() throws Throwable; |
816 |
+ |
|
817 |
+ |
public final void compute() { |
818 |
+ |
try { |
819 |
+ |
realCompute(); |
820 |
+ |
} catch (Throwable t) { |
821 |
+ |
threadUnexpectedException(t); |
822 |
+ |
} |
823 |
+ |
} |
824 |
+ |
} |
825 |
|
|
826 |
|
/** |
827 |
< |
* For use as RejectedExecutionHandler in constructors |
827 |
> |
* Analog of CheckedCallable for RecursiveTask |
828 |
|
*/ |
829 |
< |
static class NoOpREHandler implements RejectedExecutionHandler{ |
830 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
829 |
> |
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
830 |
> |
protected abstract T realCompute() throws Throwable; |
831 |
> |
|
832 |
> |
public final T compute() { |
833 |
> |
try { |
834 |
> |
return realCompute(); |
835 |
> |
} catch (Throwable t) { |
836 |
> |
threadUnexpectedException(t); |
837 |
> |
return null; |
838 |
> |
} |
839 |
> |
} |
840 |
|
} |
841 |
|
|
842 |
+ |
/** |
843 |
+ |
* For use as RejectedExecutionHandler in constructors |
844 |
+ |
*/ |
845 |
+ |
public static class NoOpREHandler implements RejectedExecutionHandler { |
846 |
+ |
public void rejectedExecution(Runnable r, |
847 |
+ |
ThreadPoolExecutor executor) {} |
848 |
+ |
} |
849 |
|
|
850 |
|
} |