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.127 by jsr166, Sat Jan 17 23:14:17 2015 UTC vs.
Revision 1.134 by jsr166, Sun Jun 14 20:58:14 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 50 | Line 52 | import java.util.regex.Pattern;
52   import junit.framework.AssertionFailedError;
53   import junit.framework.Test;
54   import junit.framework.TestCase;
55 + import junit.framework.TestResult;
56   import junit.framework.TestSuite;
57  
58   /**
# Line 160 | Line 163 | public class JSR166TestCase extends Test
163          Integer.getInteger("jsr166.runsPerTest", 1);
164  
165      /**
166 +     * The number of repetitions of the test suite (for finding leaks?).
167 +     */
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)"
177       * Usefully combined with jsr166.runsPerTest.
# Line 198 | Line 210 | public class JSR166TestCase extends Test
210  
211      /**
212       * Runs all JSR166 unit tests using junit.textui.TestRunner.
201     * Optional command line arg provides the number of iterations to
202     * repeat running the tests.
213       */
214      public static void main(String[] args) {
215 +        main(suite(), args);
216 +    }
217 +
218 +    /**
219 +     * Runs all unit tests in the given test suite.
220 +     * Actual behavior influenced by jsr166.* system properties.
221 +     */
222 +    static void main(Test suite, String[] args) {
223          if (useSecurityManager) {
224              System.err.println("Setting a permissive security manager");
225              Policy.setPolicy(permissivePolicy());
226              System.setSecurityManager(new SecurityManager());
227          }
228 <        int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]);
229 <
230 <        Test s = suite();
231 <        for (int i = 0; i < iters; ++i) {
214 <            junit.textui.TestRunner.run(s);
228 >        for (int i = 0; i < suiteRuns; i++) {
229 >            TestResult result = junit.textui.TestRunner.run(suite);
230 >            if (!result.wasSuccessful())
231 >                System.exit(1);
232              System.gc();
233              System.runFinalization();
234          }
218        System.exit(0);
235      }
236  
237      public static TestSuite newTestSuite(Object... suiteOrClasses) {
# Line 265 | 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() {
269 <        // As of 2014-05, java9 still uses 52.0 class file version
270 <        return JAVA_SPECIFICATION_VERSION.startsWith("1.9");
271 <    }
284 >    public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; }
285  
286      /**
287       * Collects all JSR166 unit tests as one suite.
# Line 372 | Line 385 | public class JSR166TestCase extends Test
385          return suite;
386      }
387  
388 +    /** Returns list of junit-style test method names in given class. */
389 +    public static ArrayList<String> testMethodNames(Class<?> testClass) {
390 +        Method[] methods = testClass.getDeclaredMethods();
391 +        ArrayList<String> names = new ArrayList<String>(methods.length);
392 +        for (Method method : methods) {
393 +            if (method.getName().startsWith("test")
394 +                && Modifier.isPublic(method.getModifiers())
395 +                // method.getParameterCount() requires jdk8+
396 +                && method.getParameterTypes().length == 0) {
397 +                names.add(method.getName());
398 +            }
399 +        }
400 +        return names;
401 +    }
402 +
403 +    /**
404 +     * Returns junit-style testSuite for the given test class, but
405 +     * parameterized by passing extra data to each test.
406 +     */
407 +    public static <ExtraData> Test parameterizedTestSuite
408 +        (Class<? extends JSR166TestCase> testClass,
409 +         Class<ExtraData> dataClass,
410 +         ExtraData data) {
411 +        try {
412 +            TestSuite suite = new TestSuite();
413 +            Constructor c =
414 +                testClass.getDeclaredConstructor(dataClass, String.class);
415 +            for (String methodName : testMethodNames(testClass))
416 +                suite.addTest((Test) c.newInstance(data, methodName));
417 +            return suite;
418 +        } catch (Exception e) {
419 +            throw new Error(e);
420 +        }
421 +    }
422 +
423 +    /**
424 +     * Returns junit-style testSuite for the jdk8 extension of the
425 +     * given test class, but parameterized by passing extra data to
426 +     * each test.  Uses reflection to allow compilation in jdk7.
427 +     */
428 +    public static <ExtraData> Test jdk8ParameterizedTestSuite
429 +        (Class<? extends JSR166TestCase> testClass,
430 +         Class<ExtraData> dataClass,
431 +         ExtraData data) {
432 +        if (atLeastJava8()) {
433 +            String name = testClass.getName();
434 +            String name8 = name.replaceAll("Test$", "8Test");
435 +            if (name.equals(name8)) throw new Error(name);
436 +            try {
437 +                return (Test)
438 +                    Class.forName(name8)
439 +                    .getMethod("testSuite", new Class[] { dataClass })
440 +                    .invoke(null, data);
441 +            } catch (Exception e) {
442 +                throw new Error(e);
443 +            }
444 +        } else {
445 +            return new TestSuite();
446 +        }
447 +
448 +    }
449 +
450      // Delays for timing-dependent tests, in milliseconds.
451  
452      public static long SHORT_DELAY_MS;
# Line 478 | Line 553 | public class JSR166TestCase extends Test
553                  // give thread some time to terminate
554                  thread.join(LONG_DELAY_MS);
555                  if (!thread.isAlive()) continue;
481                thread.stop();
556                  throw new AssertionFailedError
557                      (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
558                                     toString(), name));
# Line 564 | Line 638 | public class JSR166TestCase extends Test
638      public void threadAssertEquals(Object x, Object y) {
639          try {
640              assertEquals(x, y);
641 <        } catch (AssertionFailedError t) {
642 <            threadRecordFailure(t);
643 <            throw t;
644 <        } catch (Throwable t) {
645 <            threadUnexpectedException(t);
641 >        } catch (AssertionFailedError fail) {
642 >            threadRecordFailure(fail);
643 >            throw fail;
644 >        } catch (Throwable fail) {
645 >            threadUnexpectedException(fail);
646          }
647      }
648  
# Line 580 | Line 654 | public class JSR166TestCase extends Test
654      public void threadAssertSame(Object x, Object y) {
655          try {
656              assertSame(x, y);
657 <        } catch (AssertionFailedError t) {
658 <            threadRecordFailure(t);
659 <            throw t;
657 >        } catch (AssertionFailedError fail) {
658 >            threadRecordFailure(fail);
659 >            throw fail;
660          }
661      }
662  
# Line 652 | Line 726 | public class JSR166TestCase extends Test
726                       " did not terminate in a timely manner");
727          } catch (SecurityException ok) {
728              // Allowed in case test doesn't have privs
729 <        } catch (InterruptedException ie) {
729 >        } catch (InterruptedException fail) {
730              fail("Unexpected InterruptedException");
731          }
732      }
# Line 683 | Line 757 | public class JSR166TestCase extends Test
757              // No need to optimize the failing case via Thread.join.
758              delay(millis);
759              assertTrue(thread.isAlive());
760 <        } catch (InterruptedException ie) {
760 >        } catch (InterruptedException fail) {
761              fail("Unexpected InterruptedException");
762          }
763      }
# Line 705 | Line 779 | public class JSR166TestCase extends Test
779              delay(millis);
780              for (Thread thread : threads)
781                  assertTrue(thread.isAlive());
782 <        } catch (InterruptedException ie) {
782 >        } catch (InterruptedException fail) {
783              fail("Unexpected InterruptedException");
784          }
785      }
# Line 727 | Line 801 | public class JSR166TestCase extends Test
801              future.get(timeoutMillis, MILLISECONDS);
802              shouldThrow();
803          } catch (TimeoutException success) {
804 <        } catch (Exception e) {
805 <            threadUnexpectedException(e);
804 >        } catch (Exception fail) {
805 >            threadUnexpectedException(fail);
806          } finally { future.cancel(true); }
807          assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
808      }
# Line 884 | Line 958 | public class JSR166TestCase extends Test
958      void sleep(long millis) {
959          try {
960              delay(millis);
961 <        } catch (InterruptedException ie) {
961 >        } catch (InterruptedException fail) {
962              AssertionFailedError afe =
963                  new AssertionFailedError("Unexpected InterruptedException");
964 <            afe.initCause(ie);
964 >            afe.initCause(fail);
965              throw afe;
966          }
967      }
# Line 979 | Line 1053 | public class JSR166TestCase extends Test
1053      void awaitTermination(Thread t, long timeoutMillis) {
1054          try {
1055              t.join(timeoutMillis);
1056 <        } catch (InterruptedException ie) {
1057 <            threadUnexpectedException(ie);
1056 >        } catch (InterruptedException fail) {
1057 >            threadUnexpectedException(fail);
1058          } finally {
1059              if (t.getState() != Thread.State.TERMINATED) {
1060                  t.interrupt();
# Line 1006 | Line 1080 | public class JSR166TestCase extends Test
1080          public final void run() {
1081              try {
1082                  realRun();
1083 <            } catch (Throwable t) {
1084 <                threadUnexpectedException(t);
1083 >            } catch (Throwable fail) {
1084 >                threadUnexpectedException(fail);
1085              }
1086          }
1087      }
# Line 1061 | Line 1135 | public class JSR166TestCase extends Test
1135                  threadShouldThrow("InterruptedException");
1136              } catch (InterruptedException success) {
1137                  threadAssertFalse(Thread.interrupted());
1138 <            } catch (Throwable t) {
1139 <                threadUnexpectedException(t);
1138 >            } catch (Throwable fail) {
1139 >                threadUnexpectedException(fail);
1140              }
1141          }
1142      }
# Line 1073 | Line 1147 | public class JSR166TestCase extends Test
1147          public final T call() {
1148              try {
1149                  return realCall();
1150 <            } catch (Throwable t) {
1151 <                threadUnexpectedException(t);
1150 >            } catch (Throwable fail) {
1151 >                threadUnexpectedException(fail);
1152                  return null;
1153              }
1154          }
# Line 1091 | Line 1165 | public class JSR166TestCase extends Test
1165                  return result;
1166              } catch (InterruptedException success) {
1167                  threadAssertFalse(Thread.interrupted());
1168 <            } catch (Throwable t) {
1169 <                threadUnexpectedException(t);
1168 >            } catch (Throwable fail) {
1169 >                threadUnexpectedException(fail);
1170              }
1171              return null;
1172          }
# Line 1132 | Line 1206 | public class JSR166TestCase extends Test
1206      public void await(CountDownLatch latch) {
1207          try {
1208              assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1209 <        } catch (Throwable t) {
1210 <            threadUnexpectedException(t);
1209 >        } catch (Throwable fail) {
1210 >            threadUnexpectedException(fail);
1211          }
1212      }
1213  
1214      public void await(Semaphore semaphore) {
1215          try {
1216              assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1217 <        } catch (Throwable t) {
1218 <            threadUnexpectedException(t);
1217 >        } catch (Throwable fail) {
1218 >            threadUnexpectedException(fail);
1219          }
1220      }
1221  
# Line 1335 | Line 1409 | public class JSR166TestCase extends Test
1409          @Override protected final void compute() {
1410              try {
1411                  realCompute();
1412 <            } catch (Throwable t) {
1413 <                threadUnexpectedException(t);
1412 >            } catch (Throwable fail) {
1413 >                threadUnexpectedException(fail);
1414              }
1415          }
1416      }
# Line 1350 | Line 1424 | public class JSR166TestCase extends Test
1424          @Override protected final T compute() {
1425              try {
1426                  return realCompute();
1427 <            } catch (Throwable t) {
1428 <                threadUnexpectedException(t);
1427 >            } catch (Throwable fail) {
1428 >                threadUnexpectedException(fail);
1429                  return null;
1430              }
1431          }
# Line 1375 | Line 1449 | public class JSR166TestCase extends Test
1449          public int await() {
1450              try {
1451                  return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
1452 <            } catch (TimeoutException e) {
1452 >            } catch (TimeoutException timedOut) {
1453                  throw new AssertionFailedError("timed out");
1454 <            } catch (Exception e) {
1454 >            } catch (Exception fail) {
1455                  AssertionFailedError afe =
1456 <                    new AssertionFailedError("Unexpected exception: " + e);
1457 <                afe.initCause(e);
1456 >                    new AssertionFailedError("Unexpected exception: " + fail);
1457 >                afe.initCause(fail);
1458                  throw afe;
1459              }
1460          }
# Line 1408 | Line 1482 | public class JSR166TestCase extends Test
1482                  q.remove();
1483                  shouldThrow();
1484              } catch (NoSuchElementException success) {}
1485 <        } catch (InterruptedException ie) {
1412 <            threadUnexpectedException(ie);
1413 <        }
1485 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
1486      }
1487  
1488      void assertSerialEquals(Object x, Object y) {
# Line 1429 | Line 1501 | public class JSR166TestCase extends Test
1501              oos.flush();
1502              oos.close();
1503              return bos.toByteArray();
1504 <        } catch (Throwable t) {
1505 <            threadUnexpectedException(t);
1504 >        } catch (Throwable fail) {
1505 >            threadUnexpectedException(fail);
1506              return new byte[0];
1507          }
1508      }
# Line 1443 | Line 1515 | public class JSR166TestCase extends Test
1515              T clone = (T) ois.readObject();
1516              assertSame(o.getClass(), clone.getClass());
1517              return clone;
1518 <        } catch (Throwable t) {
1519 <            threadUnexpectedException(t);
1518 >        } catch (Throwable fail) {
1519 >            threadUnexpectedException(fail);
1520              return null;
1521          }
1522      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines