001 /* 002 * Copyright 2001-2010 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 MonthDay. Based on {@link TestYearMonth_Basics} 037 */ 038 public class TestMonthDay_Basics extends TestCase { 039 040 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 041 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 042 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 043 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS); 044 // private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON); 045 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO); 046 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 047 // private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 048 // private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON); 049 // private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO); 050 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 051 // private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS); 052 // private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON); 053 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO); 054 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC(); 055 056 private long TEST_TIME_NOW = 057 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 058 059 private DateTimeZone zone = null; 060 061 public static void main(String[] args) { 062 junit.textui.TestRunner.run(suite()); 063 } 064 065 public static TestSuite suite() { 066 return new TestSuite(TestMonthDay_Basics.class); 067 } 068 069 public TestMonthDay_Basics(String name) { 070 super(name); 071 } 072 073 protected void setUp() throws Exception { 074 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 075 zone = DateTimeZone.getDefault(); 076 DateTimeZone.setDefault(LONDON); 077 } 078 079 protected void tearDown() throws Exception { 080 DateTimeUtils.setCurrentMillisSystem(); 081 DateTimeZone.setDefault(zone); 082 zone = null; 083 } 084 085 //----------------------------------------------------------------------- 086 public void testGet() { 087 MonthDay test = new MonthDay(); 088 assertEquals(6, test.get(DateTimeFieldType.monthOfYear())); 089 assertEquals(9, test.get(DateTimeFieldType.dayOfMonth())); 090 try { 091 test.get(null); 092 fail(); 093 } catch (IllegalArgumentException ex) {} 094 try { 095 test.get(DateTimeFieldType.year()); 096 fail(); 097 } catch (IllegalArgumentException ex) {} 098 } 099 100 public void testSize() { 101 MonthDay test = new MonthDay(); 102 assertEquals(2, test.size()); 103 } 104 105 public void testGetFieldType() { 106 MonthDay test = new MonthDay(COPTIC_PARIS); 107 assertSame(DateTimeFieldType.monthOfYear(), test.getFieldType(0)); 108 assertSame(DateTimeFieldType.dayOfMonth(), test.getFieldType(1)); 109 110 try { 111 test.getFieldType(-1); 112 } catch (IndexOutOfBoundsException ex) {} 113 try { 114 test.getFieldType(2); 115 } catch (IndexOutOfBoundsException ex) {} 116 } 117 118 public void testGetFieldTypes() { 119 MonthDay test = new MonthDay(COPTIC_PARIS); 120 DateTimeFieldType[] fields = test.getFieldTypes(); 121 assertEquals(2, fields.length); 122 assertSame(DateTimeFieldType.monthOfYear(), fields[0]); 123 assertSame(DateTimeFieldType.dayOfMonth(), fields[1]); 124 assertNotSame(test.getFieldTypes(), test.getFieldTypes()); 125 } 126 127 public void testGetField() { 128 MonthDay test = new MonthDay(COPTIC_PARIS); 129 assertSame(COPTIC_UTC.monthOfYear(), test.getField(0)); 130 assertSame(COPTIC_UTC.dayOfMonth(), test.getField(1)); 131 try { 132 test.getField(-1); 133 } catch (IndexOutOfBoundsException ex) {} 134 try { 135 test.getField(2); 136 } catch (IndexOutOfBoundsException ex) {} 137 } 138 139 public void testGetFields() { 140 MonthDay test = new MonthDay(COPTIC_PARIS); 141 DateTimeField[] fields = test.getFields(); 142 assertEquals(2, fields.length); 143 assertSame(COPTIC_UTC.monthOfYear(), fields[0]); 144 assertSame(COPTIC_UTC.dayOfMonth(), fields[1]); 145 assertNotSame(test.getFields(), test.getFields()); 146 } 147 148 public void testGetValue() { 149 MonthDay test = new MonthDay(); 150 assertEquals(6, test.getValue(0)); 151 assertEquals(9, test.getValue(1)); 152 try { 153 test.getValue(-1); 154 } catch (IndexOutOfBoundsException ex) {} 155 try { 156 test.getValue(2); 157 } catch (IndexOutOfBoundsException ex) {} 158 } 159 160 public void testGetValues() { 161 MonthDay test = new MonthDay(); 162 int[] values = test.getValues(); 163 assertEquals(2, values.length); 164 assertEquals(6, values[0]); 165 assertEquals(9, values[1]); 166 assertNotSame(test.getValues(), test.getValues()); 167 } 168 169 public void testIsSupported() { 170 MonthDay test = new MonthDay(COPTIC_PARIS); 171 assertEquals(false, test.isSupported(DateTimeFieldType.year())); 172 assertEquals(true, test.isSupported(DateTimeFieldType.monthOfYear())); 173 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfMonth())); 174 assertEquals(false, test.isSupported(DateTimeFieldType.hourOfDay())); 175 } 176 177 public void testEqualsHashCode() { 178 MonthDay test1 = new MonthDay(10, 6, COPTIC_PARIS); 179 MonthDay test2 = new MonthDay(10, 6, COPTIC_PARIS); 180 assertEquals(true, test1.equals(test2)); 181 assertEquals(true, test2.equals(test1)); 182 assertEquals(true, test1.equals(test1)); 183 assertEquals(true, test2.equals(test2)); 184 assertEquals(true, test1.hashCode() == test2.hashCode()); 185 assertEquals(true, test1.hashCode() == test1.hashCode()); 186 assertEquals(true, test2.hashCode() == test2.hashCode()); 187 188 MonthDay test3 = new MonthDay(10, 6); 189 assertEquals(false, test1.equals(test3)); 190 assertEquals(false, test2.equals(test3)); 191 assertEquals(false, test3.equals(test1)); 192 assertEquals(false, test3.equals(test2)); 193 assertEquals(false, test1.hashCode() == test3.hashCode()); 194 assertEquals(false, test2.hashCode() == test3.hashCode()); 195 196 assertEquals(false, test1.equals("Hello")); 197 assertEquals(true, test1.equals(new MockMD())); 198 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE)); 199 } 200 201 class MockMD extends MockPartial { 202 203 @Override 204 public Chronology getChronology() { 205 return COPTIC_UTC; 206 } 207 208 @Override 209 public DateTimeField[] getFields() { 210 return new DateTimeField[] { 211 COPTIC_UTC.monthOfYear(), 212 COPTIC_UTC.dayOfMonth() 213 }; 214 } 215 216 @Override 217 public int[] getValues() { 218 return new int[] {10, 6}; 219 } 220 } 221 222 //----------------------------------------------------------------------- 223 public void testCompareTo() { 224 MonthDay test1 = new MonthDay(6, 6); 225 MonthDay test1a = new MonthDay(6, 6); 226 assertEquals(0, test1.compareTo(test1a)); 227 assertEquals(0, test1a.compareTo(test1)); 228 assertEquals(0, test1.compareTo(test1)); 229 assertEquals(0, test1a.compareTo(test1a)); 230 231 MonthDay test2 = new MonthDay(6, 7); 232 assertEquals(-1, test1.compareTo(test2)); 233 assertEquals(+1, test2.compareTo(test1)); 234 235 MonthDay test3 = new MonthDay(6, 7, GregorianChronology.getInstanceUTC()); 236 assertEquals(-1, test1.compareTo(test3)); 237 assertEquals(+1, test3.compareTo(test1)); 238 assertEquals(0, test3.compareTo(test2)); 239 240 DateTimeFieldType[] types = new DateTimeFieldType[] { 241 DateTimeFieldType.monthOfYear(), 242 DateTimeFieldType.dayOfMonth() 243 }; 244 int[] values = new int[] {6, 6}; 245 Partial p = new Partial(types, values); 246 assertEquals(0, test1.compareTo(p)); 247 try { 248 test1.compareTo(null); 249 fail(); 250 } catch (NullPointerException ex) {} 251 try { 252 test1.compareTo(new LocalTime()); 253 fail(); 254 } catch (ClassCastException ex) {} 255 Partial partial = new Partial() 256 .with(DateTimeFieldType.centuryOfEra(), 1) 257 .with(DateTimeFieldType.halfdayOfDay(), 0) 258 .with(DateTimeFieldType.dayOfMonth(), 9); 259 try { 260 new MonthDay(10, 6).compareTo(partial); 261 fail(); 262 } catch (ClassCastException ex) {} 263 } 264 265 //----------------------------------------------------------------------- 266 public void testIsEqual_MD() { 267 MonthDay test1 = new MonthDay(6, 6); 268 MonthDay test1a = new MonthDay(6, 6); 269 assertEquals(true, test1.isEqual(test1a)); 270 assertEquals(true, test1a.isEqual(test1)); 271 assertEquals(true, test1.isEqual(test1)); 272 assertEquals(true, test1a.isEqual(test1a)); 273 274 MonthDay test2 = new MonthDay(6, 7); 275 assertEquals(false, test1.isEqual(test2)); 276 assertEquals(false, test2.isEqual(test1)); 277 278 MonthDay test3 = new MonthDay(6, 7, GregorianChronology.getInstanceUTC()); 279 assertEquals(false, test1.isEqual(test3)); 280 assertEquals(false, test3.isEqual(test1)); 281 assertEquals(true, test3.isEqual(test2)); 282 283 try { 284 new MonthDay(6, 7).isEqual(null); 285 fail(); 286 } catch (IllegalArgumentException ex) {} 287 } 288 289 //----------------------------------------------------------------------- 290 public void testIsBefore_MD() { 291 MonthDay test1 = new MonthDay(6, 6); 292 MonthDay test1a = new MonthDay(6, 6); 293 assertEquals(false, test1.isBefore(test1a)); 294 assertEquals(false, test1a.isBefore(test1)); 295 assertEquals(false, test1.isBefore(test1)); 296 assertEquals(false, test1a.isBefore(test1a)); 297 298 MonthDay test2 = new MonthDay(6, 7); 299 assertEquals(true, test1.isBefore(test2)); 300 assertEquals(false, test2.isBefore(test1)); 301 302 MonthDay test3 = new MonthDay(6, 7, GregorianChronology.getInstanceUTC()); 303 assertEquals(true, test1.isBefore(test3)); 304 assertEquals(false, test3.isBefore(test1)); 305 assertEquals(false, test3.isBefore(test2)); 306 307 try { 308 new MonthDay(6, 7).isBefore(null); 309 fail(); 310 } catch (IllegalArgumentException ex) {} 311 } 312 313 //----------------------------------------------------------------------- 314 public void testIsAfter_MD() { 315 MonthDay test1 = new MonthDay(6, 6); 316 MonthDay test1a = new MonthDay(6, 6); 317 assertEquals(false, test1.isAfter(test1a)); 318 assertEquals(false, test1a.isAfter(test1)); 319 assertEquals(false, test1.isAfter(test1)); 320 assertEquals(false, test1a.isAfter(test1a)); 321 322 MonthDay test2 = new MonthDay(6, 7); 323 assertEquals(false, test1.isAfter(test2)); 324 assertEquals(true, test2.isAfter(test1)); 325 326 MonthDay test3 = new MonthDay(6, 7, GregorianChronology.getInstanceUTC()); 327 assertEquals(false, test1.isAfter(test3)); 328 assertEquals(true, test3.isAfter(test1)); 329 assertEquals(false, test3.isAfter(test2)); 330 331 try { 332 new MonthDay(6, 7).isAfter(null); 333 fail(); 334 } catch (IllegalArgumentException ex) {} 335 } 336 337 //----------------------------------------------------------------------- 338 public void testWithChronologyRetainFields_Chrono() { 339 MonthDay base = new MonthDay(6, 6, COPTIC_PARIS); 340 MonthDay test = base.withChronologyRetainFields(BUDDHIST_TOKYO); 341 check(base, 6, 6); 342 assertEquals(COPTIC_UTC, base.getChronology()); 343 check(test, 6, 6); 344 assertEquals(BUDDHIST_UTC, test.getChronology()); 345 } 346 347 public void testWithChronologyRetainFields_sameChrono() { 348 MonthDay base = new MonthDay(6, 6, COPTIC_PARIS); 349 MonthDay test = base.withChronologyRetainFields(COPTIC_TOKYO); 350 assertSame(base, test); 351 } 352 353 public void testWithChronologyRetainFields_nullChrono() { 354 MonthDay base = new MonthDay(6, 6, COPTIC_PARIS); 355 MonthDay test = base.withChronologyRetainFields(null); 356 check(base, 6, 6); 357 assertEquals(COPTIC_UTC, base.getChronology()); 358 check(test, 6, 6); 359 assertEquals(ISO_UTC, test.getChronology()); 360 } 361 362 //----------------------------------------------------------------------- 363 public void testWithField() { 364 MonthDay test = new MonthDay(9, 6); 365 MonthDay result = test.withField(DateTimeFieldType.monthOfYear(), 10); 366 367 assertEquals(new MonthDay(9, 6), test); 368 assertEquals(new MonthDay(10, 6), result); 369 } 370 371 public void testWithField_nullField() { 372 MonthDay test = new MonthDay(9, 6); 373 try { 374 test.withField(null, 6); 375 fail(); 376 } catch (IllegalArgumentException ex) {} 377 } 378 379 public void testWithField_unknownField() { 380 MonthDay test = new MonthDay(9, 6); 381 try { 382 test.withField(DateTimeFieldType.hourOfDay(), 6); 383 fail(); 384 } catch (IllegalArgumentException ex) {} 385 } 386 387 public void testWithField_same() { 388 MonthDay test = new MonthDay(9, 6); 389 MonthDay result = test.withField(DateTimeFieldType.monthOfYear(), 9); 390 assertEquals(new MonthDay(9, 6), test); 391 assertSame(test, result); 392 } 393 394 //----------------------------------------------------------------------- 395 public void testWithFieldAdded() { 396 MonthDay test = new MonthDay(9, 6); 397 MonthDay result = test.withFieldAdded(DurationFieldType.months(), 1); 398 399 assertEquals(new MonthDay(9, 6), test); 400 assertEquals(new MonthDay(10, 6), result); 401 } 402 403 public void testWithFieldAdded_nullField_zero() { 404 MonthDay test = new MonthDay(9, 6); 405 try { 406 test.withFieldAdded(null, 0); 407 fail(); 408 } catch (IllegalArgumentException ex) {} 409 } 410 411 public void testWithFieldAdded_nullField_nonZero() { 412 MonthDay test = new MonthDay(9, 6); 413 try { 414 test.withFieldAdded(null, 6); 415 fail(); 416 } catch (IllegalArgumentException ex) {} 417 } 418 419 public void testWithFieldAdded_zero() { 420 MonthDay test = new MonthDay(9, 6); 421 MonthDay result = test.withFieldAdded(DurationFieldType.months(), 0); 422 assertSame(test, result); 423 } 424 425 public void testWithFieldAdded_unknownField() { 426 MonthDay test = new MonthDay(9, 6); 427 try { 428 test.withFieldAdded(DurationFieldType.hours(), 6); 429 fail(); 430 } catch (IllegalArgumentException ex) {} 431 } 432 433 //----------------------------------------------------------------------- 434 public void testPlus_RP() { 435 MonthDay test = new MonthDay(6, 5, BuddhistChronology.getInstance()); 436 MonthDay result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7, 8)); 437 MonthDay expected = new MonthDay(8, 9, BuddhistChronology.getInstance()); 438 assertEquals(expected, result); 439 440 result = test.plus((ReadablePeriod) null); 441 assertSame(test, result); 442 } 443 444 public void testPlusMonths_int() { 445 MonthDay test = new MonthDay(6, 5, BuddhistChronology.getInstance()); 446 MonthDay result = test.plusMonths(1); 447 MonthDay expected = new MonthDay(7, 5, BuddhistChronology.getInstance()); 448 assertEquals(expected, result); 449 } 450 451 public void testPlusMonths_int_fromLeap() { 452 MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC()); 453 MonthDay result = test.plusMonths(1); 454 MonthDay expected = new MonthDay(3, 29, ISOChronology.getInstance()); 455 assertEquals(expected, result); 456 } 457 458 public void testPlusMonths_int_negativeFromLeap() { 459 MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC()); 460 MonthDay result = test.plusMonths(-1); 461 MonthDay expected = new MonthDay(1, 29, ISOChronology.getInstance()); 462 assertEquals(expected, result); 463 } 464 465 public void testPlusMonths_int_endOfMonthAdjust() { 466 MonthDay test = new MonthDay(3, 31, ISOChronology.getInstanceUTC()); 467 MonthDay result = test.plusMonths(1); 468 MonthDay expected = new MonthDay(4, 30, ISOChronology.getInstance()); 469 assertEquals(expected, result); 470 } 471 472 public void testPlusMonths_int_negativeEndOfMonthAdjust() { 473 MonthDay test = new MonthDay(3, 31, ISOChronology.getInstanceUTC()); 474 MonthDay result = test.plusMonths(-1); 475 MonthDay expected = new MonthDay(2, 29, ISOChronology.getInstance()); 476 assertEquals(expected, result); 477 } 478 479 public void testPlusMonths_int_same() { 480 MonthDay test = new MonthDay(6, 5, ISO_UTC); 481 MonthDay result = test.plusMonths(0); 482 assertSame(test, result); 483 } 484 485 public void testPlusMonths_int_wrap() { 486 MonthDay test = new MonthDay(6, 5, ISO_UTC); 487 MonthDay result = test.plusMonths(10); 488 MonthDay expected = new MonthDay(4, 5, ISO_UTC); 489 assertEquals(expected, result); 490 } 491 492 public void testPlusMonths_int_adjust() { 493 MonthDay test = new MonthDay(7, 31, ISO_UTC); 494 MonthDay result = test.plusMonths(2); 495 MonthDay expected = new MonthDay(9, 30, ISO_UTC); 496 assertEquals(expected, result); 497 } 498 499 //------------------------------------------------------------------------- 500 public void testPlusDays_int() { 501 MonthDay test = new MonthDay(5, 10, BuddhistChronology.getInstance()); 502 MonthDay result = test.plusDays(1); 503 MonthDay expected = new MonthDay(5, 11, BuddhistChronology.getInstance()); 504 assertEquals(expected, result); 505 } 506 507 public void testPlusDays_int_fromLeap() { 508 MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC()); 509 MonthDay result = test.plusDays(1); 510 MonthDay expected = new MonthDay(3, 1, ISOChronology.getInstance()); 511 assertEquals(expected, result); 512 } 513 514 public void testPlusDays_int_negativeFromLeap() { 515 MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC()); 516 MonthDay result = test.plusDays(-1); 517 MonthDay expected = new MonthDay(2, 28, ISOChronology.getInstance()); 518 assertEquals(expected, result); 519 } 520 521 public void testPlusDays_same() { 522 MonthDay test = new MonthDay(5, 10, BuddhistChronology.getInstance()); 523 MonthDay result = test.plusDays(0); 524 assertSame(test, result); 525 } 526 527 //----------------------------------------------------------------------- 528 public void testMinus_RP() { 529 MonthDay test = new MonthDay(6, 5, BuddhistChronology.getInstance()); 530 MonthDay result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1)); 531 MonthDay expected = new MonthDay(5, 4, BuddhistChronology.getInstance()); 532 assertEquals(expected, result); 533 534 result = test.minus((ReadablePeriod) null); 535 assertSame(test, result); 536 } 537 538 public void testMinusMonths_int() { 539 MonthDay test = new MonthDay(6, 5, BuddhistChronology.getInstance()); 540 MonthDay result = test.minusMonths(1); 541 MonthDay expected = new MonthDay(5, 5, BuddhistChronology.getInstance()); 542 assertEquals(expected, result); 543 } 544 545 public void testMinusMonths_int_fromLeap() { 546 MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC()); 547 MonthDay result = test.minusMonths(1); 548 MonthDay expected = new MonthDay(1, 29, ISOChronology.getInstance()); 549 assertEquals(expected, result); 550 } 551 552 public void testMinusMonths_int_negativeFromLeap() { 553 MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC()); 554 MonthDay result = test.minusMonths(-1); 555 MonthDay expected = new MonthDay(3, 29, ISOChronology.getInstance()); 556 assertEquals(expected, result); 557 } 558 559 public void testMinusMonths_int_endOfMonthAdjust() { 560 MonthDay test = new MonthDay(3, 31, ISOChronology.getInstanceUTC()); 561 MonthDay result = test.minusMonths(1); 562 MonthDay expected = new MonthDay(2, 29, ISOChronology.getInstance()); 563 assertEquals(expected, result); 564 } 565 566 public void testMinusMonths_int_negativeEndOfMonthAdjust() { 567 MonthDay test = new MonthDay(3, 31, ISOChronology.getInstanceUTC()); 568 MonthDay result = test.minusMonths(-1); 569 MonthDay expected = new MonthDay(4, 30, ISOChronology.getInstance()); 570 assertEquals(expected, result); 571 } 572 573 public void testMinusMonths_int_same() { 574 MonthDay test = new MonthDay(6, 5, ISO_UTC); 575 MonthDay result = test.minusMonths(0); 576 assertSame(test, result); 577 } 578 579 public void testMinusMonths_int_wrap() { 580 MonthDay test = new MonthDay(6, 5, ISO_UTC); 581 MonthDay result = test.minusMonths(10); 582 MonthDay expected = new MonthDay(8, 5, ISO_UTC); 583 assertEquals(expected, result); 584 } 585 586 public void testMinusMonths_int_adjust() { 587 MonthDay test = new MonthDay(7, 31, ISO_UTC); 588 MonthDay result = test.minusMonths(3); 589 MonthDay expected = new MonthDay(4, 30, ISO_UTC); 590 assertEquals(expected, result); 591 } 592 593 //------------------------------------------------------------------------- 594 public void testMinusDays_int() { 595 MonthDay test = new MonthDay(5, 11, BuddhistChronology.getInstance()); 596 MonthDay result = test.minusDays(1); 597 MonthDay expected = new MonthDay(5, 10, BuddhistChronology.getInstance()); 598 assertEquals(expected, result); 599 } 600 601 public void testMinusDays_int_fromLeap() { 602 MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC()); 603 MonthDay result = test.minusDays(1); 604 MonthDay expected = new MonthDay(2, 28, ISOChronology.getInstance()); 605 assertEquals(expected, result); 606 } 607 608 public void testMinusDays_int_negativeFromLeap() { 609 MonthDay test = new MonthDay(2, 29, ISOChronology.getInstanceUTC()); 610 MonthDay result = test.minusDays(-1); 611 MonthDay expected = new MonthDay(3, 1, ISOChronology.getInstance()); 612 assertEquals(expected, result); 613 } 614 615 public void testMinusDays_same() { 616 MonthDay test = new MonthDay(5, 11, BuddhistChronology.getInstance()); 617 MonthDay result = test.minusDays(0); 618 assertSame(test, result); 619 } 620 621 //----------------------------------------------------------------------- 622 public void testToLocalDate() { 623 MonthDay base = new MonthDay(6, 6, COPTIC_UTC); 624 LocalDate test = base.toLocalDate(2009); 625 assertEquals(new LocalDate(2009, 6, 6, COPTIC_UTC), test); 626 try { 627 base.toLocalDate(0); 628 fail(); 629 } catch (IllegalArgumentException ex) {} 630 } 631 632 //----------------------------------------------------------------------- 633 public void testToDateTime_RI() { 634 MonthDay base = new MonthDay(6, 6, COPTIC_PARIS); 635 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7); 636 637 DateTime test = base.toDateTime(dt); 638 check(base, 6, 6); 639 DateTime expected = dt; 640 expected = expected.monthOfYear().setCopy(6); 641 expected = expected.dayOfMonth().setCopy(6); 642 assertEquals(expected, test); 643 } 644 645 public void testToDateTime_nullRI() { 646 MonthDay base = new MonthDay(6, 6); 647 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7); 648 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 649 650 DateTime test = base.toDateTime((ReadableInstant) null); 651 check(base, 6, 6); 652 DateTime expected = dt; 653 expected = expected.monthOfYear().setCopy(6); 654 expected = expected.dayOfMonth().setCopy(6); 655 assertEquals(expected, test); 656 } 657 658 //----------------------------------------------------------------------- 659 public void testWithers() { 660 MonthDay test = new MonthDay(10, 6); 661 check(test.withMonthOfYear(5), 5, 6); 662 check(test.withDayOfMonth(2), 10, 2); 663 try { 664 test.withMonthOfYear(0); 665 fail(); 666 } catch (IllegalArgumentException ex) {} 667 try { 668 test.withMonthOfYear(13); 669 fail(); 670 } catch (IllegalArgumentException ex) {} 671 } 672 673 //----------------------------------------------------------------------- 674 public void testProperty() { 675 MonthDay test = new MonthDay(6, 6); 676 assertEquals(test.monthOfYear(), test.property(DateTimeFieldType.monthOfYear())); 677 assertEquals(test.dayOfMonth(), test.property(DateTimeFieldType.dayOfMonth())); 678 try { 679 test.property(DateTimeFieldType.millisOfDay()); 680 fail(); 681 } catch (IllegalArgumentException ex) {} 682 try { 683 test.property(null); 684 fail(); 685 } catch (IllegalArgumentException ex) {} 686 } 687 688 //----------------------------------------------------------------------- 689 public void testSerialization() throws Exception { 690 MonthDay test = new MonthDay(5, 6, COPTIC_PARIS); 691 692 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 693 ObjectOutputStream oos = new ObjectOutputStream(baos); 694 oos.writeObject(test); 695 byte[] bytes = baos.toByteArray(); 696 oos.close(); 697 698 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 699 ObjectInputStream ois = new ObjectInputStream(bais); 700 MonthDay result = (MonthDay) ois.readObject(); 701 ois.close(); 702 703 assertEquals(test, result); 704 assertTrue(Arrays.equals(test.getValues(), result.getValues())); 705 assertTrue(Arrays.equals(test.getFields(), result.getFields())); 706 assertEquals(test.getChronology(), result.getChronology()); 707 } 708 709 //----------------------------------------------------------------------- 710 public void testToString() { 711 MonthDay test = new MonthDay(5, 6); 712 assertEquals("--05-06", test.toString()); 713 } 714 715 //----------------------------------------------------------------------- 716 public void testToString_String() { 717 MonthDay test = new MonthDay(5, 6); 718 assertEquals("05 \ufffd\ufffd", test.toString("MM HH")); 719 assertEquals("--05-06", test.toString((String) null)); 720 } 721 722 //----------------------------------------------------------------------- 723 public void testToString_String_Locale() { 724 MonthDay test = new MonthDay(5, 6); 725 assertEquals("\ufffd 6/5", test.toString("EEE d/M", Locale.ENGLISH)); 726 assertEquals("\ufffd 6/5", test.toString("EEE d/M", Locale.FRENCH)); 727 assertEquals("--05-06", test.toString(null, Locale.ENGLISH)); 728 assertEquals("\ufffd 6/5", test.toString("EEE d/M", null)); 729 assertEquals("--05-06", test.toString(null, null)); 730 } 731 732 //----------------------------------------------------------------------- 733 public void testToString_DTFormatter() { 734 MonthDay test = new MonthDay(5, 6); 735 assertEquals("05 \ufffd\ufffd", test.toString(DateTimeFormat.forPattern("MM HH"))); 736 assertEquals("--05-06", test.toString((DateTimeFormatter) null)); 737 } 738 739 //----------------------------------------------------------------------- 740 private void check(MonthDay test, int month, int day) { 741 assertEquals(month, test.getMonthOfYear()); 742 assertEquals(day, test.getDayOfMonth()); 743 } 744 }