19 |
|
return new TestSuite(PrivilegedFutureTaskTest.class); |
20 |
|
} |
21 |
|
|
22 |
+ |
Policy savedPolicy; |
23 |
+ |
|
24 |
+ |
/** |
25 |
+ |
* Establish permissions for get/set for contextClassLoader for each test |
26 |
+ |
*/ |
27 |
+ |
public void setUp() { |
28 |
+ |
super.setUp(); |
29 |
+ |
savedPolicy = Policy.getPolicy(); |
30 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
31 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
32 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
33 |
+ |
Policy.setPolicy(policy); |
34 |
+ |
// System.setSecurityManager(new SecurityManager()); |
35 |
+ |
} |
36 |
+ |
|
37 |
+ |
public void tearDown() { |
38 |
+ |
Policy.setPolicy(savedPolicy); |
39 |
+ |
super.tearDown(); |
40 |
+ |
} |
41 |
+ |
|
42 |
|
/** |
43 |
|
* Subclass to expose protected methods |
44 |
|
*/ |
45 |
|
static class PublicPrivilegedFutureTask extends PrivilegedFutureTask { |
46 |
|
public PublicPrivilegedFutureTask(Callable r) { super(r); } |
27 |
– |
public PublicPrivilegedFutureTask(Callable r, ClassLoader ccl, AccessControlContext acc) { super(r, ccl, acc); } |
47 |
|
public boolean reset() { return super.reset(); } |
48 |
|
public void setCancelled() { super.setCancelled(); } |
49 |
|
public void setDone() { super.setDone(); } |
67 |
|
* isDone is true when a task completes |
68 |
|
*/ |
69 |
|
public void testIsDone() { |
70 |
< |
PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable(), null, null); |
70 |
> |
PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable()); |
71 |
|
task.run(); |
72 |
|
assertTrue(task.isDone()); |
73 |
|
assertFalse(task.isCancelled()); |
77 |
|
* reset of a done task succeeds and changes status to not done |
78 |
|
*/ |
79 |
|
public void testReset() { |
80 |
< |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null); |
80 |
> |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable()); |
81 |
|
task.run(); |
82 |
|
assertTrue(task.isDone()); |
83 |
|
assertTrue(task.reset()); |
88 |
|
* Resetting after cancellation fails |
89 |
|
*/ |
90 |
|
public void testResetAfterCancel() { |
91 |
< |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null); |
91 |
> |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable()); |
92 |
|
assertTrue(task.cancel(false)); |
93 |
|
task.run(); |
94 |
|
assertTrue(task.isDone()); |
100 |
|
* setDone of new task causes isDone to be true |
101 |
|
*/ |
102 |
|
public void testSetDone() { |
103 |
< |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null); |
103 |
> |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable()); |
104 |
|
task.setDone(); |
105 |
|
assertTrue(task.isDone()); |
106 |
|
assertFalse(task.isCancelled()); |
110 |
|
* setCancelled of a new task causes isCancelled to be true |
111 |
|
*/ |
112 |
|
public void testSetCancelled() { |
113 |
< |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null); |
113 |
> |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable()); |
114 |
|
assertTrue(task.cancel(false)); |
115 |
|
task.setCancelled(); |
116 |
|
assertTrue(task.isDone()); |
121 |
|
* setting value gauses get to return it |
122 |
|
*/ |
123 |
|
public void testSet() { |
124 |
< |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null); |
124 |
> |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable()); |
125 |
|
task.set(one); |
126 |
|
try { |
127 |
|
assertEquals(task.get(), one); |
136 |
|
*/ |
137 |
|
public void testSetException() { |
138 |
|
Exception nse = new NoSuchElementException(); |
139 |
< |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null); |
139 |
> |
PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable()); |
140 |
|
task.setException(nse); |
141 |
|
try { |
142 |
|
Object x = task.get(); |
155 |
|
* Cancelling before running succeeds |
156 |
|
*/ |
157 |
|
public void testCancelBeforeRun() { |
158 |
< |
PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable(), null, null); |
158 |
> |
PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable()); |
159 |
|
assertTrue(task.cancel(false)); |
160 |
|
task.run(); |
161 |
|
assertTrue(task.isDone()); |
166 |
|
* Cancel(true) before run succeeds |
167 |
|
*/ |
168 |
|
public void testCancelBeforeRun2() { |
169 |
< |
PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable(), null, null); |
169 |
> |
PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable()); |
170 |
|
assertTrue(task.cancel(true)); |
171 |
|
task.run(); |
172 |
|
assertTrue(task.isDone()); |
177 |
|
* cancel of a completed task fails |
178 |
|
*/ |
179 |
|
public void testCancelAfterRun() { |
180 |
< |
PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable(), null, null); |
180 |
> |
PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable()); |
181 |
|
task.run(); |
182 |
|
assertFalse(task.cancel(false)); |
183 |
|
assertTrue(task.isDone()); |
196 |
|
} |
197 |
|
catch (InterruptedException success) {} |
198 |
|
return Boolean.TRUE; |
199 |
< |
} }, null, null); |
199 |
> |
} }); |
200 |
|
Thread t = new Thread(task); |
201 |
|
t.start(); |
202 |
|
|
225 |
|
threadFail("should not interrupt"); |
226 |
|
} |
227 |
|
return Boolean.TRUE; |
228 |
< |
} }, null, null); |
228 |
> |
} }); |
229 |
|
Thread t = new Thread(task); |
230 |
|
t.start(); |
231 |
|
|
253 |
|
} |
254 |
|
return Boolean.TRUE; |
255 |
|
} |
256 |
< |
}, null, null); |
256 |
> |
}); |
257 |
|
Thread t = new Thread(new Runnable() { |
258 |
|
public void run() { |
259 |
|
try { |
291 |
|
} |
292 |
|
return Boolean.TRUE; |
293 |
|
} |
294 |
< |
}, null, null); |
294 |
> |
}); |
295 |
|
Thread t = new Thread(new Runnable() { |
296 |
|
public void run() { |
297 |
|
try { |
329 |
|
} |
330 |
|
return Boolean.TRUE; |
331 |
|
} |
332 |
< |
}, null, null); |
332 |
> |
}); |
333 |
|
try { |
334 |
|
Thread t1 = new Thread(new Runnable() { |
335 |
|
public void run() { |
367 |
|
} |
368 |
|
return Boolean.TRUE; |
369 |
|
} |
370 |
< |
}, null, null); |
370 |
> |
}); |
371 |
|
try { |
372 |
|
Thread t1 = new Thread(new Runnable() { |
373 |
|
public void run() { |
403 |
|
int i = 5/0; |
404 |
|
return Boolean.TRUE; |
405 |
|
} |
406 |
< |
}, null, null); |
406 |
> |
}); |
407 |
|
try { |
408 |
|
ft.run(); |
409 |
|
ft.get(); |
424 |
|
int i = 5/0; |
425 |
|
return Boolean.TRUE; |
426 |
|
} |
427 |
< |
}, null, null); |
427 |
> |
}); |
428 |
|
try { |
429 |
|
ft.run(); |
430 |
|
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
441 |
|
* Interrupting a waiting get causes it to throw InterruptedException |
442 |
|
*/ |
443 |
|
public void testGet_InterruptedException() { |
444 |
< |
final PrivilegedFutureTask ft = new PrivilegedFutureTask(new NoOpCallable(), null, null); |
444 |
> |
final PrivilegedFutureTask ft = new PrivilegedFutureTask(new NoOpCallable()); |
445 |
|
Thread t = new Thread(new Runnable() { |
446 |
|
public void run() { |
447 |
|
try { |
467 |
|
* Interrupting a waiting timed get causes it to throw InterruptedException |
468 |
|
*/ |
469 |
|
public void testTimedGet_InterruptedException2() { |
470 |
< |
final PrivilegedFutureTask ft = new PrivilegedFutureTask(new NoOpCallable(), null, null); |
470 |
> |
final PrivilegedFutureTask ft = new PrivilegedFutureTask(new NoOpCallable()); |
471 |
|
Thread t = new Thread(new Runnable() { |
472 |
|
public void run() { |
473 |
|
try { |
494 |
|
*/ |
495 |
|
public void testGet_TimeoutException() { |
496 |
|
try { |
497 |
< |
PrivilegedFutureTask ft = new PrivilegedFutureTask(new NoOpCallable(), null, null); |
497 |
> |
PrivilegedFutureTask ft = new PrivilegedFutureTask(new NoOpCallable()); |
498 |
|
ft.get(1,TimeUnit.MILLISECONDS); |
499 |
|
shouldThrow(); |
500 |
|
} catch(TimeoutException success){} |
503 |
|
} |
504 |
|
} |
505 |
|
|
487 |
– |
|
506 |
|
/** |
507 |
|
* Without privileges, run with default context throws ACE |
508 |
|
*/ |
509 |
|
public void testRunWithNoPrivs() { |
492 |
– |
try { |
493 |
– |
PrivilegedFutureTask task = new PrivilegedFutureTask(new NoOpCallable()); |
494 |
– |
task.run(); |
495 |
– |
shouldThrow(); |
496 |
– |
} catch(AccessControlException success) { |
497 |
– |
} |
498 |
– |
} |
499 |
– |
|
500 |
– |
|
501 |
– |
/** |
502 |
– |
* With ContextClassLoader privileges, run with default context succeeds |
503 |
– |
*/ |
504 |
– |
public void testRunWithContextClassLoaderPermissions() { |
510 |
|
AdjustablePolicy policy = new AdjustablePolicy(); |
506 |
– |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
507 |
– |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
511 |
|
Policy.setPolicy(policy); |
509 |
– |
System.setSecurityManager(new SecurityManager()); |
510 |
– |
|
512 |
|
try { |
513 |
|
PrivilegedFutureTask task = new PrivilegedFutureTask(new NoOpCallable()); |
514 |
|
task.run(); |
515 |
< |
assertTrue(task.isDone()); |
516 |
< |
} catch(Exception e) { |
516 |
< |
unexpectedException(); |
517 |
< |
|
515 |
> |
shouldThrow(); |
516 |
> |
} catch(AccessControlException success) { |
517 |
|
} |
518 |
|
} |
520 |
– |
|
519 |
|
|
520 |
|
} |