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) { |
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 |
|
*/ |
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 |
|
} |