99 |
|
import java.util.concurrent.atomic.AtomicReference; |
100 |
|
import java.util.regex.Pattern; |
101 |
|
|
102 |
– |
import junit.framework.AssertionFailedError; |
102 |
|
import junit.framework.Test; |
103 |
|
import junit.framework.TestCase; |
104 |
|
import junit.framework.TestResult; |
418 |
|
for (String testClassName : testClassNames) { |
419 |
|
try { |
420 |
|
Class<?> testClass = Class.forName(testClassName); |
421 |
< |
Method m = testClass.getDeclaredMethod("suite", |
423 |
< |
new Class<?>[0]); |
421 |
> |
Method m = testClass.getDeclaredMethod("suite"); |
422 |
|
suite.addTest(newTestSuite((Test)m.invoke(null))); |
423 |
< |
} catch (Exception e) { |
424 |
< |
throw new Error("Missing test class", e); |
423 |
> |
} catch (ReflectiveOperationException e) { |
424 |
> |
throw new AssertionError("Missing test class", e); |
425 |
|
} |
426 |
|
} |
427 |
|
} |
443 |
|
} |
444 |
|
} |
445 |
|
|
446 |
< |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
447 |
< |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
448 |
< |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
449 |
< |
public static boolean atLeastJava9() { |
450 |
< |
return JAVA_CLASS_VERSION >= 53.0 |
453 |
< |
// 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 |
< |
} |
446 |
> |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
447 |
> |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
448 |
> |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
449 |
> |
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
450 |
> |
public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; } |
451 |
|
|
452 |
|
/** |
453 |
|
* Collects all JSR166 unit tests as one suite. |
535 |
|
"DoubleAdderTest", |
536 |
|
"ForkJoinPool8Test", |
537 |
|
"ForkJoinTask8Test", |
538 |
+ |
"HashMapTest", |
539 |
|
"LinkedBlockingDeque8Test", |
540 |
|
"LinkedBlockingQueue8Test", |
541 |
|
"LongAccumulatorTest", |
598 |
|
for (String methodName : testMethodNames(testClass)) |
599 |
|
suite.addTest((Test) c.newInstance(data, methodName)); |
600 |
|
return suite; |
601 |
< |
} catch (Exception e) { |
602 |
< |
throw new Error(e); |
601 |
> |
} catch (ReflectiveOperationException e) { |
602 |
> |
throw new AssertionError(e); |
603 |
|
} |
604 |
|
} |
605 |
|
|
615 |
|
if (atLeastJava8()) { |
616 |
|
String name = testClass.getName(); |
617 |
|
String name8 = name.replaceAll("Test$", "8Test"); |
618 |
< |
if (name.equals(name8)) throw new Error(name); |
618 |
> |
if (name.equals(name8)) throw new AssertionError(name); |
619 |
|
try { |
620 |
|
return (Test) |
621 |
|
Class.forName(name8) |
622 |
< |
.getMethod("testSuite", new Class[] { dataClass }) |
622 |
> |
.getMethod("testSuite", dataClass) |
623 |
|
.invoke(null, data); |
624 |
< |
} catch (Exception e) { |
625 |
< |
throw new Error(e); |
624 |
> |
} catch (ReflectiveOperationException e) { |
625 |
> |
throw new AssertionError(e); |
626 |
|
} |
627 |
|
} else { |
628 |
|
return new TestSuite(); |
731 |
|
String msg = toString() + ": " + String.format(format, args); |
732 |
|
System.err.println(msg); |
733 |
|
dumpTestThreads(); |
734 |
< |
throw new AssertionFailedError(msg); |
734 |
> |
throw new AssertionError(msg); |
735 |
|
} |
736 |
|
|
737 |
|
/** |
752 |
|
throw (RuntimeException) t; |
753 |
|
else if (t instanceof Exception) |
754 |
|
throw (Exception) t; |
755 |
< |
else { |
756 |
< |
AssertionFailedError afe = |
765 |
< |
new AssertionFailedError(t.toString()); |
766 |
< |
afe.initCause(t); |
767 |
< |
throw afe; |
768 |
< |
} |
755 |
> |
else |
756 |
> |
throw new AssertionError(t.toString(), t); |
757 |
|
} |
758 |
|
|
759 |
|
if (Thread.interrupted()) |
787 |
|
|
788 |
|
/** |
789 |
|
* Just like fail(reason), but additionally recording (using |
790 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
791 |
< |
* the current testcase will fail. |
790 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
791 |
> |
* current testcase will fail. |
792 |
|
*/ |
793 |
|
public void threadFail(String reason) { |
794 |
|
try { |
795 |
|
fail(reason); |
796 |
< |
} catch (AssertionFailedError t) { |
797 |
< |
threadRecordFailure(t); |
798 |
< |
throw t; |
796 |
> |
} catch (AssertionError fail) { |
797 |
> |
threadRecordFailure(fail); |
798 |
> |
throw fail; |
799 |
|
} |
800 |
|
} |
801 |
|
|
802 |
|
/** |
803 |
|
* Just like assertTrue(b), but additionally recording (using |
804 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
805 |
< |
* the current testcase will fail. |
804 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
805 |
> |
* current testcase will fail. |
806 |
|
*/ |
807 |
|
public void threadAssertTrue(boolean b) { |
808 |
|
try { |
809 |
|
assertTrue(b); |
810 |
< |
} catch (AssertionFailedError t) { |
811 |
< |
threadRecordFailure(t); |
812 |
< |
throw t; |
810 |
> |
} catch (AssertionError fail) { |
811 |
> |
threadRecordFailure(fail); |
812 |
> |
throw fail; |
813 |
|
} |
814 |
|
} |
815 |
|
|
816 |
|
/** |
817 |
|
* Just like assertFalse(b), but additionally recording (using |
818 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
819 |
< |
* the current testcase will fail. |
818 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
819 |
> |
* current testcase will fail. |
820 |
|
*/ |
821 |
|
public void threadAssertFalse(boolean b) { |
822 |
|
try { |
823 |
|
assertFalse(b); |
824 |
< |
} catch (AssertionFailedError t) { |
825 |
< |
threadRecordFailure(t); |
826 |
< |
throw t; |
824 |
> |
} catch (AssertionError fail) { |
825 |
> |
threadRecordFailure(fail); |
826 |
> |
throw fail; |
827 |
|
} |
828 |
|
} |
829 |
|
|
830 |
|
/** |
831 |
|
* Just like assertNull(x), but additionally recording (using |
832 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
833 |
< |
* the current testcase will fail. |
832 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
833 |
> |
* current testcase will fail. |
834 |
|
*/ |
835 |
|
public void threadAssertNull(Object x) { |
836 |
|
try { |
837 |
|
assertNull(x); |
838 |
< |
} catch (AssertionFailedError t) { |
839 |
< |
threadRecordFailure(t); |
840 |
< |
throw t; |
838 |
> |
} catch (AssertionError fail) { |
839 |
> |
threadRecordFailure(fail); |
840 |
> |
throw fail; |
841 |
|
} |
842 |
|
} |
843 |
|
|
844 |
|
/** |
845 |
|
* Just like assertEquals(x, y), but additionally recording (using |
846 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
847 |
< |
* the current testcase will fail. |
846 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
847 |
> |
* current testcase will fail. |
848 |
|
*/ |
849 |
|
public void threadAssertEquals(long x, long y) { |
850 |
|
try { |
851 |
|
assertEquals(x, y); |
852 |
< |
} catch (AssertionFailedError t) { |
853 |
< |
threadRecordFailure(t); |
854 |
< |
throw t; |
852 |
> |
} catch (AssertionError fail) { |
853 |
> |
threadRecordFailure(fail); |
854 |
> |
throw fail; |
855 |
|
} |
856 |
|
} |
857 |
|
|
858 |
|
/** |
859 |
|
* Just like assertEquals(x, y), but additionally recording (using |
860 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
861 |
< |
* the current testcase will fail. |
860 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
861 |
> |
* current testcase will fail. |
862 |
|
*/ |
863 |
|
public void threadAssertEquals(Object x, Object y) { |
864 |
|
try { |
865 |
|
assertEquals(x, y); |
866 |
< |
} catch (AssertionFailedError fail) { |
866 |
> |
} catch (AssertionError fail) { |
867 |
|
threadRecordFailure(fail); |
868 |
|
throw fail; |
869 |
|
} catch (Throwable fail) { |
873 |
|
|
874 |
|
/** |
875 |
|
* Just like assertSame(x, y), but additionally recording (using |
876 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
877 |
< |
* the current testcase will fail. |
876 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
877 |
> |
* current testcase will fail. |
878 |
|
*/ |
879 |
|
public void threadAssertSame(Object x, Object y) { |
880 |
|
try { |
881 |
|
assertSame(x, y); |
882 |
< |
} catch (AssertionFailedError fail) { |
882 |
> |
} catch (AssertionError fail) { |
883 |
|
threadRecordFailure(fail); |
884 |
|
throw fail; |
885 |
|
} |
901 |
|
|
902 |
|
/** |
903 |
|
* Records the given exception using {@link #threadRecordFailure}, |
904 |
< |
* then rethrows the exception, wrapping it in an |
905 |
< |
* AssertionFailedError if necessary. |
904 |
> |
* then rethrows the exception, wrapping it in an AssertionError |
905 |
> |
* if necessary. |
906 |
|
*/ |
907 |
|
public void threadUnexpectedException(Throwable t) { |
908 |
|
threadRecordFailure(t); |
911 |
|
throw (RuntimeException) t; |
912 |
|
else if (t instanceof Error) |
913 |
|
throw (Error) t; |
914 |
< |
else { |
915 |
< |
AssertionFailedError afe = |
928 |
< |
new AssertionFailedError("unexpected exception: " + t); |
929 |
< |
afe.initCause(t); |
930 |
< |
throw afe; |
931 |
< |
} |
914 |
> |
else |
915 |
> |
throw new AssertionError("unexpected exception: " + t, t); |
916 |
|
} |
917 |
|
|
918 |
|
/** |
1088 |
|
for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { |
1089 |
|
try { delay(1); } |
1090 |
|
catch (InterruptedException fail) { |
1091 |
< |
fail("Unexpected InterruptedException"); |
1091 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1092 |
|
} |
1093 |
|
Thread.State s = thread.getState(); |
1094 |
|
if (s == expected) |
1274 |
|
|
1275 |
|
/** |
1276 |
|
* Sleeps until the given time has elapsed. |
1277 |
< |
* Throws AssertionFailedError if interrupted. |
1277 |
> |
* Throws AssertionError if interrupted. |
1278 |
|
*/ |
1279 |
|
static void sleep(long millis) { |
1280 |
|
try { |
1281 |
|
delay(millis); |
1282 |
|
} catch (InterruptedException fail) { |
1283 |
< |
AssertionFailedError afe = |
1300 |
< |
new AssertionFailedError("Unexpected InterruptedException"); |
1301 |
< |
afe.initCause(fail); |
1302 |
< |
throw afe; |
1283 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1284 |
|
} |
1285 |
|
} |
1286 |
|
|
1371 |
|
// r.run(); |
1372 |
|
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1373 |
|
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1374 |
< |
// throw new AssertionFailedError("did not return promptly"); |
1374 |
> |
// throw new AssertionError("did not return promptly"); |
1375 |
|
// } |
1376 |
|
|
1377 |
|
// void assertTerminatesPromptly(Runnable r) { |
1388 |
|
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1389 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1390 |
|
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1391 |
< |
throw new AssertionFailedError("timed get did not return promptly"); |
1391 |
> |
throw new AssertionError("timed get did not return promptly"); |
1392 |
|
} |
1393 |
|
|
1394 |
|
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1446 |
|
} |
1447 |
|
} |
1448 |
|
|
1468 |
– |
public abstract class RunnableShouldThrow implements Runnable { |
1469 |
– |
protected abstract void realRun() throws Throwable; |
1470 |
– |
|
1471 |
– |
final Class<?> exceptionClass; |
1472 |
– |
|
1473 |
– |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
1474 |
– |
this.exceptionClass = exceptionClass; |
1475 |
– |
} |
1476 |
– |
|
1477 |
– |
public final void run() { |
1478 |
– |
try { |
1479 |
– |
realRun(); |
1480 |
– |
threadShouldThrow(exceptionClass.getSimpleName()); |
1481 |
– |
} catch (Throwable t) { |
1482 |
– |
if (! exceptionClass.isInstance(t)) |
1483 |
– |
threadUnexpectedException(t); |
1484 |
– |
} |
1485 |
– |
} |
1486 |
– |
} |
1487 |
– |
|
1449 |
|
public abstract class ThreadShouldThrow extends Thread { |
1450 |
|
protected abstract void realRun() throws Throwable; |
1451 |
|
|
1610 |
|
// long startTime = System.nanoTime(); |
1611 |
|
// while (!flag.get()) { |
1612 |
|
// if (millisElapsedSince(startTime) > timeoutMillis) |
1613 |
< |
// throw new AssertionFailedError("timed out"); |
1613 |
> |
// throw new AssertionError("timed out"); |
1614 |
|
// Thread.yield(); |
1615 |
|
// } |
1616 |
|
// } |
1697 |
|
|
1698 |
|
/** |
1699 |
|
* A CyclicBarrier that uses timed await and fails with |
1700 |
< |
* AssertionFailedErrors instead of throwing checked exceptions. |
1700 |
> |
* AssertionErrors instead of throwing checked exceptions. |
1701 |
|
*/ |
1702 |
|
public static class CheckedBarrier extends CyclicBarrier { |
1703 |
|
public CheckedBarrier(int parties) { super(parties); } |
1706 |
|
try { |
1707 |
|
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1708 |
|
} catch (TimeoutException timedOut) { |
1709 |
< |
throw new AssertionFailedError("timed out"); |
1709 |
> |
throw new AssertionError("timed out"); |
1710 |
|
} catch (Exception fail) { |
1711 |
< |
AssertionFailedError afe = |
1751 |
< |
new AssertionFailedError("Unexpected exception: " + fail); |
1752 |
< |
afe.initCause(fail); |
1753 |
< |
throw afe; |
1711 |
> |
throw new AssertionError("Unexpected exception: " + fail, fail); |
1712 |
|
} |
1713 |
|
} |
1714 |
|
} |
1831 |
|
try { throwingAction.run(); } |
1832 |
|
catch (Throwable t) { |
1833 |
|
threw = true; |
1834 |
< |
if (!expectedExceptionClass.isInstance(t)) { |
1835 |
< |
AssertionFailedError afe = |
1836 |
< |
new AssertionFailedError |
1837 |
< |
("Expected " + expectedExceptionClass.getName() + |
1838 |
< |
", got " + t.getClass().getName()); |
1881 |
< |
afe.initCause(t); |
1882 |
< |
threadUnexpectedException(afe); |
1883 |
< |
} |
1834 |
> |
if (!expectedExceptionClass.isInstance(t)) |
1835 |
> |
throw new AssertionError( |
1836 |
> |
"Expected " + expectedExceptionClass.getName() + |
1837 |
> |
", got " + t.getClass().getName(), |
1838 |
> |
t); |
1839 |
|
} |
1840 |
|
if (!threw) |
1841 |
|
shouldThrow(expectedExceptionClass.getName()); |
1868 |
|
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
1869 |
|
} |
1870 |
|
|
1871 |
+ |
/** |
1872 |
+ |
* Returns the same String as would be returned by {@link |
1873 |
+ |
* Object#toString}, whether or not the given object's class |
1874 |
+ |
* overrides toString(). |
1875 |
+ |
* |
1876 |
+ |
* @see System#identityHashCode |
1877 |
+ |
*/ |
1878 |
+ |
static String identityString(Object x) { |
1879 |
+ |
return x.getClass().getName() |
1880 |
+ |
+ "@" + Integer.toHexString(System.identityHashCode(x)); |
1881 |
+ |
} |
1882 |
+ |
|
1883 |
|
// --- Shared assertions for Executor tests --- |
1884 |
|
|
1885 |
|
/** |