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