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.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.23 by jsr166, Thu Nov 19 03:55:29 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 TestCase{
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 <    private static long SHORT_DELAY_MS = 100;
25 <    private static long MEDIUM_DELAY_MS = 1000;
26 <    private static long LONG_DELAY_MS = 10000;
27 <
28 <    class SleepRun implements Runnable {
29 <        public void run() {
30 <            try{
31 <                Thread.sleep(MEDIUM_DELAY_MS);
32 <            } catch(InterruptedException e){
34 <                fail("unexpected exception");
35 <            }
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          }
37    }
38    
34  
35 <    class SleepCall implements Callable {
36 <        public Object call(){
37 <            try{
38 <                Thread.sleep(MEDIUM_DELAY_MS);
39 <            }catch(InterruptedException e){
40 <                fail("unexpected exception");
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              }
47            return Boolean.TRUE;
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 <     *  Test to verify execute(Executor, Runnable) will throw
55 <     *  RejectedExecutionException Attempting to execute a runnable on
56 <     *  a full ThreadPool will cause such an exception here, up to 5
57 <     *  runnables are attempted on a pool capable on handling one
58 <     *  until it throws an exception
65 >     * A newCachedThreadPool can execute runnables
66       */
67 <    public void testExecute1(){
68 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
69 <        try{
70 <            
71 <            for(int i = 0; i < 5; ++i){
72 <                Executors.execute(p, new SleepRun(), Boolean.TRUE);
66 <            }
67 <            fail("should throw");
68 <        } catch(RejectedExecutionException success){}
69 <        p.shutdownNow();
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 <     *  Test to verify execute(Executor, Callable) will throw
77 <     *  RejectedExecutionException Attempting to execute a callable on
78 <     *  a full ThreadPool will cause such an exception here, up to 5
79 <     *  runnables are attempted on a pool capable on handling one
80 <     *  until it throws an exception
81 <     */
82 <    public void testExecute2(){
83 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
81 <        try{
82 <            for(int i = 0; i < 5; ++i) {
83 <                Executors.execute(p, new SleepCall());
84 <            }
85 <            fail("should throw");
86 <        }catch(RejectedExecutionException e){}
87 <        p.shutdownNow();
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  
90
86      /**
87 <     *  Test to verify invoke(Executor, Runnable) throws InterruptedException
88 <     *  A single use of invoke starts that will wait long enough
89 <     *  for the invoking thread to be interrupted
90 <     */
91 <    public void testInvoke2(){
92 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
93 <        Thread t = new Thread(new Runnable() {
99 <                public void run(){
100 <                    try{
101 <                        Executors.invoke(p,new Runnable(){
102 <                                public void run(){
103 <                                    try{
104 <                                        Thread.sleep(MEDIUM_DELAY_MS);
105 <                                        fail("should throw");
106 <                                    }catch(InterruptedException e){
107 <                                    }
108 <                                }
109 <                            });
110 <                    } catch(InterruptedException success){
111 <                    } catch(Exception e) {
112 <                        fail("unexpected exception");
113 <                    }
114 <                    
115 <                }
116 <            });
117 <        try{
118 <            t.start();
119 <            Thread.sleep(SHORT_DELAY_MS);
120 <            t.interrupt();
121 <        }catch(Exception e){
122 <            fail("unexpected exception");
123 <        }
124 <        p.shutdownNow();
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 <     *  Test to verify invoke(Executor, Runnable) will throw
99 <     *  ExecutionException An ExecutionException occurs when the
100 <     *  underlying Runnable throws an exception, here the
101 <     *  DivideByZeroException will cause an ExecutionException
102 <     */
103 <    public void testInvoke3(){
104 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
105 <        try{
136 <            Runnable r = new Runnable(){
137 <                    public void run(){
138 <                        int i = 5/0;
139 <                    }
140 <                };
141 <            
142 <            for(int i =0; i < 5; i++){
143 <                Executors.invoke(p,r);
144 <            }
145 <            
146 <            fail("should throw");
147 <        } catch(ExecutionException success){
148 <        } catch(Exception e){
149 <            fail("should throw EE");
150 <        }
151 <        p.shutdownNow();
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  
154
155
108      /**
109 <     *  Test to verify invoke(Executor, Callable) throws
110 <     *  InterruptedException A single use of invoke starts that will
111 <     *  wait long enough for the invoking thread to be interrupted
112 <     */
113 <    public void testInvoke5(){
114 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
115 <        
116 <        final Callable c = new Callable(){
165 <                public Object call(){
166 <                    try{
167 <                        Executors.invoke(p, new SleepCall());
168 <                        fail("should throw");
169 <                    }catch(InterruptedException e){}
170 <                    catch(RejectedExecutionException e2){}
171 <                    catch(ExecutionException e3){}
172 <                    return Boolean.TRUE;
173 <                }
174 <            };
175 <
176 <
177 <        
178 <        Thread t = new Thread(new Runnable(){
179 <                public void run(){
180 <                    try{
181 <                        c.call();
182 <                    }catch(Exception e){}
183 <                }
184 <          });
185 <        try{
186 <            t.start();
187 <            Thread.sleep(SHORT_DELAY_MS);
188 <            t.interrupt();
189 <            t.join();
190 <        }catch(InterruptedException e){
191 <            fail("unexpected exception");
192 <        }
193 <        
194 <        p.shutdownNow();
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 <     *  Test to verify 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,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
126 <
205 <        try{
206 <            Callable c = new Callable(){
207 <                    public Object call(){
208 <                        int i = 5/0;
209 <                        return Boolean.TRUE;
210 <                    }
211 <                };
212 <            
213 <            for(int i =0; i < 5; i++){
214 <                Executors.invoke(p,c);
215 <            }
216 <            
217 <            fail("should throw");
218 <        }catch(RejectedExecutionException e){}
219 <        catch(InterruptedException e2){}
220 <        catch(ExecutionException e3){}
221 <        p.shutdownNow();
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  
229            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  
252            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();
269 <
270 <            assertSame("should return test string", TEST_STRING, result);
271 <        }
272 <        catch (ExecutionException ex) {
273 <            fail("Unexpected exception");
274 <        }
275 <        catch (InterruptedException ex) {
276 <            fail("Unexpected exception");
277 <        }
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 <
285 <            assertSame("should return test string", TEST_STRING, result);
286 <        }
287 <        catch (ExecutionException ex) {
288 <            fail("Unexpected exception" );
289 <        }
290 <        catch (InterruptedException ex) {
291 <            fail("Unexpected exception");
292 <        }
213 >            ExecutorService e = Executors.unconfigurableScheduledExecutorService(null);
214 >            shouldThrow();
215 >        } catch (NullPointerException success) {}
216      }
217  
295    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 >        try {
224 >            TrackedCallable callable = new TrackedCallable();
225 >            ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor();
226 >            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
227 >            assertFalse(callable.done);
228 >            Thread.sleep(MEDIUM_DELAY_MS);
229 >            assertTrue(callable.done);
230 >            assertEquals(Boolean.TRUE, f.get());
231 >            joinPool(p1);
232 >        } catch (RejectedExecutionException e) {}
233      }
234  
235 <    private static class StringTask implements Callable<String> {
236 <        public String call() { return TEST_STRING; }
235 >    /**
236 >     * a newScheduledThreadPool successfully runs delayed task
237 >     */
238 >    public void testnewScheduledThreadPool() throws Exception {
239 >        try {
240 >            TrackedCallable callable = new TrackedCallable();
241 >            ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2);
242 >            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
243 >            assertFalse(callable.done);
244 >            Thread.sleep(MEDIUM_DELAY_MS);
245 >            assertTrue(callable.done);
246 >            assertEquals(Boolean.TRUE, f.get());
247 >            joinPool(p1);
248 >        } catch (RejectedExecutionException e) {}
249      }
250  
251 <    static class DirectExecutor implements Executor {
252 <        public void execute(Runnable r) {
253 <            r.run();
254 <        }
251 >    /**
252 >     * an unconfigurable  newScheduledThreadPool successfully runs delayed task
253 >     */
254 >    public void testunconfigurableScheduledExecutorService() throws Exception {
255 >        try {
256 >            TrackedCallable callable = new TrackedCallable();
257 >            ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2));
258 >            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
259 >            assertFalse(callable.done);
260 >            Thread.sleep(MEDIUM_DELAY_MS);
261 >            assertTrue(callable.done);
262 >            assertEquals(Boolean.TRUE, f.get());
263 >            joinPool(p1);
264 >        } catch (RejectedExecutionException e) {}
265      }
266  
267      /**
268 <     * Check that timeouts from execute will time out if they compute
316 <     * too long.
268 >     *  timeouts from execute will time out if they compute too long.
269       */
270 <
319 <    public void testTimedCallable() {
270 >    public void testTimedCallable() throws Exception {
271          int N = 10000;
272          ExecutorService executor = Executors.newSingleThreadExecutor();
273          List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N);
274          try {
275              long startTime = System.currentTimeMillis();
276 <            
276 >
277              long i = 0;
278              while (tasks.size() < N) {
279                  tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1));
280                  i += 10;
281              }
282 <            
282 >
283              int iters = 0;
284              BigInteger sum = BigInteger.ZERO;
285              for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) {
# Line 340 | Line 291 | public class ExecutorsTest extends TestC
291                      assertTrue(iters > 0);
292                      return;
293                  }
343                catch (Exception e) {
344                    fail("unexpected exception: " + e);
345                }
294              }
295              // if by chance we didn't ever time out, total time must be small
296              long elapsed = System.currentTimeMillis() - startTime;
297              assertTrue(elapsed < N);
298          }
299          finally {
300 <            executor.shutdownNow();
300 >            joinPool(executor);
301          }
302      }
303  
304 <    
305 <    static class TimedCallable<T> implements Callable<T> {
306 <        private final Executor exec;
307 <        private final Callable<T> func;
308 <        private final long msecs;
309 <        
310 <        TimedCallable(Executor exec, Callable<T> func, long msecs) {
311 <            this.exec = exec;
312 <            this.func = func;
313 <            this.msecs = msecs;
304 >
305 >    /**
306 >     * ThreadPoolExecutor using defaultThreadFactory has
307 >     * specified group, priority, daemon status, and name
308 >     */
309 >    public void testDefaultThreadFactory() throws Exception {
310 >        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
311 >        Runnable r = new Runnable() {
312 >                public void run() {
313 >                    try {
314 >                        Thread current = Thread.currentThread();
315 >                        threadAssertTrue(!current.isDaemon());
316 >                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
317 >                        ThreadGroup g = current.getThreadGroup();
318 >                        SecurityManager s = System.getSecurityManager();
319 >                        if (s != null)
320 >                            threadAssertTrue(g == s.getThreadGroup());
321 >                        else
322 >                            threadAssertTrue(g == egroup);
323 >                        String name = current.getName();
324 >                        threadAssertTrue(name.endsWith("thread-1"));
325 >                    } catch (SecurityException ok) {
326 >                        // Also pass if not allowed to change setting
327 >                    }
328 >                }
329 >            };
330 >        ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
331 >
332 >        e.execute(r);
333 >        try {
334 >            e.shutdown();
335 >        } catch (SecurityException ok) {
336          }
337 <        
338 <        public T call() throws Exception {
339 <            Future<T> ftask = Executors.execute(exec, func);
340 <            try {
341 <                return ftask.get(msecs, TimeUnit.MILLISECONDS);
372 <            } finally {
373 <                ftask.cancel(true);
374 <            }
337 >
338 >        try {
339 >            Thread.sleep(SHORT_DELAY_MS);
340 >        } finally {
341 >            joinPool(e);
342          }
343      }
344  
345 +    /**
346 +     * ThreadPoolExecutor using privilegedThreadFactory has
347 +     * specified group, priority, daemon status, name,
348 +     * access control context and context class loader
349 +     */
350 +    public void testPrivilegedThreadFactory() throws Exception {
351 +        Policy savedPolicy = null;
352 +        try {
353 +            savedPolicy = Policy.getPolicy();
354 +            AdjustablePolicy policy = new AdjustablePolicy();
355 +            policy.addPermission(new RuntimePermission("getContextClassLoader"));
356 +            policy.addPermission(new RuntimePermission("setContextClassLoader"));
357 +            Policy.setPolicy(policy);
358 +        } catch (AccessControlException ok) {
359 +            return;
360 +        }
361 +        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
362 +        final ClassLoader thisccl = Thread.currentThread().getContextClassLoader();
363 +        final AccessControlContext thisacc = AccessController.getContext();
364 +        Runnable r = new Runnable() {
365 +                public void run() {
366 +                    try {
367 +                        Thread current = Thread.currentThread();
368 +                        threadAssertTrue(!current.isDaemon());
369 +                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
370 +                        ThreadGroup g = current.getThreadGroup();
371 +                        SecurityManager s = System.getSecurityManager();
372 +                        if (s != null)
373 +                            threadAssertTrue(g == s.getThreadGroup());
374 +                        else
375 +                            threadAssertTrue(g == egroup);
376 +                        String name = current.getName();
377 +                        threadAssertTrue(name.endsWith("thread-1"));
378 +                        threadAssertTrue(thisccl == current.getContextClassLoader());
379 +                        threadAssertTrue(thisacc.equals(AccessController.getContext()));
380 +                    } catch (SecurityException ok) {
381 +                        // Also pass if not allowed to change settings
382 +                    }
383 +                }
384 +            };
385 +        ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
386  
387 <    private static class Fib implements Callable<BigInteger> {
388 <        private final BigInteger n;
389 <        Fib(long n) {
390 <            if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n);
391 <            this.n = BigInteger.valueOf(n);
387 >        Policy.setPolicy(savedPolicy);
388 >        e.execute(r);
389 >        try {
390 >            e.shutdown();
391 >        } catch (SecurityException ok) {
392          }
393 <        public BigInteger call() {
394 <            BigInteger f1 = BigInteger.ONE;
395 <            BigInteger f2 = f1;
396 <            for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
389 <                BigInteger t = f1.add(f2);
390 <                f1 = f2;
391 <                f2 = t;
392 <            }
393 <            return f1;
393 >        try {
394 >            Thread.sleep(SHORT_DELAY_MS);
395 >        } finally {
396 >            joinPool(e);
397          }
395    };
398  
399 +    }
400 +
401 +    void checkCCL() {
402 +        SecurityManager sm = System.getSecurityManager();
403 +        if (sm != null) {
404 +            sm.checkPermission(new RuntimePermission("setContextClassLoader"));
405 +            sm.checkPermission(new RuntimePermission("getClassLoader"));
406 +        }
407 +    }
408 +
409 +    class CheckCCL implements Callable<Object> {
410 +        public Object call() {
411 +            checkCCL();
412 +            return null;
413 +        }
414 +    }
415 +
416 +
417 +    /**
418 +     * Without class loader permissions, creating
419 +     * privilegedCallableUsingCurrentClassLoader throws ACE
420 +     */
421 +    public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() {
422 +        Policy savedPolicy = null;
423 +        try {
424 +            savedPolicy = Policy.getPolicy();
425 +            AdjustablePolicy policy = new AdjustablePolicy();
426 +            Policy.setPolicy(policy);
427 +        } catch (AccessControlException ok) {
428 +            return;
429 +        }
430 +
431 +        // Check if program still has too many permissions to run test
432 +        try {
433 +            checkCCL();
434 +            // too many privileges to test; so return
435 +            Policy.setPolicy(savedPolicy);
436 +            return;
437 +        } catch (AccessControlException ok) {
438 +        }
439 +
440 +        try {
441 +            Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
442 +            shouldThrow();
443 +        } catch (AccessControlException success) {
444 +        } finally {
445 +            Policy.setPolicy(savedPolicy);
446 +        }
447 +    }
448 +
449 +    /**
450 +     * With class loader permissions, calling
451 +     * privilegedCallableUsingCurrentClassLoader does not throw ACE
452 +     */
453 +    public void testprivilegedCallableUsingCCLWithPrivs() throws Exception {
454 +        Policy savedPolicy = null;
455 +        try {
456 +            savedPolicy = Policy.getPolicy();
457 +            AdjustablePolicy policy = new AdjustablePolicy();
458 +            policy.addPermission(new RuntimePermission("getContextClassLoader"));
459 +            policy.addPermission(new RuntimePermission("setContextClassLoader"));
460 +            Policy.setPolicy(policy);
461 +        } catch (AccessControlException ok) {
462 +            return;
463 +        }
464 +
465 +        try {
466 +            Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
467 +            task.call();
468 +        }
469 +        finally {
470 +            Policy.setPolicy(savedPolicy);
471 +        }
472 +    }
473 +
474 +    /**
475 +     * Without permissions, calling privilegedCallable throws ACE
476 +     */
477 +    public void testprivilegedCallableWithNoPrivs() throws Exception {
478 +        Callable task;
479 +        Policy savedPolicy = null;
480 +        AdjustablePolicy policy = null;
481 +        AccessControlContext noprivAcc = null;
482 +        try {
483 +            savedPolicy = Policy.getPolicy();
484 +            policy = new AdjustablePolicy();
485 +            Policy.setPolicy(policy);
486 +            noprivAcc = AccessController.getContext();
487 +            task = Executors.privilegedCallable(new CheckCCL());
488 +            Policy.setPolicy(savedPolicy);
489 +        } catch (AccessControlException ok) {
490 +            return; // program has too few permissions to set up test
491 +        }
492 +
493 +        // Make sure that program doesn't have too many permissions
494 +        try {
495 +            AccessController.doPrivileged(new PrivilegedAction() {
496 +                    public Object run() {
497 +                        checkCCL();
498 +                        return null;
499 +                    }}, noprivAcc);
500 +            // too many permssions; skip test
501 +            return;
502 +        } catch (AccessControlException ok) {
503 +        }
504 +
505 +        try {
506 +            task.call();
507 +            shouldThrow();
508 +        } catch (AccessControlException success) {}
509 +    }
510 +
511 +    /**
512 +     * With permissions, calling privilegedCallable succeeds
513 +     */
514 +    public void testprivilegedCallableWithPrivs() throws Exception {
515 +        Policy savedPolicy = null;
516 +        try {
517 +            savedPolicy = Policy.getPolicy();
518 +            AdjustablePolicy policy = new AdjustablePolicy();
519 +            policy.addPermission(new RuntimePermission("getContextClassLoader"));
520 +            policy.addPermission(new RuntimePermission("setContextClassLoader"));
521 +            Policy.setPolicy(policy);
522 +        } catch (AccessControlException ok) {
523 +            return;
524 +        }
525 +
526 +        Callable task = Executors.privilegedCallable(new CheckCCL());
527 +        try {
528 +            task.call();
529 +        } finally {
530 +            Policy.setPolicy(savedPolicy);
531 +        }
532 +    }
533 +
534 +    /**
535 +     * callable(Runnable) returns null when called
536 +     */
537 +    public void testCallable1() throws Exception {
538 +        Callable c = Executors.callable(new NoOpRunnable());
539 +        assertNull(c.call());
540 +    }
541 +
542 +    /**
543 +     * callable(Runnable, result) returns result when called
544 +     */
545 +    public void testCallable2() throws Exception {
546 +        Callable c = Executors.callable(new NoOpRunnable(), one);
547 +        assertEquals(one, c.call());
548 +    }
549 +
550 +    /**
551 +     * callable(PrivilegedAction) returns its result when called
552 +     */
553 +    public void testCallable3() throws Exception {
554 +        Callable c = Executors.callable(new PrivilegedAction() {
555 +                public Object run() { return one; }});
556 +        assertEquals(one, c.call());
557 +    }
558 +
559 +    /**
560 +     * callable(PrivilegedExceptionAction) returns its result when called
561 +     */
562 +    public void testCallable4() throws Exception {
563 +        Callable c = Executors.callable(new PrivilegedExceptionAction() {
564 +                public Object run() { return one; }});
565 +        assertEquals(one, c.call());
566 +    }
567 +
568 +
569 +    /**
570 +     * callable(null Runnable) throws NPE
571 +     */
572 +    public void testCallableNPE1() {
573 +        try {
574 +            Callable c = Executors.callable((Runnable) null);
575 +            shouldThrow();
576 +        } catch (NullPointerException success) {}
577 +    }
578 +
579 +    /**
580 +     * callable(null, result) throws NPE
581 +     */
582 +    public void testCallableNPE2() {
583 +        try {
584 +            Callable c = Executors.callable((Runnable) null, one);
585 +            shouldThrow();
586 +        } catch (NullPointerException success) {}
587 +    }
588 +
589 +    /**
590 +     * callable(null PrivilegedAction) throws NPE
591 +     */
592 +    public void testCallableNPE3() {
593 +        try {
594 +            Callable c = Executors.callable((PrivilegedAction) null);
595 +            shouldThrow();
596 +        } catch (NullPointerException success) {}
597 +    }
598 +
599 +    /**
600 +     * callable(null PrivilegedExceptionAction) throws NPE
601 +     */
602 +    public void testCallableNPE4() {
603 +        try {
604 +            Callable c = Executors.callable((PrivilegedExceptionAction) null);
605 +            shouldThrow();
606 +        } catch (NullPointerException success) {}
607 +    }
608  
609  
610   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines