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.calendar;
  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.calendar.JapaneseChrono;
  71 import java.time.temporal.Adjusters;
  72 import java.time.temporal.Chrono;
  73 import java.time.temporal.ChronoLocalDate;
  74 import java.time.temporal.Era;
  75 import java.time.temporal.ISOChrono;
  76 
  77 import org.testng.Assert;
  78 import org.testng.annotations.DataProvider;
  79 import org.testng.annotations.Test;
  80 
  81 /**
  82  * Test.
  83  */
  84 @Test
  85 public class TestJapaneseChrono {
  86 
  87     //-----------------------------------------------------------------------
  88     // Chrono.ofName("Japanese")  Lookup by name
  89     //-----------------------------------------------------------------------
  90     @Test(groups={"tck"})
  91     public void test_chrono_byName() {
  92         Chrono<JapaneseChrono> c = JapaneseChrono.INSTANCE;
  93         Chrono<?> test = Chrono.of("Japanese");
  94         Assert.assertNotNull(test, "The Japanese calendar could not be found byName");
  95         Assert.assertEquals(test.getId(), "Japanese", "ID mismatch");
  96         Assert.assertEquals(test.getCalendarType(), "japanese", "Type mismatch");
  97         Assert.assertEquals(test, c);
  98     }
  99 
 100     //-----------------------------------------------------------------------
 101     // creation, toLocalDate()
 102     //-----------------------------------------------------------------------
 103     @DataProvider(name="samples")
 104     Object[][] data_samples() {
 105         return new Object[][] {
 106             {JapaneseChrono.INSTANCE.date(1, 1, 1), LocalDate.of(1, 1, 1)},
 107             {JapaneseChrono.INSTANCE.date(1, 1, 2), LocalDate.of(1, 1, 2)},
 108             {JapaneseChrono.INSTANCE.date(1, 1, 3), LocalDate.of(1, 1, 3)},
 109 
 110             {JapaneseChrono.INSTANCE.date(2, 1, 1), LocalDate.of(2, 1, 1)},
 111             {JapaneseChrono.INSTANCE.date(3, 1, 1), LocalDate.of(3, 1, 1)},
 112             {JapaneseChrono.INSTANCE.date(3, 12, 6), LocalDate.of(3, 12, 6)},
 113             {JapaneseChrono.INSTANCE.date(4, 1, 1), LocalDate.of(4, 1, 1)},
 114             {JapaneseChrono.INSTANCE.date(4, 7, 3), LocalDate.of(4, 7, 3)},
 115             {JapaneseChrono.INSTANCE.date(4, 7, 4), LocalDate.of(4, 7, 4)},
 116             {JapaneseChrono.INSTANCE.date(5, 1, 1), LocalDate.of(5, 1, 1)},
 117             {JapaneseChrono.INSTANCE.date(1662, 3, 3), LocalDate.of(1662, 3, 3)},
 118             {JapaneseChrono.INSTANCE.date(1728, 10, 28), LocalDate.of(1728, 10, 28)},
 119             {JapaneseChrono.INSTANCE.date(1728, 10, 29), LocalDate.of(1728, 10, 29)},
 120         };
 121     }
 122 
 123     @Test(dataProvider="samples", groups={"tck"})
 124     public void test_toLocalDate(ChronoLocalDate<JapaneseChrono> jdate, LocalDate iso) {
 125         assertEquals(LocalDate.from(jdate), iso);
 126     }
 127 
 128     @Test(dataProvider="samples", groups={"tck"})
 129     public void test_fromCalendrical(ChronoLocalDate<JapaneseChrono> jdate, LocalDate iso) {
 130         assertEquals(JapaneseChrono.INSTANCE.date(iso), jdate);
 131     }
 132 
 133     @DataProvider(name="badDates")
 134     Object[][] data_badDates() {
 135         return new Object[][] {
 136             {1728, 0, 0},
 137 
 138             {1728, -1, 1},
 139             {1728, 0, 1},
 140             {1728, 14, 1},
 141             {1728, 15, 1},
 142 
 143             {1728, 1, -1},
 144             {1728, 1, 0},
 145             {1728, 1, 32},
 146 
 147             {1728, 12, -1},
 148             {1728, 12, 0},
 149             {1728, 12, 32},
 150         };
 151     }
 152 
 153     @Test(dataProvider="badDates", groups={"tck"}, expectedExceptions=DateTimeException.class)
 154     public void test_badDates(int year, int month, int dom) {
 155         JapaneseChrono.INSTANCE.date(year, month, dom);
 156     }
 157 
 158     //-----------------------------------------------------------------------
 159     // with(WithAdjuster)
 160     //-----------------------------------------------------------------------
 161     @Test(groups={"tck"})
 162     public void test_adjust1() {
 163         ChronoLocalDate<JapaneseChrono> base = JapaneseChrono.INSTANCE.date(1728, 10, 29);
 164         ChronoLocalDate<JapaneseChrono> test = base.with(Adjusters.lastDayOfMonth());
 165         assertEquals(test, JapaneseChrono.INSTANCE.date(1728, 10, 31));
 166     }
 167 
 168     @Test(groups={"tck"})
 169     public void test_adjust2() {
 170         ChronoLocalDate<JapaneseChrono> base = JapaneseChrono.INSTANCE.date(1728, 12, 2);
 171         ChronoLocalDate<JapaneseChrono> test = base.with(Adjusters.lastDayOfMonth());
 172         assertEquals(test, JapaneseChrono.INSTANCE.date(1728, 12, 31));
 173     }
 174 
 175     //-----------------------------------------------------------------------
 176     // JapaneseDate.with(Local*)
 177     //-----------------------------------------------------------------------
 178     @Test(groups={"tck"})
 179     public void test_adjust_toLocalDate() {
 180         ChronoLocalDate<JapaneseChrono> jdate = JapaneseChrono.INSTANCE.date(1726, 1, 4);
 181         ChronoLocalDate<JapaneseChrono> test = jdate.with(LocalDate.of(2012, 7, 6));
 182         assertEquals(test, JapaneseChrono.INSTANCE.date(2012, 7, 6));
 183     }
 184 
 185     @Test(groups={"tck"}, expectedExceptions=DateTimeException.class)
 186     public void test_adjust_toMonth() {
 187         ChronoLocalDate<?> jdate = JapaneseChrono.INSTANCE.date(1726, 1, 4);
 188         jdate.with(Month.APRIL);
 189     }
 190 
 191     //-----------------------------------------------------------------------
 192     // LocalDate.with(JapaneseDate)
 193     //-----------------------------------------------------------------------
 194     @Test(groups={"tck"})
 195     public void test_LocalDate_adjustToJapaneseDate() {
 196         ChronoLocalDate<JapaneseChrono> jdate = JapaneseChrono.INSTANCE.date(1728, 10, 29);
 197         LocalDate test = LocalDate.MIN.with(jdate);
 198         assertEquals(test, LocalDate.of(1728, 10, 29));
 199     }
 200 
 201     @Test(groups={"tck"})
 202     public void test_LocalDateTime_adjustToJapaneseDate() {
 203         ChronoLocalDate<JapaneseChrono> jdate = JapaneseChrono.INSTANCE.date(1728, 10, 29);
 204         LocalDateTime test = LocalDateTime.MIN.with(jdate);
 205         assertEquals(test, LocalDateTime.of(1728, 10, 29, 0, 0));
 206     }
 207 
 208     //-----------------------------------------------------------------------
 209     // Check Japanese Eras
 210     //-----------------------------------------------------------------------
 211     @DataProvider(name="japaneseEras")
 212     Object[][] data_japanseseEras() {
 213         return new Object[][] {
 214             { JapaneseChrono.ERA_SEIREKI, -999, "Seireki"},
 215             { JapaneseChrono.ERA_MEIJI, -1, "Meiji"},
 216             { JapaneseChrono.ERA_TAISHO, 0, "Taisho"},
 217             { JapaneseChrono.ERA_SHOWA, 1, "Showa"},
 218             { JapaneseChrono.ERA_HEISEI, 2, "Heisei"},
 219         };
 220     }
 221 
 222     @Test(groups={"tck"}, dataProvider="japaneseEras")
 223     public void test_Japanese_Eras(Era era, int eraValue, String name) {
 224         assertEquals(era.getValue(), eraValue, "EraValue");
 225         assertEquals(era.toString(), name, "Era Name");
 226         assertEquals(era, JapaneseChrono.INSTANCE.eraOf(eraValue), "JapaneseChrono.eraOf()");
 227         List<Era<JapaneseChrono>> eras = JapaneseChrono.INSTANCE.eras();
 228         assertTrue(eras.contains(era), "Era is not present in JapaneseChrono.INSTANCE.eras()");
 229     }
 230 
 231     @Test(groups="tck")
 232     public void test_Japanese_badEras() {
 233         int badEras[] = {-1000, -998, -997, -2, 3, 4, 1000};
 234         for (int badEra : badEras) {
 235             try {
 236                 Era<JapaneseChrono> era = JapaneseChrono.INSTANCE.eraOf(badEra);
 237                 fail("JapaneseChrono.eraOf returned " + era + " + for invalid eraValue " + badEra);
 238             } catch (DateTimeException ex) {
 239                 // ignore expected exception
 240             }
 241         }
 242     }
 243 
 244     //-----------------------------------------------------------------------
 245     // toString()
 246     //-----------------------------------------------------------------------
 247     @DataProvider(name="toString")
 248     Object[][] data_toString() {
 249         return new Object[][] {
 250             {JapaneseChrono.INSTANCE.date(0001,  1,  1), "Japanese 0001-01-01"},
 251             {JapaneseChrono.INSTANCE.date(1728, 10, 28), "Japanese 1728-10-28"},
 252             {JapaneseChrono.INSTANCE.date(1728, 10, 29), "Japanese 1728-10-29"},
 253             {JapaneseChrono.INSTANCE.date(1727, 12,  5), "Japanese 1727-12-05"},
 254             {JapaneseChrono.INSTANCE.date(1727, 12,  6), "Japanese 1727-12-06"},
 255             {JapaneseChrono.INSTANCE.date(1868,  9,  8), "Japanese Meiji 1-09-08"},
 256             {JapaneseChrono.INSTANCE.date(1912,  7, 29), "Japanese Meiji 45-07-29"},
 257             {JapaneseChrono.INSTANCE.date(1912,  7, 30), "Japanese Taisho 1-07-30"},
 258             {JapaneseChrono.INSTANCE.date(1926, 12, 24), "Japanese Taisho 15-12-24"},
 259             {JapaneseChrono.INSTANCE.date(1926, 12, 25), "Japanese Showa 1-12-25"},
 260             {JapaneseChrono.INSTANCE.date(1989,  1,  7), "Japanese Showa 64-01-07"},
 261             {JapaneseChrono.INSTANCE.date(1989,  1,  8), "Japanese Heisei 1-01-08"},
 262             {JapaneseChrono.INSTANCE.date(2012, 12,  6), "Japanese Heisei 24-12-06"},
 263         };
 264     }
 265 
 266     @Test(dataProvider="toString", groups={"tck"})
 267     public void test_toString(ChronoLocalDate<JapaneseChrono> jdate, String expected) {
 268         assertEquals(jdate.toString(), expected);
 269     }
 270 
 271     //-----------------------------------------------------------------------
 272     // equals()
 273     //-----------------------------------------------------------------------
 274     @Test(groups="tck")
 275     public void test_equals_true() {
 276         assertTrue(JapaneseChrono.INSTANCE.equals(JapaneseChrono.INSTANCE));
 277     }
 278 
 279     @Test(groups="tck")
 280     public void test_equals_false() {
 281         assertFalse(JapaneseChrono.INSTANCE.equals(ISOChrono.INSTANCE));
 282     }
 283 
 284 }