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