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