ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
(Generate patch)

Comparing jsr166/src/test/tck/JSR166TestCase.java (file contents):
Revision 1.132 by jsr166, Mon Apr 27 06:01:31 2015 UTC vs.
Revision 1.136 by jsr166, Fri Sep 4 18:16:28 2015 UTC

# Line 15 | Line 15 | import java.io.ObjectInputStream;
15   import java.io.ObjectOutputStream;
16   import java.lang.management.ManagementFactory;
17   import java.lang.management.ThreadInfo;
18 + import java.lang.reflect.Constructor;
19   import java.lang.reflect.Method;
20 + import java.lang.reflect.Modifier;
21   import java.security.CodeSource;
22   import java.security.Permission;
23   import java.security.PermissionCollection;
# Line 166 | Line 168 | public class JSR166TestCase extends Test
168      private static final int suiteRuns =
169          Integer.getInteger("jsr166.suiteRuns", 1);
170  
171 +    public JSR166TestCase() { super(); }
172 +    public JSR166TestCase(String name) { super(name); }
173 +
174      /**
175       * A filter for tests to run, matching strings of the form
176       * methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)"
# Line 276 | Line 281 | public class JSR166TestCase extends Test
281      public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
282      public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
283      public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
284 <    public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; }
284 >    public static boolean atLeastJava9() {
285 >        // As of 2015-09, java9 still uses 52.0 class file version
286 >        return JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$");
287 >    }
288 >    // public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; }
289 >    public static boolean atLeastJava10() {
290 >        return JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$");
291 >    }
292  
293      /**
294       * Collects all JSR166 unit tests as one suite.
# Line 380 | Line 392 | public class JSR166TestCase extends Test
392          return suite;
393      }
394  
395 +    /** Returns list of junit-style test method names in given class. */
396 +    public static ArrayList<String> testMethodNames(Class<?> testClass) {
397 +        Method[] methods = testClass.getDeclaredMethods();
398 +        ArrayList<String> names = new ArrayList<String>(methods.length);
399 +        for (Method method : methods) {
400 +            if (method.getName().startsWith("test")
401 +                && Modifier.isPublic(method.getModifiers())
402 +                // method.getParameterCount() requires jdk8+
403 +                && method.getParameterTypes().length == 0) {
404 +                names.add(method.getName());
405 +            }
406 +        }
407 +        return names;
408 +    }
409 +
410 +    /**
411 +     * Returns junit-style testSuite for the given test class, but
412 +     * parameterized by passing extra data to each test.
413 +     */
414 +    public static <ExtraData> Test parameterizedTestSuite
415 +        (Class<? extends JSR166TestCase> testClass,
416 +         Class<ExtraData> dataClass,
417 +         ExtraData data) {
418 +        try {
419 +            TestSuite suite = new TestSuite();
420 +            Constructor c =
421 +                testClass.getDeclaredConstructor(dataClass, String.class);
422 +            for (String methodName : testMethodNames(testClass))
423 +                suite.addTest((Test) c.newInstance(data, methodName));
424 +            return suite;
425 +        } catch (Exception e) {
426 +            throw new Error(e);
427 +        }
428 +    }
429 +
430 +    /**
431 +     * Returns junit-style testSuite for the jdk8 extension of the
432 +     * given test class, but parameterized by passing extra data to
433 +     * each test.  Uses reflection to allow compilation in jdk7.
434 +     */
435 +    public static <ExtraData> Test jdk8ParameterizedTestSuite
436 +        (Class<? extends JSR166TestCase> testClass,
437 +         Class<ExtraData> dataClass,
438 +         ExtraData data) {
439 +        if (atLeastJava8()) {
440 +            String name = testClass.getName();
441 +            String name8 = name.replaceAll("Test$", "8Test");
442 +            if (name.equals(name8)) throw new Error(name);
443 +            try {
444 +                return (Test)
445 +                    Class.forName(name8)
446 +                    .getMethod("testSuite", new Class[] { dataClass })
447 +                    .invoke(null, data);
448 +            } catch (Exception e) {
449 +                throw new Error(e);
450 +            }
451 +        } else {
452 +            return new TestSuite();
453 +        }
454 +
455 +    }
456 +
457      // Delays for timing-dependent tests, in milliseconds.
458  
459      public static long SHORT_DELAY_MS;
# Line 414 | Line 488 | public class JSR166TestCase extends Test
488      }
489  
490      /**
491 <     * Returns a new Date instance representing a time delayMillis
492 <     * milliseconds in the future.
491 >     * Returns a new Date instance representing a time at least
492 >     * delayMillis milliseconds in the future.
493       */
494      Date delayedDate(long delayMillis) {
495 <        return new Date(System.currentTimeMillis() + delayMillis);
495 >        // Add 1 because currentTimeMillis is known to round into the past.
496 >        return new Date(System.currentTimeMillis() + delayMillis + 1);
497      }
498  
499      /**
# Line 486 | Line 561 | public class JSR166TestCase extends Test
561                  // give thread some time to terminate
562                  thread.join(LONG_DELAY_MS);
563                  if (!thread.isAlive()) continue;
489                thread.stop();
564                  throw new AssertionFailedError
565                      (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
566                                     toString(), name));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines