changeset 48843 | 21efc1774302 |
parent 48541 | 946e34c2dec9 |
child 49564 | 260bf39376a4 |
48842:a5a2e4770524 | 48843:21efc1774302 |
---|---|
124 import java.util.concurrent.TimeoutException; |
124 import java.util.concurrent.TimeoutException; |
125 import java.util.concurrent.atomic.AtomicBoolean; |
125 import java.util.concurrent.atomic.AtomicBoolean; |
126 import java.util.concurrent.atomic.AtomicReference; |
126 import java.util.concurrent.atomic.AtomicReference; |
127 import java.util.regex.Pattern; |
127 import java.util.regex.Pattern; |
128 |
128 |
129 import junit.framework.AssertionFailedError; |
|
130 import junit.framework.Test; |
129 import junit.framework.Test; |
131 import junit.framework.TestCase; |
130 import junit.framework.TestCase; |
132 import junit.framework.TestResult; |
131 import junit.framework.TestResult; |
133 import junit.framework.TestSuite; |
132 import junit.framework.TestSuite; |
134 |
133 |
444 public static void addNamedTestClasses(TestSuite suite, |
443 public static void addNamedTestClasses(TestSuite suite, |
445 String... testClassNames) { |
444 String... testClassNames) { |
446 for (String testClassName : testClassNames) { |
445 for (String testClassName : testClassNames) { |
447 try { |
446 try { |
448 Class<?> testClass = Class.forName(testClassName); |
447 Class<?> testClass = Class.forName(testClassName); |
449 Method m = testClass.getDeclaredMethod("suite", |
448 Method m = testClass.getDeclaredMethod("suite"); |
450 new Class<?>[0]); |
|
451 suite.addTest(newTestSuite((Test)m.invoke(null))); |
449 suite.addTest(newTestSuite((Test)m.invoke(null))); |
452 } catch (Exception e) { |
450 } catch (ReflectiveOperationException e) { |
453 throw new Error("Missing test class", e); |
451 throw new AssertionError("Missing test class", e); |
454 } |
452 } |
455 } |
453 } |
456 } |
454 } |
457 |
455 |
458 public static final double JAVA_CLASS_VERSION; |
456 public static final double JAVA_CLASS_VERSION; |
625 Constructor c = |
623 Constructor c = |
626 testClass.getDeclaredConstructor(dataClass, String.class); |
624 testClass.getDeclaredConstructor(dataClass, String.class); |
627 for (String methodName : testMethodNames(testClass)) |
625 for (String methodName : testMethodNames(testClass)) |
628 suite.addTest((Test) c.newInstance(data, methodName)); |
626 suite.addTest((Test) c.newInstance(data, methodName)); |
629 return suite; |
627 return suite; |
630 } catch (Exception e) { |
628 } catch (ReflectiveOperationException e) { |
631 throw new Error(e); |
629 throw new AssertionError(e); |
632 } |
630 } |
633 } |
631 } |
634 |
632 |
635 /** |
633 /** |
636 * Returns junit-style testSuite for the jdk8 extension of the |
634 * Returns junit-style testSuite for the jdk8 extension of the |
642 Class<ExtraData> dataClass, |
640 Class<ExtraData> dataClass, |
643 ExtraData data) { |
641 ExtraData data) { |
644 if (atLeastJava8()) { |
642 if (atLeastJava8()) { |
645 String name = testClass.getName(); |
643 String name = testClass.getName(); |
646 String name8 = name.replaceAll("Test$", "8Test"); |
644 String name8 = name.replaceAll("Test$", "8Test"); |
647 if (name.equals(name8)) throw new Error(name); |
645 if (name.equals(name8)) throw new AssertionError(name); |
648 try { |
646 try { |
649 return (Test) |
647 return (Test) |
650 Class.forName(name8) |
648 Class.forName(name8) |
651 .getMethod("testSuite", new Class[] { dataClass }) |
649 .getMethod("testSuite", dataClass) |
652 .invoke(null, data); |
650 .invoke(null, data); |
653 } catch (Exception e) { |
651 } catch (ReflectiveOperationException e) { |
654 throw new Error(e); |
652 throw new AssertionError(e); |
655 } |
653 } |
656 } else { |
654 } else { |
657 return new TestSuite(); |
655 return new TestSuite(); |
658 } |
656 } |
659 } |
657 } |
758 |
756 |
759 void tearDownFail(String format, Object... args) { |
757 void tearDownFail(String format, Object... args) { |
760 String msg = toString() + ": " + String.format(format, args); |
758 String msg = toString() + ": " + String.format(format, args); |
761 System.err.println(msg); |
759 System.err.println(msg); |
762 dumpTestThreads(); |
760 dumpTestThreads(); |
763 throw new AssertionFailedError(msg); |
761 throw new AssertionError(msg); |
764 } |
762 } |
765 |
763 |
766 /** |
764 /** |
767 * Extra checks that get done for all test cases. |
765 * Extra checks that get done for all test cases. |
768 * |
766 * |
779 throw (Error) t; |
777 throw (Error) t; |
780 else if (t instanceof RuntimeException) |
778 else if (t instanceof RuntimeException) |
781 throw (RuntimeException) t; |
779 throw (RuntimeException) t; |
782 else if (t instanceof Exception) |
780 else if (t instanceof Exception) |
783 throw (Exception) t; |
781 throw (Exception) t; |
784 else { |
782 else |
785 AssertionFailedError afe = |
783 throw new AssertionError(t.toString(), t); |
786 new AssertionFailedError(t.toString()); |
|
787 afe.initCause(t); |
|
788 throw afe; |
|
789 } |
|
790 } |
784 } |
791 |
785 |
792 if (Thread.interrupted()) |
786 if (Thread.interrupted()) |
793 tearDownFail("interrupt status set in main thread"); |
787 tearDownFail("interrupt status set in main thread"); |
794 |
788 |
818 tearDownFail("ForkJoin common pool thread stuck"); |
812 tearDownFail("ForkJoin common pool thread stuck"); |
819 } |
813 } |
820 |
814 |
821 /** |
815 /** |
822 * Just like fail(reason), but additionally recording (using |
816 * Just like fail(reason), but additionally recording (using |
823 * threadRecordFailure) any AssertionFailedError thrown, so that |
817 * threadRecordFailure) any AssertionError thrown, so that the |
824 * the current testcase will fail. |
818 * current testcase will fail. |
825 */ |
819 */ |
826 public void threadFail(String reason) { |
820 public void threadFail(String reason) { |
827 try { |
821 try { |
828 fail(reason); |
822 fail(reason); |
829 } catch (AssertionFailedError t) { |
823 } catch (AssertionError fail) { |
830 threadRecordFailure(t); |
824 threadRecordFailure(fail); |
831 throw t; |
825 throw fail; |
832 } |
826 } |
833 } |
827 } |
834 |
828 |
835 /** |
829 /** |
836 * Just like assertTrue(b), but additionally recording (using |
830 * Just like assertTrue(b), but additionally recording (using |
837 * threadRecordFailure) any AssertionFailedError thrown, so that |
831 * threadRecordFailure) any AssertionError thrown, so that the |
838 * the current testcase will fail. |
832 * current testcase will fail. |
839 */ |
833 */ |
840 public void threadAssertTrue(boolean b) { |
834 public void threadAssertTrue(boolean b) { |
841 try { |
835 try { |
842 assertTrue(b); |
836 assertTrue(b); |
843 } catch (AssertionFailedError t) { |
837 } catch (AssertionError fail) { |
844 threadRecordFailure(t); |
838 threadRecordFailure(fail); |
845 throw t; |
839 throw fail; |
846 } |
840 } |
847 } |
841 } |
848 |
842 |
849 /** |
843 /** |
850 * Just like assertFalse(b), but additionally recording (using |
844 * Just like assertFalse(b), but additionally recording (using |
851 * threadRecordFailure) any AssertionFailedError thrown, so that |
845 * threadRecordFailure) any AssertionError thrown, so that the |
852 * the current testcase will fail. |
846 * current testcase will fail. |
853 */ |
847 */ |
854 public void threadAssertFalse(boolean b) { |
848 public void threadAssertFalse(boolean b) { |
855 try { |
849 try { |
856 assertFalse(b); |
850 assertFalse(b); |
857 } catch (AssertionFailedError t) { |
851 } catch (AssertionError fail) { |
858 threadRecordFailure(t); |
852 threadRecordFailure(fail); |
859 throw t; |
853 throw fail; |
860 } |
854 } |
861 } |
855 } |
862 |
856 |
863 /** |
857 /** |
864 * Just like assertNull(x), but additionally recording (using |
858 * Just like assertNull(x), but additionally recording (using |
865 * threadRecordFailure) any AssertionFailedError thrown, so that |
859 * threadRecordFailure) any AssertionError thrown, so that the |
866 * the current testcase will fail. |
860 * current testcase will fail. |
867 */ |
861 */ |
868 public void threadAssertNull(Object x) { |
862 public void threadAssertNull(Object x) { |
869 try { |
863 try { |
870 assertNull(x); |
864 assertNull(x); |
871 } catch (AssertionFailedError t) { |
865 } catch (AssertionError fail) { |
872 threadRecordFailure(t); |
866 threadRecordFailure(fail); |
873 throw t; |
867 throw fail; |
874 } |
868 } |
875 } |
869 } |
876 |
870 |
877 /** |
871 /** |
878 * Just like assertEquals(x, y), but additionally recording (using |
872 * Just like assertEquals(x, y), but additionally recording (using |
879 * threadRecordFailure) any AssertionFailedError thrown, so that |
873 * threadRecordFailure) any AssertionError thrown, so that the |
880 * the current testcase will fail. |
874 * current testcase will fail. |
881 */ |
875 */ |
882 public void threadAssertEquals(long x, long y) { |
876 public void threadAssertEquals(long x, long y) { |
883 try { |
877 try { |
884 assertEquals(x, y); |
878 assertEquals(x, y); |
885 } catch (AssertionFailedError t) { |
879 } catch (AssertionError fail) { |
886 threadRecordFailure(t); |
880 threadRecordFailure(fail); |
887 throw t; |
881 throw fail; |
888 } |
882 } |
889 } |
883 } |
890 |
884 |
891 /** |
885 /** |
892 * Just like assertEquals(x, y), but additionally recording (using |
886 * Just like assertEquals(x, y), but additionally recording (using |
893 * threadRecordFailure) any AssertionFailedError thrown, so that |
887 * threadRecordFailure) any AssertionError thrown, so that the |
894 * the current testcase will fail. |
888 * current testcase will fail. |
895 */ |
889 */ |
896 public void threadAssertEquals(Object x, Object y) { |
890 public void threadAssertEquals(Object x, Object y) { |
897 try { |
891 try { |
898 assertEquals(x, y); |
892 assertEquals(x, y); |
899 } catch (AssertionFailedError fail) { |
893 } catch (AssertionError fail) { |
900 threadRecordFailure(fail); |
894 threadRecordFailure(fail); |
901 throw fail; |
895 throw fail; |
902 } catch (Throwable fail) { |
896 } catch (Throwable fail) { |
903 threadUnexpectedException(fail); |
897 threadUnexpectedException(fail); |
904 } |
898 } |
905 } |
899 } |
906 |
900 |
907 /** |
901 /** |
908 * Just like assertSame(x, y), but additionally recording (using |
902 * Just like assertSame(x, y), but additionally recording (using |
909 * threadRecordFailure) any AssertionFailedError thrown, so that |
903 * threadRecordFailure) any AssertionError thrown, so that the |
910 * the current testcase will fail. |
904 * current testcase will fail. |
911 */ |
905 */ |
912 public void threadAssertSame(Object x, Object y) { |
906 public void threadAssertSame(Object x, Object y) { |
913 try { |
907 try { |
914 assertSame(x, y); |
908 assertSame(x, y); |
915 } catch (AssertionFailedError fail) { |
909 } catch (AssertionError fail) { |
916 threadRecordFailure(fail); |
910 threadRecordFailure(fail); |
917 throw fail; |
911 throw fail; |
918 } |
912 } |
919 } |
913 } |
920 |
914 |
932 threadFail("should throw " + exceptionName); |
926 threadFail("should throw " + exceptionName); |
933 } |
927 } |
934 |
928 |
935 /** |
929 /** |
936 * Records the given exception using {@link #threadRecordFailure}, |
930 * Records the given exception using {@link #threadRecordFailure}, |
937 * then rethrows the exception, wrapping it in an |
931 * then rethrows the exception, wrapping it in an AssertionError |
938 * AssertionFailedError if necessary. |
932 * if necessary. |
939 */ |
933 */ |
940 public void threadUnexpectedException(Throwable t) { |
934 public void threadUnexpectedException(Throwable t) { |
941 threadRecordFailure(t); |
935 threadRecordFailure(t); |
942 t.printStackTrace(); |
936 t.printStackTrace(); |
943 if (t instanceof RuntimeException) |
937 if (t instanceof RuntimeException) |
944 throw (RuntimeException) t; |
938 throw (RuntimeException) t; |
945 else if (t instanceof Error) |
939 else if (t instanceof Error) |
946 throw (Error) t; |
940 throw (Error) t; |
947 else { |
941 else |
948 AssertionFailedError afe = |
942 throw new AssertionError("unexpected exception: " + t, t); |
949 new AssertionFailedError("unexpected exception: " + t); |
|
950 afe.initCause(t); |
|
951 throw afe; |
|
952 } |
|
953 } |
943 } |
954 |
944 |
955 /** |
945 /** |
956 * Delays, via Thread.sleep, for the given millisecond delay, but |
946 * Delays, via Thread.sleep, for the given millisecond delay, but |
957 * if the sleep is shorter than specified, may re-sleep or yield |
947 * if the sleep is shorter than specified, may re-sleep or yield |
1123 // always sleep at least 1 ms, with high probability avoiding |
1113 // always sleep at least 1 ms, with high probability avoiding |
1124 // transitory states |
1114 // transitory states |
1125 for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { |
1115 for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { |
1126 try { delay(1); } |
1116 try { delay(1); } |
1127 catch (InterruptedException fail) { |
1117 catch (InterruptedException fail) { |
1128 fail("Unexpected InterruptedException"); |
1118 throw new AssertionError("Unexpected InterruptedException", fail); |
1129 } |
1119 } |
1130 Thread.State s = thread.getState(); |
1120 Thread.State s = thread.getState(); |
1131 if (s == expected) |
1121 if (s == expected) |
1132 return; |
1122 return; |
1133 else if (s == Thread.State.TERMINATED) |
1123 else if (s == Thread.State.TERMINATED) |
1309 new java.io.FilePermission("<<ALL FILES>>", "read")); |
1299 new java.io.FilePermission("<<ALL FILES>>", "read")); |
1310 } |
1300 } |
1311 |
1301 |
1312 /** |
1302 /** |
1313 * Sleeps until the given time has elapsed. |
1303 * Sleeps until the given time has elapsed. |
1314 * Throws AssertionFailedError if interrupted. |
1304 * Throws AssertionError if interrupted. |
1315 */ |
1305 */ |
1316 static void sleep(long millis) { |
1306 static void sleep(long millis) { |
1317 try { |
1307 try { |
1318 delay(millis); |
1308 delay(millis); |
1319 } catch (InterruptedException fail) { |
1309 } catch (InterruptedException fail) { |
1320 AssertionFailedError afe = |
1310 throw new AssertionError("Unexpected InterruptedException", fail); |
1321 new AssertionFailedError("Unexpected InterruptedException"); |
|
1322 afe.initCause(fail); |
|
1323 throw afe; |
|
1324 } |
1311 } |
1325 } |
1312 } |
1326 |
1313 |
1327 /** |
1314 /** |
1328 * Spin-waits up to the specified number of milliseconds for the given |
1315 * Spin-waits up to the specified number of milliseconds for the given |
1409 // long startTime = System.nanoTime(); |
1396 // long startTime = System.nanoTime(); |
1410 // try { |
1397 // try { |
1411 // r.run(); |
1398 // r.run(); |
1412 // } catch (Throwable fail) { threadUnexpectedException(fail); } |
1399 // } catch (Throwable fail) { threadUnexpectedException(fail); } |
1413 // if (millisElapsedSince(startTime) > timeoutMillis/2) |
1400 // if (millisElapsedSince(startTime) > timeoutMillis/2) |
1414 // throw new AssertionFailedError("did not return promptly"); |
1401 // throw new AssertionError("did not return promptly"); |
1415 // } |
1402 // } |
1416 |
1403 |
1417 // void assertTerminatesPromptly(Runnable r) { |
1404 // void assertTerminatesPromptly(Runnable r) { |
1418 // assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1405 // assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1419 // } |
1406 // } |
1426 long startTime = System.nanoTime(); |
1413 long startTime = System.nanoTime(); |
1427 try { |
1414 try { |
1428 assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1415 assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1429 } catch (Throwable fail) { threadUnexpectedException(fail); } |
1416 } catch (Throwable fail) { threadUnexpectedException(fail); } |
1430 if (millisElapsedSince(startTime) > timeoutMillis/2) |
1417 if (millisElapsedSince(startTime) > timeoutMillis/2) |
1431 throw new AssertionFailedError("timed get did not return promptly"); |
1418 throw new AssertionError("timed get did not return promptly"); |
1432 } |
1419 } |
1433 |
1420 |
1434 <T> void checkTimedGet(Future<T> f, T expectedValue) { |
1421 <T> void checkTimedGet(Future<T> f, T expectedValue) { |
1435 checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
1422 checkTimedGet(f, expectedValue, LONG_DELAY_MS); |
1436 } |
1423 } |
1668 // */ |
1655 // */ |
1669 // public void await(AtomicBoolean flag, long timeoutMillis) { |
1656 // public void await(AtomicBoolean flag, long timeoutMillis) { |
1670 // long startTime = System.nanoTime(); |
1657 // long startTime = System.nanoTime(); |
1671 // while (!flag.get()) { |
1658 // while (!flag.get()) { |
1672 // if (millisElapsedSince(startTime) > timeoutMillis) |
1659 // if (millisElapsedSince(startTime) > timeoutMillis) |
1673 // throw new AssertionFailedError("timed out"); |
1660 // throw new AssertionError("timed out"); |
1674 // Thread.yield(); |
1661 // Thread.yield(); |
1675 // } |
1662 // } |
1676 // } |
1663 // } |
1677 |
1664 |
1678 public static class NPETask implements Callable<String> { |
1665 public static class NPETask implements Callable<String> { |
1755 ThreadPoolExecutor executor) {} |
1742 ThreadPoolExecutor executor) {} |
1756 } |
1743 } |
1757 |
1744 |
1758 /** |
1745 /** |
1759 * A CyclicBarrier that uses timed await and fails with |
1746 * A CyclicBarrier that uses timed await and fails with |
1760 * AssertionFailedErrors instead of throwing checked exceptions. |
1747 * AssertionErrors instead of throwing checked exceptions. |
1761 */ |
1748 */ |
1762 public static class CheckedBarrier extends CyclicBarrier { |
1749 public static class CheckedBarrier extends CyclicBarrier { |
1763 public CheckedBarrier(int parties) { super(parties); } |
1750 public CheckedBarrier(int parties) { super(parties); } |
1764 |
1751 |
1765 public int await() { |
1752 public int await() { |
1766 try { |
1753 try { |
1767 return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1754 return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1768 } catch (TimeoutException timedOut) { |
1755 } catch (TimeoutException timedOut) { |
1769 throw new AssertionFailedError("timed out"); |
1756 throw new AssertionError("timed out"); |
1770 } catch (Exception fail) { |
1757 } catch (Exception fail) { |
1771 AssertionFailedError afe = |
1758 throw new AssertionError("Unexpected exception: " + fail, fail); |
1772 new AssertionFailedError("Unexpected exception: " + fail); |
|
1773 afe.initCause(fail); |
|
1774 throw afe; |
|
1775 } |
1759 } |
1776 } |
1760 } |
1777 } |
1761 } |
1778 |
1762 |
1779 void checkEmpty(BlockingQueue q) { |
1763 void checkEmpty(BlockingQueue q) { |
1892 for (Runnable throwingAction : throwingActions) { |
1876 for (Runnable throwingAction : throwingActions) { |
1893 boolean threw = false; |
1877 boolean threw = false; |
1894 try { throwingAction.run(); } |
1878 try { throwingAction.run(); } |
1895 catch (Throwable t) { |
1879 catch (Throwable t) { |
1896 threw = true; |
1880 threw = true; |
1897 if (!expectedExceptionClass.isInstance(t)) { |
1881 if (!expectedExceptionClass.isInstance(t)) |
1898 AssertionFailedError afe = |
1882 throw new AssertionError( |
1899 new AssertionFailedError |
1883 "Expected " + expectedExceptionClass.getName() + |
1900 ("Expected " + expectedExceptionClass.getName() + |
1884 ", got " + t.getClass().getName(), |
1901 ", got " + t.getClass().getName()); |
1885 t); |
1902 afe.initCause(t); |
|
1903 threadUnexpectedException(afe); |
|
1904 } |
|
1905 } |
1886 } |
1906 if (!threw) |
1887 if (!threw) |
1907 shouldThrow(expectedExceptionClass.getName()); |
1888 shouldThrow(expectedExceptionClass.getName()); |
1908 } |
1889 } |
1909 } |
1890 } |