ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorsTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ExecutorsTest.java (file contents):
Revision 1.27 by jsr166, Tue Dec 1 09:56:28 2009 UTC vs.
Revision 1.43 by jsr166, Wed Dec 31 19:05:42 2014 UTC

# Line 1 | Line 1
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
10 import junit.framework.*;
11 import java.util.*;
12 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.math.BigInteger;
11 < import java.security.*;
10 >
11 > import java.security.AccessControlContext;
12 > import java.security.AccessControlException;
13 > import java.security.AccessController;
14 > import java.security.PrivilegedAction;
15 > import java.security.PrivilegedExceptionAction;
16 > import java.util.ArrayList;
17 > import java.util.List;
18 > import java.util.concurrent.Callable;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 > import java.util.concurrent.Future;
23 > import java.util.concurrent.ScheduledExecutorService;
24 > import java.util.concurrent.ThreadPoolExecutor;
25 >
26 > import junit.framework.Test;
27 > import junit.framework.TestSuite;
28  
29   public class ExecutorsTest extends JSR166TestCase {
30      public static void main(String[] args) {
31 <        junit.textui.TestRunner.run (suite());
31 >        junit.textui.TestRunner.run(suite());
32      }
33      public static Test suite() {
34          return new TestSuite(ExecutorsTest.class);
35      }
36  
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
37      /**
38       * A newCachedThreadPool can execute runnables
39       */
# Line 94 | Line 66 | public class ExecutorsTest extends JSR16
66          } catch (NullPointerException success) {}
67      }
68  
97
69      /**
70       * A new SingleThreadExecutor can execute runnables
71       */
# Line 141 | Line 112 | public class ExecutorsTest extends JSR16
112          }
113      }
114  
144
115      /**
116       * A new newFixedThreadPool can execute runnables
117       */
# Line 184 | Line 154 | public class ExecutorsTest extends JSR16
154          } catch (IllegalArgumentException success) {}
155      }
156  
187
157      /**
158       * An unconfigurable newFixedThreadPool can execute runnables
159       */
160 <    public void testunconfigurableExecutorService() {
160 >    public void testUnconfigurableExecutorService() {
161          ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2));
162          e.execute(new NoOpRunnable());
163          e.execute(new NoOpRunnable());
# Line 199 | Line 168 | public class ExecutorsTest extends JSR16
168      /**
169       * unconfigurableExecutorService(null) throws NPE
170       */
171 <    public void testunconfigurableExecutorServiceNPE() {
171 >    public void testUnconfigurableExecutorServiceNPE() {
172          try {
173              ExecutorService e = Executors.unconfigurableExecutorService(null);
174              shouldThrow();
# Line 209 | Line 178 | public class ExecutorsTest extends JSR16
178      /**
179       * unconfigurableScheduledExecutorService(null) throws NPE
180       */
181 <    public void testunconfigurableScheduledExecutorServiceNPE() {
181 >    public void testUnconfigurableScheduledExecutorServiceNPE() {
182          try {
183              ExecutorService e = Executors.unconfigurableScheduledExecutorService(null);
184              shouldThrow();
185          } catch (NullPointerException success) {}
186      }
187  
219
188      /**
189       * a newSingleThreadScheduledExecutor successfully runs delayed task
190       */
191      public void testNewSingleThreadScheduledExecutor() throws Exception {
192 <        TrackedCallable callable = new TrackedCallable();
193 <        ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor();
194 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
195 <        assertFalse(callable.done);
196 <        Thread.sleep(MEDIUM_DELAY_MS);
197 <        assertTrue(callable.done);
198 <        assertEquals(Boolean.TRUE, f.get());
199 <        joinPool(p1);
192 >        ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor();
193 >        try {
194 >            final CountDownLatch proceed = new CountDownLatch(1);
195 >            final Runnable task = new CheckedRunnable() {
196 >                public void realRun() {
197 >                    await(proceed);
198 >                }};
199 >            long startTime = System.nanoTime();
200 >            Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
201 >                                  timeoutMillis(), MILLISECONDS);
202 >            assertFalse(f.isDone());
203 >            proceed.countDown();
204 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
205 >            assertSame(Boolean.TRUE, f.get());
206 >            assertTrue(f.isDone());
207 >            assertFalse(f.isCancelled());
208 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
209 >        } finally {
210 >            joinPool(p);
211 >        }
212      }
213  
214      /**
215       * a newScheduledThreadPool successfully runs delayed task
216       */
217 <    public void testnewScheduledThreadPool() throws Exception {
218 <        TrackedCallable callable = new TrackedCallable();
219 <        ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2);
220 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
221 <        assertFalse(callable.done);
222 <        Thread.sleep(MEDIUM_DELAY_MS);
223 <        assertTrue(callable.done);
224 <        assertEquals(Boolean.TRUE, f.get());
225 <        joinPool(p1);
217 >    public void testNewScheduledThreadPool() throws Exception {
218 >        ScheduledExecutorService p = Executors.newScheduledThreadPool(2);
219 >        try {
220 >            final CountDownLatch proceed = new CountDownLatch(1);
221 >            final Runnable task = new CheckedRunnable() {
222 >                public void realRun() {
223 >                    await(proceed);
224 >                }};
225 >            long startTime = System.nanoTime();
226 >            Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
227 >                                  timeoutMillis(), MILLISECONDS);
228 >            assertFalse(f.isDone());
229 >            proceed.countDown();
230 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
231 >            assertSame(Boolean.TRUE, f.get());
232 >            assertTrue(f.isDone());
233 >            assertFalse(f.isCancelled());
234 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
235 >        } finally {
236 >            joinPool(p);
237 >        }
238      }
239  
240      /**
241       * an unconfigurable newScheduledThreadPool successfully runs delayed task
242       */
243 <    public void testunconfigurableScheduledExecutorService() throws Exception {
244 <        TrackedCallable callable = new TrackedCallable();
245 <        ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2));
246 <        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
247 <        assertFalse(callable.done);
248 <        Thread.sleep(MEDIUM_DELAY_MS);
249 <        assertTrue(callable.done);
250 <        assertEquals(Boolean.TRUE, f.get());
251 <        joinPool(p1);
243 >    public void testUnconfigurableScheduledExecutorService() throws Exception {
244 >        ScheduledExecutorService p =
245 >            Executors.unconfigurableScheduledExecutorService
246 >            (Executors.newScheduledThreadPool(2));
247 >        try {
248 >            final CountDownLatch proceed = new CountDownLatch(1);
249 >            final Runnable task = new CheckedRunnable() {
250 >                public void realRun() {
251 >                    await(proceed);
252 >                }};
253 >            long startTime = System.nanoTime();
254 >            Future f = p.schedule(Executors.callable(task, Boolean.TRUE),
255 >                                  timeoutMillis(), MILLISECONDS);
256 >            assertFalse(f.isDone());
257 >            proceed.countDown();
258 >            assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS));
259 >            assertSame(Boolean.TRUE, f.get());
260 >            assertTrue(f.isDone());
261 >            assertFalse(f.isCancelled());
262 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
263 >        } finally {
264 >            joinPool(p);
265 >        }
266      }
267  
268      /**
269 <     *  timeouts from execute will time out if they compute too long.
269 >     * Future.get on submitted tasks will time out if they compute too long.
270       */
271      public void testTimedCallable() throws Exception {
272 <        int N = 10000;
273 <        ExecutorService executor = Executors.newSingleThreadExecutor();
274 <        List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N);
275 <        try {
276 <            long startTime = System.currentTimeMillis();
277 <
278 <            long i = 0;
279 <            while (tasks.size() < N) {
280 <                tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1));
281 <                i += 10;
282 <            }
283 <
284 <            int iters = 0;
285 <            BigInteger sum = BigInteger.ZERO;
286 <            for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) {
287 <                try {
288 <                    ++iters;
289 <                    sum = sum.add(it.next().call());
290 <                }
291 <                catch (TimeoutException success) {
292 <                    assertTrue(iters > 0);
293 <                    return;
294 <                }
295 <            }
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 {
272 >        final ExecutorService[] executors = {
273 >            Executors.newSingleThreadExecutor(),
274 >            Executors.newCachedThreadPool(),
275 >            Executors.newFixedThreadPool(2),
276 >            Executors.newScheduledThreadPool(2),
277 >        };
278 >
279 >        final Runnable sleeper = new CheckedInterruptedRunnable() {
280 >            public void realRun() throws InterruptedException {
281 >                delay(LONG_DELAY_MS);
282 >            }};
283 >
284 >        List<Thread> threads = new ArrayList<Thread>();
285 >        for (final ExecutorService executor : executors) {
286 >            threads.add(newStartedThread(new CheckedRunnable() {
287 >                public void realRun() {
288 >                    long startTime = System.nanoTime();
289 >                    Future future = executor.submit(sleeper);
290 >                    assertFutureTimesOut(future);
291 >                }}));
292 >        }
293 >        for (Thread thread : threads)
294 >            awaitTermination(thread);
295 >        for (ExecutorService executor : executors)
296              joinPool(executor);
296        }
297      }
298  
299
299      /**
300       * ThreadPoolExecutor using defaultThreadFactory has
301       * specified group, priority, daemon status, and name
302       */
303      public void testDefaultThreadFactory() throws Exception {
304          final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
305 <        Runnable r = new Runnable() {
306 <                public void run() {
307 <                    try {
308 <                        Thread current = Thread.currentThread();
309 <                        threadAssertTrue(!current.isDaemon());
310 <                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
311 <                        ThreadGroup g = current.getThreadGroup();
312 <                        SecurityManager s = System.getSecurityManager();
313 <                        if (s != null)
314 <                            threadAssertTrue(g == s.getThreadGroup());
315 <                        else
316 <                            threadAssertTrue(g == egroup);
317 <                        String name = current.getName();
318 <                        threadAssertTrue(name.endsWith("thread-1"));
319 <                    } catch (SecurityException ok) {
320 <                        // Also pass if not allowed to change setting
321 <                    }
305 >        final CountDownLatch done = new CountDownLatch(1);
306 >        Runnable r = new CheckedRunnable() {
307 >            public void realRun() {
308 >                try {
309 >                    Thread current = Thread.currentThread();
310 >                    assertTrue(!current.isDaemon());
311 >                    assertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
312 >                    ThreadGroup g = current.getThreadGroup();
313 >                    SecurityManager s = System.getSecurityManager();
314 >                    if (s != null)
315 >                        assertTrue(g == s.getThreadGroup());
316 >                    else
317 >                        assertTrue(g == egroup);
318 >                    String name = current.getName();
319 >                    assertTrue(name.endsWith("thread-1"));
320 >                } catch (SecurityException ok) {
321 >                    // Also pass if not allowed to change setting
322                  }
323 <            };
323 >                done.countDown();
324 >            }};
325          ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
326  
327          e.execute(r);
328 +        await(done);
329 +
330          try {
331              e.shutdown();
332          } catch (SecurityException ok) {
333          }
334  
335 <        try {
334 <            Thread.sleep(SHORT_DELAY_MS);
335 <        } finally {
336 <            joinPool(e);
337 <        }
335 >        joinPool(e);
336      }
337  
338      /**
# Line 343 | Line 341 | public class ExecutorsTest extends JSR16
341       * access control context and context class loader
342       */
343      public void testPrivilegedThreadFactory() throws Exception {
344 <        Policy savedPolicy = null;
345 <        try {
346 <            savedPolicy = Policy.getPolicy();
347 <            AdjustablePolicy policy = new AdjustablePolicy();
348 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
349 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
350 <            Policy.setPolicy(policy);
351 <        } 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 {
344 >        final CountDownLatch done = new CountDownLatch(1);
345 >        Runnable r = new CheckedRunnable() {
346 >            public void realRun() throws Exception {
347 >                final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
348 >                final ClassLoader thisccl = Thread.currentThread().getContextClassLoader();
349 >                final AccessControlContext thisacc = AccessController.getContext();
350 >                Runnable r = new CheckedRunnable() {
351 >                    public void realRun() {
352                          Thread current = Thread.currentThread();
353 <                        threadAssertTrue(!current.isDaemon());
354 <                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
353 >                        assertTrue(!current.isDaemon());
354 >                        assertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
355                          ThreadGroup g = current.getThreadGroup();
356                          SecurityManager s = System.getSecurityManager();
357                          if (s != null)
358 <                            threadAssertTrue(g == s.getThreadGroup());
358 >                            assertTrue(g == s.getThreadGroup());
359                          else
360 <                            threadAssertTrue(g == egroup);
360 >                            assertTrue(g == egroup);
361                          String name = current.getName();
362 <                        threadAssertTrue(name.endsWith("thread-1"));
363 <                        threadAssertTrue(thisccl == current.getContextClassLoader());
364 <                        threadAssertTrue(thisacc.equals(AccessController.getContext()));
365 <                    } catch (SecurityException ok) {
366 <                        // Also pass if not allowed to change settings
367 <                    }
368 <                }
369 <            };
370 <        ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
362 >                        assertTrue(name.endsWith("thread-1"));
363 >                        assertSame(thisccl, current.getContextClassLoader());
364 >                        assertEquals(thisacc, AccessController.getContext());
365 >                        done.countDown();
366 >                    }};
367 >                ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
368 >                e.execute(r);
369 >                await(done);
370 >                e.shutdown();
371 >                joinPool(e);
372 >            }};
373 >
374 >        runWithPermissions(r,
375 >                           new RuntimePermission("getClassLoader"),
376 >                           new RuntimePermission("setContextClassLoader"),
377 >                           new RuntimePermission("modifyThread"));
378 >    }
379  
380 <        Policy.setPolicy(savedPolicy);
381 <        e.execute(r);
382 <        try {
383 <            e.shutdown();
384 <        } catch (SecurityException ok) {
385 <        }
386 <        try {
387 <            Thread.sleep(SHORT_DELAY_MS);
388 <        } finally {
391 <            joinPool(e);
380 >    boolean haveCCLPermissions() {
381 >        SecurityManager sm = System.getSecurityManager();
382 >        if (sm != null) {
383 >            try {
384 >                sm.checkPermission(new RuntimePermission("setContextClassLoader"));
385 >                sm.checkPermission(new RuntimePermission("getClassLoader"));
386 >            } catch (AccessControlException e) {
387 >                return false;
388 >            }
389          }
390 +        return true;
391      }
392  
393      void checkCCL() {
# Line 407 | Line 405 | public class ExecutorsTest extends JSR16
405          }
406      }
407  
410
408      /**
409       * Without class loader permissions, creating
410       * privilegedCallableUsingCurrentClassLoader throws ACE
411       */
412      public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() {
413 <        Policy savedPolicy = null;
414 <        try {
415 <            savedPolicy = Policy.getPolicy();
416 <            AdjustablePolicy policy = new AdjustablePolicy();
417 <            Policy.setPolicy(policy);
418 <        } catch (AccessControlException ok) {
419 <            return;
420 <        }
421 <
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 <        }
413 >        Runnable r = new CheckedRunnable() {
414 >            public void realRun() throws Exception {
415 >                if (System.getSecurityManager() == null)
416 >                    return;
417 >                try {
418 >                    Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
419 >                    shouldThrow();
420 >                } catch (AccessControlException success) {}
421 >            }};
422  
423 <        try {
435 <            Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
436 <            shouldThrow();
437 <        } catch (AccessControlException success) {
438 <        } finally {
439 <            Policy.setPolicy(savedPolicy);
440 <        }
423 >        runWithoutPermissions(r);
424      }
425  
426      /**
427       * With class loader permissions, calling
428       * privilegedCallableUsingCurrentClassLoader does not throw ACE
429       */
430 <    public void testprivilegedCallableUsingCCLWithPrivs() throws Exception {
431 <        Policy savedPolicy = null;
432 <        try {
433 <            savedPolicy = Policy.getPolicy();
434 <            AdjustablePolicy policy = new AdjustablePolicy();
435 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
436 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
437 <            Policy.setPolicy(policy);
438 <        } catch (AccessControlException ok) {
439 <            return;
440 <        }
458 <
459 <        try {
460 <            Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
461 <            task.call();
462 <        }
463 <        finally {
464 <            Policy.setPolicy(savedPolicy);
465 <        }
430 >    public void testPrivilegedCallableUsingCCLWithPrivs() throws Exception {
431 >        Runnable r = new CheckedRunnable() {
432 >            public void realRun() throws Exception {
433 >                Executors.privilegedCallableUsingCurrentClassLoader
434 >                    (new NoOpCallable())
435 >                    .call();
436 >            }};
437 >
438 >        runWithPermissions(r,
439 >                           new RuntimePermission("getClassLoader"),
440 >                           new RuntimePermission("setContextClassLoader"));
441      }
442  
443      /**
444       * Without permissions, calling privilegedCallable throws ACE
445       */
446 <    public void testprivilegedCallableWithNoPrivs() throws Exception {
447 <        Callable task;
448 <        Policy savedPolicy = null;
449 <        AdjustablePolicy policy = null;
450 <        AccessControlContext noprivAcc = null;
451 <        try {
452 <            savedPolicy = Policy.getPolicy();
453 <            policy = new AdjustablePolicy();
454 <            Policy.setPolicy(policy);
455 <            noprivAcc = AccessController.getContext();
456 <            task = Executors.privilegedCallable(new CheckCCL());
457 <            Policy.setPolicy(savedPolicy);
458 <        } catch (AccessControlException ok) {
459 <            return; // program has too few permissions to set up test
460 <        }
461 <
462 <        // Make sure that program doesn't have too many permissions
463 <        try {
464 <            AccessController.doPrivileged(new PrivilegedAction() {
465 <                    public Object run() {
466 <                        checkCCL();
467 <                        return null;
468 <                    }}, noprivAcc);
469 <            // too many permssions; skip test
470 <            return;
471 <        } catch (AccessControlException ok) {
472 <        }
473 <
474 <        try {
475 <            task.call();
476 <            shouldThrow();
477 <        } catch (AccessControlException success) {}
446 >    public void testPrivilegedCallableWithNoPrivs() throws Exception {
447 >        // Avoid classloader-related SecurityExceptions in swingui.TestRunner
448 >        Executors.privilegedCallable(new CheckCCL());
449 >
450 >        Runnable r = new CheckedRunnable() {
451 >            public void realRun() throws Exception {
452 >                if (System.getSecurityManager() == null)
453 >                    return;
454 >                Callable task = Executors.privilegedCallable(new CheckCCL());
455 >                try {
456 >                    task.call();
457 >                    shouldThrow();
458 >                } catch (AccessControlException success) {}
459 >            }};
460 >
461 >        runWithoutPermissions(r);
462 >
463 >        // It seems rather difficult to test that the
464 >        // AccessControlContext of the privilegedCallable is used
465 >        // instead of its caller.  Below is a failed attempt to do
466 >        // that, which does not work because the AccessController
467 >        // cannot capture the internal state of the current Policy.
468 >        // It would be much more work to differentiate based on,
469 >        // e.g. CodeSource.
470 >
471 > //         final AccessControlContext[] noprivAcc = new AccessControlContext[1];
472 > //         final Callable[] task = new Callable[1];
473 >
474 > //         runWithPermissions
475 > //             (new CheckedRunnable() {
476 > //                 public void realRun() {
477 > //                     if (System.getSecurityManager() == null)
478 > //                         return;
479 > //                     noprivAcc[0] = AccessController.getContext();
480 > //                     task[0] = Executors.privilegedCallable(new CheckCCL());
481 > //                     try {
482 > //                         AccessController.doPrivileged(new PrivilegedAction<Void>() {
483 > //                                                           public Void run() {
484 > //                                                               checkCCL();
485 > //                                                               return null;
486 > //                                                           }}, noprivAcc[0]);
487 > //                         shouldThrow();
488 > //                     } catch (AccessControlException success) {}
489 > //                 }});
490 >
491 > //         runWithPermissions
492 > //             (new CheckedRunnable() {
493 > //                 public void realRun() throws Exception {
494 > //                     if (System.getSecurityManager() == null)
495 > //                         return;
496 > //                     // Verify that we have an underprivileged ACC
497 > //                     try {
498 > //                         AccessController.doPrivileged(new PrivilegedAction<Void>() {
499 > //                                                           public Void run() {
500 > //                                                               checkCCL();
501 > //                                                               return null;
502 > //                                                           }}, noprivAcc[0]);
503 > //                         shouldThrow();
504 > //                     } catch (AccessControlException success) {}
505 >
506 > //                     try {
507 > //                         task[0].call();
508 > //                         shouldThrow();
509 > //                     } catch (AccessControlException success) {}
510 > //                 }},
511 > //              new RuntimePermission("getClassLoader"),
512 > //              new RuntimePermission("setContextClassLoader"));
513      }
514  
515      /**
516       * With permissions, calling privilegedCallable succeeds
517       */
518 <    public void testprivilegedCallableWithPrivs() throws Exception {
519 <        Policy savedPolicy = null;
520 <        try {
521 <            savedPolicy = Policy.getPolicy();
522 <            AdjustablePolicy policy = new AdjustablePolicy();
523 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
524 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
525 <            Policy.setPolicy(policy);
526 <        } 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 <        }
518 >    public void testPrivilegedCallableWithPrivs() throws Exception {
519 >        Runnable r = new CheckedRunnable() {
520 >            public void realRun() throws Exception {
521 >                Executors.privilegedCallable(new CheckCCL()).call();
522 >            }};
523 >
524 >        runWithPermissions(r,
525 >                           new RuntimePermission("getClassLoader"),
526 >                           new RuntimePermission("setContextClassLoader"));
527      }
528  
529      /**
# Line 559 | Line 560 | public class ExecutorsTest extends JSR16
560          assertSame(one, c.call());
561      }
562  
562
563      /**
564       * callable(null Runnable) throws NPE
565       */
# Line 600 | Line 600 | public class ExecutorsTest extends JSR16
600          } catch (NullPointerException success) {}
601      }
602  
603
603   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines