10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
|
import java.math.BigInteger; |
13 |
+ |
import java.security.*; |
14 |
|
|
15 |
|
public class ExecutorsTest extends JSR166TestCase{ |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(ExecutorsTest.class); |
20 |
> |
return new TestSuite(ExecutorsTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
private static final String TEST_STRING = "a test string"; |
190 |
|
/** |
191 |
|
* execute of runnable runs it to completion |
192 |
|
*/ |
193 |
< |
public void testExecuteRunnable () { |
193 |
> |
public void testExecuteRunnable() { |
194 |
|
try { |
195 |
|
Executor e = new DirectExecutor(); |
196 |
< |
TrackedRunnable task = new TrackedRunnable(); |
196 |
> |
TrackedShortRunnable task = new TrackedShortRunnable(); |
197 |
|
assertFalse(task.done); |
198 |
< |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
199 |
< |
String result = future.get(); |
198 |
> |
Future<?> future = Executors.execute(e, task); |
199 |
> |
future.get(); |
200 |
|
assertTrue(task.done); |
200 |
– |
assertSame(TEST_STRING, result); |
201 |
|
} |
202 |
|
catch (ExecutionException ex) { |
203 |
|
unexpectedException(); |
210 |
|
/** |
211 |
|
* invoke of a runnable runs it to completion |
212 |
|
*/ |
213 |
< |
public void testInvokeRunnable () { |
213 |
> |
public void testInvokeRunnable() { |
214 |
|
try { |
215 |
|
Executor e = new DirectExecutor(); |
216 |
< |
TrackedRunnable task = new TrackedRunnable(); |
216 |
> |
TrackedShortRunnable task = new TrackedShortRunnable(); |
217 |
|
assertFalse(task.done); |
218 |
|
Executors.invoke(e, task); |
219 |
|
assertTrue(task.done); |
229 |
|
/** |
230 |
|
* execute of a callable runs it to completion |
231 |
|
*/ |
232 |
< |
public void testExecuteCallable () { |
232 |
> |
public void testExecuteCallable() { |
233 |
|
try { |
234 |
|
Executor e = new DirectExecutor(); |
235 |
|
Future<String> future = Executors.execute(e, new StringTask()); |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
+ |
|
248 |
+ |
/** |
249 |
+ |
* execute of a privileged action runs it to completion |
250 |
+ |
*/ |
251 |
+ |
public void testExecutePrivilegedAction() { |
252 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
253 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
254 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
255 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
256 |
+ |
Policy.setPolicy(policy); |
257 |
+ |
try { |
258 |
+ |
Executor e = new DirectExecutor(); |
259 |
+ |
Future future = Executors.execute(e, new PrivilegedAction() { |
260 |
+ |
public Object run() { |
261 |
+ |
return TEST_STRING; |
262 |
+ |
}}); |
263 |
+ |
|
264 |
+ |
Object result = future.get(); |
265 |
+ |
assertSame(TEST_STRING, result); |
266 |
+ |
} |
267 |
+ |
catch (ExecutionException ex) { |
268 |
+ |
unexpectedException(); |
269 |
+ |
} |
270 |
+ |
catch (InterruptedException ex) { |
271 |
+ |
unexpectedException(); |
272 |
+ |
} |
273 |
+ |
finally { |
274 |
+ |
Policy.setPolicy(savedPolicy); |
275 |
+ |
} |
276 |
+ |
} |
277 |
+ |
|
278 |
+ |
/** |
279 |
+ |
* execute of a privileged exception action runs it to completion |
280 |
+ |
*/ |
281 |
+ |
public void testExecutePrivilegedExceptionAction() { |
282 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
283 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
284 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
285 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
286 |
+ |
Policy.setPolicy(policy); |
287 |
+ |
try { |
288 |
+ |
Executor e = new DirectExecutor(); |
289 |
+ |
Future future = Executors.execute(e, new PrivilegedExceptionAction() { |
290 |
+ |
public Object run() { |
291 |
+ |
return TEST_STRING; |
292 |
+ |
}}); |
293 |
+ |
|
294 |
+ |
Object result = future.get(); |
295 |
+ |
assertSame(TEST_STRING, result); |
296 |
+ |
} |
297 |
+ |
catch (ExecutionException ex) { |
298 |
+ |
unexpectedException(); |
299 |
+ |
} |
300 |
+ |
catch (InterruptedException ex) { |
301 |
+ |
unexpectedException(); |
302 |
+ |
} |
303 |
+ |
finally { |
304 |
+ |
Policy.setPolicy(savedPolicy); |
305 |
+ |
} |
306 |
+ |
} |
307 |
+ |
|
308 |
+ |
/** |
309 |
+ |
* execute of a failed privileged exception action reports exception |
310 |
+ |
*/ |
311 |
+ |
public void testExecuteFailedPrivilegedExceptionAction() { |
312 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
313 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
314 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
315 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
316 |
+ |
Policy.setPolicy(policy); |
317 |
+ |
try { |
318 |
+ |
Executor e = new DirectExecutor(); |
319 |
+ |
Future future = Executors.execute(e, new PrivilegedExceptionAction() { |
320 |
+ |
public Object run() throws Exception { |
321 |
+ |
throw new IndexOutOfBoundsException(); |
322 |
+ |
}}); |
323 |
+ |
|
324 |
+ |
Object result = future.get(); |
325 |
+ |
shouldThrow(); |
326 |
+ |
} |
327 |
+ |
catch (ExecutionException success) { |
328 |
+ |
} |
329 |
+ |
catch (InterruptedException ex) { |
330 |
+ |
unexpectedException(); |
331 |
+ |
} |
332 |
+ |
finally { |
333 |
+ |
Policy.setPolicy(savedPolicy); |
334 |
+ |
} |
335 |
+ |
} |
336 |
+ |
|
337 |
|
/** |
338 |
|
* invoke of a collable runs it to completion |
339 |
|
*/ |
340 |
< |
public void testInvokeCallable () { |
340 |
> |
public void testInvokeCallable() { |
341 |
|
try { |
342 |
|
Executor e = new DirectExecutor(); |
343 |
|
String result = Executors.invoke(e, new StringTask()); |
355 |
|
/** |
356 |
|
* execute with null executor throws NPE |
357 |
|
*/ |
358 |
< |
public void testNullExecuteRunnable () { |
358 |
> |
public void testNullExecuteRunnable() { |
359 |
|
try { |
360 |
< |
TrackedRunnable task = new TrackedRunnable(); |
360 |
> |
TrackedShortRunnable task = new TrackedShortRunnable(); |
361 |
|
assertFalse(task.done); |
362 |
< |
Future<String> future = Executors.execute(null, task, TEST_STRING); |
362 |
> |
Future<?> future = Executors.execute(null, task); |
363 |
|
shouldThrow(); |
364 |
|
} |
365 |
|
catch (NullPointerException success) { |
375 |
|
public void testExecuteNullRunnable() { |
376 |
|
try { |
377 |
|
Executor e = new DirectExecutor(); |
378 |
< |
TrackedRunnable task = null; |
379 |
< |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
378 |
> |
TrackedShortRunnable task = null; |
379 |
> |
Future<?> future = Executors.execute(e, task); |
380 |
|
shouldThrow(); |
381 |
|
} |
382 |
|
catch (NullPointerException success) { |
389 |
|
/** |
390 |
|
* invoke of a null runnable throws NPE |
391 |
|
*/ |
392 |
< |
public void testInvokeNullRunnable () { |
392 |
> |
public void testInvokeNullRunnable() { |
393 |
|
try { |
394 |
|
Executor e = new DirectExecutor(); |
395 |
< |
TrackedRunnable task = null; |
395 |
> |
TrackedShortRunnable task = null; |
396 |
|
Executors.invoke(e, task); |
397 |
|
shouldThrow(); |
398 |
|
} |
406 |
|
/** |
407 |
|
* execute of a null callable throws NPE |
408 |
|
*/ |
409 |
< |
public void testExecuteNullCallable () { |
409 |
> |
public void testExecuteNullCallable() { |
410 |
|
try { |
411 |
|
Executor e = new DirectExecutor(); |
412 |
|
StringTask t = null; |
423 |
|
/** |
424 |
|
* invoke of a null callable throws NPE |
425 |
|
*/ |
426 |
< |
public void testInvokeNullCallable () { |
426 |
> |
public void testInvokeNullCallable() { |
427 |
|
try { |
428 |
|
Executor e = new DirectExecutor(); |
429 |
|
StringTask t = null; |
446 |
|
try { |
447 |
|
|
448 |
|
for(int i = 0; i < 5; ++i){ |
449 |
< |
Executors.execute(p, new MediumRunnable(), Boolean.TRUE); |
449 |
> |
Executors.execute(p, new MediumRunnable()); |
450 |
|
} |
451 |
|
shouldThrow(); |
452 |
|
} catch(RejectedExecutionException success){} |
642 |
|
} |
643 |
|
|
644 |
|
|
645 |
+ |
/** |
646 |
+ |
* ThreadPoolExecutor using defaultThreadFactory has |
647 |
+ |
* specified group, priority, daemon status, and name |
648 |
+ |
*/ |
649 |
+ |
public void testDefaultThreadFactory() { |
650 |
+ |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
651 |
+ |
Runnable r = new Runnable() { |
652 |
+ |
public void run() { |
653 |
+ |
Thread current = Thread.currentThread(); |
654 |
+ |
threadAssertTrue(!current.isDaemon()); |
655 |
+ |
threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); |
656 |
+ |
ThreadGroup g = current.getThreadGroup(); |
657 |
+ |
SecurityManager s = System.getSecurityManager(); |
658 |
+ |
if (s != null) |
659 |
+ |
threadAssertTrue(g == s.getThreadGroup()); |
660 |
+ |
else |
661 |
+ |
threadAssertTrue(g == egroup); |
662 |
+ |
String name = current.getName(); |
663 |
+ |
threadAssertTrue(name.endsWith("thread-1")); |
664 |
+ |
} |
665 |
+ |
}; |
666 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
667 |
+ |
|
668 |
+ |
e.execute(r); |
669 |
+ |
e.shutdown(); |
670 |
+ |
try { |
671 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
672 |
+ |
} catch (Exception eX) { |
673 |
+ |
unexpectedException(); |
674 |
+ |
} finally { |
675 |
+ |
joinPool(e); |
676 |
+ |
} |
677 |
+ |
} |
678 |
|
|
679 |
+ |
/** |
680 |
+ |
* ThreadPoolExecutor using privilegedThreadFactory has |
681 |
+ |
* specified group, priority, daemon status, name, |
682 |
+ |
* access control context and context class loader |
683 |
+ |
*/ |
684 |
+ |
public void testPrivilegedThreadFactory() { |
685 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
686 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(); |
687 |
+ |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
688 |
+ |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
689 |
+ |
Policy.setPolicy(policy); |
690 |
+ |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
691 |
+ |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
692 |
+ |
final AccessControlContext thisacc = AccessController.getContext(); |
693 |
+ |
Runnable r = new Runnable() { |
694 |
+ |
public void run() { |
695 |
+ |
Thread current = Thread.currentThread(); |
696 |
+ |
threadAssertTrue(!current.isDaemon()); |
697 |
+ |
threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); |
698 |
+ |
ThreadGroup g = current.getThreadGroup(); |
699 |
+ |
SecurityManager s = System.getSecurityManager(); |
700 |
+ |
if (s != null) |
701 |
+ |
threadAssertTrue(g == s.getThreadGroup()); |
702 |
+ |
else |
703 |
+ |
threadAssertTrue(g == egroup); |
704 |
+ |
String name = current.getName(); |
705 |
+ |
threadAssertTrue(name.endsWith("thread-1")); |
706 |
+ |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
707 |
+ |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
708 |
+ |
} |
709 |
+ |
}; |
710 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
711 |
+ |
|
712 |
+ |
Policy.setPolicy(savedPolicy); |
713 |
+ |
e.execute(r); |
714 |
+ |
e.shutdown(); |
715 |
+ |
try { |
716 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
717 |
+ |
} catch (Exception ex) { |
718 |
+ |
unexpectedException(); |
719 |
+ |
} finally { |
720 |
+ |
joinPool(e); |
721 |
+ |
} |
722 |
+ |
|
723 |
+ |
} |
724 |
|
|
725 |
|
} |