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

Comparing jsr166/src/test/tck/ExecutorsTest.java (file contents):
Revision 1.21 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.25 by jsr166, Sat Nov 21 02:07:26 2009 UTC

# Line 13 | Line 13 | import java.util.concurrent.*;
13   import java.math.BigInteger;
14   import java.security.*;
15  
16 < public class ExecutorsTest extends JSR166TestCase{
16 > public class ExecutorsTest extends JSR166TestCase {
17      public static void main(String[] args) {
18          junit.textui.TestRunner.run (suite());
19      }
# Line 90 | Line 90 | public class ExecutorsTest extends JSR16
90          try {
91              ExecutorService e = Executors.newCachedThreadPool(null);
92              shouldThrow();
93 <        }
94 <        catch (NullPointerException success) {
95 <        }
93 >        } catch (NullPointerException success) {}
94      }
95  
96  
# Line 125 | Line 123 | public class ExecutorsTest extends JSR16
123          try {
124              ExecutorService e = Executors.newSingleThreadExecutor(null);
125              shouldThrow();
126 <        }
129 <        catch (NullPointerException success) {
130 <        }
126 >        } catch (NullPointerException success) {}
127      }
128  
129      /**
# Line 137 | Line 133 | public class ExecutorsTest extends JSR16
133          ExecutorService e = Executors.newSingleThreadExecutor();
134          try {
135              ThreadPoolExecutor tpe = (ThreadPoolExecutor)e;
136 +            shouldThrow();
137          } catch (ClassCastException success) {
138          } finally {
139              joinPool(e);
# Line 173 | Line 170 | public class ExecutorsTest extends JSR16
170          try {
171              ExecutorService e = Executors.newFixedThreadPool(2, null);
172              shouldThrow();
173 <        }
177 <        catch (NullPointerException success) {
178 <        }
173 >        } catch (NullPointerException success) {}
174      }
175  
176      /**
# Line 185 | Line 180 | public class ExecutorsTest extends JSR16
180          try {
181              ExecutorService e = Executors.newFixedThreadPool(0);
182              shouldThrow();
183 <        }
189 <        catch (IllegalArgumentException success) {
190 <        }
183 >        } catch (IllegalArgumentException success) {}
184      }
185  
186  
# Line 208 | Line 201 | public class ExecutorsTest extends JSR16
201      public void testunconfigurableExecutorServiceNPE() {
202          try {
203              ExecutorService e = Executors.unconfigurableExecutorService(null);
204 <        }
205 <        catch (NullPointerException success) {
213 <        }
204 >            shouldThrow();
205 >        } catch (NullPointerException success) {}
206      }
207  
208      /**
# Line 219 | Line 211 | public class ExecutorsTest extends JSR16
211      public void testunconfigurableScheduledExecutorServiceNPE() {
212          try {
213              ExecutorService e = Executors.unconfigurableScheduledExecutorService(null);
214 <        }
215 <        catch (NullPointerException success) {
224 <        }
214 >            shouldThrow();
215 >        } catch (NullPointerException success) {}
216      }
217  
218  
219      /**
220       * a newSingleThreadScheduledExecutor successfully runs delayed task
221       */
222 <    public void testNewSingleThreadScheduledExecutor() {
223 <        try {
224 <            TrackedCallable callable = new TrackedCallable();
225 <            ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor();
226 <            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
227 <            assertFalse(callable.done);
228 <            Thread.sleep(MEDIUM_DELAY_MS);
229 <            assertTrue(callable.done);
230 <            assertEquals(Boolean.TRUE, f.get());
240 <            joinPool(p1);
241 <        } catch (RejectedExecutionException e){}
242 <        catch (Exception e){
243 <            e.printStackTrace();
244 <            unexpectedException();
245 <        }
222 >    public void testNewSingleThreadScheduledExecutor() throws Exception {
223 >        TrackedCallable callable = new TrackedCallable();
224 >        ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor();
225 >        Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
226 >        assertFalse(callable.done);
227 >        Thread.sleep(MEDIUM_DELAY_MS);
228 >        assertTrue(callable.done);
229 >        assertEquals(Boolean.TRUE, f.get());
230 >        joinPool(p1);
231      }
232  
233      /**
234       * a newScheduledThreadPool successfully runs delayed task
235       */
236 <    public void testnewScheduledThreadPool() {
237 <        try {
238 <            TrackedCallable callable = new TrackedCallable();
239 <            ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2);
240 <            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
241 <            assertFalse(callable.done);
242 <            Thread.sleep(MEDIUM_DELAY_MS);
243 <            assertTrue(callable.done);
244 <            assertEquals(Boolean.TRUE, f.get());
260 <            joinPool(p1);
261 <        } catch (RejectedExecutionException e){}
262 <        catch (Exception e){
263 <            e.printStackTrace();
264 <            unexpectedException();
265 <        }
236 >    public void testnewScheduledThreadPool() throws Exception {
237 >        TrackedCallable callable = new TrackedCallable();
238 >        ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2);
239 >        Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
240 >        assertFalse(callable.done);
241 >        Thread.sleep(MEDIUM_DELAY_MS);
242 >        assertTrue(callable.done);
243 >        assertEquals(Boolean.TRUE, f.get());
244 >        joinPool(p1);
245      }
246  
247      /**
248 <     * an unconfigurable  newScheduledThreadPool successfully runs delayed task
249 <     */
250 <    public void testunconfigurableScheduledExecutorService() {
251 <        try {
252 <            TrackedCallable callable = new TrackedCallable();
253 <            ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2));
254 <            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
255 <            assertFalse(callable.done);
256 <            Thread.sleep(MEDIUM_DELAY_MS);
257 <            assertTrue(callable.done);
258 <            assertEquals(Boolean.TRUE, f.get());
280 <            joinPool(p1);
281 <        } catch (RejectedExecutionException e){}
282 <        catch (Exception e){
283 <            e.printStackTrace();
284 <            unexpectedException();
285 <        }
248 >     * an unconfigurable newScheduledThreadPool successfully runs delayed task
249 >     */
250 >    public void testunconfigurableScheduledExecutorService() throws Exception {
251 >        TrackedCallable callable = new TrackedCallable();
252 >        ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2));
253 >        Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
254 >        assertFalse(callable.done);
255 >        Thread.sleep(MEDIUM_DELAY_MS);
256 >        assertTrue(callable.done);
257 >        assertEquals(Boolean.TRUE, f.get());
258 >        joinPool(p1);
259      }
260  
261      /**
262       *  timeouts from execute will time out if they compute too long.
263       */
264 <    public void testTimedCallable() {
264 >    public void testTimedCallable() throws Exception {
265          int N = 10000;
266          ExecutorService executor = Executors.newSingleThreadExecutor();
267          List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N);
# Line 312 | Line 285 | public class ExecutorsTest extends JSR16
285                      assertTrue(iters > 0);
286                      return;
287                  }
315                catch (Exception e) {
316                    unexpectedException();
317                }
288              }
289              // if by chance we didn't ever time out, total time must be small
290              long elapsed = System.currentTimeMillis() - startTime;
# Line 330 | Line 300 | public class ExecutorsTest extends JSR16
300       * ThreadPoolExecutor using defaultThreadFactory has
301       * specified group, priority, daemon status, and name
302       */
303 <    public void testDefaultThreadFactory() {
303 >    public void testDefaultThreadFactory() throws Exception {
304          final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
305          Runnable r = new Runnable() {
306                  public void run() {
307 <                    try {
308 <                        Thread current = Thread.currentThread();
309 <                        threadAssertTrue(!current.isDaemon());
310 <                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
311 <                        ThreadGroup g = current.getThreadGroup();
312 <                        SecurityManager s = System.getSecurityManager();
313 <                        if (s != null)
314 <                            threadAssertTrue(g == s.getThreadGroup());
315 <                        else
316 <                            threadAssertTrue(g == egroup);
317 <                        String name = current.getName();
318 <                        threadAssertTrue(name.endsWith("thread-1"));
319 <                    } catch (SecurityException ok) {
320 <                        // Also pass if not allowed to change setting
321 <                    }
307 >                    try {
308 >                        Thread current = Thread.currentThread();
309 >                        threadAssertTrue(!current.isDaemon());
310 >                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
311 >                        ThreadGroup g = current.getThreadGroup();
312 >                        SecurityManager s = System.getSecurityManager();
313 >                        if (s != null)
314 >                            threadAssertTrue(g == s.getThreadGroup());
315 >                        else
316 >                            threadAssertTrue(g == egroup);
317 >                        String name = current.getName();
318 >                        threadAssertTrue(name.endsWith("thread-1"));
319 >                    } catch (SecurityException ok) {
320 >                        // Also pass if not allowed to change setting
321 >                    }
322                  }
323              };
324          ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
# Line 361 | Line 331 | public class ExecutorsTest extends JSR16
331  
332          try {
333              Thread.sleep(SHORT_DELAY_MS);
364        } catch (Exception eX) {
365            unexpectedException();
334          } finally {
335              joinPool(e);
336          }
# Line 373 | Line 341 | public class ExecutorsTest extends JSR16
341       * specified group, priority, daemon status, name,
342       * access control context and context class loader
343       */
344 <    public void testPrivilegedThreadFactory() {
344 >    public void testPrivilegedThreadFactory() throws Exception {
345          Policy savedPolicy = null;
346          try {
347              savedPolicy = Policy.getPolicy();
# Line 389 | Line 357 | public class ExecutorsTest extends JSR16
357          final AccessControlContext thisacc = AccessController.getContext();
358          Runnable r = new Runnable() {
359                  public void run() {
360 <                    try {
361 <                        Thread current = Thread.currentThread();
362 <                        threadAssertTrue(!current.isDaemon());
363 <                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
364 <                        ThreadGroup g = current.getThreadGroup();
365 <                        SecurityManager s = System.getSecurityManager();
366 <                        if (s != null)
367 <                            threadAssertTrue(g == s.getThreadGroup());
368 <                        else
369 <                            threadAssertTrue(g == egroup);
370 <                        String name = current.getName();
371 <                        threadAssertTrue(name.endsWith("thread-1"));
372 <                        threadAssertTrue(thisccl == current.getContextClassLoader());
373 <                        threadAssertTrue(thisacc.equals(AccessController.getContext()));
374 <                    } catch (SecurityException ok) {
375 <                        // Also pass if not allowed to change settings
376 <                    }
360 >                    try {
361 >                        Thread current = Thread.currentThread();
362 >                        threadAssertTrue(!current.isDaemon());
363 >                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
364 >                        ThreadGroup g = current.getThreadGroup();
365 >                        SecurityManager s = System.getSecurityManager();
366 >                        if (s != null)
367 >                            threadAssertTrue(g == s.getThreadGroup());
368 >                        else
369 >                            threadAssertTrue(g == egroup);
370 >                        String name = current.getName();
371 >                        threadAssertTrue(name.endsWith("thread-1"));
372 >                        threadAssertTrue(thisccl == current.getContextClassLoader());
373 >                        threadAssertTrue(thisacc.equals(AccessController.getContext()));
374 >                    } catch (SecurityException ok) {
375 >                        // Also pass if not allowed to change settings
376 >                    }
377                  }
378              };
379          ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
# Line 418 | Line 386 | public class ExecutorsTest extends JSR16
386          }
387          try {
388              Thread.sleep(SHORT_DELAY_MS);
421        } catch (Exception ex) {
422            unexpectedException();
389          } finally {
390              joinPool(e);
391          }
# Line 447 | Line 413 | public class ExecutorsTest extends JSR16
413       * privilegedCallableUsingCurrentClassLoader throws ACE
414       */
415      public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() {
416 <        Policy savedPolicy = null;
416 >        Policy savedPolicy = null;
417          try {
418              savedPolicy = Policy.getPolicy();
419              AdjustablePolicy policy = new AdjustablePolicy();
# Line 469 | Line 435 | public class ExecutorsTest extends JSR16
435              Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
436              shouldThrow();
437          } catch (AccessControlException success) {
438 <        } catch (Exception ex) {
473 <            unexpectedException();
474 <        }
475 <        finally {
438 >        } finally {
439              Policy.setPolicy(savedPolicy);
440          }
441      }
# Line 481 | Line 444 | public class ExecutorsTest extends JSR16
444       * With class loader permissions, calling
445       * privilegedCallableUsingCurrentClassLoader does not throw ACE
446       */
447 <    public void testprivilegedCallableUsingCCLWithPrivs() {
448 <        Policy savedPolicy = null;
447 >    public void testprivilegedCallableUsingCCLWithPrivs() throws Exception {
448 >        Policy savedPolicy = null;
449          try {
450              savedPolicy = Policy.getPolicy();
451              AdjustablePolicy policy = new AdjustablePolicy();
# Line 496 | Line 459 | public class ExecutorsTest extends JSR16
459          try {
460              Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
461              task.call();
499        } catch (Exception ex) {
500            unexpectedException();
462          }
463          finally {
464              Policy.setPolicy(savedPolicy);
# Line 507 | Line 468 | public class ExecutorsTest extends JSR16
468      /**
469       * Without permissions, calling privilegedCallable throws ACE
470       */
471 <    public void testprivilegedCallableWithNoPrivs() {
471 >    public void testprivilegedCallableWithNoPrivs() throws Exception {
472          Callable task;
473          Policy savedPolicy = null;
474          AdjustablePolicy policy = null;
# Line 538 | Line 499 | public class ExecutorsTest extends JSR16
499          try {
500              task.call();
501              shouldThrow();
502 <        } catch (AccessControlException success) {
542 <        } catch (Exception ex) {
543 <            unexpectedException();
544 <        }
502 >        } catch (AccessControlException success) {}
503      }
504  
505      /**
506       * With permissions, calling privilegedCallable succeeds
507       */
508 <    public void testprivilegedCallableWithPrivs() {
509 <        Policy savedPolicy = null;
508 >    public void testprivilegedCallableWithPrivs() throws Exception {
509 >        Policy savedPolicy = null;
510          try {
511              savedPolicy = Policy.getPolicy();
512              AdjustablePolicy policy = new AdjustablePolicy();
# Line 562 | Line 520 | public class ExecutorsTest extends JSR16
520          Callable task = Executors.privilegedCallable(new CheckCCL());
521          try {
522              task.call();
565        } catch (Exception ex) {
566            unexpectedException();
523          } finally {
524              Policy.setPolicy(savedPolicy);
525          }
# Line 572 | Line 528 | public class ExecutorsTest extends JSR16
528      /**
529       * callable(Runnable) returns null when called
530       */
531 <    public void testCallable1() {
532 <        try {
533 <            Callable c = Executors.callable(new NoOpRunnable());
578 <            assertNull(c.call());
579 <        } catch (Exception ex) {
580 <            unexpectedException();
581 <        }
582 <
531 >    public void testCallable1() throws Exception {
532 >        Callable c = Executors.callable(new NoOpRunnable());
533 >        assertNull(c.call());
534      }
535  
536      /**
537       * callable(Runnable, result) returns result when called
538       */
539 <    public void testCallable2() {
540 <        try {
541 <            Callable c = Executors.callable(new NoOpRunnable(), one);
591 <            assertEquals(one, c.call());
592 <        } catch (Exception ex) {
593 <            unexpectedException();
594 <        }
539 >    public void testCallable2() throws Exception {
540 >        Callable c = Executors.callable(new NoOpRunnable(), one);
541 >        assertEquals(one, c.call());
542      }
543  
544      /**
545       * callable(PrivilegedAction) returns its result when called
546       */
547 <    public void testCallable3() {
548 <        try {
549 <            Callable c = Executors.callable(new PrivilegedAction() {
603 <                    public Object run() { return one; }});
547 >    public void testCallable3() throws Exception {
548 >        Callable c = Executors.callable(new PrivilegedAction() {
549 >                public Object run() { return one; }});
550          assertEquals(one, c.call());
605        } catch (Exception ex) {
606            unexpectedException();
607        }
551      }
552  
553      /**
554       * callable(PrivilegedExceptionAction) returns its result when called
555       */
556 <    public void testCallable4() {
557 <        try {
558 <            Callable c = Executors.callable(new PrivilegedExceptionAction() {
559 <                    public Object run() { return one; }});
617 <            assertEquals(one, c.call());
618 <        } catch (Exception ex) {
619 <            unexpectedException();
620 <        }
556 >    public void testCallable4() throws Exception {
557 >        Callable c = Executors.callable(new PrivilegedExceptionAction() {
558 >                public Object run() { return one; }});
559 >        assertEquals(one, c.call());
560      }
561  
562  
# Line 626 | Line 565 | public class ExecutorsTest extends JSR16
565       */
566      public void testCallableNPE1() {
567          try {
568 <            Runnable r = null;
569 <            Callable c = Executors.callable(r);
570 <        } catch (NullPointerException success) {
632 <        }
568 >            Callable c = Executors.callable((Runnable) null);
569 >            shouldThrow();
570 >        } catch (NullPointerException success) {}
571      }
572  
573      /**
# Line 637 | Line 575 | public class ExecutorsTest extends JSR16
575       */
576      public void testCallableNPE2() {
577          try {
578 <            Runnable r = null;
579 <            Callable c = Executors.callable(r, one);
580 <        } catch (NullPointerException success) {
643 <        }
578 >            Callable c = Executors.callable((Runnable) null, one);
579 >            shouldThrow();
580 >        } catch (NullPointerException success) {}
581      }
582  
583      /**
# Line 648 | Line 585 | public class ExecutorsTest extends JSR16
585       */
586      public void testCallableNPE3() {
587          try {
588 <            PrivilegedAction r = null;
589 <            Callable c = Executors.callable(r);
590 <        } catch (NullPointerException success) {
654 <        }
588 >            Callable c = Executors.callable((PrivilegedAction) null);
589 >            shouldThrow();
590 >        } catch (NullPointerException success) {}
591      }
592  
593      /**
# Line 659 | Line 595 | public class ExecutorsTest extends JSR16
595       */
596      public void testCallableNPE4() {
597          try {
598 <            PrivilegedExceptionAction r = null;
599 <            Callable c = Executors.callable(r);
600 <        } catch (NullPointerException success) {
665 <        }
598 >            Callable c = Executors.callable((PrivilegedExceptionAction) null);
599 >            shouldThrow();
600 >        } catch (NullPointerException success) {}
601      }
602  
603  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines