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.21 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.24 by jsr166, Wed Aug 25 00:07:02 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 345 | Line 311 | public class AbstractExecutorServiceTest
311       * invokeAny(c) throws NPE if c has null elements
312       */
313      public void testInvokeAny3() throws Exception {
348        final CountDownLatch latch = new CountDownLatch(1);
314          ExecutorService e = new DirectExecutorService();
315 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
316 +        l.add(new Callable<Integer>() {
317 +                  public Integer call() { return 5/0; }});
318 +        l.add(null);
319          try {
351            ArrayList<Callable<Integer>> l
352                = new ArrayList<Callable<Integer>>();
353            l.add(new Callable<Integer>() {
354                      public Integer call() { return 5/0; }});
355            l.add(null);
320              e.invokeAny(l);
321              shouldThrow();
322          } catch (NullPointerException success) {
323          } finally {
360            latch.countDown();
324              joinPool(e);
325          }
326      }
# Line 367 | Line 330 | public class AbstractExecutorServiceTest
330       */
331      public void testInvokeAny4() throws InterruptedException {
332          ExecutorService e = new DirectExecutorService();
333 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
334 +        l.add(new NPETask());
335          try {
371            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
372            l.add(new NPETask());
336              e.invokeAny(l);
337              shouldThrow();
338          } catch (ExecutionException success) {
# Line 385 | Line 348 | public class AbstractExecutorServiceTest
348      public void testInvokeAny5() throws Exception {
349          ExecutorService e = new DirectExecutorService();
350          try {
351 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
351 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
352              l.add(new StringTask());
353              l.add(new StringTask());
354              String result = e.invokeAny(l);
# Line 427 | Line 390 | public class AbstractExecutorServiceTest
390       */
391      public void testInvokeAll3() throws InterruptedException {
392          ExecutorService e = new DirectExecutorService();
393 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
394 +        l.add(new StringTask());
395 +        l.add(null);
396          try {
431            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
432            l.add(new StringTask());
433            l.add(null);
397              e.invokeAll(l);
398              shouldThrow();
399          } catch (NullPointerException success) {
# Line 445 | Line 408 | public class AbstractExecutorServiceTest
408      public void testInvokeAll4() throws Exception {
409          ExecutorService e = new DirectExecutorService();
410          try {
411 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
411 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
412              l.add(new NPETask());
413 <            List<Future<String>> result = e.invokeAll(l);
414 <            assertEquals(1, result.size());
415 <            for (Future<String> future : result) {
416 <                try {
417 <                    future.get();
418 <                    shouldThrow();
419 <                } catch (ExecutionException success) {
457 <                    Throwable cause = success.getCause();
458 <                    assertTrue(cause instanceof NullPointerException);
459 <                }
413 >            List<Future<String>> futures = e.invokeAll(l);
414 >            assertEquals(1, futures.size());
415 >            try {
416 >                futures.get(0).get();
417 >                shouldThrow();
418 >            } catch (ExecutionException success) {
419 >                assertTrue(success.getCause() instanceof NullPointerException);
420              }
421          } finally {
422              joinPool(e);
# Line 469 | Line 429 | public class AbstractExecutorServiceTest
429      public void testInvokeAll5() throws Exception {
430          ExecutorService e = new DirectExecutorService();
431          try {
432 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
432 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
433              l.add(new StringTask());
434              l.add(new StringTask());
435 <            List<Future<String>> result = e.invokeAll(l);
436 <            assertEquals(2, result.size());
437 <            for (Future<String> future : result)
435 >            List<Future<String>> futures = e.invokeAll(l);
436 >            assertEquals(2, futures.size());
437 >            for (Future<String> future : futures)
438                  assertSame(TEST_STRING, future.get());
439          } finally {
440              joinPool(e);
# Line 501 | Line 461 | public class AbstractExecutorServiceTest
461       */
462      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
463          ExecutorService e = new DirectExecutorService();
464 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
465 +        l.add(new StringTask());
466          try {
505            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
506            l.add(new StringTask());
467              e.invokeAny(l, MEDIUM_DELAY_MS, null);
468              shouldThrow();
469          } catch (NullPointerException success) {
# Line 530 | Line 490 | public class AbstractExecutorServiceTest
490       * timed invokeAny(c) throws NPE if c has null elements
491       */
492      public void testTimedInvokeAny3() throws Exception {
533        final CountDownLatch latch = new CountDownLatch(1);
493          ExecutorService e = new DirectExecutorService();
494 +        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
495 +        l.add(new Callable<Integer>() {
496 +                  public Integer call() { return 5/0; }});
497 +        l.add(null);
498          try {
536            ArrayList<Callable<Integer>> l
537                = new ArrayList<Callable<Integer>>();
538            l.add(new Callable<Integer>() {
539                      public Integer call() { return 5/0; }});
540            l.add(null);
499              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
500              shouldThrow();
501          } catch (NullPointerException success) {
502          } finally {
545            latch.countDown();
503              joinPool(e);
504          }
505      }
# Line 552 | Line 509 | public class AbstractExecutorServiceTest
509       */
510      public void testTimedInvokeAny4() throws Exception {
511          ExecutorService e = new DirectExecutorService();
512 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
513 +        l.add(new NPETask());
514          try {
556            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
557            l.add(new NPETask());
515              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
516              shouldThrow();
517          } catch (ExecutionException success) {
# Line 570 | Line 527 | public class AbstractExecutorServiceTest
527      public void testTimedInvokeAny5() throws Exception {
528          ExecutorService e = new DirectExecutorService();
529          try {
530 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
530 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
531              l.add(new StringTask());
532              l.add(new StringTask());
533              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 599 | Line 556 | public class AbstractExecutorServiceTest
556       */
557      public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
558          ExecutorService e = new DirectExecutorService();
559 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
560 +        l.add(new StringTask());
561          try {
603            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
604            l.add(new StringTask());
562              e.invokeAll(l, MEDIUM_DELAY_MS, null);
563              shouldThrow();
564          } catch (NullPointerException success) {
# Line 628 | Line 585 | public class AbstractExecutorServiceTest
585       */
586      public void testTimedInvokeAll3() throws InterruptedException {
587          ExecutorService e = new DirectExecutorService();
588 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
589 +        l.add(new StringTask());
590 +        l.add(null);
591          try {
632            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
633            l.add(new StringTask());
634            l.add(null);
592              e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
593              shouldThrow();
594          } catch (NullPointerException success) {
# Line 646 | Line 603 | public class AbstractExecutorServiceTest
603      public void testTimedInvokeAll4() throws Exception {
604          ExecutorService e = new DirectExecutorService();
605          try {
606 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
606 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
607              l.add(new NPETask());
608 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
609 <            assertEquals(1, result.size());
610 <            for (Future<String> future : result) {
611 <                try {
612 <                    future.get();
613 <                    shouldThrow();
614 <                } catch (ExecutionException success) {
615 <                    assertTrue(success.getCause() instanceof NullPointerException);
659 <                }
608 >            List<Future<String>> futures =
609 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
610 >            assertEquals(1, futures.size());
611 >            try {
612 >                futures.get(0).get();
613 >                shouldThrow();
614 >            } catch (ExecutionException success) {
615 >                assertTrue(success.getCause() instanceof NullPointerException);
616              }
617          } finally {
618              joinPool(e);
# Line 669 | Line 625 | public class AbstractExecutorServiceTest
625      public void testTimedInvokeAll5() throws Exception {
626          ExecutorService e = new DirectExecutorService();
627          try {
628 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
628 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
629              l.add(new StringTask());
630              l.add(new StringTask());
631 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
632 <            assertEquals(2, result.size());
633 <            for (Future<String> future : result)
631 >            List<Future<String>> futures =
632 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
633 >            assertEquals(2, futures.size());
634 >            for (Future<String> future : futures)
635                  assertSame(TEST_STRING, future.get());
636          } finally {
637              joinPool(e);
# Line 687 | Line 644 | public class AbstractExecutorServiceTest
644      public void testTimedInvokeAll6() throws InterruptedException {
645          ExecutorService e = new DirectExecutorService();
646          try {
647 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
647 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
648              l.add(new StringTask());
649              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
650              l.add(new StringTask());
651 <            List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS);
652 <            assertEquals(3, result.size());
653 <            Iterator<Future<String>> it = result.iterator();
651 >            List<Future<String>> futures =
652 >                e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS);
653 >            assertEquals(3, futures.size());
654 >            Iterator<Future<String>> it = futures.iterator();
655              Future<String> f1 = it.next();
656              Future<String> f2 = it.next();
657              Future<String> f3 = it.next();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines