1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
15 |
|
|
16 |
|
public class ExecutorsTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(ExecutorsTest.class); |
22 |
|
} |
23 |
|
|
25 |
– |
static class TimedCallable<T> implements Callable<T> { |
26 |
– |
private final ExecutorService exec; |
27 |
– |
private final Callable<T> func; |
28 |
– |
private final long msecs; |
29 |
– |
|
30 |
– |
TimedCallable(ExecutorService exec, Callable<T> func, long msecs) { |
31 |
– |
this.exec = exec; |
32 |
– |
this.func = func; |
33 |
– |
this.msecs = msecs; |
34 |
– |
} |
35 |
– |
|
36 |
– |
public T call() throws Exception { |
37 |
– |
Future<T> ftask = exec.submit(func); |
38 |
– |
try { |
39 |
– |
return ftask.get(msecs, MILLISECONDS); |
40 |
– |
} finally { |
41 |
– |
ftask.cancel(true); |
42 |
– |
} |
43 |
– |
} |
44 |
– |
} |
45 |
– |
|
46 |
– |
|
47 |
– |
private static class Fib implements Callable<BigInteger> { |
48 |
– |
private final BigInteger n; |
49 |
– |
Fib(long n) { |
50 |
– |
if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n); |
51 |
– |
this.n = BigInteger.valueOf(n); |
52 |
– |
} |
53 |
– |
public BigInteger call() { |
54 |
– |
BigInteger f1 = BigInteger.ONE; |
55 |
– |
BigInteger f2 = f1; |
56 |
– |
for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) { |
57 |
– |
BigInteger t = f1.add(f2); |
58 |
– |
f1 = f2; |
59 |
– |
f2 = t; |
60 |
– |
} |
61 |
– |
return f1; |
62 |
– |
} |
63 |
– |
}; |
64 |
– |
|
24 |
|
/** |
25 |
|
* A newCachedThreadPool can execute runnables |
26 |
|
*/ |
53 |
|
} catch (NullPointerException success) {} |
54 |
|
} |
55 |
|
|
97 |
– |
|
56 |
|
/** |
57 |
|
* A new SingleThreadExecutor can execute runnables |
58 |
|
*/ |
99 |
|
} |
100 |
|
} |
101 |
|
|
144 |
– |
|
102 |
|
/** |
103 |
|
* A new newFixedThreadPool can execute runnables |
104 |
|
*/ |
141 |
|
} catch (IllegalArgumentException success) {} |
142 |
|
} |
143 |
|
|
187 |
– |
|
144 |
|
/** |
145 |
|
* An unconfigurable newFixedThreadPool can execute runnables |
146 |
|
*/ |
172 |
|
} catch (NullPointerException success) {} |
173 |
|
} |
174 |
|
|
219 |
– |
|
175 |
|
/** |
176 |
|
* a newSingleThreadScheduledExecutor successfully runs delayed task |
177 |
|
*/ |
178 |
|
public void testNewSingleThreadScheduledExecutor() throws Exception { |
179 |
< |
TrackedCallable callable = new TrackedCallable(); |
180 |
< |
ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); |
181 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
182 |
< |
assertFalse(callable.done); |
183 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
184 |
< |
assertTrue(callable.done); |
185 |
< |
assertEquals(Boolean.TRUE, f.get()); |
186 |
< |
joinPool(p1); |
179 |
> |
ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor(); |
180 |
> |
try { |
181 |
> |
final CountDownLatch done = new CountDownLatch(1); |
182 |
> |
final Runnable task = new CheckedRunnable() { |
183 |
> |
public void realRun() { |
184 |
> |
done.countDown(); |
185 |
> |
}}; |
186 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
187 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
188 |
> |
assertFalse(f.isDone()); |
189 |
> |
assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
190 |
> |
assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); |
191 |
> |
assertSame(Boolean.TRUE, f.get()); |
192 |
> |
assertTrue(f.isDone()); |
193 |
> |
} finally { |
194 |
> |
joinPool(p); |
195 |
> |
} |
196 |
|
} |
197 |
|
|
198 |
|
/** |
199 |
|
* a newScheduledThreadPool successfully runs delayed task |
200 |
|
*/ |
201 |
|
public void testnewScheduledThreadPool() throws Exception { |
202 |
< |
TrackedCallable callable = new TrackedCallable(); |
203 |
< |
ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); |
204 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
205 |
< |
assertFalse(callable.done); |
206 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
207 |
< |
assertTrue(callable.done); |
208 |
< |
assertEquals(Boolean.TRUE, f.get()); |
209 |
< |
joinPool(p1); |
202 |
> |
ScheduledExecutorService p = Executors.newScheduledThreadPool(2); |
203 |
> |
try { |
204 |
> |
final CountDownLatch done = new CountDownLatch(1); |
205 |
> |
final Runnable task = new CheckedRunnable() { |
206 |
> |
public void realRun() { |
207 |
> |
done.countDown(); |
208 |
> |
}}; |
209 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
210 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
211 |
> |
assertFalse(f.isDone()); |
212 |
> |
assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
213 |
> |
assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); |
214 |
> |
assertSame(Boolean.TRUE, f.get()); |
215 |
> |
assertTrue(f.isDone()); |
216 |
> |
} finally { |
217 |
> |
joinPool(p); |
218 |
> |
} |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
|
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
223 |
|
*/ |
224 |
|
public void testunconfigurableScheduledExecutorService() throws Exception { |
225 |
< |
TrackedCallable callable = new TrackedCallable(); |
226 |
< |
ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); |
227 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
228 |
< |
assertFalse(callable.done); |
229 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
230 |
< |
assertTrue(callable.done); |
231 |
< |
assertEquals(Boolean.TRUE, f.get()); |
232 |
< |
joinPool(p1); |
225 |
> |
ScheduledExecutorService p = |
226 |
> |
Executors.unconfigurableScheduledExecutorService |
227 |
> |
(Executors.newScheduledThreadPool(2)); |
228 |
> |
try { |
229 |
> |
final CountDownLatch done = new CountDownLatch(1); |
230 |
> |
final Runnable task = new CheckedRunnable() { |
231 |
> |
public void realRun() { |
232 |
> |
done.countDown(); |
233 |
> |
}}; |
234 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
235 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
236 |
> |
assertFalse(f.isDone()); |
237 |
> |
assertTrue(done.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
238 |
> |
assertSame(Boolean.TRUE, f.get(SMALL_DELAY_MS, MILLISECONDS)); |
239 |
> |
assertSame(Boolean.TRUE, f.get()); |
240 |
> |
assertTrue(f.isDone()); |
241 |
> |
} finally { |
242 |
> |
joinPool(p); |
243 |
> |
} |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
< |
* timeouts from execute will time out if they compute too long. |
247 |
> |
* Future.get on submitted tasks will time out if they compute too long. |
248 |
|
*/ |
249 |
|
public void testTimedCallable() throws Exception { |
250 |
< |
int N = 10000; |
251 |
< |
ExecutorService executor = Executors.newSingleThreadExecutor(); |
252 |
< |
List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N); |
253 |
< |
try { |
254 |
< |
long startTime = System.currentTimeMillis(); |
255 |
< |
|
256 |
< |
long i = 0; |
257 |
< |
while (tasks.size() < N) { |
258 |
< |
tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1)); |
259 |
< |
i += 10; |
260 |
< |
} |
261 |
< |
|
262 |
< |
int iters = 0; |
263 |
< |
BigInteger sum = BigInteger.ZERO; |
264 |
< |
for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) { |
265 |
< |
try { |
266 |
< |
++iters; |
267 |
< |
sum = sum.add(it.next().call()); |
268 |
< |
} |
269 |
< |
catch (TimeoutException success) { |
270 |
< |
assertTrue(iters > 0); |
271 |
< |
return; |
272 |
< |
} |
273 |
< |
} |
290 |
< |
// if by chance we didn't ever time out, total time must be small |
291 |
< |
long elapsed = System.currentTimeMillis() - startTime; |
292 |
< |
assertTrue(elapsed < N); |
293 |
< |
} |
294 |
< |
finally { |
250 |
> |
final ExecutorService[] executors = { |
251 |
> |
Executors.newSingleThreadExecutor(), |
252 |
> |
Executors.newCachedThreadPool(), |
253 |
> |
Executors.newFixedThreadPool(2), |
254 |
> |
Executors.newScheduledThreadPool(2), |
255 |
> |
}; |
256 |
> |
|
257 |
> |
final Runnable sleeper = new CheckedInterruptedRunnable() { |
258 |
> |
public void realRun() throws InterruptedException { |
259 |
> |
delay(LONG_DELAY_MS); |
260 |
> |
}}; |
261 |
> |
|
262 |
> |
List<Thread> threads = new ArrayList<Thread>(); |
263 |
> |
for (final ExecutorService executor : executors) { |
264 |
> |
threads.add(newStartedThread(new CheckedRunnable() { |
265 |
> |
public void realRun() { |
266 |
> |
long startTime = System.nanoTime(); |
267 |
> |
Future future = executor.submit(sleeper); |
268 |
> |
assertFutureTimesOut(future); |
269 |
> |
}})); |
270 |
> |
} |
271 |
> |
for (Thread thread : threads) |
272 |
> |
awaitTermination(thread); |
273 |
> |
for (ExecutorService executor : executors) |
274 |
|
joinPool(executor); |
296 |
– |
} |
275 |
|
} |
276 |
|
|
299 |
– |
|
277 |
|
/** |
278 |
|
* ThreadPoolExecutor using defaultThreadFactory has |
279 |
|
* specified group, priority, daemon status, and name |
280 |
|
*/ |
281 |
|
public void testDefaultThreadFactory() throws Exception { |
282 |
|
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
283 |
< |
Runnable r = new Runnable() { |
284 |
< |
public void run() { |
285 |
< |
try { |
286 |
< |
Thread current = Thread.currentThread(); |
287 |
< |
threadAssertTrue(!current.isDaemon()); |
288 |
< |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
289 |
< |
ThreadGroup g = current.getThreadGroup(); |
290 |
< |
SecurityManager s = System.getSecurityManager(); |
291 |
< |
if (s != null) |
292 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
293 |
< |
else |
294 |
< |
threadAssertTrue(g == egroup); |
295 |
< |
String name = current.getName(); |
296 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
297 |
< |
} catch (SecurityException ok) { |
298 |
< |
// Also pass if not allowed to change setting |
322 |
< |
} |
283 |
> |
Runnable r = new CheckedRunnable() { |
284 |
> |
public void realRun() { |
285 |
> |
try { |
286 |
> |
Thread current = Thread.currentThread(); |
287 |
> |
assertTrue(!current.isDaemon()); |
288 |
> |
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
289 |
> |
ThreadGroup g = current.getThreadGroup(); |
290 |
> |
SecurityManager s = System.getSecurityManager(); |
291 |
> |
if (s != null) |
292 |
> |
assertTrue(g == s.getThreadGroup()); |
293 |
> |
else |
294 |
> |
assertTrue(g == egroup); |
295 |
> |
String name = current.getName(); |
296 |
> |
assertTrue(name.endsWith("thread-1")); |
297 |
> |
} catch (SecurityException ok) { |
298 |
> |
// Also pass if not allowed to change setting |
299 |
|
} |
300 |
< |
}; |
300 |
> |
}}; |
301 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
302 |
|
|
303 |
|
e.execute(r); |
307 |
|
} |
308 |
|
|
309 |
|
try { |
310 |
< |
Thread.sleep(SHORT_DELAY_MS); |
310 |
> |
delay(SHORT_DELAY_MS); |
311 |
|
} finally { |
312 |
|
joinPool(e); |
313 |
|
} |
319 |
|
* access control context and context class loader |
320 |
|
*/ |
321 |
|
public void testPrivilegedThreadFactory() throws Exception { |
322 |
< |
Policy savedPolicy = null; |
323 |
< |
try { |
324 |
< |
savedPolicy = Policy.getPolicy(); |
325 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
326 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
327 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
328 |
< |
Policy.setPolicy(policy); |
353 |
< |
} catch (AccessControlException ok) { |
354 |
< |
return; |
355 |
< |
} |
356 |
< |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
357 |
< |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
358 |
< |
final AccessControlContext thisacc = AccessController.getContext(); |
359 |
< |
Runnable r = new Runnable() { |
360 |
< |
public void run() { |
361 |
< |
try { |
322 |
> |
Runnable r = new CheckedRunnable() { |
323 |
> |
public void realRun() throws Exception { |
324 |
> |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
325 |
> |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
326 |
> |
final AccessControlContext thisacc = AccessController.getContext(); |
327 |
> |
Runnable r = new CheckedRunnable() { |
328 |
> |
public void realRun() { |
329 |
|
Thread current = Thread.currentThread(); |
330 |
< |
threadAssertTrue(!current.isDaemon()); |
331 |
< |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
330 |
> |
assertTrue(!current.isDaemon()); |
331 |
> |
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
332 |
|
ThreadGroup g = current.getThreadGroup(); |
333 |
|
SecurityManager s = System.getSecurityManager(); |
334 |
|
if (s != null) |
335 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
335 |
> |
assertTrue(g == s.getThreadGroup()); |
336 |
|
else |
337 |
< |
threadAssertTrue(g == egroup); |
337 |
> |
assertTrue(g == egroup); |
338 |
|
String name = current.getName(); |
339 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
340 |
< |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
341 |
< |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
342 |
< |
} catch (SecurityException ok) { |
343 |
< |
// Also pass if not allowed to change settings |
344 |
< |
} |
345 |
< |
} |
346 |
< |
}; |
347 |
< |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
339 |
> |
assertTrue(name.endsWith("thread-1")); |
340 |
> |
assertSame(thisccl, current.getContextClassLoader()); |
341 |
> |
assertEquals(thisacc, AccessController.getContext()); |
342 |
> |
}}; |
343 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
344 |
> |
e.execute(r); |
345 |
> |
e.shutdown(); |
346 |
> |
delay(SHORT_DELAY_MS); |
347 |
> |
joinPool(e); |
348 |
> |
}}; |
349 |
> |
|
350 |
> |
runWithPermissions(r, |
351 |
> |
new RuntimePermission("getClassLoader"), |
352 |
> |
new RuntimePermission("setContextClassLoader"), |
353 |
> |
new RuntimePermission("modifyThread")); |
354 |
> |
} |
355 |
|
|
356 |
< |
Policy.setPolicy(savedPolicy); |
357 |
< |
e.execute(r); |
358 |
< |
try { |
359 |
< |
e.shutdown(); |
360 |
< |
} catch (SecurityException ok) { |
361 |
< |
} |
362 |
< |
try { |
363 |
< |
Thread.sleep(SHORT_DELAY_MS); |
364 |
< |
} finally { |
391 |
< |
joinPool(e); |
356 |
> |
boolean haveCCLPermissions() { |
357 |
> |
SecurityManager sm = System.getSecurityManager(); |
358 |
> |
if (sm != null) { |
359 |
> |
try { |
360 |
> |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
361 |
> |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
362 |
> |
} catch (AccessControlException e) { |
363 |
> |
return false; |
364 |
> |
} |
365 |
|
} |
366 |
+ |
return true; |
367 |
|
} |
368 |
|
|
369 |
|
void checkCCL() { |
381 |
|
} |
382 |
|
} |
383 |
|
|
410 |
– |
|
384 |
|
/** |
385 |
|
* Without class loader permissions, creating |
386 |
|
* privilegedCallableUsingCurrentClassLoader throws ACE |
387 |
|
*/ |
388 |
|
public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { |
389 |
< |
Policy savedPolicy = null; |
390 |
< |
try { |
391 |
< |
savedPolicy = Policy.getPolicy(); |
392 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
393 |
< |
Policy.setPolicy(policy); |
394 |
< |
} catch (AccessControlException ok) { |
395 |
< |
return; |
396 |
< |
} |
397 |
< |
|
425 |
< |
// Check if program still has too many permissions to run test |
426 |
< |
try { |
427 |
< |
checkCCL(); |
428 |
< |
// too many privileges to test; so return |
429 |
< |
Policy.setPolicy(savedPolicy); |
430 |
< |
return; |
431 |
< |
} catch (AccessControlException ok) { |
432 |
< |
} |
389 |
> |
Runnable r = new CheckedRunnable() { |
390 |
> |
public void realRun() throws Exception { |
391 |
> |
if (System.getSecurityManager() == null) |
392 |
> |
return; |
393 |
> |
try { |
394 |
> |
Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
395 |
> |
shouldThrow(); |
396 |
> |
} catch (AccessControlException success) {} |
397 |
> |
}}; |
398 |
|
|
399 |
< |
try { |
435 |
< |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
436 |
< |
shouldThrow(); |
437 |
< |
} catch (AccessControlException success) { |
438 |
< |
} finally { |
439 |
< |
Policy.setPolicy(savedPolicy); |
440 |
< |
} |
399 |
> |
runWithoutPermissions(r); |
400 |
|
} |
401 |
|
|
402 |
|
/** |
404 |
|
* privilegedCallableUsingCurrentClassLoader does not throw ACE |
405 |
|
*/ |
406 |
|
public void testprivilegedCallableUsingCCLWithPrivs() throws Exception { |
407 |
< |
Policy savedPolicy = null; |
408 |
< |
try { |
409 |
< |
savedPolicy = Policy.getPolicy(); |
410 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
411 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
412 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
413 |
< |
Policy.setPolicy(policy); |
414 |
< |
} catch (AccessControlException ok) { |
415 |
< |
return; |
416 |
< |
} |
458 |
< |
|
459 |
< |
try { |
460 |
< |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
461 |
< |
task.call(); |
462 |
< |
} |
463 |
< |
finally { |
464 |
< |
Policy.setPolicy(savedPolicy); |
465 |
< |
} |
407 |
> |
Runnable r = new CheckedRunnable() { |
408 |
> |
public void realRun() throws Exception { |
409 |
> |
Executors.privilegedCallableUsingCurrentClassLoader |
410 |
> |
(new NoOpCallable()) |
411 |
> |
.call(); |
412 |
> |
}}; |
413 |
> |
|
414 |
> |
runWithPermissions(r, |
415 |
> |
new RuntimePermission("getClassLoader"), |
416 |
> |
new RuntimePermission("setContextClassLoader")); |
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
|
* Without permissions, calling privilegedCallable throws ACE |
421 |
|
*/ |
422 |
|
public void testprivilegedCallableWithNoPrivs() throws Exception { |
423 |
< |
Callable task; |
424 |
< |
Policy savedPolicy = null; |
474 |
< |
AdjustablePolicy policy = null; |
475 |
< |
AccessControlContext noprivAcc = null; |
476 |
< |
try { |
477 |
< |
savedPolicy = Policy.getPolicy(); |
478 |
< |
policy = new AdjustablePolicy(); |
479 |
< |
Policy.setPolicy(policy); |
480 |
< |
noprivAcc = AccessController.getContext(); |
481 |
< |
task = Executors.privilegedCallable(new CheckCCL()); |
482 |
< |
Policy.setPolicy(savedPolicy); |
483 |
< |
} catch (AccessControlException ok) { |
484 |
< |
return; // program has too few permissions to set up test |
485 |
< |
} |
486 |
< |
|
487 |
< |
// Make sure that program doesn't have too many permissions |
488 |
< |
try { |
489 |
< |
AccessController.doPrivileged(new PrivilegedAction() { |
490 |
< |
public Object run() { |
491 |
< |
checkCCL(); |
492 |
< |
return null; |
493 |
< |
}}, noprivAcc); |
494 |
< |
// too many permssions; skip test |
495 |
< |
return; |
496 |
< |
} catch (AccessControlException ok) { |
497 |
< |
} |
423 |
> |
// Avoid classloader-related SecurityExceptions in swingui.TestRunner |
424 |
> |
Executors.privilegedCallable(new CheckCCL()); |
425 |
|
|
426 |
< |
try { |
427 |
< |
task.call(); |
428 |
< |
shouldThrow(); |
429 |
< |
} catch (AccessControlException success) {} |
426 |
> |
Runnable r = new CheckedRunnable() { |
427 |
> |
public void realRun() throws Exception { |
428 |
> |
if (System.getSecurityManager() == null) |
429 |
> |
return; |
430 |
> |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
431 |
> |
try { |
432 |
> |
task.call(); |
433 |
> |
shouldThrow(); |
434 |
> |
} catch (AccessControlException success) {} |
435 |
> |
}}; |
436 |
> |
|
437 |
> |
runWithoutPermissions(r); |
438 |
> |
|
439 |
> |
// It seems rather difficult to test that the |
440 |
> |
// AccessControlContext of the privilegedCallable is used |
441 |
> |
// instead of its caller. Below is a failed attempt to do |
442 |
> |
// that, which does not work because the AccessController |
443 |
> |
// cannot capture the internal state of the current Policy. |
444 |
> |
// It would be much more work to differentiate based on, |
445 |
> |
// e.g. CodeSource. |
446 |
> |
|
447 |
> |
// final AccessControlContext[] noprivAcc = new AccessControlContext[1]; |
448 |
> |
// final Callable[] task = new Callable[1]; |
449 |
> |
|
450 |
> |
// runWithPermissions |
451 |
> |
// (new CheckedRunnable() { |
452 |
> |
// public void realRun() { |
453 |
> |
// if (System.getSecurityManager() == null) |
454 |
> |
// return; |
455 |
> |
// noprivAcc[0] = AccessController.getContext(); |
456 |
> |
// task[0] = Executors.privilegedCallable(new CheckCCL()); |
457 |
> |
// try { |
458 |
> |
// AccessController.doPrivileged(new PrivilegedAction<Void>() { |
459 |
> |
// public Void run() { |
460 |
> |
// checkCCL(); |
461 |
> |
// return null; |
462 |
> |
// }}, noprivAcc[0]); |
463 |
> |
// shouldThrow(); |
464 |
> |
// } catch (AccessControlException success) {} |
465 |
> |
// }}); |
466 |
> |
|
467 |
> |
// runWithPermissions |
468 |
> |
// (new CheckedRunnable() { |
469 |
> |
// public void realRun() throws Exception { |
470 |
> |
// if (System.getSecurityManager() == null) |
471 |
> |
// return; |
472 |
> |
// // Verify that we have an underprivileged ACC |
473 |
> |
// try { |
474 |
> |
// AccessController.doPrivileged(new PrivilegedAction<Void>() { |
475 |
> |
// public Void run() { |
476 |
> |
// checkCCL(); |
477 |
> |
// return null; |
478 |
> |
// }}, noprivAcc[0]); |
479 |
> |
// shouldThrow(); |
480 |
> |
// } catch (AccessControlException success) {} |
481 |
> |
|
482 |
> |
// try { |
483 |
> |
// task[0].call(); |
484 |
> |
// shouldThrow(); |
485 |
> |
// } catch (AccessControlException success) {} |
486 |
> |
// }}, |
487 |
> |
// new RuntimePermission("getClassLoader"), |
488 |
> |
// new RuntimePermission("setContextClassLoader")); |
489 |
|
} |
490 |
|
|
491 |
|
/** |
492 |
|
* With permissions, calling privilegedCallable succeeds |
493 |
|
*/ |
494 |
|
public void testprivilegedCallableWithPrivs() throws Exception { |
495 |
< |
Policy savedPolicy = null; |
496 |
< |
try { |
497 |
< |
savedPolicy = Policy.getPolicy(); |
498 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
499 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
500 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
501 |
< |
Policy.setPolicy(policy); |
502 |
< |
} catch (AccessControlException ok) { |
517 |
< |
return; |
518 |
< |
} |
519 |
< |
|
520 |
< |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
521 |
< |
try { |
522 |
< |
task.call(); |
523 |
< |
} finally { |
524 |
< |
Policy.setPolicy(savedPolicy); |
525 |
< |
} |
495 |
> |
Runnable r = new CheckedRunnable() { |
496 |
> |
public void realRun() throws Exception { |
497 |
> |
Executors.privilegedCallable(new CheckCCL()).call(); |
498 |
> |
}}; |
499 |
> |
|
500 |
> |
runWithPermissions(r, |
501 |
> |
new RuntimePermission("getClassLoader"), |
502 |
> |
new RuntimePermission("setContextClassLoader")); |
503 |
|
} |
504 |
|
|
505 |
|
/** |
536 |
|
assertSame(one, c.call()); |
537 |
|
} |
538 |
|
|
562 |
– |
|
539 |
|
/** |
540 |
|
* callable(null Runnable) throws NPE |
541 |
|
*/ |
576 |
|
} catch (NullPointerException success) {} |
577 |
|
} |
578 |
|
|
603 |
– |
|
579 |
|
} |