1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.java.time.chrono;
  27 
  28 import java.time.*;
  29 import java.time.chrono.*;
  30 import java.time.temporal.*;
  31 import java.util.List;
  32 import java.util.Locale;
  33 
  34 import org.testng.annotations.DataProvider;
  35 import org.testng.annotations.Test;
  36 import static org.testng.Assert.assertEquals;
  37 
  38 /**
  39  * Tests for the Japanese chronology
  40  */
  41 @Test
  42 public class TestJapaneseChronology {
  43     private static final JapaneseChronology JAPANESE = JapaneseChronology.INSTANCE;
  44     private static final Locale jaJPJP = Locale.forLanguageTag("ja-JP-u-ca-japanese");
  45 
  46     @DataProvider(name="transitions")
  47     Object[][] transitionData() {
  48         return new Object[][] {
  49             // Japanese era, yearOfEra, month, dayOfMonth, gregorianYear
  50             { JapaneseEra.SEIREKI, Year.MIN_VALUE, 1, 1, Year.MIN_VALUE },
  51             { JapaneseEra.SEIREKI, 1867, 12, 31, 1867 },
  52             { JapaneseEra.MEIJI,      1,  1, 25, 1868 }, // Note: the dates of Meiji 1 to 5 are incorrect
  53             { JapaneseEra.MEIJI,      6,  1,  1, 1873 },
  54             // Meiji-Taisho transition isn't accurate. 1912-07-30 is the last day of Meiji
  55             // and the first day of Taisho.
  56             { JapaneseEra.MEIJI,     45,  7, 29, 1912 },
  57             { JapaneseEra.TAISHO,     1,  7, 30, 1912 },
  58             // Same for Taisho-Showa transition. 1926-12-25 is the last day of Taisho
  59             // and the first day of Showa.
  60             { JapaneseEra.TAISHO,    15, 12, 24, 1926 },
  61             { JapaneseEra.SHOWA,      1, 12, 25, 1926 },
  62             { JapaneseEra.SHOWA,     64,  1,  7, 1989 },
  63             { JapaneseEra.HEISEI,     1,  1,  8, 1989 },
  64         };
  65     }
  66 
  67     @DataProvider(name="day_year_data")
  68     Object[][] dayYearData() {
  69         return new Object[][] {
  70             // Japanese era, yearOfEra, dayOfYear, month, dayOfMonth
  71             { JapaneseEra.MEIJI,  45,  211,  7, 29 },
  72             { JapaneseEra.TAISHO,  1,    1,  7, 30 },
  73             { JapaneseEra.TAISHO,  2,   60,  3,  1 },
  74             { JapaneseEra.TAISHO, 15,  358, 12, 24 },
  75             { JapaneseEra.SHOWA,   1,    1, 12, 25 },
  76             { JapaneseEra.SHOWA,   2,    8,  1,  8 },
  77             { JapaneseEra.SHOWA,  64,    7,  1,  7 },
  78             { JapaneseEra.HEISEI,  1,    1,  1,  8 },
  79             { JapaneseEra.HEISEI,  2,    8,  1,  8 },
  80         };
  81     }
  82 
  83     @DataProvider(name="range_data")
  84     Object[][] rangeData() {
  85         return new Object[][] {
  86             // field, minSmallest, minLargest, maxSmallest, maxLargest
  87             { ChronoField.ERA,         -999, -999, 2, 2},
  88             { ChronoField.YEAR_OF_ERA, -999999999, 1, 15, 999999999-1989 }, // depends on the current era
  89             { ChronoField.DAY_OF_YEAR, 1, 1, 7, 366},
  90         };
  91     }
  92 
  93     @DataProvider(name="invalid_dates")
  94     Object[][] invalidDatesData() {
  95         return new Object[][] {
  96             // Japanese era, yearOfEra, month, dayOfMonth
  97             { JapaneseEra.SEIREKI, Year.MIN_VALUE - 1, 1, 1 },
  98             { JapaneseEra.SEIREKI, 1855,  2, 29 },
  99             { JapaneseEra.SEIREKI, 1868,  1, 25 },
 100             { JapaneseEra.MEIJI,      6,  2, 29 },
 101             { JapaneseEra.MEIJI,     45,  7, 30 },
 102             { JapaneseEra.MEIJI,     46,  1,  1 },
 103             { JapaneseEra.TAISHO,     1,  7, 29 },
 104             { JapaneseEra.TAISHO,     2,  2, 29 },
 105             { JapaneseEra.TAISHO,    15, 12, 25 },
 106             { JapaneseEra.TAISHO,    16,  1,  1 },
 107             { JapaneseEra.SHOWA,      1, 12, 24 },
 108             { JapaneseEra.SHOWA,      2,  2, 29 },
 109             { JapaneseEra.SHOWA,     64,  1,  8 },
 110             { JapaneseEra.SHOWA,     65,  1,  1 },
 111             { JapaneseEra.HEISEI,     1,  1,  7 },
 112             { JapaneseEra.HEISEI,     1,  2, 29 },
 113             { JapaneseEra.HEISEI, Year.MAX_VALUE,  12, 31 },
 114         };
 115     }
 116 
 117     @DataProvider(name="invalid_eraYear")
 118     Object[][] invalidEraYearData() {
 119         return new Object[][] {
 120             // Japanese era, yearOfEra
 121             { JapaneseEra.SEIREKI, Year.MIN_VALUE - 1 },
 122             { JapaneseEra.SEIREKI, 2012 },
 123             { JapaneseEra.MEIJI,     -1 },
 124             { JapaneseEra.MEIJI,      0 },
 125             { JapaneseEra.MEIJI,     46 },
 126             { JapaneseEra.TAISHO,    -1 },
 127             { JapaneseEra.TAISHO,     0 },
 128             { JapaneseEra.TAISHO,    16 },
 129             { JapaneseEra.SHOWA,     -1 },
 130             { JapaneseEra.SHOWA,      0 },
 131             { JapaneseEra.SHOWA,     65 },
 132             { JapaneseEra.HEISEI,    -1 },
 133             { JapaneseEra.HEISEI,     0 },
 134             { JapaneseEra.HEISEI, Year.MAX_VALUE },
 135         };
 136     }
 137 
 138     @DataProvider(name="invalid_day_year_data")
 139     Object[][] invalidDayYearData() {
 140         return new Object[][] {
 141             // Japanese era, yearOfEra, dayOfYear
 142             { JapaneseEra.MEIJI,  45, 240 },
 143             { JapaneseEra.TAISHO,  1, 365 },
 144             { JapaneseEra.TAISHO,  2, 366 },
 145             { JapaneseEra.TAISHO, 15, 359 },
 146             { JapaneseEra.SHOWA,   1,   8 },
 147             { JapaneseEra.SHOWA,   2, 366 },
 148             { JapaneseEra.SHOWA,  64,   8 },
 149             { JapaneseEra.HEISEI,  1, 360 },
 150             { JapaneseEra.HEISEI,  2, 366 },
 151         };
 152     }
 153 
 154     @Test
 155     public void test_ofLocale() {
 156         // must be a singleton
 157         assertEquals(Chronology.ofLocale(jaJPJP) == JAPANESE, true);
 158     }
 159 
 160     @Test(dataProvider="transitions")
 161     public void test_transitions(JapaneseEra era, int yearOfEra, int month, int dayOfMonth, int gregorianYear) {
 162         assertEquals(JAPANESE.prolepticYear(era, yearOfEra), gregorianYear);
 163 
 164         JapaneseDate jdate1 = JapaneseDate.of(era, yearOfEra, month, dayOfMonth);
 165         JapaneseDate jdate2 = JapaneseDate.of(gregorianYear, month, dayOfMonth);
 166         assertEquals(jdate1, jdate2);
 167     }
 168 
 169     @Test(dataProvider="range_data")
 170     public void test_range(ChronoField field, int minSmallest, int minLargest, int maxSmallest, int maxLargest) {
 171         ValueRange range = JAPANESE.range(field);
 172         assertEquals(range.getMinimum(), minSmallest);
 173         assertEquals(range.getLargestMinimum(), minLargest);
 174         assertEquals(range.getSmallestMaximum(), maxSmallest);
 175         assertEquals(range.getMaximum(), maxLargest);
 176     }
 177 
 178     @Test(dataProvider="day_year_data")
 179     public void test_firstDayOfEra(JapaneseEra era, int yearOfEra, int dayOfYear, int month, int dayOfMonth) {
 180         JapaneseDate date1 = JAPANESE.dateYearDay(era, yearOfEra, dayOfYear);
 181         JapaneseDate date2 = JAPANESE.date(era, yearOfEra, month, dayOfMonth);
 182         assertEquals(date1, date2);
 183     }
 184 
 185     @Test(dataProvider="invalid_dates", expectedExceptions=DateTimeException.class)
 186     public void test_invalidDate(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {
 187         JapaneseDate jdate = JapaneseDate.of(era, yearOfEra, month, dayOfMonth);
 188         System.out.printf("No DateTimeException with %s %d.%02d.%02d%n", era, yearOfEra, month, dayOfMonth);
 189     }
 190 
 191     @Test(dataProvider="invalid_eraYear", expectedExceptions=DateTimeException.class)
 192     public void test_invalidEraYear(JapaneseEra era, int yearOfEra) {
 193         int year = JAPANESE.prolepticYear(era, yearOfEra);
 194         System.out.printf("No DateTimeException with era=%s, year=%d%n", era, yearOfEra);
 195     }
 196 
 197     @Test(dataProvider="invalid_day_year_data", expectedExceptions=DateTimeException.class)
 198     public void test_invalidDayYear(JapaneseEra era, int yearOfEra, int dayOfYear) {
 199         JapaneseDate date = JAPANESE.dateYearDay(era, yearOfEra, dayOfYear);
 200         System.out.printf("No DateTimeException with era=%s, year=%d, dayOfYear=%d%n", era, yearOfEra, dayOfYear);
 201     }
 202 }