test/java/time/tck/java/time/chrono/TestHijrahChronology.java

Print this page




  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 }


  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 }