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.Locale ; 24 import java.util.TimeZone ; 25 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import org.joda.time.base.BasePeriod; 30 31 36 public class TestPeriod_Basics extends TestCase { 37 40 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 41 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 42 43 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 44 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 45 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 46 366 + 365; 47 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 48 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 49 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 50 366 + 365 + 365; 51 52 private long TEST_TIME_NOW = 54 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 55 56 private long TEST_TIME1 = 58 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY 59 + 12L * DateTimeConstants.MILLIS_PER_HOUR 60 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 61 62 private long TEST_TIME2 = 64 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 65 + 14L * DateTimeConstants.MILLIS_PER_HOUR 66 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 67 68 private DateTimeZone originalDateTimeZone = null; 69 private TimeZone originalTimeZone = null; 70 private Locale originalLocale = null; 71 72 public static void main(String [] args) { 73 junit.textui.TestRunner.run(suite()); 74 } 75 76 public static TestSuite suite() { 77 return new TestSuite(TestPeriod_Basics.class); 78 } 79 80 public TestPeriod_Basics(String name) { 81 super(name); 82 } 83 84 protected void setUp() throws Exception { 85 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 86 originalDateTimeZone = DateTimeZone.getDefault(); 87 originalTimeZone = TimeZone.getDefault(); 88 originalLocale = Locale.getDefault(); 89 DateTimeZone.setDefault(LONDON); 90 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 91 Locale.setDefault(Locale.UK); 92 } 93 94 protected void tearDown() throws Exception { 95 DateTimeUtils.setCurrentMillisSystem(); 96 DateTimeZone.setDefault(originalDateTimeZone); 97 TimeZone.setDefault(originalTimeZone); 98 Locale.setDefault(originalLocale); 99 originalDateTimeZone = null; 100 originalTimeZone = null; 101 originalLocale = null; 102 } 103 104 public void testTest() { 106 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString()); 107 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString()); 108 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString()); 109 } 110 111 public void testGetPeriodType() { 113 Period test = new Period(0L); 114 assertEquals(PeriodType.standard(), test.getPeriodType()); 115 } 116 117 public void testGetMethods() { 118 Period test = new Period(0L); 119 assertEquals(0, test.getYears()); 120 assertEquals(0, test.getMonths()); 121 assertEquals(0, test.getWeeks()); 122 assertEquals(0, test.getDays()); 123 assertEquals(0, test.getHours()); 124 assertEquals(0, test.getMinutes()); 125 assertEquals(0, test.getSeconds()); 126 assertEquals(0, test.getMillis()); 127 } 128 129 public void testValueIndexMethods() { 130 Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime()); 131 assertEquals(6, test.size()); 132 assertEquals(1, test.getValue(0)); 133 assertEquals(4, test.getValue(1)); 134 assertEquals(5, test.getValue(2)); 135 assertEquals(6, test.getValue(3)); 136 assertEquals(7, test.getValue(4)); 137 assertEquals(8, test.getValue(5)); 138 assertEquals(true, Arrays.equals(new int[] {1, 4, 5, 6, 7, 8}, test.getValues())); 139 } 140 141 public void testTypeIndexMethods() { 142 Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime()); 143 assertEquals(6, test.size()); 144 assertEquals(DurationFieldType.years(), test.getFieldType(0)); 145 assertEquals(DurationFieldType.days(), test.getFieldType(1)); 146 assertEquals(DurationFieldType.hours(), test.getFieldType(2)); 147 assertEquals(DurationFieldType.minutes(), test.getFieldType(3)); 148 assertEquals(DurationFieldType.seconds(), test.getFieldType(4)); 149 assertEquals(DurationFieldType.millis(), test.getFieldType(5)); 150 assertEquals(true, Arrays.equals(new DurationFieldType[] { 151 DurationFieldType.years(), DurationFieldType.days(), DurationFieldType.hours(), 152 DurationFieldType.minutes(), DurationFieldType.seconds(), DurationFieldType.millis()}, 153 test.getFieldTypes())); 154 } 155 156 public void testIsSupported() { 157 Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime()); 158 assertEquals(true, test.isSupported(DurationFieldType.years())); 159 assertEquals(false, test.isSupported(DurationFieldType.months())); 160 assertEquals(false, test.isSupported(DurationFieldType.weeks())); 161 assertEquals(true, test.isSupported(DurationFieldType.days())); 162 assertEquals(true, test.isSupported(DurationFieldType.hours())); 163 assertEquals(true, test.isSupported(DurationFieldType.minutes())); 164 assertEquals(true, test.isSupported(DurationFieldType.seconds())); 165 assertEquals(true, test.isSupported(DurationFieldType.millis())); 166 } 167 168 public void testIndexOf() { 169 Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime()); 170 assertEquals(0, test.indexOf(DurationFieldType.years())); 171 assertEquals(-1, test.indexOf(DurationFieldType.months())); 172 assertEquals(-1, test.indexOf(DurationFieldType.weeks())); 173 assertEquals(1, test.indexOf(DurationFieldType.days())); 174 assertEquals(2, test.indexOf(DurationFieldType.hours())); 175 assertEquals(3, test.indexOf(DurationFieldType.minutes())); 176 assertEquals(4, test.indexOf(DurationFieldType.seconds())); 177 assertEquals(5, test.indexOf(DurationFieldType.millis())); 178 } 179 180 public void testGet() { 181 Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime()); 182 assertEquals(1, test.get(DurationFieldType.years())); 183 assertEquals(0, test.get(DurationFieldType.months())); 184 assertEquals(0, test.get(DurationFieldType.weeks())); 185 assertEquals(4, test.get(DurationFieldType.days())); 186 assertEquals(5, test.get(DurationFieldType.hours())); 187 assertEquals(6, test.get(DurationFieldType.minutes())); 188 assertEquals(7, test.get(DurationFieldType.seconds())); 189 assertEquals(8, test.get(DurationFieldType.millis())); 190 } 191 192 public void testEqualsHashCode() { 193 Period test1 = new Period(123L); 194 Period test2 = new Period(123L); 195 assertEquals(true, test1.equals(test2)); 196 assertEquals(true, test2.equals(test1)); 197 assertEquals(true, test1.equals(test1)); 198 assertEquals(true, test2.equals(test2)); 199 assertEquals(true, test1.hashCode() == test2.hashCode()); 200 assertEquals(true, test1.hashCode() == test1.hashCode()); 201 assertEquals(true, test2.hashCode() == test2.hashCode()); 202 203 Period test3 = new Period(321L); 204 assertEquals(false, test1.equals(test3)); 205 assertEquals(false, test2.equals(test3)); 206 assertEquals(false, test3.equals(test1)); 207 assertEquals(false, test3.equals(test2)); 208 assertEquals(false, test1.hashCode() == test3.hashCode()); 209 assertEquals(false, test2.hashCode() == test3.hashCode()); 210 211 assertEquals(false, test1.equals("Hello")); 212 assertEquals(true, test1.equals(new MockPeriod(123L))); 213 assertEquals(false, test1.equals(new Period(123L, PeriodType.dayTime()))); 214 } 215 216 class MockPeriod extends BasePeriod { 217 public MockPeriod(long value) { 218 super(value, null, null); 219 } 220 } 221 222 public void testSerialization() throws Exception { 224 Period test = new Period(123L); 225 226 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 227 ObjectOutputStream oos = new ObjectOutputStream (baos); 228 oos.writeObject(test); 229 byte[] bytes = baos.toByteArray(); 230 oos.close(); 231 232 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 233 ObjectInputStream ois = new ObjectInputStream (bais); 234 Period result = (Period) ois.readObject(); 235 ois.close(); 236 237 assertEquals(test, result); 238 } 239 240 465 public void testToString() { 467 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8); 468 assertEquals("P1Y2M3W4DT5H6M7.008S", test.toString()); 469 470 test = new Period(0, 0, 0, 0, 0, 0, 0, 0); 471 assertEquals("PT0S", test.toString()); 472 473 test = new Period(12345L); 474 assertEquals("PT12.345S", test.toString()); 475 } 476 477 public void testToPeriod() { 479 Period test = new Period(123L); 480 Period result = test.toPeriod(); 481 assertSame(test, result); 482 } 483 484 public void testToMutablePeriod() { 485 Period test = new Period(123L); 486 MutablePeriod result = test.toMutablePeriod(); 487 assertEquals(test, result); 488 } 489 490 496 public void testToDurationFrom() { 497 Period test = new Period(123L); 498 assertEquals(new Duration(123L), test.toDurationFrom(new Instant(0L))); 499 } 500 501 public void testToDurationTo() { 502 Period test = new Period(123L); 503 assertEquals(new Duration(123L), test.toDurationTo(new Instant(123L))); 504 } 505 506 public void testWithPeriodType1() { 508 Period test = new Period(123L); 509 Period result = test.withPeriodType(PeriodType.standard()); 510 assertSame(test, result); 511 } 512 513 public void testWithPeriodType2() { 514 Period test = new Period(3123L); 515 Period result = test.withPeriodType(PeriodType.dayTime()); 516 assertEquals(3, result.getSeconds()); 517 assertEquals(123, result.getMillis()); 518 assertEquals(PeriodType.dayTime(), result.getPeriodType()); 519 } 520 521 public void testWithPeriodType3() { 522 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard()); 523 try { 524 test.withPeriodType(PeriodType.dayTime()); 525 fail(); 526 } catch (IllegalArgumentException ex) {} 527 } 528 529 public void testWithPeriodType4() { 530 Period test = new Period(3123L); 531 Period result = test.withPeriodType(null); 532 assertEquals(3, result.getSeconds()); 533 assertEquals(123, result.getMillis()); 534 assertEquals(PeriodType.standard(), result.getPeriodType()); 535 } 536 537 public void testWithPeriodType5() { 538 Period test = new Period(1, 2, 0, 4, 5, 6, 7, 8, PeriodType.standard()); 539 Period result = test.withPeriodType(PeriodType.yearMonthDayTime()); 540 assertEquals(PeriodType.yearMonthDayTime(), result.getPeriodType()); 541 assertEquals(1, result.getYears()); 542 assertEquals(2, result.getMonths()); 543 assertEquals(0, result.getWeeks()); 544 assertEquals(4, result.getDays()); 545 assertEquals(5, result.getHours()); 546 assertEquals(6, result.getMinutes()); 547 assertEquals(7, result.getSeconds()); 548 assertEquals(8, result.getMillis()); 549 } 550 551 public void testWithFields1() { 553 Period test1 = new Period(1, 2, 3, 4, 5, 6, 7, 8); 554 Period test2 = new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType.millis()); 555 Period result = test1.withFields(test2); 556 557 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test1); 558 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType.millis()), test2); 559 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 9), result); 560 } 561 562 public void testWithFields2() { 563 Period test1 = new Period(1, 2, 3, 4, 5, 6, 7, 8); 564 Period test2 = null; 565 Period result = test1.withFields(test2); 566 567 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test1); 568 assertSame(test1, result); 569 } 570 571 public void testWithFields3() { 572 Period test1 = new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType.millis()); 573 Period test2 = new Period(1, 2, 3, 4, 5, 6, 7, 8); 574 try { 575 test1.withFields(test2); 576 fail(); 577 } catch (IllegalArgumentException ex) {} 578 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType.millis()), test1); 579 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test2); 580 } 581 582 public void testWithField1() { 584 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8); 585 Period result = test.withField(DurationFieldType.years(), 6); 586 587 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test); 588 assertEquals(new Period(6, 2, 3, 4, 5, 6, 7, 8), result); 589 } 590 591 public void testWithField2() { 592 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8); 593 try { 594 test.withField(null, 6); 595 fail(); 596 } catch (IllegalArgumentException ex) {} 597 } 598 599 public void testWithField3() { 600 Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType.time()); 601 try { 602 test.withField(DurationFieldType.years(), 6); 603 fail(); 604 } catch (IllegalArgumentException ex) {} 605 } 606 607 public void testWithField4() { 608 Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType.time()); 609 Period result = test.withField(DurationFieldType.years(), 0); 610 assertEquals(test, result); 611 } 612 613 public void testWithFieldAdded1() { 615 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8); 616 Period result = test.withFieldAdded(DurationFieldType.years(), 6); 617 618 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test); 619 assertEquals(new Period(7, 2, 3, 4, 5, 6, 7, 8), result); 620 } 621 622 public void testWithFieldAdded2() { 623 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8); 624 try { 625 test.withFieldAdded(null, 0); 626 fail(); 627 } catch (IllegalArgumentException ex) {} 628 } 629 630 public void testWithFieldAdded3() { 631 Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType.time()); 632 try { 633 test.withFieldAdded(DurationFieldType.years(), 6); 634 fail(); 635 } catch (IllegalArgumentException ex) {} 636 } 637 638 public void testWithFieldAdded4() { 639 Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType.time()); 640 Period result = test.withFieldAdded(DurationFieldType.years(), 0); 641 assertEquals(test, result); 642 } 643 644 public void testPeriodStatics() { 646 Period test; 647 test = Period.years(1); 648 assertEquals(test, new Period(1, 0, 0, 0, 0, 0, 0, 0, PeriodType.standard())); 649 test = Period.months(1); 650 assertEquals(test, new Period(0, 1, 0, 0, 0, 0, 0, 0, PeriodType.standard())); 651 test = Period.weeks(1); 652 assertEquals(test, new Period(0, 0, 1, 0, 0, 0, 0, 0, PeriodType.standard())); 653 test = Period.days(1); 654 assertEquals(test, new Period(0, 0, 0, 1, 0, 0, 0, 0, PeriodType.standard())); 655 test = Period.hours(1); 656 assertEquals(test, new Period(0, 0, 0, 0, 1, 0, 0, 0, PeriodType.standard())); 657 test = Period.minutes(1); 658 assertEquals(test, new Period(0, 0, 0, 0, 0, 1, 0, 0, PeriodType.standard())); 659 test = Period.seconds(1); 660 assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 1, 0, PeriodType.standard())); 661 test = Period.millis(1); 662 assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 0, 1, PeriodType.standard())); 663 } 664 665 public void testWith() { 667 Period test; 668 test = Period.years(5).withYears(1); 669 assertEquals(test, new Period(1, 0, 0, 0, 0, 0, 0, 0, PeriodType.standard())); 670 test = Period.months(5).withMonths(1); 671 assertEquals(test, new Period(0, 1, 0, 0, 0, 0, 0, 0, PeriodType.standard())); 672 test = Period.weeks(5).withWeeks(1); 673 assertEquals(test, new Period(0, 0, 1, 0, 0, 0, 0, 0, PeriodType.standard())); 674 test = Period.days(5).withDays(1); 675 assertEquals(test, new Period(0, 0, 0, 1, 0, 0, 0, 0, PeriodType.standard())); 676 test = Period.hours(5).withHours(1); 677 assertEquals(test, new Period(0, 0, 0, 0, 1, 0, 0, 0, PeriodType.standard())); 678 test = Period.minutes(5).withMinutes(1); 679 assertEquals(test, new Period(0, 0, 0, 0, 0, 1, 0, 0, PeriodType.standard())); 680 test = Period.seconds(5).withSeconds(1); 681 assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 1, 0, PeriodType.standard())); 682 test = Period.millis(5).withMillis(1); 683 assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 0, 1, PeriodType.standard())); 684 685 test = new Period(0L, PeriodType.millis()); 686 try { 687 test.withYears(1); 688 fail(); 689 } catch (IllegalArgumentException ex) {} 690 } 691 692 public void testPlus() { 694 Period test; 695 test = Period.years(1).plusYears(1); 696 assertEquals(new Period(2, 0, 0, 0, 0, 0, 0, 0, PeriodType.standard()), test); 697 test = Period.months(1).plusMonths(1); 698 assertEquals(new Period(0, 2, 0, 0, 0, 0, 0, 0, PeriodType.standard()), test); 699 test = Period.weeks(1).plusWeeks(1); 700 assertEquals(new Period(0, 0, 2, 0, 0, 0, 0, 0, PeriodType.standard()), test); 701 test = Period.days(1).plusDays(1); 702 assertEquals(new Period(0, 0, 0, 2, 0, 0, 0, 0, PeriodType.standard()), test); 703 test = Period.hours(1).plusHours(1); 704 assertEquals(new Period(0, 0, 0, 0, 2, 0, 0, 0, PeriodType.standard()), test); 705 test = Period.minutes(1).plusMinutes(1); 706 assertEquals(new Period(0, 0, 0, 0, 0, 2, 0, 0, PeriodType.standard()), test); 707 test = Period.seconds(1).plusSeconds(1); 708 assertEquals(new Period(0, 0, 0, 0, 0, 0, 2, 0, PeriodType.standard()), test); 709 test = Period.millis(1).plusMillis(1); 710 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 2, PeriodType.standard()), test); 711 712 test = new Period(0L, PeriodType.millis()); 713 try { 714 test.plusYears(1); 715 fail(); 716 } catch (IllegalArgumentException ex) {} 717 } 718 719 public void testPlusZero() { 720 Period test, result; 721 test = Period.years(1); 722 result = test.plusYears(0); 723 assertSame(test, result); 724 test = Period.months(1); 725 result = test.plusMonths(0); 726 assertSame(test, result); 727 test = Period.weeks(1); 728 result = test.plusWeeks(0); 729 assertSame(test, result); 730 test = Period.days(1); 731 result = test.plusDays(0); 732 assertSame(test, result); 733 test = Period.hours(1); 734 result = test.plusHours(0); 735 assertSame(test, result); 736 test = Period.minutes(1); 737 result = test.plusMinutes(0); 738 assertSame(test, result); 739 test = Period.seconds(1); 740 result = test.plusSeconds(0); 741 assertSame(test, result); 742 test = Period.millis(1); 743 result = test.plusMillis(0); 744 assertSame(test, result); 745 } 746 747 public void testMinus() { 748 Period test; 749 test = Period.years(3).minusYears(1); 750 assertEquals(new Period(2, 0, 0, 0, 0, 0, 0, 0, PeriodType.standard()), test); 751 test = Period.months(3).minusMonths(1); 752 assertEquals(new Period(0, 2, 0, 0, 0, 0, 0, 0, PeriodType.standard()), test); 753 test = Period.weeks(3).minusWeeks(1); 754 assertEquals(new Period(0, 0, 2, 0, 0, 0, 0, 0, PeriodType.standard()), test); 755 test = Period.days(3).minusDays(1); 756 assertEquals(new Period(0, 0, 0, 2, 0, 0, 0, 0, PeriodType.standard()), test); 757 test = Period.hours(3).minusHours(1); 758 assertEquals(new Period(0, 0, 0, 0, 2, 0, 0, 0, PeriodType.standard()), test); 759 test = Period.minutes(3).minusMinutes(1); 760 assertEquals(new Period(0, 0, 0, 0, 0, 2, 0, 0, PeriodType.standard()), test); 761 test = Period.seconds(3).minusSeconds(1); 762 assertEquals(new Period(0, 0, 0, 0, 0, 0, 2, 0, PeriodType.standard()), test); 763 test = Period.millis(3).minusMillis(1); 764 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 2, PeriodType.standard()), test); 765 766 test = new Period(0L, PeriodType.millis()); 767 try { 768 test.minusYears(1); 769 fail(); 770 } catch (IllegalArgumentException ex) {} 771 } 772 773 } 774 | Popular Tags |