001 /* 002 * Copyright 2001-2009 Stephen Colebourne 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.joda.time; 017 018 import java.io.ByteArrayInputStream; 019 import java.io.ByteArrayOutputStream; 020 import java.io.ObjectInputStream; 021 import java.io.ObjectOutputStream; 022 import java.util.Arrays; 023 import java.util.Locale; 024 025 import junit.framework.TestCase; 026 import junit.framework.TestSuite; 027 028 import org.joda.time.chrono.BuddhistChronology; 029 import org.joda.time.chrono.CopticChronology; 030 import org.joda.time.chrono.GregorianChronology; 031 import org.joda.time.chrono.ISOChronology; 032 import org.joda.time.format.DateTimeFormat; 033 import org.joda.time.format.DateTimeFormatter; 034 035 /** 036 * This class is a Junit unit test for YearMonthDay. 037 * 038 * @author Stephen Colebourne 039 */ 040 public class TestYearMonthDay_Basics extends TestCase { 041 042 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 043 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 044 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 045 private static final int OFFSET = 1; 046 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS); 047 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON); 048 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO); 049 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 050 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 051 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON); 052 private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO); 053 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 054 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS); 055 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON); 056 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO); 057 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC(); 058 059 private long TEST_TIME_NOW = 060 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 061 062 private long TEST_TIME1 = 063 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 064 + 12L * DateTimeConstants.MILLIS_PER_HOUR 065 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 066 067 private long TEST_TIME2 = 068 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 069 + 14L * DateTimeConstants.MILLIS_PER_HOUR 070 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 071 072 private DateTimeZone zone = null; 073 074 public static void main(String[] args) { 075 junit.textui.TestRunner.run(suite()); 076 } 077 078 public static TestSuite suite() { 079 return new TestSuite(TestYearMonthDay_Basics.class); 080 } 081 082 public TestYearMonthDay_Basics(String name) { 083 super(name); 084 } 085 086 protected void setUp() throws Exception { 087 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 088 zone = DateTimeZone.getDefault(); 089 DateTimeZone.setDefault(LONDON); 090 } 091 092 protected void tearDown() throws Exception { 093 DateTimeUtils.setCurrentMillisSystem(); 094 DateTimeZone.setDefault(zone); 095 zone = null; 096 } 097 098 //----------------------------------------------------------------------- 099 public void testGet() { 100 YearMonthDay test = new YearMonthDay(); 101 assertEquals(1970, test.get(DateTimeFieldType.year())); 102 assertEquals(6, test.get(DateTimeFieldType.monthOfYear())); 103 assertEquals(9, test.get(DateTimeFieldType.dayOfMonth())); 104 try { 105 test.get(null); 106 fail(); 107 } catch (IllegalArgumentException ex) {} 108 try { 109 test.get(DateTimeFieldType.hourOfDay()); 110 fail(); 111 } catch (IllegalArgumentException ex) {} 112 } 113 114 public void testSize() { 115 YearMonthDay test = new YearMonthDay(); 116 assertEquals(3, test.size()); 117 } 118 119 public void testGetFieldType() { 120 YearMonthDay test = new YearMonthDay(COPTIC_PARIS); 121 assertSame(DateTimeFieldType.year(), test.getFieldType(0)); 122 assertSame(DateTimeFieldType.monthOfYear(), test.getFieldType(1)); 123 assertSame(DateTimeFieldType.dayOfMonth(), test.getFieldType(2)); 124 try { 125 test.getFieldType(-1); 126 } catch (IndexOutOfBoundsException ex) {} 127 try { 128 test.getFieldType(3); 129 } catch (IndexOutOfBoundsException ex) {} 130 } 131 132 public void testGetFieldTypes() { 133 YearMonthDay test = new YearMonthDay(COPTIC_PARIS); 134 DateTimeFieldType[] fields = test.getFieldTypes(); 135 assertSame(DateTimeFieldType.year(), fields[0]); 136 assertSame(DateTimeFieldType.monthOfYear(), fields[1]); 137 assertSame(DateTimeFieldType.dayOfMonth(), fields[2]); 138 assertNotSame(test.getFieldTypes(), test.getFieldTypes()); 139 } 140 141 public void testGetField() { 142 YearMonthDay test = new YearMonthDay(COPTIC_PARIS); 143 assertSame(COPTIC_UTC.year(), test.getField(0)); 144 assertSame(COPTIC_UTC.monthOfYear(), test.getField(1)); 145 assertSame(COPTIC_UTC.dayOfMonth(), test.getField(2)); 146 try { 147 test.getField(-1); 148 } catch (IndexOutOfBoundsException ex) {} 149 try { 150 test.getField(3); 151 } catch (IndexOutOfBoundsException ex) {} 152 } 153 154 public void testGetFields() { 155 YearMonthDay test = new YearMonthDay(COPTIC_PARIS); 156 DateTimeField[] fields = test.getFields(); 157 assertSame(COPTIC_UTC.year(), fields[0]); 158 assertSame(COPTIC_UTC.monthOfYear(), fields[1]); 159 assertSame(COPTIC_UTC.dayOfMonth(), fields[2]); 160 assertNotSame(test.getFields(), test.getFields()); 161 } 162 163 public void testGetValue() { 164 YearMonthDay test = new YearMonthDay(); 165 assertEquals(1970, test.getValue(0)); 166 assertEquals(6, test.getValue(1)); 167 assertEquals(9, test.getValue(2)); 168 try { 169 test.getValue(-1); 170 } catch (IndexOutOfBoundsException ex) {} 171 try { 172 test.getValue(3); 173 } catch (IndexOutOfBoundsException ex) {} 174 } 175 176 public void testGetValues() { 177 YearMonthDay test = new YearMonthDay(); 178 int[] values = test.getValues(); 179 assertEquals(1970, values[0]); 180 assertEquals(6, values[1]); 181 assertEquals(9, values[2]); 182 assertNotSame(test.getValues(), test.getValues()); 183 } 184 185 public void testIsSupported() { 186 YearMonthDay test = new YearMonthDay(COPTIC_PARIS); 187 assertEquals(true, test.isSupported(DateTimeFieldType.year())); 188 assertEquals(true, test.isSupported(DateTimeFieldType.monthOfYear())); 189 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfMonth())); 190 assertEquals(false, test.isSupported(DateTimeFieldType.hourOfDay())); 191 } 192 193 public void testEqualsHashCode() { 194 YearMonthDay test1 = new YearMonthDay(1970, 6, 9, COPTIC_PARIS); 195 YearMonthDay test2 = new YearMonthDay(1970, 6, 9, COPTIC_PARIS); 196 assertEquals(true, test1.equals(test2)); 197 assertEquals(true, test2.equals(test1)); 198 assertEquals(true, test1.equals(test1)); 199 assertEquals(true, test2.equals(test2)); 200 assertEquals(true, test1.hashCode() == test2.hashCode()); 201 assertEquals(true, test1.hashCode() == test1.hashCode()); 202 assertEquals(true, test2.hashCode() == test2.hashCode()); 203 204 YearMonthDay test3 = new YearMonthDay(1971, 6, 9); 205 assertEquals(false, test1.equals(test3)); 206 assertEquals(false, test2.equals(test3)); 207 assertEquals(false, test3.equals(test1)); 208 assertEquals(false, test3.equals(test2)); 209 assertEquals(false, test1.hashCode() == test3.hashCode()); 210 assertEquals(false, test2.hashCode() == test3.hashCode()); 211 212 assertEquals(false, test1.equals("Hello")); 213 assertEquals(true, test1.equals(new MockInstant())); 214 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE)); 215 } 216 217 class MockInstant extends MockPartial { 218 public Chronology getChronology() { 219 return COPTIC_UTC; 220 } 221 public DateTimeField[] getFields() { 222 return new DateTimeField[] { 223 COPTIC_UTC.year(), 224 COPTIC_UTC.monthOfYear(), 225 COPTIC_UTC.dayOfMonth(), 226 }; 227 } 228 public int[] getValues() { 229 return new int[] {1970, 6, 9}; 230 } 231 } 232 233 //----------------------------------------------------------------------- 234 public void testCompareTo() { 235 YearMonthDay test1 = new YearMonthDay(2005, 6, 2); 236 YearMonthDay test1a = new YearMonthDay(2005, 6, 2); 237 assertEquals(0, test1.compareTo(test1a)); 238 assertEquals(0, test1a.compareTo(test1)); 239 assertEquals(0, test1.compareTo(test1)); 240 assertEquals(0, test1a.compareTo(test1a)); 241 242 YearMonthDay test2 = new YearMonthDay(2005, 7, 2); 243 assertEquals(-1, test1.compareTo(test2)); 244 assertEquals(+1, test2.compareTo(test1)); 245 246 YearMonthDay test3 = new YearMonthDay(2005, 7, 2, GregorianChronology.getInstanceUTC()); 247 assertEquals(-1, test1.compareTo(test3)); 248 assertEquals(+1, test3.compareTo(test1)); 249 assertEquals(0, test3.compareTo(test2)); 250 251 DateTimeFieldType[] types = new DateTimeFieldType[] { 252 DateTimeFieldType.year(), 253 DateTimeFieldType.monthOfYear(), 254 DateTimeFieldType.dayOfMonth(), 255 }; 256 int[] values = new int[] {2005, 6, 2}; 257 Partial p = new Partial(types, values); 258 assertEquals(0, test1.compareTo(p)); 259 try { 260 test1.compareTo(null); 261 fail(); 262 } catch (NullPointerException ex) {} 263 // try { 264 // test1.compareTo(new Date()); 265 // fail(); 266 // } catch (ClassCastException ex) {} 267 try { 268 test1.compareTo(new TimeOfDay()); 269 fail(); 270 } catch (ClassCastException ex) {} 271 Partial partial = new Partial() 272 .with(DateTimeFieldType.centuryOfEra(), 1) 273 .with(DateTimeFieldType.halfdayOfDay(), 0) 274 .with(DateTimeFieldType.dayOfMonth(), 9); 275 try { 276 new YearMonthDay(1970, 6, 9).compareTo(partial); 277 fail(); 278 } catch (ClassCastException ex) {} 279 } 280 281 //----------------------------------------------------------------------- 282 public void testIsEqual_YMD() { 283 YearMonthDay test1 = new YearMonthDay(2005, 6, 2); 284 YearMonthDay test1a = new YearMonthDay(2005, 6, 2); 285 assertEquals(true, test1.isEqual(test1a)); 286 assertEquals(true, test1a.isEqual(test1)); 287 assertEquals(true, test1.isEqual(test1)); 288 assertEquals(true, test1a.isEqual(test1a)); 289 290 YearMonthDay test2 = new YearMonthDay(2005, 7, 2); 291 assertEquals(false, test1.isEqual(test2)); 292 assertEquals(false, test2.isEqual(test1)); 293 294 YearMonthDay test3 = new YearMonthDay(2005, 7, 2, GregorianChronology.getInstanceUTC()); 295 assertEquals(false, test1.isEqual(test3)); 296 assertEquals(false, test3.isEqual(test1)); 297 assertEquals(true, test3.isEqual(test2)); 298 299 try { 300 new YearMonthDay(2005, 7, 2).isEqual(null); 301 fail(); 302 } catch (IllegalArgumentException ex) {} 303 } 304 305 //----------------------------------------------------------------------- 306 public void testIsBefore_YMD() { 307 YearMonthDay test1 = new YearMonthDay(2005, 6, 2); 308 YearMonthDay test1a = new YearMonthDay(2005, 6, 2); 309 assertEquals(false, test1.isBefore(test1a)); 310 assertEquals(false, test1a.isBefore(test1)); 311 assertEquals(false, test1.isBefore(test1)); 312 assertEquals(false, test1a.isBefore(test1a)); 313 314 YearMonthDay test2 = new YearMonthDay(2005, 7, 2); 315 assertEquals(true, test1.isBefore(test2)); 316 assertEquals(false, test2.isBefore(test1)); 317 318 YearMonthDay test3 = new YearMonthDay(2005, 7, 2, GregorianChronology.getInstanceUTC()); 319 assertEquals(true, test1.isBefore(test3)); 320 assertEquals(false, test3.isBefore(test1)); 321 assertEquals(false, test3.isBefore(test2)); 322 323 try { 324 new YearMonthDay(2005, 7, 2).isBefore(null); 325 fail(); 326 } catch (IllegalArgumentException ex) {} 327 } 328 329 //----------------------------------------------------------------------- 330 public void testIsAfter_YMD() { 331 YearMonthDay test1 = new YearMonthDay(2005, 6, 2); 332 YearMonthDay test1a = new YearMonthDay(2005, 6, 2); 333 assertEquals(false, test1.isAfter(test1a)); 334 assertEquals(false, test1a.isAfter(test1)); 335 assertEquals(false, test1.isAfter(test1)); 336 assertEquals(false, test1a.isAfter(test1a)); 337 338 YearMonthDay test2 = new YearMonthDay(2005, 7, 2); 339 assertEquals(false, test1.isAfter(test2)); 340 assertEquals(true, test2.isAfter(test1)); 341 342 YearMonthDay test3 = new YearMonthDay(2005, 7, 2, GregorianChronology.getInstanceUTC()); 343 assertEquals(false, test1.isAfter(test3)); 344 assertEquals(true, test3.isAfter(test1)); 345 assertEquals(false, test3.isAfter(test2)); 346 347 try { 348 new YearMonthDay(2005, 7, 2).isAfter(null); 349 fail(); 350 } catch (IllegalArgumentException ex) {} 351 } 352 353 //----------------------------------------------------------------------- 354 public void testWithChronologyRetainFields_Chrono() { 355 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 356 YearMonthDay test = base.withChronologyRetainFields(BUDDHIST_TOKYO); 357 check(base, 2005, 6, 9); 358 assertEquals(COPTIC_UTC, base.getChronology()); 359 check(test, 2005, 6, 9); 360 assertEquals(BUDDHIST_UTC, test.getChronology()); 361 } 362 363 public void testWithChronologyRetainFields_sameChrono() { 364 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 365 YearMonthDay test = base.withChronologyRetainFields(COPTIC_TOKYO); 366 assertSame(base, test); 367 } 368 369 public void testWithChronologyRetainFields_nullChrono() { 370 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 371 YearMonthDay test = base.withChronologyRetainFields(null); 372 check(base, 2005, 6, 9); 373 assertEquals(COPTIC_UTC, base.getChronology()); 374 check(test, 2005, 6, 9); 375 assertEquals(ISO_UTC, test.getChronology()); 376 } 377 378 public void testWithChronologyRetainFields_invalidInNewChrono() { 379 YearMonthDay base = new YearMonthDay(2005, 1, 31, ISO_UTC); 380 try { 381 base.withChronologyRetainFields(COPTIC_UTC); 382 fail(); 383 } catch (IllegalArgumentException ex) { 384 // expected 385 } 386 } 387 388 //----------------------------------------------------------------------- 389 public void testWithField1() { 390 YearMonthDay test = new YearMonthDay(2004, 6, 9); 391 YearMonthDay result = test.withField(DateTimeFieldType.year(), 2006); 392 393 assertEquals(new YearMonthDay(2004, 6, 9), test); 394 assertEquals(new YearMonthDay(2006, 6, 9), result); 395 } 396 397 public void testWithField2() { 398 YearMonthDay test = new YearMonthDay(2004, 6, 9); 399 try { 400 test.withField(null, 6); 401 fail(); 402 } catch (IllegalArgumentException ex) {} 403 } 404 405 public void testWithField3() { 406 YearMonthDay test = new YearMonthDay(2004, 6, 9); 407 try { 408 test.withField(DateTimeFieldType.hourOfDay(), 6); 409 fail(); 410 } catch (IllegalArgumentException ex) {} 411 } 412 413 public void testWithField4() { 414 YearMonthDay test = new YearMonthDay(2004, 6, 9); 415 YearMonthDay result = test.withField(DateTimeFieldType.year(), 2004); 416 assertEquals(new YearMonthDay(2004, 6, 9), test); 417 assertSame(test, result); 418 } 419 420 //----------------------------------------------------------------------- 421 public void testWithFieldAdded1() { 422 YearMonthDay test = new YearMonthDay(2004, 6, 9); 423 YearMonthDay result = test.withFieldAdded(DurationFieldType.years(), 6); 424 425 assertEquals(new YearMonthDay(2004, 6, 9), test); 426 assertEquals(new YearMonthDay(2010, 6, 9), result); 427 } 428 429 public void testWithFieldAdded2() { 430 YearMonthDay test = new YearMonthDay(2004, 6, 9); 431 try { 432 test.withFieldAdded(null, 0); 433 fail(); 434 } catch (IllegalArgumentException ex) {} 435 } 436 437 public void testWithFieldAdded3() { 438 YearMonthDay test = new YearMonthDay(2004, 6, 9); 439 try { 440 test.withFieldAdded(null, 6); 441 fail(); 442 } catch (IllegalArgumentException ex) {} 443 } 444 445 public void testWithFieldAdded4() { 446 YearMonthDay test = new YearMonthDay(2004, 6, 9); 447 YearMonthDay result = test.withFieldAdded(DurationFieldType.years(), 0); 448 assertSame(test, result); 449 } 450 451 public void testWithFieldAdded5() { 452 YearMonthDay test = new YearMonthDay(2004, 6, 9); 453 try { 454 test.withFieldAdded(DurationFieldType.hours(), 6); 455 fail(); 456 } catch (IllegalArgumentException ex) {} 457 } 458 459 //----------------------------------------------------------------------- 460 public void testPlus_RP() { 461 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 462 YearMonthDay result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7, 8)); 463 YearMonthDay expected = new YearMonthDay(2003, 7, 7, BuddhistChronology.getInstance()); 464 assertEquals(expected, result); 465 466 result = test.plus((ReadablePeriod) null); 467 assertSame(test, result); 468 } 469 470 public void testPlusYears_int() { 471 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 472 YearMonthDay result = test.plusYears(1); 473 YearMonthDay expected = new YearMonthDay(2003, 5, 3, BuddhistChronology.getInstance()); 474 assertEquals(expected, result); 475 476 result = test.plusYears(0); 477 assertSame(test, result); 478 } 479 480 public void testPlusMonths_int() { 481 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 482 YearMonthDay result = test.plusMonths(1); 483 YearMonthDay expected = new YearMonthDay(2002, 6, 3, BuddhistChronology.getInstance()); 484 assertEquals(expected, result); 485 486 result = test.plusMonths(0); 487 assertSame(test, result); 488 } 489 490 public void testPlusDays_int() { 491 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 492 YearMonthDay result = test.plusDays(1); 493 YearMonthDay expected = new YearMonthDay(2002, 5, 4, BuddhistChronology.getInstance()); 494 assertEquals(expected, result); 495 496 result = test.plusDays(0); 497 assertSame(test, result); 498 } 499 500 //----------------------------------------------------------------------- 501 public void testMinus_RP() { 502 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 503 YearMonthDay result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1)); 504 YearMonthDay expected = new YearMonthDay(2001, 4, 2, BuddhistChronology.getInstance()); 505 assertEquals(expected, result); 506 507 result = test.minus((ReadablePeriod) null); 508 assertSame(test, result); 509 } 510 511 public void testMinusYears_int() { 512 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 513 YearMonthDay result = test.minusYears(1); 514 YearMonthDay expected = new YearMonthDay(2001, 5, 3, BuddhistChronology.getInstance()); 515 assertEquals(expected, result); 516 517 result = test.minusYears(0); 518 assertSame(test, result); 519 } 520 521 public void testMinusMonths_int() { 522 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 523 YearMonthDay result = test.minusMonths(1); 524 YearMonthDay expected = new YearMonthDay(2002, 4, 3, BuddhistChronology.getInstance()); 525 assertEquals(expected, result); 526 527 result = test.minusMonths(0); 528 assertSame(test, result); 529 } 530 531 public void testMinusDays_int() { 532 YearMonthDay test = new YearMonthDay(2002, 5, 3, BuddhistChronology.getInstance()); 533 YearMonthDay result = test.minusDays(1); 534 YearMonthDay expected = new YearMonthDay(2002, 5, 2, BuddhistChronology.getInstance()); 535 assertEquals(expected, result); 536 537 result = test.minusDays(0); 538 assertSame(test, result); 539 } 540 541 //----------------------------------------------------------------------- 542 public void testToLocalDate() { 543 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_UTC); 544 LocalDate test = base.toLocalDate(); 545 assertEquals(new LocalDate(2005, 6, 9, COPTIC_UTC), test); 546 } 547 548 //----------------------------------------------------------------------- 549 public void testToDateTimeAtMidnight() { 550 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 551 552 DateTime test = base.toDateTimeAtMidnight(); 553 check(base, 2005, 6, 9); 554 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test); 555 } 556 557 //----------------------------------------------------------------------- 558 public void testToDateTimeAtMidnight_Zone() { 559 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 560 561 DateTime test = base.toDateTimeAtMidnight(TOKYO); 562 check(base, 2005, 6, 9); 563 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_TOKYO), test); 564 } 565 566 public void testToDateTimeAtMidnight_nullZone() { 567 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 568 569 DateTime test = base.toDateTimeAtMidnight((DateTimeZone) null); 570 check(base, 2005, 6, 9); 571 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test); 572 } 573 574 //----------------------------------------------------------------------- 575 public void testToDateTimeAtCurrentTime() { 576 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 577 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 578 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 579 580 DateTime test = base.toDateTimeAtCurrentTime(); 581 check(base, 2005, 6, 9); 582 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 583 expected = expected.year().setCopy(2005); 584 expected = expected.monthOfYear().setCopy(6); 585 expected = expected.dayOfMonth().setCopy(9); 586 assertEquals(expected, test); 587 } 588 589 //----------------------------------------------------------------------- 590 public void testToDateTimeAtCurrentTime_Zone() { 591 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 592 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 593 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 594 595 DateTime test = base.toDateTimeAtCurrentTime(TOKYO); 596 check(base, 2005, 6, 9); 597 DateTime expected = new DateTime(dt.getMillis(), COPTIC_TOKYO); 598 expected = expected.year().setCopy(2005); 599 expected = expected.monthOfYear().setCopy(6); 600 expected = expected.dayOfMonth().setCopy(9); 601 assertEquals(expected, test); 602 } 603 604 public void testToDateTimeAtCurrentTime_nullZone() { 605 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 606 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 607 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 608 609 DateTime test = base.toDateTimeAtCurrentTime((DateTimeZone) null); 610 check(base, 2005, 6, 9); 611 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 612 expected = expected.year().setCopy(2005); 613 expected = expected.monthOfYear().setCopy(6); 614 expected = expected.dayOfMonth().setCopy(9); 615 assertEquals(expected, test); 616 } 617 618 //----------------------------------------------------------------------- 619 public void testToDateTime_TOD() { 620 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 621 TimeOfDay tod = new TimeOfDay(12, 13, 14, 15, BUDDHIST_TOKYO); 622 623 DateTime test = base.toDateTime(tod); 624 check(base, 2005, 6, 9); 625 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 626 assertEquals(expected, test); 627 } 628 629 public void testToDateTime_nullTOD() { 630 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 631 long now = new DateTime(2004, 5, 8, 12, 13, 14, 15, COPTIC_LONDON).getMillis(); 632 DateTimeUtils.setCurrentMillisFixed(now); 633 634 DateTime test = base.toDateTime((TimeOfDay) null); 635 check(base, 2005, 6, 9); 636 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 637 assertEquals(expected, test); 638 } 639 640 //----------------------------------------------------------------------- 641 public void testToDateTime_TOD_Zone() { 642 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 643 TimeOfDay tod = new TimeOfDay(12, 13, 14, 15, BUDDHIST_TOKYO); 644 645 DateTime test = base.toDateTime(tod, TOKYO); 646 check(base, 2005, 6, 9); 647 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_TOKYO); 648 assertEquals(expected, test); 649 } 650 651 public void testToDateTime_TOD_nullZone() { 652 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 653 TimeOfDay tod = new TimeOfDay(12, 13, 14, 15, BUDDHIST_TOKYO); 654 655 DateTime test = base.toDateTime(tod, null); 656 check(base, 2005, 6, 9); 657 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 658 assertEquals(expected, test); 659 } 660 661 public void testToDateTime_nullTOD_Zone() { 662 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 663 long now = new DateTime(2004, 5, 8, 12, 13, 14, 15, COPTIC_TOKYO).getMillis(); 664 DateTimeUtils.setCurrentMillisFixed(now); 665 666 DateTime test = base.toDateTime((TimeOfDay) null, TOKYO); 667 check(base, 2005, 6, 9); 668 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_TOKYO); 669 assertEquals(expected, test); 670 } 671 672 //----------------------------------------------------------------------- 673 public void testToDateMidnight() { 674 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 675 676 DateMidnight test = base.toDateMidnight(); 677 check(base, 2005, 6, 9); 678 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test); 679 } 680 681 //----------------------------------------------------------------------- 682 public void testToDateMidnight_Zone() { 683 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 684 685 DateMidnight test = base.toDateMidnight(TOKYO); 686 check(base, 2005, 6, 9); 687 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_TOKYO), test); 688 } 689 690 public void testToDateMidnight_nullZone() { 691 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 692 693 DateMidnight test = base.toDateMidnight((DateTimeZone) null); 694 check(base, 2005, 6, 9); 695 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test); 696 } 697 698 //----------------------------------------------------------------------- 699 public void testToDateTime_RI() { 700 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); 701 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7); 702 703 DateTime test = base.toDateTime(dt); 704 check(base, 2005, 6, 9); 705 DateTime expected = dt; 706 expected = expected.year().setCopy(2005); 707 expected = expected.monthOfYear().setCopy(6); 708 expected = expected.dayOfMonth().setCopy(9); 709 assertEquals(expected, test); 710 } 711 712 public void testToDateTime_nullRI() { 713 YearMonthDay base = new YearMonthDay(2005, 6, 9); 714 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7); 715 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 716 717 DateTime test = base.toDateTime((ReadableInstant) null); 718 check(base, 2005, 6, 9); 719 DateTime expected = dt; 720 expected = expected.year().setCopy(2005); 721 expected = expected.monthOfYear().setCopy(6); 722 expected = expected.dayOfMonth().setCopy(9); 723 assertEquals(expected, test); 724 } 725 726 //----------------------------------------------------------------------- 727 public void testToInterval() { 728 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 729 Interval test = base.toInterval(); 730 check(base, 2005, 6, 9); 731 DateTime start = base.toDateTime(TimeOfDay.MIDNIGHT); 732 DateTime end = start.plus(Period.days(1)); 733 Interval expected = new Interval(start, end); 734 assertEquals(expected, test); 735 } 736 737 //----------------------------------------------------------------------- 738 public void testToInterval_Zone() { 739 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 740 Interval test = base.toInterval(TOKYO); 741 check(base, 2005, 6, 9); 742 DateTime start = base.toDateTime(TimeOfDay.MIDNIGHT, TOKYO); 743 DateTime end = start.plus(Period.days(1)); 744 Interval expected = new Interval(start, end); 745 assertEquals(expected, test); 746 } 747 748 public void testToInterval_nullZone() { 749 YearMonthDay base = new YearMonthDay(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 750 Interval test = base.toInterval(null); 751 check(base, 2005, 6, 9); 752 DateTime start = base.toDateTime(TimeOfDay.MIDNIGHT, LONDON); 753 DateTime end = start.plus(Period.days(1)); 754 Interval expected = new Interval(start, end); 755 assertEquals(expected, test); 756 } 757 758 //----------------------------------------------------------------------- 759 public void testWithers() { 760 YearMonthDay test = new YearMonthDay(1970, 6, 9); 761 check(test.withYear(2000), 2000, 6, 9); 762 check(test.withMonthOfYear(2), 1970, 2, 9); 763 check(test.withDayOfMonth(2), 1970, 6, 2); 764 try { 765 test.withMonthOfYear(0); 766 fail(); 767 } catch (IllegalArgumentException ex) {} 768 try { 769 test.withMonthOfYear(13); 770 fail(); 771 } catch (IllegalArgumentException ex) {} 772 } 773 774 //----------------------------------------------------------------------- 775 public void testProperty() { 776 YearMonthDay test = new YearMonthDay(2005, 6, 9); 777 assertEquals(test.year(), test.property(DateTimeFieldType.year())); 778 assertEquals(test.monthOfYear(), test.property(DateTimeFieldType.monthOfYear())); 779 assertEquals(test.dayOfMonth(), test.property(DateTimeFieldType.dayOfMonth())); 780 try { 781 test.property(DateTimeFieldType.millisOfDay()); 782 fail(); 783 } catch (IllegalArgumentException ex) {} 784 try { 785 test.property(null); 786 fail(); 787 } catch (IllegalArgumentException ex) {} 788 } 789 790 //----------------------------------------------------------------------- 791 public void testSerialization() throws Exception { 792 YearMonthDay test = new YearMonthDay(1972, 6, 9, COPTIC_PARIS); 793 794 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 795 ObjectOutputStream oos = new ObjectOutputStream(baos); 796 oos.writeObject(test); 797 byte[] bytes = baos.toByteArray(); 798 oos.close(); 799 800 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 801 ObjectInputStream ois = new ObjectInputStream(bais); 802 YearMonthDay result = (YearMonthDay) ois.readObject(); 803 ois.close(); 804 805 assertEquals(test, result); 806 assertTrue(Arrays.equals(test.getValues(), result.getValues())); 807 assertTrue(Arrays.equals(test.getFields(), result.getFields())); 808 assertEquals(test.getChronology(), result.getChronology()); 809 } 810 811 //----------------------------------------------------------------------- 812 public void testToString() { 813 YearMonthDay test = new YearMonthDay(2002, 6, 9); 814 assertEquals("2002-06-09", test.toString()); 815 } 816 817 //----------------------------------------------------------------------- 818 public void testToString_String() { 819 YearMonthDay test = new YearMonthDay(2002, 6, 9); 820 assertEquals("2002 \ufffd\ufffd", test.toString("yyyy HH")); 821 assertEquals("2002-06-09", test.toString((String) null)); 822 } 823 824 //----------------------------------------------------------------------- 825 public void testToString_String_Locale() { 826 YearMonthDay test = new YearMonthDay(2002, 6, 9); 827 assertEquals("\ufffd 9/6", test.toString("EEE d/M", Locale.ENGLISH)); 828 assertEquals("\ufffd 9/6", test.toString("EEE d/M", Locale.FRENCH)); 829 assertEquals("2002-06-09", test.toString(null, Locale.ENGLISH)); 830 assertEquals("\ufffd 9/6", test.toString("EEE d/M", null)); 831 assertEquals("2002-06-09", test.toString(null, null)); 832 } 833 834 //----------------------------------------------------------------------- 835 public void testToString_DTFormatter() { 836 YearMonthDay test = new YearMonthDay(2002, 6, 9); 837 assertEquals("2002 \ufffd\ufffd", test.toString(DateTimeFormat.forPattern("yyyy HH"))); 838 assertEquals("2002-06-09", test.toString((DateTimeFormatter) null)); 839 } 840 841 //----------------------------------------------------------------------- 842 private void check(YearMonthDay test, int hour, int min, int sec) { 843 assertEquals(hour, test.getYear()); 844 assertEquals(min, test.getMonthOfYear()); 845 assertEquals(sec, test.getDayOfMonth()); 846 } 847 }