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.5 by dl, Thu Sep 25 11:02:41 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";
24  
24    private static class MyTask implements Runnable {
25        public void run() { completed = true; }
26        public boolean isCompleted() { return completed; }
27        public void reset() { completed = false; }
28        private boolean completed = false;
29    }
30
25      private static class StringTask implements Callable<String> {
26          public String call() { return TEST_STRING; }
27      }
# Line 79 | Line 73 | public class ExecutorsTest extends JSR16
73      };
74  
75      /**
82     * For use as ThreadFactory in constructors
83     */
84    static class MyThreadFactory implements ThreadFactory{
85        public Thread newThread(Runnable r){
86            return new Thread(r);
87        }  
88    }
89
90    /**
76       * A newCachedThreadPool can execute runnables
77       */
78      public void testNewCachedThreadPool1() {
# Line 102 | Line 87 | public class ExecutorsTest extends JSR16
87       * A newCachedThreadPool with given ThreadFactory can execute runnables
88       */
89      public void testNewCachedThreadPool2() {
90 <        ExecutorService e = Executors.newCachedThreadPool(new MyThreadFactory());
90 >        ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory());
91          e.execute(new NoOpRunnable());
92          e.execute(new NoOpRunnable());
93          e.execute(new NoOpRunnable());
# Line 137 | Line 122 | public class ExecutorsTest extends JSR16
122       * A new SingleThreadExecutor with given ThreadFactory can execute runnables
123       */
124      public void testNewSingleThreadExecutor2() {
125 <        ExecutorService e = Executors.newSingleThreadExecutor(new MyThreadFactory());
125 >        ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory());
126          e.execute(new NoOpRunnable());
127          e.execute(new NoOpRunnable());
128          e.execute(new NoOpRunnable());
# Line 171 | Line 156 | public class ExecutorsTest extends JSR16
156       * A new newFixedThreadPool with given ThreadFactory can execute runnables
157       */
158      public void testNewFixedThreadPool2() {
159 <        ExecutorService e = Executors.newFixedThreadPool(2, new MyThreadFactory());
159 >        ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory());
160          e.execute(new NoOpRunnable());
161          e.execute(new NoOpRunnable());
162          e.execute(new NoOpRunnable());
# Line 205 | Line 190 | public class ExecutorsTest extends JSR16
190      /**
191       * execute of runnable runs it to completion
192       */
193 <    public void testExecuteRunnable () {
193 >    public void testExecuteRunnable() {
194          try {
195              Executor e = new DirectExecutor();
196 <            MyTask task = new MyTask();
197 <            assertFalse(task.isCompleted());
198 <            Future<String> future = Executors.execute(e, task, TEST_STRING);
199 <            String result = future.get();
200 <            assertTrue(task.isCompleted());
216 <            assertSame(TEST_STRING, result);
196 >            TrackedShortRunnable task = new TrackedShortRunnable();
197 >            assertFalse(task.done);
198 >            Future<?> future = Executors.execute(e, task);
199 >            future.get();
200 >            assertTrue(task.done);
201          }
202          catch (ExecutionException ex) {
203              unexpectedException();
# Line 226 | Line 210 | public class ExecutorsTest extends JSR16
210      /**
211       * invoke of a runnable runs it to completion
212       */
213 <    public void testInvokeRunnable () {
213 >    public void testInvokeRunnable() {
214          try {
215              Executor e = new DirectExecutor();
216 <            MyTask task = new MyTask();
217 <            assertFalse(task.isCompleted());
216 >            TrackedShortRunnable task = new TrackedShortRunnable();
217 >            assertFalse(task.done);
218              Executors.invoke(e, task);
219 <            assertTrue(task.isCompleted());
219 >            assertTrue(task.done);
220          }
221          catch (ExecutionException ex) {
222              unexpectedException();
# Line 245 | Line 229 | public class ExecutorsTest extends JSR16
229      /**
230       * execute of a callable runs it to completion
231       */
232 <    public void testExecuteCallable () {
232 >    public void testExecuteCallable() {
233          try {
234              Executor e = new DirectExecutor();
235              Future<String> future = Executors.execute(e, new StringTask());
# Line 260 | 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       */
340 <    public void testInvokeCallable () {
340 >    public void testInvokeCallable() {
341          try {
342              Executor e = new DirectExecutor();
343              String result = Executors.invoke(e, new StringTask());
# Line 281 | Line 355 | public class ExecutorsTest extends JSR16
355      /**
356       * execute with null executor throws NPE
357       */
358 <    public void testNullExecuteRunnable () {
358 >    public void testNullExecuteRunnable() {
359          try {
360 <            MyTask task = new MyTask();
361 <            assertFalse(task.isCompleted());
362 <            Future<String> future = Executors.execute(null, task, TEST_STRING);
360 >            TrackedShortRunnable task = new TrackedShortRunnable();
361 >            assertFalse(task.done);
362 >            Future<?> future = Executors.execute(null, task);
363              shouldThrow();
364          }
365          catch (NullPointerException success) {
# Line 301 | Line 375 | public class ExecutorsTest extends JSR16
375      public void testExecuteNullRunnable() {
376          try {
377              Executor e = new DirectExecutor();
378 <            MyTask task = null;
379 <            Future<String> future = Executors.execute(e, task, TEST_STRING);
378 >            TrackedShortRunnable task = null;
379 >            Future<?> future = Executors.execute(e, task);
380              shouldThrow();
381          }
382          catch (NullPointerException success) {
# Line 315 | Line 389 | public class ExecutorsTest extends JSR16
389      /**
390       * invoke of a null runnable throws NPE
391       */
392 <    public void testInvokeNullRunnable () {
392 >    public void testInvokeNullRunnable() {
393          try {
394              Executor e = new DirectExecutor();
395 <            MyTask task = null;
395 >            TrackedShortRunnable task = null;
396              Executors.invoke(e, task);
397              shouldThrow();
398          }
# Line 332 | Line 406 | public class ExecutorsTest extends JSR16
406      /**
407       * execute of a null callable throws NPE
408       */
409 <    public void testExecuteNullCallable () {
409 >    public void testExecuteNullCallable() {
410          try {
411              Executor e = new DirectExecutor();
412              StringTask t = null;
# Line 349 | Line 423 | public class ExecutorsTest extends JSR16
423      /**
424       * invoke of a null callable throws NPE
425       */
426 <    public void testInvokeNullCallable () {
426 >    public void testInvokeNullCallable() {
427          try {
428              Executor e = new DirectExecutor();
429              StringTask t = null;
# Line 372 | 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 568 | 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