ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PrivilegedFutureTaskTest.java
Revision: 1.2
Committed: Sat Oct 25 16:04:17 2003 UTC (20 years, 6 months ago) by dl
Branch: MAIN
Changes since 1.1: +2 -2 lines
Log Message:
Fixed typo

File Contents

# Content
1 /*
2 * Written by members of JCP JSR-166 Expert Group and released to the
3 * public domain. Use, modify, and redistribute this code in any way
4 * without acknowledgement. Other contributors include Andrew Wright,
5 * Jeffrey Hayes, Pat Fischer, Mike Judd.
6 */
7
8 import junit.framework.*;
9 import java.util.concurrent.*;
10 import java.util.*;
11 import java.security.*;
12
13 public class PrivilegedFutureTaskTest extends JSR166TestCase {
14
15 public static void main(String[] args) {
16 junit.textui.TestRunner.run (suite());
17 }
18 public static Test suite() {
19 return new TestSuite(PrivilegedFutureTaskTest.class);
20 }
21
22 /**
23 * Subclass to expose protected methods
24 */
25 static class PublicPrivilegedFutureTask extends PrivilegedFutureTask {
26 public PublicPrivilegedFutureTask(Callable r) { super(r); }
27 public PublicPrivilegedFutureTask(Callable r, ClassLoader ccl, AccessControlContext acc) { super(r, ccl, acc); }
28 public boolean reset() { return super.reset(); }
29 public void setCancelled() { super.setCancelled(); }
30 public void setDone() { super.setDone(); }
31 public void set(Object x) { super.set(x); }
32 public void setException(Throwable t) { super.setException(t); }
33 }
34
35 /**
36 * Creating a future with a null callable throws NPE
37 */
38 public void testConstructor() {
39 try {
40 PrivilegedFutureTask task = new PrivilegedFutureTask(null);
41 shouldThrow();
42 }
43 catch(NullPointerException success) {
44 }
45 }
46
47 /**
48 * isDone is true when a task completes
49 */
50 public void testIsDone() {
51 PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable(), null, null);
52 task.run();
53 assertTrue(task.isDone());
54 assertFalse(task.isCancelled());
55 }
56
57 /**
58 * reset of a done task succeeds and changes status to not done
59 */
60 public void testReset() {
61 PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null);
62 task.run();
63 assertTrue(task.isDone());
64 assertTrue(task.reset());
65 assertFalse(task.isDone());
66 }
67
68 /**
69 * Resetting after cancellation fails
70 */
71 public void testResetAfterCancel() {
72 PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null);
73 assertTrue(task.cancel(false));
74 task.run();
75 assertTrue(task.isDone());
76 assertTrue(task.isCancelled());
77 assertFalse(task.reset());
78 }
79
80 /**
81 * setDone of new task causes isDone to be true
82 */
83 public void testSetDone() {
84 PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null);
85 task.setDone();
86 assertTrue(task.isDone());
87 assertFalse(task.isCancelled());
88 }
89
90 /**
91 * setCancelled of a new task causes isCancelled to be true
92 */
93 public void testSetCancelled() {
94 PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null);
95 assertTrue(task.cancel(false));
96 task.setCancelled();
97 assertTrue(task.isDone());
98 assertTrue(task.isCancelled());
99 }
100
101 /**
102 * setting value gauses get to return it
103 */
104 public void testSet() {
105 PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null);
106 task.set(one);
107 try {
108 assertEquals(task.get(), one);
109 }
110 catch(Exception e) {
111 unexpectedException();
112 }
113 }
114
115 /**
116 * setException causes get to throw ExecutionException
117 */
118 public void testSetException() {
119 Exception nse = new NoSuchElementException();
120 PublicPrivilegedFutureTask task = new PublicPrivilegedFutureTask(new NoOpCallable(), null, null);
121 task.setException(nse);
122 try {
123 Object x = task.get();
124 shouldThrow();
125 }
126 catch(ExecutionException ee) {
127 Throwable cause = ee.getCause();
128 assertEquals(cause, nse);
129 }
130 catch(Exception e) {
131 unexpectedException();
132 }
133 }
134
135 /**
136 * Cancelling before running succeeds
137 */
138 public void testCancelBeforeRun() {
139 PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable(), null, null);
140 assertTrue(task.cancel(false));
141 task.run();
142 assertTrue(task.isDone());
143 assertTrue(task.isCancelled());
144 }
145
146 /**
147 * Cancel(true) before run succeeds
148 */
149 public void testCancelBeforeRun2() {
150 PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable(), null, null);
151 assertTrue(task.cancel(true));
152 task.run();
153 assertTrue(task.isDone());
154 assertTrue(task.isCancelled());
155 }
156
157 /**
158 * cancel of a completed task fails
159 */
160 public void testCancelAfterRun() {
161 PrivilegedFutureTask task = new PrivilegedFutureTask( new NoOpCallable(), null, null);
162 task.run();
163 assertFalse(task.cancel(false));
164 assertTrue(task.isDone());
165 assertFalse(task.isCancelled());
166 }
167
168 /**
169 * cancel(true) interrupts a running task
170 */
171 public void testCancelInterrupt() {
172 PrivilegedFutureTask task = new PrivilegedFutureTask( new Callable() {
173 public Object call() {
174 try {
175 Thread.sleep(MEDIUM_DELAY_MS);
176 threadShouldThrow();
177 }
178 catch (InterruptedException success) {}
179 return Boolean.TRUE;
180 } }, null, null);
181 Thread t = new Thread(task);
182 t.start();
183
184 try {
185 Thread.sleep(SHORT_DELAY_MS);
186 assertTrue(task.cancel(true));
187 t.join();
188 assertTrue(task.isDone());
189 assertTrue(task.isCancelled());
190 } catch(InterruptedException e){
191 unexpectedException();
192 }
193 }
194
195
196 /**
197 * cancel(false) does not interrupt a running task
198 */
199 public void testCancelNoInterrupt() {
200 PrivilegedFutureTask task = new PrivilegedFutureTask( new Callable() {
201 public Object call() {
202 try {
203 Thread.sleep(MEDIUM_DELAY_MS);
204 }
205 catch (InterruptedException success) {
206 threadFail("should not interrupt");
207 }
208 return Boolean.TRUE;
209 } }, null, null);
210 Thread t = new Thread(task);
211 t.start();
212
213 try {
214 Thread.sleep(SHORT_DELAY_MS);
215 assertTrue(task.cancel(false));
216 t.join();
217 assertTrue(task.isDone());
218 assertTrue(task.isCancelled());
219 } catch(InterruptedException e){
220 unexpectedException();
221 }
222 }
223
224 /**
225 * set in one thread causes get in another thread to retrieve value
226 */
227 public void testGet1() {
228 final PrivilegedFutureTask ft = new PrivilegedFutureTask(new Callable() {
229 public Object call() {
230 try {
231 Thread.sleep(MEDIUM_DELAY_MS);
232 } catch(InterruptedException e){
233 threadUnexpectedException();
234 }
235 return Boolean.TRUE;
236 }
237 }, null, null);
238 Thread t = new Thread(new Runnable() {
239 public void run() {
240 try {
241 ft.get();
242 } catch(Exception e){
243 threadUnexpectedException();
244 }
245 }
246 });
247 try {
248 assertFalse(ft.isDone());
249 assertFalse(ft.isCancelled());
250 t.start();
251 Thread.sleep(SHORT_DELAY_MS);
252 ft.run();
253 t.join();
254 assertTrue(ft.isDone());
255 assertFalse(ft.isCancelled());
256 } catch(InterruptedException e){
257 unexpectedException();
258
259 }
260 }
261
262 /**
263 * set in one thread causes timed get in another thread to retrieve value
264 */
265 public void testTimedGet1() {
266 final PrivilegedFutureTask ft = new PrivilegedFutureTask(new Callable() {
267 public Object call() {
268 try {
269 Thread.sleep(MEDIUM_DELAY_MS);
270 } catch(InterruptedException e){
271 threadUnexpectedException();
272 }
273 return Boolean.TRUE;
274 }
275 }, null, null);
276 Thread t = new Thread(new Runnable() {
277 public void run() {
278 try {
279 ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
280 } catch(TimeoutException success) {
281 } catch(Exception e){
282 threadUnexpectedException();
283 }
284 }
285 });
286 try {
287 assertFalse(ft.isDone());
288 assertFalse(ft.isCancelled());
289 t.start();
290 ft.run();
291 t.join();
292 assertTrue(ft.isDone());
293 assertFalse(ft.isCancelled());
294 } catch(InterruptedException e){
295 unexpectedException();
296
297 }
298 }
299
300 /**
301 * Cancelling a task causes timed get in another thread to throw CancellationException
302 */
303 public void testTimedGet_Cancellation() {
304 final PrivilegedFutureTask ft = new PrivilegedFutureTask(new Callable() {
305 public Object call() {
306 try {
307 Thread.sleep(SMALL_DELAY_MS);
308 threadShouldThrow();
309 } catch(InterruptedException e) {
310 }
311 return Boolean.TRUE;
312 }
313 }, null, null);
314 try {
315 Thread t1 = new Thread(new Runnable() {
316 public void run() {
317 try {
318 ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
319 threadShouldThrow();
320 } catch(CancellationException success) {}
321 catch(Exception e){
322 threadUnexpectedException();
323 }
324 }
325 });
326 Thread t2 = new Thread(ft);
327 t1.start();
328 t2.start();
329 Thread.sleep(SHORT_DELAY_MS);
330 ft.cancel(true);
331 t1.join();
332 t2.join();
333 } catch(InterruptedException ie){
334 unexpectedException();
335 }
336 }
337
338 /**
339 * Cancelling a task causes get in another thread to throw CancellationException
340 */
341 public void testGet_Cancellation() {
342 final PrivilegedFutureTask ft = new PrivilegedFutureTask(new Callable() {
343 public Object call() {
344 try {
345 Thread.sleep(MEDIUM_DELAY_MS);
346 threadShouldThrow();
347 } catch(InterruptedException e){
348 }
349 return Boolean.TRUE;
350 }
351 }, null, null);
352 try {
353 Thread t1 = new Thread(new Runnable() {
354 public void run() {
355 try {
356 ft.get();
357 threadShouldThrow();
358 } catch(CancellationException success){
359 }
360 catch(Exception e){
361 threadUnexpectedException();
362 }
363 }
364 });
365 Thread t2 = new Thread(ft);
366 t1.start();
367 t2.start();
368 Thread.sleep(SHORT_DELAY_MS);
369 ft.cancel(true);
370 t1.join();
371 t2.join();
372 } catch(InterruptedException success){
373 unexpectedException();
374 }
375 }
376
377
378 /**
379 * A runtime exception in task causes get to throw ExecutionException
380 */
381 public void testGet_ExecutionException() {
382 final PrivilegedFutureTask ft = new PrivilegedFutureTask(new Callable() {
383 public Object call() {
384 int i = 5/0;
385 return Boolean.TRUE;
386 }
387 }, null, null);
388 try {
389 ft.run();
390 ft.get();
391 shouldThrow();
392 } catch(ExecutionException success){
393 }
394 catch(Exception e){
395 unexpectedException();
396 }
397 }
398
399 /**
400 * A runtime exception in task causes timed get to throw ExecutionException
401 */
402 public void testTimedGet_ExecutionException2() {
403 final PrivilegedFutureTask ft = new PrivilegedFutureTask(new Callable() {
404 public Object call() {
405 int i = 5/0;
406 return Boolean.TRUE;
407 }
408 }, null, null);
409 try {
410 ft.run();
411 ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
412 shouldThrow();
413 } catch(ExecutionException success) {
414 } catch(TimeoutException success) { } // unlikely but OK
415 catch(Exception e){
416 unexpectedException();
417 }
418 }
419
420
421 /**
422 * Interrupting a waiting get causes it to throw InterruptedException
423 */
424 public void testGet_InterruptedException() {
425 final PrivilegedFutureTask ft = new PrivilegedFutureTask(new NoOpCallable(), null, null);
426 Thread t = new Thread(new Runnable() {
427 public void run() {
428 try {
429 ft.get();
430 threadShouldThrow();
431 } catch(InterruptedException success){
432 } catch(Exception e){
433 threadUnexpectedException();
434 }
435 }
436 });
437 try {
438 t.start();
439 Thread.sleep(SHORT_DELAY_MS);
440 t.interrupt();
441 t.join();
442 } catch(Exception e){
443 unexpectedException();
444 }
445 }
446
447 /**
448 * Interrupting a waiting timed get causes it to throw InterruptedException
449 */
450 public void testTimedGet_InterruptedException2() {
451 final PrivilegedFutureTask ft = new PrivilegedFutureTask(new NoOpCallable(), null, null);
452 Thread t = new Thread(new Runnable() {
453 public void run() {
454 try {
455 ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS);
456 threadShouldThrow();
457 } catch(InterruptedException success){}
458 catch(Exception e){
459 threadUnexpectedException();
460 }
461 }
462 });
463 try {
464 t.start();
465 Thread.sleep(SHORT_DELAY_MS);
466 t.interrupt();
467 t.join();
468 } catch(Exception e){
469 unexpectedException();
470 }
471 }
472
473 /**
474 * A timed out timed get throws TimeoutException
475 */
476 public void testGet_TimeoutException() {
477 try {
478 PrivilegedFutureTask ft = new PrivilegedFutureTask(new NoOpCallable(), null, null);
479 ft.get(1,TimeUnit.MILLISECONDS);
480 shouldThrow();
481 } catch(TimeoutException success){}
482 catch(Exception success){
483 unexpectedException();
484 }
485 }
486
487
488 /**
489 * Without privileges, run with default context throws ACE
490 */
491 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() {
505 AdjustablePolicy policy = new AdjustablePolicy();
506 policy.addPermission(new RuntimePermission("getContextClassLoader"));
507 policy.addPermission(new RuntimePermission("setContextClassLoader"));
508 Policy.setPolicy(policy);
509 System.setSecurityManager(new SecurityManager());
510
511 try {
512 PrivilegedFutureTask task = new PrivilegedFutureTask(new NoOpCallable());
513 task.run();
514 assertTrue(task.isDone());
515 } catch(Exception e) {
516 unexpectedException();
517
518 }
519 }
520
521
522 }