1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 * version 2 for more details (a copy is included in the LICENSE file that
12 * accompanied this code).
13 *
14 * You should have received a copy of the GNU General Public License version
15 * 2 along with this work; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 * or visit www.oracle.com if you need additional information or have any
20 * questions.
21 */
22
23/*
24 * This file is available under and governed by the GNU General Public
25 * License version 2 only, as published by the Free Software Foundation.
26 * However, the following notice accompanied the original version of this
27 * file:
28 *
29 * Written by Doug Lea and Martin Buchholz with assistance from
30 * members of JCP JSR-166 Expert Group and released to the public
31 * domain, as explained at
32 * http://creativecommons.org/publicdomain/zero/1.0/
33 */
34
35import static java.util.concurrent.TimeUnit.DAYS;
36import static java.util.concurrent.TimeUnit.HOURS;
37import static java.util.concurrent.TimeUnit.MICROSECONDS;
38import static java.util.concurrent.TimeUnit.MILLISECONDS;
39import static java.util.concurrent.TimeUnit.MINUTES;
40import static java.util.concurrent.TimeUnit.NANOSECONDS;
41import static java.util.concurrent.TimeUnit.SECONDS;
42
43import java.time.temporal.ChronoUnit;
44import java.util.concurrent.TimeUnit;
45
46import junit.framework.Test;
47import junit.framework.TestSuite;
48
49public class TimeUnit8Test extends JSR166TestCase {
50    public static void main(String[] args) {
51        main(suite(), args);
52    }
53
54    public static Test suite() {
55        return new TestSuite(TimeUnit8Test.class);
56    }
57
58    /**
59     * tests for toChronoUnit.
60     */
61    public void testToChronoUnit() throws Exception {
62        assertSame(ChronoUnit.NANOS,   NANOSECONDS.toChronoUnit());
63        assertSame(ChronoUnit.MICROS,  MICROSECONDS.toChronoUnit());
64        assertSame(ChronoUnit.MILLIS,  MILLISECONDS.toChronoUnit());
65        assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit());
66        assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit());
67        assertSame(ChronoUnit.HOURS,   HOURS.toChronoUnit());
68        assertSame(ChronoUnit.DAYS,    DAYS.toChronoUnit());
69
70        // Every TimeUnit has a defined ChronoUnit equivalent
71        for (TimeUnit x : TimeUnit.values())
72            assertSame(x, TimeUnit.of(x.toChronoUnit()));
73    }
74
75    /**
76     * tests for TimeUnit.of(ChronoUnit).
77     */
78    public void testTimeUnitOf() throws Exception {
79        assertSame(NANOSECONDS,  TimeUnit.of(ChronoUnit.NANOS));
80        assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS));
81        assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS));
82        assertSame(SECONDS,      TimeUnit.of(ChronoUnit.SECONDS));
83        assertSame(MINUTES,      TimeUnit.of(ChronoUnit.MINUTES));
84        assertSame(HOURS,        TimeUnit.of(ChronoUnit.HOURS));
85        assertSame(DAYS,         TimeUnit.of(ChronoUnit.DAYS));
86
87        assertThrows(NullPointerException.class,
88                     () -> TimeUnit.of((ChronoUnit)null));
89
90        // ChronoUnits either round trip to their TimeUnit
91        // equivalents, or throw IllegalArgumentException.
92        for (ChronoUnit cu : ChronoUnit.values()) {
93            final TimeUnit tu;
94            try {
95                tu = TimeUnit.of(cu);
96            } catch (IllegalArgumentException acceptable) {
97                continue;
98            }
99            assertSame(cu, tu.toChronoUnit());
100        }
101    }
102
103}
104