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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.24 by jsr166, Fri Nov 20 22:58:48 2009 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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.
7   */
8  
9  
# Line 10 | Line 11 | import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13   import java.math.BigInteger;
14 + import java.security.*;
15  
16 < public class ExecutorsTest extends JSR166TestCase{
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      }
19    
20
20      public static Test suite() {
21 <        return new TestSuite(ExecutorsTest.class);
21 >        return new TestSuite(ExecutorsTest.class);
22      }
23  
24 <    /**
25 <     *   execute(Executor, Runnable) will throw
26 <     *  RejectedExecutionException Attempting to execute a runnable on
27 <     *  a full ThreadPool will cause such an exception here, up to 5
29 <     *  runnables are attempted on a pool capable on handling one
30 <     *  until it throws an exception
31 <     */
32 <    public void testExecute1(){
33 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
34 <        try{
35 <            
36 <            for(int i = 0; i < 5; ++i){
37 <                Executors.execute(p, new MediumRunnable(), Boolean.TRUE);
38 <            }
39 <            fail("should throw");
40 <        } catch(RejectedExecutionException success){}
41 <        joinPool(p);
42 <    }
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 <    /**
30 <     *   execute(Executor, Callable) will throw
31 <     *  RejectedExecutionException Attempting to execute a callable on
32 <     *  a full ThreadPool will cause such an exception here, up to 5
33 <     *  runnables are attempted on a pool capable on handling one
34 <     *  until it throws an exception
35 <     */
36 <    public void testExecute2(){
37 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
38 <        try{
39 <            for(int i = 0; i < 5; ++i) {
40 <                Executors.execute(p, new SmallCallable());
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 <            fail("should throw");
58 <        }catch(RejectedExecutionException e){}
59 <        joinPool(p);
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 +
64      /**
65 <     *   invoke(Executor, Runnable) throws InterruptedException
66 <     *  A single use of invoke starts that will wait long enough
67 <     *  for the invoking thread to be interrupted
68 <     */
69 <    public void testInvoke2(){
70 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
71 <        Thread t = new Thread(new Runnable() {
72 <                public void run(){
72 <                    try{
73 <                        Executors.invoke(p,new Runnable(){
74 <                                public void run(){
75 <                                    try{
76 <                                        Thread.sleep(MEDIUM_DELAY_MS);
77 <                                        fail("should throw");
78 <                                    }catch(InterruptedException e){
79 <                                    }
80 <                                }
81 <                            });
82 <                    } catch(InterruptedException success){
83 <                    } catch(Exception e) {
84 <                        fail("unexpected exception");
85 <                    }
86 <                    
87 <                }
88 <            });
89 <        try{
90 <            t.start();
91 <            Thread.sleep(SHORT_DELAY_MS);
92 <            t.interrupt();
93 <        }catch(Exception e){
94 <            fail("unexpected exception");
95 <        }
96 <        joinPool(p);
65 >     * A newCachedThreadPool can execute runnables
66 >     */
67 >    public void testNewCachedThreadPool1() {
68 >        ExecutorService e = Executors.newCachedThreadPool();
69 >        e.execute(new NoOpRunnable());
70 >        e.execute(new NoOpRunnable());
71 >        e.execute(new NoOpRunnable());
72 >        joinPool(e);
73      }
74  
75      /**
76 <     *   invoke(Executor, Runnable) will throw
77 <     *  ExecutionException An ExecutionException occurs when the
78 <     *  underlying Runnable throws an exception, here the
79 <     *  DivideByZeroException will cause an ExecutionException
80 <     */
81 <    public void testInvoke3(){
82 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
83 <        try{
108 <            Runnable r = new Runnable(){
109 <                    public void run(){
110 <                        int i = 5/0;
111 <                    }
112 <                };
113 <            
114 <            for(int i =0; i < 5; i++){
115 <                Executors.invoke(p,r);
116 <            }
117 <            
118 <            fail("should throw");
119 <        } catch(ExecutionException success){
120 <        } catch(Exception e){
121 <            fail("should throw EE");
122 <        }
123 <        joinPool(p);
76 >     * A newCachedThreadPool with given ThreadFactory can execute runnables
77 >     */
78 >    public void testNewCachedThreadPool2() {
79 >        ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory());
80 >        e.execute(new NoOpRunnable());
81 >        e.execute(new NoOpRunnable());
82 >        e.execute(new NoOpRunnable());
83 >        joinPool(e);
84      }
85  
86 +    /**
87 +     * A newCachedThreadPool with null ThreadFactory throws NPE
88 +     */
89 +    public void testNewCachedThreadPool3() {
90 +        try {
91 +            ExecutorService e = Executors.newCachedThreadPool(null);
92 +            shouldThrow();
93 +        } catch (NullPointerException success) {}
94 +    }
95  
96  
97      /**
98 <     *   invoke(Executor, Callable) throws
99 <     *  InterruptedException A single use of invoke starts that will
100 <     *  wait long enough for the invoking thread to be interrupted
101 <     */
102 <    public void testInvoke5(){
103 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
104 <        
105 <        final Callable c = new Callable(){
106 <                public Object call(){
138 <                    try{
139 <                        Executors.invoke(p, new SmallCallable());
140 <                        fail("should throw");
141 <                    }catch(InterruptedException e){}
142 <                    catch(RejectedExecutionException e2){}
143 <                    catch(ExecutionException e3){}
144 <                    return Boolean.TRUE;
145 <                }
146 <            };
147 <
98 >     * A new SingleThreadExecutor can execute runnables
99 >     */
100 >    public void testNewSingleThreadExecutor1() {
101 >        ExecutorService e = Executors.newSingleThreadExecutor();
102 >        e.execute(new NoOpRunnable());
103 >        e.execute(new NoOpRunnable());
104 >        e.execute(new NoOpRunnable());
105 >        joinPool(e);
106 >    }
107  
108 <        
109 <        Thread t = new Thread(new Runnable(){
110 <                public void run(){
111 <                    try{
112 <                        c.call();
113 <                    }catch(Exception e){}
114 <                }
115 <          });
116 <        try{
158 <            t.start();
159 <            Thread.sleep(SHORT_DELAY_MS);
160 <            t.interrupt();
161 <            t.join();
162 <        }catch(InterruptedException e){
163 <            fail("unexpected exception");
164 <        }
165 <        
166 <        joinPool(p);
108 >    /**
109 >     * A new SingleThreadExecutor with given ThreadFactory can execute runnables
110 >     */
111 >    public void testNewSingleThreadExecutor2() {
112 >        ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory());
113 >        e.execute(new NoOpRunnable());
114 >        e.execute(new NoOpRunnable());
115 >        e.execute(new NoOpRunnable());
116 >        joinPool(e);
117      }
118  
119      /**
120 <     *   invoke(Executor, Callable) will throw ExecutionException
121 <     *  An ExecutionException occurs when the underlying Runnable throws
122 <     *  an exception, here the DivideByZeroException will cause an ExecutionException
123 <     */
124 <    public void testInvoke6(){
125 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
126 <
177 <        try{
178 <            Callable c = new Callable(){
179 <                    public Object call(){
180 <                        int i = 5/0;
181 <                        return Boolean.TRUE;
182 <                    }
183 <                };
184 <            
185 <            for(int i =0; i < 5; i++){
186 <                Executors.invoke(p,c);
187 <            }
188 <            
189 <            fail("should throw");
190 <        }catch(RejectedExecutionException e){}
191 <        catch(InterruptedException e2){}
192 <        catch(ExecutionException e3){}
193 <        joinPool(p);
120 >     * A new SingleThreadExecutor with null ThreadFactory throws NPE
121 >     */
122 >    public void testNewSingleThreadExecutor3() {
123 >        try {
124 >            ExecutorService e = Executors.newSingleThreadExecutor(null);
125 >            shouldThrow();
126 >        } catch (NullPointerException success) {}
127      }
128  
129 <    public void testExecuteRunnable () {
129 >    /**
130 >     * A new SingleThreadExecutor cannot be casted to concrete implementation
131 >     */
132 >    public void testCastNewSingleThreadExecutor() {
133 >        ExecutorService e = Executors.newSingleThreadExecutor();
134          try {
135 <            Executor e = new DirectExecutor();
136 <            Task task = new Task();
135 >            ThreadPoolExecutor tpe = (ThreadPoolExecutor)e;
136 >            shouldThrow();
137 >        } catch (ClassCastException success) {
138 >        } finally {
139 >            joinPool(e);
140 >        }
141 >    }
142  
201            assertFalse("task should not be complete", task.isCompleted());
143  
144 <            Future<String> future = Executors.execute(e, task, TEST_STRING);
145 <            String result = future.get();
144 >    /**
145 >     * A new newFixedThreadPool can execute runnables
146 >     */
147 >    public void testNewFixedThreadPool1() {
148 >        ExecutorService e = Executors.newFixedThreadPool(2);
149 >        e.execute(new NoOpRunnable());
150 >        e.execute(new NoOpRunnable());
151 >        e.execute(new NoOpRunnable());
152 >        joinPool(e);
153 >    }
154  
155 <            assertTrue("task should be complete", task.isCompleted());
156 <            assertSame("should return test string", TEST_STRING, result);
157 <        }
158 <        catch (ExecutionException ex) {
159 <            fail("Unexpected exception");
160 <        }
161 <        catch (InterruptedException ex) {
162 <            fail("Unexpected exception");
163 <        }
155 >    /**
156 >     * A new newFixedThreadPool with given ThreadFactory can execute runnables
157 >     */
158 >    public void testNewFixedThreadPool2() {
159 >        ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory());
160 >        e.execute(new NoOpRunnable());
161 >        e.execute(new NoOpRunnable());
162 >        e.execute(new NoOpRunnable());
163 >        joinPool(e);
164      }
165  
166 <    public void testInvokeRunnable () {
166 >    /**
167 >     * A new newFixedThreadPool with null ThreadFactory throws NPE
168 >     */
169 >    public void testNewFixedThreadPool3() {
170          try {
171 <            Executor e = new DirectExecutor();
172 <            Task task = new Task();
171 >            ExecutorService e = Executors.newFixedThreadPool(2, null);
172 >            shouldThrow();
173 >        } catch (NullPointerException success) {}
174 >    }
175  
176 <            assertFalse("task should not be complete", task.isCompleted());
176 >    /**
177 >     * A new newFixedThreadPool with 0 threads throws IAE
178 >     */
179 >    public void testNewFixedThreadPool4() {
180 >        try {
181 >            ExecutorService e = Executors.newFixedThreadPool(0);
182 >            shouldThrow();
183 >        } catch (IllegalArgumentException success) {}
184 >    }
185  
224            Executors.invoke(e, task);
186  
187 <            assertTrue("task should be complete", task.isCompleted());
188 <        }
189 <        catch (ExecutionException ex) {
190 <            fail("Unexpected exception");
191 <        }
192 <        catch (InterruptedException ex) {
193 <            fail("Unexpected exception");
194 <        }
187 >    /**
188 >     * An unconfigurable newFixedThreadPool can execute runnables
189 >     */
190 >    public void testunconfigurableExecutorService() {
191 >        ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2));
192 >        e.execute(new NoOpRunnable());
193 >        e.execute(new NoOpRunnable());
194 >        e.execute(new NoOpRunnable());
195 >        joinPool(e);
196      }
197  
198 <    public void testExecuteCallable () {
198 >    /**
199 >     * unconfigurableExecutorService(null) throws NPE
200 >     */
201 >    public void testunconfigurableExecutorServiceNPE() {
202          try {
203 <            Executor e = new DirectExecutor();
204 <            Future<String> future = Executors.execute(e, new StringTask());
205 <            String result = future.get();
241 <
242 <            assertSame("should return test string", TEST_STRING, result);
243 <        }
244 <        catch (ExecutionException ex) {
245 <            fail("Unexpected exception");
246 <        }
247 <        catch (InterruptedException ex) {
248 <            fail("Unexpected exception");
249 <        }
203 >            ExecutorService e = Executors.unconfigurableExecutorService(null);
204 >            shouldThrow();
205 >        } catch (NullPointerException success) {}
206      }
207  
208 <    public void testInvokeCallable () {
208 >    /**
209 >     * unconfigurableScheduledExecutorService(null) throws NPE
210 >     */
211 >    public void testunconfigurableScheduledExecutorServiceNPE() {
212          try {
213 <            Executor e = new DirectExecutor();
214 <            String result = Executors.invoke(e, new StringTask());
215 <
257 <            assertSame("should return test string", TEST_STRING, result);
258 <        }
259 <        catch (ExecutionException ex) {
260 <            fail("Unexpected exception" );
261 <        }
262 <        catch (InterruptedException ex) {
263 <            fail("Unexpected exception");
264 <        }
213 >            ExecutorService e = Executors.unconfigurableScheduledExecutorService(null);
214 >            shouldThrow();
215 >        } catch (NullPointerException success) {}
216      }
217  
267    private static final String TEST_STRING = "a test string";
218  
219 <    private static class Task implements Runnable {
220 <        public void run() { completed = true; }
221 <        public boolean isCompleted() { return completed; }
222 <        public void reset() { completed = false; }
223 <        private boolean completed = false;
219 >    /**
220 >     * a newSingleThreadScheduledExecutor successfully runs delayed task
221 >     */
222 >    public void testNewSingleThreadScheduledExecutor() throws Exception {
223 >        TrackedCallable callable = new TrackedCallable();
224 >        ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor();
225 >        Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
226 >        assertFalse(callable.done);
227 >        Thread.sleep(MEDIUM_DELAY_MS);
228 >        assertTrue(callable.done);
229 >        assertEquals(Boolean.TRUE, f.get());
230 >        joinPool(p1);
231      }
232  
233 <    private static class StringTask implements Callable<String> {
234 <        public String call() { return TEST_STRING; }
233 >    /**
234 >     * a newScheduledThreadPool successfully runs delayed task
235 >     */
236 >    public void testnewScheduledThreadPool() throws Exception {
237 >        TrackedCallable callable = new TrackedCallable();
238 >        ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2);
239 >        Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
240 >        assertFalse(callable.done);
241 >        Thread.sleep(MEDIUM_DELAY_MS);
242 >        assertTrue(callable.done);
243 >        assertEquals(Boolean.TRUE, f.get());
244 >        joinPool(p1);
245      }
246  
247 <    static class DirectExecutor implements Executor {
248 <        public void execute(Runnable r) {
249 <            r.run();
250 <        }
247 >    /**
248 >     * an unconfigurable newScheduledThreadPool successfully runs delayed task
249 >     */
250 >    public void testunconfigurableScheduledExecutorService() throws Exception {
251 >        TrackedCallable callable = new TrackedCallable();
252 >        ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2));
253 >        Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
254 >        assertFalse(callable.done);
255 >        Thread.sleep(MEDIUM_DELAY_MS);
256 >        assertTrue(callable.done);
257 >        assertEquals(Boolean.TRUE, f.get());
258 >        joinPool(p1);
259      }
260  
261      /**
262 <     * Check that timeouts from execute will time out if they compute
288 <     * too long.
262 >     *  timeouts from execute will time out if they compute too long.
263       */
264 <
291 <    public void testTimedCallable() {
264 >    public void testTimedCallable() throws Exception {
265          int N = 10000;
266          ExecutorService executor = Executors.newSingleThreadExecutor();
267          List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N);
268          try {
269              long startTime = System.currentTimeMillis();
270 <            
270 >
271              long i = 0;
272              while (tasks.size() < N) {
273                  tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1));
274                  i += 10;
275              }
276 <            
276 >
277              int iters = 0;
278              BigInteger sum = BigInteger.ZERO;
279              for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) {
# Line 312 | Line 285 | public class ExecutorsTest extends JSR16
285                      assertTrue(iters > 0);
286                      return;
287                  }
315                catch (Exception e) {
316                    fail("unexpected exception: " + e);
317                }
288              }
289              // if by chance we didn't ever time out, total time must be small
290              long elapsed = System.currentTimeMillis() - startTime;
# Line 325 | Line 295 | public class ExecutorsTest extends JSR16
295          }
296      }
297  
298 <    
299 <    static class TimedCallable<T> implements Callable<T> {
300 <        private final Executor exec;
301 <        private final Callable<T> func;
302 <        private final long msecs;
303 <        
304 <        TimedCallable(Executor exec, Callable<T> func, long msecs) {
305 <            this.exec = exec;
306 <            this.func = func;
307 <            this.msecs = msecs;
298 >
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 >                    }
322 >                }
323 >            };
324 >        ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
325 >
326 >        e.execute(r);
327 >        try {
328 >            e.shutdown();
329 >        } catch (SecurityException ok) {
330          }
331 <        
332 <        public T call() throws Exception {
333 <            Future<T> ftask = Executors.execute(exec, func);
334 <            try {
335 <                return ftask.get(msecs, TimeUnit.MILLISECONDS);
344 <            } finally {
345 <                ftask.cancel(true);
346 <            }
331 >
332 >        try {
333 >            Thread.sleep(SHORT_DELAY_MS);
334 >        } finally {
335 >            joinPool(e);
336          }
337      }
338  
339 +    /**
340 +     * ThreadPoolExecutor using privilegedThreadFactory has
341 +     * specified group, priority, daemon status, name,
342 +     * access control context and context class loader
343 +     */
344 +    public void testPrivilegedThreadFactory() throws Exception {
345 +        Policy savedPolicy = null;
346 +        try {
347 +            savedPolicy = Policy.getPolicy();
348 +            AdjustablePolicy policy = new AdjustablePolicy();
349 +            policy.addPermission(new RuntimePermission("getContextClassLoader"));
350 +            policy.addPermission(new RuntimePermission("setContextClassLoader"));
351 +            Policy.setPolicy(policy);
352 +        } 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 {
361 +                        Thread current = Thread.currentThread();
362 +                        threadAssertTrue(!current.isDaemon());
363 +                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
364 +                        ThreadGroup g = current.getThreadGroup();
365 +                        SecurityManager s = System.getSecurityManager();
366 +                        if (s != null)
367 +                            threadAssertTrue(g == s.getThreadGroup());
368 +                        else
369 +                            threadAssertTrue(g == egroup);
370 +                        String name = current.getName();
371 +                        threadAssertTrue(name.endsWith("thread-1"));
372 +                        threadAssertTrue(thisccl == current.getContextClassLoader());
373 +                        threadAssertTrue(thisacc.equals(AccessController.getContext()));
374 +                    } catch (SecurityException ok) {
375 +                        // Also pass if not allowed to change settings
376 +                    }
377 +                }
378 +            };
379 +        ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
380  
381 <    private static class Fib implements Callable<BigInteger> {
382 <        private final BigInteger n;
383 <        Fib(long n) {
384 <            if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n);
385 <            this.n = BigInteger.valueOf(n);
381 >        Policy.setPolicy(savedPolicy);
382 >        e.execute(r);
383 >        try {
384 >            e.shutdown();
385 >        } catch (SecurityException ok) {
386          }
387 <        public BigInteger call() {
388 <            BigInteger f1 = BigInteger.ONE;
389 <            BigInteger f2 = f1;
390 <            for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
391 <                BigInteger t = f1.add(f2);
392 <                f1 = f2;
393 <                f2 = t;
394 <            }
395 <            return f1;
387 >        try {
388 >            Thread.sleep(SHORT_DELAY_MS);
389 >        } finally {
390 >            joinPool(e);
391 >        }
392 >
393 >    }
394 >
395 >    void checkCCL() {
396 >        SecurityManager sm = System.getSecurityManager();
397 >        if (sm != null) {
398 >            sm.checkPermission(new RuntimePermission("setContextClassLoader"));
399 >            sm.checkPermission(new RuntimePermission("getClassLoader"));
400 >        }
401 >    }
402 >
403 >    class CheckCCL implements Callable<Object> {
404 >        public Object call() {
405 >            checkCCL();
406 >            return null;
407 >        }
408 >    }
409 >
410 >
411 >    /**
412 >     * Without class loader permissions, creating
413 >     * privilegedCallableUsingCurrentClassLoader throws ACE
414 >     */
415 >    public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() {
416 >        Policy savedPolicy = null;
417 >        try {
418 >            savedPolicy = Policy.getPolicy();
419 >            AdjustablePolicy policy = new AdjustablePolicy();
420 >            Policy.setPolicy(policy);
421 >        } catch (AccessControlException ok) {
422 >            return;
423          }
367    };
424  
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 +        }
433 +
434 +        try {
435 +            Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
436 +            shouldThrow();
437 +        } catch (AccessControlException success) {
438 +        } finally {
439 +            Policy.setPolicy(savedPolicy);
440 +        }
441 +    }
442 +
443 +    /**
444 +     * With class loader permissions, calling
445 +     * privilegedCallableUsingCurrentClassLoader does not throw ACE
446 +     */
447 +    public void testprivilegedCallableUsingCCLWithPrivs() throws Exception {
448 +        Policy savedPolicy = null;
449 +        try {
450 +            savedPolicy = Policy.getPolicy();
451 +            AdjustablePolicy policy = new AdjustablePolicy();
452 +            policy.addPermission(new RuntimePermission("getContextClassLoader"));
453 +            policy.addPermission(new RuntimePermission("setContextClassLoader"));
454 +            Policy.setPolicy(policy);
455 +        } catch (AccessControlException ok) {
456 +            return;
457 +        }
458 +
459 +        try {
460 +            Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
461 +            task.call();
462 +        }
463 +        finally {
464 +            Policy.setPolicy(savedPolicy);
465 +        }
466 +    }
467 +
468 +    /**
469 +     * Without permissions, calling privilegedCallable throws ACE
470 +     */
471 +    public void testprivilegedCallableWithNoPrivs() throws Exception {
472 +        Callable task;
473 +        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 +        }
498 +
499 +        try {
500 +            task.call();
501 +            shouldThrow();
502 +        } catch (AccessControlException success) {}
503 +    }
504 +
505 +    /**
506 +     * With permissions, calling privilegedCallable succeeds
507 +     */
508 +    public void testprivilegedCallableWithPrivs() throws Exception {
509 +        Policy savedPolicy = null;
510 +        try {
511 +            savedPolicy = Policy.getPolicy();
512 +            AdjustablePolicy policy = new AdjustablePolicy();
513 +            policy.addPermission(new RuntimePermission("getContextClassLoader"));
514 +            policy.addPermission(new RuntimePermission("setContextClassLoader"));
515 +            Policy.setPolicy(policy);
516 +        } 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 +        }
526 +    }
527 +
528 +    /**
529 +     * callable(Runnable) returns null when called
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() throws Exception {
540 +        Callable c = Executors.callable(new NoOpRunnable(), one);
541 +        assertEquals(one, c.call());
542 +    }
543 +
544 +    /**
545 +     * callable(PrivilegedAction) returns its result when called
546 +     */
547 +    public void testCallable3() throws Exception {
548 +        Callable c = Executors.callable(new PrivilegedAction() {
549 +                public Object run() { return one; }});
550 +        assertEquals(one, c.call());
551 +    }
552 +
553 +    /**
554 +     * callable(PrivilegedExceptionAction) returns its result when called
555 +     */
556 +    public void testCallable4() throws Exception {
557 +        Callable c = Executors.callable(new PrivilegedExceptionAction() {
558 +                public Object run() { return one; }});
559 +        assertEquals(one, c.call());
560 +    }
561 +
562 +
563 +    /**
564 +     * callable(null Runnable) throws NPE
565 +     */
566 +    public void testCallableNPE1() {
567 +        try {
568 +            Callable c = Executors.callable((Runnable) null);
569 +            shouldThrow();
570 +        } catch (NullPointerException success) {}
571 +    }
572 +
573 +    /**
574 +     * callable(null, result) throws NPE
575 +     */
576 +    public void testCallableNPE2() {
577 +        try {
578 +            Callable c = Executors.callable((Runnable) null, one);
579 +            shouldThrow();
580 +        } catch (NullPointerException success) {}
581 +    }
582 +
583 +    /**
584 +     * callable(null PrivilegedAction) throws NPE
585 +     */
586 +    public void testCallableNPE3() {
587 +        try {
588 +            Callable c = Executors.callable((PrivilegedAction) null);
589 +            shouldThrow();
590 +        } catch (NullPointerException success) {}
591 +    }
592 +
593 +    /**
594 +     * callable(null PrivilegedExceptionAction) throws NPE
595 +     */
596 +    public void testCallableNPE4() {
597 +        try {
598 +            Callable c = Executors.callable((PrivilegedExceptionAction) null);
599 +            shouldThrow();
600 +        } catch (NullPointerException success) {}
601 +    }
602  
603  
604   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines