ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AbstractExecutorServiceTest.java (file contents):
Revision 1.22 by jsr166, Tue Dec 1 22:51:44 2009 UTC vs.
Revision 1.25 by jsr166, Thu Sep 16 02:54:10 2010 UTC

# Line 16 | Line 16 | import java.security.*;
16  
17   public class AbstractExecutorServiceTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());
19 >        junit.textui.TestRunner.run(suite());
20      }
21      public static Test suite() {
22          return new TestSuite(AbstractExecutorServiceTest.class);
# Line 81 | Line 81 | public class AbstractExecutorServiceTest
81  
82  
83      /**
84 <     * A submitted privileged action to completion
84 >     * A submitted privileged action runs to completion
85       */
86      public void testSubmitPrivilegedAction() throws Exception {
87 <        Policy savedPolicy = null;
88 <        try {
89 <            savedPolicy = Policy.getPolicy();
90 <            AdjustablePolicy policy = new AdjustablePolicy();
91 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
92 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
93 <            Policy.setPolicy(policy);
94 <        } catch (AccessControlException ok) {
95 <            return;
96 <        }
97 <        try {
98 <            ExecutorService e = new DirectExecutorService();
99 <            Future future = e.submit(Executors.callable(new PrivilegedAction() {
87 >        Runnable r = new CheckedRunnable() {
88 >            public void realRun() throws Exception {
89 >                ExecutorService e = new DirectExecutorService();
90 >                Future future = e.submit(Executors.callable(new PrivilegedAction() {
91                      public Object run() {
92                          return TEST_STRING;
93                      }}));
94  
95 <            Object result = future.get();
96 <            assertSame(TEST_STRING, result);
97 <        }
98 <        finally {
99 <            try {
100 <                Policy.setPolicy(savedPolicy);
101 <            } catch (AccessControlException ok) {
111 <                return;
112 <            }
113 <        }
95 >                assertSame(TEST_STRING, future.get());
96 >            }};
97 >
98 >        runWithPermissions(r,
99 >                           new RuntimePermission("getClassLoader"),
100 >                           new RuntimePermission("setContextClassLoader"),
101 >                           new RuntimePermission("modifyThread"));
102      }
103  
104      /**
105 <     * A submitted a privileged exception action runs to completion
105 >     * A submitted privileged exception action runs to completion
106       */
107      public void testSubmitPrivilegedExceptionAction() throws Exception {
108 <        Policy savedPolicy = null;
109 <        try {
110 <            savedPolicy = Policy.getPolicy();
111 <            AdjustablePolicy policy = new AdjustablePolicy();
124 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
125 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
126 <            Policy.setPolicy(policy);
127 <        } catch (AccessControlException ok) {
128 <            return;
129 <        }
130 <
131 <        try {
132 <            ExecutorService e = new DirectExecutorService();
133 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
108 >        Runnable r = new CheckedRunnable() {
109 >            public void realRun() throws Exception {
110 >                ExecutorService e = new DirectExecutorService();
111 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
112                      public Object run() {
113                          return TEST_STRING;
114                      }}));
115  
116 <            Object result = future.get();
117 <            assertSame(TEST_STRING, result);
118 <        }
119 <        finally {
142 <            Policy.setPolicy(savedPolicy);
143 <        }
116 >                assertSame(TEST_STRING, future.get());
117 >            }};
118 >
119 >        runWithPermissions(r);
120      }
121  
122      /**
123       * A submitted failed privileged exception action reports exception
124       */
125      public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
126 <        Policy savedPolicy = null;
127 <        try {
128 <            savedPolicy = Policy.getPolicy();
129 <            AdjustablePolicy policy = new AdjustablePolicy();
154 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
155 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
156 <            Policy.setPolicy(policy);
157 <        } catch (AccessControlException ok) {
158 <            return;
159 <        }
160 <
161 <        try {
162 <            ExecutorService e = new DirectExecutorService();
163 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
126 >        Runnable r = new CheckedRunnable() {
127 >            public void realRun() throws Exception {
128 >                ExecutorService e = new DirectExecutorService();
129 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
130                      public Object run() throws Exception {
131                          throw new IndexOutOfBoundsException();
132                      }}));
133  
134 <            future.get();
135 <            shouldThrow();
136 <        } catch (ExecutionException success) {
137 <            assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
138 <        }
139 <        finally {
140 <            Policy.setPolicy(savedPolicy);
141 <        }
134 >                try {
135 >                    future.get();
136 >                    shouldThrow();
137 >                } catch (ExecutionException success) {
138 >                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
139 >                }}};
140 >
141 >        runWithPermissions(r);
142      }
143  
144      /**
# Line 246 | Line 212 | public class AbstractExecutorServiceTest
212  
213  
214      /**
215 <     *  Blocking on submit(callable) throws InterruptedException if
250 <     *  caller interrupted.
215 >     * submit(callable).get() throws InterruptedException if interrupted
216       */
217      public void testInterruptedSubmit() throws InterruptedException {
218 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
219 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
220 <            public void realRun() throws Exception {
221 <                p.submit(new CheckedCallable<Object>() {
222 <                             public Object realCall()
223 <                                 throws InterruptedException {
224 <                                 Thread.sleep(SMALL_DELAY_MS);
225 <                                 return null;
226 <                             }}).get();
227 <            }});
228 <
229 <        t.start();
230 <        Thread.sleep(SHORT_DELAY_MS);
231 <        t.interrupt();
232 <        joinPool(p);
218 >        final CountDownLatch submitted    = new CountDownLatch(1);
219 >        final CountDownLatch quittingTime = new CountDownLatch(1);
220 >        final ExecutorService p
221 >            = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
222 >                                     new ArrayBlockingQueue<Runnable>(10));
223 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
224 >            public Void realCall() throws InterruptedException {
225 >                quittingTime.await();
226 >                return null;
227 >            }};
228 >        try {
229 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
230 >                public void realRun() throws Exception {
231 >                    Future<Void> future = p.submit(awaiter);
232 >                    submitted.countDown();
233 >                    future.get();
234 >                }});
235 >            t.start();
236 >            submitted.await();
237 >            t.interrupt();
238 >            t.join();
239 >        } finally {
240 >            quittingTime.countDown();
241 >            joinPool(p);
242 >        }
243      }
244  
245      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines