1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp.chrono; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertFalse; 36 import static org.testng.Assert.assertTrue; 37 38 import static org.threeten.bp.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; 39 import static org.threeten.bp.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; 40 41 import org.testng.Assert; 42 import org.testng.annotations.DataProvider; 43 import org.testng.annotations.Test; 44 import org.threeten.bp.DateTimeException; 45 import org.threeten.bp.LocalDate; 46 import org.threeten.bp.LocalDateTime; 47 import org.threeten.bp.Month; 48 import org.threeten.bp.chrono.HijrahChronology; 49 import org.threeten.bp.temporal.TemporalAdjusters; 50 51 /** 52 * Test. 53 */ 54 @Test 55 public class TestHijrahChronology { 56 57 //----------------------------------------------------------------------- 58 // Chrono.ofName("Hijrah") Lookup by name 59 //----------------------------------------------------------------------- 60 @Test test_chrono_byName()61 public void test_chrono_byName() { 62 Chronology c = HijrahChronology.INSTANCE; 63 Chronology test = Chronology.of("Hijrah"); 64 Assert.assertNotNull(test, "The Hijrah calendar could not be found byName"); 65 Assert.assertEquals(test.getId(), "Hijrah-umalqura", "ID mismatch"); 66 Assert.assertEquals(test.getCalendarType(), "islamic-umalqura", "Type mismatch"); 67 Assert.assertEquals(test, c); 68 } 69 70 //----------------------------------------------------------------------- 71 // creation, toLocalDate() 72 //----------------------------------------------------------------------- 73 @DataProvider(name="samples") data_samples()74 Object[][] data_samples() { 75 return new Object[][] { 76 {HijrahChronology.INSTANCE.date(1, 1, 1), LocalDate.of(622, 7, 19)}, 77 {HijrahChronology.INSTANCE.date(1, 1, 2), LocalDate.of(622, 7, 20)}, 78 {HijrahChronology.INSTANCE.date(1, 1, 3), LocalDate.of(622, 7, 21)}, 79 80 {HijrahChronology.INSTANCE.date(2, 1, 1), LocalDate.of(623, 7, 8)}, 81 {HijrahChronology.INSTANCE.date(3, 1, 1), LocalDate.of(624, 6, 27)}, 82 {HijrahChronology.INSTANCE.date(3, 12, 6), LocalDate.of(625, 5, 23)}, 83 {HijrahChronology.INSTANCE.date(4, 1, 1), LocalDate.of(625, 6, 16)}, 84 {HijrahChronology.INSTANCE.date(4, 7, 3), LocalDate.of(625, 12, 12)}, 85 {HijrahChronology.INSTANCE.date(4, 7, 4), LocalDate.of(625, 12, 13)}, 86 {HijrahChronology.INSTANCE.date(5, 1, 1), LocalDate.of(626, 6, 5)}, 87 {HijrahChronology.INSTANCE.date(1662, 3, 3), LocalDate.of(2234, 4, 3)}, 88 {HijrahChronology.INSTANCE.date(1728, 10, 28), LocalDate.of(2298, 12, 03)}, 89 {HijrahChronology.INSTANCE.date(1728, 10, 29), LocalDate.of(2298, 12, 04)}, 90 }; 91 } 92 93 @Test(dataProvider="samples") test_toLocalDate(ChronoLocalDate hijrahDate, LocalDate iso)94 public void test_toLocalDate(ChronoLocalDate hijrahDate, LocalDate iso) { 95 assertEquals(LocalDate.from(hijrahDate), iso); 96 } 97 98 @Test(dataProvider="samples") test_fromCalendrical(ChronoLocalDate hijrahDate, LocalDate iso)99 public void test_fromCalendrical(ChronoLocalDate hijrahDate, LocalDate iso) { 100 assertEquals(HijrahChronology.INSTANCE.date(iso), hijrahDate); 101 } 102 103 @DataProvider(name="badDates") data_badDates()104 Object[][] data_badDates() { 105 return new Object[][] { 106 {1728, 0, 0}, 107 108 {1728, -1, 1}, 109 {1728, 0, 1}, 110 {1728, 14, 1}, 111 {1728, 15, 1}, 112 113 {1728, 1, -1}, 114 {1728, 1, 0}, 115 {1728, 1, 32}, 116 117 {1728, 12, -1}, 118 {1728, 12, 0}, 119 {1728, 12, 32}, 120 }; 121 } 122 123 @Test(dataProvider="badDates", expectedExceptions=DateTimeException.class) test_badDates(int year, int month, int dom)124 public void test_badDates(int year, int month, int dom) { 125 HijrahChronology.INSTANCE.date(year, month, dom); 126 } 127 128 //----------------------------------------------------------------------- 129 // getLong(field) 130 //----------------------------------------------------------------------- 131 @Test test_alignedDayOfWeekInMonth()132 public void test_alignedDayOfWeekInMonth() { 133 for (int dom = 1; dom <= 29; dom++) { 134 HijrahDate date = HijrahChronology.INSTANCE.date(1728, 10, dom); 135 assertEquals(date.getLong(ALIGNED_WEEK_OF_MONTH), ((dom - 1) / 7) + 1); 136 assertEquals(date.getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH), ((dom - 1) % 7) + 1); 137 date = date.plusDays(1); 138 } 139 } 140 141 //----------------------------------------------------------------------- 142 // with(WithAdjuster) 143 //----------------------------------------------------------------------- 144 @Test test_adjust1()145 public void test_adjust1() { 146 ChronoLocalDate base = HijrahChronology.INSTANCE.date(1728, 10, 28); 147 ChronoLocalDate test = base.with(TemporalAdjusters.lastDayOfMonth()); 148 assertEquals(test, HijrahChronology.INSTANCE.date(1728, 10, 29)); 149 } 150 151 @Test test_adjust2()152 public void test_adjust2() { 153 ChronoLocalDate base = HijrahChronology.INSTANCE.date(1728, 12, 2); 154 ChronoLocalDate test = base.with(TemporalAdjusters.lastDayOfMonth()); 155 assertEquals(test, HijrahChronology.INSTANCE.date(1728, 12, 30)); 156 } 157 158 //----------------------------------------------------------------------- 159 // HijrahDate.with(Local*) 160 //----------------------------------------------------------------------- 161 @Test test_adjust_toLocalDate()162 public void test_adjust_toLocalDate() { 163 ChronoLocalDate hijrahDate = HijrahChronology.INSTANCE.date(1726, 1, 4); 164 ChronoLocalDate test = hijrahDate.with(LocalDate.of(2012, 7, 6)); 165 assertEquals(test, HijrahChronology.INSTANCE.date(1433, 8, 16)); 166 } 167 168 @Test(expectedExceptions=DateTimeException.class) test_adjust_toMonth()169 public void test_adjust_toMonth() { 170 ChronoLocalDate hijrahDate = HijrahChronology.INSTANCE.date(1726, 1, 4); 171 hijrahDate.with(Month.APRIL); 172 } 173 174 //----------------------------------------------------------------------- 175 // LocalDate.with(HijrahDate) 176 //----------------------------------------------------------------------- 177 @Test test_LocalDate_adjustToHijrahDate()178 public void test_LocalDate_adjustToHijrahDate() { 179 ChronoLocalDate hijrahDate = HijrahChronology.INSTANCE.date(1728, 10, 29); 180 LocalDate test = LocalDate.MIN.with(hijrahDate); 181 assertEquals(test, LocalDate.of(2298, 12, 4)); 182 } 183 184 @Test test_LocalDateTime_adjustToHijrahDate()185 public void test_LocalDateTime_adjustToHijrahDate() { 186 ChronoLocalDate hijrahDate = HijrahChronology.INSTANCE.date(1728, 10, 29); 187 LocalDateTime test = LocalDateTime.MIN.with(hijrahDate); 188 assertEquals(test, LocalDateTime.of(2298, 12, 4, 0, 0)); 189 } 190 191 //----------------------------------------------------------------------- 192 // toString() 193 //----------------------------------------------------------------------- 194 @DataProvider(name="toString") data_toString()195 Object[][] data_toString() { 196 return new Object[][] { 197 {HijrahChronology.INSTANCE.date(1, 1, 1), "Hijrah-umalqura AH 1-01-01"}, 198 {HijrahChronology.INSTANCE.date(1728, 10, 28), "Hijrah-umalqura AH 1728-10-28"}, 199 {HijrahChronology.INSTANCE.date(1728, 10, 29), "Hijrah-umalqura AH 1728-10-29"}, 200 {HijrahChronology.INSTANCE.date(1727, 12, 5), "Hijrah-umalqura AH 1727-12-05"}, 201 {HijrahChronology.INSTANCE.date(1727, 12, 6), "Hijrah-umalqura AH 1727-12-06"}, 202 }; 203 } 204 205 @Test(dataProvider="toString") test_toString(ChronoLocalDate hijrahDate, String expected)206 public void test_toString(ChronoLocalDate hijrahDate, String expected) { 207 assertEquals(hijrahDate.toString(), expected); 208 } 209 210 //----------------------------------------------------------------------- 211 // equals() 212 //----------------------------------------------------------------------- 213 @Test test_equals_true()214 public void test_equals_true() { 215 assertTrue(HijrahChronology.INSTANCE.equals(HijrahChronology.INSTANCE)); 216 } 217 218 @Test test_equals_false()219 public void test_equals_false() { 220 assertFalse(HijrahChronology.INSTANCE.equals(IsoChronology.INSTANCE)); 221 } 222 223 } 224