30 |
|
public void setException(Throwable t) { super.setException(t); } |
31 |
|
} |
32 |
|
|
33 |
< |
public void testConstructor(){ |
33 |
> |
/** |
34 |
> |
* |
35 |
> |
*/ |
36 |
> |
public void testConstructor() { |
37 |
|
try { |
38 |
|
FutureTask task = new FutureTask(null); |
39 |
< |
fail("should throw"); |
39 |
> |
shouldThrow(); |
40 |
|
} |
41 |
|
catch(NullPointerException success) { |
42 |
|
} |
43 |
|
} |
44 |
|
|
45 |
< |
public void testConstructor2(){ |
45 |
> |
/** |
46 |
> |
* |
47 |
> |
*/ |
48 |
> |
public void testConstructor2() { |
49 |
|
try { |
50 |
|
FutureTask task = new FutureTask(null, Boolean.TRUE); |
51 |
< |
fail("should throw"); |
51 |
> |
shouldThrow(); |
52 |
|
} |
53 |
|
catch(NullPointerException success) { |
54 |
|
} |
55 |
|
} |
56 |
|
|
57 |
< |
public void testIsDone(){ |
57 |
> |
/** |
58 |
> |
* |
59 |
> |
*/ |
60 |
> |
public void testIsDone() { |
61 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
62 |
|
task.run(); |
63 |
|
assertTrue(task.isDone()); |
64 |
|
assertFalse(task.isCancelled()); |
65 |
|
} |
66 |
|
|
67 |
< |
public void testReset(){ |
67 |
> |
/** |
68 |
> |
* |
69 |
> |
*/ |
70 |
> |
public void testReset() { |
71 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
72 |
|
task.run(); |
73 |
|
assertTrue(task.isDone()); |
74 |
|
assertTrue(task.reset()); |
75 |
|
} |
76 |
|
|
77 |
+ |
/** |
78 |
+ |
* |
79 |
+ |
*/ |
80 |
|
public void testResetAfterCancel() { |
81 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
82 |
|
assertTrue(task.cancel(false)); |
86 |
|
assertFalse(task.reset()); |
87 |
|
} |
88 |
|
|
89 |
+ |
/** |
90 |
+ |
* |
91 |
+ |
*/ |
92 |
|
public void testSetDone() { |
93 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
94 |
|
task.setDone(); |
96 |
|
assertFalse(task.isCancelled()); |
97 |
|
} |
98 |
|
|
99 |
+ |
/** |
100 |
+ |
* |
101 |
+ |
*/ |
102 |
|
public void testSetCancelled() { |
103 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
104 |
|
assertTrue(task.cancel(false)); |
107 |
|
assertTrue(task.isCancelled()); |
108 |
|
} |
109 |
|
|
110 |
+ |
/** |
111 |
+ |
* |
112 |
+ |
*/ |
113 |
|
public void testSet() { |
114 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
115 |
|
task.set(one); |
117 |
|
assertEquals(task.get(), one); |
118 |
|
} |
119 |
|
catch(Exception e) { |
120 |
< |
fail("unexpected exception"); |
120 |
> |
unexpectedException(); |
121 |
|
} |
122 |
|
} |
123 |
|
|
124 |
+ |
/** |
125 |
+ |
* |
126 |
+ |
*/ |
127 |
|
public void testSetException() { |
128 |
|
Exception nse = new NoSuchElementException(); |
129 |
|
MyFutureTask task = new MyFutureTask(new NoOpCallable()); |
130 |
|
task.setException(nse); |
131 |
|
try { |
132 |
|
Object x = task.get(); |
133 |
< |
fail("should throw"); |
133 |
> |
shouldThrow(); |
134 |
|
} |
135 |
|
catch(ExecutionException ee) { |
136 |
|
Throwable cause = ee.getCause(); |
137 |
|
assertEquals(cause, nse); |
138 |
|
} |
139 |
|
catch(Exception e) { |
140 |
< |
fail("unexpected exception"); |
140 |
> |
unexpectedException(); |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
+ |
/** |
145 |
+ |
* |
146 |
+ |
*/ |
147 |
|
public void testCancelBeforeRun() { |
148 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
149 |
|
assertTrue(task.cancel(false)); |
152 |
|
assertTrue(task.isCancelled()); |
153 |
|
} |
154 |
|
|
155 |
+ |
/** |
156 |
+ |
* |
157 |
+ |
*/ |
158 |
|
public void testCancelBeforeRun2() { |
159 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
160 |
|
assertTrue(task.cancel(true)); |
163 |
|
assertTrue(task.isCancelled()); |
164 |
|
} |
165 |
|
|
166 |
+ |
/** |
167 |
+ |
* |
168 |
+ |
*/ |
169 |
|
public void testCancelAfterRun() { |
170 |
|
FutureTask task = new FutureTask( new NoOpCallable()); |
171 |
|
task.run(); |
174 |
|
assertFalse(task.isCancelled()); |
175 |
|
} |
176 |
|
|
177 |
< |
public void testCancelInterrupt(){ |
177 |
> |
/** |
178 |
> |
* |
179 |
> |
*/ |
180 |
> |
public void testCancelInterrupt() { |
181 |
|
FutureTask task = new FutureTask( new Callable() { |
182 |
|
public Object call() { |
183 |
|
try { |
184 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
185 |
< |
threadFail("should throw"); |
185 |
> |
threadShouldThrow(); |
186 |
|
} |
187 |
|
catch (InterruptedException success) {} |
188 |
|
return Boolean.TRUE; |
190 |
|
Thread t = new Thread(task); |
191 |
|
t.start(); |
192 |
|
|
193 |
< |
try{ |
193 |
> |
try { |
194 |
|
Thread.sleep(SHORT_DELAY_MS); |
195 |
|
assertTrue(task.cancel(true)); |
196 |
|
t.join(); |
197 |
|
assertTrue(task.isDone()); |
198 |
|
assertTrue(task.isCancelled()); |
199 |
|
} catch(InterruptedException e){ |
200 |
< |
fail("unexpected exception"); |
200 |
> |
unexpectedException(); |
201 |
|
} |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
< |
public void testCancelNoInterrupt(){ |
205 |
> |
/** |
206 |
> |
* |
207 |
> |
*/ |
208 |
> |
public void testCancelNoInterrupt() { |
209 |
|
FutureTask task = new FutureTask( new Callable() { |
210 |
|
public Object call() { |
211 |
|
try { |
219 |
|
Thread t = new Thread(task); |
220 |
|
t.start(); |
221 |
|
|
222 |
< |
try{ |
222 |
> |
try { |
223 |
|
Thread.sleep(SHORT_DELAY_MS); |
224 |
|
assertTrue(task.cancel(false)); |
225 |
|
t.join(); |
226 |
|
assertTrue(task.isDone()); |
227 |
|
assertTrue(task.isCancelled()); |
228 |
|
} catch(InterruptedException e){ |
229 |
< |
fail("unexpected exception"); |
229 |
> |
unexpectedException(); |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
+ |
/** |
234 |
+ |
* |
235 |
+ |
*/ |
236 |
|
public void testGet1() { |
237 |
< |
final FutureTask ft = new FutureTask(new Callable(){ |
238 |
< |
public Object call(){ |
239 |
< |
try{ |
237 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
238 |
> |
public Object call() { |
239 |
> |
try { |
240 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
241 |
|
} catch(InterruptedException e){ |
242 |
< |
threadFail("unexpected exception"); |
242 |
> |
threadUnexpectedException(); |
243 |
|
} |
244 |
|
return Boolean.TRUE; |
245 |
|
} |
246 |
|
}); |
247 |
< |
Thread t = new Thread(new Runnable(){ |
248 |
< |
public void run(){ |
249 |
< |
try{ |
247 |
> |
Thread t = new Thread(new Runnable() { |
248 |
> |
public void run() { |
249 |
> |
try { |
250 |
|
ft.get(); |
251 |
|
} catch(Exception e){ |
252 |
< |
threadFail("unexpected exception"); |
252 |
> |
threadUnexpectedException(); |
253 |
|
} |
254 |
|
} |
255 |
|
}); |
256 |
< |
try{ |
256 |
> |
try { |
257 |
|
assertFalse(ft.isDone()); |
258 |
|
assertFalse(ft.isCancelled()); |
259 |
|
t.start(); |
263 |
|
assertTrue(ft.isDone()); |
264 |
|
assertFalse(ft.isCancelled()); |
265 |
|
} catch(InterruptedException e){ |
266 |
< |
fail("unexpected exception"); |
266 |
> |
unexpectedException(); |
267 |
|
|
268 |
|
} |
269 |
|
} |
270 |
|
|
271 |
+ |
/** |
272 |
+ |
* |
273 |
+ |
*/ |
274 |
|
public void testTimedGet1() { |
275 |
< |
final FutureTask ft = new FutureTask(new Callable(){ |
276 |
< |
public Object call(){ |
277 |
< |
try{ |
275 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
276 |
> |
public Object call() { |
277 |
> |
try { |
278 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
279 |
|
} catch(InterruptedException e){ |
280 |
< |
threadFail("unexpected exception"); |
280 |
> |
threadUnexpectedException(); |
281 |
|
} |
282 |
|
return Boolean.TRUE; |
283 |
|
} |
284 |
|
}); |
285 |
< |
Thread t = new Thread(new Runnable(){ |
286 |
< |
public void run(){ |
287 |
< |
try{ |
285 |
> |
Thread t = new Thread(new Runnable() { |
286 |
> |
public void run() { |
287 |
> |
try { |
288 |
|
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
289 |
|
} catch(TimeoutException success) { |
290 |
|
} catch(Exception e){ |
291 |
< |
threadFail("unexpected exception"); |
291 |
> |
threadUnexpectedException(); |
292 |
|
} |
293 |
|
} |
294 |
|
}); |
295 |
< |
try{ |
295 |
> |
try { |
296 |
|
assertFalse(ft.isDone()); |
297 |
|
assertFalse(ft.isCancelled()); |
298 |
|
t.start(); |
301 |
|
assertTrue(ft.isDone()); |
302 |
|
assertFalse(ft.isCancelled()); |
303 |
|
} catch(InterruptedException e){ |
304 |
< |
fail("unexpected exception"); |
304 |
> |
unexpectedException(); |
305 |
|
|
306 |
|
} |
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
< |
public void testGet_Cancellation(){ |
311 |
< |
final FutureTask ft = new FutureTask(new Callable(){ |
312 |
< |
public Object call(){ |
313 |
< |
try{ |
310 |
> |
/** |
311 |
> |
* |
312 |
> |
*/ |
313 |
> |
public void testGet_Cancellation() { |
314 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
315 |
> |
public Object call() { |
316 |
> |
try { |
317 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
318 |
|
} catch(InterruptedException e){ |
319 |
< |
threadFail("unexpected exception"); |
319 |
> |
threadUnexpectedException(); |
320 |
|
} |
321 |
|
return Boolean.TRUE; |
322 |
|
} |
323 |
|
}); |
324 |
|
try { |
325 |
|
Thread.sleep(SHORT_DELAY_MS); |
326 |
< |
Thread t = new Thread(new Runnable(){ |
327 |
< |
public void run(){ |
328 |
< |
try{ |
326 |
> |
Thread t = new Thread(new Runnable() { |
327 |
> |
public void run() { |
328 |
> |
try { |
329 |
|
ft.get(); |
330 |
< |
threadFail("should throw"); |
330 |
> |
threadShouldThrow(); |
331 |
|
} catch(CancellationException success){ |
332 |
|
} |
333 |
|
catch(Exception e){ |
334 |
< |
threadFail("unexpected exception"); |
334 |
> |
threadUnexpectedException(); |
335 |
|
} |
336 |
|
} |
337 |
|
}); |
339 |
|
ft.cancel(true); |
340 |
|
t.join(); |
341 |
|
} catch(InterruptedException success){ |
342 |
< |
fail("unexpected exception"); |
342 |
> |
unexpectedException(); |
343 |
|
} |
344 |
|
} |
345 |
|
|
346 |
< |
public void testGet_Cancellation2(){ |
347 |
< |
final FutureTask ft = new FutureTask(new Callable(){ |
348 |
< |
public Object call(){ |
349 |
< |
try{ |
346 |
> |
/** |
347 |
> |
* |
348 |
> |
*/ |
349 |
> |
public void testGet_Cancellation2() { |
350 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
351 |
> |
public Object call() { |
352 |
> |
try { |
353 |
|
Thread.sleep(SHORT_DELAY_MS); |
354 |
|
} catch(InterruptedException e) { |
355 |
< |
threadFail("unexpected exception"); |
355 |
> |
threadUnexpectedException(); |
356 |
|
} |
357 |
|
return Boolean.TRUE; |
358 |
|
} |
359 |
|
}); |
360 |
< |
try{ |
360 |
> |
try { |
361 |
|
Thread.sleep(SHORT_DELAY_MS); |
362 |
< |
Thread t = new Thread(new Runnable(){ |
363 |
< |
public void run(){ |
364 |
< |
try{ |
362 |
> |
Thread t = new Thread(new Runnable() { |
363 |
> |
public void run() { |
364 |
> |
try { |
365 |
|
ft.get(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
366 |
< |
threadFail("should throw"); |
366 |
> |
threadShouldThrow(); |
367 |
|
} catch(CancellationException success) {} |
368 |
|
catch(Exception e){ |
369 |
< |
threadFail("unexpected exception"); |
369 |
> |
threadUnexpectedException(); |
370 |
|
} |
371 |
|
} |
372 |
|
}); |
376 |
|
Thread.sleep(SHORT_DELAY_MS); |
377 |
|
t.join(); |
378 |
|
} catch(InterruptedException ie){ |
379 |
< |
fail("unexpected exception"); |
379 |
> |
unexpectedException(); |
380 |
|
} |
381 |
|
} |
382 |
|
|
383 |
< |
public void testGet_ExecutionException(){ |
384 |
< |
final FutureTask ft = new FutureTask(new Callable(){ |
385 |
< |
public Object call(){ |
383 |
> |
/** |
384 |
> |
* |
385 |
> |
*/ |
386 |
> |
public void testGet_ExecutionException() { |
387 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
388 |
> |
public Object call() { |
389 |
|
int i = 5/0; |
390 |
|
return Boolean.TRUE; |
391 |
|
} |
392 |
|
}); |
393 |
< |
try{ |
393 |
> |
try { |
394 |
|
ft.run(); |
395 |
|
ft.get(); |
396 |
< |
fail("should throw"); |
396 |
> |
shouldThrow(); |
397 |
|
} catch(ExecutionException success){ |
398 |
|
} |
399 |
|
catch(Exception e){ |
400 |
< |
fail("unexpected exception"); |
400 |
> |
unexpectedException(); |
401 |
|
} |
402 |
|
} |
403 |
|
|
404 |
< |
public void testTimedGet_ExecutionException2(){ |
405 |
< |
final FutureTask ft = new FutureTask(new Callable(){ |
406 |
< |
public Object call(){ |
404 |
> |
/** |
405 |
> |
* |
406 |
> |
*/ |
407 |
> |
public void testTimedGet_ExecutionException2() { |
408 |
> |
final FutureTask ft = new FutureTask(new Callable() { |
409 |
> |
public Object call() { |
410 |
|
int i = 5/0; |
411 |
|
return Boolean.TRUE; |
412 |
|
} |
413 |
|
}); |
414 |
< |
try{ |
414 |
> |
try { |
415 |
|
ft.run(); |
416 |
|
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
417 |
< |
fail("should throw"); |
417 |
> |
shouldThrow(); |
418 |
|
} catch(ExecutionException success) { |
419 |
|
} catch(TimeoutException success) { } // unlikely but OK |
420 |
|
catch(Exception e){ |
421 |
< |
fail("unexpected exception"); |
421 |
> |
unexpectedException(); |
422 |
|
} |
423 |
|
} |
424 |
|
|
425 |
|
|
426 |
< |
public void testGet_InterruptedException(){ |
426 |
> |
/** |
427 |
> |
* |
428 |
> |
*/ |
429 |
> |
public void testGet_InterruptedException() { |
430 |
|
final FutureTask ft = new FutureTask(new NoOpCallable()); |
431 |
< |
Thread t = new Thread(new Runnable(){ |
432 |
< |
public void run(){ |
433 |
< |
try{ |
431 |
> |
Thread t = new Thread(new Runnable() { |
432 |
> |
public void run() { |
433 |
> |
try { |
434 |
|
ft.get(); |
435 |
< |
threadFail("should throw"); |
435 |
> |
threadShouldThrow(); |
436 |
|
} catch(InterruptedException success){ |
437 |
|
} catch(Exception e){ |
438 |
< |
threadFail("unexpected exception"); |
438 |
> |
threadUnexpectedException(); |
439 |
|
} |
440 |
|
} |
441 |
|
}); |
445 |
|
t.interrupt(); |
446 |
|
t.join(); |
447 |
|
} catch(Exception e){ |
448 |
< |
fail("unexpected exception"); |
448 |
> |
unexpectedException(); |
449 |
|
} |
450 |
|
} |
451 |
|
|
452 |
< |
public void testTimedGet_InterruptedException2(){ |
452 |
> |
/** |
453 |
> |
* |
454 |
> |
*/ |
455 |
> |
public void testTimedGet_InterruptedException2() { |
456 |
|
final FutureTask ft = new FutureTask(new NoOpCallable()); |
457 |
< |
Thread t = new Thread(new Runnable(){ |
458 |
< |
public void run(){ |
459 |
< |
try{ |
457 |
> |
Thread t = new Thread(new Runnable() { |
458 |
> |
public void run() { |
459 |
> |
try { |
460 |
|
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
461 |
< |
threadFail("should throw"); |
461 |
> |
threadShouldThrow(); |
462 |
|
} catch(InterruptedException success){} |
463 |
|
catch(Exception e){ |
464 |
< |
threadFail("unexpected exception"); |
464 |
> |
threadUnexpectedException(); |
465 |
|
} |
466 |
|
} |
467 |
|
}); |
471 |
|
t.interrupt(); |
472 |
|
t.join(); |
473 |
|
} catch(Exception e){ |
474 |
< |
fail("unexpected exception"); |
474 |
> |
unexpectedException(); |
475 |
|
} |
476 |
|
} |
477 |
|
|
478 |
< |
public void testGet_TimeoutException(){ |
479 |
< |
try{ |
478 |
> |
/** |
479 |
> |
* |
480 |
> |
*/ |
481 |
> |
public void testGet_TimeoutException() { |
482 |
> |
try { |
483 |
|
FutureTask ft = new FutureTask(new NoOpCallable()); |
484 |
|
ft.get(1,TimeUnit.MILLISECONDS); |
485 |
< |
fail("should throw"); |
485 |
> |
shouldThrow(); |
486 |
|
} catch(TimeoutException success){} |
487 |
|
catch(Exception success){ |
488 |
< |
fail("unexpected exception"); |
488 |
> |
unexpectedException(); |
489 |
|
} |
490 |
|
} |
491 |
|
|