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 
  63 import java.time.DateTimeException;
  64 import java.time.LocalDate;
  65 import java.time.LocalDateTime;
  66 import java.time.LocalTime;
  67 import java.time.Month;
  68 import java.time.ZoneOffset;
  69 import java.time.chrono.MinguoChronology;
  70 import java.time.chrono.MinguoDate;
  71 import java.time.temporal.Adjusters;
  72 import java.time.temporal.ChronoUnit;
  73 import java.time.chrono.ChronoZonedDateTime;
  74 import java.time.chrono.Chronology;
  75 import java.time.chrono.ChronoLocalDate;
  76 import java.time.chrono.ChronoLocalDateTime;
  77 import java.time.chrono.IsoChronology;
  78 import java.time.chrono.Era;
  79 import java.time.Year;
  80 
  81 import org.testng.Assert;
  82 import org.testng.annotations.DataProvider;
  83 import org.testng.annotations.Test;
  84 
  85 /**
  86  * Test.
  87  */
  88 @Test
  89 public class TestMinguoChronology {
  90 
  91     private static final int YDIFF = 1911;
  92     //-----------------------------------------------------------------------
  93     // Chronology.ofName("Minguo")  Lookup by name
  94     //-----------------------------------------------------------------------
  95     @Test(groups={"tck"})
  96     public void test_chrono_byName() {
  97         Chronology c = MinguoChronology.INSTANCE;
  98         Chronology test = Chronology.of("Minguo");
  99         Assert.assertNotNull(test, "The Minguo calendar could not be found byName");
 100         Assert.assertEquals(test.getId(), "Minguo", "ID mismatch");
 101         Assert.assertEquals(test.getCalendarType(), "roc", "Type mismatch");
 102         Assert.assertEquals(test, c);
 103     }
 104 
 105     //-----------------------------------------------------------------------
 106     // creation, toLocalDate()
 107     //-----------------------------------------------------------------------
 108     @DataProvider(name="samples")
 109     Object[][] data_samples() {
 110         return new Object[][] {
 111             {MinguoChronology.INSTANCE.date(1, 1, 1), LocalDate.of(1 + YDIFF, 1, 1)},
 112             {MinguoChronology.INSTANCE.date(1, 1, 2), LocalDate.of(1 + YDIFF, 1, 2)},
 113             {MinguoChronology.INSTANCE.date(1, 1, 3), LocalDate.of(1 + YDIFF, 1, 3)},
 114 
 115             {MinguoChronology.INSTANCE.date(2, 1, 1), LocalDate.of(2 + YDIFF, 1, 1)},
 116             {MinguoChronology.INSTANCE.date(3, 1, 1), LocalDate.of(3 + YDIFF, 1, 1)},
 117             {MinguoChronology.INSTANCE.date(3, 12, 6), LocalDate.of(3 + YDIFF, 12, 6)},
 118             {MinguoChronology.INSTANCE.date(4, 1, 1), LocalDate.of(4 + YDIFF, 1, 1)},
 119             {MinguoChronology.INSTANCE.date(4, 7, 3), LocalDate.of(4 + YDIFF, 7, 3)},
 120             {MinguoChronology.INSTANCE.date(4, 7, 4), LocalDate.of(4 + YDIFF, 7, 4)},
 121             {MinguoChronology.INSTANCE.date(5, 1, 1), LocalDate.of(5 + YDIFF, 1, 1)},
 122             {MinguoChronology.INSTANCE.date(100, 3, 3), LocalDate.of(100 + YDIFF, 3, 3)},
 123             {MinguoChronology.INSTANCE.date(101, 10, 28), LocalDate.of(101 + YDIFF, 10, 28)},
 124             {MinguoChronology.INSTANCE.date(101, 10, 29), LocalDate.of(101 + YDIFF, 10, 29)},
 125 
 126             {MinguoChronology.INSTANCE.dateYearDay(1916 - YDIFF, 60), LocalDate.of(1916, 2, 29)},
 127             {MinguoChronology.INSTANCE.dateYearDay(1908 - YDIFF, 60), LocalDate.of(1908, 2, 29)},
 128             {MinguoChronology.INSTANCE.dateYearDay(2000 - YDIFF, 60), LocalDate.of(2000, 2, 29)},
 129             {MinguoChronology.INSTANCE.dateYearDay(2400 - YDIFF, 60), LocalDate.of(2400, 2, 29)},
 130         };
 131     }
 132 
 133     @Test(dataProvider="samples", groups={"tck"})
 134     public void test_toLocalDate(MinguoDate minguo, LocalDate iso) {
 135         assertEquals(LocalDate.from(minguo), iso);
 136     }
 137 
 138     @Test(dataProvider="samples", groups={"tck"})
 139     public void test_fromCalendrical(MinguoDate minguo, LocalDate iso) {
 140         assertEquals(MinguoChronology.INSTANCE.date(iso), minguo);
 141     }
 142 
 143     @SuppressWarnings("unused")
 144     @Test(dataProvider="samples", groups={"implementation"})
 145     public void test_MinguoDate(MinguoDate minguoDate, LocalDate iso) {
 146         MinguoDate hd = minguoDate;
 147         ChronoLocalDateTime<MinguoDate> hdt = hd.atTime(LocalTime.NOON);
 148         ZoneOffset zo = ZoneOffset.ofHours(1);
 149         ChronoZonedDateTime<MinguoDate> hzdt = hdt.atZone(zo);
 150         hdt = hdt.plus(1, ChronoUnit.YEARS);
 151         hdt = hdt.plus(1, ChronoUnit.MONTHS);
 152         hdt = hdt.plus(1, ChronoUnit.DAYS);
 153         hdt = hdt.plus(1, ChronoUnit.HOURS);
 154         hdt = hdt.plus(1, ChronoUnit.MINUTES);
 155         hdt = hdt.plus(1, ChronoUnit.SECONDS);
 156         hdt = hdt.plus(1, ChronoUnit.NANOS);
 157         ChronoLocalDateTime<MinguoDate> a2 = hzdt.toLocalDateTime();
 158         MinguoDate a3 = a2.toLocalDate();
 159         MinguoDate a5 = hzdt.toLocalDate();
 160         //System.out.printf(" d: %s, dt: %s; odt: %s; zodt: %s; a4: %s%n", date, hdt, hodt, hzdt, a5);
 161     }
 162 
 163     @Test()
 164     public void test_MinguoChrono() {
 165         MinguoDate h1 = (MinguoDate)MinguoChronology.ERA_ROC.date(1, 2, 3);
 166         MinguoDate h2 = h1;
 167         ChronoLocalDateTime<MinguoDate> h3 = h2.atTime(LocalTime.NOON);
 168         @SuppressWarnings("unused")
 169         ChronoZonedDateTime<MinguoDate> h4 = h3.atZone(ZoneOffset.UTC);
 170     }
 171 
 172     @DataProvider(name="badDates")
 173     Object[][] data_badDates() {
 174         return new Object[][] {
 175             {1912, 0, 0},
 176 
 177             {1912, -1, 1},
 178             {1912, 0, 1},
 179             {1912, 14, 1},
 180             {1912, 15, 1},
 181 
 182             {1912, 1, -1},
 183             {1912, 1, 0},
 184             {1912, 1, 32},
 185             {1912, 2, 29},
 186             {1912, 2, 30},
 187 
 188             {1912, 12, -1},
 189             {1912, 12, 0},
 190             {1912, 12, 32},
 191 
 192             {1907 - YDIFF, 2, 29},
 193             {100 - YDIFF, 2, 29},
 194             {2100 - YDIFF, 2, 29},
 195             {2101 - YDIFF, 2, 29},
 196             };
 197     }
 198 
 199     @Test(dataProvider="badDates", groups={"tck"}, expectedExceptions=DateTimeException.class)
 200     public void test_badDates(int year, int month, int dom) {
 201         MinguoChronology.INSTANCE.date(year, month, dom);
 202     }
 203 
 204     //-----------------------------------------------------------------------
 205     // prolepticYear() and is LeapYear()
 206     //-----------------------------------------------------------------------
 207     @DataProvider(name="prolepticYear")
 208     Object[][] data_prolepticYear() {
 209         return new Object[][] {
 210             {1, MinguoChronology.ERA_ROC, 1912 - YDIFF, 1912 - YDIFF, true},
 211             {1, MinguoChronology.ERA_ROC, 1916 - YDIFF, 1916 - YDIFF, true},
 212             {1, MinguoChronology.ERA_ROC, 1914 - YDIFF, 1914 - YDIFF, false},
 213             {1, MinguoChronology.ERA_ROC, 2000 - YDIFF, 2000 - YDIFF, true},
 214             {1, MinguoChronology.ERA_ROC, 2100 - YDIFF, 2100 - YDIFF, false},
 215             {1, MinguoChronology.ERA_ROC, 0, 0, false},
 216             {1, MinguoChronology.ERA_ROC, 1908 - YDIFF, 1908 - YDIFF, true},
 217             {1, MinguoChronology.ERA_ROC, 1900 - YDIFF, 1900 - YDIFF, false},
 218             {1, MinguoChronology.ERA_ROC, 1600 - YDIFF, 1600 - YDIFF, true},
 219 
 220             {0, MinguoChronology.ERA_BEFORE_ROC, YDIFF - 1911, 1912 - YDIFF, true},
 221             {0, MinguoChronology.ERA_BEFORE_ROC, YDIFF - 1915, 1916 - YDIFF, true},
 222             {0, MinguoChronology.ERA_BEFORE_ROC, YDIFF - 1913, 1914 - YDIFF, false},
 223             {0, MinguoChronology.ERA_BEFORE_ROC, YDIFF - 1999, 2000 - YDIFF, true},
 224             {0, MinguoChronology.ERA_BEFORE_ROC, YDIFF - 2099, 2100 - YDIFF, false},
 225             {0, MinguoChronology.ERA_BEFORE_ROC, 1, 0, false},
 226             {0, MinguoChronology.ERA_BEFORE_ROC, YDIFF - 1907, 1908 - YDIFF, true},
 227             {0, MinguoChronology.ERA_BEFORE_ROC, YDIFF - 1899, 1900 - YDIFF, false},
 228             {0, MinguoChronology.ERA_BEFORE_ROC, YDIFF - 1599, 1600 - YDIFF, true},
 229 
 230         };
 231     }
 232 
 233     @Test(dataProvider="prolepticYear", groups={"tck"})
 234     public void test_prolepticYear(int eraValue, Era  era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) {
 235         Era eraObj = MinguoChronology.INSTANCE.eraOf(eraValue) ;
 236         assertTrue(MinguoChronology.INSTANCE.eras().contains(eraObj));
 237         assertEquals(eraObj, era);
 238         assertEquals(MinguoChronology.INSTANCE.prolepticYear(era, yearOfEra), expectedProlepticYear);
 239         assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear) ;
 240         assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear + YDIFF).isLeap()) ;
 241     }
 242 
 243     //-----------------------------------------------------------------------
 244     // with(DateTimeAdjuster)
 245     //-----------------------------------------------------------------------
 246     @Test(groups={"tck"})
 247     public void test_adjust1() {
 248         MinguoDate base = MinguoChronology.INSTANCE.date(2012, 10, 29);
 249         MinguoDate test = base.with(Adjusters.lastDayOfMonth());
 250         assertEquals(test, MinguoChronology.INSTANCE.date(2012, 10, 31));
 251     }
 252 
 253     @Test(groups={"tck"})
 254     public void test_adjust2() {
 255         MinguoDate base = MinguoChronology.INSTANCE.date(1728, 12, 2);
 256         MinguoDate test = base.with(Adjusters.lastDayOfMonth());
 257         assertEquals(test, MinguoChronology.INSTANCE.date(1728, 12, 31));
 258     }
 259 
 260     //-----------------------------------------------------------------------
 261     // MinguoDate.with(Local*)
 262     //-----------------------------------------------------------------------
 263     @Test(groups={"tck"})
 264     public void test_adjust_toLocalDate() {
 265         MinguoDate minguo = MinguoChronology.INSTANCE.date(99, 1, 4);
 266         MinguoDate test = minguo.with(LocalDate.of(2012, 7, 6));
 267         assertEquals(test, MinguoChronology.INSTANCE.date(101, 7, 6));
 268     }
 269 
 270     @Test(groups={"tck"}, expectedExceptions=DateTimeException.class)
 271     public void test_adjust_toMonth() {
 272         MinguoDate minguo = MinguoChronology.INSTANCE.date(1726, 1, 4);
 273         minguo.with(Month.APRIL);
 274     }
 275 
 276     //-----------------------------------------------------------------------
 277     // LocalDate.with(MinguoDate)
 278     //-----------------------------------------------------------------------
 279     @Test(groups={"tck"})
 280     public void test_LocalDate_adjustToMinguoDate() {
 281         MinguoDate minguo = MinguoChronology.INSTANCE.date(101, 10, 29);
 282         LocalDate test = LocalDate.MIN.with(minguo);
 283         assertEquals(test, LocalDate.of(2012, 10, 29));
 284     }
 285 
 286     @Test(groups={"tck"})
 287     public void test_LocalDateTime_adjustToMinguoDate() {
 288         MinguoDate minguo = MinguoChronology.INSTANCE.date(101, 10, 29);
 289         LocalDateTime test = LocalDateTime.MIN.with(minguo);
 290         assertEquals(test, LocalDateTime.of(2012, 10, 29, 0, 0));
 291     }
 292 
 293     //-----------------------------------------------------------------------
 294     // toString()
 295     //-----------------------------------------------------------------------
 296     @DataProvider(name="toString")
 297     Object[][] data_toString() {
 298         return new Object[][] {
 299             {MinguoChronology.INSTANCE.date(1, 1, 1), "Minguo ROC 1-01-01"},
 300             {MinguoChronology.INSTANCE.date(1728, 10, 28), "Minguo ROC 1728-10-28"},
 301             {MinguoChronology.INSTANCE.date(1728, 10, 29), "Minguo ROC 1728-10-29"},
 302             {MinguoChronology.INSTANCE.date(1727, 12, 5), "Minguo ROC 1727-12-05"},
 303             {MinguoChronology.INSTANCE.date(1727, 12, 6), "Minguo ROC 1727-12-06"},
 304         };
 305     }
 306 
 307     @Test(dataProvider="toString", groups={"tck"})
 308     public void test_toString(MinguoDate minguo, String expected) {
 309         assertEquals(minguo.toString(), expected);
 310     }
 311 
 312     //-----------------------------------------------------------------------
 313     // equals()
 314     //-----------------------------------------------------------------------
 315     @Test(groups="tck")
 316     public void test_equals_true() {
 317         assertTrue(MinguoChronology.INSTANCE.equals(MinguoChronology.INSTANCE));
 318     }
 319 
 320     @Test(groups="tck")
 321     public void test_equals_false() {
 322         assertFalse(MinguoChronology.INSTANCE.equals(IsoChronology.INSTANCE));
 323     }
 324 
 325 }