1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class TimeUnitTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run(suite()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
|
18 |
|
public static Test suite() { |
19 |
< |
return new TestSuite(TimeUnitTest.class); |
19 |
> |
return new TestSuite(TimeUnitTest.class); |
20 |
|
} |
21 |
|
|
22 |
|
// (loops to 88888 check increments at all time divisions.) |
48 |
|
TimeUnit.SECONDS.convert(1000000000L*t, |
49 |
|
TimeUnit.NANOSECONDS)); |
50 |
|
|
52 |
– |
|
51 |
|
assertEquals(1000L*t*60*60*24, |
52 |
|
TimeUnit.MILLISECONDS.convert(t, |
53 |
|
TimeUnit.DAYS)); |
277 |
|
} |
278 |
|
} |
279 |
|
|
282 |
– |
|
280 |
|
/** |
281 |
|
* convert saturates positive too-large values to Long.MAX_VALUE |
282 |
|
* and negative to LONG.MIN_VALUE |
306 |
|
assertEquals(Long.MIN_VALUE, |
307 |
|
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
308 |
|
TimeUnit.DAYS)); |
312 |
– |
|
309 |
|
} |
310 |
|
|
311 |
|
/** |
319 |
|
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
320 |
|
} |
321 |
|
|
326 |
– |
|
322 |
|
/** |
323 |
< |
* toString returns string containing common name of unit |
323 |
> |
* toString returns name of unit |
324 |
|
*/ |
325 |
|
public void testToString() { |
326 |
< |
String s = TimeUnit.SECONDS.toString(); |
332 |
< |
assertTrue(s.indexOf("ECOND") >= 0); |
326 |
> |
assertEquals("SECONDS", TimeUnit.SECONDS.toString()); |
327 |
|
} |
328 |
|
|
329 |
+ |
/** |
330 |
+ |
* name returns name of unit |
331 |
+ |
*/ |
332 |
+ |
public void testName() { |
333 |
+ |
assertEquals("SECONDS", TimeUnit.SECONDS.name()); |
334 |
+ |
} |
335 |
|
|
336 |
|
/** |
337 |
< |
* Timed wait without holding lock throws |
338 |
< |
* IllegalMonitorStateException |
337 |
> |
* Timed wait without holding lock throws |
338 |
> |
* IllegalMonitorStateException |
339 |
|
*/ |
340 |
|
public void testTimedWait_IllegalMonitorException() throws Exception { |
341 |
|
Thread t = new Thread(new CheckedRunnable() { |
357 |
|
* timedWait throws InterruptedException when interrupted |
358 |
|
*/ |
359 |
|
public void testTimedWait() throws InterruptedException { |
360 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
361 |
< |
public void realRun() throws InterruptedException { |
360 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
361 |
> |
public void realRun() throws InterruptedException { |
362 |
|
Object o = new Object(); |
363 |
|
|
364 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
365 |
< |
synchronized(o) { |
365 |
> |
synchronized (o) { |
366 |
|
tu.timedWait(o,MEDIUM_DELAY_MS); |
367 |
|
} |
368 |
|
}}); |
369 |
< |
t.start(); |
369 |
> |
t.start(); |
370 |
|
Thread.sleep(SHORT_DELAY_MS); |
371 |
|
t.interrupt(); |
372 |
|
t.join(); |
373 |
|
} |
374 |
|
|
375 |
– |
|
375 |
|
/** |
376 |
|
* timedJoin throws InterruptedException when interrupted |
377 |
|
*/ |
380 |
|
public void realRun() throws InterruptedException { |
381 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
382 |
|
}}); |
383 |
< |
final Thread t = new Thread(new CheckedInterruptedRunnable() { |
384 |
< |
public void realRun() throws InterruptedException { |
383 |
> |
final Thread t = new Thread(new CheckedInterruptedRunnable() { |
384 |
> |
public void realRun() throws InterruptedException { |
385 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
386 |
|
tu.timedJoin(s, MEDIUM_DELAY_MS); |
387 |
< |
}});; |
387 |
> |
}}); |
388 |
|
s.start(); |
389 |
< |
t.start(); |
389 |
> |
t.start(); |
390 |
|
Thread.sleep(SHORT_DELAY_MS); |
391 |
|
t.interrupt(); |
392 |
|
t.join(); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
< |
* timedSleep throws InterruptedException when interrupted |
398 |
> |
* timedSleep throws InterruptedException when interrupted |
399 |
|
*/ |
400 |
|
public void testTimedSleep() throws InterruptedException { |
401 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
402 |
< |
public void realRun() throws InterruptedException { |
401 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
402 |
> |
public void realRun() throws InterruptedException { |
403 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
404 |
|
tu.sleep(MEDIUM_DELAY_MS); |
405 |
|
}}); |
406 |
|
|
407 |
< |
t.start(); |
407 |
> |
t.start(); |
408 |
|
Thread.sleep(SHORT_DELAY_MS); |
409 |
|
t.interrupt(); |
410 |
|
t.join(); |