--- jsr166/src/test/tck/ExecutorsTest.java 2003/09/25 11:02:41 1.5 +++ jsr166/src/test/tck/ExecutorsTest.java 2003/11/01 18:37:02 1.8 @@ -10,6 +10,7 @@ import junit.framework.*; import java.util.*; import java.util.concurrent.*; import java.math.BigInteger; +import java.security.*; public class ExecutorsTest extends JSR166TestCase{ public static void main(String[] args) { @@ -21,13 +22,6 @@ public class ExecutorsTest extends JSR16 private static final String TEST_STRING = "a test string"; - private static class MyTask implements Runnable { - public void run() { completed = true; } - public boolean isCompleted() { return completed; } - public void reset() { completed = false; } - private boolean completed = false; - } - private static class StringTask implements Callable { public String call() { return TEST_STRING; } } @@ -79,15 +73,6 @@ public class ExecutorsTest extends JSR16 }; /** - * For use as ThreadFactory in constructors - */ - static class MyThreadFactory implements ThreadFactory{ - public Thread newThread(Runnable r){ - return new Thread(r); - } - } - - /** * A newCachedThreadPool can execute runnables */ public void testNewCachedThreadPool1() { @@ -102,7 +87,7 @@ public class ExecutorsTest extends JSR16 * A newCachedThreadPool with given ThreadFactory can execute runnables */ public void testNewCachedThreadPool2() { - ExecutorService e = Executors.newCachedThreadPool(new MyThreadFactory()); + ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); @@ -137,7 +122,7 @@ public class ExecutorsTest extends JSR16 * A new SingleThreadExecutor with given ThreadFactory can execute runnables */ public void testNewSingleThreadExecutor2() { - ExecutorService e = Executors.newSingleThreadExecutor(new MyThreadFactory()); + ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); @@ -171,7 +156,7 @@ public class ExecutorsTest extends JSR16 * A new newFixedThreadPool with given ThreadFactory can execute runnables */ public void testNewFixedThreadPool2() { - ExecutorService e = Executors.newFixedThreadPool(2, new MyThreadFactory()); + ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); e.execute(new NoOpRunnable()); @@ -205,14 +190,14 @@ public class ExecutorsTest extends JSR16 /** * execute of runnable runs it to completion */ - public void testExecuteRunnable () { + public void testExecuteRunnable() { try { Executor e = new DirectExecutor(); - MyTask task = new MyTask(); - assertFalse(task.isCompleted()); + TrackedShortRunnable task = new TrackedShortRunnable(); + assertFalse(task.done); Future future = Executors.execute(e, task, TEST_STRING); String result = future.get(); - assertTrue(task.isCompleted()); + assertTrue(task.done); assertSame(TEST_STRING, result); } catch (ExecutionException ex) { @@ -226,13 +211,13 @@ public class ExecutorsTest extends JSR16 /** * invoke of a runnable runs it to completion */ - public void testInvokeRunnable () { + public void testInvokeRunnable() { try { Executor e = new DirectExecutor(); - MyTask task = new MyTask(); - assertFalse(task.isCompleted()); + TrackedShortRunnable task = new TrackedShortRunnable(); + assertFalse(task.done); Executors.invoke(e, task); - assertTrue(task.isCompleted()); + assertTrue(task.done); } catch (ExecutionException ex) { unexpectedException(); @@ -245,7 +230,7 @@ public class ExecutorsTest extends JSR16 /** * execute of a callable runs it to completion */ - public void testExecuteCallable () { + public void testExecuteCallable() { try { Executor e = new DirectExecutor(); Future future = Executors.execute(e, new StringTask()); @@ -260,10 +245,100 @@ public class ExecutorsTest extends JSR16 } } + + /** + * execute of a privileged action runs it to completion + */ + public void testExecutePrivilegedAction() { + Policy savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + policy.addPermission(new RuntimePermission("getContextClassLoader")); + policy.addPermission(new RuntimePermission("setContextClassLoader")); + Policy.setPolicy(policy); + try { + Executor e = new DirectExecutor(); + Future future = Executors.execute(e, new PrivilegedAction() { + public Object run() { + return TEST_STRING; + }}); + + Object result = future.get(); + assertSame(TEST_STRING, result); + } + catch (ExecutionException ex) { + unexpectedException(); + } + catch (InterruptedException ex) { + unexpectedException(); + } + finally { + Policy.setPolicy(savedPolicy); + } + } + + /** + * execute of a privileged exception action runs it to completion + */ + public void testExecutePrivilegedExceptionAction() { + Policy savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + policy.addPermission(new RuntimePermission("getContextClassLoader")); + policy.addPermission(new RuntimePermission("setContextClassLoader")); + Policy.setPolicy(policy); + try { + Executor e = new DirectExecutor(); + Future future = Executors.execute(e, new PrivilegedExceptionAction() { + public Object run() { + return TEST_STRING; + }}); + + Object result = future.get(); + assertSame(TEST_STRING, result); + } + catch (ExecutionException ex) { + unexpectedException(); + } + catch (InterruptedException ex) { + unexpectedException(); + } + finally { + Policy.setPolicy(savedPolicy); + } + } + + /** + * execute of a failed privileged exception action reports exception + */ + public void testExecuteFailedPrivilegedExceptionAction() { + Policy savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + policy.addPermission(new RuntimePermission("getContextClassLoader")); + policy.addPermission(new RuntimePermission("setContextClassLoader")); + Policy.setPolicy(policy); + try { + Executor e = new DirectExecutor(); + Future future = Executors.execute(e, new PrivilegedExceptionAction() { + public Object run() throws Exception { + throw new IndexOutOfBoundsException(); + }}); + + Object result = future.get(); + shouldThrow(); + } + catch (ExecutionException success) { + } + catch (InterruptedException ex) { + unexpectedException(); + } + finally { + Policy.setPolicy(savedPolicy); + } + } + /** * invoke of a collable runs it to completion */ - public void testInvokeCallable () { + public void testInvokeCallable() { try { Executor e = new DirectExecutor(); String result = Executors.invoke(e, new StringTask()); @@ -281,10 +356,10 @@ public class ExecutorsTest extends JSR16 /** * execute with null executor throws NPE */ - public void testNullExecuteRunnable () { + public void testNullExecuteRunnable() { try { - MyTask task = new MyTask(); - assertFalse(task.isCompleted()); + TrackedShortRunnable task = new TrackedShortRunnable(); + assertFalse(task.done); Future future = Executors.execute(null, task, TEST_STRING); shouldThrow(); } @@ -301,7 +376,7 @@ public class ExecutorsTest extends JSR16 public void testExecuteNullRunnable() { try { Executor e = new DirectExecutor(); - MyTask task = null; + TrackedShortRunnable task = null; Future future = Executors.execute(e, task, TEST_STRING); shouldThrow(); } @@ -315,10 +390,10 @@ public class ExecutorsTest extends JSR16 /** * invoke of a null runnable throws NPE */ - public void testInvokeNullRunnable () { + public void testInvokeNullRunnable() { try { Executor e = new DirectExecutor(); - MyTask task = null; + TrackedShortRunnable task = null; Executors.invoke(e, task); shouldThrow(); } @@ -332,7 +407,7 @@ public class ExecutorsTest extends JSR16 /** * execute of a null callable throws NPE */ - public void testExecuteNullCallable () { + public void testExecuteNullCallable() { try { Executor e = new DirectExecutor(); StringTask t = null; @@ -349,7 +424,7 @@ public class ExecutorsTest extends JSR16 /** * invoke of a null callable throws NPE */ - public void testInvokeNullCallable () { + public void testInvokeNullCallable() { try { Executor e = new DirectExecutor(); StringTask t = null; @@ -568,6 +643,84 @@ public class ExecutorsTest extends JSR16 } + /** + * ThreadPoolExecutor using defaultThreadFactory has + * specified group, priority, daemon status, and name + */ + public void testDefaultThreadFactory() { + final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); + Runnable r = new Runnable() { + public void run() { + Thread current = Thread.currentThread(); + threadAssertTrue(!current.isDaemon()); + threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); + ThreadGroup g = current.getThreadGroup(); + SecurityManager s = System.getSecurityManager(); + if (s != null) + threadAssertTrue(g == s.getThreadGroup()); + else + threadAssertTrue(g == egroup); + String name = current.getName(); + threadAssertTrue(name.endsWith("thread-1")); + } + }; + ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); + + e.execute(r); + e.shutdown(); + try { + Thread.sleep(SHORT_DELAY_MS); + } catch (Exception eX) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * ThreadPoolExecutor using privilegedThreadFactory has + * specified group, priority, daemon status, name, + * access control context and context class loader + */ + public void testPrivilegedThreadFactory() { + Policy savedPolicy = Policy.getPolicy(); + AdjustablePolicy policy = new AdjustablePolicy(); + policy.addPermission(new RuntimePermission("getContextClassLoader")); + policy.addPermission(new RuntimePermission("setContextClassLoader")); + Policy.setPolicy(policy); + final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); + final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); + final AccessControlContext thisacc = AccessController.getContext(); + Runnable r = new Runnable() { + public void run() { + Thread current = Thread.currentThread(); + threadAssertTrue(!current.isDaemon()); + threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); + ThreadGroup g = current.getThreadGroup(); + SecurityManager s = System.getSecurityManager(); + if (s != null) + threadAssertTrue(g == s.getThreadGroup()); + else + threadAssertTrue(g == egroup); + String name = current.getName(); + threadAssertTrue(name.endsWith("thread-1")); + threadAssertTrue(thisccl == current.getContextClassLoader()); + threadAssertTrue(thisacc.equals(AccessController.getContext())); + } + }; + ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); + + Policy.setPolicy(savedPolicy); + e.execute(r); + e.shutdown(); + try { + Thread.sleep(SHORT_DELAY_MS); + } catch (Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } }