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.ISOChronology; 32 import org.joda.time.format.DateTimeFormat; 33 import org.joda.time.format.DateTimeFormatter; 34 35 40 public class TestPartial_Basics extends TestCase { 41 42 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 43 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 44 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 45 private static final int OFFSET = 1; 46 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS); 47 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON); 48 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO); 49 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 50 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 51 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON); 52 private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO); 53 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 54 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS); 55 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON); 56 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO); 57 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC(); 58 59 private long TEST_TIME_NOW = 60 10L * DateTimeConstants.MILLIS_PER_HOUR 61 + 20L * DateTimeConstants.MILLIS_PER_MINUTE 62 + 30L * DateTimeConstants.MILLIS_PER_SECOND 63 + 40L; 64 65 private long TEST_TIME1 = 66 1L * DateTimeConstants.MILLIS_PER_HOUR 67 + 2L * DateTimeConstants.MILLIS_PER_MINUTE 68 + 3L * DateTimeConstants.MILLIS_PER_SECOND 69 + 4L; 70 71 private long TEST_TIME2 = 72 1L * DateTimeConstants.MILLIS_PER_DAY 73 + 5L * DateTimeConstants.MILLIS_PER_HOUR 74 + 6L * DateTimeConstants.MILLIS_PER_MINUTE 75 + 7L * DateTimeConstants.MILLIS_PER_SECOND 76 + 8L; 77 78 private DateTimeZone zone = null; 79 80 public static void main(String [] args) { 81 junit.textui.TestRunner.run(suite()); 82 } 83 84 public static TestSuite suite() { 85 return new TestSuite(TestPartial_Basics.class); 86 } 87 88 public TestPartial_Basics(String name) { 89 super(name); 90 } 91 92 protected void setUp() throws Exception { 93 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 94 zone = DateTimeZone.getDefault(); 95 DateTimeZone.setDefault(LONDON); 96 } 97 98 protected void tearDown() throws Exception { 99 DateTimeUtils.setCurrentMillisSystem(); 100 DateTimeZone.setDefault(zone); 101 zone = null; 102 } 103 104 public void testGet() { 106 Partial test = createHourMinPartial(); 107 assertEquals(10, test.get(DateTimeFieldType.hourOfDay())); 108 assertEquals(20, test.get(DateTimeFieldType.minuteOfHour())); 109 try { 110 test.get(null); 111 fail(); 112 } catch (IllegalArgumentException ex) {} 113 try { 114 test.get(DateTimeFieldType.secondOfMinute()); 115 fail(); 116 } catch (IllegalArgumentException ex) {} 117 } 118 119 public void testSize() { 120 Partial test = createHourMinPartial(); 121 assertEquals(2, test.size()); 122 } 123 124 public void testGetFieldType() { 125 Partial test = createHourMinPartial(); 126 assertSame(DateTimeFieldType.hourOfDay(), test.getFieldType(0)); 127 assertSame(DateTimeFieldType.minuteOfHour(), test.getFieldType(1)); 128 try { 129 test.getFieldType(-1); 130 } catch (IndexOutOfBoundsException ex) {} 131 try { 132 test.getFieldType(2); 133 } catch (IndexOutOfBoundsException ex) {} 134 } 135 136 public void testGetFieldTypes() { 137 Partial test = createHourMinPartial(); 138 DateTimeFieldType[] fields = test.getFieldTypes(); 139 assertEquals(2, fields.length); 140 assertSame(DateTimeFieldType.hourOfDay(), fields[0]); 141 assertSame(DateTimeFieldType.minuteOfHour(), fields[1]); 142 assertNotSame(test.getFieldTypes(), test.getFieldTypes()); 143 } 144 145 public void testGetField() { 146 Partial test = createHourMinPartial(COPTIC_PARIS); 147 assertSame(CopticChronology.getInstanceUTC().hourOfDay(), test.getField(0)); 148 assertSame(CopticChronology.getInstanceUTC().minuteOfHour(), test.getField(1)); 149 try { 150 test.getField(-1); 151 } catch (IndexOutOfBoundsException ex) {} 152 try { 153 test.getField(5); 154 } catch (IndexOutOfBoundsException ex) {} 155 } 156 157 public void testGetFields() { 158 Partial test = createHourMinPartial(COPTIC_PARIS); 159 DateTimeField[] fields = test.getFields(); 160 assertEquals(2, fields.length); 161 assertSame(CopticChronology.getInstanceUTC().hourOfDay(), fields[0]); 162 assertSame(CopticChronology.getInstanceUTC().minuteOfHour(), fields[1]); 163 assertNotSame(test.getFields(), test.getFields()); 164 } 165 166 public void testGetValue() { 167 Partial test = createHourMinPartial(COPTIC_PARIS); 168 assertEquals(10, test.getValue(0)); 169 assertEquals(20, test.getValue(1)); 170 try { 171 test.getValue(-1); 172 } catch (IndexOutOfBoundsException ex) {} 173 try { 174 test.getValue(2); 175 } catch (IndexOutOfBoundsException ex) {} 176 } 177 178 public void testGetValues() { 179 Partial test = createHourMinPartial(COPTIC_PARIS); 180 int[] values = test.getValues(); 181 assertEquals(2, values.length); 182 assertEquals(10, values[0]); 183 assertEquals(20, values[1]); 184 assertNotSame(test.getValues(), test.getValues()); 185 } 186 187 public void testIsSupported() { 188 Partial test = createHourMinPartial(COPTIC_PARIS); 189 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfDay())); 190 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfHour())); 191 assertEquals(false, test.isSupported(DateTimeFieldType.secondOfMinute())); 192 assertEquals(false, test.isSupported(DateTimeFieldType.millisOfSecond())); 193 assertEquals(false, test.isSupported(DateTimeFieldType.dayOfMonth())); 194 } 195 196 public void testEqualsHashCode() { 197 Partial test1 = createHourMinPartial(COPTIC_PARIS); 198 Partial test2 = createHourMinPartial(COPTIC_PARIS); 199 assertEquals(true, test1.equals(test2)); 200 assertEquals(true, test2.equals(test1)); 201 assertEquals(true, test1.equals(test1)); 202 assertEquals(true, test2.equals(test2)); 203 assertEquals(true, test1.hashCode() == test2.hashCode()); 204 assertEquals(true, test1.hashCode() == test1.hashCode()); 205 assertEquals(true, test2.hashCode() == test2.hashCode()); 206 207 Partial test3 = createHourMinPartial2(COPTIC_PARIS); 208 assertEquals(false, test1.equals(test3)); 209 assertEquals(false, test2.equals(test3)); 210 assertEquals(false, test3.equals(test1)); 211 assertEquals(false, test3.equals(test2)); 212 assertEquals(false, test1.hashCode() == test3.hashCode()); 213 assertEquals(false, test2.hashCode() == test3.hashCode()); 214 215 assertEquals(false, test1.equals("Hello")); 216 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE)); 217 assertEquals(new TimeOfDay(10, 20, 30, 40), createTODPartial(ISO_UTC)); 218 } 219 220 public void testCompareTo() { 222 Partial test1 = createHourMinPartial(); 223 Partial test1a = createHourMinPartial(); 224 assertEquals(0, test1.compareTo(test1a)); 225 assertEquals(0, test1a.compareTo(test1)); 226 assertEquals(0, test1.compareTo(test1)); 227 assertEquals(0, test1a.compareTo(test1a)); 228 229 Partial test2 = createHourMinPartial2(ISO_UTC); 230 assertEquals(-1, test1.compareTo(test2)); 231 assertEquals(+1, test2.compareTo(test1)); 232 233 Partial test3 = createHourMinPartial2(COPTIC_UTC); 234 assertEquals(-1, test1.compareTo(test3)); 235 assertEquals(+1, test3.compareTo(test1)); 236 assertEquals(0, test3.compareTo(test2)); 237 238 assertEquals(0, new TimeOfDay(10, 20, 30, 40).compareTo(createTODPartial(ISO_UTC))); 239 240 try { 241 test1.compareTo(null); 242 fail(); 243 } catch (NullPointerException ex) {} 244 try { 245 test1.compareTo(new Date ()); 246 fail(); 247 } catch (ClassCastException ex) {} 248 try { 249 test1.compareTo(new YearMonthDay()); 250 fail(); 251 } catch (ClassCastException ex) {} 252 try { 253 createTODPartial(ISO_UTC).without(DateTimeFieldType.hourOfDay()).compareTo(new YearMonthDay()); 254 fail(); 255 } catch (ClassCastException ex) {} 256 } 257 258 public void testIsEqual_TOD() { 260 Partial test1 = createHourMinPartial(); 261 Partial test1a = createHourMinPartial(); 262 assertEquals(true, test1.isEqual(test1a)); 263 assertEquals(true, test1a.isEqual(test1)); 264 assertEquals(true, test1.isEqual(test1)); 265 assertEquals(true, test1a.isEqual(test1a)); 266 267 Partial test2 = createHourMinPartial2(ISO_UTC); 268 assertEquals(false, test1.isEqual(test2)); 269 assertEquals(false, test2.isEqual(test1)); 270 271 Partial test3 = createHourMinPartial2(COPTIC_UTC); 272 assertEquals(false, test1.isEqual(test3)); 273 assertEquals(false, test3.isEqual(test1)); 274 assertEquals(true, test3.isEqual(test2)); 275 276 try { 277 createHourMinPartial().isEqual(null); 278 fail(); 279 } catch (IllegalArgumentException ex) {} 280 } 281 282 public void testIsBefore_TOD() { 284 Partial test1 = createHourMinPartial(); 285 Partial test1a = createHourMinPartial(); 286 assertEquals(false, test1.isBefore(test1a)); 287 assertEquals(false, test1a.isBefore(test1)); 288 assertEquals(false, test1.isBefore(test1)); 289 assertEquals(false, test1a.isBefore(test1a)); 290 291 Partial test2 = createHourMinPartial2(ISO_UTC); 292 assertEquals(true, test1.isBefore(test2)); 293 assertEquals(false, test2.isBefore(test1)); 294 295 Partial test3 = createHourMinPartial2(COPTIC_UTC); 296 assertEquals(true, test1.isBefore(test3)); 297 assertEquals(false, test3.isBefore(test1)); 298 assertEquals(false, test3.isBefore(test2)); 299 300 try { 301 createHourMinPartial().isBefore(null); 302 fail(); 303 } catch (IllegalArgumentException ex) {} 304 } 305 306 public void testIsAfter_TOD() { 308 Partial test1 = createHourMinPartial(); 309 Partial test1a = createHourMinPartial(); 310 assertEquals(false, test1.isAfter(test1a)); 311 assertEquals(false, test1a.isAfter(test1)); 312 assertEquals(false, test1.isAfter(test1)); 313 assertEquals(false, test1a.isAfter(test1a)); 314 315 Partial test2 = createHourMinPartial2(ISO_UTC); 316 assertEquals(false, test1.isAfter(test2)); 317 assertEquals(true, test2.isAfter(test1)); 318 319 Partial test3 = createHourMinPartial2(COPTIC_UTC); 320 assertEquals(false, test1.isAfter(test3)); 321 assertEquals(true, test3.isAfter(test1)); 322 assertEquals(false, test3.isAfter(test2)); 323 324 try { 325 createHourMinPartial().isAfter(null); 326 fail(); 327 } catch (IllegalArgumentException ex) {} 328 } 329 330 public void testWithChronologyRetainFields_Chrono() { 332 Partial base = createHourMinPartial(COPTIC_PARIS); 333 Partial test = base.withChronologyRetainFields(BUDDHIST_TOKYO); 334 check(base, 10, 20); 335 assertEquals(COPTIC_UTC, base.getChronology()); 336 check(test, 10, 20); 337 assertEquals(BUDDHIST_UTC, test.getChronology()); 338 } 339 340 public void testWithChronologyRetainFields_sameChrono() { 341 Partial base = createHourMinPartial(COPTIC_PARIS); 342 Partial test = base.withChronologyRetainFields(COPTIC_TOKYO); 343 assertSame(base, test); 344 } 345 346 public void testWithChronologyRetainFields_nullChrono() { 347 Partial base = createHourMinPartial(COPTIC_PARIS); 348 Partial test = base.withChronologyRetainFields(null); 349 check(base, 10, 20); 350 assertEquals(COPTIC_UTC, base.getChronology()); 351 check(test, 10, 20); 352 assertEquals(ISO_UTC, test.getChronology()); 353 } 354 355 public void testWith1() { 357 Partial test = createHourMinPartial(); 358 Partial result = test.with(DateTimeFieldType.hourOfDay(), 15); 359 check(test, 10, 20); 360 check(result, 15, 20); 361 } 362 363 public void testWith2() { 364 Partial test = createHourMinPartial(); 365 try { 366 test.with(null, 6); 367 fail(); 368 } catch (IllegalArgumentException ex) {} 369 check(test, 10, 20); 370 } 371 372 public void testWith3a() { 373 Partial test = createHourMinPartial(); 374 Partial result = test.with(DateTimeFieldType.secondOfMinute(), 15); 375 check(test, 10, 20); 376 assertEquals(3, result.size()); 377 assertEquals(true, result.isSupported(DateTimeFieldType.hourOfDay())); 378 assertEquals(true, result.isSupported(DateTimeFieldType.minuteOfHour())); 379 assertEquals(true, result.isSupported(DateTimeFieldType.secondOfMinute())); 380 assertEquals(DateTimeFieldType.hourOfDay(), result.getFieldType(0)); 381 assertEquals(DateTimeFieldType.minuteOfHour(), result.getFieldType(1)); 382 assertEquals(DateTimeFieldType.secondOfMinute(), result.getFieldType(2)); 383 assertEquals(10, result.get(DateTimeFieldType.hourOfDay())); 384 assertEquals(20, result.get(DateTimeFieldType.minuteOfHour())); 385 assertEquals(15, result.get(DateTimeFieldType.secondOfMinute())); 386 } 387 388 public void testWith3b() { 389 Partial test = createHourMinPartial(); 390 Partial result = test.with(DateTimeFieldType.minuteOfDay(), 15); 391 check(test, 10, 20); 392 assertEquals(3, result.size()); 393 assertEquals(true, result.isSupported(DateTimeFieldType.hourOfDay())); 394 assertEquals(true, result.isSupported(DateTimeFieldType.minuteOfDay())); 395 assertEquals(true, result.isSupported(DateTimeFieldType.minuteOfHour())); 396 assertEquals(DateTimeFieldType.hourOfDay(), result.getFieldType(0)); 397 assertEquals(DateTimeFieldType.minuteOfDay(), result.getFieldType(1)); 398 assertEquals(DateTimeFieldType.minuteOfHour(), result.getFieldType(2)); 399 assertEquals(10, result.get(DateTimeFieldType.hourOfDay())); 400 assertEquals(20, result.get(DateTimeFieldType.minuteOfHour())); 401 assertEquals(15, result.get(DateTimeFieldType.minuteOfDay())); 402 } 403 404 public void testWith3c() { 405 Partial test = createHourMinPartial(); 406 Partial result = test.with(DateTimeFieldType.dayOfMonth(), 15); 407 check(test, 10, 20); 408 assertEquals(3, result.size()); 409 assertEquals(true, result.isSupported(DateTimeFieldType.dayOfMonth())); 410 assertEquals(true, result.isSupported(DateTimeFieldType.hourOfDay())); 411 assertEquals(true, result.isSupported(DateTimeFieldType.minuteOfHour())); 412 assertEquals(DateTimeFieldType.dayOfMonth(), result.getFieldType(0)); 413 assertEquals(DateTimeFieldType.hourOfDay(), result.getFieldType(1)); 414 assertEquals(DateTimeFieldType.minuteOfHour(), result.getFieldType(2)); 415 assertEquals(10, result.get(DateTimeFieldType.hourOfDay())); 416 assertEquals(20, result.get(DateTimeFieldType.minuteOfHour())); 417 assertEquals(15, result.get(DateTimeFieldType.dayOfMonth())); 418 } 419 420 public void testWith3d() { 421 Partial test = new Partial(DateTimeFieldType.year(), 2005); 422 Partial result = test.with(DateTimeFieldType.monthOfYear(), 6); 423 assertEquals(2, result.size()); 424 assertEquals(2005, result.get(DateTimeFieldType.year())); 425 assertEquals(6, result.get(DateTimeFieldType.monthOfYear())); 426 } 427 428 public void testWith3e() { 429 Partial test = new Partial(DateTimeFieldType.era(), 1); 430 Partial result = test.with(DateTimeFieldType.halfdayOfDay(), 0); 431 assertEquals(2, result.size()); 432 assertEquals(1, result.get(DateTimeFieldType.era())); 433 assertEquals(0, result.get(DateTimeFieldType.halfdayOfDay())); 434 assertEquals(0, result.indexOf(DateTimeFieldType.era())); 435 assertEquals(1, result.indexOf(DateTimeFieldType.halfdayOfDay())); 436 } 437 438 public void testWith3f() { 439 Partial test = new Partial(DateTimeFieldType.halfdayOfDay(), 0); 440 Partial result = test.with(DateTimeFieldType.era(), 1); 441 assertEquals(2, result.size()); 442 assertEquals(1, result.get(DateTimeFieldType.era())); 443 assertEquals(0, result.get(DateTimeFieldType.halfdayOfDay())); 444 assertEquals(0, result.indexOf(DateTimeFieldType.era())); 445 assertEquals(1, result.indexOf(DateTimeFieldType.halfdayOfDay())); 446 } 447 448 public void testWith4() { 449 Partial test = createHourMinPartial(); 450 Partial result = test.with(DateTimeFieldType.hourOfDay(), 10); 451 assertSame(test, result); 452 } 453 454 public void testWithout1() { 456 Partial test = createHourMinPartial(); 457 Partial result = test.without(DateTimeFieldType.year()); 458 check(test, 10, 20); 459 check(result, 10, 20); 460 } 461 462 public void testWithout2() { 463 Partial test = createHourMinPartial(); 464 Partial result = test.without((DateTimeFieldType) null); 465 check(test, 10, 20); 466 check(result, 10, 20); 467 } 468 469 public void testWithout3() { 470 Partial test = createHourMinPartial(); 471 Partial result = test.without(DateTimeFieldType.hourOfDay()); 472 check(test, 10, 20); 473 assertEquals(1, result.size()); 474 assertEquals(false, result.isSupported(DateTimeFieldType.hourOfDay())); 475 assertEquals(true, result.isSupported(DateTimeFieldType.minuteOfHour())); 476 assertEquals(DateTimeFieldType.minuteOfHour(), result.getFieldType(0)); 477 } 478 479 public void testWithout4() { 480 Partial test = createHourMinPartial(); 481 Partial result = test.without(DateTimeFieldType.minuteOfHour()); 482 check(test, 10, 20); 483 assertEquals(1, result.size()); 484 assertEquals(true, result.isSupported(DateTimeFieldType.hourOfDay())); 485 assertEquals(false, result.isSupported(DateTimeFieldType.minuteOfHour())); 486 assertEquals(DateTimeFieldType.hourOfDay(), result.getFieldType(0)); 487 } 488 489 public void testWithout5() { 490 Partial test = new Partial(DateTimeFieldType.hourOfDay(), 12); 491 Partial result = test.without(DateTimeFieldType.hourOfDay()); 492 assertEquals(0, result.size()); 493 assertEquals(false, result.isSupported(DateTimeFieldType.hourOfDay())); 494 } 495 496 public void testWithField1() { 498 Partial test = createHourMinPartial(); 499 Partial result = test.withField(DateTimeFieldType.hourOfDay(), 15); 500 check(test, 10, 20); 501 check(result, 15, 20); 502 } 503 504 public void testWithField2() { 505 Partial test = createHourMinPartial(); 506 try { 507 test.withField(null, 6); 508 fail(); 509 } catch (IllegalArgumentException ex) {} 510 check(test, 10, 20); 511 } 512 513 public void testWithField3() { 514 Partial test = createHourMinPartial(); 515 try { 516 test.withField(DateTimeFieldType.dayOfMonth(), 6); 517 fail(); 518 } catch (IllegalArgumentException ex) {} 519 check(test, 10, 20); 520 } 521 522 public void testWithField4() { 523 Partial test = createHourMinPartial(); 524 Partial result = test.withField(DateTimeFieldType.hourOfDay(), 10); 525 assertSame(test, result); 526 } 527 528 public void testWithFieldAdded1() { 530 Partial test = createHourMinPartial(); 531 Partial result = test.withFieldAdded(DurationFieldType.hours(), 6); 532 533 assertEquals(createHourMinPartial(), test); 534 check(test, 10, 20); 535 check(result, 16, 20); 536 } 537 538 public void testWithFieldAdded2() { 539 Partial test = createHourMinPartial(); 540 try { 541 test.withFieldAdded(null, 0); 542 fail(); 543 } catch (IllegalArgumentException ex) {} 544 check(test, 10, 20); 545 } 546 547 public void testWithFieldAdded3() { 548 Partial test = createHourMinPartial(); 549 try { 550 test.withFieldAdded(null, 6); 551 fail(); 552 } catch (IllegalArgumentException ex) {} 553 check(test, 10, 20); 554 } 555 556 public void testWithFieldAdded4() { 557 Partial test = createHourMinPartial(); 558 Partial result = test.withFieldAdded(DurationFieldType.hours(), 0); 559 assertSame(test, result); 560 } 561 562 public void testWithFieldAdded5() { 563 Partial test = createHourMinPartial(); 564 try { 565 test.withFieldAdded(DurationFieldType.days(), 6); 566 fail(); 567 } catch (IllegalArgumentException ex) {} 568 check(test, 10, 20); 569 } 570 571 public void testWithFieldAdded6() { 572 Partial test = createHourMinPartial(); 573 try { 574 test.withFieldAdded(DurationFieldType.hours(), 16); 575 fail(); 576 } catch (IllegalArgumentException ex) { 577 } 579 check(test, 10, 20); 580 } 581 582 public void testWithFieldAdded7() { 583 Partial test = createHourMinPartial(23, 59, ISO_UTC); 584 try { 585 test.withFieldAdded(DurationFieldType.minutes(), 1); 586 fail(); 587 } catch (IllegalArgumentException ex) { 588 } 590 check(test, 23, 59); 591 592 test = createHourMinPartial(23, 59, ISO_UTC); 593 try { 594 test.withFieldAdded(DurationFieldType.hours(), 1); 595 fail(); 596 } catch (IllegalArgumentException ex) { 597 } 599 check(test, 23, 59); 600 } 601 602 public void testWithFieldAdded8() { 603 Partial test = createHourMinPartial(0, 0, ISO_UTC); 604 try { 605 test.withFieldAdded(DurationFieldType.minutes(), -1); 606 fail(); 607 } catch (IllegalArgumentException ex) { 608 } 610 check(test, 0, 0); 611 612 test = createHourMinPartial(0, 0, ISO_UTC); 613 try { 614 test.withFieldAdded(DurationFieldType.hours(), -1); 615 fail(); 616 } catch (IllegalArgumentException ex) { 617 } 619 check(test, 0, 0); 620 } 621 622 public void testWithFieldAddWrapped1() { 624 Partial test = createHourMinPartial(); 625 Partial result = test.withFieldAddWrapped(DurationFieldType.hours(), 6); 626 627 assertEquals(createHourMinPartial(), test); 628 check(test, 10, 20); 629 check(result, 16, 20); 630 } 631 632 public void testWithFieldAddWrapped2() { 633 Partial test = createHourMinPartial(); 634 try { 635 test.withFieldAddWrapped(null, 0); 636 fail(); 637 } catch (IllegalArgumentException ex) {} 638 check(test, 10, 20); 639 } 640 641 public void testWithFieldAddWrapped3() { 642 Partial test = createHourMinPartial(); 643 try { 644 test.withFieldAddWrapped(null, 6); 645 fail(); 646 } catch (IllegalArgumentException ex) {} 647 check(test, 10, 20); 648 } 649 650 public void testWithFieldAddWrapped4() { 651 Partial test = createHourMinPartial(); 652 Partial result = test.withFieldAddWrapped(DurationFieldType.hours(), 0); 653 assertSame(test, result); 654 } 655 656 public void testWithFieldAddWrapped5() { 657 Partial test = createHourMinPartial(); 658 try { 659 test.withFieldAddWrapped(DurationFieldType.days(), 6); 660 fail(); 661 } catch (IllegalArgumentException ex) {} 662 check(test, 10, 20); 663 } 664 665 public void testWithFieldAddWrapped6() { 666 Partial test = createHourMinPartial(); 667 Partial result = test.withFieldAddWrapped(DurationFieldType.hours(), 16); 668 669 assertEquals(createHourMinPartial(), test); 670 check(test, 10, 20); 671 check(result, 2, 20); 672 } 673 674 public void testWithFieldAddWrapped7() { 675 Partial test = createHourMinPartial(23, 59, ISO_UTC); 676 Partial result = test.withFieldAddWrapped(DurationFieldType.minutes(), 1); 677 check(test, 23, 59); 678 check(result, 0, 0); 679 680 test = createHourMinPartial(23, 59, ISO_UTC); 681 result = test.withFieldAddWrapped(DurationFieldType.hours(), 1); 682 check(test, 23, 59); 683 check(result, 0, 59); 684 } 685 686 public void testWithFieldAddWrapped8() { 687 Partial test = createHourMinPartial(0, 0, ISO_UTC); 688 Partial result = test.withFieldAddWrapped(DurationFieldType.minutes(), -1); 689 check(test, 0, 0); 690 check(result, 23, 59); 691 692 test = createHourMinPartial(0, 0, ISO_UTC); 693 result = test.withFieldAddWrapped(DurationFieldType.hours(), -1); 694 check(test, 0, 0); 695 check(result, 23, 0); 696 } 697 698 public void testPlus_RP() { 700 Partial test = createHourMinPartial(BUDDHIST_LONDON); 701 Partial result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7, 8)); 702 check(test, 10, 20); 703 check(result, 15, 26); 704 705 result = test.plus((ReadablePeriod) null); 706 assertSame(test, result); 707 } 708 709 public void testMinus_RP() { 711 Partial test = createHourMinPartial(BUDDHIST_LONDON); 712 Partial result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1)); 713 check(test, 10, 20); 714 check(result, 9, 19); 715 716 result = test.minus((ReadablePeriod) null); 717 assertSame(test, result); 718 } 719 720 public void testToDateTime_RI() { 722 Partial base = createHourMinPartial(COPTIC_PARIS); 723 DateTime dt = new DateTime(0L); assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString()); 725 726 DateTime test = base.toDateTime(dt); 727 check(base, 10, 20); 728 assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString()); 729 assertEquals("1970-01-01T10:20:00.000+01:00", test.toString()); 730 } 731 732 public void testToDateTime_nullRI() { 733 Partial base = createHourMinPartial(1, 2, ISO_UTC); 734 DateTimeUtils.setCurrentMillisFixed(TEST_TIME2); 735 736 DateTime test = base.toDateTime((ReadableInstant) null); 737 check(base, 1, 2); 738 assertEquals("1970-01-02T01:02:07.008+01:00", test.toString()); 739 } 740 741 public void testProperty() { 743 Partial test = createHourMinPartial(); 744 assertNotNull(test.property(DateTimeFieldType.hourOfDay())); 745 assertNotNull(test.property(DateTimeFieldType.minuteOfHour())); 746 try { 747 test.property(DateTimeFieldType.secondOfDay()); 748 fail(); 749 } catch (IllegalArgumentException ex) {} 750 try { 751 test.property(null); 752 fail(); 753 } catch (IllegalArgumentException ex) {} 754 } 755 756 public void testSerialization() throws Exception { 758 Partial test = createHourMinPartial(COPTIC_PARIS); 759 760 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 761 ObjectOutputStream oos = new ObjectOutputStream (baos); 762 oos.writeObject(test); 763 byte[] bytes = baos.toByteArray(); 764 oos.close(); 765 766 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 767 ObjectInputStream ois = new ObjectInputStream (bais); 768 Partial result = (Partial) ois.readObject(); 769 ois.close(); 770 771 assertEquals(test, result); 772 assertTrue(Arrays.equals(test.getValues(), result.getValues())); 773 assertTrue(Arrays.equals(test.getFields(), result.getFields())); 774 assertEquals(test.getChronology(), result.getChronology()); 775 } 776 777 public void testGetFormatter1() { 779 Partial test = new Partial(DateTimeFieldType.year(), 2005); 780 assertEquals("2005", test.getFormatter().print(test)); 781 782 test = test.with(DateTimeFieldType.monthOfYear(), 6); 783 assertEquals("2005-06", test.getFormatter().print(test)); 784 785 test = test.with(DateTimeFieldType.dayOfMonth(), 25); 786 assertEquals("2005-06-25", test.getFormatter().print(test)); 787 788 test = test.without(DateTimeFieldType.monthOfYear()); 789 assertEquals("2005--25", test.getFormatter().print(test)); 790 } 791 792 public void testGetFormatter2() { 793 Partial test = new Partial(); 794 assertEquals(null, test.getFormatter()); 795 796 test = test.with(DateTimeFieldType.era(), 1); 797 assertEquals(null, test.getFormatter()); 798 799 test = test.with(DateTimeFieldType.halfdayOfDay(), 0); 800 assertEquals(null, test.getFormatter()); 801 } 802 803 public void testGetFormatter3() { 804 Partial test = new Partial(DateTimeFieldType.dayOfWeek(), 5); 805 assertEquals("-W-5", test.getFormatter().print(test)); 806 807 test = test.with(DateTimeFieldType.dayOfMonth(), 13); 809 assertEquals("---13", test.getFormatter().print(test)); 810 } 811 812 public void testToString1() { 814 Partial test = createHourMinPartial(); 815 assertEquals("10:20", test.toString()); 816 } 817 818 public void testToString2() { 819 Partial test = new Partial(); 820 assertEquals("[]", test.toString()); 821 } 822 823 public void testToString3() { 824 Partial test = new Partial(DateTimeFieldType.year(), 2005); 825 assertEquals("2005", test.toString()); 826 827 test = test.with(DateTimeFieldType.monthOfYear(), 6); 828 assertEquals("2005-06", test.toString()); 829 830 test = test.with(DateTimeFieldType.dayOfMonth(), 25); 831 assertEquals("2005-06-25", test.toString()); 832 833 test = test.without(DateTimeFieldType.monthOfYear()); 834 assertEquals("2005--25", test.toString()); 835 } 836 837 public void testToString4() { 838 Partial test = new Partial(DateTimeFieldType.dayOfWeek(), 5); 839 assertEquals("-W-5", test.toString()); 840 841 test = test.with(DateTimeFieldType.dayOfMonth(), 13); 842 assertEquals("[dayOfMonth=13, dayOfWeek=5]", test.toString()); 843 } 844 845 public void testToString5() { 846 Partial test = new Partial(DateTimeFieldType.era(), 1); 847 assertEquals("[era=1]", test.toString()); 848 849 test = test.with(DateTimeFieldType.halfdayOfDay(), 0); 850 assertEquals("[era=1, halfdayOfDay=0]", test.toString()); 851 } 852 853 public void testToString_String() { 855 Partial test = createHourMinPartial(); 856 assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test.toString("yyyy HH")); 857 assertEquals("10:20", test.toString((String ) null)); 858 } 859 860 public void testToString_String_Locale() { 862 Partial test = createHourMinPartial(); 863 assertEquals("10 20", test.toString("H m", Locale.ENGLISH)); 864 assertEquals("10:20", test.toString(null, Locale.ENGLISH)); 865 assertEquals("10 20", test.toString("H m", null)); 866 assertEquals("10:20", test.toString(null, null)); 867 } 868 869 public void testToString_DTFormatter() { 871 Partial test = createHourMinPartial(); 872 assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test.toString(DateTimeFormat.forPattern("yyyy HH"))); 873 assertEquals("10:20", test.toString((DateTimeFormatter) null)); 874 } 875 876 private Partial createHourMinPartial() { 878 return createHourMinPartial(ISO_UTC); 879 } 880 881 private Partial createHourMinPartial(Chronology chrono) { 882 return createHourMinPartial(10, 20, chrono); 883 } 884 885 private Partial createHourMinPartial2(Chronology chrono) { 886 return createHourMinPartial(15, 20, chrono); 887 } 888 889 private Partial createHourMinPartial(int hour, int min, Chronology chrono) { 890 return new Partial( 891 new DateTimeFieldType[] {DateTimeFieldType.hourOfDay(), DateTimeFieldType.minuteOfHour()}, 892 new int[] {hour, min}, 893 chrono); 894 } 895 896 private Partial createTODPartial(Chronology chrono) { 897 return new Partial( 898 new DateTimeFieldType[] { 899 DateTimeFieldType.hourOfDay(), DateTimeFieldType.minuteOfHour(), 900 DateTimeFieldType.secondOfMinute(), DateTimeFieldType.millisOfSecond()}, 901 new int[] {10, 20, 30, 40}, 902 chrono); 903 } 904 905 private void check(Partial test, int hour, int min) { 906 assertEquals(test.toString(), hour, test.get(DateTimeFieldType.hourOfDay())); 907 assertEquals(test.toString(), min, test.get(DateTimeFieldType.minuteOfHour())); 908 } 909 } 910 | Popular Tags |