1 16 package org.joda.time; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.util.Arrays ; 23 import java.util.Date ; 24 import java.util.Locale ; 25 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import org.joda.time.chrono.BuddhistChronology; 30 import org.joda.time.chrono.CopticChronology; 31 import org.joda.time.chrono.GJChronology; 32 import org.joda.time.chrono.GregorianChronology; 33 import org.joda.time.chrono.ISOChronology; 34 import org.joda.time.chrono.LenientChronology; 35 import org.joda.time.chrono.StrictChronology; 36 import org.joda.time.format.DateTimeFormat; 37 import org.joda.time.format.DateTimeFormatter; 38 39 44 public class TestLocalDate_Basics extends TestCase { 45 46 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 47 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 48 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 49 private static final int OFFSET = 1; 50 private static final GJChronology GJ_UTC = GJChronology.getInstanceUTC(); 51 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS); 52 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON); 53 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO); 54 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 55 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 56 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON); 57 private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO); 58 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 59 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS); 60 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON); 61 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO); 62 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC(); 63 64 private long TEST_TIME_NOW = 65 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 66 67 private long TEST_TIME1 = 68 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 69 + 12L * DateTimeConstants.MILLIS_PER_HOUR 70 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 71 72 private long TEST_TIME2 = 73 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 74 + 14L * DateTimeConstants.MILLIS_PER_HOUR 75 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 76 77 private DateTimeZone zone = null; 78 79 public static void main(String [] args) { 80 junit.textui.TestRunner.run(suite()); 81 } 82 83 public static TestSuite suite() { 84 return new TestSuite(TestLocalDate_Basics.class); 85 } 86 87 public TestLocalDate_Basics(String name) { 88 super(name); 89 } 90 91 protected void setUp() throws Exception { 92 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 93 zone = DateTimeZone.getDefault(); 94 DateTimeZone.setDefault(LONDON); 95 } 96 97 protected void tearDown() throws Exception { 98 DateTimeUtils.setCurrentMillisSystem(); 99 DateTimeZone.setDefault(zone); 100 zone = null; 101 } 102 103 public void testGet_DateTimeFieldType() { 105 LocalDate test = new LocalDate(); 106 assertEquals(1970, test.get(DateTimeFieldType.year())); 107 assertEquals(6, test.get(DateTimeFieldType.monthOfYear())); 108 assertEquals(9, test.get(DateTimeFieldType.dayOfMonth())); 109 assertEquals(2, test.get(DateTimeFieldType.dayOfWeek())); 110 assertEquals(160, test.get(DateTimeFieldType.dayOfYear())); 111 assertEquals(24, test.get(DateTimeFieldType.weekOfWeekyear())); 112 assertEquals(1970, test.get(DateTimeFieldType.weekyear())); 113 try { 114 test.get(null); 115 fail(); 116 } catch (IllegalArgumentException ex) {} 117 try { 118 test.get(DateTimeFieldType.hourOfDay()); 119 fail(); 120 } catch (IllegalArgumentException ex) {} 121 } 122 123 public void testSize() { 124 LocalDate test = new LocalDate(); 125 assertEquals(3, test.size()); 126 } 127 128 public void testGetFieldType_int() { 129 LocalDate test = new LocalDate(COPTIC_PARIS); 130 assertSame(DateTimeFieldType.year(), test.getFieldType(0)); 131 assertSame(DateTimeFieldType.monthOfYear(), test.getFieldType(1)); 132 assertSame(DateTimeFieldType.dayOfMonth(), test.getFieldType(2)); 133 try { 134 test.getFieldType(-1); 135 } catch (IndexOutOfBoundsException ex) {} 136 try { 137 test.getFieldType(3); 138 } catch (IndexOutOfBoundsException ex) {} 139 } 140 141 public void testGetFieldTypes() { 142 LocalDate test = new LocalDate(COPTIC_PARIS); 143 DateTimeFieldType[] fields = test.getFieldTypes(); 144 assertSame(DateTimeFieldType.year(), fields[0]); 145 assertSame(DateTimeFieldType.monthOfYear(), fields[1]); 146 assertSame(DateTimeFieldType.dayOfMonth(), fields[2]); 147 assertNotSame(test.getFieldTypes(), test.getFieldTypes()); 148 } 149 150 public void testGetField_int() { 151 LocalDate test = new LocalDate(COPTIC_PARIS); 152 assertSame(COPTIC_UTC.year(), test.getField(0)); 153 assertSame(COPTIC_UTC.monthOfYear(), test.getField(1)); 154 assertSame(COPTIC_UTC.dayOfMonth(), test.getField(2)); 155 try { 156 test.getField(-1); 157 } catch (IndexOutOfBoundsException ex) {} 158 try { 159 test.getField(3); 160 } catch (IndexOutOfBoundsException ex) {} 161 } 162 163 public void testGetFields() { 164 LocalDate test = new LocalDate(COPTIC_PARIS); 165 DateTimeField[] fields = test.getFields(); 166 assertSame(COPTIC_UTC.year(), fields[0]); 167 assertSame(COPTIC_UTC.monthOfYear(), fields[1]); 168 assertSame(COPTIC_UTC.dayOfMonth(), fields[2]); 169 assertNotSame(test.getFields(), test.getFields()); 170 } 171 172 public void testGetValue_int() { 173 LocalDate test = new LocalDate(); 174 assertEquals(1970, test.getValue(0)); 175 assertEquals(6, test.getValue(1)); 176 assertEquals(9, test.getValue(2)); 177 try { 178 test.getValue(-1); 179 } catch (IndexOutOfBoundsException ex) {} 180 try { 181 test.getValue(3); 182 } catch (IndexOutOfBoundsException ex) {} 183 } 184 185 public void testGetValues() { 186 LocalDate test = new LocalDate(); 187 int[] values = test.getValues(); 188 assertEquals(1970, values[0]); 189 assertEquals(6, values[1]); 190 assertEquals(9, values[2]); 191 assertNotSame(test.getValues(), test.getValues()); 192 } 193 194 public void testIsSupported_DateTimeFieldType() { 195 LocalDate test = new LocalDate(COPTIC_PARIS); 196 assertEquals(true, test.isSupported(DateTimeFieldType.year())); 197 assertEquals(true, test.isSupported(DateTimeFieldType.monthOfYear())); 198 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfMonth())); 199 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfWeek())); 200 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear())); 201 assertEquals(true, test.isSupported(DateTimeFieldType.weekOfWeekyear())); 202 assertEquals(true, test.isSupported(DateTimeFieldType.weekyear())); 203 assertEquals(true, test.isSupported(DateTimeFieldType.yearOfCentury())); 204 assertEquals(true, test.isSupported(DateTimeFieldType.yearOfEra())); 205 assertEquals(true, test.isSupported(DateTimeFieldType.centuryOfEra())); 206 assertEquals(true, test.isSupported(DateTimeFieldType.weekyearOfCentury())); 207 assertEquals(true, test.isSupported(DateTimeFieldType.era())); 208 assertEquals(false, test.isSupported(DateTimeFieldType.hourOfDay())); 209 assertEquals(false, test.isSupported((DateTimeFieldType) null)); 210 } 211 212 public void testIsSupported_DurationFieldType() { 213 LocalDate test = new LocalDate(1970, 6, 9); 214 assertEquals(false, test.isSupported(DurationFieldType.eras())); 215 assertEquals(true, test.isSupported(DurationFieldType.centuries())); 216 assertEquals(true, test.isSupported(DurationFieldType.years())); 217 assertEquals(true, test.isSupported(DurationFieldType.months())); 218 assertEquals(true, test.isSupported(DurationFieldType.weekyears())); 219 assertEquals(true, test.isSupported(DurationFieldType.weeks())); 220 assertEquals(true, test.isSupported(DurationFieldType.days())); 221 222 assertEquals(false, test.isSupported(DurationFieldType.hours())); 223 assertEquals(false, test.isSupported((DurationFieldType) null)); 224 } 225 226 public void testEqualsHashCode() { 227 LocalDate test1 = new LocalDate(1970, 6, 9, COPTIC_PARIS); 228 LocalDate test2 = new LocalDate(1970, 6, 9, COPTIC_PARIS); 229 assertEquals(true, test1.equals(test2)); 230 assertEquals(true, test2.equals(test1)); 231 assertEquals(true, test1.equals(test1)); 232 assertEquals(true, test2.equals(test2)); 233 assertEquals(true, test1.hashCode() == test2.hashCode()); 234 assertEquals(true, test1.hashCode() == test1.hashCode()); 235 assertEquals(true, test2.hashCode() == test2.hashCode()); 236 237 LocalDate test3 = new LocalDate(1971, 6, 9); 238 assertEquals(false, test1.equals(test3)); 239 assertEquals(false, test2.equals(test3)); 240 assertEquals(false, test3.equals(test1)); 241 assertEquals(false, test3.equals(test2)); 242 assertEquals(false, test1.hashCode() == test3.hashCode()); 243 assertEquals(false, test2.hashCode() == test3.hashCode()); 244 245 assertEquals(false, test1.equals("Hello")); 246 assertEquals(true, test1.equals(new MockInstant())); 247 assertEquals(true, test1.equals(new YearMonthDay(1970, 6, 9, COPTIC_PARIS))); 248 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE)); 249 } 250 251 class MockInstant extends MockPartial { 252 public Chronology getChronology() { 253 return COPTIC_UTC; 254 } 255 public DateTimeField[] getFields() { 256 return new DateTimeField[] { 257 COPTIC_UTC.year(), 258 COPTIC_UTC.monthOfYear(), 259 COPTIC_UTC.dayOfMonth(), 260 }; 261 } 262 public int[] getValues() { 263 return new int[] {1970, 6, 9}; 264 } 265 } 266 267 public void testEqualsHashCodeLenient() { 268 LocalDate test1 = new LocalDate(1970, 6, 9, LenientChronology.getInstance(COPTIC_PARIS)); 269 LocalDate test2 = new LocalDate(1970, 6, 9, LenientChronology.getInstance(COPTIC_PARIS)); 270 assertEquals(true, test1.equals(test2)); 271 assertEquals(true, test2.equals(test1)); 272 assertEquals(true, test1.equals(test1)); 273 assertEquals(true, test2.equals(test2)); 274 assertEquals(true, test1.hashCode() == test2.hashCode()); 275 assertEquals(true, test1.hashCode() == test1.hashCode()); 276 assertEquals(true, test2.hashCode() == test2.hashCode()); 277 } 278 279 public void testEqualsHashCodeStrict() { 280 LocalDate test1 = new LocalDate(1970, 6, 9, StrictChronology.getInstance(COPTIC_PARIS)); 281 LocalDate test2 = new LocalDate(1970, 6, 9, StrictChronology.getInstance(COPTIC_PARIS)); 282 assertEquals(true, test1.equals(test2)); 283 assertEquals(true, test2.equals(test1)); 284 assertEquals(true, test1.equals(test1)); 285 assertEquals(true, test2.equals(test2)); 286 assertEquals(true, test1.hashCode() == test2.hashCode()); 287 assertEquals(true, test1.hashCode() == test1.hashCode()); 288 assertEquals(true, test2.hashCode() == test2.hashCode()); 289 } 290 291 public void testCompareTo() { 293 LocalDate test1 = new LocalDate(2005, 6, 2); 294 LocalDate test1a = new LocalDate(2005, 6, 2); 295 assertEquals(0, test1.compareTo(test1a)); 296 assertEquals(0, test1a.compareTo(test1)); 297 assertEquals(0, test1.compareTo(test1)); 298 assertEquals(0, test1a.compareTo(test1a)); 299 300 LocalDate test2 = new LocalDate(2005, 7, 2); 301 assertEquals(-1, test1.compareTo(test2)); 302 assertEquals(+1, test2.compareTo(test1)); 303 304 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC()); 305 assertEquals(-1, test1.compareTo(test3)); 306 assertEquals(+1, test3.compareTo(test1)); 307 assertEquals(0, test3.compareTo(test2)); 308 309 DateTimeFieldType[] types = new DateTimeFieldType[] { 310 DateTimeFieldType.year(), 311 DateTimeFieldType.monthOfYear(), 312 DateTimeFieldType.dayOfMonth(), 313 }; 314 int[] values = new int[] {2005, 6, 2}; 315 Partial p = new Partial(types, values); 316 assertEquals(0, test1.compareTo(p)); 317 assertEquals(0, test1.compareTo(new YearMonthDay(2005, 6, 2))); 318 try { 319 test1.compareTo(null); 320 fail(); 321 } catch (NullPointerException ex) {} 322 try { 323 test1.compareTo(new Date ()); 324 fail(); 325 } catch (ClassCastException ex) {} 326 try { 327 test1.compareTo(new TimeOfDay()); 328 fail(); 329 } catch (ClassCastException ex) {} 330 Partial partial = new Partial() 331 .with(DateTimeFieldType.centuryOfEra(), 1) 332 .with(DateTimeFieldType.halfdayOfDay(), 0) 333 .with(DateTimeFieldType.dayOfMonth(), 9); 334 try { 335 new LocalDate(1970, 6, 9).compareTo(partial); 336 fail(); 337 } catch (ClassCastException ex) {} 338 } 339 340 public void testIsEqual_LocalDate() { 342 LocalDate test1 = new LocalDate(2005, 6, 2); 343 LocalDate test1a = new LocalDate(2005, 6, 2); 344 assertEquals(true, test1.isEqual(test1a)); 345 assertEquals(true, test1a.isEqual(test1)); 346 assertEquals(true, test1.isEqual(test1)); 347 assertEquals(true, test1a.isEqual(test1a)); 348 349 LocalDate test2 = new LocalDate(2005, 7, 2); 350 assertEquals(false, test1.isEqual(test2)); 351 assertEquals(false, test2.isEqual(test1)); 352 353 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC()); 354 assertEquals(false, test1.isEqual(test3)); 355 assertEquals(false, test3.isEqual(test1)); 356 assertEquals(true, test3.isEqual(test2)); 357 358 try { 359 new LocalDate(2005, 7, 2).isEqual(null); 360 fail(); 361 } catch (IllegalArgumentException ex) {} 362 } 363 364 public void testIsBefore_LocalDate() { 366 LocalDate test1 = new LocalDate(2005, 6, 2); 367 LocalDate test1a = new LocalDate(2005, 6, 2); 368 assertEquals(false, test1.isBefore(test1a)); 369 assertEquals(false, test1a.isBefore(test1)); 370 assertEquals(false, test1.isBefore(test1)); 371 assertEquals(false, test1a.isBefore(test1a)); 372 373 LocalDate test2 = new LocalDate(2005, 7, 2); 374 assertEquals(true, test1.isBefore(test2)); 375 assertEquals(false, test2.isBefore(test1)); 376 377 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC()); 378 assertEquals(true, test1.isBefore(test3)); 379 assertEquals(false, test3.isBefore(test1)); 380 assertEquals(false, test3.isBefore(test2)); 381 382 try { 383 new LocalDate(2005, 7, 2).isBefore(null); 384 fail(); 385 } catch (IllegalArgumentException ex) {} 386 } 387 388 public void testIsAfter_LocalDate() { 390 LocalDate test1 = new LocalDate(2005, 6, 2); 391 LocalDate test1a = new LocalDate(2005, 6, 2); 392 assertEquals(false, test1.isAfter(test1a)); 393 assertEquals(false, test1a.isAfter(test1)); 394 assertEquals(false, test1.isAfter(test1)); 395 assertEquals(false, test1a.isAfter(test1a)); 396 397 LocalDate test2 = new LocalDate(2005, 7, 2); 398 assertEquals(false, test1.isAfter(test2)); 399 assertEquals(true, test2.isAfter(test1)); 400 401 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC()); 402 assertEquals(false, test1.isAfter(test3)); 403 assertEquals(true, test3.isAfter(test1)); 404 assertEquals(false, test3.isAfter(test2)); 405 406 try { 407 new LocalDate(2005, 7, 2).isAfter(null); 408 fail(); 409 } catch (IllegalArgumentException ex) {} 410 } 411 412 public void testWithField_DateTimeFieldType_int_1() { 414 LocalDate test = new LocalDate(2004, 6, 9); 415 LocalDate result = test.withField(DateTimeFieldType.year(), 2006); 416 417 assertEquals(new LocalDate(2004, 6, 9), test); 418 assertEquals(new LocalDate(2006, 6, 9), result); 419 } 420 421 public void testWithField_DateTimeFieldType_int_2() { 422 LocalDate test = new LocalDate(2004, 6, 9); 423 try { 424 test.withField(null, 6); 425 fail(); 426 } catch (IllegalArgumentException ex) {} 427 } 428 429 public void testWithField_DateTimeFieldType_int_3() { 430 LocalDate test = new LocalDate(2004, 6, 9); 431 try { 432 test.withField(DateTimeFieldType.hourOfDay(), 6); 433 fail(); 434 } catch (IllegalArgumentException ex) {} 435 } 436 437 public void testWithField_DateTimeFieldType_int_4() { 438 LocalDate test = new LocalDate(2004, 6, 9); 439 LocalDate result = test.withField(DateTimeFieldType.year(), 2004); 440 assertEquals(new LocalDate(2004, 6, 9), test); 441 assertSame(test, result); 442 } 443 444 public void testWithFieldAdded_DurationFieldType_int_1() { 446 LocalDate test = new LocalDate(2004, 6, 9); 447 LocalDate result = test.withFieldAdded(DurationFieldType.years(), 6); 448 449 assertEquals(new LocalDate(2004, 6, 9), test); 450 assertEquals(new LocalDate(2010, 6, 9), result); 451 } 452 453 public void testWithFieldAdded_DurationFieldType_int_2() { 454 LocalDate test = new LocalDate(2004, 6, 9); 455 try { 456 test.withFieldAdded(null, 0); 457 fail(); 458 } catch (IllegalArgumentException ex) {} 459 } 460 461 public void testWithFieldAdded_DurationFieldType_int_3() { 462 LocalDate test = new LocalDate(2004, 6, 9); 463 try { 464 test.withFieldAdded(null, 6); 465 fail(); 466 } catch (IllegalArgumentException ex) {} 467 } 468 469 public void testWithFieldAdded_DurationFieldType_int_4() { 470 LocalDate test = new LocalDate(2004, 6, 9); 471 LocalDate result = test.withFieldAdded(DurationFieldType.years(), 0); 472 assertSame(test, result); 473 } 474 475 public void testWithFieldAdded_DurationFieldType_int_5() { 476 LocalDate test = new LocalDate(2004, 6, 9); 477 try { 478 test.withFieldAdded(DurationFieldType.hours(), 6); 479 fail(); 480 } catch (IllegalArgumentException ex) {} 481 } 482 483 public void testPlus_RP() { 485 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 486 LocalDate result = test.plus(new Period(1, 2, 3, 4, 29, 6, 7, 8)); 487 LocalDate expected = new LocalDate(2003, 7, 28, BUDDHIST_LONDON); 488 assertEquals(expected, result); 489 490 result = test.plus((ReadablePeriod) null); 491 assertSame(test, result); 492 } 493 494 public void testPlusYears_int() { 495 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 496 LocalDate result = test.plusYears(1); 497 LocalDate expected = new LocalDate(2003, 5, 3, BUDDHIST_LONDON); 498 assertEquals(expected, result); 499 500 result = test.plusYears(0); 501 assertSame(test, result); 502 } 503 504 public void testPlusMonths_int() { 505 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 506 LocalDate result = test.plusMonths(1); 507 LocalDate expected = new LocalDate(2002, 6, 3, BUDDHIST_LONDON); 508 assertEquals(expected, result); 509 510 result = test.plusMonths(0); 511 assertSame(test, result); 512 } 513 514 public void testPlusWeeks_int() { 515 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 516 LocalDate result = test.plusWeeks(1); 517 LocalDate expected = new LocalDate(2002, 5, 10, BUDDHIST_LONDON); 518 assertEquals(expected, result); 519 520 result = test.plusWeeks(0); 521 assertSame(test, result); 522 } 523 524 public void testPlusDays_int() { 525 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 526 LocalDate result = test.plusDays(1); 527 LocalDate expected = new LocalDate(2002, 5, 4, BUDDHIST_LONDON); 528 assertEquals(expected, result); 529 530 result = test.plusDays(0); 531 assertSame(test, result); 532 } 533 534 public void testMinus_RP() { 536 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 537 LocalDate result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1)); 538 539 542 LocalDate expected = new LocalDate(2001, 3, 26, BUDDHIST_LONDON); 543 assertEquals(expected, result); 544 545 result = test.minus((ReadablePeriod) null); 546 assertSame(test, result); 547 } 548 549 public void testMinusYears_int() { 550 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 551 LocalDate result = test.minusYears(1); 552 LocalDate expected = new LocalDate(2001, 5, 3, BUDDHIST_LONDON); 553 assertEquals(expected, result); 554 555 result = test.minusYears(0); 556 assertSame(test, result); 557 } 558 559 public void testMinusMonths_int() { 560 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 561 LocalDate result = test.minusMonths(1); 562 LocalDate expected = new LocalDate(2002, 4, 3, BUDDHIST_LONDON); 563 assertEquals(expected, result); 564 565 result = test.minusMonths(0); 566 assertSame(test, result); 567 } 568 569 public void testMinusWeeks_int() { 570 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 571 LocalDate result = test.minusWeeks(1); 572 LocalDate expected = new LocalDate(2002, 4, 26, BUDDHIST_LONDON); 573 assertEquals(expected, result); 574 575 result = test.minusWeeks(0); 576 assertSame(test, result); 577 } 578 579 public void testMinusDays_int() { 580 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 581 LocalDate result = test.minusDays(1); 582 LocalDate expected = new LocalDate(2002, 5, 2, BUDDHIST_LONDON); 583 assertEquals(expected, result); 584 585 result = test.minusDays(0); 586 assertSame(test, result); 587 } 588 589 public void testGetters() { 591 LocalDate test = new LocalDate(1970, 6, 9, GJ_UTC); 592 assertEquals(1970, test.getYear()); 593 assertEquals(6, test.getMonthOfYear()); 594 assertEquals(9, test.getDayOfMonth()); 595 assertEquals(160, test.getDayOfYear()); 596 assertEquals(2, test.getDayOfWeek()); 597 assertEquals(24, test.getWeekOfWeekyear()); 598 assertEquals(1970, test.getWeekyear()); 599 assertEquals(70, test.getYearOfCentury()); 600 assertEquals(20, test.getCenturyOfEra()); 601 assertEquals(1970, test.getYearOfEra()); 602 assertEquals(DateTimeConstants.AD, test.getEra()); 603 } 604 605 public void testWithers() { 607 LocalDate test = new LocalDate(1970, 6, 9, GJ_UTC); 608 check(test.withYear(2000), 2000, 6, 9); 609 check(test.withMonthOfYear(2), 1970, 2, 9); 610 check(test.withDayOfMonth(2), 1970, 6, 2); 611 check(test.withDayOfYear(6), 1970, 1, 6); 612 check(test.withDayOfWeek(6), 1970, 6, 13); 613 check(test.withWeekOfWeekyear(6), 1970, 2, 3); 614 check(test.withWeekyear(1971), 1971, 6, 15); 615 check(test.withYearOfCentury(60), 1960, 6, 9); 616 check(test.withCenturyOfEra(21), 2070, 6, 9); 617 check(test.withYearOfEra(1066), 1066, 6, 9); 618 check(test.withEra(DateTimeConstants.BC), -1970, 6, 9); 619 try { 620 test.withMonthOfYear(0); 621 fail(); 622 } catch (IllegalArgumentException ex) {} 623 try { 624 test.withMonthOfYear(13); 625 fail(); 626 } catch (IllegalArgumentException ex) {} 627 } 628 629 public void testToDateTimeAtMidnight() { 631 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 632 633 DateTime test = base.toDateTimeAtMidnight(); 634 check(base, 2005, 6, 9); 635 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test); 636 } 637 638 public void testToDateTimeAtMidnight_Zone() { 640 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 641 642 DateTime test = base.toDateTimeAtMidnight(TOKYO); 643 check(base, 2005, 6, 9); 644 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_TOKYO), test); 645 } 646 647 public void testToDateTimeAtMidnight_nullZone() { 648 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 649 650 DateTime test = base.toDateTimeAtMidnight((DateTimeZone) null); 651 check(base, 2005, 6, 9); 652 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test); 653 } 654 655 public void testToDateTimeAtCurrentTime() { 657 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 659 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 660 661 DateTime test = base.toDateTimeAtCurrentTime(); 662 check(base, 2005, 6, 9); 663 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 664 expected = expected.year().setCopy(2005); 665 expected = expected.monthOfYear().setCopy(6); 666 expected = expected.dayOfMonth().setCopy(9); 667 assertEquals(expected, test); 668 } 669 670 public void testToDateTimeAtCurrentTime_Zone() { 672 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 674 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 675 676 DateTime test = base.toDateTimeAtCurrentTime(TOKYO); 677 check(base, 2005, 6, 9); 678 DateTime expected = new DateTime(dt.getMillis(), COPTIC_TOKYO); 679 expected = expected.year().setCopy(2005); 680 expected = expected.monthOfYear().setCopy(6); 681 expected = expected.dayOfMonth().setCopy(9); 682 assertEquals(expected, test); 683 } 684 685 public void testToDateTimeAtCurrentTime_nullZone() { 686 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 688 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 689 690 DateTime test = base.toDateTimeAtCurrentTime((DateTimeZone) null); 691 check(base, 2005, 6, 9); 692 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 693 expected = expected.year().setCopy(2005); 694 expected = expected.monthOfYear().setCopy(6); 695 expected = expected.dayOfMonth().setCopy(9); 696 assertEquals(expected, test); 697 } 698 699 public void testToDateTime_LocalTime() { 701 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO); 703 704 DateTime test = base.toDateTime(tod); 705 check(base, 2005, 6, 9); 706 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 707 assertEquals(expected, test); 708 } 709 710 public void testToDateTime_nullLocalTime() { 711 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); long now = new DateTime(2004, 5, 8, 12, 13, 14, 15, COPTIC_LONDON).getMillis(); 713 DateTimeUtils.setCurrentMillisFixed(now); 714 715 DateTime test = base.toDateTime((LocalTime) null); 716 check(base, 2005, 6, 9); 717 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 718 assertEquals(expected, test); 719 } 720 721 public void testToDateTime_LocalTime_Zone() { 723 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO); 725 726 DateTime test = base.toDateTime(tod, TOKYO); 727 check(base, 2005, 6, 9); 728 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_TOKYO); 729 assertEquals(expected, test); 730 } 731 732 public void testToDateTime_LocalTime_nullZone() { 733 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO); 735 736 DateTime test = base.toDateTime(tod, null); 737 check(base, 2005, 6, 9); 738 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 739 assertEquals(expected, test); 740 } 741 742 public void testToDateTime_nullLocalTime_Zone() { 743 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); long now = new DateTime(2004, 5, 8, 12, 13, 14, 15, COPTIC_TOKYO).getMillis(); 745 DateTimeUtils.setCurrentMillisFixed(now); 746 747 DateTime test = base.toDateTime((LocalTime) null, TOKYO); 748 check(base, 2005, 6, 9); 749 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_TOKYO); 750 assertEquals(expected, test); 751 } 752 753 public void testToDateTime_wrongChronoLocalTime_Zone() { 754 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); LocalTime tod = new LocalTime(12, 13, 14, 15, BUDDHIST_TOKYO); 756 757 try { 758 base.toDateTime(tod, LONDON); 759 fail(); 760 } catch (IllegalArgumentException ex) {} 761 } 762 763 public void testToDateMidnight() { 765 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 766 767 DateMidnight test = base.toDateMidnight(); 768 check(base, 2005, 6, 9); 769 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test); 770 } 771 772 public void testToDateMidnight_Zone() { 774 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 775 776 DateMidnight test = base.toDateMidnight(TOKYO); 777 check(base, 2005, 6, 9); 778 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_TOKYO), test); 779 } 780 781 public void testToDateMidnight_nullZone() { 782 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 783 784 DateMidnight test = base.toDateMidnight((DateTimeZone) null); 785 check(base, 2005, 6, 9); 786 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test); 787 } 788 789 public void testToDateTime_RI() { 791 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 792 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7); 793 794 DateTime test = base.toDateTime(dt); 795 check(base, 2005, 6, 9); 796 DateTime expected = dt; 797 expected = expected.year().setCopy(2005); 798 expected = expected.monthOfYear().setCopy(6); 799 expected = expected.dayOfMonth().setCopy(9); 800 assertEquals(expected, test); 801 } 802 803 public void testToDateTime_nullRI() { 804 LocalDate base = new LocalDate(2005, 6, 9); 805 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7); 806 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 807 808 DateTime test = base.toDateTime((ReadableInstant) null); 809 check(base, 2005, 6, 9); 810 DateTime expected = dt; 811 expected = expected.year().setCopy(2005); 812 expected = expected.monthOfYear().setCopy(6); 813 expected = expected.dayOfMonth().setCopy(9); 814 assertEquals(expected, test); 815 } 816 817 public void testToInterval() { 819 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); Interval test = base.toInterval(); 821 check(base, 2005, 6, 9); 822 DateTime start = base.toDateTimeAtMidnight(); 823 DateTime end = start.plus(Period.days(1)); 824 Interval expected = new Interval(start, end); 825 assertEquals(expected, test); 826 } 827 828 public void testToInterval_Zone() { 830 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); Interval test = base.toInterval(TOKYO); 832 check(base, 2005, 6, 9); 833 DateTime start = base.toDateTimeAtMidnight(TOKYO); 834 DateTime end = start.plus(Period.days(1)); 835 Interval expected = new Interval(start, end); 836 assertEquals(expected, test); 837 } 838 839 public void testToInterval_nullZone() { 840 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); Interval test = base.toInterval(null); 842 check(base, 2005, 6, 9); 843 DateTime start = base.toDateTimeAtMidnight(LONDON); 844 DateTime end = start.plus(Period.days(1)); 845 Interval expected = new Interval(start, end); 846 assertEquals(expected, test); 847 } 848 849 public void testProperty() { 851 LocalDate test = new LocalDate(2005, 6, 9, GJ_UTC); 852 assertEquals(test.year(), test.property(DateTimeFieldType.year())); 853 assertEquals(test.monthOfYear(), test.property(DateTimeFieldType.monthOfYear())); 854 assertEquals(test.dayOfMonth(), test.property(DateTimeFieldType.dayOfMonth())); 855 assertEquals(test.dayOfWeek(), test.property(DateTimeFieldType.dayOfWeek())); 856 assertEquals(test.dayOfYear(), test.property(DateTimeFieldType.dayOfYear())); 857 assertEquals(test.weekOfWeekyear(), test.property(DateTimeFieldType.weekOfWeekyear())); 858 assertEquals(test.weekyear(), test.property(DateTimeFieldType.weekyear())); 859 assertEquals(test.yearOfCentury(), test.property(DateTimeFieldType.yearOfCentury())); 860 assertEquals(test.yearOfEra(), test.property(DateTimeFieldType.yearOfEra())); 861 assertEquals(test.centuryOfEra(), test.property(DateTimeFieldType.centuryOfEra())); 862 assertEquals(test.era(), test.property(DateTimeFieldType.era())); 863 try { 864 test.property(DateTimeFieldType.millisOfDay()); 865 fail(); 866 } catch (IllegalArgumentException ex) {} 867 try { 868 test.property(null); 869 fail(); 870 } catch (IllegalArgumentException ex) {} 871 } 872 873 public void testSerialization() throws Exception { 875 LocalDate test = new LocalDate(1972, 6, 9, COPTIC_PARIS); 876 877 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 878 ObjectOutputStream oos = new ObjectOutputStream (baos); 879 oos.writeObject(test); 880 byte[] bytes = baos.toByteArray(); 881 oos.close(); 882 883 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 884 ObjectInputStream ois = new ObjectInputStream (bais); 885 LocalDate result = (LocalDate) ois.readObject(); 886 ois.close(); 887 888 assertEquals(test, result); 889 assertTrue(Arrays.equals(test.getValues(), result.getValues())); 890 assertTrue(Arrays.equals(test.getFields(), result.getFields())); 891 assertEquals(test.getChronology(), result.getChronology()); 892 } 893 894 public void testToString() { 896 LocalDate test = new LocalDate(2002, 6, 9); 897 assertEquals("2002-06-09", test.toString()); 898 } 899 900 public void testToString_String() { 902 LocalDate test = new LocalDate(2002, 6, 9); 903 assertEquals("2002 \ufffd\ufffd", test.toString("yyyy HH")); 904 assertEquals("2002-06-09", test.toString((String ) null)); 905 } 906 907 public void testToString_String_Locale() { 909 LocalDate test = new LocalDate(1970, 6, 9); 910 assertEquals("Tue 9/6", test.toString("EEE d/M", Locale.ENGLISH)); 911 assertEquals("mar. 9/6", test.toString("EEE d/M", Locale.FRENCH)); 912 assertEquals("1970-06-09", test.toString(null, Locale.ENGLISH)); 913 assertEquals("Tue 9/6", test.toString("EEE d/M", null)); 914 assertEquals("1970-06-09", test.toString(null, null)); 915 } 916 917 public void testToString_DTFormatter() { 919 LocalDate test = new LocalDate(2002, 6, 9); 920 assertEquals("2002 \ufffd\ufffd", test.toString(DateTimeFormat.forPattern("yyyy HH"))); 921 assertEquals("2002-06-09", test.toString((DateTimeFormatter) null)); 922 } 923 924 private void check(LocalDate test, int hour, int min, int sec) { 926 assertEquals(hour, test.getYear()); 927 assertEquals(min, test.getMonthOfYear()); 928 assertEquals(sec, test.getDayOfMonth()); 929 } 930 } 931 | Popular Tags |