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. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
import java.io.*; |
12 |
|
13 |
|
14 |
/** |
15 |
* Base class for JSR166 Junit TCK tests. Defines some constants and |
16 |
* utility methods, as well as a simple framework for helping to make |
17 |
* sure that assertions failing in generated threads cause the |
18 |
* associated test that generated them to itself fail (which JUnit doe |
19 |
* not otherwise arrange). The rules for creating such tests are: |
20 |
* |
21 |
* <ol> |
22 |
* |
23 |
* <li> All assertions in code running in generated threads must use |
24 |
* the forms {@link threadFail} , {@link threadAssertTrue} {@link |
25 |
* threadAssertEquals}, or {@link threadAssertNull}, (not |
26 |
* <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not |
27 |
* particularly recommended) for other code to use these forms too. |
28 |
* Only the most typically used JUnit assertion methods are defined |
29 |
* this way, but enough to live with.</li> |
30 |
* |
31 |
* <li> If you override {@link setUp} or {@link tearDown}, make sure |
32 |
* to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within |
33 |
* them. These methods are used to clear and check for thread |
34 |
* assertion failures.</li> |
35 |
* |
36 |
* <li>All delays and timeouts must use one of the constants {@link |
37 |
* SHORT_DELAY_MS}, {@link SMALL_DELAY_MS}, {@link MEDIUM_DELAY_MS}, |
38 |
* {@link LONG_DELAY_MS}. The idea here is that a SHORT is always |
39 |
* discriminatable from zero time, and always allows enough time for |
40 |
* the small amounts of computation (creating a thread, calling a few |
41 |
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
42 |
* is always discriminable as larger than SHORT and smaller than |
43 |
* MEDIUM. And so on. These constants are set to conservative values, |
44 |
* but even so, if there is ever any doubt, they can all be increased |
45 |
* in one spot to rerun tests on slower platforms</li> |
46 |
* |
47 |
* <li> All threads generated must be joined inside each test case |
48 |
* method (or <tt>fail</tt> to do so) before returning from the |
49 |
* method. The {@link joinPool} method can be used to do this when |
50 |
* using Executors.</li> |
51 |
* |
52 |
* </ol> |
53 |
*/ |
54 |
public class JSR166TestCase extends TestCase { |
55 |
|
56 |
public static long SHORT_DELAY_MS; |
57 |
public static long SMALL_DELAY_MS; |
58 |
public static long MEDIUM_DELAY_MS; |
59 |
public static long LONG_DELAY_MS; |
60 |
|
61 |
|
62 |
/** |
63 |
* Return the shortest timed delay. This could |
64 |
* be reimplmented to use for example a Property. |
65 |
*/ |
66 |
protected long getShortDelay() { |
67 |
return 50; |
68 |
} |
69 |
|
70 |
|
71 |
/** |
72 |
* Set delays as multiples fo SHORT_DELAY. |
73 |
*/ |
74 |
protected void setDelays() { |
75 |
SHORT_DELAY_MS = getShortDelay(); |
76 |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
77 |
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
78 |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
79 |
} |
80 |
|
81 |
/** |
82 |
* Flag set true if any threadAssert methods fail |
83 |
*/ |
84 |
protected volatile boolean threadFailed; |
85 |
|
86 |
/** |
87 |
* Initialize test to indicat that no thread assertions have failed |
88 |
*/ |
89 |
public void setUp() { |
90 |
setDelays(); |
91 |
threadFailed = false; |
92 |
} |
93 |
|
94 |
/** |
95 |
* Trigger test case failure if any thread assertions have failed |
96 |
*/ |
97 |
public void tearDown() { |
98 |
assertFalse(threadFailed); |
99 |
} |
100 |
|
101 |
public void threadFail(String reason) { |
102 |
threadFailed = true; |
103 |
fail(reason); |
104 |
} |
105 |
|
106 |
public void threadAssertTrue(boolean b) { |
107 |
if (!b) { |
108 |
threadFailed = true; |
109 |
assertTrue(b); |
110 |
} |
111 |
} |
112 |
public void threadAssertFalse(boolean b) { |
113 |
if (b) { |
114 |
threadFailed = true; |
115 |
assertFalse(b); |
116 |
} |
117 |
} |
118 |
public void threadAssertNull(Object x) { |
119 |
if (x != null) { |
120 |
threadFailed = true; |
121 |
assertNull(x); |
122 |
} |
123 |
} |
124 |
public void threadAssertEquals(long x, long y) { |
125 |
if (x != y) { |
126 |
threadFailed = true; |
127 |
assertEquals(x, y); |
128 |
} |
129 |
} |
130 |
public void threadAssertEquals(Object x, Object y) { |
131 |
if (x != y && (x == null || !x.equals(y))) { |
132 |
threadFailed = true; |
133 |
assertEquals(x, y); |
134 |
} |
135 |
} |
136 |
|
137 |
/** |
138 |
* Wait out termination of a thread pool or fail doing so |
139 |
*/ |
140 |
public void joinPool(ExecutorService exec) { |
141 |
try { |
142 |
exec.shutdown(); |
143 |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
144 |
} catch(InterruptedException ie) { |
145 |
fail("unexpected exception"); |
146 |
} |
147 |
} |
148 |
|
149 |
|
150 |
|
151 |
/** |
152 |
* The number of elements to place in collections, arrays, etc. |
153 |
*/ |
154 |
public static final int SIZE = 20; |
155 |
|
156 |
// Some convenient Integer constants |
157 |
|
158 |
public static final Integer zero = new Integer(0); |
159 |
public static final Integer one = new Integer(1); |
160 |
public static final Integer two = new Integer(2); |
161 |
public static final Integer three = new Integer(3); |
162 |
public static final Integer four = new Integer(4); |
163 |
public static final Integer five = new Integer(5); |
164 |
public static final Integer six = new Integer(6); |
165 |
public static final Integer seven = new Integer(7); |
166 |
public static final Integer eight = new Integer(8); |
167 |
public static final Integer nine = new Integer(9); |
168 |
public static final Integer m1 = new Integer(-1); |
169 |
public static final Integer m2 = new Integer(-2); |
170 |
public static final Integer m3 = new Integer(-3); |
171 |
public static final Integer m4 = new Integer(-4); |
172 |
public static final Integer m5 = new Integer(-5); |
173 |
public static final Integer m10 = new Integer(-10); |
174 |
|
175 |
|
176 |
// Some convenient Runnable classes |
177 |
|
178 |
public static class NoOpRunnable implements Runnable { |
179 |
public void run() {} |
180 |
} |
181 |
|
182 |
public static class NoOpCallable implements Callable { |
183 |
public Object call() { return Boolean.TRUE; } |
184 |
} |
185 |
|
186 |
public class ShortRunnable implements Runnable { |
187 |
public void run() { |
188 |
try { |
189 |
Thread.sleep(SHORT_DELAY_MS); |
190 |
} |
191 |
catch(Exception e) { |
192 |
threadFail("unexpectedException"); |
193 |
} |
194 |
} |
195 |
} |
196 |
|
197 |
public class ShortInterruptedRunnable implements Runnable { |
198 |
public void run() { |
199 |
try { |
200 |
Thread.sleep(SHORT_DELAY_MS); |
201 |
threadFail("should throw IE"); |
202 |
} |
203 |
catch(InterruptedException success) { |
204 |
} |
205 |
} |
206 |
} |
207 |
|
208 |
public class SmallRunnable implements Runnable { |
209 |
public void run() { |
210 |
try { |
211 |
Thread.sleep(SMALL_DELAY_MS); |
212 |
} |
213 |
catch(Exception e) { |
214 |
threadFail("unexpectedException"); |
215 |
} |
216 |
} |
217 |
} |
218 |
|
219 |
public class SmallCallable implements Callable { |
220 |
public Object call() { |
221 |
try { |
222 |
Thread.sleep(SMALL_DELAY_MS); |
223 |
} |
224 |
catch(Exception e) { |
225 |
threadFail("unexpectedException"); |
226 |
} |
227 |
return Boolean.TRUE; |
228 |
} |
229 |
} |
230 |
|
231 |
public class SmallInterruptedRunnable implements Runnable { |
232 |
public void run() { |
233 |
try { |
234 |
Thread.sleep(SMALL_DELAY_MS); |
235 |
threadFail("should throw IE"); |
236 |
} |
237 |
catch(InterruptedException success) { |
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
|
243 |
public class MediumRunnable implements Runnable { |
244 |
public void run() { |
245 |
try { |
246 |
Thread.sleep(MEDIUM_DELAY_MS); |
247 |
} |
248 |
catch(Exception e) { |
249 |
threadFail("unexpectedException"); |
250 |
} |
251 |
} |
252 |
} |
253 |
|
254 |
public class MediumInterruptedRunnable implements Runnable { |
255 |
public void run() { |
256 |
try { |
257 |
Thread.sleep(MEDIUM_DELAY_MS); |
258 |
threadFail("should throw IE"); |
259 |
} |
260 |
catch(InterruptedException success) { |
261 |
} |
262 |
} |
263 |
} |
264 |
|
265 |
public class MediumPossiblyInterruptedRunnable implements Runnable { |
266 |
public void run() { |
267 |
try { |
268 |
Thread.sleep(MEDIUM_DELAY_MS); |
269 |
} |
270 |
catch(InterruptedException success) { |
271 |
} |
272 |
} |
273 |
} |
274 |
|
275 |
} |