7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
– |
import java.security.Permission; |
10 |
|
import java.util.concurrent.Callable; |
11 |
|
import java.util.concurrent.CancellationException; |
12 |
|
import java.util.concurrent.CountDownLatch; |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
< |
* cancel(true) interrupts a running task that subsequently |
409 |
< |
* succeeds, with a security manager that does not permit |
410 |
< |
* Thread.interrupt |
408 |
> |
* cancel(true) tries to interrupt a running task, but |
409 |
> |
* Thread.interrupt throws (simulating a restrictive security |
410 |
> |
* manager) |
411 |
|
*/ |
412 |
|
public void testCancelInterrupt_ThrowsSecurityException() { |
414 |
– |
if (System.getSecurityManager() != null) |
415 |
– |
return; |
416 |
– |
|
413 |
|
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
414 |
|
final CountDownLatch cancelled = new CountDownLatch(1); |
415 |
|
final PublicFutureTask task = |
420 |
|
assertFalse(Thread.interrupted()); |
421 |
|
}}); |
422 |
|
|
423 |
< |
final Thread t = newStartedThread(task); |
423 |
> |
final Thread t = new Thread(task) { |
424 |
> |
// Simulate a restrictive security manager. |
425 |
> |
@Override public void interrupt() { |
426 |
> |
throw new SecurityException(); |
427 |
> |
}}; |
428 |
> |
t.setDaemon(true); |
429 |
> |
t.start(); |
430 |
> |
|
431 |
|
await(pleaseCancel); |
432 |
< |
System.setSecurityManager(new SecurityManager() { |
433 |
< |
public void checkAccess(Thread t) { throw new SecurityException(); } |
434 |
< |
public void checkPermission(Permission p) {}}); |
435 |
< |
try { |
436 |
< |
try { |
437 |
< |
task.cancel(true); |
438 |
< |
shouldThrow(); |
436 |
< |
} |
437 |
< |
catch (SecurityException expected) {} |
438 |
< |
} finally { |
439 |
< |
System.setSecurityManager(null); |
440 |
< |
} |
432 |
> |
try { |
433 |
> |
task.cancel(true); |
434 |
> |
shouldThrow(); |
435 |
> |
} catch (SecurityException expected) {} |
436 |
> |
|
437 |
> |
// We failed to deliver the interrupt, but the world retains |
438 |
> |
// its sanity, as if we had done task.cancel(false) |
439 |
|
assertTrue(task.isCancelled()); |
440 |
|
assertTrue(task.isDone()); |
441 |
|
assertEquals(1, task.runCount()); |