2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.math.BigInteger; |
15 |
|
import java.security.*; |
15 |
– |
import sun.security.util.SecurityConstants; |
16 |
|
|
17 |
< |
public class ExecutorsTest extends JSR166TestCase{ |
17 |
> |
public class ExecutorsTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
junit.textui.TestRunner.run (suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(ExecutorsTest.class); |
26 |
|
private final ExecutorService exec; |
27 |
|
private final Callable<T> func; |
28 |
|
private final long msecs; |
29 |
< |
|
29 |
> |
|
30 |
|
TimedCallable(ExecutorService exec, Callable<T> func, long msecs) { |
31 |
|
this.exec = exec; |
32 |
|
this.func = func; |
33 |
|
this.msecs = msecs; |
34 |
|
} |
35 |
< |
|
35 |
> |
|
36 |
|
public T call() throws Exception { |
37 |
|
Future<T> ftask = exec.submit(func); |
38 |
|
try { |
39 |
< |
return ftask.get(msecs, TimeUnit.MILLISECONDS); |
39 |
> |
return ftask.get(msecs, MILLISECONDS); |
40 |
|
} finally { |
41 |
|
ftask.cancel(true); |
42 |
|
} |
91 |
|
try { |
92 |
|
ExecutorService e = Executors.newCachedThreadPool(null); |
93 |
|
shouldThrow(); |
94 |
< |
} |
95 |
< |
catch(NullPointerException success) { |
96 |
< |
} |
94 |
> |
} catch (NullPointerException success) {} |
95 |
|
} |
96 |
|
|
97 |
|
|
124 |
|
try { |
125 |
|
ExecutorService e = Executors.newSingleThreadExecutor(null); |
126 |
|
shouldThrow(); |
127 |
< |
} |
130 |
< |
catch(NullPointerException success) { |
131 |
< |
} |
127 |
> |
} catch (NullPointerException success) {} |
128 |
|
} |
129 |
|
|
130 |
|
/** |
134 |
|
ExecutorService e = Executors.newSingleThreadExecutor(); |
135 |
|
try { |
136 |
|
ThreadPoolExecutor tpe = (ThreadPoolExecutor)e; |
137 |
+ |
shouldThrow(); |
138 |
|
} catch (ClassCastException success) { |
139 |
|
} finally { |
140 |
|
joinPool(e); |
171 |
|
try { |
172 |
|
ExecutorService e = Executors.newFixedThreadPool(2, null); |
173 |
|
shouldThrow(); |
174 |
< |
} |
178 |
< |
catch(NullPointerException success) { |
179 |
< |
} |
174 |
> |
} catch (NullPointerException success) {} |
175 |
|
} |
176 |
|
|
177 |
|
/** |
181 |
|
try { |
182 |
|
ExecutorService e = Executors.newFixedThreadPool(0); |
183 |
|
shouldThrow(); |
184 |
< |
} |
190 |
< |
catch(IllegalArgumentException success) { |
191 |
< |
} |
184 |
> |
} catch (IllegalArgumentException success) {} |
185 |
|
} |
186 |
|
|
187 |
|
|
202 |
|
public void testunconfigurableExecutorServiceNPE() { |
203 |
|
try { |
204 |
|
ExecutorService e = Executors.unconfigurableExecutorService(null); |
205 |
< |
} |
206 |
< |
catch (NullPointerException success) { |
214 |
< |
} |
205 |
> |
shouldThrow(); |
206 |
> |
} catch (NullPointerException success) {} |
207 |
|
} |
208 |
|
|
209 |
|
/** |
212 |
|
public void testunconfigurableScheduledExecutorServiceNPE() { |
213 |
|
try { |
214 |
|
ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); |
215 |
< |
} |
216 |
< |
catch (NullPointerException success) { |
225 |
< |
} |
215 |
> |
shouldThrow(); |
216 |
> |
} catch (NullPointerException success) {} |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
/** |
221 |
|
* a newSingleThreadScheduledExecutor successfully runs delayed task |
222 |
|
*/ |
223 |
< |
public void testNewSingleThreadScheduledExecutor() { |
224 |
< |
try { |
225 |
< |
TrackedCallable callable = new TrackedCallable(); |
226 |
< |
ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); |
227 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
228 |
< |
assertFalse(callable.done); |
229 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
230 |
< |
assertTrue(callable.done); |
231 |
< |
assertEquals(Boolean.TRUE, f.get()); |
241 |
< |
joinPool(p1); |
242 |
< |
} catch(RejectedExecutionException e){} |
243 |
< |
catch(Exception e){ |
244 |
< |
e.printStackTrace(); |
245 |
< |
unexpectedException(); |
246 |
< |
} |
223 |
> |
public void testNewSingleThreadScheduledExecutor() throws Exception { |
224 |
> |
TrackedCallable callable = new TrackedCallable(); |
225 |
> |
ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); |
226 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
227 |
> |
assertFalse(callable.done); |
228 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
229 |
> |
assertTrue(callable.done); |
230 |
> |
assertEquals(Boolean.TRUE, f.get()); |
231 |
> |
joinPool(p1); |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
|
* a newScheduledThreadPool successfully runs delayed task |
236 |
|
*/ |
237 |
< |
public void testnewScheduledThreadPool() { |
238 |
< |
try { |
239 |
< |
TrackedCallable callable = new TrackedCallable(); |
240 |
< |
ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); |
241 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
242 |
< |
assertFalse(callable.done); |
243 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
244 |
< |
assertTrue(callable.done); |
245 |
< |
assertEquals(Boolean.TRUE, f.get()); |
261 |
< |
joinPool(p1); |
262 |
< |
} catch(RejectedExecutionException e){} |
263 |
< |
catch(Exception e){ |
264 |
< |
e.printStackTrace(); |
265 |
< |
unexpectedException(); |
266 |
< |
} |
237 |
> |
public void testnewScheduledThreadPool() throws Exception { |
238 |
> |
TrackedCallable callable = new TrackedCallable(); |
239 |
> |
ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); |
240 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
241 |
> |
assertFalse(callable.done); |
242 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
243 |
> |
assertTrue(callable.done); |
244 |
> |
assertEquals(Boolean.TRUE, f.get()); |
245 |
> |
joinPool(p1); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
< |
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
250 |
< |
*/ |
251 |
< |
public void testunconfigurableScheduledExecutorService() { |
252 |
< |
try { |
253 |
< |
TrackedCallable callable = new TrackedCallable(); |
254 |
< |
ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); |
255 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
256 |
< |
assertFalse(callable.done); |
257 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
258 |
< |
assertTrue(callable.done); |
259 |
< |
assertEquals(Boolean.TRUE, f.get()); |
281 |
< |
joinPool(p1); |
282 |
< |
} catch(RejectedExecutionException e){} |
283 |
< |
catch(Exception e){ |
284 |
< |
e.printStackTrace(); |
285 |
< |
unexpectedException(); |
286 |
< |
} |
249 |
> |
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
250 |
> |
*/ |
251 |
> |
public void testunconfigurableScheduledExecutorService() throws Exception { |
252 |
> |
TrackedCallable callable = new TrackedCallable(); |
253 |
> |
ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); |
254 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
255 |
> |
assertFalse(callable.done); |
256 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
257 |
> |
assertTrue(callable.done); |
258 |
> |
assertEquals(Boolean.TRUE, f.get()); |
259 |
> |
joinPool(p1); |
260 |
|
} |
261 |
|
|
262 |
|
/** |
263 |
|
* timeouts from execute will time out if they compute too long. |
264 |
|
*/ |
265 |
< |
public void testTimedCallable() { |
265 |
> |
public void testTimedCallable() throws Exception { |
266 |
|
int N = 10000; |
267 |
|
ExecutorService executor = Executors.newSingleThreadExecutor(); |
268 |
|
List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N); |
269 |
|
try { |
270 |
|
long startTime = System.currentTimeMillis(); |
271 |
< |
|
271 |
> |
|
272 |
|
long i = 0; |
273 |
|
while (tasks.size() < N) { |
274 |
|
tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1)); |
275 |
|
i += 10; |
276 |
|
} |
277 |
< |
|
277 |
> |
|
278 |
|
int iters = 0; |
279 |
|
BigInteger sum = BigInteger.ZERO; |
280 |
|
for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) { |
286 |
|
assertTrue(iters > 0); |
287 |
|
return; |
288 |
|
} |
316 |
– |
catch (Exception e) { |
317 |
– |
unexpectedException(); |
318 |
– |
} |
289 |
|
} |
290 |
|
// if by chance we didn't ever time out, total time must be small |
291 |
|
long elapsed = System.currentTimeMillis() - startTime; |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
< |
|
299 |
> |
|
300 |
|
/** |
301 |
|
* ThreadPoolExecutor using defaultThreadFactory has |
302 |
|
* specified group, priority, daemon status, and name |
303 |
|
*/ |
304 |
< |
public void testDefaultThreadFactory() { |
304 |
> |
public void testDefaultThreadFactory() throws Exception { |
305 |
|
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
306 |
|
Runnable r = new Runnable() { |
307 |
|
public void run() { |
308 |
< |
try { |
309 |
< |
Thread current = Thread.currentThread(); |
310 |
< |
threadAssertTrue(!current.isDaemon()); |
311 |
< |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
312 |
< |
ThreadGroup g = current.getThreadGroup(); |
313 |
< |
SecurityManager s = System.getSecurityManager(); |
314 |
< |
if (s != null) |
315 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
316 |
< |
else |
317 |
< |
threadAssertTrue(g == egroup); |
318 |
< |
String name = current.getName(); |
319 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
320 |
< |
} catch (SecurityException ok) { |
321 |
< |
// Also pass if not allowed to change setting |
322 |
< |
} |
308 |
> |
try { |
309 |
> |
Thread current = Thread.currentThread(); |
310 |
> |
threadAssertTrue(!current.isDaemon()); |
311 |
> |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
312 |
> |
ThreadGroup g = current.getThreadGroup(); |
313 |
> |
SecurityManager s = System.getSecurityManager(); |
314 |
> |
if (s != null) |
315 |
> |
threadAssertTrue(g == s.getThreadGroup()); |
316 |
> |
else |
317 |
> |
threadAssertTrue(g == egroup); |
318 |
> |
String name = current.getName(); |
319 |
> |
threadAssertTrue(name.endsWith("thread-1")); |
320 |
> |
} catch (SecurityException ok) { |
321 |
> |
// Also pass if not allowed to change setting |
322 |
> |
} |
323 |
|
} |
324 |
|
}; |
325 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
326 |
< |
|
326 |
> |
|
327 |
|
e.execute(r); |
328 |
|
try { |
329 |
|
e.shutdown(); |
330 |
< |
} catch(SecurityException ok) { |
330 |
> |
} catch (SecurityException ok) { |
331 |
|
} |
332 |
< |
|
332 |
> |
|
333 |
|
try { |
334 |
|
Thread.sleep(SHORT_DELAY_MS); |
365 |
– |
} catch (Exception eX) { |
366 |
– |
unexpectedException(); |
335 |
|
} finally { |
336 |
|
joinPool(e); |
337 |
|
} |
342 |
|
* specified group, priority, daemon status, name, |
343 |
|
* access control context and context class loader |
344 |
|
*/ |
345 |
< |
public void testPrivilegedThreadFactory() { |
345 |
> |
public void testPrivilegedThreadFactory() throws Exception { |
346 |
|
Policy savedPolicy = null; |
347 |
|
try { |
348 |
|
savedPolicy = Policy.getPolicy(); |
358 |
|
final AccessControlContext thisacc = AccessController.getContext(); |
359 |
|
Runnable r = new Runnable() { |
360 |
|
public void run() { |
361 |
< |
try { |
362 |
< |
Thread current = Thread.currentThread(); |
363 |
< |
threadAssertTrue(!current.isDaemon()); |
364 |
< |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
365 |
< |
ThreadGroup g = current.getThreadGroup(); |
366 |
< |
SecurityManager s = System.getSecurityManager(); |
367 |
< |
if (s != null) |
368 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
369 |
< |
else |
370 |
< |
threadAssertTrue(g == egroup); |
371 |
< |
String name = current.getName(); |
372 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
373 |
< |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
374 |
< |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
375 |
< |
} catch(SecurityException ok) { |
376 |
< |
// Also pass if not allowed to change settings |
377 |
< |
} |
361 |
> |
try { |
362 |
> |
Thread current = Thread.currentThread(); |
363 |
> |
threadAssertTrue(!current.isDaemon()); |
364 |
> |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
365 |
> |
ThreadGroup g = current.getThreadGroup(); |
366 |
> |
SecurityManager s = System.getSecurityManager(); |
367 |
> |
if (s != null) |
368 |
> |
threadAssertTrue(g == s.getThreadGroup()); |
369 |
> |
else |
370 |
> |
threadAssertTrue(g == egroup); |
371 |
> |
String name = current.getName(); |
372 |
> |
threadAssertTrue(name.endsWith("thread-1")); |
373 |
> |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
374 |
> |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
375 |
> |
} catch (SecurityException ok) { |
376 |
> |
// Also pass if not allowed to change settings |
377 |
> |
} |
378 |
|
} |
379 |
|
}; |
380 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
381 |
< |
|
381 |
> |
|
382 |
|
Policy.setPolicy(savedPolicy); |
383 |
|
e.execute(r); |
384 |
|
try { |
385 |
|
e.shutdown(); |
386 |
< |
} catch(SecurityException ok) { |
386 |
> |
} catch (SecurityException ok) { |
387 |
|
} |
388 |
|
try { |
389 |
|
Thread.sleep(SHORT_DELAY_MS); |
422 |
– |
} catch (Exception ex) { |
423 |
– |
unexpectedException(); |
390 |
|
} finally { |
391 |
|
joinPool(e); |
392 |
|
} |
427 |
– |
|
393 |
|
} |
394 |
|
|
395 |
|
void checkCCL() { |
396 |
|
SecurityManager sm = System.getSecurityManager(); |
397 |
|
if (sm != null) { |
398 |
|
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
399 |
< |
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); |
399 |
> |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
400 |
|
} |
401 |
|
} |
402 |
|
|
413 |
|
* privilegedCallableUsingCurrentClassLoader throws ACE |
414 |
|
*/ |
415 |
|
public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { |
416 |
< |
Policy savedPolicy = null; |
416 |
> |
Policy savedPolicy = null; |
417 |
|
try { |
418 |
|
savedPolicy = Policy.getPolicy(); |
419 |
|
AdjustablePolicy policy = new AdjustablePolicy(); |
428 |
|
// too many privileges to test; so return |
429 |
|
Policy.setPolicy(savedPolicy); |
430 |
|
return; |
431 |
< |
} catch(AccessControlException ok) { |
432 |
< |
} |
431 |
> |
} catch (AccessControlException ok) { |
432 |
> |
} |
433 |
|
|
434 |
|
try { |
435 |
|
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
436 |
|
shouldThrow(); |
437 |
< |
} catch(AccessControlException success) { |
438 |
< |
} catch(Exception ex) { |
474 |
< |
unexpectedException(); |
475 |
< |
} |
476 |
< |
finally { |
437 |
> |
} catch (AccessControlException success) { |
438 |
> |
} finally { |
439 |
|
Policy.setPolicy(savedPolicy); |
440 |
|
} |
441 |
|
} |
444 |
|
* With class loader permissions, calling |
445 |
|
* privilegedCallableUsingCurrentClassLoader does not throw ACE |
446 |
|
*/ |
447 |
< |
public void testprivilegedCallableUsingCCLWithPrivs() { |
448 |
< |
Policy savedPolicy = null; |
447 |
> |
public void testprivilegedCallableUsingCCLWithPrivs() throws Exception { |
448 |
> |
Policy savedPolicy = null; |
449 |
|
try { |
450 |
|
savedPolicy = Policy.getPolicy(); |
451 |
|
AdjustablePolicy policy = new AdjustablePolicy(); |
455 |
|
} catch (AccessControlException ok) { |
456 |
|
return; |
457 |
|
} |
458 |
< |
|
458 |
> |
|
459 |
|
try { |
460 |
|
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
461 |
|
task.call(); |
462 |
< |
} catch(Exception ex) { |
501 |
< |
unexpectedException(); |
502 |
< |
} |
462 |
> |
} |
463 |
|
finally { |
464 |
|
Policy.setPolicy(savedPolicy); |
465 |
|
} |
468 |
|
/** |
469 |
|
* Without permissions, calling privilegedCallable throws ACE |
470 |
|
*/ |
471 |
< |
public void testprivilegedCallableWithNoPrivs() { |
471 |
> |
public void testprivilegedCallableWithNoPrivs() throws Exception { |
472 |
|
Callable task; |
473 |
|
Policy savedPolicy = null; |
474 |
|
AdjustablePolicy policy = null; |
484 |
|
return; // program has too few permissions to set up test |
485 |
|
} |
486 |
|
|
487 |
< |
// Make sure that program doesn't have too many permissions |
487 |
> |
// Make sure that program doesn't have too many permissions |
488 |
|
try { |
489 |
|
AccessController.doPrivileged(new PrivilegedAction() { |
490 |
|
public Object run() { |
493 |
|
}}, noprivAcc); |
494 |
|
// too many permssions; skip test |
495 |
|
return; |
496 |
< |
} catch(AccessControlException ok) { |
496 |
> |
} catch (AccessControlException ok) { |
497 |
|
} |
498 |
|
|
499 |
|
try { |
500 |
|
task.call(); |
501 |
|
shouldThrow(); |
502 |
< |
} catch(AccessControlException success) { |
543 |
< |
} catch(Exception ex) { |
544 |
< |
unexpectedException(); |
545 |
< |
} |
502 |
> |
} catch (AccessControlException success) {} |
503 |
|
} |
504 |
|
|
505 |
|
/** |
506 |
|
* With permissions, calling privilegedCallable succeeds |
507 |
|
*/ |
508 |
< |
public void testprivilegedCallableWithPrivs() { |
509 |
< |
Policy savedPolicy = null; |
508 |
> |
public void testprivilegedCallableWithPrivs() throws Exception { |
509 |
> |
Policy savedPolicy = null; |
510 |
|
try { |
511 |
|
savedPolicy = Policy.getPolicy(); |
512 |
|
AdjustablePolicy policy = new AdjustablePolicy(); |
516 |
|
} catch (AccessControlException ok) { |
517 |
|
return; |
518 |
|
} |
519 |
< |
|
519 |
> |
|
520 |
|
Callable task = Executors.privilegedCallable(new CheckCCL()); |
521 |
|
try { |
522 |
|
task.call(); |
566 |
– |
} catch(Exception ex) { |
567 |
– |
unexpectedException(); |
523 |
|
} finally { |
524 |
|
Policy.setPolicy(savedPolicy); |
525 |
|
} |
527 |
|
|
528 |
|
/** |
529 |
|
* callable(Runnable) returns null when called |
530 |
< |
*/ |
531 |
< |
public void testCallable1() { |
532 |
< |
try { |
533 |
< |
Callable c = Executors.callable(new NoOpRunnable()); |
579 |
< |
assertNull(c.call()); |
580 |
< |
} catch(Exception ex) { |
581 |
< |
unexpectedException(); |
582 |
< |
} |
583 |
< |
|
530 |
> |
*/ |
531 |
> |
public void testCallable1() throws Exception { |
532 |
> |
Callable c = Executors.callable(new NoOpRunnable()); |
533 |
> |
assertNull(c.call()); |
534 |
|
} |
535 |
|
|
536 |
|
/** |
537 |
|
* callable(Runnable, result) returns result when called |
538 |
< |
*/ |
539 |
< |
public void testCallable2() { |
540 |
< |
try { |
541 |
< |
Callable c = Executors.callable(new NoOpRunnable(), one); |
592 |
< |
assertEquals(one, c.call()); |
593 |
< |
} catch(Exception ex) { |
594 |
< |
unexpectedException(); |
595 |
< |
} |
538 |
> |
*/ |
539 |
> |
public void testCallable2() throws Exception { |
540 |
> |
Callable c = Executors.callable(new NoOpRunnable(), one); |
541 |
> |
assertSame(one, c.call()); |
542 |
|
} |
543 |
|
|
544 |
|
/** |
545 |
|
* callable(PrivilegedAction) returns its result when called |
546 |
< |
*/ |
547 |
< |
public void testCallable3() { |
548 |
< |
try { |
549 |
< |
Callable c = Executors.callable(new PrivilegedAction() { |
550 |
< |
public Object run() { return one; }}); |
605 |
< |
assertEquals(one, c.call()); |
606 |
< |
} catch(Exception ex) { |
607 |
< |
unexpectedException(); |
608 |
< |
} |
546 |
> |
*/ |
547 |
> |
public void testCallable3() throws Exception { |
548 |
> |
Callable c = Executors.callable(new PrivilegedAction() { |
549 |
> |
public Object run() { return one; }}); |
550 |
> |
assertSame(one, c.call()); |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
|
* callable(PrivilegedExceptionAction) returns its result when called |
555 |
< |
*/ |
556 |
< |
public void testCallable4() { |
557 |
< |
try { |
558 |
< |
Callable c = Executors.callable(new PrivilegedExceptionAction() { |
559 |
< |
public Object run() { return one; }}); |
618 |
< |
assertEquals(one, c.call()); |
619 |
< |
} catch(Exception ex) { |
620 |
< |
unexpectedException(); |
621 |
< |
} |
555 |
> |
*/ |
556 |
> |
public void testCallable4() throws Exception { |
557 |
> |
Callable c = Executors.callable(new PrivilegedExceptionAction() { |
558 |
> |
public Object run() { return one; }}); |
559 |
> |
assertSame(one, c.call()); |
560 |
|
} |
561 |
|
|
562 |
|
|
563 |
|
/** |
564 |
|
* callable(null Runnable) throws NPE |
565 |
< |
*/ |
565 |
> |
*/ |
566 |
|
public void testCallableNPE1() { |
567 |
|
try { |
568 |
< |
Runnable r = null; |
569 |
< |
Callable c = Executors.callable(r); |
570 |
< |
} catch (NullPointerException success) { |
633 |
< |
} |
568 |
> |
Callable c = Executors.callable((Runnable) null); |
569 |
> |
shouldThrow(); |
570 |
> |
} catch (NullPointerException success) {} |
571 |
|
} |
572 |
|
|
573 |
|
/** |
574 |
|
* callable(null, result) throws NPE |
575 |
< |
*/ |
575 |
> |
*/ |
576 |
|
public void testCallableNPE2() { |
577 |
|
try { |
578 |
< |
Runnable r = null; |
579 |
< |
Callable c = Executors.callable(r, one); |
580 |
< |
} catch (NullPointerException success) { |
644 |
< |
} |
578 |
> |
Callable c = Executors.callable((Runnable) null, one); |
579 |
> |
shouldThrow(); |
580 |
> |
} catch (NullPointerException success) {} |
581 |
|
} |
582 |
|
|
583 |
|
/** |
584 |
|
* callable(null PrivilegedAction) throws NPE |
585 |
< |
*/ |
585 |
> |
*/ |
586 |
|
public void testCallableNPE3() { |
587 |
|
try { |
588 |
< |
PrivilegedAction r = null; |
589 |
< |
Callable c = Executors.callable(r); |
590 |
< |
} catch (NullPointerException success) { |
655 |
< |
} |
588 |
> |
Callable c = Executors.callable((PrivilegedAction) null); |
589 |
> |
shouldThrow(); |
590 |
> |
} catch (NullPointerException success) {} |
591 |
|
} |
592 |
|
|
593 |
|
/** |
594 |
|
* callable(null PrivilegedExceptionAction) throws NPE |
595 |
< |
*/ |
595 |
> |
*/ |
596 |
|
public void testCallableNPE4() { |
597 |
|
try { |
598 |
< |
PrivilegedExceptionAction r = null; |
599 |
< |
Callable c = Executors.callable(r); |
600 |
< |
} catch (NullPointerException success) { |
666 |
< |
} |
598 |
> |
Callable c = Executors.callable((PrivilegedExceptionAction) null); |
599 |
> |
shouldThrow(); |
600 |
> |
} catch (NullPointerException success) {} |
601 |
|
} |
602 |
|
|
603 |
|
|