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