1 /*
   2  * Copyright (c) 2012, 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 /*
  27  * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
  28  *
  29  * All rights reserved.
  30  *
  31  * Redistribution and use in source and binary forms, with or without
  32  * modification, are permitted provided that the following conditions are met:
  33  *
  34  *  * Redistributions of source code must retain the above copyright notice,
  35  *    this list of conditions and the following disclaimer.
  36  *
  37  *  * Redistributions in binary form must reproduce the above copyright notice,
  38  *    this list of conditions and the following disclaimer in the documentation
  39  *    and/or other materials provided with the distribution.
  40  *
  41  *  * Neither the name of JSR-310 nor the names of its contributors
  42  *    may be used to endorse or promote products derived from this software
  43  *    without specific prior written permission.
  44  *
  45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56  */
  57 package tck.java.time.chrono;
  58 
  59 import static org.testng.Assert.assertEquals;
  60 import static org.testng.Assert.assertFalse;
  61 import static org.testng.Assert.assertTrue;
  62 import static org.testng.Assert.fail;
  63 
  64 import java.util.List;
  65 
  66 import java.time.DateTimeException;
  67 import java.time.LocalDate;
  68 import java.time.LocalDateTime;
  69 import java.time.Month;
  70 import java.time.Year;
  71 import java.time.chrono.JapaneseChronology;
  72 import java.time.chrono.JapaneseDate;
  73 import java.time.chrono.Chronology;
  74 import java.time.chrono.Era;
  75 import java.time.chrono.IsoChronology;
  76 import java.time.temporal.Adjusters;
  77 import java.util.Locale;
  78 
  79 import org.testng.Assert;
  80 import org.testng.annotations.DataProvider;
  81 import org.testng.annotations.Test;
  82 
  83 /**
  84  * Test.
  85  */
  86 @Test
  87 public class TestJapaneseChronology {
  88     private static int YDIFF_HEISEI = 1988;
  89     private static int YDIFF_MEIJI = 1867;
  90     private static int YDIFF_SHOWA = 1925;
  91     private static int YDIFF_TAISHO = 1911;
  92 
  93     //-----------------------------------------------------------------------
  94     // Chronology.of(String)
  95     //-----------------------------------------------------------------------
  96     @Test
  97     public void test_chrono_byName() {
  98         Chronology c = JapaneseChronology.INSTANCE;
  99         Chronology test = Chronology.of("Japanese");
 100         Assert.assertNotNull(test, "The Japanese calendar could not be found byName");
 101         Assert.assertEquals(test.getId(), "Japanese", "ID mismatch");
 102         Assert.assertEquals(test.getCalendarType(), "japanese", "Type mismatch");
 103         Assert.assertEquals(test, c);
 104     }
 105 
 106     //-----------------------------------------------------------------------
 107     // Chronology.ofLocale(Locale)
 108     //-----------------------------------------------------------------------
 109     @Test
 110     public void test_chrono_byLocale_fullTag_japaneseCalendarFromJapan() {
 111         Chronology test = Chronology.ofLocale(Locale.forLanguageTag("ja-JP-u-ca-japanese"));
 112         Assert.assertEquals(test.getId(), "Japanese");
 113         Assert.assertEquals(test, JapaneseChronology.INSTANCE);
 114     }
 115 
 116     @Test
 117     public void test_chrono_byLocale_fullTag_japaneseCalendarFromElsewhere() {
 118         Chronology test = Chronology.ofLocale(Locale.forLanguageTag("en-US-u-ca-japanese"));
 119         Assert.assertEquals(test.getId(), "Japanese");
 120         Assert.assertEquals(test, JapaneseChronology.INSTANCE);
 121     }
 122 
 123     @Test
 124     public void test_chrono_byLocale_oldJP_noVariant() {
 125         Chronology test = Chronology.ofLocale(new Locale("ja", "JP"));
 126         Assert.assertEquals(test.getId(), "ISO");
 127         Assert.assertEquals(test, IsoChronology.INSTANCE);
 128     }
 129 
 130     @Test
 131     public void test_chrono_byLocale_oldJP_variant() {
 132         Chronology test = Chronology.ofLocale(new Locale("ja", "JP", "JP"));
 133         Assert.assertEquals(test.getId(), "Japanese");
 134         Assert.assertEquals(test, JapaneseChronology.INSTANCE);
 135     }
 136 
 137     @Test
 138     public void test_chrono_byLocale_iso() {
 139         Assert.assertEquals(Chronology.ofLocale(new Locale("ja", "JP")).getId(), "ISO");
 140         Assert.assertEquals(Chronology.ofLocale(Locale.forLanguageTag("ja-JP")).getId(), "ISO");
 141         Assert.assertEquals(Chronology.ofLocale(Locale.forLanguageTag("ja-JP-JP")).getId(), "ISO");
 142     }
 143 
 144     //-----------------------------------------------------------------------
 145     // creation, toLocalDate()
 146     //-----------------------------------------------------------------------
 147     @DataProvider(name="samples")
 148     Object[][] data_samples() {
 149         return new Object[][] {
 150             {JapaneseChronology.INSTANCE.date(1, 1, 1), LocalDate.of(1, 1, 1)},
 151             {JapaneseChronology.INSTANCE.date(1, 1, 2), LocalDate.of(1, 1, 2)},
 152             {JapaneseChronology.INSTANCE.date(1, 1, 3), LocalDate.of(1, 1, 3)},
 153 
 154             {JapaneseChronology.INSTANCE.date(2, 1, 1), LocalDate.of(2, 1, 1)},
 155             {JapaneseChronology.INSTANCE.date(3, 1, 1), LocalDate.of(3, 1, 1)},
 156             {JapaneseChronology.INSTANCE.date(3, 12, 6), LocalDate.of(3, 12, 6)},
 157             {JapaneseChronology.INSTANCE.date(4, 1, 1), LocalDate.of(4, 1, 1)},
 158             {JapaneseChronology.INSTANCE.date(4, 7, 3), LocalDate.of(4, 7, 3)},
 159             {JapaneseChronology.INSTANCE.date(4, 7, 4), LocalDate.of(4, 7, 4)},
 160             {JapaneseChronology.INSTANCE.date(5, 1, 1), LocalDate.of(5, 1, 1)},
 161             {JapaneseChronology.INSTANCE.date(1662, 3, 3), LocalDate.of(1662, 3, 3)},
 162             {JapaneseChronology.INSTANCE.date(1728, 10, 28), LocalDate.of(1728, 10, 28)},
 163             {JapaneseChronology.INSTANCE.date(1728, 10, 29), LocalDate.of(1728, 10, 29)},
 164 
 165             {JapaneseChronology.INSTANCE.date(JapaneseChronology.ERA_HEISEI, 1996 - YDIFF_HEISEI, 2, 29), LocalDate.of(1996, 2, 29)},
 166             {JapaneseChronology.INSTANCE.date(JapaneseChronology.ERA_HEISEI, 2000 - YDIFF_HEISEI, 2, 29), LocalDate.of(2000, 2, 29)},
 167             {JapaneseChronology.INSTANCE.date(JapaneseChronology.ERA_MEIJI, 1868 - YDIFF_MEIJI, 2, 29), LocalDate.of(1868, 2, 29)},
 168             {JapaneseChronology.INSTANCE.date(JapaneseChronology.ERA_SHOWA, 1928 - YDIFF_SHOWA, 2, 29), LocalDate.of(1928, 2, 29)},
 169             {JapaneseChronology.INSTANCE.date(JapaneseChronology.ERA_TAISHO, 1912 - YDIFF_TAISHO, 2, 29), LocalDate.of(1912, 2, 29)},
 170 
 171             {JapaneseChronology.INSTANCE.dateYearDay(1996, 60), LocalDate.of(1996, 2, 29)},
 172             {JapaneseChronology.INSTANCE.dateYearDay(1868, 60), LocalDate.of(1868, 2, 29)},
 173             {JapaneseChronology.INSTANCE.dateYearDay(1928, 60), LocalDate.of(1928, 2, 29)},
 174             {JapaneseChronology.INSTANCE.dateYearDay(1912, 60), LocalDate.of(1912, 2, 29)},
 175         };
 176     }
 177 
 178     @Test(dataProvider="samples", groups={"tck"})
 179     public void test_toLocalDate(JapaneseDate jdate, LocalDate iso) {
 180         assertEquals(LocalDate.from(jdate), iso);
 181     }
 182 
 183     @Test(dataProvider="samples", groups={"tck"})
 184     public void test_fromCalendrical(JapaneseDate jdate, LocalDate iso) {
 185         assertEquals(JapaneseChronology.INSTANCE.date(iso), jdate);
 186     }
 187 
 188     @DataProvider(name="badDates")
 189     Object[][] data_badDates() {
 190         return new Object[][] {
 191             {1728, 0, 0},
 192 
 193             {1728, -1, 1},
 194             {1728, 0, 1},
 195             {1728, 14, 1},
 196             {1728, 15, 1},
 197 
 198             {1728, 1, -1},
 199             {1728, 1, 0},
 200             {1728, 1, 32},
 201 
 202             {1728, 12, -1},
 203             {1728, 12, 0},
 204             {1728, 12, 32},
 205 
 206             {1725, 2, 29},
 207             {500, 2, 29},
 208             {2100, 2, 29},
 209         };
 210     }
 211 
 212     @Test(dataProvider="badDates", groups={"tck"}, expectedExceptions=DateTimeException.class)
 213     public void test_badDates(int year, int month, int dom) {
 214         JapaneseChronology.INSTANCE.date(year, month, dom);
 215     }
 216 
 217     //-----------------------------------------------------------------------
 218     // prolepticYear() and is LeapYear()
 219     //-----------------------------------------------------------------------
 220     @DataProvider(name="prolepticYear")
 221     Object[][] data_prolepticYear() {
 222         return new Object[][] {
 223             {2, JapaneseChronology.ERA_HEISEI, 1, 1 + YDIFF_HEISEI, false},
 224             {2, JapaneseChronology.ERA_HEISEI, 100, 100 + YDIFF_HEISEI, true},
 225             {2, JapaneseChronology.ERA_HEISEI, 0, YDIFF_HEISEI, true},
 226             {2, JapaneseChronology.ERA_HEISEI, -10, -10 + YDIFF_HEISEI, false},
 227 
 228             {-1, JapaneseChronology.ERA_MEIJI, 1, 1 + YDIFF_MEIJI, true},
 229             {-1, JapaneseChronology.ERA_MEIJI, 100, 100 + YDIFF_MEIJI, false},
 230             {-1, JapaneseChronology.ERA_MEIJI, 0, YDIFF_MEIJI, false},
 231             {-1, JapaneseChronology.ERA_MEIJI, -10, -10 + YDIFF_MEIJI, false},
 232 
 233             {1, JapaneseChronology.ERA_SHOWA, 1, 1 + YDIFF_SHOWA, false},
 234             {1, JapaneseChronology.ERA_SHOWA, 100, 100 + YDIFF_SHOWA, false},
 235             {1, JapaneseChronology.ERA_SHOWA, 0, YDIFF_SHOWA, false},
 236             {1, JapaneseChronology.ERA_SHOWA, -5, -5 + YDIFF_SHOWA, true},
 237 
 238             {0, JapaneseChronology.ERA_TAISHO, 1, 1 + YDIFF_TAISHO, true},
 239             {0, JapaneseChronology.ERA_TAISHO, 100, 100 + YDIFF_TAISHO, false},
 240             {0, JapaneseChronology.ERA_TAISHO, 0, YDIFF_TAISHO, false},
 241             {0, JapaneseChronology.ERA_TAISHO, -10, -10 + YDIFF_TAISHO, false},
 242 
 243         };
 244     }
 245 
 246     @Test(dataProvider="prolepticYear", groups={"tck"})
 247     public void test_prolepticYear(int eraValue, Era  era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) {
 248         Era eraObj = JapaneseChronology.INSTANCE.eraOf(eraValue) ;
 249         assertTrue(JapaneseChronology.INSTANCE.eras().contains(eraObj));
 250         assertEquals(eraObj, era);
 251         assertEquals(JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra), expectedProlepticYear);
 252         assertEquals(JapaneseChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear) ;
 253         assertEquals(JapaneseChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear).isLeap()) ;
 254     }
 255 
 256     //-----------------------------------------------------------------------
 257     // with(WithAdjuster)
 258     //-----------------------------------------------------------------------
 259     @Test(groups={"tck"})
 260     public void test_adjust1() {
 261         JapaneseDate base = JapaneseChronology.INSTANCE.date(1728, 10, 29);
 262         JapaneseDate test = base.with(Adjusters.lastDayOfMonth());
 263         assertEquals(test, JapaneseChronology.INSTANCE.date(1728, 10, 31));
 264     }
 265 
 266     @Test(groups={"tck"})
 267     public void test_adjust2() {
 268         JapaneseDate base = JapaneseChronology.INSTANCE.date(1728, 12, 2);
 269         JapaneseDate test = base.with(Adjusters.lastDayOfMonth());
 270         assertEquals(test, JapaneseChronology.INSTANCE.date(1728, 12, 31));
 271     }
 272 
 273     //-----------------------------------------------------------------------
 274     // JapaneseDate.with(Local*)
 275     //-----------------------------------------------------------------------
 276     @Test(groups={"tck"})
 277     public void test_adjust_toLocalDate() {
 278         JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1726, 1, 4);
 279         JapaneseDate test = jdate.with(LocalDate.of(2012, 7, 6));
 280         assertEquals(test, JapaneseChronology.INSTANCE.date(2012, 7, 6));
 281     }
 282 
 283     @Test(groups={"tck"}, expectedExceptions=DateTimeException.class)
 284     public void test_adjust_toMonth() {
 285         JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1726, 1, 4);
 286         jdate.with(Month.APRIL);
 287     }
 288 
 289     //-----------------------------------------------------------------------
 290     // LocalDate.with(JapaneseDate)
 291     //-----------------------------------------------------------------------
 292     @Test(groups={"tck"})
 293     public void test_LocalDate_adjustToJapaneseDate() {
 294         JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1728, 10, 29);
 295         LocalDate test = LocalDate.MIN.with(jdate);
 296         assertEquals(test, LocalDate.of(1728, 10, 29));
 297     }
 298 
 299     @Test(groups={"tck"})
 300     public void test_LocalDateTime_adjustToJapaneseDate() {
 301         JapaneseDate jdate = JapaneseChronology.INSTANCE.date(1728, 10, 29);
 302         LocalDateTime test = LocalDateTime.MIN.with(jdate);
 303         assertEquals(test, LocalDateTime.of(1728, 10, 29, 0, 0));
 304     }
 305 
 306     //-----------------------------------------------------------------------
 307     // Check Japanese Eras
 308     //-----------------------------------------------------------------------
 309     @DataProvider(name="japaneseEras")
 310     Object[][] data_japanseseEras() {
 311         return new Object[][] {
 312             { JapaneseChronology.ERA_SEIREKI, -999, "Seireki"},
 313             { JapaneseChronology.ERA_MEIJI, -1, "Meiji"},
 314             { JapaneseChronology.ERA_TAISHO, 0, "Taisho"},
 315             { JapaneseChronology.ERA_SHOWA, 1, "Showa"},
 316             { JapaneseChronology.ERA_HEISEI, 2, "Heisei"},
 317         };
 318     }
 319 
 320     @Test(groups={"tck"}, dataProvider="japaneseEras")
 321     public void test_Japanese_Eras(Era era, int eraValue, String name) {
 322         assertEquals(era.getValue(), eraValue, "EraValue");
 323         assertEquals(era.toString(), name, "Era Name");
 324         assertEquals(era, JapaneseChronology.INSTANCE.eraOf(eraValue), "JapaneseChronology.eraOf()");
 325         List<Era> eras = JapaneseChronology.INSTANCE.eras();
 326         assertTrue(eras.contains(era), "Era is not present in JapaneseChronology.INSTANCE.eras()");
 327     }
 328 
 329     @Test(groups="tck")
 330     public void test_Japanese_badEras() {
 331         int badEras[] = {-1000, -998, -997, -2, 3, 4, 1000};
 332         for (int badEra : badEras) {
 333             try {
 334                 Era era = JapaneseChronology.INSTANCE.eraOf(badEra);
 335                 fail("JapaneseChronology.eraOf returned " + era + " + for invalid eraValue " + badEra);
 336             } catch (DateTimeException ex) {
 337                 // ignore expected exception
 338             }
 339         }
 340     }
 341 
 342     //-----------------------------------------------------------------------
 343     // toString()
 344     //-----------------------------------------------------------------------
 345     @DataProvider(name="toString")
 346     Object[][] data_toString() {
 347         return new Object[][] {
 348             {JapaneseChronology.INSTANCE.date(0001,  1,  1), "Japanese 0001-01-01"},
 349             {JapaneseChronology.INSTANCE.date(1728, 10, 28), "Japanese 1728-10-28"},
 350             {JapaneseChronology.INSTANCE.date(1728, 10, 29), "Japanese 1728-10-29"},
 351             {JapaneseChronology.INSTANCE.date(1727, 12,  5), "Japanese 1727-12-05"},
 352             {JapaneseChronology.INSTANCE.date(1727, 12,  6), "Japanese 1727-12-06"},
 353             {JapaneseChronology.INSTANCE.date(1868,  9,  8), "Japanese Meiji 1-09-08"},
 354             {JapaneseChronology.INSTANCE.date(1912,  7, 29), "Japanese Meiji 45-07-29"},
 355             {JapaneseChronology.INSTANCE.date(1912,  7, 30), "Japanese Taisho 1-07-30"},
 356             {JapaneseChronology.INSTANCE.date(1926, 12, 24), "Japanese Taisho 15-12-24"},
 357             {JapaneseChronology.INSTANCE.date(1926, 12, 25), "Japanese Showa 1-12-25"},
 358             {JapaneseChronology.INSTANCE.date(1989,  1,  7), "Japanese Showa 64-01-07"},
 359             {JapaneseChronology.INSTANCE.date(1989,  1,  8), "Japanese Heisei 1-01-08"},
 360             {JapaneseChronology.INSTANCE.date(2012, 12,  6), "Japanese Heisei 24-12-06"},
 361         };
 362     }
 363 
 364     @Test(dataProvider="toString", groups={"tck"})
 365     public void test_toString(JapaneseDate jdate, String expected) {
 366         assertEquals(jdate.toString(), expected);
 367     }
 368 
 369     //-----------------------------------------------------------------------
 370     // equals()
 371     //-----------------------------------------------------------------------
 372     @Test(groups="tck")
 373     public void test_equals_true() {
 374         assertTrue(JapaneseChronology.INSTANCE.equals(JapaneseChronology.INSTANCE));
 375     }
 376 
 377     @Test(groups="tck")
 378     public void test_equals_false() {
 379         assertFalse(JapaneseChronology.INSTANCE.equals(IsoChronology.INSTANCE));
 380     }
 381 
 382 }