1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package libcore.java.time.chrono;
17
18import org.junit.Test;
19import java.time.Instant;
20import java.time.LocalDate;
21import java.time.LocalTime;
22import java.time.ZoneId;
23import java.time.ZoneOffset;
24import java.time.ZonedDateTime;
25import java.time.chrono.ChronoZonedDateTime;
26import java.time.chrono.JapaneseChronology;
27import java.time.chrono.JapaneseDate;
28import java.time.chrono.JapaneseEra;
29import java.time.temporal.ChronoField;
30
31import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertSame;
33
34/**
35 * Additional tests for {@link JapaneseChronology} and {@link JapaneseDate}.
36 *
37 * @see tck.java.time.chrono.TCKJapaneseChronology
38 */
39public class JapaneseChronologyTest {
40
41    @Test
42    public void test_zonedDateTime() {
43        ZonedDateTime zonedDateTime = ZonedDateTime
44                .of(2017, 4, 1, 15, 14, 13, 12, ZoneId.of("Europe/London"));
45
46        ChronoZonedDateTime<JapaneseDate> result = JapaneseChronology.INSTANCE
47                .zonedDateTime(zonedDateTime.toInstant(), zonedDateTime.getZone());
48        assertEquals(JapaneseDate.of(JapaneseEra.HEISEI, 29, 4, 1), result.toLocalDate());
49        assertEquals(LocalTime.of(15, 14, 13, 12), result.toLocalTime());
50        assertEquals(ZoneOffset.ofHours(1), result.getOffset());
51    }
52
53    @Test(expected = NullPointerException.class)
54    public void test_zonedDateTime_nullInstant() {
55        JapaneseChronology.INSTANCE.zonedDateTime(null, ZoneOffset.UTC);
56    }
57
58    @Test(expected = NullPointerException.class)
59    public void test_zonedDateTime_nullZone() {
60        JapaneseChronology.INSTANCE.zonedDateTime(Instant.EPOCH, null);
61    }
62
63    @Test
64    public void test_JapaneseDate_getChronology() {
65        assertSame(JapaneseChronology.INSTANCE, JapaneseDate.now().getChronology());
66    }
67
68    @Test
69    public void test_JapaneseDate_getEra() {
70        // pick the first january of the second year of each era, except for Meiji, because the
71        // first supported year in JapaneseChronology is Meiji 6.
72        assertEquals(JapaneseEra.MEIJI, JapaneseDate.from(LocalDate.of(1873, 1, 1)).getEra());
73        assertEquals(JapaneseEra.TAISHO, JapaneseDate.from(LocalDate.of(1913, 1, 1)).getEra());
74        assertEquals(JapaneseEra.SHOWA, JapaneseDate.from(LocalDate.of(1927, 1, 1)).getEra());
75        assertEquals(JapaneseEra.HEISEI, JapaneseDate.from(LocalDate.of(1990, 1, 1)).getEra());
76    }
77
78    @Test
79    public void test_JapaneseDate_isSupported_TemporalField() {
80        JapaneseDate date = JapaneseDate.now();
81        // all date based fields, except for the aligned week ones are supported.
82        assertEquals(false, date.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
83        assertEquals(false, date.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR));
84        assertEquals(false, date.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH));
85        assertEquals(false, date.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR));
86        assertEquals(false, date.isSupported(ChronoField.AMPM_OF_DAY));
87        assertEquals(false, date.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM));
88        assertEquals(false, date.isSupported(ChronoField.CLOCK_HOUR_OF_DAY));
89        assertEquals(true, date.isSupported(ChronoField.DAY_OF_MONTH));
90        assertEquals(true, date.isSupported(ChronoField.DAY_OF_WEEK));
91        assertEquals(true, date.isSupported(ChronoField.DAY_OF_YEAR));
92        assertEquals(true, date.isSupported(ChronoField.EPOCH_DAY));
93        assertEquals(true, date.isSupported(ChronoField.ERA));
94        assertEquals(false, date.isSupported(ChronoField.HOUR_OF_AMPM));
95        assertEquals(false, date.isSupported(ChronoField.HOUR_OF_DAY));
96        assertEquals(false, date.isSupported(ChronoField.INSTANT_SECONDS));
97        assertEquals(false, date.isSupported(ChronoField.MICRO_OF_DAY));
98        assertEquals(false, date.isSupported(ChronoField.MICRO_OF_SECOND));
99        assertEquals(false, date.isSupported(ChronoField.MILLI_OF_DAY));
100        assertEquals(false, date.isSupported(ChronoField.MILLI_OF_SECOND));
101        assertEquals(false, date.isSupported(ChronoField.MINUTE_OF_DAY));
102        assertEquals(false, date.isSupported(ChronoField.MINUTE_OF_HOUR));
103        assertEquals(true, date.isSupported(ChronoField.MONTH_OF_YEAR));
104        assertEquals(false, date.isSupported(ChronoField.NANO_OF_DAY));
105        assertEquals(false, date.isSupported(ChronoField.NANO_OF_SECOND));
106        assertEquals(false, date.isSupported(ChronoField.OFFSET_SECONDS));
107        assertEquals(true, date.isSupported(ChronoField.PROLEPTIC_MONTH));
108        assertEquals(false, date.isSupported(ChronoField.SECOND_OF_DAY));
109        assertEquals(false, date.isSupported(ChronoField.SECOND_OF_MINUTE));
110        assertEquals(true, date.isSupported(ChronoField.YEAR));
111        assertEquals(true, date.isSupported(ChronoField.YEAR_OF_ERA));
112    }
113}
114