--- jsr166/src/test/tck/ExecutorsTest.java 2003/09/26 15:33:13 1.6 +++ jsr166/src/test/tck/ExecutorsTest.java 2003/12/09 19:09:24 1.9 @@ -10,13 +10,14 @@ 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) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { - return new TestSuite(ExecutorsTest.class); + return new TestSuite(ExecutorsTest.class); } private static final String TEST_STRING = "a test string"; @@ -189,15 +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(); - TrackedRunnable task = new TrackedRunnable(); + TrackedShortRunnable task = new TrackedShortRunnable(); assertFalse(task.done); - Future future = Executors.execute(e, task, TEST_STRING); - String result = future.get(); + Future future = Executors.execute(e, task); + future.get(); assertTrue(task.done); - assertSame(TEST_STRING, result); } catch (ExecutionException ex) { unexpectedException(); @@ -210,10 +210,10 @@ public class ExecutorsTest extends JSR16 /** * invoke of a runnable runs it to completion */ - public void testInvokeRunnable () { + public void testInvokeRunnable() { try { Executor e = new DirectExecutor(); - TrackedRunnable task = new TrackedRunnable(); + TrackedShortRunnable task = new TrackedShortRunnable(); assertFalse(task.done); Executors.invoke(e, task); assertTrue(task.done); @@ -229,7 +229,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()); @@ -244,10 +244,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()); @@ -265,11 +355,11 @@ public class ExecutorsTest extends JSR16 /** * execute with null executor throws NPE */ - public void testNullExecuteRunnable () { + public void testNullExecuteRunnable() { try { - TrackedRunnable task = new TrackedRunnable(); + TrackedShortRunnable task = new TrackedShortRunnable(); assertFalse(task.done); - Future future = Executors.execute(null, task, TEST_STRING); + Future future = Executors.execute(null, task); shouldThrow(); } catch (NullPointerException success) { @@ -285,8 +375,8 @@ public class ExecutorsTest extends JSR16 public void testExecuteNullRunnable() { try { Executor e = new DirectExecutor(); - TrackedRunnable task = null; - Future future = Executors.execute(e, task, TEST_STRING); + TrackedShortRunnable task = null; + Future future = Executors.execute(e, task); shouldThrow(); } catch (NullPointerException success) { @@ -299,10 +389,10 @@ public class ExecutorsTest extends JSR16 /** * invoke of a null runnable throws NPE */ - public void testInvokeNullRunnable () { + public void testInvokeNullRunnable() { try { Executor e = new DirectExecutor(); - TrackedRunnable task = null; + TrackedShortRunnable task = null; Executors.invoke(e, task); shouldThrow(); } @@ -316,7 +406,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; @@ -333,7 +423,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; @@ -356,7 +446,7 @@ public class ExecutorsTest extends JSR16 try { for(int i = 0; i < 5; ++i){ - Executors.execute(p, new MediumRunnable(), Boolean.TRUE); + Executors.execute(p, new MediumRunnable()); } shouldThrow(); } catch(RejectedExecutionException success){} @@ -552,6 +642,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); + } + + } }