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.chrono.HijrahChronology;
20import java.time.chrono.HijrahDate;
21import java.time.chrono.HijrahEra;
22import java.time.temporal.ChronoField;
23
24import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertSame;
26
27/**
28 * Additional tests for {@link HijrahDate}.
29 *
30 * @see tck.java.time.chrono.TCKHijrahChronology
31 */
32public class HijrahChronologyTest {
33    @Test
34    public void test_HijrahDate_getEra() {
35        // HijrahChronology has only one valid era.
36        assertEquals(HijrahEra.AH, HijrahDate.of(1300, 1, 1).getEra());
37    }
38
39    @Test
40    public void test_HijrahDate_getLong() {
41        // 1300 is the first year in the HijrahChronology in the umalqura configuration.
42        HijrahDate date = HijrahDate.of(1300, 2, 5);
43        assertEquals(1300, date.getLong(ChronoField.YEAR_OF_ERA));
44        assertEquals(1300, date.getLong(ChronoField.YEAR));
45        assertEquals(2, date.getLong(ChronoField.MONTH_OF_YEAR));
46        // Proleptic month starts with 0 for the first month of the proleptic year 0.
47        assertEquals(1300 * 12 + 2 - 1, date.getLong(ChronoField.PROLEPTIC_MONTH));
48        assertEquals(5, date.getLong(ChronoField.DAY_OF_MONTH));
49        // first month of the year 1300 has 30 days.
50        assertEquals(30 + 5, date.getLong(ChronoField.DAY_OF_YEAR));
51        assertEquals(date.toEpochDay(), date.getLong(ChronoField.EPOCH_DAY));
52    }
53
54    @Test
55    public void test_HijrahDate_withVariant_same() {
56        // There is currently no way of creating an alternative HijrahChronology, so only this
57        // case and the null case are tested.
58        HijrahDate date1 = HijrahDate.now();
59        HijrahDate date2 = date1.withVariant(HijrahChronology.INSTANCE);
60        assertSame(date1, date2);
61    }
62
63    @Test(expected = NullPointerException.class)
64    public void test_HijrahDate_withVariant_null() {
65        HijrahDate.now().withVariant(null);
66    }
67}
68