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.234 by jsr166, Mon Jul 17 22:27:31 2017 UTC vs.
Revision 1.247 by jsr166, Sun Jul 22 21:37:31 2018 UTC

# Line 66 | Line 66 | import java.util.Arrays;
66   import java.util.Collection;
67   import java.util.Collections;
68   import java.util.Date;
69 + import java.util.Deque;
70   import java.util.Enumeration;
71 + import java.util.HashSet;
72   import java.util.Iterator;
73   import java.util.List;
74   import java.util.NoSuchElementException;
75   import java.util.PropertyPermission;
76 + import java.util.Set;
77   import java.util.concurrent.BlockingQueue;
78   import java.util.concurrent.Callable;
79   import java.util.concurrent.CountDownLatch;
# Line 99 | Line 102 | import java.util.concurrent.atomic.Atomi
102   import java.util.concurrent.atomic.AtomicReference;
103   import java.util.regex.Pattern;
104  
102 import junit.framework.AssertionFailedError;
105   import junit.framework.Test;
106   import junit.framework.TestCase;
107   import junit.framework.TestResult;
# Line 419 | Line 421 | public class JSR166TestCase extends Test
421          for (String testClassName : testClassNames) {
422              try {
423                  Class<?> testClass = Class.forName(testClassName);
424 <                Method m = testClass.getDeclaredMethod("suite",
423 <                                                       new Class<?>[0]);
424 >                Method m = testClass.getDeclaredMethod("suite");
425                  suite.addTest(newTestSuite((Test)m.invoke(null)));
426 <            } catch (Exception e) {
427 <                throw new Error("Missing test class", e);
426 >            } catch (ReflectiveOperationException e) {
427 >                throw new AssertionError("Missing test class", e);
428              }
429          }
430      }
# Line 445 | Line 446 | public class JSR166TestCase extends Test
446          }
447      }
448  
449 <    public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
450 <    public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
451 <    public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
452 <    public static boolean atLeastJava9() {
453 <        return JAVA_CLASS_VERSION >= 53.0
454 <            // As of 2015-09, java9 still uses 52.0 class file version
454 <            || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?(9|[0-9][0-9])$");
455 <    }
456 <    public static boolean atLeastJava10() {
457 <        return JAVA_CLASS_VERSION >= 54.0
458 <            || JAVA_SPECIFICATION_VERSION.matches("^(1\\.)?[0-9][0-9]$");
459 <    }
449 >    public static boolean atLeastJava6()  { return JAVA_CLASS_VERSION >= 50.0; }
450 >    public static boolean atLeastJava7()  { return JAVA_CLASS_VERSION >= 51.0; }
451 >    public static boolean atLeastJava8()  { return JAVA_CLASS_VERSION >= 52.0; }
452 >    public static boolean atLeastJava9()  { return JAVA_CLASS_VERSION >= 53.0; }
453 >    public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; }
454 >    public static boolean atLeastJava11() { return JAVA_CLASS_VERSION >= 55.0; }
455  
456      /**
457       * Collects all JSR166 unit tests as one suite.
# Line 544 | Line 539 | public class JSR166TestCase extends Test
539                  "DoubleAdderTest",
540                  "ForkJoinPool8Test",
541                  "ForkJoinTask8Test",
542 +                "HashMapTest",
543                  "LinkedBlockingDeque8Test",
544                  "LinkedBlockingQueue8Test",
545                  "LongAccumulatorTest",
# Line 606 | Line 602 | public class JSR166TestCase extends Test
602              for (String methodName : testMethodNames(testClass))
603                  suite.addTest((Test) c.newInstance(data, methodName));
604              return suite;
605 <        } catch (Exception e) {
606 <            throw new Error(e);
605 >        } catch (ReflectiveOperationException e) {
606 >            throw new AssertionError(e);
607          }
608      }
609  
# Line 623 | Line 619 | public class JSR166TestCase extends Test
619          if (atLeastJava8()) {
620              String name = testClass.getName();
621              String name8 = name.replaceAll("Test$", "8Test");
622 <            if (name.equals(name8)) throw new Error(name);
622 >            if (name.equals(name8)) throw new AssertionError(name);
623              try {
624                  return (Test)
625                      Class.forName(name8)
626 <                    .getMethod("testSuite", new Class[] { dataClass })
626 >                    .getMethod("testSuite", dataClass)
627                      .invoke(null, data);
628 <            } catch (Exception e) {
629 <                throw new Error(e);
628 >            } catch (ReflectiveOperationException e) {
629 >                throw new AssertionError(e);
630              }
631          } else {
632              return new TestSuite();
# Line 739 | Line 735 | public class JSR166TestCase extends Test
735          String msg = toString() + ": " + String.format(format, args);
736          System.err.println(msg);
737          dumpTestThreads();
738 <        throw new AssertionFailedError(msg);
738 >        throw new AssertionError(msg);
739      }
740  
741      /**
# Line 760 | Line 756 | public class JSR166TestCase extends Test
756                  throw (RuntimeException) t;
757              else if (t instanceof Exception)
758                  throw (Exception) t;
759 <            else {
760 <                AssertionFailedError afe =
765 <                    new AssertionFailedError(t.toString());
766 <                afe.initCause(t);
767 <                throw afe;
768 <            }
759 >            else
760 >                throw new AssertionError(t.toString(), t);
761          }
762  
763          if (Thread.interrupted())
# Line 799 | Line 791 | public class JSR166TestCase extends Test
791  
792      /**
793       * Just like fail(reason), but additionally recording (using
794 <     * threadRecordFailure) any AssertionFailedError thrown, so that
795 <     * the current testcase will fail.
794 >     * threadRecordFailure) any AssertionError thrown, so that the
795 >     * current testcase will fail.
796       */
797      public void threadFail(String reason) {
798          try {
799              fail(reason);
800 <        } catch (AssertionFailedError t) {
801 <            threadRecordFailure(t);
802 <            throw t;
800 >        } catch (AssertionError fail) {
801 >            threadRecordFailure(fail);
802 >            throw fail;
803          }
804      }
805  
806      /**
807       * Just like assertTrue(b), but additionally recording (using
808 <     * threadRecordFailure) any AssertionFailedError thrown, so that
809 <     * the current testcase will fail.
808 >     * threadRecordFailure) any AssertionError thrown, so that the
809 >     * current testcase will fail.
810       */
811      public void threadAssertTrue(boolean b) {
812          try {
813              assertTrue(b);
814 <        } catch (AssertionFailedError t) {
815 <            threadRecordFailure(t);
816 <            throw t;
814 >        } catch (AssertionError fail) {
815 >            threadRecordFailure(fail);
816 >            throw fail;
817          }
818      }
819  
820      /**
821       * Just like assertFalse(b), but additionally recording (using
822 <     * threadRecordFailure) any AssertionFailedError thrown, so that
823 <     * the current testcase will fail.
822 >     * threadRecordFailure) any AssertionError thrown, so that the
823 >     * current testcase will fail.
824       */
825      public void threadAssertFalse(boolean b) {
826          try {
827              assertFalse(b);
828 <        } catch (AssertionFailedError t) {
829 <            threadRecordFailure(t);
830 <            throw t;
828 >        } catch (AssertionError fail) {
829 >            threadRecordFailure(fail);
830 >            throw fail;
831          }
832      }
833  
834      /**
835       * Just like assertNull(x), but additionally recording (using
836 <     * threadRecordFailure) any AssertionFailedError thrown, so that
837 <     * the current testcase will fail.
836 >     * threadRecordFailure) any AssertionError thrown, so that the
837 >     * current testcase will fail.
838       */
839      public void threadAssertNull(Object x) {
840          try {
841              assertNull(x);
842 <        } catch (AssertionFailedError t) {
843 <            threadRecordFailure(t);
844 <            throw t;
842 >        } catch (AssertionError fail) {
843 >            threadRecordFailure(fail);
844 >            throw fail;
845          }
846      }
847  
848      /**
849       * Just like assertEquals(x, y), but additionally recording (using
850 <     * threadRecordFailure) any AssertionFailedError thrown, so that
851 <     * the current testcase will fail.
850 >     * threadRecordFailure) any AssertionError thrown, so that the
851 >     * current testcase will fail.
852       */
853      public void threadAssertEquals(long x, long y) {
854          try {
855              assertEquals(x, y);
856 <        } catch (AssertionFailedError t) {
857 <            threadRecordFailure(t);
858 <            throw t;
856 >        } catch (AssertionError fail) {
857 >            threadRecordFailure(fail);
858 >            throw fail;
859          }
860      }
861  
862      /**
863       * Just like assertEquals(x, y), but additionally recording (using
864 <     * threadRecordFailure) any AssertionFailedError thrown, so that
865 <     * the current testcase will fail.
864 >     * threadRecordFailure) any AssertionError thrown, so that the
865 >     * current testcase will fail.
866       */
867      public void threadAssertEquals(Object x, Object y) {
868          try {
869              assertEquals(x, y);
870 <        } catch (AssertionFailedError fail) {
870 >        } catch (AssertionError fail) {
871              threadRecordFailure(fail);
872              throw fail;
873          } catch (Throwable fail) {
# Line 885 | Line 877 | public class JSR166TestCase extends Test
877  
878      /**
879       * Just like assertSame(x, y), but additionally recording (using
880 <     * threadRecordFailure) any AssertionFailedError thrown, so that
881 <     * the current testcase will fail.
880 >     * threadRecordFailure) any AssertionError thrown, so that the
881 >     * current testcase will fail.
882       */
883      public void threadAssertSame(Object x, Object y) {
884          try {
885              assertSame(x, y);
886 <        } catch (AssertionFailedError fail) {
886 >        } catch (AssertionError fail) {
887              threadRecordFailure(fail);
888              throw fail;
889          }
# Line 913 | Line 905 | public class JSR166TestCase extends Test
905  
906      /**
907       * Records the given exception using {@link #threadRecordFailure},
908 <     * then rethrows the exception, wrapping it in an
909 <     * AssertionFailedError if necessary.
908 >     * then rethrows the exception, wrapping it in an AssertionError
909 >     * if necessary.
910       */
911      public void threadUnexpectedException(Throwable t) {
912          threadRecordFailure(t);
# Line 923 | Line 915 | public class JSR166TestCase extends Test
915              throw (RuntimeException) t;
916          else if (t instanceof Error)
917              throw (Error) t;
918 <        else {
919 <            AssertionFailedError afe =
928 <                new AssertionFailedError("unexpected exception: " + t);
929 <            afe.initCause(t);
930 <            throw afe;
931 <        }
918 >        else
919 >            throw new AssertionError("unexpected exception: " + t, t);
920      }
921  
922      /**
# Line 1104 | Line 1092 | public class JSR166TestCase extends Test
1092          for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) {
1093              try { delay(1); }
1094              catch (InterruptedException fail) {
1095 <                fail("Unexpected InterruptedException");
1095 >                throw new AssertionError("Unexpected InterruptedException", fail);
1096              }
1097              Thread.State s = thread.getState();
1098              if (s == expected)
# Line 1116 | Line 1104 | public class JSR166TestCase extends Test
1104      }
1105  
1106      /**
1119     * Checks that thread does not terminate within the default
1120     * millisecond delay of {@code timeoutMillis()}.
1121     * TODO: REMOVEME
1122     */
1123    void assertThreadStaysAlive(Thread thread) {
1124        assertThreadStaysAlive(thread, timeoutMillis());
1125    }
1126
1127    /**
1128     * Checks that thread does not terminate within the given millisecond delay.
1129     * TODO: REMOVEME
1130     */
1131    void assertThreadStaysAlive(Thread thread, long millis) {
1132        try {
1133            // No need to optimize the failing case via Thread.join.
1134            delay(millis);
1135            assertTrue(thread.isAlive());
1136        } catch (InterruptedException fail) {
1137            threadFail("Unexpected InterruptedException");
1138        }
1139    }
1140
1141    /**
1142     * Checks that the threads do not terminate within the default
1143     * millisecond delay of {@code timeoutMillis()}.
1144     * TODO: REMOVEME
1145     */
1146    void assertThreadsStayAlive(Thread... threads) {
1147        assertThreadsStayAlive(timeoutMillis(), threads);
1148    }
1149
1150    /**
1151     * Checks that the threads do not terminate within the given millisecond delay.
1152     * TODO: REMOVEME
1153     */
1154    void assertThreadsStayAlive(long millis, Thread... threads) {
1155        try {
1156            // No need to optimize the failing case via Thread.join.
1157            delay(millis);
1158            for (Thread thread : threads)
1159                assertTrue(thread.isAlive());
1160        } catch (InterruptedException fail) {
1161            threadFail("Unexpected InterruptedException");
1162        }
1163    }
1164
1165    /**
1107       * Checks that future.get times out, with the default timeout of
1108       * {@code timeoutMillis()}.
1109       */
# Line 1337 | Line 1278 | public class JSR166TestCase extends Test
1278  
1279      /**
1280       * Sleeps until the given time has elapsed.
1281 <     * Throws AssertionFailedError if interrupted.
1281 >     * Throws AssertionError if interrupted.
1282       */
1283      static void sleep(long millis) {
1284          try {
1285              delay(millis);
1286          } catch (InterruptedException fail) {
1287 <            AssertionFailedError afe =
1347 <                new AssertionFailedError("Unexpected InterruptedException");
1348 <            afe.initCause(fail);
1349 <            throw afe;
1287 >            throw new AssertionError("Unexpected InterruptedException", fail);
1288          }
1289      }
1290  
# Line 1437 | Line 1375 | public class JSR166TestCase extends Test
1375   //             r.run();
1376   //         } catch (Throwable fail) { threadUnexpectedException(fail); }
1377   //         if (millisElapsedSince(startTime) > timeoutMillis/2)
1378 < //             throw new AssertionFailedError("did not return promptly");
1378 > //             throw new AssertionError("did not return promptly");
1379   //     }
1380  
1381   //     void assertTerminatesPromptly(Runnable r) {
# Line 1450 | Line 1388 | public class JSR166TestCase extends Test
1388       */
1389      <T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) {
1390          long startTime = System.nanoTime();
1391 +        T actual = null;
1392          try {
1393 <            assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS));
1393 >            actual = f.get(timeoutMillis, MILLISECONDS);
1394          } catch (Throwable fail) { threadUnexpectedException(fail); }
1395 +        assertEquals(expectedValue, actual);
1396          if (millisElapsedSince(startTime) > timeoutMillis/2)
1397 <            throw new AssertionFailedError("timed get did not return promptly");
1397 >            throw new AssertionError("timed get did not return promptly");
1398      }
1399  
1400      <T> void checkTimedGet(Future<T> f, T expectedValue) {
# Line 1512 | Line 1452 | public class JSR166TestCase extends Test
1452          }
1453      }
1454  
1515    public abstract class RunnableShouldThrow implements Runnable {
1516        protected abstract void realRun() throws Throwable;
1517
1518        final Class<?> exceptionClass;
1519
1520        <T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) {
1521            this.exceptionClass = exceptionClass;
1522        }
1523
1524        public final void run() {
1525            try {
1526                realRun();
1527                threadShouldThrow(exceptionClass.getSimpleName());
1528            } catch (Throwable t) {
1529                if (! exceptionClass.isInstance(t))
1530                    threadUnexpectedException(t);
1531            }
1532        }
1533    }
1534
1455      public abstract class ThreadShouldThrow extends Thread {
1456          protected abstract void realRun() throws Throwable;
1457  
# Line 1651 | Line 1571 | public class JSR166TestCase extends Test
1571      }
1572  
1573      public void await(CountDownLatch latch, long timeoutMillis) {
1574 +        boolean timedOut = false;
1575          try {
1576 <            if (!latch.await(timeoutMillis, MILLISECONDS))
1656 <                fail("timed out waiting for CountDownLatch for "
1657 <                     + (timeoutMillis/1000) + " sec");
1576 >            timedOut = !latch.await(timeoutMillis, MILLISECONDS);
1577          } catch (Throwable fail) {
1578              threadUnexpectedException(fail);
1579          }
1580 +        if (timedOut)
1581 +            fail("timed out waiting for CountDownLatch for "
1582 +                 + (timeoutMillis/1000) + " sec");
1583      }
1584  
1585      public void await(CountDownLatch latch) {
# Line 1665 | Line 1587 | public class JSR166TestCase extends Test
1587      }
1588  
1589      public void await(Semaphore semaphore) {
1590 +        boolean timedOut = false;
1591          try {
1592 <            if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
1670 <                fail("timed out waiting for Semaphore for "
1671 <                     + (LONG_DELAY_MS/1000) + " sec");
1592 >            timedOut = !semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS);
1593          } catch (Throwable fail) {
1594              threadUnexpectedException(fail);
1595          }
1596 +        if (timedOut)
1597 +            fail("timed out waiting for Semaphore for "
1598 +                 + (LONG_DELAY_MS/1000) + " sec");
1599      }
1600  
1601      public void await(CyclicBarrier barrier) {
# Line 1696 | Line 1620 | public class JSR166TestCase extends Test
1620   //         long startTime = System.nanoTime();
1621   //         while (!flag.get()) {
1622   //             if (millisElapsedSince(startTime) > timeoutMillis)
1623 < //                 throw new AssertionFailedError("timed out");
1623 > //                 throw new AssertionError("timed out");
1624   //             Thread.yield();
1625   //         }
1626   //     }
# Line 1783 | Line 1707 | public class JSR166TestCase extends Test
1707  
1708      /**
1709       * A CyclicBarrier that uses timed await and fails with
1710 <     * AssertionFailedErrors instead of throwing checked exceptions.
1710 >     * AssertionErrors instead of throwing checked exceptions.
1711       */
1712      public static class CheckedBarrier extends CyclicBarrier {
1713          public CheckedBarrier(int parties) { super(parties); }
# Line 1792 | Line 1716 | public class JSR166TestCase extends Test
1716              try {
1717                  return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
1718              } catch (TimeoutException timedOut) {
1719 <                throw new AssertionFailedError("timed out");
1719 >                throw new AssertionError("timed out");
1720              } catch (Exception fail) {
1721 <                AssertionFailedError afe =
1798 <                    new AssertionFailedError("Unexpected exception: " + fail);
1799 <                afe.initCause(fail);
1800 <                throw afe;
1721 >                throw new AssertionError("Unexpected exception: " + fail, fail);
1722              }
1723          }
1724      }
# Line 1860 | Line 1781 | public class JSR166TestCase extends Test
1781  
1782      @SuppressWarnings("unchecked")
1783      <T> T serialClone(T o) {
1784 +        T clone = null;
1785          try {
1786              ObjectInputStream ois = new ObjectInputStream
1787                  (new ByteArrayInputStream(serialBytes(o)));
1788 <            T clone = (T) ois.readObject();
1867 <            if (o == clone) assertImmutable(o);
1868 <            assertSame(o.getClass(), clone.getClass());
1869 <            return clone;
1788 >            clone = (T) ois.readObject();
1789          } catch (Throwable fail) {
1790              threadUnexpectedException(fail);
1872            return null;
1791          }
1792 +        if (o == clone) assertImmutable(o);
1793 +        else assertSame(o.getClass(), clone.getClass());
1794 +        return clone;
1795      }
1796  
1797      /**
# Line 1889 | Line 1810 | public class JSR166TestCase extends Test
1810              (new ByteArrayInputStream(bos.toByteArray()));
1811          T clone = (T) ois.readObject();
1812          if (o == clone) assertImmutable(o);
1813 <        assertSame(o.getClass(), clone.getClass());
1813 >        else assertSame(o.getClass(), clone.getClass());
1814          return clone;
1815      }
1816  
# Line 1920 | Line 1841 | public class JSR166TestCase extends Test
1841              try { throwingAction.run(); }
1842              catch (Throwable t) {
1843                  threw = true;
1844 <                if (!expectedExceptionClass.isInstance(t)) {
1845 <                    AssertionFailedError afe =
1846 <                        new AssertionFailedError
1847 <                        ("Expected " + expectedExceptionClass.getName() +
1848 <                         ", got " + t.getClass().getName());
1928 <                    afe.initCause(t);
1929 <                    threadUnexpectedException(afe);
1930 <                }
1844 >                if (!expectedExceptionClass.isInstance(t))
1845 >                    throw new AssertionError(
1846 >                            "Expected " + expectedExceptionClass.getName() +
1847 >                            ", got " + t.getClass().getName(),
1848 >                            t);
1849              }
1850              if (!threw)
1851                  shouldThrow(expectedExceptionClass.getName());
# Line 1960 | Line 1878 | public class JSR166TestCase extends Test
1878          Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current());
1879      }
1880  
1881 +    /**
1882 +     * Returns the same String as would be returned by {@link
1883 +     * Object#toString}, whether or not the given object's class
1884 +     * overrides toString().
1885 +     *
1886 +     * @see System#identityHashCode
1887 +     */
1888 +    static String identityString(Object x) {
1889 +        return x.getClass().getName()
1890 +            + "@" + Integer.toHexString(System.identityHashCode(x));
1891 +    }
1892 +
1893      // --- Shared assertions for Executor tests ---
1894  
1895      /**
# Line 2131 | Line 2061 | public class JSR166TestCase extends Test
2061          assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount());
2062          assertEquals(savedQueueSize, p.getQueue().size());
2063      }
2064 +
2065 +    void assertCollectionsEquals(Collection<?> x, Collection<?> y) {
2066 +        assertEquals(x, y);
2067 +        assertEquals(y, x);
2068 +        assertEquals(x.isEmpty(), y.isEmpty());
2069 +        assertEquals(x.size(), y.size());
2070 +        if (x instanceof List) {
2071 +            assertEquals(x.toString(), y.toString());
2072 +        }
2073 +        if (x instanceof List || x instanceof Set) {
2074 +            assertEquals(x.hashCode(), y.hashCode());
2075 +        }
2076 +        if (x instanceof List || x instanceof Deque) {
2077 +            assertTrue(Arrays.equals(x.toArray(), y.toArray()));
2078 +            assertTrue(Arrays.equals(x.toArray(new Object[0]),
2079 +                                     y.toArray(new Object[0])));
2080 +        }
2081 +    }
2082 +
2083 +    /**
2084 +     * A weaker form of assertCollectionsEquals which does not insist
2085 +     * that the two collections satisfy Object#equals(Object), since
2086 +     * they may use identity semantics as Deques do.
2087 +     */
2088 +    void assertCollectionsEquivalent(Collection<?> x, Collection<?> y) {
2089 +        if (x instanceof List || x instanceof Set)
2090 +            assertCollectionsEquals(x, y);
2091 +        else {
2092 +            assertEquals(x.isEmpty(), y.isEmpty());
2093 +            assertEquals(x.size(), y.size());
2094 +            assertEquals(new HashSet(x), new HashSet(y));
2095 +            if (x instanceof Deque) {
2096 +                assertTrue(Arrays.equals(x.toArray(), y.toArray()));
2097 +                assertTrue(Arrays.equals(x.toArray(new Object[0]),
2098 +                                         y.toArray(new Object[0])));
2099 +            }
2100 +        }
2101 +    }
2102   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines