1 /* 2 * Copyright (c) 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 package test.java.time.chrono; 27 28 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 29 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 30 import static java.time.temporal.ChronoField.YEAR; 31 import static org.testng.Assert.assertEquals; 32 import static org.testng.Assert.fail; 33 34 import java.time.DateTimeException; 35 import java.time.LocalDate; 36 import java.time.chrono.Chronology; 37 import java.time.chrono.HijrahChronology; 38 import java.time.chrono.HijrahDate; 39 import java.time.temporal.ChronoField; 40 import java.time.temporal.ChronoUnit; 41 import java.time.temporal.ValueRange; 42 43 import org.testng.annotations.DataProvider; 44 import org.testng.annotations.Test; 45 46 /** 47 * Tests for the Umm alQura chronology and data 48 */ 49 @Test 50 public class TestUmmAlQuraChronology { 51 52 @Test 53 public void test_aliases() { 54 HijrahChronology hc = (HijrahChronology) Chronology.of("Hijrah"); 55 assertEquals(hc, HijrahChronology.INSTANCE, "Alias for Hijrah-umalqura"); 56 hc = (HijrahChronology) Chronology.of("islamic"); 57 assertEquals(hc, HijrahChronology.INSTANCE, "Alias for Hijrah-umalqura"); 58 } 59 60 //----------------------------------------------------------------------- 61 // regular data factory for Umm alQura dates and the corresponding ISO dates 62 //----------------------------------------------------------------------- 63 @DataProvider(name = "UmmalQuraVsISODates") 64 Object[][] data_of_ummalqura() { 65 return new Object[][]{ 66 67 //{1318, 01, 01, 1900, 04, 30}, 68 //{1318, 01, 02, 1900, 05, 01}, 69 70 //{1318, 12, 29, 1901, 04, 18}, 71 //{1319, 01, 01, 1901, 04, 19}, 72 73 //{1433, 12, 29, 2012, 11, 14}, 74 //{1434, 01, 01, 2012, 11, 15}, 75 76 {1434, 02, 18, 2012, 12, 31}, 77 {1434, 02, 19, 2013, 01, 01}, 78 79 //{1502, 12, 30, 2079, 10, 25}, 80 // not in Umm alQura data {1503, 01, 01, 2079, 10, 26}, 81 82 // not in Umm alQura data {1503, 06, 28, 2080, 04, 18}, 83 // not in Umm alQura data ~/ws/Downloads 84 }; 85 } 86 87 @Test(dataProvider="UmmalQuraVsISODates") 88 public void Test_UmmAlQuraDatesVsISO(int h_year, int h_month, int h_day, int iso_year, int iso_month, int iso_day) { 89 HijrahDate hd = HijrahDate.of(h_year, h_month, h_day); 90 LocalDate ld = LocalDate.of(iso_year, iso_month, iso_day); 91 assertEquals(hd.toEpochDay(), ld.toEpochDay(), "Umm alQura date and ISO date should have same epochDay"); 92 } 93 94 95 @Test 96 public void Test_UmmAlQuraChronoRange() { 97 HijrahChronology chrono = HijrahChronology.INSTANCE; 98 ValueRange year = chrono.range(YEAR); 99 assertEquals(year.getMinimum(), 1432, "Minimum year"); 100 assertEquals(year.getLargestMinimum(), 1432, "Largest minimum year"); 101 assertEquals(year.getMaximum(), 1435, "Largest year"); 102 assertEquals(year.getSmallestMaximum(), 1435, "Smallest Maximum year"); 103 104 ValueRange month = chrono.range(MONTH_OF_YEAR); 105 assertEquals(month.getMinimum(), 1, "Minimum month"); 106 assertEquals(month.getLargestMinimum(), 1, "Largest minimum month"); 107 assertEquals(month.getMaximum(), 12, "Largest month"); 108 assertEquals(month.getSmallestMaximum(), 12, "Smallest Maximum month"); 109 110 ValueRange day = chrono.range(DAY_OF_MONTH); 111 assertEquals(day.getMinimum(), 1, "Minimum day"); 112 assertEquals(day.getLargestMinimum(), 1, "Largest minimum day"); 113 assertEquals(day.getMaximum(), 30, "Largest day"); 114 assertEquals(day.getSmallestMaximum(), 29, "Smallest Maximum day"); 115 } 116 117 //----------------------------------------------------------------------- 118 // regular data factory for dates and the corresponding range values 119 //----------------------------------------------------------------------- 120 @DataProvider(name = "dates") 121 Object[][] data_of_calendars() { 122 return new Object[][]{ 123 {HijrahDate.of(1434, 5, 1), 1432, 1435, 1, 12, 1, 29, 30}, 124 {HijrahDate.of(1434, 6, 1), 1432, 1435, 1, 12, 1, 30, 30}, 125 }; 126 } 127 128 @Test(dataProvider="dates") 129 public void Test_UmmAlQuraRanges(HijrahDate date, 130 int minYear, int maxYear, 131 int minMonth, int maxMonth, 132 int minDay, int maxDay, int maxChronoDay) { 133 // Check the chronology ranges 134 HijrahChronology chrono = date.getChronology(); 135 ValueRange yearRange = chrono.range(YEAR); 136 assertEquals(yearRange.getMinimum(), minYear, "Minimum year for Hijrah chronology"); 137 assertEquals(yearRange.getLargestMinimum(), minYear, "Largest minimum year for Hijrah chronology"); 138 assertEquals(yearRange.getMaximum(), maxYear, "Maximum year for Hijrah chronology"); 139 assertEquals(yearRange.getSmallestMaximum(), maxYear, "Smallest Maximum year for Hijrah chronology"); 140 141 ValueRange monthRange = chrono.range(MONTH_OF_YEAR); 142 assertEquals(monthRange.getMinimum(), minMonth, "Minimum month for Hijrah chronology"); 143 assertEquals(monthRange.getMaximum(), maxMonth, "Maximum month for Hijrah chronology"); 144 145 ValueRange daysRange = chrono.range(DAY_OF_MONTH); 146 assertEquals(daysRange.getMinimum(), minDay, "Minimum day for chronology"); 147 assertEquals(daysRange.getMaximum(), maxChronoDay, "Maximum day for Hijrah chronology"); 148 149 // Check the date ranges 150 yearRange = date.range(YEAR); 151 assertEquals(yearRange.getMinimum(), minYear, "Minimum year for Hijrah date"); 152 assertEquals(yearRange.getLargestMinimum(), minYear, "Largest minimum year for Hijrah date"); 153 assertEquals(yearRange.getMaximum(), maxYear, "Maximum year for Hijrah date"); 154 assertEquals(yearRange.getSmallestMaximum(), maxYear, "Smallest maximum year for Hijrah date"); 155 156 monthRange = date.range(MONTH_OF_YEAR); 157 assertEquals(monthRange.getMinimum(), minMonth, "Minimum month for HijrahDate"); 158 assertEquals(monthRange.getMaximum(), maxMonth, "Maximum month for HijrahDate"); 159 160 daysRange = date.range(DAY_OF_MONTH); 161 assertEquals(daysRange.getMinimum(), minDay, "Minimum day for HijrahDate"); 162 assertEquals(daysRange.getMaximum(), maxDay, "Maximum day for HijrahDate"); 163 164 } 165 166 @Test 167 public void test_hijrahDateLimits() { 168 HijrahChronology chrono = HijrahChronology.INSTANCE; 169 ValueRange yearRange = chrono.range(YEAR); 170 ValueRange monthRange = chrono.range(MONTH_OF_YEAR); 171 ValueRange dayRange = chrono.range(DAY_OF_MONTH); 172 173 HijrahDate xx = chrono.date(1434, 1, 1); 174 HijrahDate minDate = chrono.date((int)yearRange.getLargestMinimum(), 175 (int)monthRange.getMinimum(), (int)dayRange.getMinimum()); 176 try { 177 HijrahDate before = minDate.minus(1, ChronoUnit.DAYS); 178 fail("Exception did not occur, minDate: " + minDate + ".minus(1, DAYS) = " + before); 179 180 } catch (DateTimeException ex) { 181 // ignore, this exception was expected 182 } 183 184 HijrahDate maxDate = chrono.date((int)yearRange.getSmallestMaximum(), 185 (int)monthRange.getMaximum(), 1); 186 int monthLen = maxDate.lengthOfMonth(); 187 maxDate = maxDate.with(DAY_OF_MONTH, monthLen); 188 try { 189 HijrahDate after = maxDate.plus(1, ChronoUnit.DAYS); 190 fail("Exception did not occur, maxDate: " + maxDate + ".plus(1, DAYS) = " + after); 191 } catch (DateTimeException ex) { 192 // ignore, this exception was expected 193 } 194 } 195 196 @DataProvider(name="badDates") 197 Object[][] data_badDates() { 198 return new Object[][] { 199 {1317, 12, 29}, 200 {1317, 12, 30}, 201 202 {1320, 1, 29 + 1}, 203 {1320, 2, 30 + 1}, 204 {1320, 3, 29 + 1}, 205 {1320, 4, 29 + 1}, 206 {1320, 5, 30 + 1}, 207 {1320, 6, 29 + 1}, 208 {1320, 7, 30 + 1}, 209 {1320, 8, 30 + 1}, 210 {1320, 9, 29 + 1}, 211 {1320, 10, 30 + 1}, 212 {1320, 11, 30 + 1}, 213 {1320, 12, 30 + 1}, 214 }; 215 } 216 217 @Test(dataProvider="badDates", expectedExceptions=DateTimeException.class) 218 public void test_badDates(int year, int month, int dom) { 219 HijrahChronology.INSTANCE.date(year, month, dom); 220 } 221 222 void printRange(ValueRange range, Object obj, ChronoField field) { 223 System.err.printf(" range: min: %d, max: %d; of: %s, field: %s%n", range.getMinimum(), range.getMaximum(), obj.toString(), field.toString()); 224 } 225 }