1 |
dl |
1.1 |
/* |
2 |
dl |
1.13 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.74 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.27 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
jsr166 |
1.79 |
import java.io.ByteArrayInputStream; |
11 |
|
|
import java.io.ByteArrayOutputStream; |
12 |
|
|
import java.io.ObjectInputStream; |
13 |
|
|
import java.io.ObjectOutputStream; |
14 |
jsr166 |
1.96 |
import java.lang.management.ManagementFactory; |
15 |
|
|
import java.lang.management.ThreadInfo; |
16 |
jsr166 |
1.97 |
import java.lang.reflect.Method; |
17 |
jsr166 |
1.93 |
import java.util.ArrayList; |
18 |
jsr166 |
1.72 |
import java.util.Arrays; |
19 |
jsr166 |
1.81 |
import java.util.Date; |
20 |
jsr166 |
1.93 |
import java.util.Enumeration; |
21 |
|
|
import java.util.List; |
22 |
jsr166 |
1.72 |
import java.util.NoSuchElementException; |
23 |
jsr166 |
1.53 |
import java.util.PropertyPermission; |
24 |
dl |
1.1 |
import java.util.concurrent.*; |
25 |
jsr166 |
1.81 |
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
jsr166 |
1.53 |
import java.util.concurrent.atomic.AtomicReference; |
27 |
jsr166 |
1.36 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
jsr166 |
1.66 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
29 |
jsr166 |
1.53 |
import java.security.CodeSource; |
30 |
|
|
import java.security.Permission; |
31 |
|
|
import java.security.PermissionCollection; |
32 |
|
|
import java.security.Permissions; |
33 |
|
|
import java.security.Policy; |
34 |
|
|
import java.security.ProtectionDomain; |
35 |
|
|
import java.security.SecurityPermission; |
36 |
dl |
1.1 |
|
37 |
|
|
/** |
38 |
dl |
1.5 |
* Base class for JSR166 Junit TCK tests. Defines some constants, |
39 |
|
|
* utility methods and classes, as well as a simple framework for |
40 |
|
|
* helping to make sure that assertions failing in generated threads |
41 |
|
|
* cause the associated test that generated them to itself fail (which |
42 |
jsr166 |
1.27 |
* JUnit does not otherwise arrange). The rules for creating such |
43 |
dl |
1.5 |
* tests are: |
44 |
dl |
1.1 |
* |
45 |
|
|
* <ol> |
46 |
|
|
* |
47 |
|
|
* <li> All assertions in code running in generated threads must use |
48 |
jsr166 |
1.27 |
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
49 |
dl |
1.18 |
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
50 |
jsr166 |
1.50 |
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
51 |
dl |
1.1 |
* particularly recommended) for other code to use these forms too. |
52 |
|
|
* Only the most typically used JUnit assertion methods are defined |
53 |
|
|
* this way, but enough to live with.</li> |
54 |
|
|
* |
55 |
dl |
1.18 |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
56 |
jsr166 |
1.50 |
* to invoke {@code super.setUp} and {@code super.tearDown} within |
57 |
dl |
1.1 |
* them. These methods are used to clear and check for thread |
58 |
|
|
* assertion failures.</li> |
59 |
|
|
* |
60 |
jsr166 |
1.51 |
* <li>All delays and timeouts must use one of the constants {@code |
61 |
jsr166 |
1.50 |
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
62 |
|
|
* {@code LONG_DELAY_MS}. The idea here is that a SHORT is always |
63 |
dl |
1.5 |
* discriminable from zero time, and always allows enough time for the |
64 |
|
|
* small amounts of computation (creating a thread, calling a few |
65 |
dl |
1.1 |
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
66 |
|
|
* is always discriminable as larger than SHORT and smaller than |
67 |
|
|
* MEDIUM. And so on. These constants are set to conservative values, |
68 |
dl |
1.2 |
* but even so, if there is ever any doubt, they can all be increased |
69 |
jsr166 |
1.27 |
* in one spot to rerun tests on slower platforms.</li> |
70 |
dl |
1.1 |
* |
71 |
|
|
* <li> All threads generated must be joined inside each test case |
72 |
jsr166 |
1.50 |
* method (or {@code fail} to do so) before returning from the |
73 |
|
|
* method. The {@code joinPool} method can be used to do this when |
74 |
dl |
1.1 |
* using Executors.</li> |
75 |
|
|
* |
76 |
|
|
* </ol> |
77 |
dl |
1.6 |
* |
78 |
jsr166 |
1.92 |
* <p><b>Other notes</b> |
79 |
dl |
1.6 |
* <ul> |
80 |
|
|
* |
81 |
|
|
* <li> Usually, there is one testcase method per JSR166 method |
82 |
|
|
* covering "normal" operation, and then as many exception-testing |
83 |
|
|
* methods as there are exceptions the method can throw. Sometimes |
84 |
|
|
* there are multiple tests per JSR166 method when the different |
85 |
|
|
* "normal" behaviors differ significantly. And sometimes testcases |
86 |
|
|
* cover multiple methods when they cannot be tested in |
87 |
|
|
* isolation.</li> |
88 |
jsr166 |
1.27 |
* |
89 |
dl |
1.6 |
* <li> The documentation style for testcases is to provide as javadoc |
90 |
|
|
* a simple sentence or two describing the property that the testcase |
91 |
|
|
* method purports to test. The javadocs do not say anything about how |
92 |
|
|
* the property is tested. To find out, read the code.</li> |
93 |
|
|
* |
94 |
|
|
* <li> These tests are "conformance tests", and do not attempt to |
95 |
|
|
* test throughput, latency, scalability or other performance factors |
96 |
|
|
* (see the separate "jtreg" tests for a set intended to check these |
97 |
|
|
* for the most central aspects of functionality.) So, most tests use |
98 |
|
|
* the smallest sensible numbers of threads, collection sizes, etc |
99 |
|
|
* needed to check basic conformance.</li> |
100 |
|
|
* |
101 |
|
|
* <li>The test classes currently do not declare inclusion in |
102 |
|
|
* any particular package to simplify things for people integrating |
103 |
|
|
* them in TCK test suites.</li> |
104 |
|
|
* |
105 |
jsr166 |
1.50 |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
106 |
dl |
1.6 |
* runs all JSR166 unit tests.</li> |
107 |
|
|
* |
108 |
|
|
* </ul> |
109 |
dl |
1.1 |
*/ |
110 |
|
|
public class JSR166TestCase extends TestCase { |
111 |
jsr166 |
1.49 |
private static final boolean useSecurityManager = |
112 |
|
|
Boolean.getBoolean("jsr166.useSecurityManager"); |
113 |
|
|
|
114 |
jsr166 |
1.62 |
protected static final boolean expensiveTests = |
115 |
|
|
Boolean.getBoolean("jsr166.expensiveTests"); |
116 |
|
|
|
117 |
dl |
1.6 |
/** |
118 |
jsr166 |
1.61 |
* If true, report on stdout all "slow" tests, that is, ones that |
119 |
|
|
* take more than profileThreshold milliseconds to execute. |
120 |
|
|
*/ |
121 |
|
|
private static final boolean profileTests = |
122 |
|
|
Boolean.getBoolean("jsr166.profileTests"); |
123 |
|
|
|
124 |
|
|
/** |
125 |
|
|
* The number of milliseconds that tests are permitted for |
126 |
|
|
* execution without being reported, when profileTests is set. |
127 |
|
|
*/ |
128 |
|
|
private static final long profileThreshold = |
129 |
|
|
Long.getLong("jsr166.profileThreshold", 100); |
130 |
|
|
|
131 |
|
|
protected void runTest() throws Throwable { |
132 |
|
|
if (profileTests) |
133 |
|
|
runTestProfiled(); |
134 |
|
|
else |
135 |
|
|
super.runTest(); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
protected void runTestProfiled() throws Throwable { |
139 |
|
|
long t0 = System.nanoTime(); |
140 |
|
|
try { |
141 |
|
|
super.runTest(); |
142 |
|
|
} finally { |
143 |
|
|
long elapsedMillis = |
144 |
|
|
(System.nanoTime() - t0) / (1000L * 1000L); |
145 |
|
|
if (elapsedMillis >= profileThreshold) |
146 |
|
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
147 |
|
|
} |
148 |
|
|
} |
149 |
jsr166 |
1.63 |
|
150 |
jsr166 |
1.61 |
/** |
151 |
jsr166 |
1.94 |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
152 |
|
|
* Optional command line arg provides the number of iterations to |
153 |
|
|
* repeat running the tests. |
154 |
jsr166 |
1.27 |
*/ |
155 |
jsr166 |
1.41 |
public static void main(String[] args) { |
156 |
jsr166 |
1.49 |
if (useSecurityManager) { |
157 |
|
|
System.err.println("Setting a permissive security manager"); |
158 |
|
|
Policy.setPolicy(permissivePolicy()); |
159 |
|
|
System.setSecurityManager(new SecurityManager()); |
160 |
|
|
} |
161 |
jsr166 |
1.56 |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
162 |
|
|
|
163 |
dl |
1.16 |
Test s = suite(); |
164 |
dl |
1.22 |
for (int i = 0; i < iters; ++i) { |
165 |
jsr166 |
1.41 |
junit.textui.TestRunner.run(s); |
166 |
dl |
1.22 |
System.gc(); |
167 |
|
|
System.runFinalization(); |
168 |
|
|
} |
169 |
|
|
System.exit(0); |
170 |
dl |
1.6 |
} |
171 |
|
|
|
172 |
jsr166 |
1.60 |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
173 |
|
|
TestSuite suite = new TestSuite(); |
174 |
|
|
for (Object suiteOrClass : suiteOrClasses) { |
175 |
|
|
if (suiteOrClass instanceof TestSuite) |
176 |
|
|
suite.addTest((TestSuite) suiteOrClass); |
177 |
|
|
else if (suiteOrClass instanceof Class) |
178 |
|
|
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
179 |
|
|
else |
180 |
|
|
throw new ClassCastException("not a test suite or class"); |
181 |
|
|
} |
182 |
|
|
return suite; |
183 |
|
|
} |
184 |
|
|
|
185 |
jsr166 |
1.98 |
public static void addNamedTestClasses(TestSuite suite, |
186 |
|
|
String... testClassNames) { |
187 |
|
|
for (String testClassName : testClassNames) { |
188 |
|
|
try { |
189 |
|
|
Class<?> testClass = Class.forName(testClassName); |
190 |
|
|
Method m = testClass.getDeclaredMethod("suite", |
191 |
|
|
new Class<?>[0]); |
192 |
|
|
suite.addTest(newTestSuite((Test)m.invoke(null))); |
193 |
|
|
} catch (Exception e) { |
194 |
|
|
throw new Error("Missing test class", e); |
195 |
|
|
} |
196 |
jsr166 |
1.97 |
} |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
public static final double JAVA_CLASS_VERSION; |
200 |
|
|
static { |
201 |
|
|
try { |
202 |
|
|
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
203 |
|
|
new java.security.PrivilegedAction<Double>() { |
204 |
|
|
public Double run() { |
205 |
|
|
return Double.valueOf(System.getProperty("java.class.version"));}}); |
206 |
|
|
} catch (Throwable t) { |
207 |
|
|
throw new Error(t); |
208 |
|
|
} |
209 |
|
|
} |
210 |
|
|
|
211 |
jsr166 |
1.98 |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
212 |
|
|
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
213 |
|
|
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
214 |
jsr166 |
1.97 |
|
215 |
dl |
1.6 |
/** |
216 |
jsr166 |
1.60 |
* Collects all JSR166 unit tests as one suite. |
217 |
jsr166 |
1.27 |
*/ |
218 |
jsr166 |
1.41 |
public static Test suite() { |
219 |
jsr166 |
1.98 |
// Java7+ test classes |
220 |
jsr166 |
1.97 |
TestSuite suite = newTestSuite( |
221 |
jsr166 |
1.60 |
ForkJoinPoolTest.suite(), |
222 |
|
|
ForkJoinTaskTest.suite(), |
223 |
|
|
RecursiveActionTest.suite(), |
224 |
|
|
RecursiveTaskTest.suite(), |
225 |
|
|
LinkedTransferQueueTest.suite(), |
226 |
|
|
PhaserTest.suite(), |
227 |
|
|
ThreadLocalRandomTest.suite(), |
228 |
|
|
AbstractExecutorServiceTest.suite(), |
229 |
|
|
AbstractQueueTest.suite(), |
230 |
|
|
AbstractQueuedSynchronizerTest.suite(), |
231 |
|
|
AbstractQueuedLongSynchronizerTest.suite(), |
232 |
|
|
ArrayBlockingQueueTest.suite(), |
233 |
|
|
ArrayDequeTest.suite(), |
234 |
|
|
AtomicBooleanTest.suite(), |
235 |
|
|
AtomicIntegerArrayTest.suite(), |
236 |
|
|
AtomicIntegerFieldUpdaterTest.suite(), |
237 |
|
|
AtomicIntegerTest.suite(), |
238 |
|
|
AtomicLongArrayTest.suite(), |
239 |
|
|
AtomicLongFieldUpdaterTest.suite(), |
240 |
|
|
AtomicLongTest.suite(), |
241 |
|
|
AtomicMarkableReferenceTest.suite(), |
242 |
|
|
AtomicReferenceArrayTest.suite(), |
243 |
|
|
AtomicReferenceFieldUpdaterTest.suite(), |
244 |
|
|
AtomicReferenceTest.suite(), |
245 |
|
|
AtomicStampedReferenceTest.suite(), |
246 |
|
|
ConcurrentHashMapTest.suite(), |
247 |
|
|
ConcurrentLinkedDequeTest.suite(), |
248 |
|
|
ConcurrentLinkedQueueTest.suite(), |
249 |
|
|
ConcurrentSkipListMapTest.suite(), |
250 |
|
|
ConcurrentSkipListSubMapTest.suite(), |
251 |
|
|
ConcurrentSkipListSetTest.suite(), |
252 |
|
|
ConcurrentSkipListSubSetTest.suite(), |
253 |
|
|
CopyOnWriteArrayListTest.suite(), |
254 |
|
|
CopyOnWriteArraySetTest.suite(), |
255 |
|
|
CountDownLatchTest.suite(), |
256 |
|
|
CyclicBarrierTest.suite(), |
257 |
|
|
DelayQueueTest.suite(), |
258 |
|
|
EntryTest.suite(), |
259 |
|
|
ExchangerTest.suite(), |
260 |
|
|
ExecutorsTest.suite(), |
261 |
|
|
ExecutorCompletionServiceTest.suite(), |
262 |
|
|
FutureTaskTest.suite(), |
263 |
|
|
LinkedBlockingDequeTest.suite(), |
264 |
|
|
LinkedBlockingQueueTest.suite(), |
265 |
|
|
LinkedListTest.suite(), |
266 |
|
|
LockSupportTest.suite(), |
267 |
|
|
PriorityBlockingQueueTest.suite(), |
268 |
|
|
PriorityQueueTest.suite(), |
269 |
|
|
ReentrantLockTest.suite(), |
270 |
|
|
ReentrantReadWriteLockTest.suite(), |
271 |
|
|
ScheduledExecutorTest.suite(), |
272 |
|
|
ScheduledExecutorSubclassTest.suite(), |
273 |
|
|
SemaphoreTest.suite(), |
274 |
|
|
SynchronousQueueTest.suite(), |
275 |
|
|
SystemTest.suite(), |
276 |
|
|
ThreadLocalTest.suite(), |
277 |
|
|
ThreadPoolExecutorTest.suite(), |
278 |
|
|
ThreadPoolExecutorSubclassTest.suite(), |
279 |
|
|
ThreadTest.suite(), |
280 |
|
|
TimeUnitTest.suite(), |
281 |
|
|
TreeMapTest.suite(), |
282 |
|
|
TreeSetTest.suite(), |
283 |
|
|
TreeSubMapTest.suite(), |
284 |
|
|
TreeSubSetTest.suite()); |
285 |
jsr166 |
1.98 |
|
286 |
|
|
// Java8+ test classes |
287 |
|
|
if (atLeastJava8()) { |
288 |
|
|
String[] java8TestClassNames = { |
289 |
|
|
"StampedLockTest", |
290 |
jsr166 |
1.99 |
"ForkJoinPool8Test", |
291 |
jsr166 |
1.98 |
}; |
292 |
|
|
addNamedTestClasses(suite, java8TestClassNames); |
293 |
jsr166 |
1.97 |
} |
294 |
jsr166 |
1.98 |
|
295 |
jsr166 |
1.97 |
return suite; |
296 |
dl |
1.6 |
} |
297 |
|
|
|
298 |
dl |
1.1 |
|
299 |
dl |
1.2 |
public static long SHORT_DELAY_MS; |
300 |
|
|
public static long SMALL_DELAY_MS; |
301 |
|
|
public static long MEDIUM_DELAY_MS; |
302 |
|
|
public static long LONG_DELAY_MS; |
303 |
|
|
|
304 |
|
|
|
305 |
|
|
/** |
306 |
jsr166 |
1.27 |
* Returns the shortest timed delay. This could |
307 |
dl |
1.15 |
* be reimplemented to use for example a Property. |
308 |
jsr166 |
1.27 |
*/ |
309 |
dl |
1.2 |
protected long getShortDelay() { |
310 |
dl |
1.21 |
return 50; |
311 |
dl |
1.2 |
} |
312 |
|
|
|
313 |
|
|
/** |
314 |
jsr166 |
1.27 |
* Sets delays as multiples of SHORT_DELAY. |
315 |
dl |
1.2 |
*/ |
316 |
jsr166 |
1.43 |
protected void setDelays() { |
317 |
dl |
1.2 |
SHORT_DELAY_MS = getShortDelay(); |
318 |
jsr166 |
1.53 |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
319 |
dl |
1.2 |
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
320 |
jsr166 |
1.71 |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
321 |
dl |
1.2 |
} |
322 |
|
|
|
323 |
dl |
1.1 |
/** |
324 |
jsr166 |
1.81 |
* Returns a timeout in milliseconds to be used in tests that |
325 |
|
|
* verify that operations block or time out. |
326 |
|
|
*/ |
327 |
|
|
long timeoutMillis() { |
328 |
|
|
return SHORT_DELAY_MS / 4; |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
/** |
332 |
|
|
* Returns a new Date instance representing a time delayMillis |
333 |
|
|
* milliseconds in the future. |
334 |
|
|
*/ |
335 |
|
|
Date delayedDate(long delayMillis) { |
336 |
jsr166 |
1.84 |
return new Date(System.currentTimeMillis() + delayMillis); |
337 |
jsr166 |
1.81 |
} |
338 |
|
|
|
339 |
|
|
/** |
340 |
jsr166 |
1.53 |
* The first exception encountered if any threadAssertXXX method fails. |
341 |
dl |
1.1 |
*/ |
342 |
jsr166 |
1.53 |
private final AtomicReference<Throwable> threadFailure |
343 |
|
|
= new AtomicReference<Throwable>(null); |
344 |
dl |
1.1 |
|
345 |
|
|
/** |
346 |
jsr166 |
1.53 |
* Records an exception so that it can be rethrown later in the test |
347 |
|
|
* harness thread, triggering a test case failure. Only the first |
348 |
|
|
* failure is recorded; subsequent calls to this method from within |
349 |
|
|
* the same test have no effect. |
350 |
dl |
1.1 |
*/ |
351 |
jsr166 |
1.53 |
public void threadRecordFailure(Throwable t) { |
352 |
|
|
threadFailure.compareAndSet(null, t); |
353 |
|
|
} |
354 |
|
|
|
355 |
jsr166 |
1.27 |
public void setUp() { |
356 |
dl |
1.2 |
setDelays(); |
357 |
dl |
1.1 |
} |
358 |
|
|
|
359 |
|
|
/** |
360 |
jsr166 |
1.85 |
* Extra checks that get done for all test cases. |
361 |
|
|
* |
362 |
jsr166 |
1.53 |
* Triggers test case failure if any thread assertions have failed, |
363 |
|
|
* by rethrowing, in the test harness thread, any exception recorded |
364 |
|
|
* earlier by threadRecordFailure. |
365 |
jsr166 |
1.85 |
* |
366 |
|
|
* Triggers test case failure if interrupt status is set in the main thread. |
367 |
jsr166 |
1.53 |
*/ |
368 |
|
|
public void tearDown() throws Exception { |
369 |
jsr166 |
1.70 |
Throwable t = threadFailure.getAndSet(null); |
370 |
jsr166 |
1.53 |
if (t != null) { |
371 |
|
|
if (t instanceof Error) |
372 |
|
|
throw (Error) t; |
373 |
|
|
else if (t instanceof RuntimeException) |
374 |
|
|
throw (RuntimeException) t; |
375 |
|
|
else if (t instanceof Exception) |
376 |
|
|
throw (Exception) t; |
377 |
jsr166 |
1.57 |
else { |
378 |
|
|
AssertionFailedError afe = |
379 |
|
|
new AssertionFailedError(t.toString()); |
380 |
|
|
afe.initCause(t); |
381 |
|
|
throw afe; |
382 |
|
|
} |
383 |
jsr166 |
1.53 |
} |
384 |
jsr166 |
1.85 |
|
385 |
|
|
if (Thread.interrupted()) |
386 |
|
|
throw new AssertionFailedError("interrupt status set in main thread"); |
387 |
jsr166 |
1.100 |
|
388 |
|
|
checkForkJoinPoolThreadLeaks(); |
389 |
dl |
1.1 |
} |
390 |
|
|
|
391 |
dl |
1.5 |
/** |
392 |
jsr166 |
1.100 |
* Find missing try { ... } finally { joinPool(e); } |
393 |
|
|
*/ |
394 |
|
|
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
395 |
|
|
Thread[] survivors = new Thread[5]; |
396 |
|
|
int count = Thread.enumerate(survivors); |
397 |
|
|
for (int i = 0; i < count; i++) { |
398 |
|
|
Thread thread = survivors[i]; |
399 |
|
|
String name = thread.getName(); |
400 |
|
|
if (name.startsWith("ForkJoinPool-")) { |
401 |
|
|
// give thread some time to terminate |
402 |
|
|
thread.join(LONG_DELAY_MS); |
403 |
|
|
if (!thread.isAlive()) continue; |
404 |
|
|
thread.stop(); |
405 |
|
|
throw new AssertionFailedError |
406 |
|
|
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
407 |
|
|
toString(), name)); |
408 |
|
|
} |
409 |
|
|
} |
410 |
|
|
} |
411 |
|
|
|
412 |
|
|
/** |
413 |
jsr166 |
1.53 |
* Just like fail(reason), but additionally recording (using |
414 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
415 |
|
|
* the current testcase will fail. |
416 |
jsr166 |
1.27 |
*/ |
417 |
dl |
1.1 |
public void threadFail(String reason) { |
418 |
jsr166 |
1.53 |
try { |
419 |
|
|
fail(reason); |
420 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
421 |
jsr166 |
1.53 |
threadRecordFailure(t); |
422 |
|
|
fail(reason); |
423 |
|
|
} |
424 |
dl |
1.1 |
} |
425 |
|
|
|
426 |
dl |
1.5 |
/** |
427 |
jsr166 |
1.53 |
* Just like assertTrue(b), but additionally recording (using |
428 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
429 |
|
|
* the current testcase will fail. |
430 |
jsr166 |
1.27 |
*/ |
431 |
dl |
1.1 |
public void threadAssertTrue(boolean b) { |
432 |
jsr166 |
1.53 |
try { |
433 |
dl |
1.1 |
assertTrue(b); |
434 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
435 |
jsr166 |
1.53 |
threadRecordFailure(t); |
436 |
|
|
throw t; |
437 |
dl |
1.1 |
} |
438 |
|
|
} |
439 |
dl |
1.5 |
|
440 |
|
|
/** |
441 |
jsr166 |
1.53 |
* Just like assertFalse(b), but additionally recording (using |
442 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
443 |
|
|
* the current testcase will fail. |
444 |
jsr166 |
1.27 |
*/ |
445 |
dl |
1.1 |
public void threadAssertFalse(boolean b) { |
446 |
jsr166 |
1.53 |
try { |
447 |
dl |
1.1 |
assertFalse(b); |
448 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
449 |
jsr166 |
1.53 |
threadRecordFailure(t); |
450 |
|
|
throw t; |
451 |
dl |
1.1 |
} |
452 |
|
|
} |
453 |
dl |
1.5 |
|
454 |
|
|
/** |
455 |
jsr166 |
1.53 |
* Just like assertNull(x), but additionally recording (using |
456 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
457 |
|
|
* the current testcase will fail. |
458 |
jsr166 |
1.27 |
*/ |
459 |
dl |
1.1 |
public void threadAssertNull(Object x) { |
460 |
jsr166 |
1.53 |
try { |
461 |
dl |
1.1 |
assertNull(x); |
462 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
463 |
jsr166 |
1.53 |
threadRecordFailure(t); |
464 |
|
|
throw t; |
465 |
dl |
1.1 |
} |
466 |
|
|
} |
467 |
dl |
1.5 |
|
468 |
|
|
/** |
469 |
jsr166 |
1.53 |
* Just like assertEquals(x, y), but additionally recording (using |
470 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
471 |
|
|
* the current testcase will fail. |
472 |
jsr166 |
1.27 |
*/ |
473 |
dl |
1.1 |
public void threadAssertEquals(long x, long y) { |
474 |
jsr166 |
1.53 |
try { |
475 |
dl |
1.1 |
assertEquals(x, y); |
476 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
477 |
jsr166 |
1.53 |
threadRecordFailure(t); |
478 |
|
|
throw t; |
479 |
dl |
1.1 |
} |
480 |
|
|
} |
481 |
dl |
1.5 |
|
482 |
|
|
/** |
483 |
jsr166 |
1.53 |
* Just like assertEquals(x, y), but additionally recording (using |
484 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
485 |
|
|
* the current testcase will fail. |
486 |
jsr166 |
1.27 |
*/ |
487 |
dl |
1.1 |
public void threadAssertEquals(Object x, Object y) { |
488 |
jsr166 |
1.53 |
try { |
489 |
dl |
1.1 |
assertEquals(x, y); |
490 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
491 |
jsr166 |
1.53 |
threadRecordFailure(t); |
492 |
|
|
throw t; |
493 |
jsr166 |
1.57 |
} catch (Throwable t) { |
494 |
|
|
threadUnexpectedException(t); |
495 |
dl |
1.1 |
} |
496 |
|
|
} |
497 |
|
|
|
498 |
dl |
1.5 |
/** |
499 |
jsr166 |
1.53 |
* Just like assertSame(x, y), but additionally recording (using |
500 |
jsr166 |
1.57 |
* threadRecordFailure) any AssertionFailedError thrown, so that |
501 |
|
|
* the current testcase will fail. |
502 |
jsr166 |
1.52 |
*/ |
503 |
|
|
public void threadAssertSame(Object x, Object y) { |
504 |
jsr166 |
1.53 |
try { |
505 |
jsr166 |
1.52 |
assertSame(x, y); |
506 |
jsr166 |
1.57 |
} catch (AssertionFailedError t) { |
507 |
jsr166 |
1.53 |
threadRecordFailure(t); |
508 |
|
|
throw t; |
509 |
jsr166 |
1.52 |
} |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
/** |
513 |
jsr166 |
1.53 |
* Calls threadFail with message "should throw exception". |
514 |
jsr166 |
1.33 |
*/ |
515 |
dl |
1.3 |
public void threadShouldThrow() { |
516 |
jsr166 |
1.53 |
threadFail("should throw exception"); |
517 |
dl |
1.3 |
} |
518 |
|
|
|
519 |
dl |
1.5 |
/** |
520 |
jsr166 |
1.53 |
* Calls threadFail with message "should throw" + exceptionName. |
521 |
jsr166 |
1.40 |
*/ |
522 |
|
|
public void threadShouldThrow(String exceptionName) { |
523 |
jsr166 |
1.53 |
threadFail("should throw " + exceptionName); |
524 |
dl |
1.3 |
} |
525 |
|
|
|
526 |
dl |
1.31 |
/** |
527 |
jsr166 |
1.57 |
* Records the given exception using {@link #threadRecordFailure}, |
528 |
|
|
* then rethrows the exception, wrapping it in an |
529 |
|
|
* AssertionFailedError if necessary. |
530 |
dl |
1.31 |
*/ |
531 |
jsr166 |
1.53 |
public void threadUnexpectedException(Throwable t) { |
532 |
|
|
threadRecordFailure(t); |
533 |
|
|
t.printStackTrace(); |
534 |
|
|
if (t instanceof RuntimeException) |
535 |
|
|
throw (RuntimeException) t; |
536 |
|
|
else if (t instanceof Error) |
537 |
|
|
throw (Error) t; |
538 |
|
|
else { |
539 |
jsr166 |
1.57 |
AssertionFailedError afe = |
540 |
|
|
new AssertionFailedError("unexpected exception: " + t); |
541 |
jsr166 |
1.82 |
afe.initCause(t); |
542 |
jsr166 |
1.57 |
throw afe; |
543 |
jsr166 |
1.55 |
} |
544 |
dl |
1.31 |
} |
545 |
dl |
1.3 |
|
546 |
dl |
1.1 |
/** |
547 |
jsr166 |
1.81 |
* Delays, via Thread.sleep, for the given millisecond delay, but |
548 |
dl |
1.76 |
* if the sleep is shorter than specified, may re-sleep or yield |
549 |
|
|
* until time elapses. |
550 |
|
|
*/ |
551 |
jsr166 |
1.81 |
static void delay(long millis) throws InterruptedException { |
552 |
dl |
1.76 |
long startTime = System.nanoTime(); |
553 |
jsr166 |
1.80 |
long ns = millis * 1000 * 1000; |
554 |
dl |
1.76 |
for (;;) { |
555 |
jsr166 |
1.80 |
if (millis > 0L) |
556 |
|
|
Thread.sleep(millis); |
557 |
dl |
1.76 |
else // too short to sleep |
558 |
|
|
Thread.yield(); |
559 |
|
|
long d = ns - (System.nanoTime() - startTime); |
560 |
|
|
if (d > 0L) |
561 |
jsr166 |
1.80 |
millis = d / (1000 * 1000); |
562 |
dl |
1.76 |
else |
563 |
|
|
break; |
564 |
|
|
} |
565 |
|
|
} |
566 |
|
|
|
567 |
|
|
/** |
568 |
jsr166 |
1.53 |
* Waits out termination of a thread pool or fails doing so. |
569 |
dl |
1.1 |
*/ |
570 |
jsr166 |
1.81 |
void joinPool(ExecutorService exec) { |
571 |
dl |
1.1 |
try { |
572 |
|
|
exec.shutdown(); |
573 |
jsr166 |
1.54 |
assertTrue("ExecutorService did not terminate in a timely manner", |
574 |
jsr166 |
1.71 |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
575 |
jsr166 |
1.33 |
} catch (SecurityException ok) { |
576 |
dl |
1.22 |
// Allowed in case test doesn't have privs |
577 |
jsr166 |
1.33 |
} catch (InterruptedException ie) { |
578 |
jsr166 |
1.44 |
fail("Unexpected InterruptedException"); |
579 |
dl |
1.1 |
} |
580 |
|
|
} |
581 |
|
|
|
582 |
jsr166 |
1.78 |
/** |
583 |
jsr166 |
1.95 |
* A debugging tool to print all stack traces, as jstack does. |
584 |
|
|
*/ |
585 |
jsr166 |
1.96 |
static void printAllStackTraces() { |
586 |
|
|
for (ThreadInfo info : |
587 |
|
|
ManagementFactory.getThreadMXBean() |
588 |
|
|
.dumpAllThreads(true, true)) |
589 |
|
|
System.err.print(info); |
590 |
jsr166 |
1.95 |
} |
591 |
|
|
|
592 |
|
|
/** |
593 |
jsr166 |
1.81 |
* Checks that thread does not terminate within the default |
594 |
|
|
* millisecond delay of {@code timeoutMillis()}. |
595 |
|
|
*/ |
596 |
|
|
void assertThreadStaysAlive(Thread thread) { |
597 |
|
|
assertThreadStaysAlive(thread, timeoutMillis()); |
598 |
|
|
} |
599 |
|
|
|
600 |
|
|
/** |
601 |
jsr166 |
1.80 |
* Checks that thread does not terminate within the given millisecond delay. |
602 |
jsr166 |
1.78 |
*/ |
603 |
jsr166 |
1.81 |
void assertThreadStaysAlive(Thread thread, long millis) { |
604 |
jsr166 |
1.78 |
try { |
605 |
jsr166 |
1.80 |
// No need to optimize the failing case via Thread.join. |
606 |
|
|
delay(millis); |
607 |
jsr166 |
1.78 |
assertTrue(thread.isAlive()); |
608 |
|
|
} catch (InterruptedException ie) { |
609 |
|
|
fail("Unexpected InterruptedException"); |
610 |
|
|
} |
611 |
|
|
} |
612 |
dl |
1.5 |
|
613 |
|
|
/** |
614 |
jsr166 |
1.90 |
* Checks that the threads do not terminate within the default |
615 |
|
|
* millisecond delay of {@code timeoutMillis()}. |
616 |
|
|
*/ |
617 |
|
|
void assertThreadsStayAlive(Thread... threads) { |
618 |
|
|
assertThreadsStayAlive(timeoutMillis(), threads); |
619 |
|
|
} |
620 |
|
|
|
621 |
|
|
/** |
622 |
|
|
* Checks that the threads do not terminate within the given millisecond delay. |
623 |
|
|
*/ |
624 |
|
|
void assertThreadsStayAlive(long millis, Thread... threads) { |
625 |
|
|
try { |
626 |
|
|
// No need to optimize the failing case via Thread.join. |
627 |
|
|
delay(millis); |
628 |
|
|
for (Thread thread : threads) |
629 |
|
|
assertTrue(thread.isAlive()); |
630 |
|
|
} catch (InterruptedException ie) { |
631 |
|
|
fail("Unexpected InterruptedException"); |
632 |
|
|
} |
633 |
|
|
} |
634 |
|
|
|
635 |
|
|
/** |
636 |
jsr166 |
1.83 |
* Checks that future.get times out, with the default timeout of |
637 |
|
|
* {@code timeoutMillis()}. |
638 |
|
|
*/ |
639 |
|
|
void assertFutureTimesOut(Future future) { |
640 |
|
|
assertFutureTimesOut(future, timeoutMillis()); |
641 |
|
|
} |
642 |
|
|
|
643 |
|
|
/** |
644 |
|
|
* Checks that future.get times out, with the given millisecond timeout. |
645 |
|
|
*/ |
646 |
|
|
void assertFutureTimesOut(Future future, long timeoutMillis) { |
647 |
|
|
long startTime = System.nanoTime(); |
648 |
|
|
try { |
649 |
|
|
future.get(timeoutMillis, MILLISECONDS); |
650 |
|
|
shouldThrow(); |
651 |
|
|
} catch (TimeoutException success) { |
652 |
|
|
} catch (Exception e) { |
653 |
|
|
threadUnexpectedException(e); |
654 |
|
|
} finally { future.cancel(true); } |
655 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
656 |
|
|
} |
657 |
|
|
|
658 |
|
|
/** |
659 |
jsr166 |
1.53 |
* Fails with message "should throw exception". |
660 |
jsr166 |
1.27 |
*/ |
661 |
dl |
1.3 |
public void shouldThrow() { |
662 |
|
|
fail("Should throw exception"); |
663 |
|
|
} |
664 |
|
|
|
665 |
dl |
1.5 |
/** |
666 |
jsr166 |
1.53 |
* Fails with message "should throw " + exceptionName. |
667 |
jsr166 |
1.40 |
*/ |
668 |
|
|
public void shouldThrow(String exceptionName) { |
669 |
|
|
fail("Should throw " + exceptionName); |
670 |
|
|
} |
671 |
|
|
|
672 |
|
|
/** |
673 |
dl |
1.1 |
* The number of elements to place in collections, arrays, etc. |
674 |
|
|
*/ |
675 |
jsr166 |
1.45 |
public static final int SIZE = 20; |
676 |
dl |
1.1 |
|
677 |
|
|
// Some convenient Integer constants |
678 |
|
|
|
679 |
jsr166 |
1.47 |
public static final Integer zero = new Integer(0); |
680 |
|
|
public static final Integer one = new Integer(1); |
681 |
|
|
public static final Integer two = new Integer(2); |
682 |
|
|
public static final Integer three = new Integer(3); |
683 |
jsr166 |
1.45 |
public static final Integer four = new Integer(4); |
684 |
|
|
public static final Integer five = new Integer(5); |
685 |
jsr166 |
1.47 |
public static final Integer six = new Integer(6); |
686 |
jsr166 |
1.45 |
public static final Integer seven = new Integer(7); |
687 |
|
|
public static final Integer eight = new Integer(8); |
688 |
jsr166 |
1.47 |
public static final Integer nine = new Integer(9); |
689 |
jsr166 |
1.45 |
public static final Integer m1 = new Integer(-1); |
690 |
|
|
public static final Integer m2 = new Integer(-2); |
691 |
|
|
public static final Integer m3 = new Integer(-3); |
692 |
jsr166 |
1.47 |
public static final Integer m4 = new Integer(-4); |
693 |
|
|
public static final Integer m5 = new Integer(-5); |
694 |
|
|
public static final Integer m6 = new Integer(-6); |
695 |
jsr166 |
1.45 |
public static final Integer m10 = new Integer(-10); |
696 |
dl |
1.7 |
|
697 |
|
|
|
698 |
|
|
/** |
699 |
jsr166 |
1.49 |
* Runs Runnable r with a security policy that permits precisely |
700 |
|
|
* the specified permissions. If there is no current security |
701 |
|
|
* manager, the runnable is run twice, both with and without a |
702 |
|
|
* security manager. We require that any security manager permit |
703 |
|
|
* getPolicy/setPolicy. |
704 |
|
|
*/ |
705 |
|
|
public void runWithPermissions(Runnable r, Permission... permissions) { |
706 |
|
|
SecurityManager sm = System.getSecurityManager(); |
707 |
|
|
if (sm == null) { |
708 |
|
|
r.run(); |
709 |
jsr166 |
1.93 |
} |
710 |
|
|
runWithSecurityManagerWithPermissions(r, permissions); |
711 |
|
|
} |
712 |
|
|
|
713 |
|
|
/** |
714 |
|
|
* Runs Runnable r with a security policy that permits precisely |
715 |
|
|
* the specified permissions. If there is no current security |
716 |
|
|
* manager, a temporary one is set for the duration of the |
717 |
|
|
* Runnable. We require that any security manager permit |
718 |
|
|
* getPolicy/setPolicy. |
719 |
|
|
*/ |
720 |
|
|
public void runWithSecurityManagerWithPermissions(Runnable r, |
721 |
|
|
Permission... permissions) { |
722 |
|
|
SecurityManager sm = System.getSecurityManager(); |
723 |
|
|
if (sm == null) { |
724 |
jsr166 |
1.49 |
Policy savedPolicy = Policy.getPolicy(); |
725 |
|
|
try { |
726 |
|
|
Policy.setPolicy(permissivePolicy()); |
727 |
|
|
System.setSecurityManager(new SecurityManager()); |
728 |
jsr166 |
1.93 |
runWithSecurityManagerWithPermissions(r, permissions); |
729 |
jsr166 |
1.49 |
} finally { |
730 |
|
|
System.setSecurityManager(null); |
731 |
|
|
Policy.setPolicy(savedPolicy); |
732 |
|
|
} |
733 |
|
|
} else { |
734 |
|
|
Policy savedPolicy = Policy.getPolicy(); |
735 |
|
|
AdjustablePolicy policy = new AdjustablePolicy(permissions); |
736 |
|
|
Policy.setPolicy(policy); |
737 |
|
|
|
738 |
|
|
try { |
739 |
|
|
r.run(); |
740 |
|
|
} finally { |
741 |
|
|
policy.addPermission(new SecurityPermission("setPolicy")); |
742 |
|
|
Policy.setPolicy(savedPolicy); |
743 |
|
|
} |
744 |
|
|
} |
745 |
|
|
} |
746 |
|
|
|
747 |
|
|
/** |
748 |
|
|
* Runs a runnable without any permissions. |
749 |
|
|
*/ |
750 |
|
|
public void runWithoutPermissions(Runnable r) { |
751 |
|
|
runWithPermissions(r); |
752 |
|
|
} |
753 |
|
|
|
754 |
|
|
/** |
755 |
dl |
1.7 |
* A security policy where new permissions can be dynamically added |
756 |
|
|
* or all cleared. |
757 |
|
|
*/ |
758 |
jsr166 |
1.45 |
public static class AdjustablePolicy extends java.security.Policy { |
759 |
dl |
1.7 |
Permissions perms = new Permissions(); |
760 |
jsr166 |
1.49 |
AdjustablePolicy(Permission... permissions) { |
761 |
|
|
for (Permission permission : permissions) |
762 |
|
|
perms.add(permission); |
763 |
|
|
} |
764 |
dl |
1.7 |
void addPermission(Permission perm) { perms.add(perm); } |
765 |
|
|
void clearPermissions() { perms = new Permissions(); } |
766 |
jsr166 |
1.42 |
public PermissionCollection getPermissions(CodeSource cs) { |
767 |
|
|
return perms; |
768 |
|
|
} |
769 |
|
|
public PermissionCollection getPermissions(ProtectionDomain pd) { |
770 |
|
|
return perms; |
771 |
|
|
} |
772 |
|
|
public boolean implies(ProtectionDomain pd, Permission p) { |
773 |
|
|
return perms.implies(p); |
774 |
|
|
} |
775 |
|
|
public void refresh() {} |
776 |
jsr166 |
1.93 |
public String toString() { |
777 |
|
|
List<Permission> ps = new ArrayList<Permission>(); |
778 |
|
|
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
779 |
|
|
ps.add(e.nextElement()); |
780 |
|
|
return "AdjustablePolicy with permissions " + ps; |
781 |
|
|
} |
782 |
dl |
1.7 |
} |
783 |
dl |
1.1 |
|
784 |
jsr166 |
1.38 |
/** |
785 |
jsr166 |
1.49 |
* Returns a policy containing all the permissions we ever need. |
786 |
|
|
*/ |
787 |
|
|
public static Policy permissivePolicy() { |
788 |
|
|
return new AdjustablePolicy |
789 |
|
|
// Permissions j.u.c. needs directly |
790 |
|
|
(new RuntimePermission("modifyThread"), |
791 |
|
|
new RuntimePermission("getClassLoader"), |
792 |
|
|
new RuntimePermission("setContextClassLoader"), |
793 |
|
|
// Permissions needed to change permissions! |
794 |
|
|
new SecurityPermission("getPolicy"), |
795 |
|
|
new SecurityPermission("setPolicy"), |
796 |
|
|
new RuntimePermission("setSecurityManager"), |
797 |
|
|
// Permissions needed by the junit test harness |
798 |
|
|
new RuntimePermission("accessDeclaredMembers"), |
799 |
|
|
new PropertyPermission("*", "read"), |
800 |
|
|
new java.io.FilePermission("<<ALL FILES>>", "read")); |
801 |
|
|
} |
802 |
|
|
|
803 |
|
|
/** |
804 |
jsr166 |
1.60 |
* Sleeps until the given time has elapsed. |
805 |
|
|
* Throws AssertionFailedError if interrupted. |
806 |
|
|
*/ |
807 |
|
|
void sleep(long millis) { |
808 |
|
|
try { |
809 |
dl |
1.76 |
delay(millis); |
810 |
jsr166 |
1.60 |
} catch (InterruptedException ie) { |
811 |
|
|
AssertionFailedError afe = |
812 |
|
|
new AssertionFailedError("Unexpected InterruptedException"); |
813 |
|
|
afe.initCause(ie); |
814 |
|
|
throw afe; |
815 |
|
|
} |
816 |
|
|
} |
817 |
|
|
|
818 |
|
|
/** |
819 |
jsr166 |
1.88 |
* Spin-waits up to the specified number of milliseconds for the given |
820 |
jsr166 |
1.65 |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
821 |
|
|
*/ |
822 |
|
|
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
823 |
jsr166 |
1.88 |
long startTime = System.nanoTime(); |
824 |
jsr166 |
1.65 |
for (;;) { |
825 |
|
|
Thread.State s = thread.getState(); |
826 |
|
|
if (s == Thread.State.BLOCKED || |
827 |
|
|
s == Thread.State.WAITING || |
828 |
jsr166 |
1.67 |
s == Thread.State.TIMED_WAITING) |
829 |
jsr166 |
1.65 |
return; |
830 |
jsr166 |
1.67 |
else if (s == Thread.State.TERMINATED) |
831 |
|
|
fail("Unexpected thread termination"); |
832 |
jsr166 |
1.88 |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
833 |
jsr166 |
1.67 |
threadAssertTrue(thread.isAlive()); |
834 |
|
|
return; |
835 |
|
|
} |
836 |
jsr166 |
1.65 |
Thread.yield(); |
837 |
|
|
} |
838 |
|
|
} |
839 |
|
|
|
840 |
|
|
/** |
841 |
jsr166 |
1.75 |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
842 |
|
|
* state: BLOCKED, WAITING, or TIMED_WAITING. |
843 |
|
|
*/ |
844 |
|
|
void waitForThreadToEnterWaitState(Thread thread) { |
845 |
|
|
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
846 |
|
|
} |
847 |
|
|
|
848 |
|
|
/** |
849 |
jsr166 |
1.66 |
* Returns the number of milliseconds since time given by |
850 |
|
|
* startNanoTime, which must have been previously returned from a |
851 |
|
|
* call to {@link System.nanoTime()}. |
852 |
|
|
*/ |
853 |
|
|
long millisElapsedSince(long startNanoTime) { |
854 |
|
|
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
855 |
|
|
} |
856 |
jsr166 |
1.68 |
|
857 |
jsr166 |
1.66 |
/** |
858 |
jsr166 |
1.58 |
* Returns a new started daemon Thread running the given runnable. |
859 |
jsr166 |
1.38 |
*/ |
860 |
|
|
Thread newStartedThread(Runnable runnable) { |
861 |
|
|
Thread t = new Thread(runnable); |
862 |
jsr166 |
1.58 |
t.setDaemon(true); |
863 |
jsr166 |
1.38 |
t.start(); |
864 |
|
|
return t; |
865 |
|
|
} |
866 |
dl |
1.1 |
|
867 |
jsr166 |
1.59 |
/** |
868 |
|
|
* Waits for the specified time (in milliseconds) for the thread |
869 |
|
|
* to terminate (using {@link Thread#join(long)}), else interrupts |
870 |
|
|
* the thread (in the hope that it may terminate later) and fails. |
871 |
|
|
*/ |
872 |
|
|
void awaitTermination(Thread t, long timeoutMillis) { |
873 |
|
|
try { |
874 |
|
|
t.join(timeoutMillis); |
875 |
|
|
} catch (InterruptedException ie) { |
876 |
|
|
threadUnexpectedException(ie); |
877 |
|
|
} finally { |
878 |
jsr166 |
1.83 |
if (t.getState() != Thread.State.TERMINATED) { |
879 |
jsr166 |
1.59 |
t.interrupt(); |
880 |
|
|
fail("Test timed out"); |
881 |
|
|
} |
882 |
|
|
} |
883 |
|
|
} |
884 |
|
|
|
885 |
jsr166 |
1.75 |
/** |
886 |
|
|
* Waits for LONG_DELAY_MS milliseconds for the thread to |
887 |
|
|
* terminate (using {@link Thread#join(long)}), else interrupts |
888 |
|
|
* the thread (in the hope that it may terminate later) and fails. |
889 |
|
|
*/ |
890 |
|
|
void awaitTermination(Thread t) { |
891 |
|
|
awaitTermination(t, LONG_DELAY_MS); |
892 |
|
|
} |
893 |
|
|
|
894 |
dl |
1.1 |
// Some convenient Runnable classes |
895 |
|
|
|
896 |
jsr166 |
1.45 |
public abstract class CheckedRunnable implements Runnable { |
897 |
|
|
protected abstract void realRun() throws Throwable; |
898 |
jsr166 |
1.35 |
|
899 |
|
|
public final void run() { |
900 |
|
|
try { |
901 |
|
|
realRun(); |
902 |
|
|
} catch (Throwable t) { |
903 |
|
|
threadUnexpectedException(t); |
904 |
|
|
} |
905 |
|
|
} |
906 |
|
|
} |
907 |
|
|
|
908 |
jsr166 |
1.45 |
public abstract class RunnableShouldThrow implements Runnable { |
909 |
|
|
protected abstract void realRun() throws Throwable; |
910 |
jsr166 |
1.40 |
|
911 |
|
|
final Class<?> exceptionClass; |
912 |
|
|
|
913 |
|
|
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
914 |
|
|
this.exceptionClass = exceptionClass; |
915 |
|
|
} |
916 |
|
|
|
917 |
|
|
public final void run() { |
918 |
|
|
try { |
919 |
|
|
realRun(); |
920 |
|
|
threadShouldThrow(exceptionClass.getSimpleName()); |
921 |
|
|
} catch (Throwable t) { |
922 |
|
|
if (! exceptionClass.isInstance(t)) |
923 |
|
|
threadUnexpectedException(t); |
924 |
|
|
} |
925 |
|
|
} |
926 |
|
|
} |
927 |
|
|
|
928 |
jsr166 |
1.45 |
public abstract class ThreadShouldThrow extends Thread { |
929 |
|
|
protected abstract void realRun() throws Throwable; |
930 |
jsr166 |
1.40 |
|
931 |
|
|
final Class<?> exceptionClass; |
932 |
|
|
|
933 |
|
|
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
934 |
|
|
this.exceptionClass = exceptionClass; |
935 |
|
|
} |
936 |
|
|
|
937 |
|
|
public final void run() { |
938 |
|
|
try { |
939 |
|
|
realRun(); |
940 |
|
|
threadShouldThrow(exceptionClass.getSimpleName()); |
941 |
|
|
} catch (Throwable t) { |
942 |
|
|
if (! exceptionClass.isInstance(t)) |
943 |
|
|
threadUnexpectedException(t); |
944 |
|
|
} |
945 |
|
|
} |
946 |
|
|
} |
947 |
|
|
|
948 |
jsr166 |
1.45 |
public abstract class CheckedInterruptedRunnable implements Runnable { |
949 |
|
|
protected abstract void realRun() throws Throwable; |
950 |
jsr166 |
1.35 |
|
951 |
|
|
public final void run() { |
952 |
|
|
try { |
953 |
|
|
realRun(); |
954 |
jsr166 |
1.40 |
threadShouldThrow("InterruptedException"); |
955 |
jsr166 |
1.35 |
} catch (InterruptedException success) { |
956 |
jsr166 |
1.81 |
threadAssertFalse(Thread.interrupted()); |
957 |
jsr166 |
1.35 |
} catch (Throwable t) { |
958 |
|
|
threadUnexpectedException(t); |
959 |
|
|
} |
960 |
|
|
} |
961 |
|
|
} |
962 |
|
|
|
963 |
jsr166 |
1.45 |
public abstract class CheckedCallable<T> implements Callable<T> { |
964 |
|
|
protected abstract T realCall() throws Throwable; |
965 |
jsr166 |
1.35 |
|
966 |
|
|
public final T call() { |
967 |
|
|
try { |
968 |
|
|
return realCall(); |
969 |
|
|
} catch (Throwable t) { |
970 |
|
|
threadUnexpectedException(t); |
971 |
jsr166 |
1.53 |
return null; |
972 |
jsr166 |
1.35 |
} |
973 |
jsr166 |
1.40 |
} |
974 |
|
|
} |
975 |
|
|
|
976 |
jsr166 |
1.53 |
public abstract class CheckedInterruptedCallable<T> |
977 |
|
|
implements Callable<T> { |
978 |
jsr166 |
1.45 |
protected abstract T realCall() throws Throwable; |
979 |
jsr166 |
1.40 |
|
980 |
|
|
public final T call() { |
981 |
|
|
try { |
982 |
|
|
T result = realCall(); |
983 |
|
|
threadShouldThrow("InterruptedException"); |
984 |
|
|
return result; |
985 |
|
|
} catch (InterruptedException success) { |
986 |
jsr166 |
1.81 |
threadAssertFalse(Thread.interrupted()); |
987 |
jsr166 |
1.40 |
} catch (Throwable t) { |
988 |
|
|
threadUnexpectedException(t); |
989 |
|
|
} |
990 |
|
|
return null; |
991 |
jsr166 |
1.35 |
} |
992 |
|
|
} |
993 |
|
|
|
994 |
jsr166 |
1.45 |
public static class NoOpRunnable implements Runnable { |
995 |
dl |
1.1 |
public void run() {} |
996 |
|
|
} |
997 |
|
|
|
998 |
jsr166 |
1.45 |
public static class NoOpCallable implements Callable { |
999 |
dl |
1.1 |
public Object call() { return Boolean.TRUE; } |
1000 |
dl |
1.10 |
} |
1001 |
|
|
|
1002 |
jsr166 |
1.45 |
public static final String TEST_STRING = "a test string"; |
1003 |
dl |
1.10 |
|
1004 |
jsr166 |
1.45 |
public static class StringTask implements Callable<String> { |
1005 |
dl |
1.10 |
public String call() { return TEST_STRING; } |
1006 |
|
|
} |
1007 |
|
|
|
1008 |
jsr166 |
1.48 |
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1009 |
|
|
return new CheckedCallable<String>() { |
1010 |
jsr166 |
1.64 |
protected String realCall() { |
1011 |
jsr166 |
1.48 |
try { |
1012 |
|
|
latch.await(); |
1013 |
|
|
} catch (InterruptedException quittingTime) {} |
1014 |
|
|
return TEST_STRING; |
1015 |
|
|
}}; |
1016 |
|
|
} |
1017 |
|
|
|
1018 |
jsr166 |
1.73 |
public Runnable awaiter(final CountDownLatch latch) { |
1019 |
|
|
return new CheckedRunnable() { |
1020 |
|
|
public void realRun() throws InterruptedException { |
1021 |
jsr166 |
1.79 |
await(latch); |
1022 |
jsr166 |
1.73 |
}}; |
1023 |
|
|
} |
1024 |
|
|
|
1025 |
jsr166 |
1.79 |
public void await(CountDownLatch latch) { |
1026 |
|
|
try { |
1027 |
|
|
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1028 |
|
|
} catch (Throwable t) { |
1029 |
|
|
threadUnexpectedException(t); |
1030 |
|
|
} |
1031 |
|
|
} |
1032 |
|
|
|
1033 |
jsr166 |
1.89 |
public void await(Semaphore semaphore) { |
1034 |
|
|
try { |
1035 |
|
|
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1036 |
|
|
} catch (Throwable t) { |
1037 |
|
|
threadUnexpectedException(t); |
1038 |
|
|
} |
1039 |
|
|
} |
1040 |
|
|
|
1041 |
jsr166 |
1.81 |
// /** |
1042 |
|
|
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1043 |
|
|
// */ |
1044 |
|
|
// public void await(AtomicBoolean flag) { |
1045 |
|
|
// await(flag, LONG_DELAY_MS); |
1046 |
|
|
// } |
1047 |
|
|
|
1048 |
|
|
// /** |
1049 |
|
|
// * Spin-waits up to the specified timeout until flag becomes true. |
1050 |
|
|
// */ |
1051 |
|
|
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1052 |
|
|
// long startTime = System.nanoTime(); |
1053 |
|
|
// while (!flag.get()) { |
1054 |
|
|
// if (millisElapsedSince(startTime) > timeoutMillis) |
1055 |
|
|
// throw new AssertionFailedError("timed out"); |
1056 |
|
|
// Thread.yield(); |
1057 |
|
|
// } |
1058 |
|
|
// } |
1059 |
|
|
|
1060 |
jsr166 |
1.45 |
public static class NPETask implements Callable<String> { |
1061 |
dl |
1.10 |
public String call() { throw new NullPointerException(); } |
1062 |
|
|
} |
1063 |
|
|
|
1064 |
jsr166 |
1.45 |
public static class CallableOne implements Callable<Integer> { |
1065 |
dl |
1.10 |
public Integer call() { return one; } |
1066 |
dl |
1.1 |
} |
1067 |
|
|
|
1068 |
jsr166 |
1.45 |
public class ShortRunnable extends CheckedRunnable { |
1069 |
|
|
protected void realRun() throws Throwable { |
1070 |
dl |
1.76 |
delay(SHORT_DELAY_MS); |
1071 |
dl |
1.1 |
} |
1072 |
|
|
} |
1073 |
|
|
|
1074 |
jsr166 |
1.45 |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1075 |
|
|
protected void realRun() throws InterruptedException { |
1076 |
dl |
1.76 |
delay(SHORT_DELAY_MS); |
1077 |
dl |
1.1 |
} |
1078 |
|
|
} |
1079 |
|
|
|
1080 |
jsr166 |
1.45 |
public class SmallRunnable extends CheckedRunnable { |
1081 |
|
|
protected void realRun() throws Throwable { |
1082 |
dl |
1.76 |
delay(SMALL_DELAY_MS); |
1083 |
dl |
1.1 |
} |
1084 |
|
|
} |
1085 |
|
|
|
1086 |
jsr166 |
1.45 |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1087 |
|
|
protected void realRun() { |
1088 |
dl |
1.6 |
try { |
1089 |
dl |
1.76 |
delay(SMALL_DELAY_MS); |
1090 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1091 |
dl |
1.6 |
} |
1092 |
|
|
} |
1093 |
|
|
|
1094 |
jsr166 |
1.45 |
public class SmallCallable extends CheckedCallable { |
1095 |
|
|
protected Object realCall() throws InterruptedException { |
1096 |
dl |
1.76 |
delay(SMALL_DELAY_MS); |
1097 |
dl |
1.1 |
return Boolean.TRUE; |
1098 |
|
|
} |
1099 |
|
|
} |
1100 |
|
|
|
1101 |
jsr166 |
1.45 |
public class MediumRunnable extends CheckedRunnable { |
1102 |
|
|
protected void realRun() throws Throwable { |
1103 |
dl |
1.76 |
delay(MEDIUM_DELAY_MS); |
1104 |
dl |
1.1 |
} |
1105 |
|
|
} |
1106 |
|
|
|
1107 |
jsr166 |
1.45 |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1108 |
|
|
protected void realRun() throws InterruptedException { |
1109 |
dl |
1.76 |
delay(MEDIUM_DELAY_MS); |
1110 |
dl |
1.1 |
} |
1111 |
|
|
} |
1112 |
|
|
|
1113 |
jsr166 |
1.63 |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1114 |
|
|
return new CheckedRunnable() { |
1115 |
|
|
protected void realRun() { |
1116 |
|
|
try { |
1117 |
dl |
1.76 |
delay(timeoutMillis); |
1118 |
jsr166 |
1.63 |
} catch (InterruptedException ok) {} |
1119 |
|
|
}}; |
1120 |
|
|
} |
1121 |
|
|
|
1122 |
jsr166 |
1.45 |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1123 |
|
|
protected void realRun() { |
1124 |
dl |
1.1 |
try { |
1125 |
dl |
1.76 |
delay(MEDIUM_DELAY_MS); |
1126 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1127 |
dl |
1.1 |
} |
1128 |
|
|
} |
1129 |
dl |
1.5 |
|
1130 |
jsr166 |
1.45 |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1131 |
|
|
protected void realRun() { |
1132 |
dl |
1.12 |
try { |
1133 |
dl |
1.76 |
delay(LONG_DELAY_MS); |
1134 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1135 |
dl |
1.12 |
} |
1136 |
|
|
} |
1137 |
|
|
|
1138 |
dl |
1.5 |
/** |
1139 |
|
|
* For use as ThreadFactory in constructors |
1140 |
|
|
*/ |
1141 |
jsr166 |
1.45 |
public static class SimpleThreadFactory implements ThreadFactory { |
1142 |
jsr166 |
1.33 |
public Thread newThread(Runnable r) { |
1143 |
dl |
1.5 |
return new Thread(r); |
1144 |
jsr166 |
1.27 |
} |
1145 |
dl |
1.5 |
} |
1146 |
|
|
|
1147 |
jsr166 |
1.61 |
public interface TrackedRunnable extends Runnable { |
1148 |
|
|
boolean isDone(); |
1149 |
|
|
} |
1150 |
|
|
|
1151 |
|
|
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1152 |
|
|
return new TrackedRunnable() { |
1153 |
|
|
private volatile boolean done = false; |
1154 |
|
|
public boolean isDone() { return done; } |
1155 |
|
|
public void run() { |
1156 |
|
|
try { |
1157 |
dl |
1.76 |
delay(timeoutMillis); |
1158 |
jsr166 |
1.61 |
done = true; |
1159 |
|
|
} catch (InterruptedException ok) {} |
1160 |
|
|
} |
1161 |
|
|
}; |
1162 |
|
|
} |
1163 |
|
|
|
1164 |
jsr166 |
1.45 |
public static class TrackedShortRunnable implements Runnable { |
1165 |
|
|
public volatile boolean done = false; |
1166 |
dl |
1.5 |
public void run() { |
1167 |
|
|
try { |
1168 |
dl |
1.76 |
delay(SHORT_DELAY_MS); |
1169 |
jsr166 |
1.61 |
done = true; |
1170 |
|
|
} catch (InterruptedException ok) {} |
1171 |
|
|
} |
1172 |
|
|
} |
1173 |
|
|
|
1174 |
|
|
public static class TrackedSmallRunnable implements Runnable { |
1175 |
|
|
public volatile boolean done = false; |
1176 |
|
|
public void run() { |
1177 |
|
|
try { |
1178 |
dl |
1.76 |
delay(SMALL_DELAY_MS); |
1179 |
dl |
1.5 |
done = true; |
1180 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1181 |
dl |
1.6 |
} |
1182 |
|
|
} |
1183 |
|
|
|
1184 |
jsr166 |
1.45 |
public static class TrackedMediumRunnable implements Runnable { |
1185 |
|
|
public volatile boolean done = false; |
1186 |
dl |
1.6 |
public void run() { |
1187 |
|
|
try { |
1188 |
dl |
1.76 |
delay(MEDIUM_DELAY_MS); |
1189 |
dl |
1.6 |
done = true; |
1190 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1191 |
dl |
1.6 |
} |
1192 |
|
|
} |
1193 |
|
|
|
1194 |
jsr166 |
1.45 |
public static class TrackedLongRunnable implements Runnable { |
1195 |
|
|
public volatile boolean done = false; |
1196 |
dl |
1.6 |
public void run() { |
1197 |
|
|
try { |
1198 |
dl |
1.76 |
delay(LONG_DELAY_MS); |
1199 |
dl |
1.6 |
done = true; |
1200 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1201 |
dl |
1.6 |
} |
1202 |
|
|
} |
1203 |
|
|
|
1204 |
jsr166 |
1.45 |
public static class TrackedNoOpRunnable implements Runnable { |
1205 |
|
|
public volatile boolean done = false; |
1206 |
dl |
1.6 |
public void run() { |
1207 |
|
|
done = true; |
1208 |
dl |
1.5 |
} |
1209 |
|
|
} |
1210 |
|
|
|
1211 |
jsr166 |
1.45 |
public static class TrackedCallable implements Callable { |
1212 |
|
|
public volatile boolean done = false; |
1213 |
dl |
1.5 |
public Object call() { |
1214 |
|
|
try { |
1215 |
dl |
1.76 |
delay(SMALL_DELAY_MS); |
1216 |
dl |
1.5 |
done = true; |
1217 |
jsr166 |
1.44 |
} catch (InterruptedException ok) {} |
1218 |
dl |
1.5 |
return Boolean.TRUE; |
1219 |
|
|
} |
1220 |
|
|
} |
1221 |
dl |
1.14 |
|
1222 |
jsr166 |
1.53 |
/** |
1223 |
|
|
* Analog of CheckedRunnable for RecursiveAction |
1224 |
|
|
*/ |
1225 |
|
|
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1226 |
|
|
protected abstract void realCompute() throws Throwable; |
1227 |
|
|
|
1228 |
|
|
public final void compute() { |
1229 |
|
|
try { |
1230 |
|
|
realCompute(); |
1231 |
|
|
} catch (Throwable t) { |
1232 |
|
|
threadUnexpectedException(t); |
1233 |
|
|
} |
1234 |
|
|
} |
1235 |
|
|
} |
1236 |
|
|
|
1237 |
|
|
/** |
1238 |
|
|
* Analog of CheckedCallable for RecursiveTask |
1239 |
|
|
*/ |
1240 |
|
|
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1241 |
|
|
protected abstract T realCompute() throws Throwable; |
1242 |
|
|
|
1243 |
|
|
public final T compute() { |
1244 |
|
|
try { |
1245 |
|
|
return realCompute(); |
1246 |
|
|
} catch (Throwable t) { |
1247 |
|
|
threadUnexpectedException(t); |
1248 |
|
|
return null; |
1249 |
|
|
} |
1250 |
|
|
} |
1251 |
|
|
} |
1252 |
dl |
1.5 |
|
1253 |
|
|
/** |
1254 |
|
|
* For use as RejectedExecutionHandler in constructors |
1255 |
|
|
*/ |
1256 |
jsr166 |
1.45 |
public static class NoOpREHandler implements RejectedExecutionHandler { |
1257 |
jsr166 |
1.35 |
public void rejectedExecution(Runnable r, |
1258 |
|
|
ThreadPoolExecutor executor) {} |
1259 |
dl |
1.5 |
} |
1260 |
jsr166 |
1.27 |
|
1261 |
jsr166 |
1.60 |
/** |
1262 |
jsr166 |
1.86 |
* A CyclicBarrier that uses timed await and fails with |
1263 |
|
|
* AssertionFailedErrors instead of throwing checked exceptions. |
1264 |
jsr166 |
1.60 |
*/ |
1265 |
|
|
public class CheckedBarrier extends CyclicBarrier { |
1266 |
|
|
public CheckedBarrier(int parties) { super(parties); } |
1267 |
|
|
|
1268 |
|
|
public int await() { |
1269 |
|
|
try { |
1270 |
jsr166 |
1.86 |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1271 |
|
|
} catch (TimeoutException e) { |
1272 |
|
|
throw new AssertionFailedError("timed out"); |
1273 |
jsr166 |
1.60 |
} catch (Exception e) { |
1274 |
|
|
AssertionFailedError afe = |
1275 |
|
|
new AssertionFailedError("Unexpected exception: " + e); |
1276 |
|
|
afe.initCause(e); |
1277 |
|
|
throw afe; |
1278 |
|
|
} |
1279 |
|
|
} |
1280 |
|
|
} |
1281 |
|
|
|
1282 |
jsr166 |
1.81 |
void checkEmpty(BlockingQueue q) { |
1283 |
jsr166 |
1.72 |
try { |
1284 |
|
|
assertTrue(q.isEmpty()); |
1285 |
|
|
assertEquals(0, q.size()); |
1286 |
|
|
assertNull(q.peek()); |
1287 |
|
|
assertNull(q.poll()); |
1288 |
|
|
assertNull(q.poll(0, MILLISECONDS)); |
1289 |
|
|
assertEquals(q.toString(), "[]"); |
1290 |
|
|
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1291 |
|
|
assertFalse(q.iterator().hasNext()); |
1292 |
|
|
try { |
1293 |
|
|
q.element(); |
1294 |
|
|
shouldThrow(); |
1295 |
|
|
} catch (NoSuchElementException success) {} |
1296 |
|
|
try { |
1297 |
|
|
q.iterator().next(); |
1298 |
|
|
shouldThrow(); |
1299 |
|
|
} catch (NoSuchElementException success) {} |
1300 |
|
|
try { |
1301 |
|
|
q.remove(); |
1302 |
|
|
shouldThrow(); |
1303 |
|
|
} catch (NoSuchElementException success) {} |
1304 |
|
|
} catch (InterruptedException ie) { |
1305 |
|
|
threadUnexpectedException(ie); |
1306 |
|
|
} |
1307 |
|
|
} |
1308 |
|
|
|
1309 |
jsr166 |
1.91 |
void assertSerialEquals(Object x, Object y) { |
1310 |
|
|
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1311 |
|
|
} |
1312 |
|
|
|
1313 |
|
|
void assertNotSerialEquals(Object x, Object y) { |
1314 |
|
|
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1315 |
|
|
} |
1316 |
|
|
|
1317 |
|
|
byte[] serialBytes(Object o) { |
1318 |
jsr166 |
1.79 |
try { |
1319 |
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1320 |
|
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1321 |
|
|
oos.writeObject(o); |
1322 |
|
|
oos.flush(); |
1323 |
|
|
oos.close(); |
1324 |
jsr166 |
1.91 |
return bos.toByteArray(); |
1325 |
|
|
} catch (Throwable t) { |
1326 |
|
|
threadUnexpectedException(t); |
1327 |
|
|
return new byte[0]; |
1328 |
|
|
} |
1329 |
|
|
} |
1330 |
|
|
|
1331 |
|
|
@SuppressWarnings("unchecked") |
1332 |
|
|
<T> T serialClone(T o) { |
1333 |
|
|
try { |
1334 |
jsr166 |
1.87 |
ObjectInputStream ois = new ObjectInputStream |
1335 |
jsr166 |
1.91 |
(new ByteArrayInputStream(serialBytes(o))); |
1336 |
jsr166 |
1.87 |
T clone = (T) ois.readObject(); |
1337 |
|
|
assertSame(o.getClass(), clone.getClass()); |
1338 |
|
|
return clone; |
1339 |
jsr166 |
1.79 |
} catch (Throwable t) { |
1340 |
|
|
threadUnexpectedException(t); |
1341 |
|
|
return null; |
1342 |
|
|
} |
1343 |
|
|
} |
1344 |
dl |
1.1 |
} |