1 16 package org.joda.time.field; 17 18 import java.util.Arrays ; 19 import java.util.Locale ; 20 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 import org.joda.time.DateTimeFieldType; 25 import org.joda.time.DurationField; 26 import org.joda.time.DurationFieldType; 27 import org.joda.time.TimeOfDay; 28 import org.joda.time.chrono.ISOChronology; 29 30 35 public class TestPreciseDateTimeField extends TestCase { 36 37 public static void main(String [] args) { 38 junit.textui.TestRunner.run(suite()); 39 } 40 41 public static TestSuite suite() { 42 return new TestSuite(TestPreciseDateTimeField.class); 43 } 44 45 public TestPreciseDateTimeField(String name) { 46 super(name); 47 } 48 49 protected void setUp() throws Exception { 50 } 51 52 protected void tearDown() throws Exception { 53 } 54 55 public void test_constructor() { 57 BaseDateTimeField field = new PreciseDateTimeField( 58 DateTimeFieldType.secondOfMinute(), 59 ISOChronology.getInstanceUTC().millis(), 60 ISOChronology.getInstanceUTC().hours() 61 ); 62 assertEquals(DateTimeFieldType.secondOfMinute(), field.getType()); 63 try { 64 field = new PreciseDateTimeField(null, null, null); 65 fail(); 66 } catch (IllegalArgumentException ex) {} 67 try { 68 field = new PreciseDateTimeField( 69 DateTimeFieldType.minuteOfHour(), 70 new MockImpreciseDurationField(DurationFieldType.minutes()), 71 ISOChronology.getInstanceUTC().hours()); 72 fail(); 73 } catch (IllegalArgumentException ex) {} 74 try { 75 field = new PreciseDateTimeField( 76 DateTimeFieldType.minuteOfHour(), 77 ISOChronology.getInstanceUTC().hours(), 78 new MockImpreciseDurationField(DurationFieldType.minutes())); 79 fail(); 80 } catch (IllegalArgumentException ex) {} 81 try { 82 field = new PreciseDateTimeField( 83 DateTimeFieldType.minuteOfHour(), 84 ISOChronology.getInstanceUTC().hours(), 85 ISOChronology.getInstanceUTC().hours()); 86 fail(); 87 } catch (IllegalArgumentException ex) {} 88 try { 89 field = new PreciseDateTimeField( 90 DateTimeFieldType.minuteOfHour(), 91 new MockZeroDurationField(DurationFieldType.minutes()), 92 ISOChronology.getInstanceUTC().hours()); 93 fail(); 94 } catch (IllegalArgumentException ex) {} 95 } 96 97 public void test_getType() { 98 BaseDateTimeField field = new PreciseDateTimeField( 99 DateTimeFieldType.secondOfDay(), 100 ISOChronology.getInstanceUTC().millis(), 101 ISOChronology.getInstanceUTC().hours() 102 ); 103 assertEquals(DateTimeFieldType.secondOfDay(), field.getType()); 104 } 105 106 public void test_getName() { 107 BaseDateTimeField field = new PreciseDateTimeField( 108 DateTimeFieldType.secondOfDay(), 109 ISOChronology.getInstanceUTC().millis(), 110 ISOChronology.getInstanceUTC().hours() 111 ); 112 assertEquals("secondOfDay", field.getName()); 113 } 114 115 public void test_toString() { 116 BaseDateTimeField field = new PreciseDateTimeField( 117 DateTimeFieldType.secondOfDay(), 118 ISOChronology.getInstanceUTC().millis(), 119 ISOChronology.getInstanceUTC().hours() 120 ); 121 assertEquals("DateTimeField[secondOfDay]", field.toString()); 122 } 123 124 public void test_isSupported() { 125 BaseDateTimeField field = new MockPreciseDateTimeField(); 126 assertEquals(true, field.isSupported()); 127 } 128 129 public void test_getRange() { 130 PreciseDateTimeField field = new MockPreciseDateTimeField(); 131 assertEquals(60, field.getRange()); 132 } 133 134 public void test_get() { 135 PreciseDateTimeField field = new MockPreciseDateTimeField(); 136 assertEquals(0, field.get(0)); 137 assertEquals(1, field.get(60)); 138 assertEquals(2, field.get(123)); 139 } 140 141 public void test_getAsText_long_Locale() { 143 BaseDateTimeField field = new MockPreciseDateTimeField(); 144 assertEquals("29", field.getAsText(60L * 29, Locale.ENGLISH)); 145 assertEquals("29", field.getAsText(60L * 29, null)); 146 } 147 148 public void test_getAsText_long() { 149 BaseDateTimeField field = new MockPreciseDateTimeField(); 150 assertEquals("29", field.getAsText(60L * 29)); 151 } 152 153 public void test_getAsText_RP_int_Locale() { 154 BaseDateTimeField field = new MockPreciseDateTimeField(); 155 assertEquals("20", field.getAsText(new TimeOfDay(12, 30, 40, 50), 20, Locale.ENGLISH)); 156 assertEquals("20", field.getAsText(new TimeOfDay(12, 30, 40, 50), 20, null)); 157 } 158 159 public void test_getAsText_RP_Locale() { 160 BaseDateTimeField field = new MockPreciseDateTimeField(); 161 assertEquals("40", field.getAsText(new TimeOfDay(12, 30, 40, 50), Locale.ENGLISH)); 162 assertEquals("40", field.getAsText(new TimeOfDay(12, 30, 40, 50), null)); 163 } 164 165 public void test_getAsText_int_Locale() { 166 BaseDateTimeField field = new MockPreciseDateTimeField(); 167 assertEquals("80", field.getAsText(80, Locale.ENGLISH)); 168 assertEquals("80", field.getAsText(80, null)); 169 } 170 171 public void test_getAsShortText_long_Locale() { 173 BaseDateTimeField field = new MockPreciseDateTimeField(); 174 assertEquals("29", field.getAsShortText(60L * 29, Locale.ENGLISH)); 175 assertEquals("29", field.getAsShortText(60L * 29, null)); 176 } 177 178 public void test_getAsShortText_long() { 179 BaseDateTimeField field = new MockPreciseDateTimeField(); 180 assertEquals("29", field.getAsShortText(60L * 29)); 181 } 182 183 public void test_getAsShortText_RP_int_Locale() { 184 BaseDateTimeField field = new MockPreciseDateTimeField(); 185 assertEquals("20", field.getAsShortText(new TimeOfDay(12, 30, 40, 50), 20, Locale.ENGLISH)); 186 assertEquals("20", field.getAsShortText(new TimeOfDay(12, 30, 40, 50), 20, null)); 187 } 188 189 public void test_getAsShortText_RP_Locale() { 190 BaseDateTimeField field = new MockPreciseDateTimeField(); 191 assertEquals("40", field.getAsShortText(new TimeOfDay(12, 30, 40, 50), Locale.ENGLISH)); 192 assertEquals("40", field.getAsShortText(new TimeOfDay(12, 30, 40, 50), null)); 193 } 194 195 public void test_getAsShortText_int_Locale() { 196 BaseDateTimeField field = new MockPreciseDateTimeField(); 197 assertEquals("80", field.getAsShortText(80, Locale.ENGLISH)); 198 assertEquals("80", field.getAsShortText(80, null)); 199 } 200 201 public void test_add_long_int() { 203 MockCountingDurationField.add_int = 0; 204 BaseDateTimeField field = new MockPreciseDateTimeField(); 205 assertEquals(61, field.add(1L, 1)); 206 assertEquals(1, MockCountingDurationField.add_int); 207 } 208 209 public void test_add_long_long() { 210 MockCountingDurationField.add_long = 0; 211 BaseDateTimeField field = new MockPreciseDateTimeField(); 212 assertEquals(61, field.add(1L, 1L)); 213 assertEquals(1, MockCountingDurationField.add_long); 214 } 215 216 public void test_add_RP_int_intarray_int() { 217 int[] values = new int[] {10, 20, 30, 40}; 218 int[] expected = new int[] {10, 20, 30, 40}; 219 BaseDateTimeField field = new MockStandardDateTimeField(); 220 int[] result = field.add(new TimeOfDay(), 2, values, 0); 221 assertEquals(true, Arrays.equals(expected, result)); 222 223 values = new int[] {10, 20, 30, 40}; 224 expected = new int[] {10, 20, 31, 40}; 225 result = field.add(new TimeOfDay(), 2, values, 1); 226 assertEquals(true, Arrays.equals(expected, result)); 227 228 values = new int[] {10, 20, 30, 40}; 229 expected = new int[] {10, 21, 0, 40}; 230 result = field.add(new TimeOfDay(), 2, values, 30); 231 assertEquals(true, Arrays.equals(expected, result)); 232 233 values = new int[] {23, 59, 30, 40}; 234 try { 235 field.add(new TimeOfDay(), 2, values, 30); 236 fail(); 237 } catch (IllegalArgumentException ex) {} 238 239 values = new int[] {10, 20, 30, 40}; 240 expected = new int[] {10, 20, 29, 40}; 241 result = field.add(new TimeOfDay(), 2, values, -1); 242 assertEquals(true, Arrays.equals(expected, result)); 243 244 values = new int[] {10, 20, 30, 40}; 245 expected = new int[] {10, 19, 59, 40}; 246 result = field.add(new TimeOfDay(), 2, values, -31); 247 assertEquals(true, Arrays.equals(expected, result)); 248 249 values = new int[] {0, 0, 30, 40}; 250 try { 251 field.add(new TimeOfDay(), 2, values, -31); 252 fail(); 253 } catch (IllegalArgumentException ex) {} 254 } 255 256 public void test_addWrapField_long_int() { 258 BaseDateTimeField field = new MockPreciseDateTimeField(); 259 assertEquals(29 * 60L, field.addWrapField(60L * 29, 0)); 260 assertEquals(59 * 60L, field.addWrapField(60L * 29, 30)); 261 assertEquals(0 * 60L, field.addWrapField(60L * 29, 31)); 262 } 263 264 public void test_addWrapField_RP_int_intarray_int() { 265 BaseDateTimeField field = new MockPreciseDateTimeField(); 266 int[] values = new int[] {10, 20, 30, 40}; 267 int[] expected = new int[] {10, 20, 30, 40}; 268 int[] result = field.addWrapField(new TimeOfDay(), 2, values, 0); 269 assertEquals(true, Arrays.equals(result, expected)); 270 271 values = new int[] {10, 20, 30, 40}; 272 expected = new int[] {10, 20, 59, 40}; 273 result = field.addWrapField(new TimeOfDay(), 2, values, 29); 274 assertEquals(true, Arrays.equals(result, expected)); 275 276 values = new int[] {10, 20, 30, 40}; 277 expected = new int[] {10, 20, 0, 40}; 278 result = field.addWrapField(new TimeOfDay(), 2, values, 30); 279 assertEquals(true, Arrays.equals(result, expected)); 280 281 values = new int[] {10, 20, 30, 40}; 282 expected = new int[] {10, 20, 1, 40}; 283 result = field.addWrapField(new TimeOfDay(), 2, values, 31); 284 assertEquals(true, Arrays.equals(result, expected)); 285 } 286 287 public void test_getDifference_long_long() { 289 MockCountingDurationField.difference_long = 0; 290 BaseDateTimeField field = new MockPreciseDateTimeField(); 291 assertEquals(30, field.getDifference(0L, 0L)); 292 assertEquals(1, MockCountingDurationField.difference_long); 293 } 294 295 public void test_getDifferenceAsLong_long_long() { 296 MockCountingDurationField.difference_long = 0; 297 BaseDateTimeField field = new MockPreciseDateTimeField(); 298 assertEquals(30, field.getDifferenceAsLong(0L, 0L)); 299 assertEquals(1, MockCountingDurationField.difference_long); 300 } 301 302 public void test_set_long_int() { 304 BaseDateTimeField field = new MockPreciseDateTimeField(); 305 assertEquals(0, field.set(120L, 0)); 306 assertEquals(29 * 60, field.set(120L, 29)); 307 } 308 309 public void test_set_RP_int_intarray_int() { 310 BaseDateTimeField field = new MockPreciseDateTimeField(); 311 int[] values = new int[] {10, 20, 30, 40}; 312 int[] expected = new int[] {10, 20, 30, 40}; 313 int[] result = field.set(new TimeOfDay(), 2, values, 30); 314 assertEquals(true, Arrays.equals(result, expected)); 315 316 values = new int[] {10, 20, 30, 40}; 317 expected = new int[] {10, 20, 29, 40}; 318 result = field.set(new TimeOfDay(), 2, values, 29); 319 assertEquals(true, Arrays.equals(result, expected)); 320 321 values = new int[] {10, 20, 30, 40}; 322 expected = new int[] {10, 20, 30, 40}; 323 try { 324 field.set(new TimeOfDay(), 2, values, 60); 325 fail(); 326 } catch (IllegalArgumentException ex) {} 327 assertEquals(true, Arrays.equals(values, expected)); 328 329 values = new int[] {10, 20, 30, 40}; 330 expected = new int[] {10, 20, 30, 40}; 331 try { 332 field.set(new TimeOfDay(), 2, values, -1); 333 fail(); 334 } catch (IllegalArgumentException ex) {} 335 assertEquals(true, Arrays.equals(values, expected)); 336 } 337 338 public void test_set_long_String_Locale() { 339 BaseDateTimeField field = new MockPreciseDateTimeField(); 340 assertEquals(0, field.set(0L, "0", null)); 341 assertEquals(29 * 60, field.set(0L, "29", Locale.ENGLISH)); 342 } 343 344 public void test_set_long_String() { 345 BaseDateTimeField field = new MockPreciseDateTimeField(); 346 assertEquals(0, field.set(0L, "0")); 347 assertEquals(29 * 60, field.set(0L, "29")); 348 } 349 350 public void test_set_RP_int_intarray_String_Locale() { 351 BaseDateTimeField field = new MockPreciseDateTimeField(); 352 int[] values = new int[] {10, 20, 30, 40}; 353 int[] expected = new int[] {10, 20, 30, 40}; 354 int[] result = field.set(new TimeOfDay(), 2, values, "30", null); 355 assertEquals(true, Arrays.equals(result, expected)); 356 357 values = new int[] {10, 20, 30, 40}; 358 expected = new int[] {10, 20, 29, 40}; 359 result = field.set(new TimeOfDay(), 2, values, "29", Locale.ENGLISH); 360 assertEquals(true, Arrays.equals(result, expected)); 361 362 values = new int[] {10, 20, 30, 40}; 363 expected = new int[] {10, 20, 30, 40}; 364 try { 365 field.set(new TimeOfDay(), 2, values, "60", null); 366 fail(); 367 } catch (IllegalArgumentException ex) {} 368 assertEquals(true, Arrays.equals(values, expected)); 369 370 values = new int[] {10, 20, 30, 40}; 371 expected = new int[] {10, 20, 30, 40}; 372 try { 373 field.set(new TimeOfDay(), 2, values, "-1", null); 374 fail(); 375 } catch (IllegalArgumentException ex) {} 376 assertEquals(true, Arrays.equals(values, expected)); 377 } 378 379 public void test_convertText() { 380 BaseDateTimeField field = new MockPreciseDateTimeField(); 381 assertEquals(0, field.convertText("0", null)); 382 assertEquals(29, field.convertText("29", null)); 383 try { 384 field.convertText("2A", null); 385 fail(); 386 } catch (IllegalArgumentException ex) {} 387 try { 388 field.convertText(null, null); 389 fail(); 390 } catch (IllegalArgumentException ex) {} 391 } 392 393 398 public void test_isLeap_long() { 399 BaseDateTimeField field = new MockPreciseDateTimeField(); 400 assertEquals(false, field.isLeap(0L)); 401 } 402 403 public void test_getLeapAmount_long() { 404 BaseDateTimeField field = new MockPreciseDateTimeField(); 405 assertEquals(0, field.getLeapAmount(0L)); 406 } 407 408 public void test_getLeapDurationField() { 409 BaseDateTimeField field = new MockPreciseDateTimeField(); 410 assertEquals(null, field.getLeapDurationField()); 411 } 412 413 public void test_getMinimumValue() { 415 BaseDateTimeField field = new MockPreciseDateTimeField(); 416 assertEquals(0, field.getMinimumValue()); 417 } 418 419 public void test_getMinimumValue_long() { 420 BaseDateTimeField field = new MockPreciseDateTimeField(); 421 assertEquals(0, field.getMinimumValue(0L)); 422 } 423 424 public void test_getMinimumValue_RP() { 425 BaseDateTimeField field = new MockPreciseDateTimeField(); 426 assertEquals(0, field.getMinimumValue(new TimeOfDay())); 427 } 428 429 public void test_getMinimumValue_RP_intarray() { 430 BaseDateTimeField field = new MockPreciseDateTimeField(); 431 assertEquals(0, field.getMinimumValue(new TimeOfDay(), new int[4])); 432 } 433 434 public void test_getMaximumValue() { 435 BaseDateTimeField field = new MockPreciseDateTimeField(); 436 assertEquals(59, field.getMaximumValue()); 437 } 438 439 public void test_getMaximumValue_long() { 440 BaseDateTimeField field = new MockPreciseDateTimeField(); 441 assertEquals(59, field.getMaximumValue(0L)); 442 } 443 444 public void test_getMaximumValue_RP() { 445 BaseDateTimeField field = new MockPreciseDateTimeField(); 446 assertEquals(59, field.getMaximumValue(new TimeOfDay())); 447 } 448 449 public void test_getMaximumValue_RP_intarray() { 450 BaseDateTimeField field = new MockPreciseDateTimeField(); 451 assertEquals(59, field.getMaximumValue(new TimeOfDay(), new int[4])); 452 } 453 454 public void test_getMaximumTextLength_Locale() { 456 BaseDateTimeField field = new MockPreciseDateTimeField(); 457 assertEquals(2, field.getMaximumTextLength(Locale.ENGLISH)); 458 } 459 460 public void test_getMaximumShortTextLength_Locale() { 461 BaseDateTimeField field = new MockPreciseDateTimeField(); 462 assertEquals(2, field.getMaximumShortTextLength(Locale.ENGLISH)); 463 } 464 465 public void test_roundFloor_long() { 467 BaseDateTimeField field = new MockPreciseDateTimeField(); 468 assertEquals(-120L, field.roundFloor(-61L)); 469 assertEquals(-60L, field.roundFloor(-60L)); 470 assertEquals(-60L, field.roundFloor(-59L)); 471 assertEquals(-60L, field.roundFloor(-1L)); 472 assertEquals(0L, field.roundFloor(0L)); 473 assertEquals(0L, field.roundFloor(1L)); 474 assertEquals(0L, field.roundFloor(29L)); 475 assertEquals(0L, field.roundFloor(30L)); 476 assertEquals(0L, field.roundFloor(31L)); 477 assertEquals(60L, field.roundFloor(60L)); 478 } 479 480 public void test_roundCeiling_long() { 481 BaseDateTimeField field = new MockPreciseDateTimeField(); 482 assertEquals(-60L, field.roundCeiling(-61L)); 483 assertEquals(-60L, field.roundCeiling(-60L)); 484 assertEquals(0L, field.roundCeiling(-59L)); 485 assertEquals(0L, field.roundCeiling(-1L)); 486 assertEquals(0L, field.roundCeiling(0L)); 487 assertEquals(60L, field.roundCeiling(1L)); 488 assertEquals(60L, field.roundCeiling(29L)); 489 assertEquals(60L, field.roundCeiling(30L)); 490 assertEquals(60L, field.roundCeiling(31L)); 491 assertEquals(60L, field.roundCeiling(60L)); 492 } 493 494 public void test_roundHalfFloor_long() { 495 BaseDateTimeField field = new MockPreciseDateTimeField(); 496 assertEquals(0L, field.roundHalfFloor(0L)); 497 assertEquals(0L, field.roundHalfFloor(29L)); 498 assertEquals(0L, field.roundHalfFloor(30L)); 499 assertEquals(60L, field.roundHalfFloor(31L)); 500 assertEquals(60L, field.roundHalfFloor(60L)); 501 } 502 503 public void test_roundHalfCeiling_long() { 504 BaseDateTimeField field = new MockPreciseDateTimeField(); 505 assertEquals(0L, field.roundHalfCeiling(0L)); 506 assertEquals(0L, field.roundHalfCeiling(29L)); 507 assertEquals(60L, field.roundHalfCeiling(30L)); 508 assertEquals(60L, field.roundHalfCeiling(31L)); 509 assertEquals(60L, field.roundHalfCeiling(60L)); 510 } 511 512 public void test_roundHalfEven_long() { 513 BaseDateTimeField field = new MockPreciseDateTimeField(); 514 assertEquals(0L, field.roundHalfEven(0L)); 515 assertEquals(0L, field.roundHalfEven(29L)); 516 assertEquals(0L, field.roundHalfEven(30L)); 517 assertEquals(60L, field.roundHalfEven(31L)); 518 assertEquals(60L, field.roundHalfEven(60L)); 519 assertEquals(60L, field.roundHalfEven(89L)); 520 assertEquals(120L, field.roundHalfEven(90L)); 521 assertEquals(120L, field.roundHalfEven(91L)); 522 } 523 524 public void test_remainder_long() { 525 BaseDateTimeField field = new MockPreciseDateTimeField(); 526 assertEquals(0L, field.remainder(0L)); 527 assertEquals(29L, field.remainder(29L)); 528 assertEquals(30L, field.remainder(30L)); 529 assertEquals(31L, field.remainder(31L)); 530 assertEquals(0L, field.remainder(60L)); 531 } 532 533 static class MockPreciseDateTimeField extends PreciseDateTimeField { 535 protected MockPreciseDateTimeField() { 536 super(DateTimeFieldType.secondOfMinute(), 537 new MockCountingDurationField(DurationFieldType.seconds(), 60), 538 new MockCountingDurationField(DurationFieldType.minutes(), 60 * 60)); 539 } 540 protected MockPreciseDateTimeField( 541 DateTimeFieldType type, DurationField dur, DurationField range) { 542 super(type, dur, range); 543 } 544 } 545 546 static class MockStandardDateTimeField extends MockPreciseDateTimeField { 547 protected MockStandardDateTimeField() { 548 super(); 549 } 550 public DurationField getDurationField() { 551 return ISOChronology.getInstanceUTC().seconds(); 552 } 553 public DurationField getRangeDurationField() { 554 return ISOChronology.getInstanceUTC().minutes(); 555 } 556 } 557 558 static class MockCountingDurationField extends BaseDurationField { 560 static int add_int = 0; 561 static int add_long = 0; 562 static int difference_long = 0; 563 int unit; 564 565 protected MockCountingDurationField(DurationFieldType type, int unit) { 566 super(type); 567 this.unit = unit; 568 } 569 public boolean isPrecise() { 570 return true; 571 } 572 public long getUnitMillis() { 573 return unit; 574 } 575 public long getValueAsLong(long duration, long instant) { 576 return 0; 577 } 578 public long getMillis(int value, long instant) { 579 return 0; 580 } 581 public long getMillis(long value, long instant) { 582 return 0; 583 } 584 public long add(long instant, int value) { 585 add_int++; 586 return instant + (value * 60L); 587 } 588 public long add(long instant, long value) { 589 add_long++; 590 return instant + (value * 60L); 591 } 592 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) { 593 difference_long++; 594 return 30; 595 } 596 } 597 598 static class MockZeroDurationField extends BaseDurationField { 600 protected MockZeroDurationField(DurationFieldType type) { 601 super(type); 602 } 603 public boolean isPrecise() { 604 return true; 605 } 606 public long getUnitMillis() { 607 return 0; } 609 public long getValueAsLong(long duration, long instant) { 610 return 0; 611 } 612 public long getMillis(int value, long instant) { 613 return 0; 614 } 615 public long getMillis(long value, long instant) { 616 return 0; 617 } 618 public long add(long instant, int value) { 619 return 0; 620 } 621 public long add(long instant, long value) { 622 return 0; 623 } 624 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) { 625 return 0; 626 } 627 } 628 629 static class MockImpreciseDurationField extends BaseDurationField { 631 protected MockImpreciseDurationField(DurationFieldType type) { 632 super(type); 633 } 634 public boolean isPrecise() { 635 return false; } 637 public long getUnitMillis() { 638 return 0; 639 } 640 public long getValueAsLong(long duration, long instant) { 641 return 0; 642 } 643 public long getMillis(int value, long instant) { 644 return 0; 645 } 646 public long getMillis(long value, long instant) { 647 return 0; 648 } 649 public long add(long instant, int value) { 650 return 0; 651 } 652 public long add(long instant, long value) { 653 return 0; 654 } 655 public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant) { 656 return 0; 657 } 658 } 659 660 } 661 | Popular Tags |