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 |
* (100, 500, 1000, 5000 MS) but even so, if there is ever any doubt, |
45 |
* they can all be increased in one spot to rerun tests on slower |
46 |
* platforms</li> |
47 |
* |
48 |
* <li> All threads generated must be joined inside each test case |
49 |
* method (or <tt>fail</tt> to do so) before returning from the |
50 |
* method. The {@link joinPool} method can be used to do this when |
51 |
* using Executors.</li> |
52 |
* |
53 |
* </ol> |
54 |
*/ |
55 |
public class JSR166TestCase extends TestCase { |
56 |
|
57 |
/** |
58 |
* Flag set true if any threadAssert methods fail |
59 |
*/ |
60 |
protected volatile boolean threadFailed; |
61 |
|
62 |
/** |
63 |
* Initialize test to indicat that no thread assertions have failed |
64 |
*/ |
65 |
public void setUp() { |
66 |
threadFailed = false; |
67 |
} |
68 |
|
69 |
/** |
70 |
* Trigger test case failure if any thread assertions have failed |
71 |
*/ |
72 |
public void tearDown() { |
73 |
assertFalse(threadFailed); |
74 |
} |
75 |
|
76 |
public void threadFail(String reason) { |
77 |
threadFailed = true; |
78 |
fail(reason); |
79 |
} |
80 |
|
81 |
public void threadAssertTrue(boolean b) { |
82 |
if (!b) { |
83 |
threadFailed = true; |
84 |
assertTrue(b); |
85 |
} |
86 |
} |
87 |
public void threadAssertFalse(boolean b) { |
88 |
if (b) { |
89 |
threadFailed = true; |
90 |
assertFalse(b); |
91 |
} |
92 |
} |
93 |
public void threadAssertNull(Object x) { |
94 |
if (x != null) { |
95 |
threadFailed = true; |
96 |
assertNull(x); |
97 |
} |
98 |
} |
99 |
public void threadAssertEquals(long x, long y) { |
100 |
if (x != y) { |
101 |
threadFailed = true; |
102 |
assertEquals(x, y); |
103 |
} |
104 |
} |
105 |
public void threadAssertEquals(Object x, Object y) { |
106 |
if (x != y && (x == null || !x.equals(y))) { |
107 |
threadFailed = true; |
108 |
assertEquals(x, y); |
109 |
} |
110 |
} |
111 |
|
112 |
/** |
113 |
* Wait out termination of a thread pool or fail doing so |
114 |
*/ |
115 |
public void joinPool(ExecutorService exec) { |
116 |
try { |
117 |
exec.shutdown(); |
118 |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
119 |
} catch(InterruptedException ie) { |
120 |
fail("unexpected exception"); |
121 |
} |
122 |
} |
123 |
|
124 |
public static final long SHORT_DELAY_MS = 100; |
125 |
public static final long SMALL_DELAY_MS = 500; |
126 |
public static final long MEDIUM_DELAY_MS = 1000; |
127 |
public static final long LONG_DELAY_MS = 5000; |
128 |
|
129 |
|
130 |
/** |
131 |
* The number of elements to place in collections, arrays, etc. |
132 |
*/ |
133 |
public static final int SIZE = 20; |
134 |
|
135 |
// Some convenient Integer constants |
136 |
|
137 |
public static final Integer zero = new Integer(0); |
138 |
public static final Integer one = new Integer(1); |
139 |
public static final Integer two = new Integer(2); |
140 |
public static final Integer three = new Integer(3); |
141 |
public static final Integer four = new Integer(4); |
142 |
public static final Integer five = new Integer(5); |
143 |
public static final Integer six = new Integer(6); |
144 |
public static final Integer seven = new Integer(7); |
145 |
public static final Integer eight = new Integer(8); |
146 |
public static final Integer nine = new Integer(9); |
147 |
public static final Integer m1 = new Integer(-1); |
148 |
public static final Integer m2 = new Integer(-2); |
149 |
public static final Integer m3 = new Integer(-3); |
150 |
public static final Integer m4 = new Integer(-4); |
151 |
public static final Integer m5 = new Integer(-5); |
152 |
public static final Integer m10 = new Integer(-10); |
153 |
|
154 |
|
155 |
// Some convenient Runnable classes |
156 |
|
157 |
public static class NoOpRunnable implements Runnable { |
158 |
public void run() {} |
159 |
} |
160 |
|
161 |
public static class NoOpCallable implements Callable { |
162 |
public Object call() { return Boolean.TRUE; } |
163 |
} |
164 |
|
165 |
public class ShortRunnable implements Runnable { |
166 |
public void run() { |
167 |
try { |
168 |
Thread.sleep(SHORT_DELAY_MS); |
169 |
} |
170 |
catch(Exception e) { |
171 |
threadFail("unexpectedException"); |
172 |
} |
173 |
} |
174 |
} |
175 |
|
176 |
public class ShortInterruptedRunnable implements Runnable { |
177 |
public void run() { |
178 |
try { |
179 |
Thread.sleep(SHORT_DELAY_MS); |
180 |
threadFail("should throw IE"); |
181 |
} |
182 |
catch(InterruptedException success) { |
183 |
} |
184 |
} |
185 |
} |
186 |
|
187 |
public class SmallRunnable implements Runnable { |
188 |
public void run() { |
189 |
try { |
190 |
Thread.sleep(SMALL_DELAY_MS); |
191 |
} |
192 |
catch(Exception e) { |
193 |
threadFail("unexpectedException"); |
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
public class SmallCallable implements Callable { |
199 |
public Object call() { |
200 |
try { |
201 |
Thread.sleep(SMALL_DELAY_MS); |
202 |
} |
203 |
catch(Exception e) { |
204 |
threadFail("unexpectedException"); |
205 |
} |
206 |
return Boolean.TRUE; |
207 |
} |
208 |
} |
209 |
|
210 |
public class SmallInterruptedRunnable implements Runnable { |
211 |
public void run() { |
212 |
try { |
213 |
Thread.sleep(SMALL_DELAY_MS); |
214 |
threadFail("should throw IE"); |
215 |
} |
216 |
catch(InterruptedException success) { |
217 |
} |
218 |
} |
219 |
} |
220 |
|
221 |
|
222 |
public class MediumRunnable implements Runnable { |
223 |
public void run() { |
224 |
try { |
225 |
Thread.sleep(MEDIUM_DELAY_MS); |
226 |
} |
227 |
catch(Exception e) { |
228 |
threadFail("unexpectedException"); |
229 |
} |
230 |
} |
231 |
} |
232 |
|
233 |
public class MediumInterruptedRunnable implements Runnable { |
234 |
public void run() { |
235 |
try { |
236 |
Thread.sleep(MEDIUM_DELAY_MS); |
237 |
threadFail("should throw IE"); |
238 |
} |
239 |
catch(InterruptedException success) { |
240 |
} |
241 |
} |
242 |
} |
243 |
|
244 |
public class MediumPossiblyInterruptedRunnable implements Runnable { |
245 |
public void run() { |
246 |
try { |
247 |
Thread.sleep(MEDIUM_DELAY_MS); |
248 |
} |
249 |
catch(InterruptedException success) { |
250 |
} |
251 |
} |
252 |
} |
253 |
|
254 |
} |