6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
+ |
/* |
10 |
+ |
* @test |
11 |
+ |
* @summary JSR-166 tck tests (conformance testing mode) |
12 |
+ |
* @build * |
13 |
+ |
* @modules java.management |
14 |
+ |
* @run junit/othervm/timeout=1000 JSR166TestCase |
15 |
+ |
*/ |
16 |
+ |
|
17 |
+ |
/* |
18 |
+ |
* @test |
19 |
+ |
* @summary JSR-166 tck tests (whitebox tests allowed) |
20 |
+ |
* @build * |
21 |
+ |
* @modules java.base/java.util.concurrent:open |
22 |
+ |
* java.base/java.lang:open |
23 |
+ |
* java.management |
24 |
+ |
* @run junit/othervm/timeout=1000 |
25 |
+ |
* -Djsr166.testImplementationDetails=true |
26 |
+ |
* JSR166TestCase |
27 |
+ |
* @run junit/othervm/timeout=1000 |
28 |
+ |
* -Djsr166.testImplementationDetails=true |
29 |
+ |
* -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 |
30 |
+ |
* JSR166TestCase |
31 |
+ |
* @run junit/othervm/timeout=1000 |
32 |
+ |
* -Djsr166.testImplementationDetails=true |
33 |
+ |
* -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 |
34 |
+ |
* -Djava.util.secureRandomSeed=true |
35 |
+ |
* JSR166TestCase |
36 |
+ |
* @run junit/othervm/timeout=1000/policy=tck.policy |
37 |
+ |
* -Djsr166.testImplementationDetails=true |
38 |
+ |
* JSR166TestCase |
39 |
+ |
*/ |
40 |
+ |
|
41 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
42 |
|
import static java.util.concurrent.TimeUnit.MINUTES; |
43 |
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
52 |
|
import java.lang.reflect.Constructor; |
53 |
|
import java.lang.reflect.Method; |
54 |
|
import java.lang.reflect.Modifier; |
23 |
– |
import java.nio.file.Files; |
24 |
– |
import java.nio.file.Paths; |
55 |
|
import java.security.CodeSource; |
56 |
|
import java.security.Permission; |
57 |
|
import java.security.PermissionCollection; |
61 |
|
import java.security.SecurityPermission; |
62 |
|
import java.util.ArrayList; |
63 |
|
import java.util.Arrays; |
64 |
+ |
import java.util.Collection; |
65 |
+ |
import java.util.Collections; |
66 |
|
import java.util.Date; |
67 |
|
import java.util.Enumeration; |
68 |
|
import java.util.Iterator; |
82 |
|
import java.util.concurrent.RecursiveTask; |
83 |
|
import java.util.concurrent.RejectedExecutionHandler; |
84 |
|
import java.util.concurrent.Semaphore; |
85 |
+ |
import java.util.concurrent.SynchronousQueue; |
86 |
|
import java.util.concurrent.ThreadFactory; |
87 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
88 |
|
import java.util.concurrent.ThreadPoolExecutor; |
89 |
|
import java.util.concurrent.TimeoutException; |
90 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
91 |
|
import java.util.concurrent.atomic.AtomicReference; |
57 |
– |
import java.util.regex.Matcher; |
92 |
|
import java.util.regex.Pattern; |
93 |
|
|
94 |
|
import junit.framework.AssertionFailedError; |
146 |
|
* methods as there are exceptions the method can throw. Sometimes |
147 |
|
* there are multiple tests per JSR166 method when the different |
148 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
149 |
< |
* cover multiple methods when they cannot be tested in |
116 |
< |
* isolation. |
149 |
> |
* cover multiple methods when they cannot be tested in isolation. |
150 |
|
* |
151 |
|
* <li>The documentation style for testcases is to provide as javadoc |
152 |
|
* a simple sentence or two describing the property that the testcase |
209 |
|
private static final int suiteRuns = |
210 |
|
Integer.getInteger("jsr166.suiteRuns", 1); |
211 |
|
|
212 |
+ |
/** |
213 |
+ |
* Returns the value of the system property, or NaN if not defined. |
214 |
+ |
*/ |
215 |
+ |
private static float systemPropertyValue(String name) { |
216 |
+ |
String floatString = System.getProperty(name); |
217 |
+ |
if (floatString == null) |
218 |
+ |
return Float.NaN; |
219 |
+ |
try { |
220 |
+ |
return Float.parseFloat(floatString); |
221 |
+ |
} catch (NumberFormatException ex) { |
222 |
+ |
throw new IllegalArgumentException( |
223 |
+ |
String.format("Bad float value in system property %s=%s", |
224 |
+ |
name, floatString)); |
225 |
+ |
} |
226 |
+ |
} |
227 |
+ |
|
228 |
+ |
/** |
229 |
+ |
* The scaling factor to apply to standard delays used in tests. |
230 |
+ |
* May be initialized from any of: |
231 |
+ |
* - the "jsr166.delay.factor" system property |
232 |
+ |
* - the "test.timeout.factor" system property (as used by jtreg) |
233 |
+ |
* See: http://openjdk.java.net/jtreg/tag-spec.html |
234 |
+ |
* - hard-coded fuzz factor when using a known slowpoke VM |
235 |
+ |
*/ |
236 |
+ |
private static final float delayFactor = delayFactor(); |
237 |
+ |
|
238 |
+ |
private static float delayFactor() { |
239 |
+ |
float x; |
240 |
+ |
if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor"))) |
241 |
+ |
return x; |
242 |
+ |
if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor"))) |
243 |
+ |
return x; |
244 |
+ |
String prop = System.getProperty("java.vm.version"); |
245 |
+ |
if (prop != null && prop.matches(".*debug.*")) |
246 |
+ |
return 4.0f; // How much slower is fastdebug than product?! |
247 |
+ |
return 1.0f; |
248 |
+ |
} |
249 |
+ |
|
250 |
|
public JSR166TestCase() { super(); } |
251 |
|
public JSR166TestCase(String name) { super(name); } |
252 |
|
|
298 |
|
|
299 |
|
// public static String cpuModel() { |
300 |
|
// try { |
301 |
< |
// Matcher matcher = Pattern.compile("model name\\s*: (.*)") |
301 |
> |
// java.util.regex.Matcher matcher |
302 |
> |
// = Pattern.compile("model name\\s*: (.*)") |
303 |
|
// .matcher(new String( |
304 |
< |
// Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8")); |
304 |
> |
// java.nio.file.Files.readAllBytes( |
305 |
> |
// java.nio.file.Paths.get("/proc/cpuinfo")), "UTF-8")); |
306 |
|
// matcher.find(); |
307 |
|
// return matcher.group(1); |
308 |
|
// } catch (Exception ex) { return null; } |
346 |
|
main(suite(), args); |
347 |
|
} |
348 |
|
|
349 |
+ |
static class PithyResultPrinter extends junit.textui.ResultPrinter { |
350 |
+ |
PithyResultPrinter(java.io.PrintStream writer) { super(writer); } |
351 |
+ |
long runTime; |
352 |
+ |
public void startTest(Test test) {} |
353 |
+ |
protected void printHeader(long runTime) { |
354 |
+ |
this.runTime = runTime; // defer printing for later |
355 |
+ |
} |
356 |
+ |
protected void printFooter(TestResult result) { |
357 |
+ |
if (result.wasSuccessful()) { |
358 |
+ |
getWriter().println("OK (" + result.runCount() + " tests)" |
359 |
+ |
+ " Time: " + elapsedTimeAsString(runTime)); |
360 |
+ |
} else { |
361 |
+ |
getWriter().println("Time: " + elapsedTimeAsString(runTime)); |
362 |
+ |
super.printFooter(result); |
363 |
+ |
} |
364 |
+ |
} |
365 |
+ |
} |
366 |
+ |
|
367 |
+ |
/** |
368 |
+ |
* Returns a TestRunner that doesn't bother with unnecessary |
369 |
+ |
* fluff, like printing a "." for each test case. |
370 |
+ |
*/ |
371 |
+ |
static junit.textui.TestRunner newPithyTestRunner() { |
372 |
+ |
junit.textui.TestRunner runner = new junit.textui.TestRunner(); |
373 |
+ |
runner.setPrinter(new PithyResultPrinter(System.out)); |
374 |
+ |
return runner; |
375 |
+ |
} |
376 |
+ |
|
377 |
|
/** |
378 |
|
* Runs all unit tests in the given test suite. |
379 |
|
* Actual behavior influenced by jsr166.* system properties. |
385 |
|
System.setSecurityManager(new SecurityManager()); |
386 |
|
} |
387 |
|
for (int i = 0; i < suiteRuns; i++) { |
388 |
< |
TestResult result = junit.textui.TestRunner.run(suite); |
388 |
> |
TestResult result = newPithyTestRunner().doRun(suite); |
389 |
|
if (!result.wasSuccessful()) |
390 |
|
System.exit(1); |
391 |
|
System.gc(); |
469 |
|
AbstractQueuedLongSynchronizerTest.suite(), |
470 |
|
ArrayBlockingQueueTest.suite(), |
471 |
|
ArrayDequeTest.suite(), |
472 |
+ |
ArrayListTest.suite(), |
473 |
|
AtomicBooleanTest.suite(), |
474 |
|
AtomicIntegerArrayTest.suite(), |
475 |
|
AtomicIntegerFieldUpdaterTest.suite(), |
492 |
|
CopyOnWriteArrayListTest.suite(), |
493 |
|
CopyOnWriteArraySetTest.suite(), |
494 |
|
CountDownLatchTest.suite(), |
495 |
+ |
CountedCompleterTest.suite(), |
496 |
|
CyclicBarrierTest.suite(), |
497 |
|
DelayQueueTest.suite(), |
498 |
|
EntryTest.suite(), |
521 |
|
TreeMapTest.suite(), |
522 |
|
TreeSetTest.suite(), |
523 |
|
TreeSubMapTest.suite(), |
524 |
< |
TreeSubSetTest.suite()); |
524 |
> |
TreeSubSetTest.suite(), |
525 |
> |
VectorTest.suite()); |
526 |
|
|
527 |
|
// Java8+ test classes |
528 |
|
if (atLeastJava8()) { |
529 |
|
String[] java8TestClassNames = { |
530 |
+ |
"ArrayDeque8Test", |
531 |
|
"Atomic8Test", |
532 |
|
"CompletableFutureTest", |
533 |
|
"ConcurrentHashMap8Test", |
534 |
< |
"CountedCompleterTest", |
534 |
> |
"CountedCompleter8Test", |
535 |
|
"DoubleAccumulatorTest", |
536 |
|
"DoubleAdderTest", |
537 |
|
"ForkJoinPool8Test", |
538 |
|
"ForkJoinTask8Test", |
539 |
+ |
"LinkedBlockingDeque8Test", |
540 |
+ |
"LinkedBlockingQueue8Test", |
541 |
|
"LongAccumulatorTest", |
542 |
|
"LongAdderTest", |
543 |
|
"SplittableRandomTest", |
544 |
|
"StampedLockTest", |
545 |
|
"SubmissionPublisherTest", |
546 |
|
"ThreadLocalRandom8Test", |
547 |
+ |
"TimeUnit8Test", |
548 |
|
}; |
549 |
|
addNamedTestClasses(suite, java8TestClassNames); |
550 |
|
} |
552 |
|
// Java9+ test classes |
553 |
|
if (atLeastJava9()) { |
554 |
|
String[] java9TestClassNames = { |
555 |
< |
// Currently empty, but expecting varhandle tests |
555 |
> |
"AtomicBoolean9Test", |
556 |
> |
"AtomicInteger9Test", |
557 |
> |
"AtomicIntegerArray9Test", |
558 |
> |
"AtomicLong9Test", |
559 |
> |
"AtomicLongArray9Test", |
560 |
> |
"AtomicReference9Test", |
561 |
> |
"AtomicReferenceArray9Test", |
562 |
> |
"ExecutorCompletionService9Test", |
563 |
> |
"ForkJoinPool9Test", |
564 |
|
}; |
565 |
|
addNamedTestClasses(suite, java9TestClassNames); |
566 |
|
} |
571 |
|
/** Returns list of junit-style test method names in given class. */ |
572 |
|
public static ArrayList<String> testMethodNames(Class<?> testClass) { |
573 |
|
Method[] methods = testClass.getDeclaredMethods(); |
574 |
< |
ArrayList<String> names = new ArrayList<String>(methods.length); |
574 |
> |
ArrayList<String> names = new ArrayList<>(methods.length); |
575 |
|
for (Method method : methods) { |
576 |
|
if (method.getName().startsWith("test") |
577 |
|
&& Modifier.isPublic(method.getModifiers()) |
637 |
|
public static long LONG_DELAY_MS; |
638 |
|
|
639 |
|
/** |
640 |
< |
* Returns the shortest timed delay. This could |
641 |
< |
* be reimplemented to use for example a Property. |
640 |
> |
* Returns the shortest timed delay. This can be scaled up for |
641 |
> |
* slow machines using the jsr166.delay.factor system property, |
642 |
> |
* or via jtreg's -timeoutFactor: flag. |
643 |
> |
* http://openjdk.java.net/jtreg/command-help.html |
644 |
|
*/ |
645 |
|
protected long getShortDelay() { |
646 |
< |
return 50; |
646 |
> |
return (long) (50 * delayFactor); |
647 |
|
} |
648 |
|
|
649 |
|
/** |
677 |
|
* The first exception encountered if any threadAssertXXX method fails. |
678 |
|
*/ |
679 |
|
private final AtomicReference<Throwable> threadFailure |
680 |
< |
= new AtomicReference<Throwable>(null); |
680 |
> |
= new AtomicReference<>(null); |
681 |
|
|
682 |
|
/** |
683 |
|
* Records an exception so that it can be rethrown later in the test |
956 |
|
}}; |
957 |
|
} |
958 |
|
|
959 |
+ |
PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) { |
960 |
+ |
return new PoolCleanerWithReleaser(pool, releaser(flag)); |
961 |
+ |
} |
962 |
+ |
|
963 |
+ |
Runnable releaser(final AtomicBoolean flag) { |
964 |
+ |
return new Runnable() { public void run() { flag.set(true); }}; |
965 |
+ |
} |
966 |
+ |
|
967 |
|
/** |
968 |
|
* Waits out termination of a thread pool or fails doing so. |
969 |
|
*/ |
987 |
|
} |
988 |
|
} |
989 |
|
|
990 |
< |
/** Like Runnable, but with the freedom to throw anything */ |
990 |
> |
/** |
991 |
> |
* Like Runnable, but with the freedom to throw anything. |
992 |
> |
* junit folks had the same idea: |
993 |
> |
* http://junit.org/junit5/docs/snapshot/api/org/junit/gen5/api/Executable.html |
994 |
> |
*/ |
995 |
|
interface Action { public void run() throws Throwable; } |
996 |
|
|
997 |
|
/** |
1022 |
|
* Uninteresting threads are filtered out. |
1023 |
|
*/ |
1024 |
|
static void dumpTestThreads() { |
1025 |
+ |
SecurityManager sm = System.getSecurityManager(); |
1026 |
+ |
if (sm != null) { |
1027 |
+ |
try { |
1028 |
+ |
System.setSecurityManager(null); |
1029 |
+ |
} catch (SecurityException giveUp) { |
1030 |
+ |
return; |
1031 |
+ |
} |
1032 |
+ |
} |
1033 |
+ |
|
1034 |
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1035 |
|
System.err.println("------ stacktrace dump start ------"); |
1036 |
|
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1037 |
< |
String name = info.getThreadName(); |
1037 |
> |
final String name = info.getThreadName(); |
1038 |
> |
String lockName; |
1039 |
|
if ("Signal Dispatcher".equals(name)) |
1040 |
|
continue; |
1041 |
|
if ("Reference Handler".equals(name) |
1042 |
< |
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock")) |
1042 |
> |
&& (lockName = info.getLockName()) != null |
1043 |
> |
&& lockName.startsWith("java.lang.ref.Reference$Lock")) |
1044 |
|
continue; |
1045 |
|
if ("Finalizer".equals(name) |
1046 |
< |
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1046 |
> |
&& (lockName = info.getLockName()) != null |
1047 |
> |
&& lockName.startsWith("java.lang.ref.ReferenceQueue$Lock")) |
1048 |
|
continue; |
1049 |
|
if ("checkForWedgedTest".equals(name)) |
1050 |
|
continue; |
1051 |
|
System.err.print(info); |
1052 |
|
} |
1053 |
|
System.err.println("------ stacktrace dump end ------"); |
1054 |
+ |
|
1055 |
+ |
if (sm != null) System.setSecurityManager(sm); |
1056 |
|
} |
1057 |
|
|
1058 |
|
/** |
1239 |
|
} |
1240 |
|
public void refresh() {} |
1241 |
|
public String toString() { |
1242 |
< |
List<Permission> ps = new ArrayList<Permission>(); |
1242 |
> |
List<Permission> ps = new ArrayList<>(); |
1243 |
|
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
1244 |
|
ps.add(e.nextElement()); |
1245 |
|
return "AdjustablePolicy with permissions " + ps; |
1269 |
|
* Sleeps until the given time has elapsed. |
1270 |
|
* Throws AssertionFailedError if interrupted. |
1271 |
|
*/ |
1272 |
< |
void sleep(long millis) { |
1272 |
> |
static void sleep(long millis) { |
1273 |
|
try { |
1274 |
|
delay(millis); |
1275 |
|
} catch (InterruptedException fail) { |
1285 |
|
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1286 |
|
*/ |
1287 |
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
1288 |
< |
long startTime = System.nanoTime(); |
1288 |
> |
long startTime = 0L; |
1289 |
|
for (;;) { |
1290 |
|
Thread.State s = thread.getState(); |
1291 |
|
if (s == Thread.State.BLOCKED || |
1294 |
|
return; |
1295 |
|
else if (s == Thread.State.TERMINATED) |
1296 |
|
fail("Unexpected thread termination"); |
1297 |
+ |
else if (startTime == 0L) |
1298 |
+ |
startTime = System.nanoTime(); |
1299 |
|
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1300 |
|
threadAssertTrue(thread.isAlive()); |
1301 |
< |
return; |
1301 |
> |
fail("timed out waiting for thread to enter wait state"); |
1302 |
> |
} |
1303 |
> |
Thread.yield(); |
1304 |
> |
} |
1305 |
> |
} |
1306 |
> |
|
1307 |
> |
/** |
1308 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
1309 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1310 |
> |
* and additionally satisfy the given condition. |
1311 |
> |
*/ |
1312 |
> |
void waitForThreadToEnterWaitState( |
1313 |
> |
Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) { |
1314 |
> |
long startTime = 0L; |
1315 |
> |
for (;;) { |
1316 |
> |
Thread.State s = thread.getState(); |
1317 |
> |
if (s == Thread.State.BLOCKED || |
1318 |
> |
s == Thread.State.WAITING || |
1319 |
> |
s == Thread.State.TIMED_WAITING) { |
1320 |
> |
try { |
1321 |
> |
if (waitingForGodot.call()) |
1322 |
> |
return; |
1323 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
1324 |
> |
} |
1325 |
> |
else if (s == Thread.State.TERMINATED) |
1326 |
> |
fail("Unexpected thread termination"); |
1327 |
> |
else if (startTime == 0L) |
1328 |
> |
startTime = System.nanoTime(); |
1329 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
1330 |
> |
threadAssertTrue(thread.isAlive()); |
1331 |
> |
fail("timed out waiting for thread to enter wait state"); |
1332 |
|
} |
1333 |
|
Thread.yield(); |
1334 |
|
} |
1335 |
|
} |
1336 |
|
|
1337 |
|
/** |
1338 |
< |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
1339 |
< |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
1338 |
> |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1339 |
> |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
1340 |
|
*/ |
1341 |
|
void waitForThreadToEnterWaitState(Thread thread) { |
1342 |
|
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
1343 |
|
} |
1344 |
|
|
1345 |
|
/** |
1346 |
+ |
* Spin-waits up to LONG_DELAY_MS milliseconds for the given thread to |
1347 |
+ |
* enter a wait state: BLOCKED, WAITING, or TIMED_WAITING, |
1348 |
+ |
* and additionally satisfy the given condition. |
1349 |
+ |
*/ |
1350 |
+ |
void waitForThreadToEnterWaitState( |
1351 |
+ |
Thread thread, Callable<Boolean> waitingForGodot) { |
1352 |
+ |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot); |
1353 |
+ |
} |
1354 |
+ |
|
1355 |
+ |
/** |
1356 |
|
* Returns the number of milliseconds since time given by |
1357 |
|
* startNanoTime, which must have been previously returned from a |
1358 |
|
* call to {@link System#nanoTime()}. |
1414 |
|
} finally { |
1415 |
|
if (t.getState() != Thread.State.TERMINATED) { |
1416 |
|
t.interrupt(); |
1417 |
< |
threadFail("Test timed out"); |
1417 |
> |
threadFail("timed out waiting for thread to terminate"); |
1418 |
|
} |
1419 |
|
} |
1420 |
|
} |
1580 |
|
return new LatchAwaiter(latch); |
1581 |
|
} |
1582 |
|
|
1583 |
< |
public void await(CountDownLatch latch) { |
1583 |
> |
public void await(CountDownLatch latch, long timeoutMillis) { |
1584 |
|
try { |
1585 |
< |
if (!latch.await(LONG_DELAY_MS, MILLISECONDS)) |
1585 |
> |
if (!latch.await(timeoutMillis, MILLISECONDS)) |
1586 |
|
fail("timed out waiting for CountDownLatch for " |
1587 |
< |
+ (LONG_DELAY_MS/1000) + " sec"); |
1587 |
> |
+ (timeoutMillis/1000) + " sec"); |
1588 |
|
} catch (Throwable fail) { |
1589 |
|
threadUnexpectedException(fail); |
1590 |
|
} |
1591 |
|
} |
1592 |
|
|
1593 |
+ |
public void await(CountDownLatch latch) { |
1594 |
+ |
await(latch, LONG_DELAY_MS); |
1595 |
+ |
} |
1596 |
+ |
|
1597 |
|
public void await(Semaphore semaphore) { |
1598 |
|
try { |
1599 |
|
if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)) |
1828 |
|
* A CyclicBarrier that uses timed await and fails with |
1829 |
|
* AssertionFailedErrors instead of throwing checked exceptions. |
1830 |
|
*/ |
1831 |
< |
public class CheckedBarrier extends CyclicBarrier { |
1831 |
> |
public static class CheckedBarrier extends CyclicBarrier { |
1832 |
|
public CheckedBarrier(int parties) { super(parties); } |
1833 |
|
|
1834 |
|
public int await() { |
1892 |
|
} |
1893 |
|
} |
1894 |
|
|
1895 |
+ |
void assertImmutable(final Object o) { |
1896 |
+ |
if (o instanceof Collection) { |
1897 |
+ |
assertThrows( |
1898 |
+ |
UnsupportedOperationException.class, |
1899 |
+ |
new Runnable() { public void run() { |
1900 |
+ |
((Collection) o).add(null);}}); |
1901 |
+ |
} |
1902 |
+ |
} |
1903 |
+ |
|
1904 |
|
@SuppressWarnings("unchecked") |
1905 |
|
<T> T serialClone(T o) { |
1906 |
|
try { |
1907 |
|
ObjectInputStream ois = new ObjectInputStream |
1908 |
|
(new ByteArrayInputStream(serialBytes(o))); |
1909 |
|
T clone = (T) ois.readObject(); |
1910 |
+ |
if (o == clone) assertImmutable(o); |
1911 |
|
assertSame(o.getClass(), clone.getClass()); |
1912 |
|
return clone; |
1913 |
|
} catch (Throwable fail) { |
1916 |
|
} |
1917 |
|
} |
1918 |
|
|
1919 |
+ |
/** |
1920 |
+ |
* A version of serialClone that leaves error handling (for |
1921 |
+ |
* e.g. NotSerializableException) up to the caller. |
1922 |
+ |
*/ |
1923 |
+ |
@SuppressWarnings("unchecked") |
1924 |
+ |
<T> T serialClonePossiblyFailing(T o) |
1925 |
+ |
throws ReflectiveOperationException, java.io.IOException { |
1926 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1927 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1928 |
+ |
oos.writeObject(o); |
1929 |
+ |
oos.flush(); |
1930 |
+ |
oos.close(); |
1931 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1932 |
+ |
(new ByteArrayInputStream(bos.toByteArray())); |
1933 |
+ |
T clone = (T) ois.readObject(); |
1934 |
+ |
if (o == clone) assertImmutable(o); |
1935 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1936 |
+ |
return clone; |
1937 |
+ |
} |
1938 |
+ |
|
1939 |
+ |
/** |
1940 |
+ |
* If o implements Cloneable and has a public clone method, |
1941 |
+ |
* returns a clone of o, else null. |
1942 |
+ |
*/ |
1943 |
+ |
@SuppressWarnings("unchecked") |
1944 |
+ |
<T> T cloneableClone(T o) { |
1945 |
+ |
if (!(o instanceof Cloneable)) return null; |
1946 |
+ |
final T clone; |
1947 |
+ |
try { |
1948 |
+ |
clone = (T) o.getClass().getMethod("clone").invoke(o); |
1949 |
+ |
} catch (NoSuchMethodException ok) { |
1950 |
+ |
return null; |
1951 |
+ |
} catch (ReflectiveOperationException unexpected) { |
1952 |
+ |
throw new Error(unexpected); |
1953 |
+ |
} |
1954 |
+ |
assertNotSame(o, clone); // not 100% guaranteed by spec |
1955 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1956 |
+ |
return clone; |
1957 |
+ |
} |
1958 |
+ |
|
1959 |
|
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1960 |
|
Runnable... throwingActions) { |
1961 |
|
for (Runnable throwingAction : throwingActions) { |
1984 |
|
} catch (NoSuchElementException success) {} |
1985 |
|
assertFalse(it.hasNext()); |
1986 |
|
} |
1987 |
+ |
|
1988 |
+ |
public <T> Callable<T> callableThrowing(final Exception ex) { |
1989 |
+ |
return new Callable<T>() { public T call() throws Exception { throw ex; }}; |
1990 |
+ |
} |
1991 |
+ |
|
1992 |
+ |
public Runnable runnableThrowing(final RuntimeException ex) { |
1993 |
+ |
return new Runnable() { public void run() { throw ex; }}; |
1994 |
+ |
} |
1995 |
+ |
|
1996 |
+ |
/** A reusable thread pool to be shared by tests. */ |
1997 |
+ |
static final ExecutorService cachedThreadPool = |
1998 |
+ |
new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
1999 |
+ |
1000L, MILLISECONDS, |
2000 |
+ |
new SynchronousQueue<Runnable>()); |
2001 |
+ |
|
2002 |
+ |
static <T> void shuffle(T[] array) { |
2003 |
+ |
Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current()); |
2004 |
+ |
} |
2005 |
|
} |