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.25 by jsr166, Sat Nov 21 02:07:26 2009 UTC vs.
Revision 1.44 by jsr166, Sun Feb 22 19:16:12 2015 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 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11 < import junit.framework.*;
12 < import java.util.*;
13 < import java.util.concurrent.*;
14 < import java.math.BigInteger;
15 < import java.security.*;
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  
24    static class TimedCallable<T> implements Callable<T> {
25        private final ExecutorService exec;
26        private final Callable<T> func;
27        private final long msecs;
28
29        TimedCallable(ExecutorService exec, Callable<T> func, long msecs) {
30            this.exec = exec;
31            this.func = func;
32            this.msecs = msecs;
33        }
34
35        public T call() throws Exception {
36            Future<T> ftask = exec.submit(func);
37            try {
38                return ftask.get(msecs, TimeUnit.MILLISECONDS);
39            } finally {
40                ftask.cancel(true);
41            }
42        }
43    }
44
45
46    private static class Fib implements Callable<BigInteger> {
47        private final BigInteger n;
48        Fib(long n) {
49            if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n);
50            this.n = BigInteger.valueOf(n);
51        }
52        public BigInteger call() {
53            BigInteger f1 = BigInteger.ONE;
54            BigInteger f2 = f1;
55            for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
56                BigInteger t = f1.add(f2);
57                f1 = f2;
58                f2 = t;
59            }
60            return f1;
61        }
62    };
63
37      /**
38       * A newCachedThreadPool can execute runnables
39       */
# Line 93 | Line 66 | public class ExecutorsTest extends JSR16
66          } catch (NullPointerException success) {}
67      }
68  
96
69      /**
70       * A new SingleThreadExecutor can execute runnables
71       */
# Line 140 | Line 112 | public class ExecutorsTest extends JSR16
112          }
113      }
114  
143
115      /**
116       * A new newFixedThreadPool can execute runnables
117       */
# Line 183 | Line 154 | public class ExecutorsTest extends JSR16
154          } catch (IllegalArgumentException success) {}
155      }
156  
186
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 198 | 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 208 | 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  
218
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, TimeUnit.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, TimeUnit.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, TimeUnit.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 <                }
288 <            }
289 <            // if by chance we didn't ever time out, total time must be small
290 <            long elapsed = System.currentTimeMillis() - startTime;
291 <            assertTrue(elapsed < N);
292 <        }
293 <        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 >                    Future future = executor.submit(sleeper);
289 >                    assertFutureTimesOut(future);
290 >                }}));
291 >        }
292 >        for (Thread thread : threads)
293 >            awaitTermination(thread);
294 >        for (ExecutorService executor : executors)
295              joinPool(executor);
295        }
296      }
297  
298
298      /**
299       * ThreadPoolExecutor using defaultThreadFactory has
300       * specified group, priority, daemon status, and name
301       */
302      public void testDefaultThreadFactory() throws Exception {
303          final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
304 <        Runnable r = new Runnable() {
305 <                public void run() {
306 <                    try {
307 <                        Thread current = Thread.currentThread();
308 <                        threadAssertTrue(!current.isDaemon());
309 <                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
310 <                        ThreadGroup g = current.getThreadGroup();
311 <                        SecurityManager s = System.getSecurityManager();
312 <                        if (s != null)
313 <                            threadAssertTrue(g == s.getThreadGroup());
314 <                        else
315 <                            threadAssertTrue(g == egroup);
316 <                        String name = current.getName();
317 <                        threadAssertTrue(name.endsWith("thread-1"));
318 <                    } catch (SecurityException ok) {
319 <                        // Also pass if not allowed to change setting
320 <                    }
304 >        final CountDownLatch done = new CountDownLatch(1);
305 >        Runnable r = new CheckedRunnable() {
306 >            public void realRun() {
307 >                try {
308 >                    Thread current = Thread.currentThread();
309 >                    assertTrue(!current.isDaemon());
310 >                    assertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
311 >                    ThreadGroup g = current.getThreadGroup();
312 >                    SecurityManager s = System.getSecurityManager();
313 >                    if (s != null)
314 >                        assertTrue(g == s.getThreadGroup());
315 >                    else
316 >                        assertTrue(g == egroup);
317 >                    String name = current.getName();
318 >                    assertTrue(name.endsWith("thread-1"));
319 >                } catch (SecurityException ok) {
320 >                    // Also pass if not allowed to change setting
321                  }
322 <            };
322 >                done.countDown();
323 >            }};
324          ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
325  
326          e.execute(r);
327 +        await(done);
328 +
329          try {
330              e.shutdown();
331          } catch (SecurityException ok) {
332          }
333  
334 <        try {
333 <            Thread.sleep(SHORT_DELAY_MS);
334 <        } finally {
335 <            joinPool(e);
336 <        }
334 >        joinPool(e);
335      }
336  
337      /**
# Line 342 | Line 340 | public class ExecutorsTest extends JSR16
340       * access control context and context class loader
341       */
342      public void testPrivilegedThreadFactory() throws Exception {
343 <        Policy savedPolicy = null;
344 <        try {
345 <            savedPolicy = Policy.getPolicy();
346 <            AdjustablePolicy policy = new AdjustablePolicy();
347 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
348 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
349 <            Policy.setPolicy(policy);
350 <        } catch (AccessControlException ok) {
353 <            return;
354 <        }
355 <        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
356 <        final ClassLoader thisccl = Thread.currentThread().getContextClassLoader();
357 <        final AccessControlContext thisacc = AccessController.getContext();
358 <        Runnable r = new Runnable() {
359 <                public void run() {
360 <                    try {
343 >        final CountDownLatch done = new CountDownLatch(1);
344 >        Runnable r = new CheckedRunnable() {
345 >            public void realRun() throws Exception {
346 >                final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
347 >                final ClassLoader thisccl = Thread.currentThread().getContextClassLoader();
348 >                final AccessControlContext thisacc = AccessController.getContext();
349 >                Runnable r = new CheckedRunnable() {
350 >                    public void realRun() {
351                          Thread current = Thread.currentThread();
352 <                        threadAssertTrue(!current.isDaemon());
353 <                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
352 >                        assertTrue(!current.isDaemon());
353 >                        assertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
354                          ThreadGroup g = current.getThreadGroup();
355                          SecurityManager s = System.getSecurityManager();
356                          if (s != null)
357 <                            threadAssertTrue(g == s.getThreadGroup());
357 >                            assertTrue(g == s.getThreadGroup());
358                          else
359 <                            threadAssertTrue(g == egroup);
359 >                            assertTrue(g == egroup);
360                          String name = current.getName();
361 <                        threadAssertTrue(name.endsWith("thread-1"));
362 <                        threadAssertTrue(thisccl == current.getContextClassLoader());
363 <                        threadAssertTrue(thisacc.equals(AccessController.getContext()));
364 <                    } catch (SecurityException ok) {
365 <                        // Also pass if not allowed to change settings
366 <                    }
367 <                }
368 <            };
369 <        ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
361 >                        assertTrue(name.endsWith("thread-1"));
362 >                        assertSame(thisccl, current.getContextClassLoader());
363 >                        assertEquals(thisacc, AccessController.getContext());
364 >                        done.countDown();
365 >                    }};
366 >                ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
367 >                e.execute(r);
368 >                await(done);
369 >                e.shutdown();
370 >                joinPool(e);
371 >            }};
372 >
373 >        runWithPermissions(r,
374 >                           new RuntimePermission("getClassLoader"),
375 >                           new RuntimePermission("setContextClassLoader"),
376 >                           new RuntimePermission("modifyThread"));
377 >    }
378  
379 <        Policy.setPolicy(savedPolicy);
380 <        e.execute(r);
381 <        try {
382 <            e.shutdown();
383 <        } catch (SecurityException ok) {
384 <        }
385 <        try {
386 <            Thread.sleep(SHORT_DELAY_MS);
387 <        } finally {
390 <            joinPool(e);
379 >    boolean haveCCLPermissions() {
380 >        SecurityManager sm = System.getSecurityManager();
381 >        if (sm != null) {
382 >            try {
383 >                sm.checkPermission(new RuntimePermission("setContextClassLoader"));
384 >                sm.checkPermission(new RuntimePermission("getClassLoader"));
385 >            } catch (AccessControlException e) {
386 >                return false;
387 >            }
388          }
389 <
389 >        return true;
390      }
391  
392      void checkCCL() {
# Line 407 | Line 404 | public class ExecutorsTest extends JSR16
404          }
405      }
406  
410
407      /**
408       * Without class loader permissions, creating
409       * privilegedCallableUsingCurrentClassLoader throws ACE
410       */
411      public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() {
412 <        Policy savedPolicy = null;
413 <        try {
414 <            savedPolicy = Policy.getPolicy();
415 <            AdjustablePolicy policy = new AdjustablePolicy();
416 <            Policy.setPolicy(policy);
417 <        } catch (AccessControlException ok) {
418 <            return;
419 <        }
420 <
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 <        }
412 >        Runnable r = new CheckedRunnable() {
413 >            public void realRun() throws Exception {
414 >                if (System.getSecurityManager() == null)
415 >                    return;
416 >                try {
417 >                    Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
418 >                    shouldThrow();
419 >                } catch (AccessControlException success) {}
420 >            }};
421  
422 <        try {
435 <            Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
436 <            shouldThrow();
437 <        } catch (AccessControlException success) {
438 <        } finally {
439 <            Policy.setPolicy(savedPolicy);
440 <        }
422 >        runWithoutPermissions(r);
423      }
424  
425      /**
426       * With class loader permissions, calling
427       * privilegedCallableUsingCurrentClassLoader does not throw ACE
428       */
429 <    public void testprivilegedCallableUsingCCLWithPrivs() throws Exception {
430 <        Policy savedPolicy = null;
431 <        try {
432 <            savedPolicy = Policy.getPolicy();
433 <            AdjustablePolicy policy = new AdjustablePolicy();
434 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
435 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
436 <            Policy.setPolicy(policy);
437 <        } catch (AccessControlException ok) {
438 <            return;
439 <        }
458 <
459 <        try {
460 <            Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
461 <            task.call();
462 <        }
463 <        finally {
464 <            Policy.setPolicy(savedPolicy);
465 <        }
429 >    public void testPrivilegedCallableUsingCCLWithPrivs() throws Exception {
430 >        Runnable r = new CheckedRunnable() {
431 >            public void realRun() throws Exception {
432 >                Executors.privilegedCallableUsingCurrentClassLoader
433 >                    (new NoOpCallable())
434 >                    .call();
435 >            }};
436 >
437 >        runWithPermissions(r,
438 >                           new RuntimePermission("getClassLoader"),
439 >                           new RuntimePermission("setContextClassLoader"));
440      }
441  
442      /**
443       * Without permissions, calling privilegedCallable throws ACE
444       */
445 <    public void testprivilegedCallableWithNoPrivs() throws Exception {
446 <        Callable task;
447 <        Policy savedPolicy = null;
448 <        AdjustablePolicy policy = null;
449 <        AccessControlContext noprivAcc = null;
450 <        try {
451 <            savedPolicy = Policy.getPolicy();
452 <            policy = new AdjustablePolicy();
453 <            Policy.setPolicy(policy);
454 <            noprivAcc = AccessController.getContext();
455 <            task = Executors.privilegedCallable(new CheckCCL());
456 <            Policy.setPolicy(savedPolicy);
457 <        } catch (AccessControlException ok) {
458 <            return; // program has too few permissions to set up test
459 <        }
460 <
461 <        // Make sure that program doesn't have too many permissions
462 <        try {
463 <            AccessController.doPrivileged(new PrivilegedAction() {
464 <                    public Object run() {
465 <                        checkCCL();
466 <                        return null;
467 <                    }}, noprivAcc);
468 <            // too many permssions; skip test
469 <            return;
470 <        } catch (AccessControlException ok) {
471 <        }
472 <
473 <        try {
474 <            task.call();
475 <            shouldThrow();
476 <        } catch (AccessControlException success) {}
445 >    public void testPrivilegedCallableWithNoPrivs() throws Exception {
446 >        // Avoid classloader-related SecurityExceptions in swingui.TestRunner
447 >        Executors.privilegedCallable(new CheckCCL());
448 >
449 >        Runnable r = new CheckedRunnable() {
450 >            public void realRun() throws Exception {
451 >                if (System.getSecurityManager() == null)
452 >                    return;
453 >                Callable task = Executors.privilegedCallable(new CheckCCL());
454 >                try {
455 >                    task.call();
456 >                    shouldThrow();
457 >                } catch (AccessControlException success) {}
458 >            }};
459 >
460 >        runWithoutPermissions(r);
461 >
462 >        // It seems rather difficult to test that the
463 >        // AccessControlContext of the privilegedCallable is used
464 >        // instead of its caller.  Below is a failed attempt to do
465 >        // that, which does not work because the AccessController
466 >        // cannot capture the internal state of the current Policy.
467 >        // It would be much more work to differentiate based on,
468 >        // e.g. CodeSource.
469 >
470 > //         final AccessControlContext[] noprivAcc = new AccessControlContext[1];
471 > //         final Callable[] task = new Callable[1];
472 >
473 > //         runWithPermissions
474 > //             (new CheckedRunnable() {
475 > //                 public void realRun() {
476 > //                     if (System.getSecurityManager() == null)
477 > //                         return;
478 > //                     noprivAcc[0] = AccessController.getContext();
479 > //                     task[0] = Executors.privilegedCallable(new CheckCCL());
480 > //                     try {
481 > //                         AccessController.doPrivileged(new PrivilegedAction<Void>() {
482 > //                                                           public Void run() {
483 > //                                                               checkCCL();
484 > //                                                               return null;
485 > //                                                           }}, noprivAcc[0]);
486 > //                         shouldThrow();
487 > //                     } catch (AccessControlException success) {}
488 > //                 }});
489 >
490 > //         runWithPermissions
491 > //             (new CheckedRunnable() {
492 > //                 public void realRun() throws Exception {
493 > //                     if (System.getSecurityManager() == null)
494 > //                         return;
495 > //                     // Verify that we have an underprivileged ACC
496 > //                     try {
497 > //                         AccessController.doPrivileged(new PrivilegedAction<Void>() {
498 > //                                                           public Void run() {
499 > //                                                               checkCCL();
500 > //                                                               return null;
501 > //                                                           }}, noprivAcc[0]);
502 > //                         shouldThrow();
503 > //                     } catch (AccessControlException success) {}
504 >
505 > //                     try {
506 > //                         task[0].call();
507 > //                         shouldThrow();
508 > //                     } catch (AccessControlException success) {}
509 > //                 }},
510 > //              new RuntimePermission("getClassLoader"),
511 > //              new RuntimePermission("setContextClassLoader"));
512      }
513  
514      /**
515       * With permissions, calling privilegedCallable succeeds
516       */
517 <    public void testprivilegedCallableWithPrivs() throws Exception {
518 <        Policy savedPolicy = null;
519 <        try {
520 <            savedPolicy = Policy.getPolicy();
521 <            AdjustablePolicy policy = new AdjustablePolicy();
522 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
523 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
524 <            Policy.setPolicy(policy);
525 <        } 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 <        }
517 >    public void testPrivilegedCallableWithPrivs() throws Exception {
518 >        Runnable r = new CheckedRunnable() {
519 >            public void realRun() throws Exception {
520 >                Executors.privilegedCallable(new CheckCCL()).call();
521 >            }};
522 >
523 >        runWithPermissions(r,
524 >                           new RuntimePermission("getClassLoader"),
525 >                           new RuntimePermission("setContextClassLoader"));
526      }
527  
528      /**
# Line 538 | Line 538 | public class ExecutorsTest extends JSR16
538       */
539      public void testCallable2() throws Exception {
540          Callable c = Executors.callable(new NoOpRunnable(), one);
541 <        assertEquals(one, c.call());
541 >        assertSame(one, c.call());
542      }
543  
544      /**
# Line 547 | Line 547 | public class ExecutorsTest extends JSR16
547      public void testCallable3() throws Exception {
548          Callable c = Executors.callable(new PrivilegedAction() {
549                  public Object run() { return one; }});
550 <        assertEquals(one, c.call());
550 >        assertSame(one, c.call());
551      }
552  
553      /**
# Line 556 | Line 556 | public class ExecutorsTest extends JSR16
556      public void testCallable4() throws Exception {
557          Callable c = Executors.callable(new PrivilegedExceptionAction() {
558                  public Object run() { return one; }});
559 <        assertEquals(one, c.call());
559 >        assertSame(one, c.call());
560      }
561  
562
562      /**
563       * callable(null Runnable) throws NPE
564       */
# Line 600 | Line 599 | public class ExecutorsTest extends JSR16
599          } catch (NullPointerException success) {}
600      }
601  
603
602   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines