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.Month;
  67 import java.time.chrono.HijrahChronology;
  68 import java.time.chrono.HijrahDate;
  69 import java.time.chrono.ChronoLocalDate;
  70 import java.time.temporal.Adjusters;
  71 import java.time.chrono.Chronology;
  72 import java.time.chrono.IsoChronology;
  73 
  74 import org.testng.Assert;
  75 import org.testng.annotations.DataProvider;
  76 import org.testng.annotations.Test;
  77 
  78 /**
  79  * Test.
  80  */
  81 @Test
  82 public class TestHijrahChronology {
  83 
  84     //-----------------------------------------------------------------------
  85     // Chronology.ofName("Hijrah")  Lookup by name
  86     //-----------------------------------------------------------------------
  87     @Test(groups={"tck"})
  88     public void test_chrono_byName() {
  89         Chronology c = HijrahChronology.INSTANCE;
  90         Chronology test = Chronology.of("Hijrah");
  91         Assert.assertNotNull(test, "The Hijrah calendar could not be found byName");
  92         Assert.assertEquals(test.getId(), "Hijrah", "ID mismatch");
  93         Assert.assertEquals(test.getCalendarType(), "islamicc", "Type mismatch");
  94         Assert.assertEquals(test, c);
  95     }
  96 
  97     //-----------------------------------------------------------------------
  98     // creation, toLocalDate()
  99     //-----------------------------------------------------------------------
 100     @DataProvider(name="samples")
 101     Object[][] data_samples() {
 102         return new Object[][] {
 103             {HijrahChronology.INSTANCE.date(1, 1, 1), LocalDate.of(622, 7, 19)},
 104             {HijrahChronology.INSTANCE.date(1, 1, 2), LocalDate.of(622, 7, 20)},
 105             {HijrahChronology.INSTANCE.date(1, 1, 3), LocalDate.of(622, 7, 21)},
 106 
 107             {HijrahChronology.INSTANCE.date(2, 1, 1), LocalDate.of(623, 7, 8)},
 108             {HijrahChronology.INSTANCE.date(3, 1, 1), LocalDate.of(624, 6, 27)},
 109             {HijrahChronology.INSTANCE.date(3, 12, 6), LocalDate.of(625, 5, 23)},
 110             {HijrahChronology.INSTANCE.date(4, 1, 1), LocalDate.of(625, 6, 16)},
 111             {HijrahChronology.INSTANCE.date(4, 7, 3), LocalDate.of(625, 12, 12)},
 112             {HijrahChronology.INSTANCE.date(4, 7, 4), LocalDate.of(625, 12, 13)},
 113             {HijrahChronology.INSTANCE.date(5, 1, 1), LocalDate.of(626, 6, 5)},
 114             {HijrahChronology.INSTANCE.date(1662, 3, 3), LocalDate.of(2234, 4, 3)},
 115             {HijrahChronology.INSTANCE.date(1728, 10, 28), LocalDate.of(2298, 12, 03)},
 116             {HijrahChronology.INSTANCE.date(1728, 10, 29), LocalDate.of(2298, 12, 04)},
 117         };
 118     }
 119 
 120     @Test(dataProvider="samples", groups={"tck"})
 121     public void test_toLocalDate(ChronoLocalDate<?> hijrahDate, LocalDate iso) {
 122         assertEquals(LocalDate.from(hijrahDate), iso);
 123     }
 124 
 125     @Test(dataProvider="samples", groups={"tck"})
 126     public void test_fromCalendrical(ChronoLocalDate<?> hijrahDate, LocalDate iso) {
 127         assertEquals(HijrahChronology.INSTANCE.date(iso), hijrahDate);
 128     }
 129 
 130     @DataProvider(name="badDates")
 131     Object[][] data_badDates() {
 132         return new Object[][] {
 133             {1728, 0, 0},
 134 
 135             {1728, -1, 1},
 136             {1728, 0, 1},
 137             {1728, 14, 1},
 138             {1728, 15, 1},
 139 
 140             {1728, 1, -1},
 141             {1728, 1, 0},
 142             {1728, 1, 32},
 143 
 144             {1728, 12, -1},
 145             {1728, 12, 0},
 146             {1728, 12, 32},
 147         };
 148     }
 149 
 150     @Test(dataProvider="badDates", groups={"tck"}, expectedExceptions=DateTimeException.class)
 151     public void test_badDates(int year, int month, int dom) {
 152         HijrahChronology.INSTANCE.date(year, month, dom);
 153     }
 154 
 155     //-----------------------------------------------------------------------
 156     // with(WithAdjuster)
 157     //-----------------------------------------------------------------------
 158     @Test(groups={"tck"})
 159     public void test_adjust1() {
 160         ChronoLocalDate<?> base = HijrahChronology.INSTANCE.date(1728, 10, 28);
 161         ChronoLocalDate<?> test = base.with(Adjusters.lastDayOfMonth());
 162         assertEquals(test, HijrahChronology.INSTANCE.date(1728, 10, 29));
 163     }
 164 
 165     @Test(groups={"tck"})
 166     public void test_adjust2() {
 167         ChronoLocalDate<?> base = HijrahChronology.INSTANCE.date(1728, 12, 2);
 168         ChronoLocalDate<?> test = base.with(Adjusters.lastDayOfMonth());
 169         assertEquals(test, HijrahChronology.INSTANCE.date(1728, 12, 30));
 170     }
 171 
 172     //-----------------------------------------------------------------------
 173     // HijrahDate.with(Local*)
 174     //-----------------------------------------------------------------------
 175     @Test(groups={"tck"})
 176     public void test_adjust_toLocalDate() {
 177         ChronoLocalDate<?> hijrahDate = HijrahChronology.INSTANCE.date(1726, 1, 4);
 178         ChronoLocalDate<?> test = hijrahDate.with(LocalDate.of(2012, 7, 6));
 179         assertEquals(test, HijrahChronology.INSTANCE.date(1433, 8, 16));
 180     }
 181 
 182     @Test(groups={"tck"}, expectedExceptions=DateTimeException.class)
 183     public void test_adjust_toMonth() {
 184         ChronoLocalDate<?> hijrahDate = HijrahChronology.INSTANCE.date(1726, 1, 4);
 185         hijrahDate.with(Month.APRIL);
 186     }
 187 
 188     //-----------------------------------------------------------------------
 189     // LocalDate.with(HijrahDate)
 190     //-----------------------------------------------------------------------
 191     @Test(groups={"tck"})
 192     public void test_LocalDate_adjustToHijrahDate() {
 193         ChronoLocalDate<?> hijrahDate = HijrahChronology.INSTANCE.date(1728, 10, 29);
 194         LocalDate test = LocalDate.MIN.with(hijrahDate);
 195         assertEquals(test, LocalDate.of(2298, 12, 4));
 196     }
 197 
 198     @Test(groups={"tck"})
 199     public void test_LocalDateTime_adjustToHijrahDate() {
 200         ChronoLocalDate<?> hijrahDate = HijrahChronology.INSTANCE.date(1728, 10, 29);
 201         LocalDateTime test = LocalDateTime.MIN.with(hijrahDate);
 202         assertEquals(test, LocalDateTime.of(2298, 12, 4, 0, 0));
 203     }
 204 
 205     //-----------------------------------------------------------------------
 206     // toString()
 207     //-----------------------------------------------------------------------
 208     @DataProvider(name="toString")
 209     Object[][] data_toString() {
 210         return new Object[][] {
 211             {HijrahChronology.INSTANCE.date(1, 1, 1), "Hijrah AH 1-01-01"},
 212             {HijrahChronology.INSTANCE.date(1728, 10, 28), "Hijrah AH 1728-10-28"},
 213             {HijrahChronology.INSTANCE.date(1728, 10, 29), "Hijrah AH 1728-10-29"},
 214             {HijrahChronology.INSTANCE.date(1727, 12, 5), "Hijrah AH 1727-12-05"},
 215             {HijrahChronology.INSTANCE.date(1727, 12, 6), "Hijrah AH 1727-12-06"},
 216         };
 217     }
 218 
 219     @Test(dataProvider="toString", groups={"tck"})
 220     public void test_toString(ChronoLocalDate<?> hijrahDate, String expected) {
 221         assertEquals(hijrahDate.toString(), expected);
 222     }
 223 
 224     //-----------------------------------------------------------------------
 225     // equals()
 226     //-----------------------------------------------------------------------
 227     @Test(groups="tck")
 228     public void test_equals_true() {
 229         assertTrue(HijrahChronology.INSTANCE.equals(HijrahChronology.INSTANCE));
 230     }
 231 
 232     @Test(groups="tck")
 233     public void test_equals_false() {
 234         assertFalse(HijrahChronology.INSTANCE.equals(IsoChronology.INSTANCE));
 235     }
 236 
 237 }