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