--- jsr166/src/test/tck/JSR166TestCase.java 2012/11/18 18:03:11 1.92 +++ jsr166/src/test/tck/JSR166TestCase.java 2013/02/01 19:07:36 1.97 @@ -11,8 +11,14 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadInfo; +import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.Arrays; import java.util.Date; +import java.util.Enumeration; +import java.util.List; import java.util.NoSuchElementException; import java.util.PropertyPermission; import java.util.concurrent.*; @@ -142,7 +148,9 @@ public class JSR166TestCase extends Test } /** - * Runs all JSR166 unit tests using junit.textui.TestRunner + * Runs all JSR166 unit tests using junit.textui.TestRunner. + * Optional command line arg provides the number of iterations to + * repeat running the tests. */ public static void main(String[] args) { if (useSecurityManager) { @@ -174,11 +182,37 @@ public class JSR166TestCase extends Test return suite; } + static void addTestReflectively(TestSuite suite, String testClassName) { + try { + Class klazz = Class.forName(testClassName); + Method m = klazz.getDeclaredMethod("suite", new Class[0]); + suite.addTest(newTestSuite((Test)m.invoke(null))); + } catch (Exception e) { + throw new Error(e); + } + } + + public static final double JAVA_CLASS_VERSION; + static { + try { + JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( + new java.security.PrivilegedAction() { + public Double run() { + return Double.valueOf(System.getProperty("java.class.version"));}}); + } catch (Throwable t) { + throw new Error(t); + } + } + + public static boolean isAtLeastJdk6() { return JAVA_CLASS_VERSION >= 50.0; } + public static boolean isAtLeastJdk7() { return JAVA_CLASS_VERSION >= 51.0; } + public static boolean isAtLeastJdk8() { return JAVA_CLASS_VERSION >= 52.0; } + /** * Collects all JSR166 unit tests as one suite. */ public static Test suite() { - return newTestSuite( + TestSuite suite = newTestSuite( ForkJoinPoolTest.suite(), ForkJoinTaskTest.suite(), RecursiveActionTest.suite(), @@ -243,6 +277,10 @@ public class JSR166TestCase extends Test TreeSetTest.suite(), TreeSubMapTest.suite(), TreeSubSetTest.suite()); + if (isAtLeastJdk8()) { + addTestReflectively(suite, "StampedLockTest"); + } + return suite; } @@ -507,6 +545,16 @@ public class JSR166TestCase extends Test } /** + * A debugging tool to print all stack traces, as jstack does. + */ + static void printAllStackTraces() { + for (ThreadInfo info : + ManagementFactory.getThreadMXBean() + .dumpAllThreads(true, true)) + System.err.print(info); + } + + /** * Checks that thread does not terminate within the default * millisecond delay of {@code timeoutMillis()}. */ @@ -623,11 +671,26 @@ public class JSR166TestCase extends Test SecurityManager sm = System.getSecurityManager(); if (sm == null) { r.run(); + } + runWithSecurityManagerWithPermissions(r, permissions); + } + + /** + * Runs Runnable r with a security policy that permits precisely + * the specified permissions. If there is no current security + * manager, a temporary one is set for the duration of the + * Runnable. We require that any security manager permit + * getPolicy/setPolicy. + */ + public void runWithSecurityManagerWithPermissions(Runnable r, + Permission... permissions) { + SecurityManager sm = System.getSecurityManager(); + if (sm == null) { Policy savedPolicy = Policy.getPolicy(); try { Policy.setPolicy(permissivePolicy()); System.setSecurityManager(new SecurityManager()); - runWithPermissions(r, permissions); + runWithSecurityManagerWithPermissions(r, permissions); } finally { System.setSecurityManager(null); Policy.setPolicy(savedPolicy); @@ -675,6 +738,12 @@ public class JSR166TestCase extends Test return perms.implies(p); } public void refresh() {} + public String toString() { + List ps = new ArrayList(); + for (Enumeration e = perms.elements(); e.hasMoreElements();) + ps.add(e.nextElement()); + return "AdjustablePolicy with permissions " + ps; + } } /**