1 |
dl |
1.1 |
/* |
2 |
dl |
1.12 |
* 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 |
jsr166 |
1.37 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.20 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.26 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
jsr166 |
1.43 |
|
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 |
dl |
1.1 |
|
29 |
jsr166 |
1.22 |
public class ExecutorsTest extends JSR166TestCase { |
30 |
dl |
1.1 |
public static void main(String[] args) { |
31 |
jsr166 |
1.30 |
junit.textui.TestRunner.run(suite()); |
32 |
dl |
1.1 |
} |
33 |
|
|
public static Test suite() { |
34 |
tim |
1.9 |
return new TestSuite(ExecutorsTest.class); |
35 |
dl |
1.1 |
} |
36 |
|
|
|
37 |
dl |
1.5 |
/** |
38 |
|
|
* A newCachedThreadPool can execute runnables |
39 |
|
|
*/ |
40 |
|
|
public void testNewCachedThreadPool1() { |
41 |
|
|
ExecutorService e = Executors.newCachedThreadPool(); |
42 |
|
|
e.execute(new NoOpRunnable()); |
43 |
|
|
e.execute(new NoOpRunnable()); |
44 |
|
|
e.execute(new NoOpRunnable()); |
45 |
dl |
1.14 |
joinPool(e); |
46 |
dl |
1.5 |
} |
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* A newCachedThreadPool with given ThreadFactory can execute runnables |
50 |
|
|
*/ |
51 |
|
|
public void testNewCachedThreadPool2() { |
52 |
dl |
1.6 |
ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory()); |
53 |
dl |
1.5 |
e.execute(new NoOpRunnable()); |
54 |
|
|
e.execute(new NoOpRunnable()); |
55 |
|
|
e.execute(new NoOpRunnable()); |
56 |
dl |
1.14 |
joinPool(e); |
57 |
dl |
1.5 |
} |
58 |
|
|
|
59 |
|
|
/** |
60 |
|
|
* A newCachedThreadPool with null ThreadFactory throws NPE |
61 |
|
|
*/ |
62 |
|
|
public void testNewCachedThreadPool3() { |
63 |
|
|
try { |
64 |
|
|
ExecutorService e = Executors.newCachedThreadPool(null); |
65 |
|
|
shouldThrow(); |
66 |
jsr166 |
1.23 |
} catch (NullPointerException success) {} |
67 |
dl |
1.5 |
} |
68 |
|
|
|
69 |
|
|
/** |
70 |
|
|
* A new SingleThreadExecutor can execute runnables |
71 |
|
|
*/ |
72 |
|
|
public void testNewSingleThreadExecutor1() { |
73 |
|
|
ExecutorService e = Executors.newSingleThreadExecutor(); |
74 |
|
|
e.execute(new NoOpRunnable()); |
75 |
|
|
e.execute(new NoOpRunnable()); |
76 |
|
|
e.execute(new NoOpRunnable()); |
77 |
dl |
1.14 |
joinPool(e); |
78 |
dl |
1.5 |
} |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* A new SingleThreadExecutor with given ThreadFactory can execute runnables |
82 |
|
|
*/ |
83 |
|
|
public void testNewSingleThreadExecutor2() { |
84 |
dl |
1.6 |
ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory()); |
85 |
dl |
1.5 |
e.execute(new NoOpRunnable()); |
86 |
|
|
e.execute(new NoOpRunnable()); |
87 |
|
|
e.execute(new NoOpRunnable()); |
88 |
dl |
1.14 |
joinPool(e); |
89 |
dl |
1.5 |
} |
90 |
|
|
|
91 |
|
|
/** |
92 |
|
|
* A new SingleThreadExecutor with null ThreadFactory throws NPE |
93 |
|
|
*/ |
94 |
|
|
public void testNewSingleThreadExecutor3() { |
95 |
|
|
try { |
96 |
|
|
ExecutorService e = Executors.newSingleThreadExecutor(null); |
97 |
|
|
shouldThrow(); |
98 |
jsr166 |
1.23 |
} catch (NullPointerException success) {} |
99 |
dl |
1.5 |
} |
100 |
|
|
|
101 |
|
|
/** |
102 |
dl |
1.11 |
* A new SingleThreadExecutor cannot be casted to concrete implementation |
103 |
|
|
*/ |
104 |
|
|
public void testCastNewSingleThreadExecutor() { |
105 |
|
|
ExecutorService e = Executors.newSingleThreadExecutor(); |
106 |
|
|
try { |
107 |
|
|
ThreadPoolExecutor tpe = (ThreadPoolExecutor)e; |
108 |
jsr166 |
1.23 |
shouldThrow(); |
109 |
dl |
1.11 |
} catch (ClassCastException success) { |
110 |
|
|
} finally { |
111 |
|
|
joinPool(e); |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
/** |
116 |
dl |
1.5 |
* A new newFixedThreadPool can execute runnables |
117 |
|
|
*/ |
118 |
|
|
public void testNewFixedThreadPool1() { |
119 |
|
|
ExecutorService e = Executors.newFixedThreadPool(2); |
120 |
|
|
e.execute(new NoOpRunnable()); |
121 |
|
|
e.execute(new NoOpRunnable()); |
122 |
|
|
e.execute(new NoOpRunnable()); |
123 |
dl |
1.14 |
joinPool(e); |
124 |
dl |
1.5 |
} |
125 |
|
|
|
126 |
|
|
/** |
127 |
|
|
* A new newFixedThreadPool with given ThreadFactory can execute runnables |
128 |
|
|
*/ |
129 |
|
|
public void testNewFixedThreadPool2() { |
130 |
dl |
1.6 |
ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory()); |
131 |
dl |
1.5 |
e.execute(new NoOpRunnable()); |
132 |
|
|
e.execute(new NoOpRunnable()); |
133 |
|
|
e.execute(new NoOpRunnable()); |
134 |
dl |
1.14 |
joinPool(e); |
135 |
dl |
1.5 |
} |
136 |
|
|
|
137 |
|
|
/** |
138 |
|
|
* A new newFixedThreadPool with null ThreadFactory throws NPE |
139 |
|
|
*/ |
140 |
|
|
public void testNewFixedThreadPool3() { |
141 |
|
|
try { |
142 |
|
|
ExecutorService e = Executors.newFixedThreadPool(2, null); |
143 |
|
|
shouldThrow(); |
144 |
jsr166 |
1.23 |
} catch (NullPointerException success) {} |
145 |
dl |
1.5 |
} |
146 |
|
|
|
147 |
|
|
/** |
148 |
|
|
* A new newFixedThreadPool with 0 threads throws IAE |
149 |
|
|
*/ |
150 |
|
|
public void testNewFixedThreadPool4() { |
151 |
|
|
try { |
152 |
|
|
ExecutorService e = Executors.newFixedThreadPool(0); |
153 |
|
|
shouldThrow(); |
154 |
jsr166 |
1.23 |
} catch (IllegalArgumentException success) {} |
155 |
dl |
1.5 |
} |
156 |
dl |
1.1 |
|
157 |
dl |
1.2 |
/** |
158 |
dl |
1.11 |
* An unconfigurable newFixedThreadPool can execute runnables |
159 |
|
|
*/ |
160 |
jsr166 |
1.42 |
public void testUnconfigurableExecutorService() { |
161 |
dl |
1.11 |
ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2)); |
162 |
|
|
e.execute(new NoOpRunnable()); |
163 |
|
|
e.execute(new NoOpRunnable()); |
164 |
|
|
e.execute(new NoOpRunnable()); |
165 |
dl |
1.14 |
joinPool(e); |
166 |
dl |
1.11 |
} |
167 |
|
|
|
168 |
|
|
/** |
169 |
|
|
* unconfigurableExecutorService(null) throws NPE |
170 |
|
|
*/ |
171 |
jsr166 |
1.42 |
public void testUnconfigurableExecutorServiceNPE() { |
172 |
dl |
1.11 |
try { |
173 |
|
|
ExecutorService e = Executors.unconfigurableExecutorService(null); |
174 |
jsr166 |
1.23 |
shouldThrow(); |
175 |
|
|
} catch (NullPointerException success) {} |
176 |
dl |
1.11 |
} |
177 |
|
|
|
178 |
|
|
/** |
179 |
|
|
* unconfigurableScheduledExecutorService(null) throws NPE |
180 |
|
|
*/ |
181 |
jsr166 |
1.42 |
public void testUnconfigurableScheduledExecutorServiceNPE() { |
182 |
dl |
1.11 |
try { |
183 |
|
|
ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); |
184 |
jsr166 |
1.23 |
shouldThrow(); |
185 |
|
|
} catch (NullPointerException success) {} |
186 |
dl |
1.11 |
} |
187 |
|
|
|
188 |
|
|
/** |
189 |
|
|
* a newSingleThreadScheduledExecutor successfully runs delayed task |
190 |
|
|
*/ |
191 |
jsr166 |
1.23 |
public void testNewSingleThreadScheduledExecutor() throws Exception { |
192 |
jsr166 |
1.35 |
ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor(); |
193 |
|
|
try { |
194 |
jsr166 |
1.42 |
final CountDownLatch proceed = new CountDownLatch(1); |
195 |
jsr166 |
1.35 |
final Runnable task = new CheckedRunnable() { |
196 |
|
|
public void realRun() { |
197 |
jsr166 |
1.42 |
await(proceed); |
198 |
jsr166 |
1.35 |
}}; |
199 |
jsr166 |
1.42 |
long startTime = System.nanoTime(); |
200 |
jsr166 |
1.35 |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
201 |
jsr166 |
1.42 |
timeoutMillis(), MILLISECONDS); |
202 |
jsr166 |
1.35 |
assertFalse(f.isDone()); |
203 |
jsr166 |
1.42 |
proceed.countDown(); |
204 |
|
|
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); |
205 |
jsr166 |
1.36 |
assertSame(Boolean.TRUE, f.get()); |
206 |
jsr166 |
1.35 |
assertTrue(f.isDone()); |
207 |
jsr166 |
1.42 |
assertFalse(f.isCancelled()); |
208 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
209 |
jsr166 |
1.35 |
} finally { |
210 |
|
|
joinPool(p); |
211 |
|
|
} |
212 |
dl |
1.11 |
} |
213 |
|
|
|
214 |
|
|
/** |
215 |
|
|
* a newScheduledThreadPool successfully runs delayed task |
216 |
|
|
*/ |
217 |
jsr166 |
1.42 |
public void testNewScheduledThreadPool() throws Exception { |
218 |
jsr166 |
1.35 |
ScheduledExecutorService p = Executors.newScheduledThreadPool(2); |
219 |
|
|
try { |
220 |
jsr166 |
1.42 |
final CountDownLatch proceed = new CountDownLatch(1); |
221 |
jsr166 |
1.35 |
final Runnable task = new CheckedRunnable() { |
222 |
|
|
public void realRun() { |
223 |
jsr166 |
1.42 |
await(proceed); |
224 |
jsr166 |
1.35 |
}}; |
225 |
jsr166 |
1.42 |
long startTime = System.nanoTime(); |
226 |
jsr166 |
1.35 |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
227 |
jsr166 |
1.42 |
timeoutMillis(), MILLISECONDS); |
228 |
jsr166 |
1.35 |
assertFalse(f.isDone()); |
229 |
jsr166 |
1.42 |
proceed.countDown(); |
230 |
|
|
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); |
231 |
jsr166 |
1.36 |
assertSame(Boolean.TRUE, f.get()); |
232 |
jsr166 |
1.35 |
assertTrue(f.isDone()); |
233 |
jsr166 |
1.42 |
assertFalse(f.isCancelled()); |
234 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
235 |
jsr166 |
1.35 |
} finally { |
236 |
|
|
joinPool(p); |
237 |
|
|
} |
238 |
dl |
1.11 |
} |
239 |
|
|
|
240 |
|
|
/** |
241 |
jsr166 |
1.24 |
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
242 |
dl |
1.11 |
*/ |
243 |
jsr166 |
1.42 |
public void testUnconfigurableScheduledExecutorService() throws Exception { |
244 |
jsr166 |
1.35 |
ScheduledExecutorService p = |
245 |
|
|
Executors.unconfigurableScheduledExecutorService |
246 |
|
|
(Executors.newScheduledThreadPool(2)); |
247 |
|
|
try { |
248 |
jsr166 |
1.42 |
final CountDownLatch proceed = new CountDownLatch(1); |
249 |
jsr166 |
1.35 |
final Runnable task = new CheckedRunnable() { |
250 |
|
|
public void realRun() { |
251 |
jsr166 |
1.42 |
await(proceed); |
252 |
jsr166 |
1.35 |
}}; |
253 |
jsr166 |
1.42 |
long startTime = System.nanoTime(); |
254 |
jsr166 |
1.35 |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
255 |
jsr166 |
1.42 |
timeoutMillis(), MILLISECONDS); |
256 |
jsr166 |
1.35 |
assertFalse(f.isDone()); |
257 |
jsr166 |
1.42 |
proceed.countDown(); |
258 |
|
|
assertSame(Boolean.TRUE, f.get(LONG_DELAY_MS, MILLISECONDS)); |
259 |
jsr166 |
1.36 |
assertSame(Boolean.TRUE, f.get()); |
260 |
jsr166 |
1.35 |
assertTrue(f.isDone()); |
261 |
jsr166 |
1.42 |
assertFalse(f.isCancelled()); |
262 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
263 |
jsr166 |
1.35 |
} finally { |
264 |
|
|
joinPool(p); |
265 |
|
|
} |
266 |
dl |
1.11 |
} |
267 |
|
|
|
268 |
|
|
/** |
269 |
jsr166 |
1.34 |
* Future.get on submitted tasks will time out if they compute too long. |
270 |
dl |
1.2 |
*/ |
271 |
jsr166 |
1.23 |
public void testTimedCallable() throws Exception { |
272 |
jsr166 |
1.40 |
final ExecutorService[] executors = { |
273 |
|
|
Executors.newSingleThreadExecutor(), |
274 |
|
|
Executors.newCachedThreadPool(), |
275 |
|
|
Executors.newFixedThreadPool(2), |
276 |
|
|
Executors.newScheduledThreadPool(2), |
277 |
|
|
}; |
278 |
|
|
|
279 |
jsr166 |
1.33 |
final Runnable sleeper = new CheckedInterruptedRunnable() { |
280 |
|
|
public void realRun() throws InterruptedException { |
281 |
dl |
1.39 |
delay(LONG_DELAY_MS); |
282 |
jsr166 |
1.33 |
}}; |
283 |
jsr166 |
1.40 |
|
284 |
|
|
List<Thread> threads = new ArrayList<Thread>(); |
285 |
|
|
for (final ExecutorService executor : executors) { |
286 |
|
|
threads.add(newStartedThread(new CheckedRunnable() { |
287 |
|
|
public void realRun() { |
288 |
|
|
long startTime = System.nanoTime(); |
289 |
|
|
Future future = executor.submit(sleeper); |
290 |
|
|
assertFutureTimesOut(future); |
291 |
|
|
}})); |
292 |
dl |
1.2 |
} |
293 |
jsr166 |
1.40 |
for (Thread thread : threads) |
294 |
|
|
awaitTermination(thread); |
295 |
|
|
for (ExecutorService executor : executors) |
296 |
|
|
joinPool(executor); |
297 |
dl |
1.2 |
} |
298 |
|
|
|
299 |
dl |
1.8 |
/** |
300 |
|
|
* ThreadPoolExecutor using defaultThreadFactory has |
301 |
|
|
* specified group, priority, daemon status, and name |
302 |
|
|
*/ |
303 |
jsr166 |
1.23 |
public void testDefaultThreadFactory() throws Exception { |
304 |
tim |
1.9 |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
305 |
jsr166 |
1.42 |
final CountDownLatch done = new CountDownLatch(1); |
306 |
jsr166 |
1.31 |
Runnable r = new CheckedRunnable() { |
307 |
|
|
public void realRun() { |
308 |
|
|
try { |
309 |
|
|
Thread current = Thread.currentThread(); |
310 |
|
|
assertTrue(!current.isDaemon()); |
311 |
|
|
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
312 |
|
|
ThreadGroup g = current.getThreadGroup(); |
313 |
|
|
SecurityManager s = System.getSecurityManager(); |
314 |
|
|
if (s != null) |
315 |
|
|
assertTrue(g == s.getThreadGroup()); |
316 |
|
|
else |
317 |
|
|
assertTrue(g == egroup); |
318 |
|
|
String name = current.getName(); |
319 |
|
|
assertTrue(name.endsWith("thread-1")); |
320 |
|
|
} catch (SecurityException ok) { |
321 |
|
|
// Also pass if not allowed to change setting |
322 |
tim |
1.9 |
} |
323 |
jsr166 |
1.42 |
done.countDown(); |
324 |
jsr166 |
1.31 |
}}; |
325 |
tim |
1.9 |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
326 |
jsr166 |
1.20 |
|
327 |
tim |
1.9 |
e.execute(r); |
328 |
jsr166 |
1.42 |
await(done); |
329 |
|
|
|
330 |
dl |
1.14 |
try { |
331 |
|
|
e.shutdown(); |
332 |
jsr166 |
1.21 |
} catch (SecurityException ok) { |
333 |
dl |
1.14 |
} |
334 |
jsr166 |
1.20 |
|
335 |
jsr166 |
1.42 |
joinPool(e); |
336 |
dl |
1.8 |
} |
337 |
|
|
|
338 |
|
|
/** |
339 |
|
|
* ThreadPoolExecutor using privilegedThreadFactory has |
340 |
|
|
* specified group, priority, daemon status, name, |
341 |
|
|
* access control context and context class loader |
342 |
|
|
*/ |
343 |
jsr166 |
1.23 |
public void testPrivilegedThreadFactory() throws Exception { |
344 |
jsr166 |
1.42 |
final CountDownLatch done = new CountDownLatch(1); |
345 |
jsr166 |
1.29 |
Runnable r = new CheckedRunnable() { |
346 |
|
|
public void realRun() throws Exception { |
347 |
|
|
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
348 |
|
|
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
349 |
|
|
final AccessControlContext thisacc = AccessController.getContext(); |
350 |
|
|
Runnable r = new CheckedRunnable() { |
351 |
|
|
public void realRun() { |
352 |
jsr166 |
1.25 |
Thread current = Thread.currentThread(); |
353 |
jsr166 |
1.29 |
assertTrue(!current.isDaemon()); |
354 |
|
|
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
355 |
jsr166 |
1.25 |
ThreadGroup g = current.getThreadGroup(); |
356 |
|
|
SecurityManager s = System.getSecurityManager(); |
357 |
|
|
if (s != null) |
358 |
jsr166 |
1.29 |
assertTrue(g == s.getThreadGroup()); |
359 |
jsr166 |
1.25 |
else |
360 |
jsr166 |
1.29 |
assertTrue(g == egroup); |
361 |
jsr166 |
1.25 |
String name = current.getName(); |
362 |
jsr166 |
1.29 |
assertTrue(name.endsWith("thread-1")); |
363 |
jsr166 |
1.31 |
assertSame(thisccl, current.getContextClassLoader()); |
364 |
|
|
assertEquals(thisacc, AccessController.getContext()); |
365 |
jsr166 |
1.42 |
done.countDown(); |
366 |
jsr166 |
1.29 |
}}; |
367 |
|
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
368 |
|
|
e.execute(r); |
369 |
jsr166 |
1.42 |
await(done); |
370 |
jsr166 |
1.29 |
e.shutdown(); |
371 |
|
|
joinPool(e); |
372 |
|
|
}}; |
373 |
|
|
|
374 |
|
|
runWithPermissions(r, |
375 |
|
|
new RuntimePermission("getClassLoader"), |
376 |
|
|
new RuntimePermission("setContextClassLoader"), |
377 |
|
|
new RuntimePermission("modifyThread")); |
378 |
|
|
} |
379 |
jsr166 |
1.20 |
|
380 |
jsr166 |
1.29 |
boolean haveCCLPermissions() { |
381 |
|
|
SecurityManager sm = System.getSecurityManager(); |
382 |
|
|
if (sm != null) { |
383 |
|
|
try { |
384 |
|
|
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
385 |
|
|
sm.checkPermission(new RuntimePermission("getClassLoader")); |
386 |
|
|
} catch (AccessControlException e) { |
387 |
|
|
return false; |
388 |
|
|
} |
389 |
tim |
1.9 |
} |
390 |
jsr166 |
1.29 |
return true; |
391 |
dl |
1.8 |
} |
392 |
dl |
1.11 |
|
393 |
dl |
1.16 |
void checkCCL() { |
394 |
dl |
1.18 |
SecurityManager sm = System.getSecurityManager(); |
395 |
|
|
if (sm != null) { |
396 |
|
|
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
397 |
jsr166 |
1.19 |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
398 |
dl |
1.18 |
} |
399 |
dl |
1.16 |
} |
400 |
|
|
|
401 |
|
|
class CheckCCL implements Callable<Object> { |
402 |
dl |
1.11 |
public Object call() { |
403 |
dl |
1.16 |
checkCCL(); |
404 |
dl |
1.11 |
return null; |
405 |
|
|
} |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
/** |
409 |
|
|
* Without class loader permissions, creating |
410 |
|
|
* privilegedCallableUsingCurrentClassLoader throws ACE |
411 |
|
|
*/ |
412 |
|
|
public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { |
413 |
jsr166 |
1.29 |
Runnable r = new CheckedRunnable() { |
414 |
|
|
public void realRun() throws Exception { |
415 |
|
|
if (System.getSecurityManager() == null) |
416 |
|
|
return; |
417 |
|
|
try { |
418 |
|
|
Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
419 |
|
|
shouldThrow(); |
420 |
|
|
} catch (AccessControlException success) {} |
421 |
|
|
}}; |
422 |
dl |
1.16 |
|
423 |
jsr166 |
1.29 |
runWithoutPermissions(r); |
424 |
dl |
1.11 |
} |
425 |
|
|
|
426 |
|
|
/** |
427 |
dl |
1.16 |
* With class loader permissions, calling |
428 |
|
|
* privilegedCallableUsingCurrentClassLoader does not throw ACE |
429 |
dl |
1.11 |
*/ |
430 |
jsr166 |
1.42 |
public void testPrivilegedCallableUsingCCLWithPrivs() throws Exception { |
431 |
jsr166 |
1.29 |
Runnable r = new CheckedRunnable() { |
432 |
|
|
public void realRun() throws Exception { |
433 |
|
|
Executors.privilegedCallableUsingCurrentClassLoader |
434 |
|
|
(new NoOpCallable()) |
435 |
|
|
.call(); |
436 |
|
|
}}; |
437 |
|
|
|
438 |
|
|
runWithPermissions(r, |
439 |
|
|
new RuntimePermission("getClassLoader"), |
440 |
|
|
new RuntimePermission("setContextClassLoader")); |
441 |
dl |
1.11 |
} |
442 |
|
|
|
443 |
|
|
/** |
444 |
|
|
* Without permissions, calling privilegedCallable throws ACE |
445 |
|
|
*/ |
446 |
jsr166 |
1.42 |
public void testPrivilegedCallableWithNoPrivs() throws Exception { |
447 |
jsr166 |
1.32 |
// Avoid classloader-related SecurityExceptions in swingui.TestRunner |
448 |
|
|
Executors.privilegedCallable(new CheckCCL()); |
449 |
|
|
|
450 |
jsr166 |
1.29 |
Runnable r = new CheckedRunnable() { |
451 |
|
|
public void realRun() throws Exception { |
452 |
|
|
if (System.getSecurityManager() == null) |
453 |
|
|
return; |
454 |
|
|
Callable task = Executors.privilegedCallable(new CheckCCL()); |
455 |
|
|
try { |
456 |
|
|
task.call(); |
457 |
|
|
shouldThrow(); |
458 |
|
|
} catch (AccessControlException success) {} |
459 |
|
|
}}; |
460 |
|
|
|
461 |
|
|
runWithoutPermissions(r); |
462 |
|
|
|
463 |
|
|
// It seems rather difficult to test that the |
464 |
|
|
// AccessControlContext of the privilegedCallable is used |
465 |
|
|
// instead of its caller. Below is a failed attempt to do |
466 |
|
|
// that, which does not work because the AccessController |
467 |
|
|
// cannot capture the internal state of the current Policy. |
468 |
|
|
// It would be much more work to differentiate based on, |
469 |
|
|
// e.g. CodeSource. |
470 |
|
|
|
471 |
|
|
// final AccessControlContext[] noprivAcc = new AccessControlContext[1]; |
472 |
|
|
// final Callable[] task = new Callable[1]; |
473 |
|
|
|
474 |
|
|
// runWithPermissions |
475 |
|
|
// (new CheckedRunnable() { |
476 |
|
|
// public void realRun() { |
477 |
|
|
// if (System.getSecurityManager() == null) |
478 |
|
|
// return; |
479 |
|
|
// noprivAcc[0] = AccessController.getContext(); |
480 |
|
|
// task[0] = Executors.privilegedCallable(new CheckCCL()); |
481 |
|
|
// try { |
482 |
|
|
// AccessController.doPrivileged(new PrivilegedAction<Void>() { |
483 |
|
|
// public Void run() { |
484 |
|
|
// checkCCL(); |
485 |
|
|
// return null; |
486 |
|
|
// }}, noprivAcc[0]); |
487 |
|
|
// shouldThrow(); |
488 |
|
|
// } catch (AccessControlException success) {} |
489 |
|
|
// }}); |
490 |
|
|
|
491 |
|
|
// runWithPermissions |
492 |
|
|
// (new CheckedRunnable() { |
493 |
|
|
// public void realRun() throws Exception { |
494 |
|
|
// if (System.getSecurityManager() == null) |
495 |
|
|
// return; |
496 |
|
|
// // Verify that we have an underprivileged ACC |
497 |
|
|
// try { |
498 |
|
|
// AccessController.doPrivileged(new PrivilegedAction<Void>() { |
499 |
|
|
// public Void run() { |
500 |
|
|
// checkCCL(); |
501 |
|
|
// return null; |
502 |
|
|
// }}, noprivAcc[0]); |
503 |
|
|
// shouldThrow(); |
504 |
|
|
// } catch (AccessControlException success) {} |
505 |
|
|
|
506 |
|
|
// try { |
507 |
|
|
// task[0].call(); |
508 |
|
|
// shouldThrow(); |
509 |
|
|
// } catch (AccessControlException success) {} |
510 |
|
|
// }}, |
511 |
|
|
// new RuntimePermission("getClassLoader"), |
512 |
|
|
// new RuntimePermission("setContextClassLoader")); |
513 |
dl |
1.11 |
} |
514 |
|
|
|
515 |
|
|
/** |
516 |
|
|
* With permissions, calling privilegedCallable succeeds |
517 |
|
|
*/ |
518 |
jsr166 |
1.42 |
public void testPrivilegedCallableWithPrivs() throws Exception { |
519 |
jsr166 |
1.29 |
Runnable r = new CheckedRunnable() { |
520 |
|
|
public void realRun() throws Exception { |
521 |
|
|
Executors.privilegedCallable(new CheckCCL()).call(); |
522 |
|
|
}}; |
523 |
|
|
|
524 |
jsr166 |
1.38 |
runWithPermissions(r, |
525 |
jsr166 |
1.29 |
new RuntimePermission("getClassLoader"), |
526 |
|
|
new RuntimePermission("setContextClassLoader")); |
527 |
dl |
1.11 |
} |
528 |
|
|
|
529 |
|
|
/** |
530 |
|
|
* callable(Runnable) returns null when called |
531 |
jsr166 |
1.20 |
*/ |
532 |
jsr166 |
1.23 |
public void testCallable1() throws Exception { |
533 |
|
|
Callable c = Executors.callable(new NoOpRunnable()); |
534 |
|
|
assertNull(c.call()); |
535 |
dl |
1.11 |
} |
536 |
|
|
|
537 |
|
|
/** |
538 |
|
|
* callable(Runnable, result) returns result when called |
539 |
jsr166 |
1.20 |
*/ |
540 |
jsr166 |
1.23 |
public void testCallable2() throws Exception { |
541 |
|
|
Callable c = Executors.callable(new NoOpRunnable(), one); |
542 |
jsr166 |
1.27 |
assertSame(one, c.call()); |
543 |
dl |
1.11 |
} |
544 |
|
|
|
545 |
|
|
/** |
546 |
|
|
* callable(PrivilegedAction) returns its result when called |
547 |
jsr166 |
1.20 |
*/ |
548 |
jsr166 |
1.23 |
public void testCallable3() throws Exception { |
549 |
|
|
Callable c = Executors.callable(new PrivilegedAction() { |
550 |
|
|
public Object run() { return one; }}); |
551 |
jsr166 |
1.27 |
assertSame(one, c.call()); |
552 |
dl |
1.11 |
} |
553 |
|
|
|
554 |
|
|
/** |
555 |
|
|
* callable(PrivilegedExceptionAction) returns its result when called |
556 |
jsr166 |
1.20 |
*/ |
557 |
jsr166 |
1.23 |
public void testCallable4() throws Exception { |
558 |
|
|
Callable c = Executors.callable(new PrivilegedExceptionAction() { |
559 |
|
|
public Object run() { return one; }}); |
560 |
jsr166 |
1.27 |
assertSame(one, c.call()); |
561 |
dl |
1.11 |
} |
562 |
|
|
|
563 |
|
|
/** |
564 |
|
|
* callable(null Runnable) throws NPE |
565 |
jsr166 |
1.20 |
*/ |
566 |
dl |
1.11 |
public void testCallableNPE1() { |
567 |
|
|
try { |
568 |
jsr166 |
1.23 |
Callable c = Executors.callable((Runnable) null); |
569 |
|
|
shouldThrow(); |
570 |
|
|
} catch (NullPointerException success) {} |
571 |
dl |
1.11 |
} |
572 |
|
|
|
573 |
|
|
/** |
574 |
|
|
* callable(null, result) throws NPE |
575 |
jsr166 |
1.20 |
*/ |
576 |
dl |
1.11 |
public void testCallableNPE2() { |
577 |
|
|
try { |
578 |
jsr166 |
1.23 |
Callable c = Executors.callable((Runnable) null, one); |
579 |
|
|
shouldThrow(); |
580 |
|
|
} catch (NullPointerException success) {} |
581 |
dl |
1.11 |
} |
582 |
|
|
|
583 |
|
|
/** |
584 |
|
|
* callable(null PrivilegedAction) throws NPE |
585 |
jsr166 |
1.20 |
*/ |
586 |
dl |
1.11 |
public void testCallableNPE3() { |
587 |
|
|
try { |
588 |
jsr166 |
1.23 |
Callable c = Executors.callable((PrivilegedAction) null); |
589 |
|
|
shouldThrow(); |
590 |
|
|
} catch (NullPointerException success) {} |
591 |
dl |
1.11 |
} |
592 |
|
|
|
593 |
|
|
/** |
594 |
|
|
* callable(null PrivilegedExceptionAction) throws NPE |
595 |
jsr166 |
1.20 |
*/ |
596 |
dl |
1.11 |
public void testCallableNPE4() { |
597 |
|
|
try { |
598 |
jsr166 |
1.23 |
Callable c = Executors.callable((PrivilegedExceptionAction) null); |
599 |
|
|
shouldThrow(); |
600 |
|
|
} catch (NullPointerException success) {} |
601 |
dl |
1.11 |
} |
602 |
|
|
|
603 |
dl |
1.1 |
} |