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.GregorianChronology; 32 import org.joda.time.chrono.ISOChronology; 33 import org.joda.time.format.DateTimeFormat; 34 import org.joda.time.format.DateTimeFormatter; 35 36 41 public class TestYearMonthDay_Basics extends TestCase { 42 43 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 44 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 45 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 46 private static final int OFFSET = 1; 47 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS); 48 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON); 49 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO); 50 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 51 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 52 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON); 53 private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO); 54 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 55 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS); 56 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON); 57 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO); 58 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC(); 59 60 private long TEST_TIME_NOW = 61 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 62 63 private long TEST_TIME1 = 64 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 65 + 12L * DateTimeConstants.MILLIS_PER_HOUR 66 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 67 68 private long TEST_TIME2 = 69 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 70 + 14L * DateTimeConstants.MILLIS_PER_HOUR 71 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 72 73 private DateTimeZone zone = null; 74 75 public static void main(String [] args) { 76 junit.textui.TestRunner.run(suite()); 77 } 78 79 public static TestSuite suite() { 80 return new TestSuite(TestYearMonthDay_Basics.class); 81 } 82 83 public TestYearMonthDay_Basics(String name) { 84 super(name); 85 } 86 87 protected void setUp() throws Exception { 88 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 89 zone = DateTimeZone.getDefault(); 90 DateTimeZone.setDefault(LONDON); 91 } 92 93 protected void tearDown() throws Exception { 94 DateTimeUtils.setCurrentMillisSystem(); 95 DateTimeZone.setDefault(zone); 96 zone = null; 97 } 98 99 public void testGet() { 101 YearMonthDay test = new YearMonthDay(); 102 assertEquals(1970, test.get(DateTimeFieldType.year())); 103 assertEquals(6, test.get(DateTimeFieldType.monthOfYear())); 104 assertEquals(9, test.get(DateTimeFieldType.dayOfMonth())); 105 try { 106 test.get(null); 107 fail(); 108 } catch (IllegalArgumentException ex) {} 109 try { 110 test.get(DateTimeFieldType.hourOfDay()); 111 fail(); 112 } catch (IllegalArgumentException ex) {} 113 } 114 115 public void testSize() { 116 YearMonthDay test = new YearMonthDay(); 117 assertEquals(3, test.size()); 118 } 119 120 public void testGetFieldType() { 121 YearMonthDay test = new YearMonthDay(COPTIC_PARIS); 122 assertSame(DateTimeFieldType.year(), test.getFieldType(0)); 123 assertSame(DateTimeFieldType.monthOfYear(), test.getFieldType(1)); 124 assertSame(DateTimeFieldType.dayOfMonth(), test.getFieldType(2)); 125 try { 126 test.getFieldType(-1); 127 } catch (IndexOutOfBoundsException ex) {} 128 try { 129 test.getFieldType(3); 130 } catch (IndexOutOfBoundsException ex) {} 131 } 132 133 public void testGetFieldTypes() { 134 YearMonthDay test = new YearMonthDay(COPTIC_PARIS); 135 DateTimeFieldType[] fields = test.getFieldTypes(); 136 assertSame(DateTimeFieldType.year(), fields[0]); 137 assertSame(DateTimeFieldType.monthOfYear(), fields[1]); 138 assertSame(DateTimeFieldType.dayOfMonth(), fields[2]); 139 assertNotSame(test.getFieldTypes(), test.getFieldTypes()); 140 } 141 142 public void testGetField() { 143 YearMonthDay test = new YearMonthDay(COPTIC_PARIS); 144 assertSame(COPTIC_UTC.year(), test.getField(0)); 145 assertSame(COPTIC_UTC.monthOfYear(), test.getField(1)); 146 assertSame(COPTIC_UTC.dayOfMonth(), test.getField(2)); 147 try { 148 test.getField(-1); 149 } catch (IndexOutOfBoundsException ex) {} 150 try { 151 test.getField(3); 152 } catch (IndexOutOfBoundsException ex) {} 153 } 154 155 public void testGetFields() { 156 YearMonthDay test = new YearMonthDay(COPTIC_PARIS); 157 DateTimeField[] fields = test.getFields(); 158 assertSame(COPTIC_UTC.year(), fields[0]); 159 assertSame(COPTIC_UTC.monthOfYear(), fields[1]); 160 assertSame(COPTIC_UTC.dayOfMonth(), fields[2]); 161 assertNotSame(test.getFields(), test.getFields()); 162 } 163 164 public void testGetValue() { 165 YearMonthDay test = new YearMonthDay(); 166 assertEquals(1970, test.getValue(0)); 167 assertEquals(6, test.getValue(1)); 168 assertEquals(9, test.getValue(2)); 169 try { 170 test.getValue(-1); 171 } catch (IndexOutOfBoundsException ex) {} 172 try { 173 test.getValue(3); 174 } catch (IndexOutOfBoundsException ex) {} 175 } 176 177 public void testGetValues() { 178 YearMonthDay test = new YearMonthDay(); 179 int[] values = test.getValues(); 180 assertEquals(1970, values[0]); 181 assertEquals(6, values[1]); 182 assertEquals(9, values[2]); 183 assertNotSame(test.getValues(), test.getValues()); 184 } 185 186 public void testIsSupported() { 187 YearMonthDay test = new YearMonthDay(COPTIC_PARIS); 188 assertEquals(true, test.isSupported(DateTimeFieldType.year())); 189 assertEquals(true, test.isSupported(DateTimeFieldType.monthOfYear())); 190 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfMonth())); 191 assertEquals(false, test.isSupported(DateTimeFieldType.hourOfDay())); 192 } 193 194 public void testEqualsHashCode() { 195 YearMonthDay test1 = new YearMonthDay(1970, 6, 9, COPTIC_PARIS); 196 YearMonthDay test2 = new YearMonthDay(1970, 6, 9, COPTIC_PARIS); 197 assertEquals(true, test1.equals(test2)); 198 assertEquals(true, test2.equals(test1)); 199 assertEquals(true, test1.equals(test1)); 200 assertEquals(true, test2.equals(test2)); 201 assertEquals(true, test1.hashCode() == test2.hashCode()); 202 assertEquals(true, test1.hashCode() == test1.hashCode()); 203 assertEquals(true, test2.hashCode() == test2.hashCode()); 204 205 YearMonthDay test3 = new YearMonthDay(1971, 6, 9); 206 assertEquals(false, test1.equals(test3)); 207 assertEquals(false, test2.equals(test3)); 208 assertEquals(false, test3.equals(test1)); 209 assertEquals(false, test3.equals(test2)); 210 assertEquals(false, test1.hashCode() == test3.hashCode()); 211 assertEquals(false, test2.hashCode() == test3.hashCode()); 212 213 assertEquals(false, test1.equals("Hello")); 214 assertEquals(true, test1.equals(new MockInstant())); 215 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE)); 216 } 217 218 class MockInstant extends MockPartial { 219 public Chronology getChronology() { 220 return COPTIC_UTC; 221 } 222 public DateTimeField[] getFields() { 223 return new DateTimeField[] { 224 COPTIC_UTC.year(), 225 COPTIC_UTC.monthOfYear(), 226 COPTIC_UTC.dayOfMonth(), 227 }; 228 } 229 public int[] getValues() { 230 return new int[] {1970, 6, 9}; 231 } 232 } 233 234 public void testCompareTo() { 236 YearMonthDay test1 = new YearMonthDay(2005, 6, 2); 237 YearMonthDay test1a = new YearMonthDay(2005, 6, 2); 238 assertEquals(0, test1.compareTo(test1a)); 239 assertEquals(0, test1a.compareTo(test1)); 240 assertEquals(0, test1.compareTo(test1)); 241 assertEquals(0, test1a.compareTo(test1a)); 242 243 YearMonthDay test2 = new YearMonthDay(2005, 7, 2); 244 assertEquals(-1, test1.compareTo(test2)); 245 assertEquals(+1, test2.compareTo(test1)); 246 247 YearMonthDay test3 = new YearMonthDay(2005, 7, 2, GregorianChronology.getInstanceUTC()); 248 assertEquals(-1, test1.compareTo(test3)); 249 assertEquals(+1, test3.compareTo(test1)); 250 assertEquals(0, test3.compareTo(test2)); 251 252 DateTimeFieldType[] types = new DateTimeFieldType[] { 253 DateTimeFieldType.year(), 254 DateTimeFieldType.monthOfYear(), 255 DateTimeFieldType.dayOfMonth(), 256 }; 257 int[] values = new int[] {2005, 6, 2}; 258 Partial p = new Partial(types, values); 259 assertEquals(0, test1.compareTo(p)); 260 try { 261 test1.compareTo(null); 262 fail(); 263 } catch (NullPointerException ex) {} 264 try { 265 test1.compareTo(new Date ()); 266 fail(); 267 } catch (ClassCastException ex) {} 268 try { 269 test1.compareTo(new TimeOfDay()); 270 fail(); 271 } catch (ClassCastException ex) {} 272 Partial partial = new Partial() 273 .with(DateTimeFieldType.centuryOfEra(), 1) 274 .with(DateTimeFieldType.halfdayOfDay(), 0) 275 .with(DateTimeFieldType.dayOfMonth(), 9); 276 try { 277 new YearMonthDay(1970, 6, 9).compareTo(partial); 278 fail(); 279 } catch (ClassCastException ex) {} 280 } 281 282 public void testIsEqual_YMD() { 284 YearMonthDay test1 = new YearMonthDay(2005, 6, 2); 285 YearMonthDay test1a = new YearMonthDay(2005, 6, 2); 286 assertEquals(true, test1.isEqual(test1a)); 287 assertEquals(true, test1a.isEqual(test1)); 288 assertEquals(true, test1.isEqual(test1)); 289 assertEquals(true, test1a.isEqual(test1a)); 290 291 YearMonthDay test2 = new YearMonthDay(2005, 7, 2); 292 assertEquals(false, test1.isEqual(test2)); 293 assertEquals(false, test2.isEqual(test1)); 294 295 YearMonthDay test3 = new YearMonthDay(2005, 7, 2, GregorianChronology.getInstanceUTC()); 296 assertEquals(false, test1.isEqual(test3)); 297 assertEquals(false, test3.isEqual(test1)); 298 assertEquals(true, test3.isEqual(test2)); 299 300 try { 301 new YearMonthDay(2005, 7, 2).isEqual(null); 302 fail(); 303 } catch (IllegalArgumentException ex) {} 304 } 305 306 public void testIsBefore_YMD() { 308 YearMonthDay test1 = new YearMonthDay(2005, 6, 2); 309 YearMonthDay test1a = new YearMonthDay(2005, 6, 2); 310 assertEquals(false, test1.isBefore(test1a)); 311 assertEquals(false, test1a.isBefore(test1)); 312 assertEquals(false, test1.isBefore(test1)); 313 assertEquals(false, test1a.isBefore(test1a)); 314 315 YearMonthDay test2 = new YearMonthDay(2005, 7, 2); 316 assertEquals(true, test1.isBefore(test2)); 317 assertEquals(false, test2.isBefore(test1)); 318 319 YearMonthDay test3 = new YearMonthDay(2005, 7, 2, GregorianChronology.getInstanceUTC()); 320 assertEquals(true, test1.isBefore(test3)); 321 assertEquals(false, test3.isBefore(test1)); 322 assertEquals(false, test3.isBefore(test2)); 323 324 try { 325 new YearMonthDay(2005, 7, 2).isBefore(null); 326 fail(); 327 } catch (IllegalArgumentException ex) {} 328 } 329 330 public void testIsAfter_YMD() { 332 YearMonthDay test1 = new YearMonthDay(2005, 6, 2); 333 YearMonthDay test1a = new YearMonthDay(2005, 6, 2); 334 assertEquals(false, test1.isAfter(test1a)); 335 assertEquals(false, test1a.isAfter(test1)); 336 assertEquals(false, test1.isAfter(test1)); 337 assertEquals(false, test1a.isAfter(test1a)); 338 339 YearMonthDay test2 = new YearMonthDay(2005, 7, 2); 340 assertEquals(false, test1.isAfter(test2)); 341 assertEquals(true, test2.isAfter(test1)); 342 343 YearMonthDay test3 = new YearMonthDay(2005, 7, 2, GregorianChronology.getInstanceUTC()); 344 assertEquals(false, test1.isAfter(test3)); 345 assertEquals(true, test3.isAfter(test1)); 346 assertEquals(false, test3.isAfter(test2)); 347 348 try { 349 new YearMonthDay(2005, 7, 2).isAfter(null); 350 fail(); 351 } catch (IllegalArgumentException ex) {} 352 } 353 354 public void testWithChronologyRetainFields_Chrono() { 356 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 357 YearMonthDay test = base.withChronologyRetainFields(BUDDHIST_TOKYO); 358 check(base, 2005, 6, 9); 359 assertEquals(COPTIC_UTC, base.getChronology()); 360 check(test, 2005, 6, 9); 361 assertEquals(BUDDHIST_UTC, test.getChronology()); 362 } 363 364 public void testWithChronologyRetainFields_sameChrono() { 365 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 366 YearMonthDay test = base.withChronologyRetainFields(COPTIC_TOKYO); 367 assertSame(base, test); 368 } 369 370 public void testWithChronologyRetainFields_nullChrono() { 371 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 372 YearMonthDay test = base.withChronologyRetainFields(null); 373 check(base, 2005, 6, 9); 374 assertEquals(COPTIC_UTC, base.getChronology()); 375 check(test, 2005, 6, 9); 376 assertEquals(ISO_UTC, test.getChronology()); 377 } 378 379 public void testWithChronologyRetainFields_invalidInNewChrono() { 380 YearMonthDay base = new YearMonthDay(2005, 1, 31, ISO_UTC); 381 try { 382 base.withChronologyRetainFields(COPTIC_UTC); 383 fail(); 384 } catch (IllegalArgumentException ex) { 385 } 387 } 388 389 public void testWithField1() { 391 YearMonthDay test = new YearMonthDay(2004, 6, 9); 392 YearMonthDay result = test.withField(DateTimeFieldType.year(), 2006); 393 394 assertEquals(new YearMonthDay(2004, 6, 9), test); 395 assertEquals(new YearMonthDay(2006, 6, 9), result); 396 } 397 398 public void testWithField2() { 399 YearMonthDay test = new YearMonthDay(2004, 6, 9); 400 try { 401 test.withField(null, 6); 402 fail(); 403 } catch (IllegalArgumentException ex) {} 404 } 405 406 public void testWithField3() { 407 YearMonthDay test = new YearMonthDay(2004, 6, 9); 408 try { 409 test.withField(DateTimeFieldType.hourOfDay(), 6); 410 fail(); 411 } catch (IllegalArgumentException ex) {} 412 } 413 414 public void testWithField4() { 415 YearMonthDay test = new YearMonthDay(2004, 6, 9); 416 YearMonthDay result = test.withField(DateTimeFieldType.year(), 2004); 417 assertEquals(new YearMonthDay(2004, 6, 9), test); 418 assertSame(test, result); 419 } 420 421 public void testWithFieldAdded1() { 423 YearMonthDay test = new YearMonthDay(2004, 6, 9); 424 YearMonthDay result = test.withFieldAdded(DurationFieldType.years(), 6); 425 426 assertEquals(new YearMonthDay(2004, 6, 9), test); 427 assertEquals(new YearMonthDay(2010, 6, 9), result); 428 } 429 430 public void testWithFieldAdded2() { 431 YearMonthDay test = new YearMonthDay(2004, 6, 9); 432 try { 433 test.withFieldAdded(null, 0); 434 fail(); 435 } catch (IllegalArgumentException ex) {} 436 } 437 438 public void testWithFieldAdded3() { 439 YearMonthDay test = new YearMonthDay(2004, 6, 9); 440 try { 441 test.withFieldAdded(null, 6); 442 fail(); 443 } catch (IllegalArgumentException ex) {} 444 } 445 446 public void testWithFieldAdded4() { 447 YearMonthDay test = new YearMonthDay(2004, 6, 9); 448 YearMonthDay result = test.withFieldAdded(DurationFieldType.years(), 0); 449 assertSame(test, result); 450 } 451 452 public void testWithFieldAdded5() { 453 YearMonthDay test = new YearMonthDay(2004, 6, 9); 454 try { 455 test.withFieldAdded(DurationFieldType.hours(), 6); 456 fail(); 457 } catch (IllegalArgumentException ex) {} 458 } 459 460 public void testPlus_RP() { 462 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 463 YearMonthDay result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7, 8)); 464 YearMonthDay expected = new YearMonthDay(2003, 7, 7, BuddhistChronology.getInstance()); 465 assertEquals(expected, result); 466 467 result = test.plus((ReadablePeriod) null); 468 assertSame(test, result); 469 } 470 471 public void testPlusYears_int() { 472 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 473 YearMonthDay result = test.plusYears(1); 474 YearMonthDay expected = new YearMonthDay(2003, 5, 3, BuddhistChronology.getInstance()); 475 assertEquals(expected, result); 476 477 result = test.plusYears(0); 478 assertSame(test, result); 479 } 480 481 public void testPlusMonths_int() { 482 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 483 YearMonthDay result = test.plusMonths(1); 484 YearMonthDay expected = new YearMonthDay(2002, 6, 3, BuddhistChronology.getInstance()); 485 assertEquals(expected, result); 486 487 result = test.plusMonths(0); 488 assertSame(test, result); 489 } 490 491 public void testPlusDays_int() { 492 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 493 YearMonthDay result = test.plusDays(1); 494 YearMonthDay expected = new YearMonthDay(2002, 5, 4, BuddhistChronology.getInstance()); 495 assertEquals(expected, result); 496 497 result = test.plusDays(0); 498 assertSame(test, result); 499 } 500 501 public void testMinus_RP() { 503 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 504 YearMonthDay result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1)); 505 YearMonthDay expected = new YearMonthDay(2001, 4, 2, BuddhistChronology.getInstance()); 506 assertEquals(expected, result); 507 508 result = test.minus((ReadablePeriod) null); 509 assertSame(test, result); 510 } 511 512 public void testMinusYears_int() { 513 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 514 YearMonthDay result = test.minusYears(1); 515 YearMonthDay expected = new YearMonthDay(2001, 5, 3, BuddhistChronology.getInstance()); 516 assertEquals(expected, result); 517 518 result = test.minusYears(0); 519 assertSame(test, result); 520 } 521 522 public void testMinusMonths_int() { 523 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 524 YearMonthDay result = test.minusMonths(1); 525 YearMonthDay expected = new YearMonthDay(2002, 4, 3, BuddhistChronology.getInstance()); 526 assertEquals(expected, result); 527 528 result = test.minusMonths(0); 529 assertSame(test, result); 530 } 531 532 public void testMinusDays_int() { 533 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 534 YearMonthDay result = test.minusDays(1); 535 YearMonthDay expected = new YearMonthDay(2002, 5, 2, BuddhistChronology.getInstance()); 536 assertEquals(expected, result); 537 538 result = test.minusDays(0); 539 assertSame(test, result); 540 } 541 542 public void testToLocalDate() { 544 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_UTC); 545 LocalDate test = base.toLocalDate(); 546 assertEquals(new LocalDate(2005, 6, 9, COPTIC_UTC), test); 547 } 548 549 public void testToDateTimeAtMidnight() { 551 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 552 553 DateTime test = base.toDateTimeAtMidnight(); 554 check(base, 2005, 6, 9); 555 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test); 556 } 557 558 public void testToDateTimeAtMidnight_Zone() { 560 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 561 562 DateTime test = base.toDateTimeAtMidnight(TOKYO); 563 check(base, 2005, 6, 9); 564 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_TOKYO), test); 565 } 566 567 public void testToDateTimeAtMidnight_nullZone() { 568 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 569 570 DateTime test = base.toDateTimeAtMidnight((DateTimeZone) null); 571 check(base, 2005, 6, 9); 572 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test); 573 } 574 575 public void testToDateTimeAtCurrentTime() { 577 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 579 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 580 581 DateTime test = base.toDateTimeAtCurrentTime(); 582 check(base, 2005, 6, 9); 583 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 584 expected = expected.year().setCopy(2005); 585 expected = expected.monthOfYear().setCopy(6); 586 expected = expected.dayOfMonth().setCopy(9); 587 assertEquals(expected, test); 588 } 589 590 public void testToDateTimeAtCurrentTime_Zone() { 592 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 594 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 595 596 DateTime test = base.toDateTimeAtCurrentTime(TOKYO); 597 check(base, 2005, 6, 9); 598 DateTime expected = new DateTime(dt.getMillis(), COPTIC_TOKYO); 599 expected = expected.year().setCopy(2005); 600 expected = expected.monthOfYear().setCopy(6); 601 expected = expected.dayOfMonth().setCopy(9); 602 assertEquals(expected, test); 603 } 604 605 public void testToDateTimeAtCurrentTime_nullZone() { 606 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 608 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 609 610 DateTime test = base.toDateTimeAtCurrentTime((DateTimeZone) null); 611 check(base, 2005, 6, 9); 612 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 613 expected = expected.year().setCopy(2005); 614 expected = expected.monthOfYear().setCopy(6); 615 expected = expected.dayOfMonth().setCopy(9); 616 assertEquals(expected, test); 617 } 618 619 public void testToDateTime_TOD() { 621 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); TimeOfDay tod = new TimeOfDay(12, 13, 14, 15, BUDDHIST_TOKYO); 623 624 DateTime test = base.toDateTime(tod); 625 check(base, 2005, 6, 9); 626 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 627 assertEquals(expected, test); 628 } 629 630 public void testToDateTime_nullTOD() { 631 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); long now = new DateTime(2004, 5, 8, 12, 13, 14, 15, COPTIC_LONDON).getMillis(); 633 DateTimeUtils.setCurrentMillisFixed(now); 634 635 DateTime test = base.toDateTime((TimeOfDay) null); 636 check(base, 2005, 6, 9); 637 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 638 assertEquals(expected, test); 639 } 640 641 public void testToDateTime_TOD_Zone() { 643 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); TimeOfDay tod = new TimeOfDay(12, 13, 14, 15, BUDDHIST_TOKYO); 645 646 DateTime test = base.toDateTime(tod, TOKYO); 647 check(base, 2005, 6, 9); 648 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_TOKYO); 649 assertEquals(expected, test); 650 } 651 652 public void testToDateTime_TOD_nullZone() { 653 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); TimeOfDay tod = new TimeOfDay(12, 13, 14, 15, BUDDHIST_TOKYO); 655 656 DateTime test = base.toDateTime(tod, null); 657 check(base, 2005, 6, 9); 658 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 659 assertEquals(expected, test); 660 } 661 662 public void testToDateTime_nullTOD_Zone() { 663 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); long now = new DateTime(2004, 5, 8, 12, 13, 14, 15, COPTIC_TOKYO).getMillis(); 665 DateTimeUtils.setCurrentMillisFixed(now); 666 667 DateTime test = base.toDateTime((TimeOfDay) null, TOKYO); 668 check(base, 2005, 6, 9); 669 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_TOKYO); 670 assertEquals(expected, test); 671 } 672 673 public void testToDateMidnight() { 675 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 676 677 DateMidnight test = base.toDateMidnight(); 678 check(base, 2005, 6, 9); 679 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test); 680 } 681 682 public void testToDateMidnight_Zone() { 684 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 685 686 DateMidnight test = base.toDateMidnight(TOKYO); 687 check(base, 2005, 6, 9); 688 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_TOKYO), test); 689 } 690 691 public void testToDateMidnight_nullZone() { 692 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 693 694 DateMidnight test = base.toDateMidnight((DateTimeZone) null); 695 check(base, 2005, 6, 9); 696 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test); 697 } 698 699 public void testToDateTime_RI() { 701 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 702 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7); 703 704 DateTime test = base.toDateTime(dt); 705 check(base, 2005, 6, 9); 706 DateTime expected = dt; 707 expected = expected.year().setCopy(2005); 708 expected = expected.monthOfYear().setCopy(6); 709 expected = expected.dayOfMonth().setCopy(9); 710 assertEquals(expected, test); 711 } 712 713 public void testToDateTime_nullRI() { 714 YearMonthDay base = new YearMonthDay(2005, 6, 9); 715 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7); 716 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 717 718 DateTime test = base.toDateTime((ReadableInstant) null); 719 check(base, 2005, 6, 9); 720 DateTime expected = dt; 721 expected = expected.year().setCopy(2005); 722 expected = expected.monthOfYear().setCopy(6); 723 expected = expected.dayOfMonth().setCopy(9); 724 assertEquals(expected, test); 725 } 726 727 public void testToInterval() { 729 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); Interval test = base.toInterval(); 731 check(base, 2005, 6, 9); 732 DateTime start = base.toDateTime(TimeOfDay.MIDNIGHT); 733 DateTime end = start.plus(Period.days(1)); 734 Interval expected = new Interval(start, end); 735 assertEquals(expected, test); 736 } 737 738 public void testToInterval_Zone() { 740 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); Interval test = base.toInterval(TOKYO); 742 check(base, 2005, 6, 9); 743 DateTime start = base.toDateTime(TimeOfDay.MIDNIGHT, TOKYO); 744 DateTime end = start.plus(Period.days(1)); 745 Interval expected = new Interval(start, end); 746 assertEquals(expected, test); 747 } 748 749 public void testToInterval_nullZone() { 750 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); Interval test = base.toInterval(null); 752 check(base, 2005, 6, 9); 753 DateTime start = base.toDateTime(TimeOfDay.MIDNIGHT, LONDON); 754 DateTime end = start.plus(Period.days(1)); 755 Interval expected = new Interval(start, end); 756 assertEquals(expected, test); 757 } 758 759 public void testWithers() { 761 YearMonthDay test = new YearMonthDay(1970, 6, 9); 762 check(test.withYear(2000), 2000, 6, 9); 763 check(test.withMonthOfYear(2), 1970, 2, 9); 764 check(test.withDayOfMonth(2), 1970, 6, 2); 765 try { 766 test.withMonthOfYear(0); 767 fail(); 768 } catch (IllegalArgumentException ex) {} 769 try { 770 test.withMonthOfYear(13); 771 fail(); 772 } catch (IllegalArgumentException ex) {} 773 } 774 775 public void testProperty() { 777 YearMonthDay test = new YearMonthDay(2005, 6, 9); 778 assertEquals(test.year(), test.property(DateTimeFieldType.year())); 779 assertEquals(test.monthOfYear(), test.property(DateTimeFieldType.monthOfYear())); 780 assertEquals(test.dayOfMonth(), test.property(DateTimeFieldType.dayOfMonth())); 781 try { 782 test.property(DateTimeFieldType.millisOfDay()); 783 fail(); 784 } catch (IllegalArgumentException ex) {} 785 try { 786 test.property(null); 787 fail(); 788 } catch (IllegalArgumentException ex) {} 789 } 790 791 public void testSerialization() throws Exception { 793 YearMonthDay test = new YearMonthDay(1972, 6, 9, COPTIC_PARIS); 794 795 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 796 ObjectOutputStream oos = new ObjectOutputStream (baos); 797 oos.writeObject(test); 798 byte[] bytes = baos.toByteArray(); 799 oos.close(); 800 801 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 802 ObjectInputStream ois = new ObjectInputStream (bais); 803 YearMonthDay result = (YearMonthDay) ois.readObject(); 804 ois.close(); 805 806 assertEquals(test, result); 807 assertTrue(Arrays.equals(test.getValues(), result.getValues())); 808 assertTrue(Arrays.equals(test.getFields(), result.getFields())); 809 assertEquals(test.getChronology(), result.getChronology()); 810 } 811 812 public void testToString() { 814 YearMonthDay test = new YearMonthDay(2002, 6, 9); 815 assertEquals("2002-06-09", test.toString()); 816 } 817 818 public void testToString_String() { 820 YearMonthDay test = new YearMonthDay(2002, 6, 9); 821 assertEquals("2002 \ufffd\ufffd", test.toString("yyyy HH")); 822 assertEquals("2002-06-09", test.toString((String ) null)); 823 } 824 825 public void testToString_String_Locale() { 827 YearMonthDay test = new YearMonthDay(2002, 6, 9); 828 assertEquals("\ufffd 9/6", test.toString("EEE d/M", Locale.ENGLISH)); 829 assertEquals("\ufffd 9/6", test.toString("EEE d/M", Locale.FRENCH)); 830 assertEquals("2002-06-09", test.toString(null, Locale.ENGLISH)); 831 assertEquals("\ufffd 9/6", test.toString("EEE d/M", null)); 832 assertEquals("2002-06-09", test.toString(null, null)); 833 } 834 835 public void testToString_DTFormatter() { 837 YearMonthDay test = new YearMonthDay(2002, 6, 9); 838 assertEquals("2002 \ufffd\ufffd", test.toString(DateTimeFormat.forPattern("yyyy HH"))); 839 assertEquals("2002-06-09", test.toString((DateTimeFormatter) null)); 840 } 841 842 private void check(YearMonthDay test, int hour, int min, int sec) { 844 assertEquals(hour, test.getYear()); 845 assertEquals(min, test.getMonthOfYear()); 846 assertEquals(sec, test.getDayOfMonth()); 847 } 848 } 849 | Popular Tags |