49 |
|
import java.io.ObjectInputStream; |
50 |
|
import java.io.ObjectOutputStream; |
51 |
|
import java.lang.management.ManagementFactory; |
52 |
+ |
import java.lang.management.LockInfo; |
53 |
|
import java.lang.management.ThreadInfo; |
54 |
|
import java.lang.management.ThreadMXBean; |
55 |
|
import java.lang.reflect.Constructor; |
67 |
|
import java.util.Collection; |
68 |
|
import java.util.Collections; |
69 |
|
import java.util.Date; |
70 |
+ |
import java.util.Deque; |
71 |
|
import java.util.Enumeration; |
72 |
+ |
import java.util.HashSet; |
73 |
|
import java.util.Iterator; |
74 |
|
import java.util.List; |
75 |
|
import java.util.NoSuchElementException; |
76 |
|
import java.util.PropertyPermission; |
77 |
+ |
import java.util.Set; |
78 |
|
import java.util.concurrent.BlockingQueue; |
79 |
|
import java.util.concurrent.Callable; |
80 |
|
import java.util.concurrent.CountDownLatch; |
103 |
|
import java.util.concurrent.atomic.AtomicReference; |
104 |
|
import java.util.regex.Pattern; |
105 |
|
|
102 |
– |
import junit.framework.AssertionFailedError; |
106 |
|
import junit.framework.Test; |
107 |
|
import junit.framework.TestCase; |
108 |
|
import junit.framework.TestResult; |
118 |
|
* |
119 |
|
* <ol> |
120 |
|
* |
121 |
< |
* <li>All assertions in code running in generated threads must use |
122 |
< |
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
123 |
< |
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
124 |
< |
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
125 |
< |
* particularly recommended) for other code to use these forms too. |
126 |
< |
* Only the most typically used JUnit assertion methods are defined |
127 |
< |
* this way, but enough to live with. |
121 |
> |
* <li>All code not running in the main test thread (manually spawned threads |
122 |
> |
* or the common fork join pool) must be checked for failure (and completion!). |
123 |
> |
* Mechanisms that can be used to ensure this are: |
124 |
> |
* <ol> |
125 |
> |
* <li>Signalling via a synchronizer like AtomicInteger or CountDownLatch |
126 |
> |
* that the task completed normally, which is checked before returning from |
127 |
> |
* the test method in the main thread. |
128 |
> |
* <li>Using the forms {@link #threadFail}, {@link #threadAssertTrue}, |
129 |
> |
* or {@link #threadAssertNull}, (not {@code fail}, {@code assertTrue}, etc.) |
130 |
> |
* Only the most typically used JUnit assertion methods are defined |
131 |
> |
* this way, but enough to live with. |
132 |
> |
* <li>Recording failure explicitly using {@link #threadUnexpectedException} |
133 |
> |
* or {@link #threadRecordFailure}. |
134 |
> |
* <li>Using a wrapper like CheckedRunnable that uses one the mechanisms above. |
135 |
> |
* </ol> |
136 |
|
* |
137 |
|
* <li>If you override {@link #setUp} or {@link #tearDown}, make sure |
138 |
|
* to invoke {@code super.setUp} and {@code super.tearDown} within |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
+ |
private static final ThreadMXBean THREAD_MXBEAN |
248 |
+ |
= ManagementFactory.getThreadMXBean(); |
249 |
+ |
|
250 |
|
/** |
251 |
|
* The scaling factor to apply to standard delays used in tests. |
252 |
|
* May be initialized from any of: |
288 |
|
static volatile TestCase currentTestCase; |
289 |
|
// static volatile int currentRun = 0; |
290 |
|
static { |
291 |
< |
Runnable checkForWedgedTest = new Runnable() { public void run() { |
291 |
> |
Runnable wedgedTestDetector = new Runnable() { public void run() { |
292 |
|
// Avoid spurious reports with enormous runsPerTest. |
293 |
|
// A single test case run should never take more than 1 second. |
294 |
|
// But let's cap it at the high end too ... |
295 |
< |
final int timeoutMinutes = |
296 |
< |
Math.min(15, Math.max(runsPerTest / 60, 1)); |
295 |
> |
final int timeoutMinutesMin = Math.max(runsPerTest / 60, 1) |
296 |
> |
* Math.max((int) delayFactor, 1); |
297 |
> |
final int timeoutMinutes = Math.min(15, timeoutMinutesMin); |
298 |
|
for (TestCase lastTestCase = currentTestCase;;) { |
299 |
|
try { MINUTES.sleep(timeoutMinutes); } |
300 |
|
catch (InterruptedException unexpected) { break; } |
314 |
|
} |
315 |
|
lastTestCase = currentTestCase; |
316 |
|
}}}; |
317 |
< |
Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest"); |
317 |
> |
Thread thread = new Thread(wedgedTestDetector, "WedgedTestDetector"); |
318 |
|
thread.setDaemon(true); |
319 |
|
thread.start(); |
320 |
|
} |
358 |
|
// Never report first run of any test; treat it as a |
359 |
|
// warmup run, notably to trigger all needed classloading, |
360 |
|
if (i > 0) |
361 |
< |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
361 |
> |
System.out.printf("%s: %d%n", toString(), elapsedMillis); |
362 |
|
} |
363 |
|
} |
364 |
|
|
434 |
|
for (String testClassName : testClassNames) { |
435 |
|
try { |
436 |
|
Class<?> testClass = Class.forName(testClassName); |
437 |
< |
Method m = testClass.getDeclaredMethod("suite", |
423 |
< |
new Class<?>[0]); |
437 |
> |
Method m = testClass.getDeclaredMethod("suite"); |
438 |
|
suite.addTest(newTestSuite((Test)m.invoke(null))); |
439 |
< |
} catch (Exception e) { |
440 |
< |
throw new Error("Missing test class", e); |
439 |
> |
} catch (ReflectiveOperationException e) { |
440 |
> |
throw new AssertionError("Missing test class", e); |
441 |
|
} |
442 |
|
} |
443 |
|
} |
464 |
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
465 |
|
public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; } |
466 |
|
public static boolean atLeastJava10() { return JAVA_CLASS_VERSION >= 54.0; } |
467 |
+ |
public static boolean atLeastJava11() { return JAVA_CLASS_VERSION >= 55.0; } |
468 |
+ |
public static boolean atLeastJava12() { return JAVA_CLASS_VERSION >= 56.0; } |
469 |
+ |
public static boolean atLeastJava13() { return JAVA_CLASS_VERSION >= 57.0; } |
470 |
+ |
public static boolean atLeastJava14() { return JAVA_CLASS_VERSION >= 58.0; } |
471 |
+ |
public static boolean atLeastJava15() { return JAVA_CLASS_VERSION >= 59.0; } |
472 |
+ |
public static boolean atLeastJava16() { return JAVA_CLASS_VERSION >= 60.0; } |
473 |
+ |
public static boolean atLeastJava17() { return JAVA_CLASS_VERSION >= 61.0; } |
474 |
|
|
475 |
|
/** |
476 |
|
* Collects all JSR166 unit tests as one suite. |
522 |
|
ExecutorsTest.suite(), |
523 |
|
ExecutorCompletionServiceTest.suite(), |
524 |
|
FutureTaskTest.suite(), |
525 |
+ |
HashtableTest.suite(), |
526 |
|
LinkedBlockingDequeTest.suite(), |
527 |
|
LinkedBlockingQueueTest.suite(), |
528 |
|
LinkedListTest.suite(), |
562 |
|
"HashMapTest", |
563 |
|
"LinkedBlockingDeque8Test", |
564 |
|
"LinkedBlockingQueue8Test", |
565 |
+ |
"LinkedHashMapTest", |
566 |
|
"LongAccumulatorTest", |
567 |
|
"LongAdderTest", |
568 |
|
"SplittableRandomTest", |
623 |
|
for (String methodName : testMethodNames(testClass)) |
624 |
|
suite.addTest((Test) c.newInstance(data, methodName)); |
625 |
|
return suite; |
626 |
< |
} catch (Exception e) { |
627 |
< |
throw new Error(e); |
626 |
> |
} catch (ReflectiveOperationException e) { |
627 |
> |
throw new AssertionError(e); |
628 |
|
} |
629 |
|
} |
630 |
|
|
640 |
|
if (atLeastJava8()) { |
641 |
|
String name = testClass.getName(); |
642 |
|
String name8 = name.replaceAll("Test$", "8Test"); |
643 |
< |
if (name.equals(name8)) throw new Error(name); |
643 |
> |
if (name.equals(name8)) throw new AssertionError(name); |
644 |
|
try { |
645 |
|
return (Test) |
646 |
|
Class.forName(name8) |
647 |
< |
.getMethod("testSuite", new Class[] { dataClass }) |
647 |
> |
.getMethod("testSuite", dataClass) |
648 |
|
.invoke(null, data); |
649 |
< |
} catch (Exception e) { |
650 |
< |
throw new Error(e); |
649 |
> |
} catch (ReflectiveOperationException e) { |
650 |
> |
throw new AssertionError(e); |
651 |
|
} |
652 |
|
} else { |
653 |
|
return new TestSuite(); |
661 |
|
public static long MEDIUM_DELAY_MS; |
662 |
|
public static long LONG_DELAY_MS; |
663 |
|
|
664 |
+ |
/** |
665 |
+ |
* A delay significantly longer than LONG_DELAY_MS. |
666 |
+ |
* Use this in a thread that is waited for via awaitTermination(Thread). |
667 |
+ |
*/ |
668 |
+ |
public static long LONGER_DELAY_MS; |
669 |
+ |
|
670 |
|
private static final long RANDOM_TIMEOUT; |
671 |
|
private static final long RANDOM_EXPIRED_TIMEOUT; |
672 |
|
private static final TimeUnit RANDOM_TIMEUNIT; |
695 |
|
static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; } |
696 |
|
|
697 |
|
/** |
698 |
+ |
* Returns a random boolean; a "coin flip". |
699 |
+ |
*/ |
700 |
+ |
static boolean randomBoolean() { |
701 |
+ |
return ThreadLocalRandom.current().nextBoolean(); |
702 |
+ |
} |
703 |
+ |
|
704 |
+ |
/** |
705 |
+ |
* Returns a random element from given choices. |
706 |
+ |
*/ |
707 |
+ |
<T> T chooseRandomly(List<T> choices) { |
708 |
+ |
return choices.get(ThreadLocalRandom.current().nextInt(choices.size())); |
709 |
+ |
} |
710 |
+ |
|
711 |
+ |
/** |
712 |
+ |
* Returns a random element from given choices. |
713 |
+ |
*/ |
714 |
+ |
<T> T chooseRandomly(T... choices) { |
715 |
+ |
return choices[ThreadLocalRandom.current().nextInt(choices.length)]; |
716 |
+ |
} |
717 |
+ |
|
718 |
+ |
/** |
719 |
|
* Returns the shortest timed delay. This can be scaled up for |
720 |
|
* slow machines using the jsr166.delay.factor system property, |
721 |
|
* or via jtreg's -timeoutFactor: flag. |
733 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
734 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
735 |
|
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
736 |
+ |
LONGER_DELAY_MS = 2 * LONG_DELAY_MS; |
737 |
|
} |
738 |
|
|
739 |
|
private static final long TIMEOUT_DELAY_MS |
772 |
|
*/ |
773 |
|
public void threadRecordFailure(Throwable t) { |
774 |
|
System.err.println(t); |
775 |
< |
dumpTestThreads(); |
776 |
< |
threadFailure.compareAndSet(null, t); |
775 |
> |
if (threadFailure.compareAndSet(null, t)) |
776 |
> |
dumpTestThreads(); |
777 |
|
} |
778 |
|
|
779 |
|
public void setUp() { |
784 |
|
String msg = toString() + ": " + String.format(format, args); |
785 |
|
System.err.println(msg); |
786 |
|
dumpTestThreads(); |
787 |
< |
throw new AssertionFailedError(msg); |
787 |
> |
throw new AssertionError(msg); |
788 |
|
} |
789 |
|
|
790 |
|
/** |
805 |
|
throw (RuntimeException) t; |
806 |
|
else if (t instanceof Exception) |
807 |
|
throw (Exception) t; |
808 |
< |
else { |
809 |
< |
AssertionFailedError afe = |
759 |
< |
new AssertionFailedError(t.toString()); |
760 |
< |
afe.initCause(t); |
761 |
< |
throw afe; |
762 |
< |
} |
808 |
> |
else |
809 |
> |
throw new AssertionError(t.toString(), t); |
810 |
|
} |
811 |
|
|
812 |
|
if (Thread.interrupted()) |
840 |
|
|
841 |
|
/** |
842 |
|
* Just like fail(reason), but additionally recording (using |
843 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
844 |
< |
* the current testcase will fail. |
843 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
844 |
> |
* current testcase will fail. |
845 |
|
*/ |
846 |
|
public void threadFail(String reason) { |
847 |
|
try { |
848 |
|
fail(reason); |
849 |
< |
} catch (AssertionFailedError t) { |
850 |
< |
threadRecordFailure(t); |
851 |
< |
throw t; |
849 |
> |
} catch (AssertionError fail) { |
850 |
> |
threadRecordFailure(fail); |
851 |
> |
throw fail; |
852 |
|
} |
853 |
|
} |
854 |
|
|
855 |
|
/** |
856 |
|
* Just like assertTrue(b), but additionally recording (using |
857 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
858 |
< |
* the current testcase will fail. |
857 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
858 |
> |
* current testcase will fail. |
859 |
|
*/ |
860 |
|
public void threadAssertTrue(boolean b) { |
861 |
|
try { |
862 |
|
assertTrue(b); |
863 |
< |
} catch (AssertionFailedError t) { |
864 |
< |
threadRecordFailure(t); |
865 |
< |
throw t; |
863 |
> |
} catch (AssertionError fail) { |
864 |
> |
threadRecordFailure(fail); |
865 |
> |
throw fail; |
866 |
|
} |
867 |
|
} |
868 |
|
|
869 |
|
/** |
870 |
|
* Just like assertFalse(b), but additionally recording (using |
871 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
872 |
< |
* the current testcase will fail. |
871 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
872 |
> |
* current testcase will fail. |
873 |
|
*/ |
874 |
|
public void threadAssertFalse(boolean b) { |
875 |
|
try { |
876 |
|
assertFalse(b); |
877 |
< |
} catch (AssertionFailedError t) { |
878 |
< |
threadRecordFailure(t); |
879 |
< |
throw t; |
877 |
> |
} catch (AssertionError fail) { |
878 |
> |
threadRecordFailure(fail); |
879 |
> |
throw fail; |
880 |
|
} |
881 |
|
} |
882 |
|
|
883 |
|
/** |
884 |
|
* Just like assertNull(x), but additionally recording (using |
885 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
886 |
< |
* the current testcase will fail. |
885 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
886 |
> |
* current testcase will fail. |
887 |
|
*/ |
888 |
|
public void threadAssertNull(Object x) { |
889 |
|
try { |
890 |
|
assertNull(x); |
891 |
< |
} catch (AssertionFailedError t) { |
892 |
< |
threadRecordFailure(t); |
893 |
< |
throw t; |
891 |
> |
} catch (AssertionError fail) { |
892 |
> |
threadRecordFailure(fail); |
893 |
> |
throw fail; |
894 |
|
} |
895 |
|
} |
896 |
|
|
897 |
|
/** |
898 |
|
* Just like assertEquals(x, y), but additionally recording (using |
899 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
900 |
< |
* the current testcase will fail. |
899 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
900 |
> |
* current testcase will fail. |
901 |
|
*/ |
902 |
|
public void threadAssertEquals(long x, long y) { |
903 |
|
try { |
904 |
|
assertEquals(x, y); |
905 |
< |
} catch (AssertionFailedError t) { |
906 |
< |
threadRecordFailure(t); |
907 |
< |
throw t; |
905 |
> |
} catch (AssertionError fail) { |
906 |
> |
threadRecordFailure(fail); |
907 |
> |
throw fail; |
908 |
|
} |
909 |
|
} |
910 |
|
|
911 |
|
/** |
912 |
|
* Just like assertEquals(x, y), but additionally recording (using |
913 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
914 |
< |
* the current testcase will fail. |
913 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
914 |
> |
* current testcase will fail. |
915 |
|
*/ |
916 |
|
public void threadAssertEquals(Object x, Object y) { |
917 |
|
try { |
918 |
|
assertEquals(x, y); |
919 |
< |
} catch (AssertionFailedError fail) { |
919 |
> |
} catch (AssertionError fail) { |
920 |
|
threadRecordFailure(fail); |
921 |
|
throw fail; |
922 |
|
} catch (Throwable fail) { |
926 |
|
|
927 |
|
/** |
928 |
|
* Just like assertSame(x, y), but additionally recording (using |
929 |
< |
* threadRecordFailure) any AssertionFailedError thrown, so that |
930 |
< |
* the current testcase will fail. |
929 |
> |
* threadRecordFailure) any AssertionError thrown, so that the |
930 |
> |
* current testcase will fail. |
931 |
|
*/ |
932 |
|
public void threadAssertSame(Object x, Object y) { |
933 |
|
try { |
934 |
|
assertSame(x, y); |
935 |
< |
} catch (AssertionFailedError fail) { |
935 |
> |
} catch (AssertionError fail) { |
936 |
|
threadRecordFailure(fail); |
937 |
|
throw fail; |
938 |
|
} |
954 |
|
|
955 |
|
/** |
956 |
|
* Records the given exception using {@link #threadRecordFailure}, |
957 |
< |
* then rethrows the exception, wrapping it in an |
958 |
< |
* AssertionFailedError if necessary. |
957 |
> |
* then rethrows the exception, wrapping it in an AssertionError |
958 |
> |
* if necessary. |
959 |
|
*/ |
960 |
|
public void threadUnexpectedException(Throwable t) { |
961 |
|
threadRecordFailure(t); |
964 |
|
throw (RuntimeException) t; |
965 |
|
else if (t instanceof Error) |
966 |
|
throw (Error) t; |
967 |
< |
else { |
968 |
< |
AssertionFailedError afe = |
922 |
< |
new AssertionFailedError("unexpected exception: " + t); |
923 |
< |
afe.initCause(t); |
924 |
< |
throw afe; |
925 |
< |
} |
967 |
> |
else |
968 |
> |
throw new AssertionError("unexpected exception: " + t, t); |
969 |
|
} |
970 |
|
|
971 |
|
/** |
1094 |
|
} |
1095 |
|
} |
1096 |
|
|
1097 |
+ |
/** Returns true if thread info might be useful in a thread dump. */ |
1098 |
+ |
static boolean threadOfInterest(ThreadInfo info) { |
1099 |
+ |
final String name = info.getThreadName(); |
1100 |
+ |
String lockName; |
1101 |
+ |
if (name == null) |
1102 |
+ |
return true; |
1103 |
+ |
if (name.equals("Signal Dispatcher") |
1104 |
+ |
|| name.equals("WedgedTestDetector")) |
1105 |
+ |
return false; |
1106 |
+ |
if (name.equals("Reference Handler")) { |
1107 |
+ |
// Reference Handler stacktrace changed in JDK-8156500 |
1108 |
+ |
StackTraceElement[] stackTrace; String methodName; |
1109 |
+ |
if ((stackTrace = info.getStackTrace()) != null |
1110 |
+ |
&& stackTrace.length > 0 |
1111 |
+ |
&& (methodName = stackTrace[0].getMethodName()) != null |
1112 |
+ |
&& methodName.equals("waitForReferencePendingList")) |
1113 |
+ |
return false; |
1114 |
+ |
// jdk8 Reference Handler stacktrace |
1115 |
+ |
if ((lockName = info.getLockName()) != null |
1116 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1117 |
+ |
return false; |
1118 |
+ |
} |
1119 |
+ |
if ((name.equals("Finalizer") || name.equals("Common-Cleaner")) |
1120 |
+ |
&& (lockName = info.getLockName()) != null |
1121 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1122 |
+ |
return false; |
1123 |
+ |
if (name.startsWith("ForkJoinPool.commonPool-worker") |
1124 |
+ |
&& (lockName = info.getLockName()) != null |
1125 |
+ |
&& lockName.startsWith("java.util.concurrent.ForkJoinPool")) |
1126 |
+ |
return false; |
1127 |
+ |
return true; |
1128 |
+ |
} |
1129 |
+ |
|
1130 |
|
/** |
1131 |
|
* A debugging tool to print stack traces of most threads, as jstack does. |
1132 |
|
* Uninteresting threads are filtered out. |
1141 |
|
} |
1142 |
|
} |
1143 |
|
|
1068 |
– |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1144 |
|
System.err.println("------ stacktrace dump start ------"); |
1145 |
< |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1146 |
< |
final String name = info.getThreadName(); |
1147 |
< |
String lockName; |
1073 |
< |
if ("Signal Dispatcher".equals(name)) |
1074 |
< |
continue; |
1075 |
< |
if ("Reference Handler".equals(name) |
1076 |
< |
&& (lockName = info.getLockName()) != null |
1077 |
< |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1078 |
< |
continue; |
1079 |
< |
if ("Finalizer".equals(name) |
1080 |
< |
&& (lockName = info.getLockName()) != null |
1081 |
< |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1082 |
< |
continue; |
1083 |
< |
if ("checkForWedgedTest".equals(name)) |
1084 |
< |
continue; |
1085 |
< |
System.err.print(info); |
1086 |
< |
} |
1145 |
> |
for (ThreadInfo info : THREAD_MXBEAN.dumpAllThreads(true, true)) |
1146 |
> |
if (threadOfInterest(info)) |
1147 |
> |
System.err.print(info); |
1148 |
|
System.err.println("------ stacktrace dump end ------"); |
1149 |
|
|
1150 |
|
if (sm != null) System.setSecurityManager(sm); |
1159 |
|
for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) { |
1160 |
|
try { delay(1); } |
1161 |
|
catch (InterruptedException fail) { |
1162 |
< |
fail("Unexpected InterruptedException"); |
1162 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1163 |
|
} |
1164 |
|
Thread.State s = thread.getState(); |
1165 |
|
if (s == expected) |
1171 |
|
} |
1172 |
|
|
1173 |
|
/** |
1174 |
+ |
* Returns the thread's blocker's class name, if any, else null. |
1175 |
+ |
*/ |
1176 |
+ |
String blockerClassName(Thread thread) { |
1177 |
+ |
ThreadInfo threadInfo; LockInfo lockInfo; |
1178 |
+ |
if ((threadInfo = THREAD_MXBEAN.getThreadInfo(thread.getId(), 0)) != null |
1179 |
+ |
&& (lockInfo = threadInfo.getLockInfo()) != null) |
1180 |
+ |
return lockInfo.getClassName(); |
1181 |
+ |
return null; |
1182 |
+ |
} |
1183 |
+ |
|
1184 |
+ |
/** |
1185 |
|
* Checks that future.get times out, with the default timeout of |
1186 |
|
* {@code timeoutMillis()}. |
1187 |
|
*/ |
1356 |
|
|
1357 |
|
/** |
1358 |
|
* Sleeps until the given time has elapsed. |
1359 |
< |
* Throws AssertionFailedError if interrupted. |
1359 |
> |
* Throws AssertionError if interrupted. |
1360 |
|
*/ |
1361 |
|
static void sleep(long millis) { |
1362 |
|
try { |
1363 |
|
delay(millis); |
1364 |
|
} catch (InterruptedException fail) { |
1365 |
< |
AssertionFailedError afe = |
1294 |
< |
new AssertionFailedError("Unexpected InterruptedException"); |
1295 |
< |
afe.initCause(fail); |
1296 |
< |
throw afe; |
1365 |
> |
throw new AssertionError("Unexpected InterruptedException", fail); |
1366 |
|
} |
1367 |
|
} |
1368 |
|
|
1369 |
|
/** |
1370 |
|
* Spin-waits up to the specified number of milliseconds for the given |
1371 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1372 |
+ |
* @param waitingForGodot if non-null, an additional condition to satisfy |
1373 |
|
*/ |
1374 |
< |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1375 |
< |
long startTime = 0L; |
1376 |
< |
for (;;) { |
1377 |
< |
Thread.State s = thread.getState(); |
1378 |
< |
if (s == Thread.State.BLOCKED || |
1379 |
< |
s == Thread.State.WAITING || |
1380 |
< |
s == Thread.State.TIMED_WAITING) |
1381 |
< |
return; |
1382 |
< |
else if (s == Thread.State.TERMINATED) |
1374 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis, |
1375 |
> |
Callable<Boolean> waitingForGodot) { |
1376 |
> |
for (long startTime = 0L;;) { |
1377 |
> |
switch (thread.getState()) { |
1378 |
> |
default: break; |
1379 |
> |
case BLOCKED: case WAITING: case TIMED_WAITING: |
1380 |
> |
try { |
1381 |
> |
if (waitingForGodot == null || waitingForGodot.call()) |
1382 |
> |
return; |
1383 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1384 |
> |
break; |
1385 |
> |
case TERMINATED: |
1386 |
|
fail("Unexpected thread termination"); |
1387 |
< |
else if (startTime == 0L) |
1387 |
> |
} |
1388 |
> |
|
1389 |
> |
if (startTime == 0L) |
1390 |
|
startTime = System.nanoTime(); |
1391 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1392 |
< |
threadAssertTrue(thread.isAlive()); |
1393 |
< |
fail("timed out waiting for thread to enter wait state"); |
1392 |
> |
assertTrue(thread.isAlive()); |
1393 |
> |
if (waitingForGodot == null |
1394 |
> |
|| thread.getState() == Thread.State.RUNNABLE) |
1395 |
> |
fail("timed out waiting for thread to enter wait state"); |
1396 |
> |
else |
1397 |
> |
fail("timed out waiting for condition, thread state=" |
1398 |
> |
+ thread.getState()); |
1399 |
|
} |
1400 |
|
Thread.yield(); |
1401 |
|
} |
1403 |
|
|
1404 |
|
/** |
1405 |
|
* Spin-waits up to the specified number of milliseconds for the given |
1406 |
< |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1327 |
< |
* and additionally satisfy the given condition. |
1406 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1407 |
|
*/ |
1408 |
< |
void waitForThreadToEnterWaitState( |
1409 |
< |
Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) { |
1331 |
< |
long startTime = 0L; |
1332 |
< |
for (;;) { |
1333 |
< |
Thread.State s = thread.getState(); |
1334 |
< |
if (s == Thread.State.BLOCKED || |
1335 |
< |
s == Thread.State.WAITING || |
1336 |
< |
s == Thread.State.TIMED_WAITING) { |
1337 |
< |
try { |
1338 |
< |
if (waitingForGodot.call()) |
1339 |
< |
return; |
1340 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1341 |
< |
} |
1342 |
< |
else if (s == Thread.State.TERMINATED) |
1343 |
< |
fail("Unexpected thread termination"); |
1344 |
< |
else if (startTime == 0L) |
1345 |
< |
startTime = System.nanoTime(); |
1346 |
< |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1347 |
< |
threadAssertTrue(thread.isAlive()); |
1348 |
< |
fail("timed out waiting for thread to enter wait state"); |
1349 |
< |
} |
1350 |
< |
Thread.yield(); |
1351 |
< |
} |
1408 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1409 |
> |
waitForThreadToEnterWaitState(thread, timeoutMillis, null); |
1410 |
|
} |
1411 |
|
|
1412 |
|
/** |
1414 |
|
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1415 |
|
*/ |
1416 |
|
void waitForThreadToEnterWaitState(Thread thread) { |
1417 |
< |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
1417 |
> |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, null); |
1418 |
|
} |
1419 |
|
|
1420 |
|
/** |
1422 |
|
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1423 |
|
* and additionally satisfy the given condition. |
1424 |
|
*/ |
1425 |
< |
void waitForThreadToEnterWaitState( |
1426 |
< |
Thread thread, Callable<Boolean> waitingForGodot) { |
1425 |
> |
void waitForThreadToEnterWaitState(Thread thread, |
1426 |
> |
Callable<Boolean> waitingForGodot) { |
1427 |
|
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot); |
1428 |
|
} |
1429 |
|
|
1430 |
|
/** |
1431 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the current thread to |
1432 |
+ |
* be interrupted. Clears the interrupt status before returning. |
1433 |
+ |
*/ |
1434 |
+ |
void awaitInterrupted() { |
1435 |
+ |
for (long startTime = 0L; !Thread.interrupted(); ) { |
1436 |
+ |
if (startTime == 0L) |
1437 |
+ |
startTime = System.nanoTime(); |
1438 |
+ |
else if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
1439 |
+ |
fail("timed out waiting for thread interrupt"); |
1440 |
+ |
Thread.yield(); |
1441 |
+ |
} |
1442 |
+ |
} |
1443 |
+ |
|
1444 |
+ |
/** |
1445 |
|
* Returns the number of milliseconds since time given by |
1446 |
|
* startNanoTime, which must have been previously returned from a |
1447 |
|
* call to {@link System#nanoTime()}. |
1450 |
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
1451 |
|
} |
1452 |
|
|
1381 |
– |
// void assertTerminatesPromptly(long timeoutMillis, Runnable r) { |
1382 |
– |
// long startTime = System.nanoTime(); |
1383 |
– |
// try { |
1384 |
– |
// r.run(); |
1385 |
– |
// } catch (Throwable fail) { threadUnexpectedException(fail); } |
1386 |
– |
// if (millisElapsedSince(startTime) > timeoutMillis/2) |
1387 |
– |
// throw new AssertionFailedError("did not return promptly"); |
1388 |
– |
// } |
1389 |
– |
|
1390 |
– |
// void assertTerminatesPromptly(Runnable r) { |
1391 |
– |
// assertTerminatesPromptly(LONG_DELAY_MS/2, r); |
1392 |
– |
// } |
1393 |
– |
|
1453 |
|
/** |
1454 |
|
* Checks that timed f.get() returns the expected value, and does not |
1455 |
|
* wait for the timeout to elapse before returning. |
1456 |
|
*/ |
1457 |
|
<T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) { |
1458 |
|
long startTime = System.nanoTime(); |
1459 |
+ |
T actual = null; |
1460 |
|
try { |
1461 |
< |
assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS)); |
1461 |
> |
actual = f.get(timeoutMillis, MILLISECONDS); |
1462 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1463 |
+ |
assertEquals(expectedValue, actual); |
1464 |
|
if (millisElapsedSince(startTime) > timeoutMillis/2) |
1465 |
< |
throw new AssertionFailedError("timed get did not return promptly"); |
1465 |
> |
throw new AssertionError("timed get did not return promptly"); |
1466 |
|
} |
1467 |
|
|
1468 |
|
<T> void checkTimedGet(Future<T> f, T expectedValue) { |
1480 |
|
} |
1481 |
|
|
1482 |
|
/** |
1483 |
+ |
* Returns a new started daemon Thread running the given action, |
1484 |
+ |
* wrapped in a CheckedRunnable. |
1485 |
+ |
*/ |
1486 |
+ |
Thread newStartedThread(Action action) { |
1487 |
+ |
return newStartedThread(checkedRunnable(action)); |
1488 |
+ |
} |
1489 |
+ |
|
1490 |
+ |
/** |
1491 |
|
* Waits for the specified time (in milliseconds) for the thread |
1492 |
|
* to terminate (using {@link Thread#join(long)}), else interrupts |
1493 |
|
* the thread (in the hope that it may terminate later) and fails. |
1494 |
|
*/ |
1495 |
< |
void awaitTermination(Thread t, long timeoutMillis) { |
1495 |
> |
void awaitTermination(Thread thread, long timeoutMillis) { |
1496 |
|
try { |
1497 |
< |
t.join(timeoutMillis); |
1497 |
> |
thread.join(timeoutMillis); |
1498 |
|
} catch (InterruptedException fail) { |
1499 |
|
threadUnexpectedException(fail); |
1500 |
< |
} finally { |
1501 |
< |
if (t.getState() != Thread.State.TERMINATED) { |
1502 |
< |
t.interrupt(); |
1503 |
< |
threadFail("timed out waiting for thread to terminate"); |
1500 |
> |
} |
1501 |
> |
if (thread.getState() != Thread.State.TERMINATED) { |
1502 |
> |
String detail = String.format( |
1503 |
> |
"timed out waiting for thread to terminate, thread=%s, state=%s" , |
1504 |
> |
thread, thread.getState()); |
1505 |
> |
try { |
1506 |
> |
threadFail(detail); |
1507 |
> |
} finally { |
1508 |
> |
// Interrupt thread __after__ having reported its stack trace |
1509 |
> |
thread.interrupt(); |
1510 |
|
} |
1511 |
|
} |
1512 |
|
} |
1534 |
|
} |
1535 |
|
} |
1536 |
|
|
1537 |
< |
public abstract class RunnableShouldThrow implements Runnable { |
1538 |
< |
protected abstract void realRun() throws Throwable; |
1539 |
< |
|
1540 |
< |
final Class<?> exceptionClass; |
1541 |
< |
|
1467 |
< |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
1468 |
< |
this.exceptionClass = exceptionClass; |
1469 |
< |
} |
1470 |
< |
|
1471 |
< |
public final void run() { |
1472 |
< |
try { |
1473 |
< |
realRun(); |
1474 |
< |
threadShouldThrow(exceptionClass.getSimpleName()); |
1475 |
< |
} catch (Throwable t) { |
1476 |
< |
if (! exceptionClass.isInstance(t)) |
1477 |
< |
threadUnexpectedException(t); |
1478 |
< |
} |
1479 |
< |
} |
1537 |
> |
Runnable checkedRunnable(Action action) { |
1538 |
> |
return new CheckedRunnable() { |
1539 |
> |
public void realRun() throws Throwable { |
1540 |
> |
action.run(); |
1541 |
> |
}}; |
1542 |
|
} |
1543 |
|
|
1544 |
|
public abstract class ThreadShouldThrow extends Thread { |
1553 |
|
public final void run() { |
1554 |
|
try { |
1555 |
|
realRun(); |
1494 |
– |
threadShouldThrow(exceptionClass.getSimpleName()); |
1556 |
|
} catch (Throwable t) { |
1557 |
|
if (! exceptionClass.isInstance(t)) |
1558 |
|
threadUnexpectedException(t); |
1559 |
+ |
return; |
1560 |
|
} |
1561 |
+ |
threadShouldThrow(exceptionClass.getSimpleName()); |
1562 |
|
} |
1563 |
|
} |
1564 |
|
|
1568 |
|
public final void run() { |
1569 |
|
try { |
1570 |
|
realRun(); |
1508 |
– |
threadShouldThrow("InterruptedException"); |
1571 |
|
} catch (InterruptedException success) { |
1572 |
|
threadAssertFalse(Thread.interrupted()); |
1573 |
+ |
return; |
1574 |
|
} catch (Throwable fail) { |
1575 |
|
threadUnexpectedException(fail); |
1576 |
|
} |
1577 |
+ |
threadShouldThrow("InterruptedException"); |
1578 |
|
} |
1579 |
|
} |
1580 |
|
|
1586 |
|
return realCall(); |
1587 |
|
} catch (Throwable fail) { |
1588 |
|
threadUnexpectedException(fail); |
1525 |
– |
return null; |
1589 |
|
} |
1590 |
< |
} |
1528 |
< |
} |
1529 |
< |
|
1530 |
< |
public abstract class CheckedInterruptedCallable<T> |
1531 |
< |
implements Callable<T> { |
1532 |
< |
protected abstract T realCall() throws Throwable; |
1533 |
< |
|
1534 |
< |
public final T call() { |
1535 |
< |
try { |
1536 |
< |
T result = realCall(); |
1537 |
< |
threadShouldThrow("InterruptedException"); |
1538 |
< |
return result; |
1539 |
< |
} catch (InterruptedException success) { |
1540 |
< |
threadAssertFalse(Thread.interrupted()); |
1541 |
< |
} catch (Throwable fail) { |
1542 |
< |
threadUnexpectedException(fail); |
1543 |
< |
} |
1544 |
< |
return null; |
1590 |
> |
throw new AssertionError("unreached"); |
1591 |
|
} |
1592 |
|
} |
1593 |
|
|
1644 |
|
} |
1645 |
|
|
1646 |
|
public void await(CountDownLatch latch, long timeoutMillis) { |
1647 |
+ |
boolean timedOut = false; |
1648 |
|
try { |
1649 |
< |
if (!latch.await(timeoutMillis, MILLISECONDS)) |
1603 |
< |
fail("timed out waiting for CountDownLatch for " |
1604 |
< |
+ (timeoutMillis/1000) + " sec"); |
1649 |
> |
timedOut = !latch.await(timeoutMillis, MILLISECONDS); |
1650 |
|
} catch (Throwable fail) { |
1651 |
|
threadUnexpectedException(fail); |
1652 |
|
} |
1653 |
+ |
if (timedOut) |
1654 |
+ |
fail("timed out waiting for CountDownLatch for " |
1655 |
+ |
+ (timeoutMillis/1000) + " sec"); |
1656 |
|
} |
1657 |
|
|
1658 |
|
public void await(CountDownLatch latch) { |
1660 |
|
} |
1661 |
|
|
1662 |
|
public void await(Semaphore semaphore) { |
1663 |
+ |
boolean timedOut = false; |
1664 |
|
try { |
1665 |
< |
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1617 |
< |
fail("timed out waiting for Semaphore for " |
1618 |
< |
+ (LONG_DELAY_MS/1000) + " sec"); |
1665 |
> |
timedOut = !semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS); |
1666 |
|
} catch (Throwable fail) { |
1667 |
|
threadUnexpectedException(fail); |
1668 |
|
} |
1669 |
+ |
if (timedOut) |
1670 |
+ |
fail("timed out waiting for Semaphore for " |
1671 |
+ |
+ (LONG_DELAY_MS/1000) + " sec"); |
1672 |
|
} |
1673 |
|
|
1674 |
|
public void await(CyclicBarrier barrier) { |
1693 |
|
// long startTime = System.nanoTime(); |
1694 |
|
// while (!flag.get()) { |
1695 |
|
// if (millisElapsedSince(startTime) > timeoutMillis) |
1696 |
< |
// throw new AssertionFailedError("timed out"); |
1696 |
> |
// throw new AssertionError("timed out"); |
1697 |
|
// Thread.yield(); |
1698 |
|
// } |
1699 |
|
// } |
1702 |
|
public String call() { throw new NullPointerException(); } |
1703 |
|
} |
1704 |
|
|
1655 |
– |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1656 |
– |
protected void realRun() { |
1657 |
– |
try { |
1658 |
– |
delay(SMALL_DELAY_MS); |
1659 |
– |
} catch (InterruptedException ok) {} |
1660 |
– |
} |
1661 |
– |
} |
1662 |
– |
|
1705 |
|
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1706 |
|
return new CheckedRunnable() { |
1707 |
|
protected void realRun() { |
1757 |
|
return realCompute(); |
1758 |
|
} catch (Throwable fail) { |
1759 |
|
threadUnexpectedException(fail); |
1718 |
– |
return null; |
1760 |
|
} |
1761 |
+ |
throw new AssertionError("unreached"); |
1762 |
|
} |
1763 |
|
} |
1764 |
|
|
1772 |
|
|
1773 |
|
/** |
1774 |
|
* A CyclicBarrier that uses timed await and fails with |
1775 |
< |
* AssertionFailedErrors instead of throwing checked exceptions. |
1775 |
> |
* AssertionErrors instead of throwing checked exceptions. |
1776 |
|
*/ |
1777 |
|
public static class CheckedBarrier extends CyclicBarrier { |
1778 |
|
public CheckedBarrier(int parties) { super(parties); } |
1779 |
|
|
1780 |
|
public int await() { |
1781 |
|
try { |
1782 |
< |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1782 |
> |
return super.await(LONGER_DELAY_MS, MILLISECONDS); |
1783 |
|
} catch (TimeoutException timedOut) { |
1784 |
< |
throw new AssertionFailedError("timed out"); |
1784 |
> |
throw new AssertionError("timed out"); |
1785 |
|
} catch (Exception fail) { |
1786 |
< |
AssertionFailedError afe = |
1745 |
< |
new AssertionFailedError("Unexpected exception: " + fail); |
1746 |
< |
afe.initCause(fail); |
1747 |
< |
throw afe; |
1786 |
> |
throw new AssertionError("Unexpected exception: " + fail, fail); |
1787 |
|
} |
1788 |
|
} |
1789 |
|
} |
1835 |
|
} |
1836 |
|
} |
1837 |
|
|
1838 |
< |
void assertImmutable(final Object o) { |
1838 |
> |
void assertImmutable(Object o) { |
1839 |
|
if (o instanceof Collection) { |
1840 |
|
assertThrows( |
1841 |
|
UnsupportedOperationException.class, |
1842 |
< |
new Runnable() { public void run() { |
1804 |
< |
((Collection) o).add(null);}}); |
1842 |
> |
() -> ((Collection) o).add(null)); |
1843 |
|
} |
1844 |
|
} |
1845 |
|
|
1846 |
|
@SuppressWarnings("unchecked") |
1847 |
|
<T> T serialClone(T o) { |
1848 |
+ |
T clone = null; |
1849 |
|
try { |
1850 |
|
ObjectInputStream ois = new ObjectInputStream |
1851 |
|
(new ByteArrayInputStream(serialBytes(o))); |
1852 |
< |
T clone = (T) ois.readObject(); |
1814 |
< |
if (o == clone) assertImmutable(o); |
1815 |
< |
assertSame(o.getClass(), clone.getClass()); |
1816 |
< |
return clone; |
1852 |
> |
clone = (T) ois.readObject(); |
1853 |
|
} catch (Throwable fail) { |
1854 |
|
threadUnexpectedException(fail); |
1819 |
– |
return null; |
1855 |
|
} |
1856 |
+ |
if (o == clone) assertImmutable(o); |
1857 |
+ |
else assertSame(o.getClass(), clone.getClass()); |
1858 |
+ |
return clone; |
1859 |
|
} |
1860 |
|
|
1861 |
|
/** |
1874 |
|
(new ByteArrayInputStream(bos.toByteArray())); |
1875 |
|
T clone = (T) ois.readObject(); |
1876 |
|
if (o == clone) assertImmutable(o); |
1877 |
< |
assertSame(o.getClass(), clone.getClass()); |
1877 |
> |
else assertSame(o.getClass(), clone.getClass()); |
1878 |
|
return clone; |
1879 |
|
} |
1880 |
|
|
1899 |
|
} |
1900 |
|
|
1901 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1902 |
< |
Runnable... throwingActions) { |
1903 |
< |
for (Runnable throwingAction : throwingActions) { |
1902 |
> |
Action... throwingActions) { |
1903 |
> |
for (Action throwingAction : throwingActions) { |
1904 |
|
boolean threw = false; |
1905 |
|
try { throwingAction.run(); } |
1906 |
|
catch (Throwable t) { |
1907 |
|
threw = true; |
1908 |
< |
if (!expectedExceptionClass.isInstance(t)) { |
1909 |
< |
AssertionFailedError afe = |
1910 |
< |
new AssertionFailedError |
1911 |
< |
("Expected " + expectedExceptionClass.getName() + |
1912 |
< |
", got " + t.getClass().getName()); |
1875 |
< |
afe.initCause(t); |
1876 |
< |
threadUnexpectedException(afe); |
1877 |
< |
} |
1908 |
> |
if (!expectedExceptionClass.isInstance(t)) |
1909 |
> |
throw new AssertionError( |
1910 |
> |
"Expected " + expectedExceptionClass.getName() + |
1911 |
> |
", got " + t.getClass().getName(), |
1912 |
> |
t); |
1913 |
|
} |
1914 |
|
if (!threw) |
1915 |
|
shouldThrow(expectedExceptionClass.getName()); |
2125 |
|
assertEquals(savedCompletedTaskCount, p.getCompletedTaskCount()); |
2126 |
|
assertEquals(savedQueueSize, p.getQueue().size()); |
2127 |
|
} |
2128 |
+ |
|
2129 |
+ |
void assertCollectionsEquals(Collection<?> x, Collection<?> y) { |
2130 |
+ |
assertEquals(x, y); |
2131 |
+ |
assertEquals(y, x); |
2132 |
+ |
assertEquals(x.isEmpty(), y.isEmpty()); |
2133 |
+ |
assertEquals(x.size(), y.size()); |
2134 |
+ |
if (x instanceof List) { |
2135 |
+ |
assertEquals(x.toString(), y.toString()); |
2136 |
+ |
} |
2137 |
+ |
if (x instanceof List || x instanceof Set) { |
2138 |
+ |
assertEquals(x.hashCode(), y.hashCode()); |
2139 |
+ |
} |
2140 |
+ |
if (x instanceof List || x instanceof Deque) { |
2141 |
+ |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
2142 |
+ |
assertTrue(Arrays.equals(x.toArray(new Object[0]), |
2143 |
+ |
y.toArray(new Object[0]))); |
2144 |
+ |
} |
2145 |
+ |
} |
2146 |
+ |
|
2147 |
+ |
/** |
2148 |
+ |
* A weaker form of assertCollectionsEquals which does not insist |
2149 |
+ |
* that the two collections satisfy Object#equals(Object), since |
2150 |
+ |
* they may use identity semantics as Deques do. |
2151 |
+ |
*/ |
2152 |
+ |
void assertCollectionsEquivalent(Collection<?> x, Collection<?> y) { |
2153 |
+ |
if (x instanceof List || x instanceof Set) |
2154 |
+ |
assertCollectionsEquals(x, y); |
2155 |
+ |
else { |
2156 |
+ |
assertEquals(x.isEmpty(), y.isEmpty()); |
2157 |
+ |
assertEquals(x.size(), y.size()); |
2158 |
+ |
assertEquals(new HashSet(x), new HashSet(y)); |
2159 |
+ |
if (x instanceof Deque) { |
2160 |
+ |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
2161 |
+ |
assertTrue(Arrays.equals(x.toArray(new Object[0]), |
2162 |
+ |
y.toArray(new Object[0]))); |
2163 |
+ |
} |
2164 |
+ |
} |
2165 |
+ |
} |
2166 |
|
} |