2 |
|
* 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 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
21 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
22 |
|
} |
23 |
|
|
24 |
< |
/** |
24 |
> |
/** |
25 |
|
* A no-frills implementation of AbstractExecutorService, designed |
26 |
|
* to test the submit methods only. |
27 |
|
*/ |
115 |
|
* A submitted privileged action to completion |
116 |
|
*/ |
117 |
|
public void testSubmitPrivilegedAction() { |
118 |
< |
Policy savedPolicy = Policy.getPolicy(); |
119 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
120 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
121 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
122 |
< |
Policy.setPolicy(policy); |
118 |
> |
Policy savedPolicy = null; |
119 |
> |
try { |
120 |
> |
savedPolicy = Policy.getPolicy(); |
121 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
122 |
> |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
123 |
> |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
124 |
> |
Policy.setPolicy(policy); |
125 |
> |
} catch(AccessControlException ok) { |
126 |
> |
return; |
127 |
> |
} |
128 |
|
try { |
129 |
|
ExecutorService e = new DirectExecutorService(); |
130 |
|
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
142 |
|
unexpectedException(); |
143 |
|
} |
144 |
|
finally { |
145 |
< |
Policy.setPolicy(savedPolicy); |
145 |
> |
try { |
146 |
> |
Policy.setPolicy(savedPolicy); |
147 |
> |
} catch(AccessControlException ok) { |
148 |
> |
return; |
149 |
> |
} |
150 |
|
} |
151 |
|
} |
152 |
|
|
154 |
|
* A submitted a privileged exception action runs to completion |
155 |
|
*/ |
156 |
|
public void testSubmitPrivilegedExceptionAction() { |
157 |
< |
Policy savedPolicy = Policy.getPolicy(); |
158 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
159 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
160 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
161 |
< |
Policy.setPolicy(policy); |
157 |
> |
Policy savedPolicy = null; |
158 |
> |
try { |
159 |
> |
savedPolicy = Policy.getPolicy(); |
160 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
161 |
> |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
162 |
> |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
163 |
> |
Policy.setPolicy(policy); |
164 |
> |
} catch(AccessControlException ok) { |
165 |
> |
return; |
166 |
> |
} |
167 |
> |
|
168 |
|
try { |
169 |
|
ExecutorService e = new DirectExecutorService(); |
170 |
|
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
190 |
|
* A submitted failed privileged exception action reports exception |
191 |
|
*/ |
192 |
|
public void testSubmitFailedPrivilegedExceptionAction() { |
193 |
< |
Policy savedPolicy = Policy.getPolicy(); |
194 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
195 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
196 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
197 |
< |
Policy.setPolicy(policy); |
193 |
> |
Policy savedPolicy = null; |
194 |
> |
try { |
195 |
> |
savedPolicy = Policy.getPolicy(); |
196 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
197 |
> |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
198 |
> |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
199 |
> |
Policy.setPolicy(policy); |
200 |
> |
} catch(AccessControlException ok) { |
201 |
> |
return; |
202 |
> |
} |
203 |
> |
|
204 |
> |
|
205 |
|
try { |
206 |
|
ExecutorService e = new DirectExecutorService(); |
207 |
|
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
536 |
|
l.add(new NPETask()); |
537 |
|
List<Future<String>> result = e.invokeAll(l); |
538 |
|
assertEquals(1, result.size()); |
539 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
539 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
540 |
|
it.next().get(); |
541 |
|
} catch(ExecutionException success) { |
542 |
|
} catch(Exception ex) { |
557 |
|
l.add(new StringTask()); |
558 |
|
List<Future<String>> result = e.invokeAll(l); |
559 |
|
assertEquals(2, result.size()); |
560 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
560 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
561 |
|
assertSame(TEST_STRING, it.next().get()); |
562 |
|
} catch (ExecutionException success) { |
563 |
|
} catch(Exception ex) { |
745 |
|
l.add(new NPETask()); |
746 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
747 |
|
assertEquals(1, result.size()); |
748 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
748 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
749 |
|
it.next().get(); |
750 |
|
} catch(ExecutionException success) { |
751 |
|
} catch(Exception ex) { |
766 |
|
l.add(new StringTask()); |
767 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
768 |
|
assertEquals(2, result.size()); |
769 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
769 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
770 |
|
assertSame(TEST_STRING, it.next().get()); |
771 |
|
} catch (ExecutionException success) { |
772 |
|
} catch(Exception ex) { |
788 |
|
l.add(new StringTask()); |
789 |
|
List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
790 |
|
assertEquals(3, result.size()); |
791 |
< |
Iterator<Future<String>> it = result.iterator(); |
791 |
> |
Iterator<Future<String>> it = result.iterator(); |
792 |
|
Future<String> f1 = it.next(); |
793 |
|
Future<String> f2 = it.next(); |
794 |
|
Future<String> f3 = it.next(); |