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