19 |
|
return new TestSuite(TimeUnitTest.class); |
20 |
|
} |
21 |
|
|
22 |
+ |
/** |
23 |
+ |
* |
24 |
+ |
*/ |
25 |
|
public void testConvert() { |
26 |
|
for (long t = 0; t < 10; ++t) { |
27 |
|
assertEquals(t, |
75 |
|
} |
76 |
|
} |
77 |
|
|
78 |
+ |
/** |
79 |
+ |
* |
80 |
+ |
*/ |
81 |
|
public void testToNanos() { |
82 |
|
for (long t = 0; t < 10; ++t) { |
83 |
|
assertEquals(1000000000 * t, |
92 |
|
} |
93 |
|
} |
94 |
|
|
95 |
+ |
/** |
96 |
+ |
* |
97 |
+ |
*/ |
98 |
|
public void testToMicros() { |
99 |
|
for (long t = 0; t < 10; ++t) { |
100 |
|
assertEquals(1000000 * t, |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
+ |
/** |
113 |
+ |
* |
114 |
+ |
*/ |
115 |
|
public void testToMillis() { |
116 |
|
for (long t = 0; t < 10; ++t) { |
117 |
|
assertEquals(1000 * t, |
126 |
|
} |
127 |
|
} |
128 |
|
|
129 |
+ |
/** |
130 |
+ |
* |
131 |
+ |
*/ |
132 |
|
public void testToSeconds() { |
133 |
|
for (long t = 0; t < 10; ++t) { |
134 |
|
assertEquals(t, |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
+ |
/** |
148 |
+ |
* |
149 |
+ |
*/ |
150 |
|
public void testConvertSaturate() { |
151 |
|
assertEquals(Long.MAX_VALUE, |
152 |
|
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
157 |
|
|
158 |
|
} |
159 |
|
|
160 |
+ |
/** |
161 |
+ |
* |
162 |
+ |
*/ |
163 |
|
public void testToNanosSaturate() { |
164 |
|
assertEquals(Long.MAX_VALUE, |
165 |
|
TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
+ |
/** |
173 |
+ |
* |
174 |
+ |
*/ |
175 |
|
public void testToString() { |
176 |
|
String s = TimeUnit.SECONDS.toString(); |
177 |
|
assertTrue(s.indexOf("econd") >= 0); |
182 |
|
* Timed wait without holding lock throws |
183 |
|
* IllegalMonitorStateException |
184 |
|
*/ |
185 |
+ |
/** |
186 |
+ |
* |
187 |
+ |
*/ |
188 |
|
public void testTimedWait_IllegalMonitorException() { |
189 |
|
//created a new thread with anonymous runnable |
190 |
|
|
194 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
195 |
|
try { |
196 |
|
tu.timedWait(o,LONG_DELAY_MS); |
197 |
< |
fail("should throw"); |
197 |
> |
threadShouldThrow(); |
198 |
|
} |
199 |
|
catch (InterruptedException ie) { |
200 |
< |
fail("should not throw IE here"); |
200 |
> |
threadUnexpectedException(); |
201 |
|
} |
202 |
|
catch(IllegalMonitorStateException success) { |
203 |
|
} |
210 |
|
t.interrupt(); |
211 |
|
t.join(); |
212 |
|
} catch(Exception e) { |
213 |
< |
fail("Unexpected exception"); |
213 |
> |
unexpectedException(); |
214 |
|
} |
215 |
|
} |
216 |
|
|
220 |
|
* Note: This does not throw IllegalMonitorException since timeWait |
221 |
|
* is synchronized on o |
222 |
|
*/ |
223 |
+ |
/** |
224 |
+ |
* |
225 |
+ |
*/ |
226 |
|
public void testTimedWait() { |
227 |
|
Thread t = new Thread(new Runnable() { |
228 |
|
public void run() { |
233 |
|
synchronized(o) { |
234 |
|
tu.timedWait(o,MEDIUM_DELAY_MS); |
235 |
|
} |
236 |
< |
fail("should throw"); |
236 |
> |
threadShouldThrow(); |
237 |
|
} |
238 |
|
catch(InterruptedException success) {} |
239 |
|
catch(IllegalMonitorStateException failure) { |
240 |
< |
fail("should not throw"); |
240 |
> |
threadUnexpectedException(); |
241 |
|
} |
242 |
|
} |
243 |
|
}); |
247 |
|
t.interrupt(); |
248 |
|
t.join(); |
249 |
|
} catch(Exception e) { |
250 |
< |
fail("Unexpected exception"); |
250 |
> |
unexpectedException(); |
251 |
|
} |
252 |
|
} |
253 |
|
|
256 |
|
* timedJoin will throw InterruptedException. |
257 |
|
* Thread t waits on timedJoin while the main thread interrupts it. |
258 |
|
*/ |
259 |
+ |
/** |
260 |
+ |
* |
261 |
+ |
*/ |
262 |
|
public void testTimedJoin() { |
263 |
|
Thread t = new Thread(new Runnable() { |
264 |
|
public void run() { |
266 |
|
try { |
267 |
|
Thread s = new Thread(new Runnable() { |
268 |
|
public void run() { |
269 |
< |
try{ |
269 |
> |
try { |
270 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
271 |
< |
}catch(InterruptedException success){} |
271 |
> |
} catch(InterruptedException success){} |
272 |
|
} |
273 |
|
}); |
274 |
|
s.start(); |
275 |
|
tu.timedJoin(s,MEDIUM_DELAY_MS); |
276 |
< |
fail("should throw"); |
276 |
> |
threadShouldThrow(); |
277 |
|
} |
278 |
|
catch(Exception e) {} |
279 |
|
} |
284 |
|
t.interrupt(); |
285 |
|
t.join(); |
286 |
|
} catch(Exception e) { |
287 |
< |
fail("Unexpected exception"); |
287 |
> |
unexpectedException(); |
288 |
|
} |
289 |
|
} |
290 |
|
|
292 |
|
* timedSleep will throw InterruptedException. |
293 |
|
* Thread t waits on timedSleep while the main thread interrupts it. |
294 |
|
*/ |
295 |
+ |
/** |
296 |
+ |
* |
297 |
+ |
*/ |
298 |
|
public void testTimedSleep() { |
299 |
|
//created a new thread with anonymous runnable |
300 |
|
|
303 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
304 |
|
try { |
305 |
|
tu.sleep(MEDIUM_DELAY_MS); |
306 |
< |
fail("should throw"); |
306 |
> |
threadShouldThrow(); |
307 |
|
} |
308 |
|
catch(InterruptedException success) {} |
309 |
|
} |
314 |
|
t.interrupt(); |
315 |
|
t.join(); |
316 |
|
} catch(Exception e) { |
317 |
< |
fail("Unexpected exception"); |
317 |
> |
unexpectedException(); |
318 |
|
} |
319 |
|
} |
320 |
|
|
321 |
+ |
/** |
322 |
+ |
* |
323 |
+ |
*/ |
324 |
|
public void testSerialization() { |
325 |
|
TimeUnit q = TimeUnit.MILLISECONDS; |
326 |
|
|
337 |
|
assertEquals(q.toString(), r.toString()); |
338 |
|
} catch(Exception e){ |
339 |
|
e.printStackTrace(); |
340 |
< |
fail("unexpected exception"); |
340 |
> |
unexpectedException(); |
341 |
|
} |
342 |
|
} |
343 |
|
|