1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* 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 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.PropertyPermission; |
11 |
|
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
13 |
< |
|
12 |
> |
import java.util.concurrent.atomic.AtomicReference; |
13 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
> |
import java.security.CodeSource; |
15 |
> |
import java.security.Permission; |
16 |
> |
import java.security.PermissionCollection; |
17 |
> |
import java.security.Permissions; |
18 |
> |
import java.security.Policy; |
19 |
> |
import java.security.ProtectionDomain; |
20 |
> |
import java.security.SecurityPermission; |
21 |
|
|
22 |
|
/** |
23 |
< |
* Base class for JSR166 Junit TCK tests. Defines some constants and |
24 |
< |
* utility methods, as well as a simple framework for helping to make |
25 |
< |
* sure that assertions failing in generated threads cause the |
26 |
< |
* associated test that generated them to itself fail (which JUnit doe |
27 |
< |
* not otherwise arrange). The rules for creating such tests are: |
23 |
> |
* Base class for JSR166 Junit TCK tests. Defines some constants, |
24 |
> |
* utility methods and classes, as well as a simple framework for |
25 |
> |
* helping to make sure that assertions failing in generated threads |
26 |
> |
* cause the associated test that generated them to itself fail (which |
27 |
> |
* JUnit does not otherwise arrange). The rules for creating such |
28 |
> |
* tests are: |
29 |
|
* |
30 |
|
* <ol> |
31 |
|
* |
32 |
|
* <li> All assertions in code running in generated threads must use |
33 |
< |
* the forms {@link threadFail} , {@link threadAssertTrue} {@link |
34 |
< |
* threadAssertEquals}, or {@link threadAssertNull}, (not |
35 |
< |
* <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not |
33 |
> |
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
34 |
> |
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
35 |
> |
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
36 |
|
* particularly recommended) for other code to use these forms too. |
37 |
|
* Only the most typically used JUnit assertion methods are defined |
38 |
|
* this way, but enough to live with.</li> |
39 |
|
* |
40 |
< |
* <li> If you override {@link setUp} or {@link tearDown}, make sure |
41 |
< |
* to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within |
40 |
> |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
41 |
> |
* to invoke {@code super.setUp} and {@code super.tearDown} within |
42 |
|
* them. These methods are used to clear and check for thread |
43 |
|
* assertion failures.</li> |
44 |
|
* |
45 |
< |
* <li>All delays and timeouts must use one of the constants {@link |
46 |
< |
* SHORT_DELAY_MS}, {@link SMALL_DELAY_MS}, {@link MEDIUM_DELAY_MS}, |
47 |
< |
* {@link LONG_DELAY_MS}. The idea here is that a SHORT is always |
48 |
< |
* discriminatable from zero time, and always allows enough time for |
49 |
< |
* the small amounts of computation (creating a thread, calling a few |
45 |
> |
* <li>All delays and timeouts must use one of the constants {@code |
46 |
> |
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
47 |
> |
* {@code LONG_DELAY_MS}. The idea here is that a SHORT is always |
48 |
> |
* discriminable from zero time, and always allows enough time for the |
49 |
> |
* small amounts of computation (creating a thread, calling a few |
50 |
|
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
51 |
|
* is always discriminable as larger than SHORT and smaller than |
52 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
53 |
|
* but even so, if there is ever any doubt, they can all be increased |
54 |
< |
* in one spot to rerun tests on slower platforms</li> |
54 |
> |
* in one spot to rerun tests on slower platforms.</li> |
55 |
|
* |
56 |
|
* <li> All threads generated must be joined inside each test case |
57 |
< |
* method (or <tt>fail</tt> to do so) before returning from the |
58 |
< |
* method. The {@link joinPool} method can be used to do this when |
57 |
> |
* method (or {@code fail} to do so) before returning from the |
58 |
> |
* method. The {@code joinPool} method can be used to do this when |
59 |
|
* using Executors.</li> |
60 |
|
* |
61 |
|
* </ol> |
62 |
+ |
* |
63 |
+ |
* <p> <b>Other notes</b> |
64 |
+ |
* <ul> |
65 |
+ |
* |
66 |
+ |
* <li> Usually, there is one testcase method per JSR166 method |
67 |
+ |
* covering "normal" operation, and then as many exception-testing |
68 |
+ |
* methods as there are exceptions the method can throw. Sometimes |
69 |
+ |
* there are multiple tests per JSR166 method when the different |
70 |
+ |
* "normal" behaviors differ significantly. And sometimes testcases |
71 |
+ |
* cover multiple methods when they cannot be tested in |
72 |
+ |
* isolation.</li> |
73 |
+ |
* |
74 |
+ |
* <li> The documentation style for testcases is to provide as javadoc |
75 |
+ |
* a simple sentence or two describing the property that the testcase |
76 |
+ |
* method purports to test. The javadocs do not say anything about how |
77 |
+ |
* the property is tested. To find out, read the code.</li> |
78 |
+ |
* |
79 |
+ |
* <li> These tests are "conformance tests", and do not attempt to |
80 |
+ |
* test throughput, latency, scalability or other performance factors |
81 |
+ |
* (see the separate "jtreg" tests for a set intended to check these |
82 |
+ |
* for the most central aspects of functionality.) So, most tests use |
83 |
+ |
* the smallest sensible numbers of threads, collection sizes, etc |
84 |
+ |
* needed to check basic conformance.</li> |
85 |
+ |
* |
86 |
+ |
* <li>The test classes currently do not declare inclusion in |
87 |
+ |
* any particular package to simplify things for people integrating |
88 |
+ |
* them in TCK test suites.</li> |
89 |
+ |
* |
90 |
+ |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
91 |
+ |
* runs all JSR166 unit tests.</li> |
92 |
+ |
* |
93 |
+ |
* </ul> |
94 |
|
*/ |
95 |
|
public class JSR166TestCase extends TestCase { |
96 |
+ |
private static final boolean useSecurityManager = |
97 |
+ |
Boolean.getBoolean("jsr166.useSecurityManager"); |
98 |
+ |
|
99 |
+ |
/** |
100 |
+ |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
101 |
+ |
*/ |
102 |
+ |
public static void main(String[] args) { |
103 |
+ |
if (useSecurityManager) { |
104 |
+ |
System.err.println("Setting a permissive security manager"); |
105 |
+ |
Policy.setPolicy(permissivePolicy()); |
106 |
+ |
System.setSecurityManager(new SecurityManager()); |
107 |
+ |
} |
108 |
+ |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
109 |
+ |
|
110 |
+ |
Test s = suite(); |
111 |
+ |
for (int i = 0; i < iters; ++i) { |
112 |
+ |
junit.textui.TestRunner.run(s); |
113 |
+ |
System.gc(); |
114 |
+ |
System.runFinalization(); |
115 |
+ |
} |
116 |
+ |
System.exit(0); |
117 |
+ |
} |
118 |
+ |
|
119 |
+ |
/** |
120 |
+ |
* Collects all JSR166 unit tests as one suite |
121 |
+ |
*/ |
122 |
+ |
public static Test suite() { |
123 |
+ |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
124 |
+ |
|
125 |
+ |
suite.addTest(ForkJoinPoolTest.suite()); |
126 |
+ |
suite.addTest(ForkJoinTaskTest.suite()); |
127 |
+ |
suite.addTest(RecursiveActionTest.suite()); |
128 |
+ |
suite.addTest(RecursiveTaskTest.suite()); |
129 |
+ |
suite.addTest(LinkedTransferQueueTest.suite()); |
130 |
+ |
suite.addTest(PhaserTest.suite()); |
131 |
+ |
suite.addTest(ThreadLocalRandomTest.suite()); |
132 |
+ |
suite.addTest(AbstractExecutorServiceTest.suite()); |
133 |
+ |
suite.addTest(AbstractQueueTest.suite()); |
134 |
+ |
suite.addTest(AbstractQueuedSynchronizerTest.suite()); |
135 |
+ |
suite.addTest(AbstractQueuedLongSynchronizerTest.suite()); |
136 |
+ |
suite.addTest(ArrayBlockingQueueTest.suite()); |
137 |
+ |
suite.addTest(ArrayDequeTest.suite()); |
138 |
+ |
suite.addTest(AtomicBooleanTest.suite()); |
139 |
+ |
suite.addTest(AtomicIntegerArrayTest.suite()); |
140 |
+ |
suite.addTest(AtomicIntegerFieldUpdaterTest.suite()); |
141 |
+ |
suite.addTest(AtomicIntegerTest.suite()); |
142 |
+ |
suite.addTest(AtomicLongArrayTest.suite()); |
143 |
+ |
suite.addTest(AtomicLongFieldUpdaterTest.suite()); |
144 |
+ |
suite.addTest(AtomicLongTest.suite()); |
145 |
+ |
suite.addTest(AtomicMarkableReferenceTest.suite()); |
146 |
+ |
suite.addTest(AtomicReferenceArrayTest.suite()); |
147 |
+ |
suite.addTest(AtomicReferenceFieldUpdaterTest.suite()); |
148 |
+ |
suite.addTest(AtomicReferenceTest.suite()); |
149 |
+ |
suite.addTest(AtomicStampedReferenceTest.suite()); |
150 |
+ |
suite.addTest(ConcurrentHashMapTest.suite()); |
151 |
+ |
suite.addTest(ConcurrentLinkedDequeTest.suite()); |
152 |
+ |
suite.addTest(ConcurrentLinkedQueueTest.suite()); |
153 |
+ |
suite.addTest(ConcurrentSkipListMapTest.suite()); |
154 |
+ |
suite.addTest(ConcurrentSkipListSubMapTest.suite()); |
155 |
+ |
suite.addTest(ConcurrentSkipListSetTest.suite()); |
156 |
+ |
suite.addTest(ConcurrentSkipListSubSetTest.suite()); |
157 |
+ |
suite.addTest(CopyOnWriteArrayListTest.suite()); |
158 |
+ |
suite.addTest(CopyOnWriteArraySetTest.suite()); |
159 |
+ |
suite.addTest(CountDownLatchTest.suite()); |
160 |
+ |
suite.addTest(CyclicBarrierTest.suite()); |
161 |
+ |
suite.addTest(DelayQueueTest.suite()); |
162 |
+ |
suite.addTest(EntryTest.suite()); |
163 |
+ |
suite.addTest(ExchangerTest.suite()); |
164 |
+ |
suite.addTest(ExecutorsTest.suite()); |
165 |
+ |
suite.addTest(ExecutorCompletionServiceTest.suite()); |
166 |
+ |
suite.addTest(FutureTaskTest.suite()); |
167 |
+ |
suite.addTest(LinkedBlockingDequeTest.suite()); |
168 |
+ |
suite.addTest(LinkedBlockingQueueTest.suite()); |
169 |
+ |
suite.addTest(LinkedListTest.suite()); |
170 |
+ |
suite.addTest(LockSupportTest.suite()); |
171 |
+ |
suite.addTest(PriorityBlockingQueueTest.suite()); |
172 |
+ |
suite.addTest(PriorityQueueTest.suite()); |
173 |
+ |
suite.addTest(ReentrantLockTest.suite()); |
174 |
+ |
suite.addTest(ReentrantReadWriteLockTest.suite()); |
175 |
+ |
suite.addTest(ScheduledExecutorTest.suite()); |
176 |
+ |
suite.addTest(ScheduledExecutorSubclassTest.suite()); |
177 |
+ |
suite.addTest(SemaphoreTest.suite()); |
178 |
+ |
suite.addTest(SynchronousQueueTest.suite()); |
179 |
+ |
suite.addTest(SystemTest.suite()); |
180 |
+ |
suite.addTest(ThreadLocalTest.suite()); |
181 |
+ |
suite.addTest(ThreadPoolExecutorTest.suite()); |
182 |
+ |
suite.addTest(ThreadPoolExecutorSubclassTest.suite()); |
183 |
+ |
suite.addTest(ThreadTest.suite()); |
184 |
+ |
suite.addTest(TimeUnitTest.suite()); |
185 |
+ |
suite.addTest(TreeMapTest.suite()); |
186 |
+ |
suite.addTest(TreeSetTest.suite()); |
187 |
+ |
suite.addTest(TreeSubMapTest.suite()); |
188 |
+ |
suite.addTest(TreeSubSetTest.suite()); |
189 |
+ |
|
190 |
+ |
return suite; |
191 |
+ |
} |
192 |
+ |
|
193 |
|
|
194 |
|
public static long SHORT_DELAY_MS; |
195 |
|
public static long SMALL_DELAY_MS; |
198 |
|
|
199 |
|
|
200 |
|
/** |
201 |
< |
* Return the shortest timed delay. This could |
202 |
< |
* be reimplmented to use for example a Property. |
203 |
< |
*/ |
201 |
> |
* Returns the shortest timed delay. This could |
202 |
> |
* be reimplemented to use for example a Property. |
203 |
> |
*/ |
204 |
|
protected long getShortDelay() { |
205 |
|
return 50; |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
|
/** |
210 |
< |
* Set delays as multiples fo SHORT_DELAY. |
210 |
> |
* Sets delays as multiples of SHORT_DELAY. |
211 |
|
*/ |
212 |
< |
protected void setDelays() { |
212 |
> |
protected void setDelays() { |
213 |
|
SHORT_DELAY_MS = getShortDelay(); |
214 |
< |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
214 |
> |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
215 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
216 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
216 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* Flag set true if any threadAssert methods fail |
220 |
> |
* The first exception encountered if any threadAssertXXX method fails. |
221 |
|
*/ |
222 |
< |
protected volatile boolean threadFailed; |
222 |
> |
private final AtomicReference<Throwable> threadFailure |
223 |
> |
= new AtomicReference<Throwable>(null); |
224 |
|
|
225 |
|
/** |
226 |
< |
* Initialize test to indicat that no thread assertions have failed |
226 |
> |
* Records an exception so that it can be rethrown later in the test |
227 |
> |
* harness thread, triggering a test case failure. Only the first |
228 |
> |
* failure is recorded; subsequent calls to this method from within |
229 |
> |
* the same test have no effect. |
230 |
|
*/ |
231 |
< |
public void setUp() { |
231 |
> |
public void threadRecordFailure(Throwable t) { |
232 |
> |
threadFailure.compareAndSet(null, t); |
233 |
> |
} |
234 |
> |
|
235 |
> |
public void setUp() { |
236 |
|
setDelays(); |
91 |
– |
threadFailed = false; |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
< |
* Trigger test case failure if any thread assertions have failed |
240 |
> |
* Triggers test case failure if any thread assertions have failed, |
241 |
> |
* by rethrowing, in the test harness thread, any exception recorded |
242 |
> |
* earlier by threadRecordFailure. |
243 |
|
*/ |
244 |
< |
public void tearDown() { |
245 |
< |
assertFalse(threadFailed); |
244 |
> |
public void tearDown() throws Exception { |
245 |
> |
Throwable t = threadFailure.get(); |
246 |
> |
if (t != null) { |
247 |
> |
if (t instanceof Error) |
248 |
> |
throw (Error) t; |
249 |
> |
else if (t instanceof RuntimeException) |
250 |
> |
throw (RuntimeException) t; |
251 |
> |
else if (t instanceof Exception) |
252 |
> |
throw (Exception) t; |
253 |
> |
else { |
254 |
> |
AssertionFailedError afe = |
255 |
> |
new AssertionFailedError(t.toString()); |
256 |
> |
afe.initCause(t); |
257 |
> |
throw afe; |
258 |
> |
} |
259 |
> |
} |
260 |
|
} |
261 |
|
|
262 |
+ |
/** |
263 |
+ |
* Just like fail(reason), but additionally recording (using |
264 |
+ |
* threadRecordFailure) any AssertionFailedError thrown, so that |
265 |
+ |
* the current testcase will fail. |
266 |
+ |
*/ |
267 |
|
public void threadFail(String reason) { |
268 |
< |
threadFailed = true; |
269 |
< |
fail(reason); |
268 |
> |
try { |
269 |
> |
fail(reason); |
270 |
> |
} catch (AssertionFailedError t) { |
271 |
> |
threadRecordFailure(t); |
272 |
> |
fail(reason); |
273 |
> |
} |
274 |
|
} |
275 |
|
|
276 |
+ |
/** |
277 |
+ |
* Just like assertTrue(b), but additionally recording (using |
278 |
+ |
* threadRecordFailure) any AssertionFailedError thrown, so that |
279 |
+ |
* the current testcase will fail. |
280 |
+ |
*/ |
281 |
|
public void threadAssertTrue(boolean b) { |
282 |
< |
if (!b) { |
108 |
< |
threadFailed = true; |
282 |
> |
try { |
283 |
|
assertTrue(b); |
284 |
+ |
} catch (AssertionFailedError t) { |
285 |
+ |
threadRecordFailure(t); |
286 |
+ |
throw t; |
287 |
|
} |
288 |
|
} |
289 |
+ |
|
290 |
+ |
/** |
291 |
+ |
* Just like assertFalse(b), but additionally recording (using |
292 |
+ |
* threadRecordFailure) any AssertionFailedError thrown, so that |
293 |
+ |
* the current testcase will fail. |
294 |
+ |
*/ |
295 |
|
public void threadAssertFalse(boolean b) { |
296 |
< |
if (b) { |
114 |
< |
threadFailed = true; |
296 |
> |
try { |
297 |
|
assertFalse(b); |
298 |
+ |
} catch (AssertionFailedError t) { |
299 |
+ |
threadRecordFailure(t); |
300 |
+ |
throw t; |
301 |
|
} |
302 |
|
} |
303 |
+ |
|
304 |
+ |
/** |
305 |
+ |
* Just like assertNull(x), but additionally recording (using |
306 |
+ |
* threadRecordFailure) any AssertionFailedError thrown, so that |
307 |
+ |
* the current testcase will fail. |
308 |
+ |
*/ |
309 |
|
public void threadAssertNull(Object x) { |
310 |
< |
if (x != null) { |
120 |
< |
threadFailed = true; |
310 |
> |
try { |
311 |
|
assertNull(x); |
312 |
+ |
} catch (AssertionFailedError t) { |
313 |
+ |
threadRecordFailure(t); |
314 |
+ |
throw t; |
315 |
|
} |
316 |
|
} |
317 |
+ |
|
318 |
+ |
/** |
319 |
+ |
* Just like assertEquals(x, y), but additionally recording (using |
320 |
+ |
* threadRecordFailure) any AssertionFailedError thrown, so that |
321 |
+ |
* the current testcase will fail. |
322 |
+ |
*/ |
323 |
|
public void threadAssertEquals(long x, long y) { |
324 |
< |
if (x != y) { |
126 |
< |
threadFailed = true; |
324 |
> |
try { |
325 |
|
assertEquals(x, y); |
326 |
+ |
} catch (AssertionFailedError t) { |
327 |
+ |
threadRecordFailure(t); |
328 |
+ |
throw t; |
329 |
|
} |
330 |
|
} |
331 |
+ |
|
332 |
+ |
/** |
333 |
+ |
* Just like assertEquals(x, y), but additionally recording (using |
334 |
+ |
* threadRecordFailure) any AssertionFailedError thrown, so that |
335 |
+ |
* the current testcase will fail. |
336 |
+ |
*/ |
337 |
|
public void threadAssertEquals(Object x, Object y) { |
338 |
< |
if (x != y && (x == null || !x.equals(y))) { |
132 |
< |
threadFailed = true; |
338 |
> |
try { |
339 |
|
assertEquals(x, y); |
340 |
+ |
} catch (AssertionFailedError t) { |
341 |
+ |
threadRecordFailure(t); |
342 |
+ |
throw t; |
343 |
+ |
} catch (Throwable t) { |
344 |
+ |
threadUnexpectedException(t); |
345 |
+ |
} |
346 |
+ |
} |
347 |
+ |
|
348 |
+ |
/** |
349 |
+ |
* Just like assertSame(x, y), but additionally recording (using |
350 |
+ |
* threadRecordFailure) any AssertionFailedError thrown, so that |
351 |
+ |
* the current testcase will fail. |
352 |
+ |
*/ |
353 |
+ |
public void threadAssertSame(Object x, Object y) { |
354 |
+ |
try { |
355 |
+ |
assertSame(x, y); |
356 |
+ |
} catch (AssertionFailedError t) { |
357 |
+ |
threadRecordFailure(t); |
358 |
+ |
throw t; |
359 |
|
} |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
< |
* Wait out termination of a thread pool or fail doing so |
363 |
> |
* Calls threadFail with message "should throw exception". |
364 |
> |
*/ |
365 |
> |
public void threadShouldThrow() { |
366 |
> |
threadFail("should throw exception"); |
367 |
> |
} |
368 |
> |
|
369 |
> |
/** |
370 |
> |
* Calls threadFail with message "should throw" + exceptionName. |
371 |
> |
*/ |
372 |
> |
public void threadShouldThrow(String exceptionName) { |
373 |
> |
threadFail("should throw " + exceptionName); |
374 |
> |
} |
375 |
> |
|
376 |
> |
/** |
377 |
> |
* Records the given exception using {@link #threadRecordFailure}, |
378 |
> |
* then rethrows the exception, wrapping it in an |
379 |
> |
* AssertionFailedError if necessary. |
380 |
> |
*/ |
381 |
> |
public void threadUnexpectedException(Throwable t) { |
382 |
> |
threadRecordFailure(t); |
383 |
> |
t.printStackTrace(); |
384 |
> |
if (t instanceof RuntimeException) |
385 |
> |
throw (RuntimeException) t; |
386 |
> |
else if (t instanceof Error) |
387 |
> |
throw (Error) t; |
388 |
> |
else { |
389 |
> |
AssertionFailedError afe = |
390 |
> |
new AssertionFailedError("unexpected exception: " + t); |
391 |
> |
t.initCause(t); |
392 |
> |
throw afe; |
393 |
> |
} |
394 |
> |
} |
395 |
> |
|
396 |
> |
/** |
397 |
> |
* Waits out termination of a thread pool or fails doing so. |
398 |
|
*/ |
399 |
|
public void joinPool(ExecutorService exec) { |
400 |
|
try { |
401 |
|
exec.shutdown(); |
402 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
403 |
< |
} catch(InterruptedException ie) { |
404 |
< |
fail("unexpected exception"); |
402 |
> |
assertTrue("ExecutorService did not terminate in a timely manner", |
403 |
> |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
404 |
> |
} catch (SecurityException ok) { |
405 |
> |
// Allowed in case test doesn't have privs |
406 |
> |
} catch (InterruptedException ie) { |
407 |
> |
fail("Unexpected InterruptedException"); |
408 |
|
} |
409 |
|
} |
410 |
|
|
411 |
|
|
412 |
+ |
/** |
413 |
+ |
* Fails with message "should throw exception". |
414 |
+ |
*/ |
415 |
+ |
public void shouldThrow() { |
416 |
+ |
fail("Should throw exception"); |
417 |
+ |
} |
418 |
+ |
|
419 |
+ |
/** |
420 |
+ |
* Fails with message "should throw " + exceptionName. |
421 |
+ |
*/ |
422 |
+ |
public void shouldThrow(String exceptionName) { |
423 |
+ |
fail("Should throw " + exceptionName); |
424 |
+ |
} |
425 |
|
|
426 |
|
/** |
427 |
|
* The number of elements to place in collections, arrays, etc. |
430 |
|
|
431 |
|
// Some convenient Integer constants |
432 |
|
|
433 |
< |
public static final Integer zero = new Integer(0); |
434 |
< |
public static final Integer one = new Integer(1); |
435 |
< |
public static final Integer two = new Integer(2); |
436 |
< |
public static final Integer three = new Integer(3); |
433 |
> |
public static final Integer zero = new Integer(0); |
434 |
> |
public static final Integer one = new Integer(1); |
435 |
> |
public static final Integer two = new Integer(2); |
436 |
> |
public static final Integer three = new Integer(3); |
437 |
|
public static final Integer four = new Integer(4); |
438 |
|
public static final Integer five = new Integer(5); |
439 |
< |
public static final Integer six = new Integer(6); |
439 |
> |
public static final Integer six = new Integer(6); |
440 |
|
public static final Integer seven = new Integer(7); |
441 |
|
public static final Integer eight = new Integer(8); |
442 |
< |
public static final Integer nine = new Integer(9); |
442 |
> |
public static final Integer nine = new Integer(9); |
443 |
|
public static final Integer m1 = new Integer(-1); |
444 |
|
public static final Integer m2 = new Integer(-2); |
445 |
|
public static final Integer m3 = new Integer(-3); |
446 |
< |
public static final Integer m4 = new Integer(-4); |
447 |
< |
public static final Integer m5 = new Integer(-5); |
446 |
> |
public static final Integer m4 = new Integer(-4); |
447 |
> |
public static final Integer m5 = new Integer(-5); |
448 |
> |
public static final Integer m6 = new Integer(-6); |
449 |
|
public static final Integer m10 = new Integer(-10); |
450 |
|
|
451 |
|
|
452 |
< |
// Some convenient Runnable classes |
452 |
> |
/** |
453 |
> |
* Runs Runnable r with a security policy that permits precisely |
454 |
> |
* the specified permissions. If there is no current security |
455 |
> |
* manager, the runnable is run twice, both with and without a |
456 |
> |
* security manager. We require that any security manager permit |
457 |
> |
* getPolicy/setPolicy. |
458 |
> |
*/ |
459 |
> |
public void runWithPermissions(Runnable r, Permission... permissions) { |
460 |
> |
SecurityManager sm = System.getSecurityManager(); |
461 |
> |
if (sm == null) { |
462 |
> |
r.run(); |
463 |
> |
Policy savedPolicy = Policy.getPolicy(); |
464 |
> |
try { |
465 |
> |
Policy.setPolicy(permissivePolicy()); |
466 |
> |
System.setSecurityManager(new SecurityManager()); |
467 |
> |
runWithPermissions(r, permissions); |
468 |
> |
} finally { |
469 |
> |
System.setSecurityManager(null); |
470 |
> |
Policy.setPolicy(savedPolicy); |
471 |
> |
} |
472 |
> |
} else { |
473 |
> |
Policy savedPolicy = Policy.getPolicy(); |
474 |
> |
AdjustablePolicy policy = new AdjustablePolicy(permissions); |
475 |
> |
Policy.setPolicy(policy); |
476 |
|
|
477 |
< |
public static class NoOpRunnable implements Runnable { |
478 |
< |
public void run() {} |
477 |
> |
try { |
478 |
> |
r.run(); |
479 |
> |
} finally { |
480 |
> |
policy.addPermission(new SecurityPermission("setPolicy")); |
481 |
> |
Policy.setPolicy(savedPolicy); |
482 |
> |
} |
483 |
> |
} |
484 |
|
} |
485 |
|
|
486 |
< |
public static class NoOpCallable implements Callable { |
487 |
< |
public Object call() { return Boolean.TRUE; } |
486 |
> |
/** |
487 |
> |
* Runs a runnable without any permissions. |
488 |
> |
*/ |
489 |
> |
public void runWithoutPermissions(Runnable r) { |
490 |
> |
runWithPermissions(r); |
491 |
|
} |
492 |
|
|
493 |
< |
public class ShortRunnable implements Runnable { |
494 |
< |
public void run() { |
493 |
> |
/** |
494 |
> |
* A security policy where new permissions can be dynamically added |
495 |
> |
* or all cleared. |
496 |
> |
*/ |
497 |
> |
public static class AdjustablePolicy extends java.security.Policy { |
498 |
> |
Permissions perms = new Permissions(); |
499 |
> |
AdjustablePolicy(Permission... permissions) { |
500 |
> |
for (Permission permission : permissions) |
501 |
> |
perms.add(permission); |
502 |
> |
} |
503 |
> |
void addPermission(Permission perm) { perms.add(perm); } |
504 |
> |
void clearPermissions() { perms = new Permissions(); } |
505 |
> |
public PermissionCollection getPermissions(CodeSource cs) { |
506 |
> |
return perms; |
507 |
> |
} |
508 |
> |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
509 |
> |
return perms; |
510 |
> |
} |
511 |
> |
public boolean implies(ProtectionDomain pd, Permission p) { |
512 |
> |
return perms.implies(p); |
513 |
> |
} |
514 |
> |
public void refresh() {} |
515 |
> |
} |
516 |
> |
|
517 |
> |
/** |
518 |
> |
* Returns a policy containing all the permissions we ever need. |
519 |
> |
*/ |
520 |
> |
public static Policy permissivePolicy() { |
521 |
> |
return new AdjustablePolicy |
522 |
> |
// Permissions j.u.c. needs directly |
523 |
> |
(new RuntimePermission("modifyThread"), |
524 |
> |
new RuntimePermission("getClassLoader"), |
525 |
> |
new RuntimePermission("setContextClassLoader"), |
526 |
> |
// Permissions needed to change permissions! |
527 |
> |
new SecurityPermission("getPolicy"), |
528 |
> |
new SecurityPermission("setPolicy"), |
529 |
> |
new RuntimePermission("setSecurityManager"), |
530 |
> |
// Permissions needed by the junit test harness |
531 |
> |
new RuntimePermission("accessDeclaredMembers"), |
532 |
> |
new PropertyPermission("*", "read"), |
533 |
> |
new java.io.FilePermission("<<ALL FILES>>", "read")); |
534 |
> |
} |
535 |
> |
|
536 |
> |
/** |
537 |
> |
* Sleeps until the timeout has elapsed, or interrupted. |
538 |
> |
* Does <em>NOT</em> throw InterruptedException. |
539 |
> |
*/ |
540 |
> |
void sleepTillInterrupted(long timeoutMillis) { |
541 |
> |
try { |
542 |
> |
Thread.sleep(timeoutMillis); |
543 |
> |
} catch (InterruptedException wakeup) {} |
544 |
> |
} |
545 |
> |
|
546 |
> |
/** |
547 |
> |
* Returns a new started daemon Thread running the given runnable. |
548 |
> |
*/ |
549 |
> |
Thread newStartedThread(Runnable runnable) { |
550 |
> |
Thread t = new Thread(runnable); |
551 |
> |
t.setDaemon(true); |
552 |
> |
t.start(); |
553 |
> |
return t; |
554 |
> |
} |
555 |
> |
|
556 |
> |
/** |
557 |
> |
* Waits for the specified time (in milliseconds) for the thread |
558 |
> |
* to terminate (using {@link Thread#join(long)}), else interrupts |
559 |
> |
* the thread (in the hope that it may terminate later) and fails. |
560 |
> |
*/ |
561 |
> |
void awaitTermination(Thread t, long timeoutMillis) { |
562 |
> |
try { |
563 |
> |
t.join(timeoutMillis); |
564 |
> |
} catch (InterruptedException ie) { |
565 |
> |
threadUnexpectedException(ie); |
566 |
> |
} finally { |
567 |
> |
if (t.isAlive()) { |
568 |
> |
t.interrupt(); |
569 |
> |
fail("Test timed out"); |
570 |
> |
} |
571 |
> |
} |
572 |
> |
} |
573 |
> |
|
574 |
> |
// Some convenient Runnable classes |
575 |
> |
|
576 |
> |
public abstract class CheckedRunnable implements Runnable { |
577 |
> |
protected abstract void realRun() throws Throwable; |
578 |
> |
|
579 |
> |
public final void run() { |
580 |
|
try { |
581 |
< |
Thread.sleep(SHORT_DELAY_MS); |
581 |
> |
realRun(); |
582 |
> |
} catch (Throwable t) { |
583 |
> |
threadUnexpectedException(t); |
584 |
|
} |
585 |
< |
catch(Exception e) { |
586 |
< |
threadFail("unexpectedException"); |
585 |
> |
} |
586 |
> |
} |
587 |
> |
|
588 |
> |
public abstract class RunnableShouldThrow implements Runnable { |
589 |
> |
protected abstract void realRun() throws Throwable; |
590 |
> |
|
591 |
> |
final Class<?> exceptionClass; |
592 |
> |
|
593 |
> |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
594 |
> |
this.exceptionClass = exceptionClass; |
595 |
> |
} |
596 |
> |
|
597 |
> |
public final void run() { |
598 |
> |
try { |
599 |
> |
realRun(); |
600 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
601 |
> |
} catch (Throwable t) { |
602 |
> |
if (! exceptionClass.isInstance(t)) |
603 |
> |
threadUnexpectedException(t); |
604 |
|
} |
605 |
|
} |
606 |
|
} |
607 |
|
|
608 |
< |
public class ShortInterruptedRunnable implements Runnable { |
609 |
< |
public void run() { |
608 |
> |
public abstract class ThreadShouldThrow extends Thread { |
609 |
> |
protected abstract void realRun() throws Throwable; |
610 |
> |
|
611 |
> |
final Class<?> exceptionClass; |
612 |
> |
|
613 |
> |
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
614 |
> |
this.exceptionClass = exceptionClass; |
615 |
> |
} |
616 |
> |
|
617 |
> |
public final void run() { |
618 |
|
try { |
619 |
< |
Thread.sleep(SHORT_DELAY_MS); |
620 |
< |
threadFail("should throw IE"); |
619 |
> |
realRun(); |
620 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
621 |
> |
} catch (Throwable t) { |
622 |
> |
if (! exceptionClass.isInstance(t)) |
623 |
> |
threadUnexpectedException(t); |
624 |
|
} |
625 |
< |
catch(InterruptedException success) { |
625 |
> |
} |
626 |
> |
} |
627 |
> |
|
628 |
> |
public abstract class CheckedInterruptedRunnable implements Runnable { |
629 |
> |
protected abstract void realRun() throws Throwable; |
630 |
> |
|
631 |
> |
public final void run() { |
632 |
> |
try { |
633 |
> |
realRun(); |
634 |
> |
threadShouldThrow("InterruptedException"); |
635 |
> |
} catch (InterruptedException success) { |
636 |
> |
} catch (Throwable t) { |
637 |
> |
threadUnexpectedException(t); |
638 |
|
} |
639 |
|
} |
640 |
|
} |
641 |
|
|
642 |
< |
public class SmallRunnable implements Runnable { |
643 |
< |
public void run() { |
642 |
> |
public abstract class CheckedCallable<T> implements Callable<T> { |
643 |
> |
protected abstract T realCall() throws Throwable; |
644 |
> |
|
645 |
> |
public final T call() { |
646 |
|
try { |
647 |
< |
Thread.sleep(SMALL_DELAY_MS); |
647 |
> |
return realCall(); |
648 |
> |
} catch (Throwable t) { |
649 |
> |
threadUnexpectedException(t); |
650 |
> |
return null; |
651 |
|
} |
652 |
< |
catch(Exception e) { |
653 |
< |
threadFail("unexpectedException"); |
652 |
> |
} |
653 |
> |
} |
654 |
> |
|
655 |
> |
public abstract class CheckedInterruptedCallable<T> |
656 |
> |
implements Callable<T> { |
657 |
> |
protected abstract T realCall() throws Throwable; |
658 |
> |
|
659 |
> |
public final T call() { |
660 |
> |
try { |
661 |
> |
T result = realCall(); |
662 |
> |
threadShouldThrow("InterruptedException"); |
663 |
> |
return result; |
664 |
> |
} catch (InterruptedException success) { |
665 |
> |
} catch (Throwable t) { |
666 |
> |
threadUnexpectedException(t); |
667 |
|
} |
668 |
+ |
return null; |
669 |
|
} |
670 |
|
} |
671 |
|
|
672 |
< |
public class SmallCallable implements Callable { |
673 |
< |
public Object call() { |
672 |
> |
public static class NoOpRunnable implements Runnable { |
673 |
> |
public void run() {} |
674 |
> |
} |
675 |
> |
|
676 |
> |
public static class NoOpCallable implements Callable { |
677 |
> |
public Object call() { return Boolean.TRUE; } |
678 |
> |
} |
679 |
> |
|
680 |
> |
public static final String TEST_STRING = "a test string"; |
681 |
> |
|
682 |
> |
public static class StringTask implements Callable<String> { |
683 |
> |
public String call() { return TEST_STRING; } |
684 |
> |
} |
685 |
> |
|
686 |
> |
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
687 |
> |
return new CheckedCallable<String>() { |
688 |
> |
public String realCall() { |
689 |
> |
try { |
690 |
> |
latch.await(); |
691 |
> |
} catch (InterruptedException quittingTime) {} |
692 |
> |
return TEST_STRING; |
693 |
> |
}}; |
694 |
> |
} |
695 |
> |
|
696 |
> |
public static class NPETask implements Callable<String> { |
697 |
> |
public String call() { throw new NullPointerException(); } |
698 |
> |
} |
699 |
> |
|
700 |
> |
public static class CallableOne implements Callable<Integer> { |
701 |
> |
public Integer call() { return one; } |
702 |
> |
} |
703 |
> |
|
704 |
> |
public class ShortRunnable extends CheckedRunnable { |
705 |
> |
protected void realRun() throws Throwable { |
706 |
> |
Thread.sleep(SHORT_DELAY_MS); |
707 |
> |
} |
708 |
> |
} |
709 |
> |
|
710 |
> |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
711 |
> |
protected void realRun() throws InterruptedException { |
712 |
> |
Thread.sleep(SHORT_DELAY_MS); |
713 |
> |
} |
714 |
> |
} |
715 |
> |
|
716 |
> |
public class SmallRunnable extends CheckedRunnable { |
717 |
> |
protected void realRun() throws Throwable { |
718 |
> |
Thread.sleep(SMALL_DELAY_MS); |
719 |
> |
} |
720 |
> |
} |
721 |
> |
|
722 |
> |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
723 |
> |
protected void realRun() { |
724 |
|
try { |
725 |
|
Thread.sleep(SMALL_DELAY_MS); |
726 |
< |
} |
727 |
< |
catch(Exception e) { |
728 |
< |
threadFail("unexpectedException"); |
729 |
< |
} |
726 |
> |
} catch (InterruptedException ok) {} |
727 |
> |
} |
728 |
> |
} |
729 |
> |
|
730 |
> |
public class SmallCallable extends CheckedCallable { |
731 |
> |
protected Object realCall() throws InterruptedException { |
732 |
> |
Thread.sleep(SMALL_DELAY_MS); |
733 |
|
return Boolean.TRUE; |
734 |
|
} |
735 |
|
} |
736 |
|
|
737 |
< |
public class SmallInterruptedRunnable implements Runnable { |
737 |
> |
public class MediumRunnable extends CheckedRunnable { |
738 |
> |
protected void realRun() throws Throwable { |
739 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
740 |
> |
} |
741 |
> |
} |
742 |
> |
|
743 |
> |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
744 |
> |
protected void realRun() throws InterruptedException { |
745 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
746 |
> |
} |
747 |
> |
} |
748 |
> |
|
749 |
> |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
750 |
> |
protected void realRun() { |
751 |
> |
try { |
752 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
753 |
> |
} catch (InterruptedException ok) {} |
754 |
> |
} |
755 |
> |
} |
756 |
> |
|
757 |
> |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
758 |
> |
protected void realRun() { |
759 |
> |
try { |
760 |
> |
Thread.sleep(LONG_DELAY_MS); |
761 |
> |
} catch (InterruptedException ok) {} |
762 |
> |
} |
763 |
> |
} |
764 |
> |
|
765 |
> |
/** |
766 |
> |
* For use as ThreadFactory in constructors |
767 |
> |
*/ |
768 |
> |
public static class SimpleThreadFactory implements ThreadFactory { |
769 |
> |
public Thread newThread(Runnable r) { |
770 |
> |
return new Thread(r); |
771 |
> |
} |
772 |
> |
} |
773 |
> |
|
774 |
> |
public static class TrackedShortRunnable implements Runnable { |
775 |
> |
public volatile boolean done = false; |
776 |
|
public void run() { |
777 |
|
try { |
778 |
|
Thread.sleep(SMALL_DELAY_MS); |
779 |
< |
threadFail("should throw IE"); |
780 |
< |
} |
237 |
< |
catch(InterruptedException success) { |
238 |
< |
} |
779 |
> |
done = true; |
780 |
> |
} catch (InterruptedException ok) {} |
781 |
|
} |
782 |
|
} |
783 |
|
|
784 |
< |
|
785 |
< |
public class MediumRunnable implements Runnable { |
784 |
> |
public static class TrackedMediumRunnable implements Runnable { |
785 |
> |
public volatile boolean done = false; |
786 |
|
public void run() { |
787 |
|
try { |
788 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
789 |
< |
} |
790 |
< |
catch(Exception e) { |
249 |
< |
threadFail("unexpectedException"); |
250 |
< |
} |
789 |
> |
done = true; |
790 |
> |
} catch (InterruptedException ok) {} |
791 |
|
} |
792 |
|
} |
793 |
|
|
794 |
< |
public class MediumInterruptedRunnable implements Runnable { |
794 |
> |
public static class TrackedLongRunnable implements Runnable { |
795 |
> |
public volatile boolean done = false; |
796 |
|
public void run() { |
797 |
|
try { |
798 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
799 |
< |
threadFail("should throw IE"); |
800 |
< |
} |
260 |
< |
catch(InterruptedException success) { |
261 |
< |
} |
798 |
> |
Thread.sleep(LONG_DELAY_MS); |
799 |
> |
done = true; |
800 |
> |
} catch (InterruptedException ok) {} |
801 |
|
} |
802 |
|
} |
803 |
|
|
804 |
< |
public class MediumPossiblyInterruptedRunnable implements Runnable { |
804 |
> |
public static class TrackedNoOpRunnable implements Runnable { |
805 |
> |
public volatile boolean done = false; |
806 |
|
public void run() { |
807 |
+ |
done = true; |
808 |
+ |
} |
809 |
+ |
} |
810 |
+ |
|
811 |
+ |
public static class TrackedCallable implements Callable { |
812 |
+ |
public volatile boolean done = false; |
813 |
+ |
public Object call() { |
814 |
|
try { |
815 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
815 |
> |
Thread.sleep(SMALL_DELAY_MS); |
816 |
> |
done = true; |
817 |
> |
} catch (InterruptedException ok) {} |
818 |
> |
return Boolean.TRUE; |
819 |
> |
} |
820 |
> |
} |
821 |
> |
|
822 |
> |
/** |
823 |
> |
* Analog of CheckedRunnable for RecursiveAction |
824 |
> |
*/ |
825 |
> |
public abstract class CheckedRecursiveAction extends RecursiveAction { |
826 |
> |
protected abstract void realCompute() throws Throwable; |
827 |
> |
|
828 |
> |
public final void compute() { |
829 |
> |
try { |
830 |
> |
realCompute(); |
831 |
> |
} catch (Throwable t) { |
832 |
> |
threadUnexpectedException(t); |
833 |
|
} |
834 |
< |
catch(InterruptedException success) { |
834 |
> |
} |
835 |
> |
} |
836 |
> |
|
837 |
> |
/** |
838 |
> |
* Analog of CheckedCallable for RecursiveTask |
839 |
> |
*/ |
840 |
> |
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
841 |
> |
protected abstract T realCompute() throws Throwable; |
842 |
> |
|
843 |
> |
public final T compute() { |
844 |
> |
try { |
845 |
> |
return realCompute(); |
846 |
> |
} catch (Throwable t) { |
847 |
> |
threadUnexpectedException(t); |
848 |
> |
return null; |
849 |
|
} |
850 |
|
} |
851 |
|
} |
852 |
< |
|
852 |
> |
|
853 |
> |
/** |
854 |
> |
* For use as RejectedExecutionHandler in constructors |
855 |
> |
*/ |
856 |
> |
public static class NoOpREHandler implements RejectedExecutionHandler { |
857 |
> |
public void rejectedExecution(Runnable r, |
858 |
> |
ThreadPoolExecutor executor) {} |
859 |
> |
} |
860 |
> |
|
861 |
|
} |