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.assertNotNull; 37 import static org.testng.Assert.assertTrue; 38 import static org.threeten.bp.temporal.ChronoField.ERA; 39 import static org.threeten.bp.temporal.ChronoField.YEAR; 40 import static org.threeten.bp.temporal.ChronoField.YEAR_OF_ERA; 41 42 import org.testng.Assert; 43 import org.testng.annotations.DataProvider; 44 import org.testng.annotations.Test; 45 import org.threeten.bp.DateTimeException; 46 import org.threeten.bp.LocalDate; 47 import org.threeten.bp.LocalDateTime; 48 import org.threeten.bp.Month; 49 import org.threeten.bp.chrono.Chronology; 50 import org.threeten.bp.chrono.ChronoLocalDate; 51 import org.threeten.bp.chrono.Era; 52 import org.threeten.bp.chrono.HijrahChronology; 53 import org.threeten.bp.chrono.IsoChronology; 54 import org.threeten.bp.temporal.ChronoField; 55 import org.threeten.bp.temporal.TemporalAdjusters; 56 57 /** 58 * Test. 59 */ 60 @Test 61 public class TestIsoChronology { 62 63 //----------------------------------------------------------------------- 64 // Chrono.ofName("ISO") Lookup by name 65 //----------------------------------------------------------------------- 66 @Test test_chrono_byName()67 public void test_chrono_byName() { 68 Chronology c = IsoChronology.INSTANCE; 69 Chronology test = Chronology.of("ISO"); 70 Assert.assertNotNull(test, "The ISO calendar could not be found byName"); 71 Assert.assertEquals(test.getId(), "ISO", "ID mismatch"); 72 Assert.assertEquals(test.getCalendarType(), "iso8601", "Type mismatch"); 73 Assert.assertEquals(test, c); 74 } 75 76 //----------------------------------------------------------------------- 77 // Lookup by Singleton 78 //----------------------------------------------------------------------- 79 @Test instanceNotNull()80 public void instanceNotNull() { 81 assertNotNull(IsoChronology.INSTANCE); 82 } 83 84 //----------------------------------------------------------------------- 85 // Era creation 86 //----------------------------------------------------------------------- 87 @Test test_eraOf()88 public void test_eraOf() { 89 assertEquals(IsoChronology.INSTANCE.eraOf(0), IsoEra.BCE); 90 assertEquals(IsoChronology.INSTANCE.eraOf(1), IsoEra.CE); 91 } 92 93 //----------------------------------------------------------------------- 94 // creation, toLocalDate() 95 //----------------------------------------------------------------------- 96 @DataProvider(name="samples") data_samples()97 Object[][] data_samples() { 98 return new Object[][] { 99 {IsoChronology.INSTANCE.date(1, 7, 8), LocalDate.of(1, 7, 8)}, 100 {IsoChronology.INSTANCE.date(1, 7, 20), LocalDate.of(1, 7, 20)}, 101 {IsoChronology.INSTANCE.date(1, 7, 21), LocalDate.of(1, 7, 21)}, 102 103 {IsoChronology.INSTANCE.date(2, 7, 8), LocalDate.of(2, 7, 8)}, 104 {IsoChronology.INSTANCE.date(3, 6, 27), LocalDate.of(3, 6, 27)}, 105 {IsoChronology.INSTANCE.date(3, 5, 23), LocalDate.of(3, 5, 23)}, 106 {IsoChronology.INSTANCE.date(4, 6, 16), LocalDate.of(4, 6, 16)}, 107 {IsoChronology.INSTANCE.date(4, 7, 3), LocalDate.of(4, 7, 3)}, 108 {IsoChronology.INSTANCE.date(4, 7, 4), LocalDate.of(4, 7, 4)}, 109 {IsoChronology.INSTANCE.date(5, 1, 1), LocalDate.of(5, 1, 1)}, 110 {IsoChronology.INSTANCE.date(1727, 3, 3), LocalDate.of(1727, 3, 3)}, 111 {IsoChronology.INSTANCE.date(1728, 10, 28), LocalDate.of(1728, 10, 28)}, 112 {IsoChronology.INSTANCE.date(2012, 10, 29), LocalDate.of(2012, 10, 29)}, 113 }; 114 } 115 116 @Test(dataProvider="samples") test_toLocalDate(ChronoLocalDate isoDate, LocalDate iso)117 public void test_toLocalDate(ChronoLocalDate isoDate, LocalDate iso) { 118 assertEquals(LocalDate.from(isoDate), iso); 119 } 120 121 @Test(dataProvider="samples") test_fromCalendrical(ChronoLocalDate isoDate, LocalDate iso)122 public void test_fromCalendrical(ChronoLocalDate isoDate, LocalDate iso) { 123 assertEquals(IsoChronology.INSTANCE.date(iso), isoDate); 124 } 125 126 @DataProvider(name="badDates") data_badDates()127 Object[][] data_badDates() { 128 return new Object[][] { 129 {2012, 0, 0}, 130 131 {2012, -1, 1}, 132 {2012, 0, 1}, 133 {2012, 14, 1}, 134 {2012, 15, 1}, 135 136 {2012, 1, -1}, 137 {2012, 1, 0}, 138 {2012, 1, 32}, 139 140 {2012, 12, -1}, 141 {2012, 12, 0}, 142 {2012, 12, 32}, 143 }; 144 } 145 146 @Test(dataProvider="badDates", expectedExceptions=DateTimeException.class) test_badDates(int year, int month, int dom)147 public void test_badDates(int year, int month, int dom) { 148 IsoChronology.INSTANCE.date(year, month, dom); 149 } 150 151 @Test test_date_withEra()152 public void test_date_withEra() { 153 int year = 5; 154 int month = 5; 155 int dayOfMonth = 5; 156 ChronoLocalDate test = IsoChronology.INSTANCE.date(IsoEra.BCE, year, month, dayOfMonth); 157 assertEquals(test.getEra(), IsoEra.BCE); 158 assertEquals(test.get(ChronoField.YEAR_OF_ERA), year); 159 assertEquals(test.get(ChronoField.MONTH_OF_YEAR), month); 160 assertEquals(test.get(ChronoField.DAY_OF_MONTH), dayOfMonth); 161 162 assertEquals(test.get(YEAR), 1 + (-1 * year)); 163 assertEquals(test.get(ERA), 0); 164 assertEquals(test.get(YEAR_OF_ERA), year); 165 } 166 167 @Test(expectedExceptions=ClassCastException.class) test_date_withEra_withWrongEra()168 public void test_date_withEra_withWrongEra() { 169 IsoChronology.INSTANCE.date((Era) HijrahEra.AH, 1, 1, 1); 170 } 171 172 //----------------------------------------------------------------------- 173 // with(DateTimeAdjuster) 174 //----------------------------------------------------------------------- 175 @Test test_adjust1()176 public void test_adjust1() { 177 ChronoLocalDate base = IsoChronology.INSTANCE.date(1728, 10, 28); 178 ChronoLocalDate test = base.with(TemporalAdjusters.lastDayOfMonth()); 179 assertEquals(test, IsoChronology.INSTANCE.date(1728, 10, 31)); 180 } 181 182 @Test test_adjust2()183 public void test_adjust2() { 184 ChronoLocalDate base = IsoChronology.INSTANCE.date(1728, 12, 2); 185 ChronoLocalDate test = base.with(TemporalAdjusters.lastDayOfMonth()); 186 assertEquals(test, IsoChronology.INSTANCE.date(1728, 12, 31)); 187 } 188 189 //----------------------------------------------------------------------- 190 // ISODate.with(Local*) 191 //----------------------------------------------------------------------- 192 @Test test_adjust_toLocalDate()193 public void test_adjust_toLocalDate() { 194 ChronoLocalDate isoDate = IsoChronology.INSTANCE.date(1726, 1, 4); 195 ChronoLocalDate test = isoDate.with(LocalDate.of(2012, 7, 6)); 196 assertEquals(test, IsoChronology.INSTANCE.date(2012, 7, 6)); 197 } 198 199 @Test test_adjust_toMonth()200 public void test_adjust_toMonth() { 201 ChronoLocalDate isoDate = IsoChronology.INSTANCE.date(1726, 1, 4); 202 assertEquals(IsoChronology.INSTANCE.date(1726, 4, 4), isoDate.with(Month.APRIL)); 203 } 204 205 //----------------------------------------------------------------------- 206 // LocalDate.with(ISODate) 207 //----------------------------------------------------------------------- 208 @Test test_LocalDate_adjustToISODate()209 public void test_LocalDate_adjustToISODate() { 210 ChronoLocalDate isoDate = IsoChronology.INSTANCE.date(1728, 10, 29); 211 LocalDate test = LocalDate.MIN.with(isoDate); 212 assertEquals(test, LocalDate.of(1728, 10, 29)); 213 } 214 215 @Test test_LocalDateTime_adjustToISODate()216 public void test_LocalDateTime_adjustToISODate() { 217 ChronoLocalDate isoDate = IsoChronology.INSTANCE.date(1728, 10, 29); 218 LocalDateTime test = LocalDateTime.MIN.with(isoDate); 219 assertEquals(test, LocalDateTime.of(1728, 10, 29, 0, 0)); 220 } 221 222 //----------------------------------------------------------------------- 223 // isLeapYear() 224 //----------------------------------------------------------------------- 225 @DataProvider(name="leapYears") leapYearInformation()226 Object[][] leapYearInformation() { 227 return new Object[][] { 228 {2000, true}, 229 {1996, true}, 230 {1600, true}, 231 232 {1900, false}, 233 {2100, false}, 234 235 {-500, false}, 236 {-400, true}, 237 {-300, false}, 238 {-100, false}, 239 {-5, false}, 240 {-4, true}, 241 {-3, false}, 242 {-2, false}, 243 {-1, false}, 244 {0, true}, 245 {1, false}, 246 {3, false}, 247 {4, true}, 248 {5, false}, 249 {100, false}, 250 {300, false}, 251 {400, true}, 252 {500, false}, 253 }; 254 } 255 256 @Test(dataProvider="leapYears") test_isLeapYear(int year, boolean isLeapYear)257 public void test_isLeapYear(int year, boolean isLeapYear) { 258 assertEquals(IsoChronology.INSTANCE.isLeapYear(year), isLeapYear); 259 } 260 261 //----------------------------------------------------------------------- 262 // toString() 263 //----------------------------------------------------------------------- 264 @Test test_now()265 public void test_now() { 266 assertEquals(LocalDate.from(IsoChronology.INSTANCE.dateNow()), LocalDate.now()); 267 } 268 269 //----------------------------------------------------------------------- 270 // toString() 271 //----------------------------------------------------------------------- 272 @DataProvider(name="toString") data_toString()273 Object[][] data_toString() { 274 return new Object[][] { 275 {IsoChronology.INSTANCE.date(1, 1, 1), "0001-01-01"}, 276 {IsoChronology.INSTANCE.date(1728, 10, 28), "1728-10-28"}, 277 {IsoChronology.INSTANCE.date(1728, 10, 29), "1728-10-29"}, 278 {IsoChronology.INSTANCE.date(1727, 12, 5), "1727-12-05"}, 279 {IsoChronology.INSTANCE.date(1727, 12, 6), "1727-12-06"}, 280 }; 281 } 282 283 @Test(dataProvider="toString") test_toString(ChronoLocalDate isoDate, String expected)284 public void test_toString(ChronoLocalDate isoDate, String expected) { 285 assertEquals(isoDate.toString(), expected); 286 } 287 288 //----------------------------------------------------------------------- 289 // equals() 290 //----------------------------------------------------------------------- 291 @Test test_equals_true()292 public void test_equals_true() { 293 assertTrue(IsoChronology.INSTANCE.equals(IsoChronology.INSTANCE)); 294 } 295 296 @Test test_equals_false()297 public void test_equals_false() { 298 assertFalse(IsoChronology.INSTANCE.equals(HijrahChronology.INSTANCE)); 299 } 300 301 } 302