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.7 by dl, Sun Oct 5 23:00:40 2003 UTC vs.
Revision 1.9 by tim, Tue Dec 9 19:09:24 2003 UTC

# Line 10 | Line 10 | import junit.framework.*;
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) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run (suite());  
18      }
19      public static Test suite() {
20 <        return new TestSuite(ExecutorsTest.class);
20 >        return new TestSuite(ExecutorsTest.class);
21      }
22  
23      private static final String TEST_STRING = "a test string";
# Line 194 | Line 195 | public class ExecutorsTest extends JSR16
195              Executor e = new DirectExecutor();
196              TrackedShortRunnable task = new TrackedShortRunnable();
197              assertFalse(task.done);
198 <            Future<String> future = Executors.execute(e, task, TEST_STRING);
199 <            String result = future.get();
198 >            Future<?> future = Executors.execute(e, task);
199 >            future.get();
200              assertTrue(task.done);
200            assertSame(TEST_STRING, result);
201          }
202          catch (ExecutionException ex) {
203              unexpectedException();
# Line 244 | Line 244 | public class ExecutorsTest extends JSR16
244          }
245      }
246  
247 +
248 +    /**
249 +     * execute of a privileged action runs it to completion
250 +     */
251 +    public void testExecutePrivilegedAction() {
252 +        Policy savedPolicy = Policy.getPolicy();
253 +        AdjustablePolicy policy = new AdjustablePolicy();
254 +        policy.addPermission(new RuntimePermission("getContextClassLoader"));
255 +        policy.addPermission(new RuntimePermission("setContextClassLoader"));
256 +        Policy.setPolicy(policy);
257 +        try {
258 +            Executor e = new DirectExecutor();
259 +            Future future = Executors.execute(e, new PrivilegedAction() {
260 +                    public Object run() {
261 +                        return TEST_STRING;
262 +                    }});
263 +
264 +            Object result = future.get();
265 +            assertSame(TEST_STRING, result);
266 +        }
267 +        catch (ExecutionException ex) {
268 +            unexpectedException();
269 +        }
270 +        catch (InterruptedException ex) {
271 +            unexpectedException();
272 +        }
273 +        finally {
274 +            Policy.setPolicy(savedPolicy);
275 +        }
276 +    }
277 +
278 +    /**
279 +     * execute of a privileged exception action runs it to completion
280 +     */
281 +    public void testExecutePrivilegedExceptionAction() {
282 +        Policy savedPolicy = Policy.getPolicy();
283 +        AdjustablePolicy policy = new AdjustablePolicy();
284 +        policy.addPermission(new RuntimePermission("getContextClassLoader"));
285 +        policy.addPermission(new RuntimePermission("setContextClassLoader"));
286 +        Policy.setPolicy(policy);
287 +        try {
288 +            Executor e = new DirectExecutor();
289 +            Future future = Executors.execute(e, new PrivilegedExceptionAction() {
290 +                    public Object run() {
291 +                        return TEST_STRING;
292 +                    }});
293 +
294 +            Object result = future.get();
295 +            assertSame(TEST_STRING, result);
296 +        }
297 +        catch (ExecutionException ex) {
298 +            unexpectedException();
299 +        }
300 +        catch (InterruptedException ex) {
301 +            unexpectedException();
302 +        }
303 +        finally {
304 +            Policy.setPolicy(savedPolicy);
305 +        }
306 +    }
307 +
308 +    /**
309 +     * execute of a failed privileged exception action reports exception
310 +     */
311 +    public void testExecuteFailedPrivilegedExceptionAction() {
312 +        Policy savedPolicy = Policy.getPolicy();
313 +        AdjustablePolicy policy = new AdjustablePolicy();
314 +        policy.addPermission(new RuntimePermission("getContextClassLoader"));
315 +        policy.addPermission(new RuntimePermission("setContextClassLoader"));
316 +        Policy.setPolicy(policy);
317 +        try {
318 +            Executor e = new DirectExecutor();
319 +            Future future = Executors.execute(e, new PrivilegedExceptionAction() {
320 +                    public Object run() throws Exception {
321 +                        throw new IndexOutOfBoundsException();
322 +                    }});
323 +
324 +            Object result = future.get();
325 +            shouldThrow();
326 +        }
327 +        catch (ExecutionException success) {
328 +        }
329 +        catch (InterruptedException ex) {
330 +            unexpectedException();
331 +        }
332 +        finally {
333 +            Policy.setPolicy(savedPolicy);
334 +        }
335 +    }
336 +
337      /**
338       * invoke of a collable runs it to completion
339       */
# Line 269 | Line 359 | public class ExecutorsTest extends JSR16
359          try {
360              TrackedShortRunnable task = new TrackedShortRunnable();
361              assertFalse(task.done);
362 <            Future<String> future = Executors.execute(null, task, TEST_STRING);
362 >            Future<?> future = Executors.execute(null, task);
363              shouldThrow();
364          }
365          catch (NullPointerException success) {
# Line 286 | Line 376 | public class ExecutorsTest extends JSR16
376          try {
377              Executor e = new DirectExecutor();
378              TrackedShortRunnable task = null;
379 <            Future<String> future = Executors.execute(e, task, TEST_STRING);
379 >            Future<?> future = Executors.execute(e, task);
380              shouldThrow();
381          }
382          catch (NullPointerException success) {
# Line 356 | Line 446 | public class ExecutorsTest extends JSR16
446          try {
447              
448              for(int i = 0; i < 5; ++i){
449 <                Executors.execute(p, new MediumRunnable(), Boolean.TRUE);
449 >                Executors.execute(p, new MediumRunnable());
450              }
451              shouldThrow();
452          } catch(RejectedExecutionException success){}
# Line 552 | Line 642 | public class ExecutorsTest extends JSR16
642      }
643  
644      
645 +    /**
646 +     * ThreadPoolExecutor using defaultThreadFactory has
647 +     * specified group, priority, daemon status, and name
648 +     */
649 +    public void testDefaultThreadFactory() {
650 +        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
651 +        Runnable r = new Runnable() {
652 +                public void run() {
653 +                    Thread current = Thread.currentThread();
654 +                    threadAssertTrue(!current.isDaemon());
655 +                    threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY);
656 +                    ThreadGroup g = current.getThreadGroup();
657 +                    SecurityManager s = System.getSecurityManager();
658 +                    if (s != null)
659 +                        threadAssertTrue(g == s.getThreadGroup());
660 +                    else
661 +                        threadAssertTrue(g == egroup);
662 +                    String name = current.getName();
663 +                    threadAssertTrue(name.endsWith("thread-1"));
664 +                }
665 +            };
666 +        ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
667 +        
668 +        e.execute(r);
669 +        e.shutdown();
670 +        try {
671 +            Thread.sleep(SHORT_DELAY_MS);
672 +        } catch (Exception eX) {
673 +            unexpectedException();
674 +        } finally {
675 +            joinPool(e);
676 +        }
677 +    }
678  
679 +    /**
680 +     * ThreadPoolExecutor using privilegedThreadFactory has
681 +     * specified group, priority, daemon status, name,
682 +     * access control context and context class loader
683 +     */
684 +    public void testPrivilegedThreadFactory() {
685 +        Policy savedPolicy = Policy.getPolicy();
686 +        AdjustablePolicy policy = new AdjustablePolicy();
687 +        policy.addPermission(new RuntimePermission("getContextClassLoader"));
688 +        policy.addPermission(new RuntimePermission("setContextClassLoader"));
689 +        Policy.setPolicy(policy);
690 +        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
691 +        final ClassLoader thisccl = Thread.currentThread().getContextClassLoader();
692 +        final AccessControlContext thisacc = AccessController.getContext();
693 +        Runnable r = new Runnable() {
694 +                public void run() {
695 +                    Thread current = Thread.currentThread();
696 +                    threadAssertTrue(!current.isDaemon());
697 +                    threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY);
698 +                    ThreadGroup g = current.getThreadGroup();
699 +                    SecurityManager s = System.getSecurityManager();
700 +                    if (s != null)
701 +                        threadAssertTrue(g == s.getThreadGroup());
702 +                    else
703 +                        threadAssertTrue(g == egroup);
704 +                    String name = current.getName();
705 +                    threadAssertTrue(name.endsWith("thread-1"));
706 +                    threadAssertTrue(thisccl == current.getContextClassLoader());
707 +                    threadAssertTrue(thisacc.equals(AccessController.getContext()));
708 +                }
709 +            };
710 +        ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
711 +        
712 +        Policy.setPolicy(savedPolicy);
713 +        e.execute(r);
714 +        e.shutdown();
715 +        try {
716 +            Thread.sleep(SHORT_DELAY_MS);
717 +        } catch (Exception ex) {
718 +            unexpectedException();
719 +        } finally {
720 +            joinPool(e);
721 +        }
722 +
723 +    }
724  
725   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines