--- jsr166/src/test/tck/JSR166TestCase.java 2015/02/27 22:10:29 1.129 +++ jsr166/src/test/tck/JSR166TestCase.java 2015/06/14 20:58:14 1.134 @@ -15,7 +15,9 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; +import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.security.CodeSource; import java.security.Permission; import java.security.PermissionCollection; @@ -50,6 +52,7 @@ import java.util.regex.Pattern; import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestCase; +import junit.framework.TestResult; import junit.framework.TestSuite; /** @@ -160,6 +163,15 @@ public class JSR166TestCase extends Test Integer.getInteger("jsr166.runsPerTest", 1); /** + * The number of repetitions of the test suite (for finding leaks?). + */ + private static final int suiteRuns = + Integer.getInteger("jsr166.suiteRuns", 1); + + public JSR166TestCase() { super(); } + public JSR166TestCase(String name) { super(name); } + + /** * A filter for tests to run, matching strings of the form * methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" * Usefully combined with jsr166.runsPerTest. @@ -198,24 +210,28 @@ public class JSR166TestCase extends Test /** * 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) { + main(suite(), args); + } + + /** + * Runs all unit tests in the given test suite. + * Actual behavior influenced by jsr166.* system properties. + */ + static void main(Test suite, String[] args) { if (useSecurityManager) { System.err.println("Setting a permissive security manager"); Policy.setPolicy(permissivePolicy()); System.setSecurityManager(new SecurityManager()); } - int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); - - Test s = suite(); - for (int i = 0; i < iters; ++i) { - junit.textui.TestRunner.run(s); + for (int i = 0; i < suiteRuns; i++) { + TestResult result = junit.textui.TestRunner.run(suite); + if (!result.wasSuccessful()) + System.exit(1); System.gc(); System.runFinalization(); } - System.exit(0); } public static TestSuite newTestSuite(Object... suiteOrClasses) { @@ -265,10 +281,7 @@ public class JSR166TestCase extends Test public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } - public static boolean atLeastJava9() { - // As of 2014-05, java9 still uses 52.0 class file version - return JAVA_SPECIFICATION_VERSION.startsWith("1.9"); - } + public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } /** * Collects all JSR166 unit tests as one suite. @@ -372,6 +385,68 @@ public class JSR166TestCase extends Test return suite; } + /** Returns list of junit-style test method names in given class. */ + public static ArrayList testMethodNames(Class testClass) { + Method[] methods = testClass.getDeclaredMethods(); + ArrayList names = new ArrayList(methods.length); + for (Method method : methods) { + if (method.getName().startsWith("test") + && Modifier.isPublic(method.getModifiers()) + // method.getParameterCount() requires jdk8+ + && method.getParameterTypes().length == 0) { + names.add(method.getName()); + } + } + return names; + } + + /** + * Returns junit-style testSuite for the given test class, but + * parameterized by passing extra data to each test. + */ + public static Test parameterizedTestSuite + (Class testClass, + Class dataClass, + ExtraData data) { + try { + TestSuite suite = new TestSuite(); + Constructor c = + testClass.getDeclaredConstructor(dataClass, String.class); + for (String methodName : testMethodNames(testClass)) + suite.addTest((Test) c.newInstance(data, methodName)); + return suite; + } catch (Exception e) { + throw new Error(e); + } + } + + /** + * Returns junit-style testSuite for the jdk8 extension of the + * given test class, but parameterized by passing extra data to + * each test. Uses reflection to allow compilation in jdk7. + */ + public static Test jdk8ParameterizedTestSuite + (Class testClass, + Class dataClass, + ExtraData data) { + if (atLeastJava8()) { + String name = testClass.getName(); + String name8 = name.replaceAll("Test$", "8Test"); + if (name.equals(name8)) throw new Error(name); + try { + return (Test) + Class.forName(name8) + .getMethod("testSuite", new Class[] { dataClass }) + .invoke(null, data); + } catch (Exception e) { + throw new Error(e); + } + } else { + return new TestSuite(); + } + + } + // Delays for timing-dependent tests, in milliseconds. public static long SHORT_DELAY_MS; @@ -478,7 +553,6 @@ public class JSR166TestCase extends Test // give thread some time to terminate thread.join(LONG_DELAY_MS); if (!thread.isAlive()) continue; - thread.stop(); throw new AssertionFailedError (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", toString(), name));