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.126 by jsr166, Sat Jan 17 22:55:06 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 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 266 | Line 282 | public class JSR166TestCase extends Test
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() {
285 <        // As of 2014-05, java9 still uses 52.0 class file version
286 <        return JAVA_SPECIFICATION_VERSION.startsWith("1.9");
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      /**
# Line 372 | 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 406 | 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 478 | 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;
481                thread.stop();
564                  throw new AssertionFailedError
565                      (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
566                                     toString(), name));
# Line 564 | Line 646 | public class JSR166TestCase extends Test
646      public void threadAssertEquals(Object x, Object y) {
647          try {
648              assertEquals(x, y);
649 <        } catch (AssertionFailedError t) {
650 <            threadRecordFailure(t);
651 <            throw t;
652 <        } catch (Throwable t) {
653 <            threadUnexpectedException(t);
649 >        } catch (AssertionFailedError fail) {
650 >            threadRecordFailure(fail);
651 >            throw fail;
652 >        } catch (Throwable fail) {
653 >            threadUnexpectedException(fail);
654          }
655      }
656  
# Line 580 | Line 662 | public class JSR166TestCase extends Test
662      public void threadAssertSame(Object x, Object y) {
663          try {
664              assertSame(x, y);
665 <        } catch (AssertionFailedError t) {
666 <            threadRecordFailure(t);
667 <            throw t;
665 >        } catch (AssertionFailedError fail) {
666 >            threadRecordFailure(fail);
667 >            throw fail;
668          }
669      }
670  
# Line 652 | Line 734 | public class JSR166TestCase extends Test
734                       " did not terminate in a timely manner");
735          } catch (SecurityException ok) {
736              // Allowed in case test doesn't have privs
737 <        } catch (InterruptedException ie) {
737 >        } catch (InterruptedException fail) {
738              fail("Unexpected InterruptedException");
739          }
740      }
# Line 683 | Line 765 | public class JSR166TestCase extends Test
765              // No need to optimize the failing case via Thread.join.
766              delay(millis);
767              assertTrue(thread.isAlive());
768 <        } catch (InterruptedException ie) {
768 >        } catch (InterruptedException fail) {
769              fail("Unexpected InterruptedException");
770          }
771      }
# Line 705 | Line 787 | public class JSR166TestCase extends Test
787              delay(millis);
788              for (Thread thread : threads)
789                  assertTrue(thread.isAlive());
790 <        } catch (InterruptedException ie) {
790 >        } catch (InterruptedException fail) {
791              fail("Unexpected InterruptedException");
792          }
793      }
# Line 727 | Line 809 | public class JSR166TestCase extends Test
809              future.get(timeoutMillis, MILLISECONDS);
810              shouldThrow();
811          } catch (TimeoutException success) {
812 <        } catch (Exception e) {
813 <            threadUnexpectedException(e);
812 >        } catch (Exception fail) {
813 >            threadUnexpectedException(fail);
814          } finally { future.cancel(true); }
815          assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
816      }
# Line 884 | Line 966 | public class JSR166TestCase extends Test
966      void sleep(long millis) {
967          try {
968              delay(millis);
969 <        } catch (InterruptedException ie) {
969 >        } catch (InterruptedException fail) {
970              AssertionFailedError afe =
971                  new AssertionFailedError("Unexpected InterruptedException");
972 <            afe.initCause(ie);
972 >            afe.initCause(fail);
973              throw afe;
974          }
975      }
# Line 979 | Line 1061 | public class JSR166TestCase extends Test
1061      void awaitTermination(Thread t, long timeoutMillis) {
1062          try {
1063              t.join(timeoutMillis);
1064 <        } catch (InterruptedException ie) {
1065 <            threadUnexpectedException(ie);
1064 >        } catch (InterruptedException fail) {
1065 >            threadUnexpectedException(fail);
1066          } finally {
1067              if (t.getState() != Thread.State.TERMINATED) {
1068                  t.interrupt();
# Line 1006 | Line 1088 | public class JSR166TestCase extends Test
1088          public final void run() {
1089              try {
1090                  realRun();
1091 <            } catch (Throwable t) {
1092 <                threadUnexpectedException(t);
1091 >            } catch (Throwable fail) {
1092 >                threadUnexpectedException(fail);
1093              }
1094          }
1095      }
# Line 1061 | Line 1143 | public class JSR166TestCase extends Test
1143                  threadShouldThrow("InterruptedException");
1144              } catch (InterruptedException success) {
1145                  threadAssertFalse(Thread.interrupted());
1146 <            } catch (Throwable t) {
1147 <                threadUnexpectedException(t);
1146 >            } catch (Throwable fail) {
1147 >                threadUnexpectedException(fail);
1148              }
1149          }
1150      }
# Line 1073 | Line 1155 | public class JSR166TestCase extends Test
1155          public final T call() {
1156              try {
1157                  return realCall();
1158 <            } catch (Throwable t) {
1159 <                threadUnexpectedException(t);
1158 >            } catch (Throwable fail) {
1159 >                threadUnexpectedException(fail);
1160                  return null;
1161              }
1162          }
# Line 1091 | Line 1173 | public class JSR166TestCase extends Test
1173                  return result;
1174              } catch (InterruptedException success) {
1175                  threadAssertFalse(Thread.interrupted());
1176 <            } catch (Throwable t) {
1177 <                threadUnexpectedException(t);
1176 >            } catch (Throwable fail) {
1177 >                threadUnexpectedException(fail);
1178              }
1179              return null;
1180          }
# Line 1132 | Line 1214 | public class JSR166TestCase extends Test
1214      public void await(CountDownLatch latch) {
1215          try {
1216              assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1217 <        } catch (Throwable t) {
1218 <            threadUnexpectedException(t);
1217 >        } catch (Throwable fail) {
1218 >            threadUnexpectedException(fail);
1219          }
1220      }
1221  
1222      public void await(Semaphore semaphore) {
1223          try {
1224              assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1225 <        } catch (Throwable t) {
1226 <            threadUnexpectedException(t);
1225 >        } catch (Throwable fail) {
1226 >            threadUnexpectedException(fail);
1227          }
1228      }
1229  
# Line 1335 | Line 1417 | public class JSR166TestCase extends Test
1417          @Override protected final void compute() {
1418              try {
1419                  realCompute();
1420 <            } catch (Throwable t) {
1421 <                threadUnexpectedException(t);
1420 >            } catch (Throwable fail) {
1421 >                threadUnexpectedException(fail);
1422              }
1423          }
1424      }
# Line 1350 | Line 1432 | public class JSR166TestCase extends Test
1432          @Override protected final T compute() {
1433              try {
1434                  return realCompute();
1435 <            } catch (Throwable t) {
1436 <                threadUnexpectedException(t);
1435 >            } catch (Throwable fail) {
1436 >                threadUnexpectedException(fail);
1437                  return null;
1438              }
1439          }
# Line 1375 | Line 1457 | public class JSR166TestCase extends Test
1457          public int await() {
1458              try {
1459                  return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
1460 <            } catch (TimeoutException e) {
1460 >            } catch (TimeoutException timedOut) {
1461                  throw new AssertionFailedError("timed out");
1462 <            } catch (Exception e) {
1462 >            } catch (Exception fail) {
1463                  AssertionFailedError afe =
1464 <                    new AssertionFailedError("Unexpected exception: " + e);
1465 <                afe.initCause(e);
1464 >                    new AssertionFailedError("Unexpected exception: " + fail);
1465 >                afe.initCause(fail);
1466                  throw afe;
1467              }
1468          }
# Line 1408 | Line 1490 | public class JSR166TestCase extends Test
1490                  q.remove();
1491                  shouldThrow();
1492              } catch (NoSuchElementException success) {}
1493 <        } catch (InterruptedException ie) {
1412 <            threadUnexpectedException(ie);
1413 <        }
1493 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
1494      }
1495  
1496      void assertSerialEquals(Object x, Object y) {
# Line 1429 | Line 1509 | public class JSR166TestCase extends Test
1509              oos.flush();
1510              oos.close();
1511              return bos.toByteArray();
1512 <        } catch (Throwable t) {
1513 <            threadUnexpectedException(t);
1512 >        } catch (Throwable fail) {
1513 >            threadUnexpectedException(fail);
1514              return new byte[0];
1515          }
1516      }
# Line 1443 | Line 1523 | public class JSR166TestCase extends Test
1523              T clone = (T) ois.readObject();
1524              assertSame(o.getClass(), clone.getClass());
1525              return clone;
1526 <        } catch (Throwable t) {
1527 <            threadUnexpectedException(t);
1526 >        } catch (Throwable fail) {
1527 >            threadUnexpectedException(fail);
1528              return null;
1529          }
1530      }
# Line 1476 | Line 1556 | public class JSR166TestCase extends Test
1556              shouldThrow();
1557          } catch (NoSuchElementException success) {}
1558          assertFalse(it.hasNext());
1559 <    }        
1559 >    }
1560   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines