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 java.time.temporal.ChronoField.DAY_OF_MONTH;
  60 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
  61 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  62 import static java.time.temporal.ChronoField.YEAR;
  63 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
  64 import static org.testng.Assert.assertEquals;
  65 import static org.testng.Assert.assertFalse;
  66 import static org.testng.Assert.assertTrue;
  67 
  68 import java.time.DateTimeException;
  69 import java.time.LocalDate;
  70 import java.time.LocalDateTime;
  71 import java.time.Month;
  72 import java.time.Year;
  73 import java.time.chrono.ThaiBuddhistChronology;
  74 import java.time.chrono.ThaiBuddhistDate;
  75 import java.time.chrono.Chronology;
  76 import java.time.chrono.ChronoLocalDate;
  77 import java.time.chrono.Era;
  78 import java.time.chrono.IsoChronology;
  79 import java.time.temporal.Adjusters;
  80 import java.time.temporal.ChronoField;
  81 import java.time.temporal.ValueRange;
  82 import java.util.Locale;
  83 
  84 import org.testng.Assert;
  85 import org.testng.annotations.DataProvider;
  86 import org.testng.annotations.Test;
  87 
  88 /**
  89  * Test.
  90  */
  91 @Test
  92 public class TestThaiBuddhistChronology {
  93 
  94     private static final int YDIFF = 543;
  95 
  96     //-----------------------------------------------------------------------
  97     // Chronology.of(String)
  98     //-----------------------------------------------------------------------
  99     @Test
 100     public void test_chrono_byName() {
 101         Chronology c = ThaiBuddhistChronology.INSTANCE;
 102         Chronology test = Chronology.of("ThaiBuddhist");
 103         Assert.assertNotNull(test, "The ThaiBuddhist calendar could not be found byName");
 104         Assert.assertEquals(test.getId(), "ThaiBuddhist", "ID mismatch");
 105         Assert.assertEquals(test.getCalendarType(), "buddhist", "Type mismatch");
 106         Assert.assertEquals(test, c);
 107     }
 108 
 109     //-----------------------------------------------------------------------
 110     // Chronology.ofLocale(Locale)
 111     //-----------------------------------------------------------------------
 112     @Test
 113     public void test_chrono_byLocale_fullTag_thaiCalendarFromThai() {
 114         Chronology test = Chronology.ofLocale(Locale.forLanguageTag("th-TH-u-ca-buddhist"));
 115         Assert.assertEquals(test.getId(), "ThaiBuddhist");
 116         Assert.assertEquals(test, ThaiBuddhistChronology.INSTANCE);
 117     }
 118 
 119     @Test
 120     public void test_chrono_byLocale_fullTag_thaiCalendarFromElsewhere() {
 121         Chronology test = Chronology.ofLocale(Locale.forLanguageTag("en-US-u-ca-buddhist"));
 122         Assert.assertEquals(test.getId(), "ThaiBuddhist");
 123         Assert.assertEquals(test, ThaiBuddhistChronology.INSTANCE);
 124     }
 125 
 126     @Test
 127     public void test_chrono_byLocale_oldTH_noVariant() {  // deliberately different to Calendar
 128         Chronology test = Chronology.ofLocale(new Locale("th", "TH"));
 129         Assert.assertEquals(test.getId(), "ISO");
 130         Assert.assertEquals(test, IsoChronology.INSTANCE);
 131     }
 132 
 133     @Test
 134     public void test_chrono_byLocale_oldTH_variant() {
 135         Chronology test = Chronology.ofLocale(new Locale("th", "TH", "TH"));
 136         Assert.assertEquals(test.getId(), "ISO");
 137         Assert.assertEquals(test, IsoChronology.INSTANCE);
 138     }
 139 
 140     @Test
 141     public void test_chrono_byLocale_iso() {
 142         Assert.assertEquals(Chronology.ofLocale(new Locale("th", "TH")).getId(), "ISO");
 143         Assert.assertEquals(Chronology.ofLocale(Locale.forLanguageTag("th-TH")).getId(), "ISO");
 144         Assert.assertEquals(Chronology.ofLocale(Locale.forLanguageTag("th-TH-TH")).getId(), "ISO");
 145     }
 146 
 147     //-----------------------------------------------------------------------
 148     // creation, toLocalDate()
 149     //-----------------------------------------------------------------------
 150     @DataProvider(name="samples")
 151     Object[][] data_samples() {
 152         return new Object[][] {
 153             {ThaiBuddhistChronology.INSTANCE.date(1 + YDIFF, 1, 1), LocalDate.of(1, 1, 1)},
 154             {ThaiBuddhistChronology.INSTANCE.date(1 + YDIFF, 1, 2), LocalDate.of(1, 1, 2)},
 155             {ThaiBuddhistChronology.INSTANCE.date(1 + YDIFF, 1, 3), LocalDate.of(1, 1, 3)},
 156 
 157             {ThaiBuddhistChronology.INSTANCE.date(2 + YDIFF, 1, 1), LocalDate.of(2, 1, 1)},
 158             {ThaiBuddhistChronology.INSTANCE.date(3 + YDIFF, 1, 1), LocalDate.of(3, 1, 1)},
 159             {ThaiBuddhistChronology.INSTANCE.date(3 + YDIFF, 12, 6), LocalDate.of(3, 12, 6)},
 160             {ThaiBuddhistChronology.INSTANCE.date(4 + YDIFF, 1, 1), LocalDate.of(4, 1, 1)},
 161             {ThaiBuddhistChronology.INSTANCE.date(4 + YDIFF, 7, 3), LocalDate.of(4, 7, 3)},
 162             {ThaiBuddhistChronology.INSTANCE.date(4 + YDIFF, 7, 4), LocalDate.of(4, 7, 4)},
 163             {ThaiBuddhistChronology.INSTANCE.date(5 + YDIFF, 1, 1), LocalDate.of(5, 1, 1)},
 164             {ThaiBuddhistChronology.INSTANCE.date(1662 + YDIFF, 3, 3), LocalDate.of(1662, 3, 3)},
 165             {ThaiBuddhistChronology.INSTANCE.date(1728 + YDIFF, 10, 28), LocalDate.of(1728, 10, 28)},
 166             {ThaiBuddhistChronology.INSTANCE.date(1728 + YDIFF, 10, 29), LocalDate.of(1728, 10, 29)},
 167             {ThaiBuddhistChronology.INSTANCE.date(2555, 8, 29), LocalDate.of(2012, 8, 29)},
 168 
 169             {ThaiBuddhistChronology.INSTANCE.dateYearDay(4 + YDIFF, 60), LocalDate.of(4, 2, 29)},
 170             {ThaiBuddhistChronology.INSTANCE.dateYearDay(400 + YDIFF, 60), LocalDate.of(400, 2, 29)},
 171             {ThaiBuddhistChronology.INSTANCE.dateYearDay(2000 + YDIFF, 60), LocalDate.of(2000, 2, 29)},
 172 
 173         };
 174     }
 175 
 176     @Test(dataProvider="samples", groups={"tck"})
 177     public void test_toLocalDate(ThaiBuddhistDate jdate, LocalDate iso) {
 178         assertEquals(LocalDate.from(jdate), iso);
 179     }
 180 
 181     @Test(dataProvider="samples", groups={"tck"})
 182     public void test_fromCalendrical(ThaiBuddhistDate jdate, LocalDate iso) {
 183         assertEquals(ThaiBuddhistChronology.INSTANCE.date(iso), jdate);
 184     }
 185 
 186     @DataProvider(name="badDates")
 187     Object[][] data_badDates() {
 188         return new Object[][] {
 189             {1728, 0, 0},
 190 
 191             {1728, -1, 1},
 192             {1728, 0, 1},
 193             {1728, 14, 1},
 194             {1728, 15, 1},
 195 
 196             {1728, 1, -1},
 197             {1728, 1, 0},
 198             {1728, 1, 32},
 199 
 200             {1728, 12, -1},
 201             {1728, 12, 0},
 202             {1728, 12, 32},
 203 
 204             {3 + YDIFF, 2, 29},
 205             {600 + YDIFF, 2, 29},
 206             {1501 + YDIFF, 2, 29},
 207         };
 208     }
 209 
 210     @Test(dataProvider="badDates", groups={"tck"}, expectedExceptions=DateTimeException.class)
 211     public void test_badDates(int year, int month, int dom) {
 212         ThaiBuddhistChronology.INSTANCE.date(year, month, dom);
 213     }
 214 
 215   //-----------------------------------------------------------------------
 216   // prolepticYear() and is LeapYear()
 217   //-----------------------------------------------------------------------
 218   @DataProvider(name="prolepticYear")
 219   Object[][] data_prolepticYear() {
 220       return new Object[][] {
 221           {1, ThaiBuddhistChronology.ERA_BE, 4 + YDIFF, 4 + YDIFF, true},
 222           {1, ThaiBuddhistChronology.ERA_BE, 7 + YDIFF, 7 + YDIFF, false},
 223           {1, ThaiBuddhistChronology.ERA_BE, 8 + YDIFF, 8 + YDIFF, true},
 224           {1, ThaiBuddhistChronology.ERA_BE, 1000 + YDIFF, 1000 + YDIFF, false},
 225           {1, ThaiBuddhistChronology.ERA_BE, 2000 + YDIFF, 2000 + YDIFF, true},
 226           {1, ThaiBuddhistChronology.ERA_BE, 0, 0, false},
 227           {1, ThaiBuddhistChronology.ERA_BE, -4 + YDIFF, -4 + YDIFF, true},
 228           {1, ThaiBuddhistChronology.ERA_BE, -7 + YDIFF, -7 + YDIFF, false},
 229           {1, ThaiBuddhistChronology.ERA_BE, -100 + YDIFF, -100 + YDIFF, false},
 230           {1, ThaiBuddhistChronology.ERA_BE, -800 + YDIFF, -800 + YDIFF, true},
 231 
 232           {0, ThaiBuddhistChronology.ERA_BEFORE_BE, -3 - YDIFF, 4 + YDIFF, true},
 233           {0, ThaiBuddhistChronology.ERA_BEFORE_BE, -6 - YDIFF, 7 + YDIFF, false},
 234           {0, ThaiBuddhistChronology.ERA_BEFORE_BE, -7 - YDIFF, 8 + YDIFF, true},
 235           {0, ThaiBuddhistChronology.ERA_BEFORE_BE, -999 - YDIFF, 1000 + YDIFF, false},
 236           {0, ThaiBuddhistChronology.ERA_BEFORE_BE, -1999 - YDIFF, 2000 + YDIFF, true},
 237           {0, ThaiBuddhistChronology.ERA_BEFORE_BE, 1, 0, false},
 238           {0, ThaiBuddhistChronology.ERA_BEFORE_BE, 5 - YDIFF, -4 + YDIFF, true},
 239           {0, ThaiBuddhistChronology.ERA_BEFORE_BE, 8 - YDIFF, -7 + YDIFF, false},
 240           {0, ThaiBuddhistChronology.ERA_BEFORE_BE, 101 - YDIFF, -100 + YDIFF, false},
 241           {0, ThaiBuddhistChronology.ERA_BEFORE_BE, 801 - YDIFF, -800 + YDIFF, true},
 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 = ThaiBuddhistChronology.INSTANCE.eraOf(eraValue) ;
 249       assertTrue(ThaiBuddhistChronology.INSTANCE.eras().contains(eraObj));
 250       assertEquals(eraObj, era);
 251       assertEquals(ThaiBuddhistChronology.INSTANCE.prolepticYear(era, yearOfEra), expectedProlepticYear);
 252       assertEquals(ThaiBuddhistChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear) ;
 253       assertEquals(ThaiBuddhistChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear - YDIFF).isLeap()) ;
 254   }
 255 
 256     //-----------------------------------------------------------------------
 257     // with(WithAdjuster)
 258     //-----------------------------------------------------------------------
 259     @Test(groups={"tck"})
 260     public void test_adjust1() {
 261         ThaiBuddhistDate base = ThaiBuddhistChronology.INSTANCE.date(1728, 10, 29);
 262         ThaiBuddhistDate test = base.with(Adjusters.lastDayOfMonth());
 263         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(1728, 10, 31));
 264     }
 265 
 266     @Test(groups={"tck"})
 267     public void test_adjust2() {
 268         ThaiBuddhistDate base = ThaiBuddhistChronology.INSTANCE.date(1728, 12, 2);
 269         ThaiBuddhistDate test = base.with(Adjusters.lastDayOfMonth());
 270         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(1728, 12, 31));
 271     }
 272 
 273     //-----------------------------------------------------------------------
 274     // withYear()
 275     //-----------------------------------------------------------------------
 276     @Test(groups={"tck"})
 277     public void test_withYear_BE() {
 278         ThaiBuddhistDate base = ThaiBuddhistChronology.INSTANCE.date(2555, 8, 29);
 279         ThaiBuddhistDate test = base.with(YEAR, 2554);
 280         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(2554, 8, 29));
 281     }
 282 
 283     @Test(groups={"tck"})
 284     public void test_withYear_BBE() {
 285         ThaiBuddhistDate base = ThaiBuddhistChronology.INSTANCE.date(-2554, 8, 29);
 286         ThaiBuddhistDate test = base.with(YEAR_OF_ERA, 2554);
 287         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(-2553, 8, 29));
 288     }
 289 
 290     //-----------------------------------------------------------------------
 291     // withEra()
 292     //-----------------------------------------------------------------------
 293     @Test(groups={"tck"})
 294     public void test_withEra_BE() {
 295         ThaiBuddhistDate base = ThaiBuddhistChronology.INSTANCE.date(2555, 8, 29);
 296         ThaiBuddhistDate test = base.with(ChronoField.ERA, ThaiBuddhistChronology.ERA_BE.getValue());
 297         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(2555, 8, 29));
 298     }
 299 
 300     @Test(groups={"tck"})
 301     public void test_withEra_BBE() {
 302         ThaiBuddhistDate base = ThaiBuddhistChronology.INSTANCE.date(-2554, 8, 29);
 303         ThaiBuddhistDate test = base.with(ChronoField.ERA, ThaiBuddhistChronology.ERA_BEFORE_BE.getValue());
 304         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(-2554, 8, 29));
 305     }
 306 
 307     @Test(groups={"tck"})
 308     public void test_withEra_swap() {
 309         ThaiBuddhistDate base = ThaiBuddhistChronology.INSTANCE.date(-2554, 8, 29);
 310         ThaiBuddhistDate test = base.with(ChronoField.ERA, ThaiBuddhistChronology.ERA_BE.getValue());
 311         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(2555, 8, 29));
 312     }
 313 
 314     //-----------------------------------------------------------------------
 315     // BuddhistDate.with(Local*)
 316     //-----------------------------------------------------------------------
 317     @Test(groups={"tck"})
 318     public void test_adjust_toLocalDate() {
 319         ThaiBuddhistDate jdate = ThaiBuddhistChronology.INSTANCE.date(1726, 1, 4);
 320         ThaiBuddhistDate test = jdate.with(LocalDate.of(2012, 7, 6));
 321         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(2555, 7, 6));
 322     }
 323 
 324     @Test(groups={"tck"}, expectedExceptions=DateTimeException.class)
 325     public void test_adjust_toMonth() {
 326         ThaiBuddhistDate jdate = ThaiBuddhistChronology.INSTANCE.date(1726, 1, 4);
 327         jdate.with(Month.APRIL);
 328     }
 329 
 330     //-----------------------------------------------------------------------
 331     // LocalDate.with(BuddhistDate)
 332     //-----------------------------------------------------------------------
 333     @Test(groups={"tck"})
 334     public void test_LocalDate_adjustToBuddhistDate() {
 335         ThaiBuddhistDate jdate = ThaiBuddhistChronology.INSTANCE.date(2555, 10, 29);
 336         LocalDate test = LocalDate.MIN.with(jdate);
 337         assertEquals(test, LocalDate.of(2012, 10, 29));
 338     }
 339 
 340     @Test(groups={"tck"})
 341     public void test_LocalDateTime_adjustToBuddhistDate() {
 342         ThaiBuddhistDate jdate = ThaiBuddhistChronology.INSTANCE.date(2555, 10, 29);
 343         LocalDateTime test = LocalDateTime.MIN.with(jdate);
 344         assertEquals(test, LocalDateTime.of(2012, 10, 29, 0, 0));
 345     }
 346 
 347     //-----------------------------------------------------------------------
 348     // toString()
 349     //-----------------------------------------------------------------------
 350     @DataProvider(name="toString")
 351     Object[][] data_toString() {
 352         return new Object[][] {
 353             {ThaiBuddhistChronology.INSTANCE.date(544, 1, 1), "ThaiBuddhist BE 544-01-01"},
 354             {ThaiBuddhistChronology.INSTANCE.date(2271, 10, 28), "ThaiBuddhist BE 2271-10-28"},
 355             {ThaiBuddhistChronology.INSTANCE.date(2271, 10, 29), "ThaiBuddhist BE 2271-10-29"},
 356             {ThaiBuddhistChronology.INSTANCE.date(2270, 12, 5), "ThaiBuddhist BE 2270-12-05"},
 357             {ThaiBuddhistChronology.INSTANCE.date(2270, 12, 6), "ThaiBuddhist BE 2270-12-06"},
 358         };
 359     }
 360 
 361     @Test(dataProvider="toString", groups={"tck"})
 362     public void test_toString(ThaiBuddhistDate jdate, String expected) {
 363         assertEquals(jdate.toString(), expected);
 364     }
 365 
 366     //-----------------------------------------------------------------------
 367     // chronology range(ChronoField)
 368     //-----------------------------------------------------------------------
 369     @Test(groups={"tck"})
 370     public void test_Chrono_range() {
 371         long minYear = LocalDate.MIN.getYear() + YDIFF;
 372         long maxYear = LocalDate.MAX.getYear() + YDIFF;
 373         assertEquals(ThaiBuddhistChronology.INSTANCE.range(YEAR), ValueRange.of(minYear, maxYear));
 374         assertEquals(ThaiBuddhistChronology.INSTANCE.range(YEAR_OF_ERA), ValueRange.of(1, -minYear + 1, maxYear));
 375 
 376         assertEquals(ThaiBuddhistChronology.INSTANCE.range(DAY_OF_MONTH), DAY_OF_MONTH.range());
 377         assertEquals(ThaiBuddhistChronology.INSTANCE.range(DAY_OF_YEAR), DAY_OF_YEAR.range());
 378         assertEquals(ThaiBuddhistChronology.INSTANCE.range(MONTH_OF_YEAR), MONTH_OF_YEAR.range());
 379     }
 380 
 381     //-----------------------------------------------------------------------
 382     // equals()
 383     //-----------------------------------------------------------------------
 384     @Test(groups="tck")
 385     public void test_equals_true() {
 386         assertTrue(ThaiBuddhistChronology.INSTANCE.equals(ThaiBuddhistChronology.INSTANCE));
 387     }
 388 
 389     @Test(groups="tck")
 390     public void test_equals_false() {
 391         assertFalse(ThaiBuddhistChronology.INSTANCE.equals(IsoChronology.INSTANCE));
 392     }
 393 
 394 }