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 |
|
|
20 |
|
return new TestSuite(TimeUnitTest.class); |
21 |
|
} |
22 |
|
|
23 |
+ |
/** |
24 |
+ |
* convert correctly converts sample values across the four units |
25 |
+ |
*/ |
26 |
|
public void testConvert() { |
27 |
|
for (long t = 0; t < 10; ++t) { |
28 |
|
assertEquals(t, |
76 |
|
} |
77 |
|
} |
78 |
|
|
79 |
+ |
/** |
80 |
+ |
* toNanos correctly converts sample values in different units to |
81 |
+ |
* nanoseconds |
82 |
+ |
*/ |
83 |
|
public void testToNanos() { |
84 |
|
for (long t = 0; t < 10; ++t) { |
85 |
|
assertEquals(1000000000 * t, |
94 |
|
} |
95 |
|
} |
96 |
|
|
97 |
+ |
/** |
98 |
+ |
* toMicros correctly converts sample values in different units to |
99 |
+ |
* microseconds |
100 |
+ |
*/ |
101 |
|
public void testToMicros() { |
102 |
|
for (long t = 0; t < 10; ++t) { |
103 |
|
assertEquals(1000000 * t, |
112 |
|
} |
113 |
|
} |
114 |
|
|
115 |
+ |
/** |
116 |
+ |
* toMillis correctly converts sample values in different units to |
117 |
+ |
* milliseconds |
118 |
+ |
*/ |
119 |
|
public void testToMillis() { |
120 |
|
for (long t = 0; t < 10; ++t) { |
121 |
|
assertEquals(1000 * t, |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
+ |
/** |
134 |
+ |
* toSeconds correctly converts sample values in different units to |
135 |
+ |
* seconds |
136 |
+ |
*/ |
137 |
|
public void testToSeconds() { |
138 |
|
for (long t = 0; t < 10; ++t) { |
139 |
|
assertEquals(t, |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
+ |
/** |
153 |
+ |
* convert saturates positive too-large values to Long.MAX_VALUE |
154 |
+ |
* and negative to LONG.MIN_VALUE |
155 |
+ |
*/ |
156 |
|
public void testConvertSaturate() { |
157 |
|
assertEquals(Long.MAX_VALUE, |
158 |
|
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
160 |
|
assertEquals(Long.MIN_VALUE, |
161 |
|
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
162 |
|
TimeUnit.SECONDS)); |
139 |
– |
|
163 |
|
} |
164 |
|
|
165 |
+ |
/** |
166 |
+ |
* toNanos saturates positive too-large values to Long.MAX_VALUE |
167 |
+ |
* and negative to LONG.MIN_VALUE |
168 |
+ |
*/ |
169 |
|
public void testToNanosSaturate() { |
170 |
|
assertEquals(Long.MAX_VALUE, |
171 |
|
TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
172 |
|
assertEquals(Long.MIN_VALUE, |
173 |
|
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
147 |
– |
|
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
+ |
/** |
178 |
+ |
* toString returns string containing common name of unit |
179 |
+ |
*/ |
180 |
|
public void testToString() { |
181 |
|
String s = TimeUnit.SECONDS.toString(); |
182 |
< |
assertTrue(s.indexOf("econd") >= 0); |
182 |
> |
assertTrue(s.indexOf("ECOND") >= 0); |
183 |
|
} |
184 |
|
|
185 |
|
|
196 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
197 |
|
try { |
198 |
|
tu.timedWait(o,LONG_DELAY_MS); |
199 |
< |
fail("should throw"); |
199 |
> |
threadShouldThrow(); |
200 |
|
} |
201 |
|
catch (InterruptedException ie) { |
202 |
< |
fail("should not throw IE here"); |
202 |
> |
threadUnexpectedException(); |
203 |
|
} |
204 |
|
catch(IllegalMonitorStateException success) { |
205 |
|
} |
212 |
|
t.interrupt(); |
213 |
|
t.join(); |
214 |
|
} catch(Exception e) { |
215 |
< |
fail("Unexpected exception"); |
215 |
> |
unexpectedException(); |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* timedWait will throw InterruptedException. |
192 |
< |
* Thread t waits on timedWait while the main thread interrupts it. |
193 |
< |
* Note: This does not throw IllegalMonitorException since timeWait |
194 |
< |
* is synchronized on o |
220 |
> |
* timedWait throws InterruptedException when interrupted |
221 |
|
*/ |
222 |
|
public void testTimedWait() { |
223 |
|
Thread t = new Thread(new Runnable() { |
229 |
|
synchronized(o) { |
230 |
|
tu.timedWait(o,MEDIUM_DELAY_MS); |
231 |
|
} |
232 |
< |
fail("should throw"); |
232 |
> |
threadShouldThrow(); |
233 |
|
} |
234 |
|
catch(InterruptedException success) {} |
235 |
|
catch(IllegalMonitorStateException failure) { |
236 |
< |
fail("should not throw"); |
236 |
> |
threadUnexpectedException(); |
237 |
|
} |
238 |
|
} |
239 |
|
}); |
243 |
|
t.interrupt(); |
244 |
|
t.join(); |
245 |
|
} catch(Exception e) { |
246 |
< |
fail("Unexpected exception"); |
246 |
> |
unexpectedException(); |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
/** |
252 |
< |
* timedJoin will throw InterruptedException. |
227 |
< |
* Thread t waits on timedJoin while the main thread interrupts it. |
252 |
> |
* timedJoin throws InterruptedException when interrupted |
253 |
|
*/ |
254 |
|
public void testTimedJoin() { |
255 |
|
Thread t = new Thread(new Runnable() { |
258 |
|
try { |
259 |
|
Thread s = new Thread(new Runnable() { |
260 |
|
public void run() { |
261 |
< |
try{ |
261 |
> |
try { |
262 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
263 |
< |
}catch(InterruptedException success){} |
263 |
> |
} catch(InterruptedException success){} |
264 |
|
} |
265 |
|
}); |
266 |
|
s.start(); |
267 |
|
tu.timedJoin(s,MEDIUM_DELAY_MS); |
268 |
< |
fail("should throw"); |
268 |
> |
threadShouldThrow(); |
269 |
|
} |
270 |
|
catch(Exception e) {} |
271 |
|
} |
276 |
|
t.interrupt(); |
277 |
|
t.join(); |
278 |
|
} catch(Exception e) { |
279 |
< |
fail("Unexpected exception"); |
279 |
> |
unexpectedException(); |
280 |
|
} |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
< |
* timedSleep will throw InterruptedException. |
260 |
< |
* Thread t waits on timedSleep while the main thread interrupts it. |
284 |
> |
* timedSleep throws InterruptedException when interrupted |
285 |
|
*/ |
286 |
|
public void testTimedSleep() { |
287 |
|
//created a new thread with anonymous runnable |
291 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
292 |
|
try { |
293 |
|
tu.sleep(MEDIUM_DELAY_MS); |
294 |
< |
fail("should throw"); |
294 |
> |
threadShouldThrow(); |
295 |
|
} |
296 |
|
catch(InterruptedException success) {} |
297 |
|
} |
302 |
|
t.interrupt(); |
303 |
|
t.join(); |
304 |
|
} catch(Exception e) { |
305 |
< |
fail("Unexpected exception"); |
305 |
> |
unexpectedException(); |
306 |
|
} |
307 |
|
} |
308 |
|
|
309 |
+ |
/** |
310 |
+ |
* a deserialized serialized unit is equal |
311 |
+ |
*/ |
312 |
|
public void testSerialization() { |
313 |
|
TimeUnit q = TimeUnit.MILLISECONDS; |
314 |
|
|
325 |
|
assertEquals(q.toString(), r.toString()); |
326 |
|
} catch(Exception e){ |
327 |
|
e.printStackTrace(); |
328 |
< |
fail("unexpected exception"); |
328 |
> |
unexpectedException(); |
329 |
|
} |
330 |
|
} |
331 |
|
|