1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
|
|
* members of JCP JSR-166 Expert Group and released to the public |
4 |
|
|
* domain, as explained at |
5 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import static java.util.concurrent.TimeUnit.DAYS; |
9 |
|
|
import static java.util.concurrent.TimeUnit.HOURS; |
10 |
|
|
import static java.util.concurrent.TimeUnit.MICROSECONDS; |
11 |
|
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
|
|
import static java.util.concurrent.TimeUnit.MINUTES; |
13 |
|
|
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
14 |
|
|
import static java.util.concurrent.TimeUnit.SECONDS; |
15 |
|
|
|
16 |
|
|
import java.time.temporal.ChronoUnit; |
17 |
|
|
import java.util.concurrent.TimeUnit; |
18 |
|
|
|
19 |
|
|
import junit.framework.Test; |
20 |
|
|
import junit.framework.TestSuite; |
21 |
|
|
|
22 |
|
|
public class TimeUnit8Test extends JSR166TestCase { |
23 |
|
|
public static void main(String[] args) { |
24 |
|
|
main(suite(), args); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
public static Test suite() { |
28 |
|
|
return new TestSuite(TimeUnit8Test.class); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
/** |
32 |
|
|
* tests for toChronoUnit. |
33 |
|
|
*/ |
34 |
|
|
public void testToChronoUnit() throws Exception { |
35 |
|
|
assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit()); |
36 |
|
|
assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit()); |
37 |
|
|
assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit()); |
38 |
|
|
assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit()); |
39 |
|
|
assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit()); |
40 |
|
|
assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit()); |
41 |
|
|
assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit()); |
42 |
|
|
|
43 |
|
|
// Every TimeUnit has a defined ChronoUnit equivalent |
44 |
|
|
for (TimeUnit x : TimeUnit.values()) |
45 |
|
|
assertSame(x, TimeUnit.of(x.toChronoUnit())); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
/** |
49 |
|
|
* tests for TimeUnit.of(ChronoUnit). |
50 |
|
|
*/ |
51 |
|
|
public void testTimeUnitOf() throws Exception { |
52 |
|
|
assertSame(NANOSECONDS, TimeUnit.of(ChronoUnit.NANOS)); |
53 |
|
|
assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS)); |
54 |
|
|
assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS)); |
55 |
|
|
assertSame(SECONDS, TimeUnit.of(ChronoUnit.SECONDS)); |
56 |
|
|
assertSame(MINUTES, TimeUnit.of(ChronoUnit.MINUTES)); |
57 |
|
|
assertSame(HOURS, TimeUnit.of(ChronoUnit.HOURS)); |
58 |
|
|
assertSame(DAYS, TimeUnit.of(ChronoUnit.DAYS)); |
59 |
|
|
|
60 |
|
|
assertThrows(NullPointerException.class, |
61 |
|
|
() -> TimeUnit.of((ChronoUnit)null)); |
62 |
|
|
|
63 |
|
|
// ChronoUnits either round trip to their TimeUnit |
64 |
|
|
// equivalents, or throw IllegalArgumentException. |
65 |
|
|
for (ChronoUnit cu : ChronoUnit.values()) { |
66 |
|
|
final TimeUnit tu; |
67 |
|
|
try { |
68 |
|
|
tu = TimeUnit.of(cu); |
69 |
|
|
} catch (IllegalArgumentException acceptable) { |
70 |
|
|
continue; |
71 |
|
|
} |
72 |
|
|
assertSame(cu, tu.toChronoUnit()); |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
} |