1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.math.BigInteger;
23 import java.util.Arrays;
24 import java.util.Locale;
25 import java.util.TimeZone;
26
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import org.joda.time.base.BasePeriod;
31 import org.joda.time.format.PeriodFormat;
32 import org.joda.time.format.PeriodFormatter;
33
34
35
36
37
38
39 public class TestPeriod_Basics extends TestCase {
40
41
42
43
44 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
45
46 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
47 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
48 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
49 366 + 365;
50 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
51 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
52 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
53 366 + 365 + 365;
54
55
56 private long TEST_TIME_NOW =
57 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
58
59
60 private long TEST_TIME1 =
61 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
62 + 12L * DateTimeConstants.MILLIS_PER_HOUR
63 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
64
65
66 private long TEST_TIME2 =
67 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
68 + 14L * DateTimeConstants.MILLIS_PER_HOUR
69 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
70
71 private DateTimeZone originalDateTimeZone = null;
72 private TimeZone originalTimeZone = null;
73 private Locale originalLocale = null;
74
75 public static void main(String[] args) {
76 junit.textui.TestRunner.run(suite());
77 }
78
79 public static TestSuite suite() {
80 return new TestSuite(TestPeriod_Basics.class);
81 }
82
83 public TestPeriod_Basics(String name) {
84 super(name);
85 }
86
87 protected void setUp() throws Exception {
88 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
89 originalDateTimeZone = DateTimeZone.getDefault();
90 originalTimeZone = TimeZone.getDefault();
91 originalLocale = Locale.getDefault();
92 DateTimeZone.setDefault(LONDON);
93 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
94 Locale.setDefault(Locale.UK);
95 }
96
97 protected void tearDown() throws Exception {
98 DateTimeUtils.setCurrentMillisSystem();
99 DateTimeZone.setDefault(originalDateTimeZone);
100 TimeZone.setDefault(originalTimeZone);
101 Locale.setDefault(originalLocale);
102 originalDateTimeZone = null;
103 originalTimeZone = null;
104 originalLocale = null;
105 }
106
107
108 public void testTest() {
109 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString());
110 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString());
111 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString());
112 }
113
114
115 public void testGetPeriodType() {
116 Period test = new Period(0L);
117 assertEquals(PeriodType.standard(), test.getPeriodType());
118 }
119
120 public void testGetMethods() {
121 Period test = new Period(0L);
122 assertEquals(0, test.getYears());
123 assertEquals(0, test.getMonths());
124 assertEquals(0, test.getWeeks());
125 assertEquals(0, test.getDays());
126 assertEquals(0, test.getHours());
127 assertEquals(0, test.getMinutes());
128 assertEquals(0, test.getSeconds());
129 assertEquals(0, test.getMillis());
130 }
131
132 public void testValueIndexMethods() {
133 Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime());
134 assertEquals(6, test.size());
135 assertEquals(1, test.getValue(0));
136 assertEquals(4, test.getValue(1));
137 assertEquals(5, test.getValue(2));
138 assertEquals(6, test.getValue(3));
139 assertEquals(7, test.getValue(4));
140 assertEquals(8, test.getValue(5));
141 assertEquals(true, Arrays.equals(new int[] {1, 4, 5, 6, 7, 8}, test.getValues()));
142 }
143
144 public void testTypeIndexMethods() {
145 Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime());
146 assertEquals(6, test.size());
147 assertEquals(DurationFieldType.years(), test.getFieldType(0));
148 assertEquals(DurationFieldType.days(), test.getFieldType(1));
149 assertEquals(DurationFieldType.hours(), test.getFieldType(2));
150 assertEquals(DurationFieldType.minutes(), test.getFieldType(3));
151 assertEquals(DurationFieldType.seconds(), test.getFieldType(4));
152 assertEquals(DurationFieldType.millis(), test.getFieldType(5));
153 assertEquals(true, Arrays.equals(new DurationFieldType[] {
154 DurationFieldType.years(), DurationFieldType.days(), DurationFieldType.hours(),
155 DurationFieldType.minutes(), DurationFieldType.seconds(), DurationFieldType.millis()},
156 test.getFieldTypes()));
157 }
158
159 public void testIsSupported() {
160 Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime());
161 assertEquals(true, test.isSupported(DurationFieldType.years()));
162 assertEquals(false, test.isSupported(DurationFieldType.months()));
163 assertEquals(false, test.isSupported(DurationFieldType.weeks()));
164 assertEquals(true, test.isSupported(DurationFieldType.days()));
165 assertEquals(true, test.isSupported(DurationFieldType.hours()));
166 assertEquals(true, test.isSupported(DurationFieldType.minutes()));
167 assertEquals(true, test.isSupported(DurationFieldType.seconds()));
168 assertEquals(true, test.isSupported(DurationFieldType.millis()));
169 }
170
171 public void testIndexOf() {
172 Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime());
173 assertEquals(0, test.indexOf(DurationFieldType.years()));
174 assertEquals(-1, test.indexOf(DurationFieldType.months()));
175 assertEquals(-1, test.indexOf(DurationFieldType.weeks()));
176 assertEquals(1, test.indexOf(DurationFieldType.days()));
177 assertEquals(2, test.indexOf(DurationFieldType.hours()));
178 assertEquals(3, test.indexOf(DurationFieldType.minutes()));
179 assertEquals(4, test.indexOf(DurationFieldType.seconds()));
180 assertEquals(5, test.indexOf(DurationFieldType.millis()));
181 }
182
183 public void testGet() {
184 Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime());
185 assertEquals(1, test.get(DurationFieldType.years()));
186 assertEquals(0, test.get(DurationFieldType.months()));
187 assertEquals(0, test.get(DurationFieldType.weeks()));
188 assertEquals(4, test.get(DurationFieldType.days()));
189 assertEquals(5, test.get(DurationFieldType.hours()));
190 assertEquals(6, test.get(DurationFieldType.minutes()));
191 assertEquals(7, test.get(DurationFieldType.seconds()));
192 assertEquals(8, test.get(DurationFieldType.millis()));
193 }
194
195 public void testEqualsHashCode() {
196 Period test1 = new Period(123L);
197 Period test2 = new Period(123L);
198 assertEquals(true, test1.equals(test2));
199 assertEquals(true, test2.equals(test1));
200 assertEquals(true, test1.equals(test1));
201 assertEquals(true, test2.equals(test2));
202 assertEquals(true, test1.hashCode() == test2.hashCode());
203 assertEquals(true, test1.hashCode() == test1.hashCode());
204 assertEquals(true, test2.hashCode() == test2.hashCode());
205
206 Period test3 = new Period(321L);
207 assertEquals(false, test1.equals(test3));
208 assertEquals(false, test2.equals(test3));
209 assertEquals(false, test3.equals(test1));
210 assertEquals(false, test3.equals(test2));
211 assertEquals(false, test1.hashCode() == test3.hashCode());
212 assertEquals(false, test2.hashCode() == test3.hashCode());
213
214 assertEquals(false, test1.equals("Hello"));
215 assertEquals(true, test1.equals(new MockPeriod(123L)));
216 assertEquals(false, test1.equals(new Period(123L, PeriodType.dayTime())));
217 }
218
219 class MockPeriod extends BasePeriod {
220 public MockPeriod(long value) {
221 super(value, null, null);
222 }
223 }
224
225
226 public void testSerialization() throws Exception {
227 Period test = new Period(123L);
228
229 ByteArrayOutputStream baos = new ByteArrayOutputStream();
230 ObjectOutputStream oos = new ObjectOutputStream(baos);
231 oos.writeObject(test);
232 byte[] bytes = baos.toByteArray();
233 oos.close();
234
235 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
236 ObjectInputStream ois = new ObjectInputStream(bais);
237 Period result = (Period) ois.readObject();
238 ois.close();
239
240 assertEquals(test, result);
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469 public void testToString() {
470 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
471 assertEquals("P1Y2M3W4DT5H6M7.008S", test.toString());
472
473 test = new Period(0, 0, 0, 0, 0, 0, 0, 0);
474 assertEquals("PT0S", test.toString());
475
476 test = new Period(12345L);
477 assertEquals("PT12.345S", test.toString());
478 }
479
480
481 public void testToString_PeriodFormatter() {
482 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
483 assertEquals("1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, 7 seconds and 8 milliseconds", test.toString(PeriodFormat.getDefault()));
484
485 test = new Period(0, 0, 0, 0, 0, 0, 0, 0);
486 assertEquals("0 milliseconds", test.toString(PeriodFormat.getDefault()));
487 }
488
489 public void testToString_nullPeriodFormatter() {
490 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
491 assertEquals("P1Y2M3W4DT5H6M7.008S", test.toString((PeriodFormatter) null));
492 }
493
494
495 public void testToPeriod() {
496 Period test = new Period(123L);
497 Period result = test.toPeriod();
498 assertSame(test, result);
499 }
500
501 public void testToMutablePeriod() {
502 Period test = new Period(123L);
503 MutablePeriod result = test.toMutablePeriod();
504 assertEquals(test, result);
505 }
506
507
508
509
510
511
512
513 public void testToDurationFrom() {
514 Period test = new Period(123L);
515 assertEquals(new Duration(123L), test.toDurationFrom(new Instant(0L)));
516 }
517
518 public void testToDurationTo() {
519 Period test = new Period(123L);
520 assertEquals(new Duration(123L), test.toDurationTo(new Instant(123L)));
521 }
522
523
524 public void testWithPeriodType1() {
525 Period test = new Period(123L);
526 Period result = test.withPeriodType(PeriodType.standard());
527 assertSame(test, result);
528 }
529
530 public void testWithPeriodType2() {
531 Period test = new Period(3123L);
532 Period result = test.withPeriodType(PeriodType.dayTime());
533 assertEquals(3, result.getSeconds());
534 assertEquals(123, result.getMillis());
535 assertEquals(PeriodType.dayTime(), result.getPeriodType());
536 }
537
538 public void testWithPeriodType3() {
539 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
540 try {
541 test.withPeriodType(PeriodType.dayTime());
542 fail();
543 } catch (IllegalArgumentException ex) {}
544 }
545
546 public void testWithPeriodType4() {
547 Period test = new Period(3123L);
548 Period result = test.withPeriodType(null);
549 assertEquals(3, result.getSeconds());
550 assertEquals(123, result.getMillis());
551 assertEquals(PeriodType.standard(), result.getPeriodType());
552 }
553
554 public void testWithPeriodType5() {
555 Period test = new Period(1, 2, 0, 4, 5, 6, 7, 8, PeriodType.standard());
556 Period result = test.withPeriodType(PeriodType.yearMonthDayTime());
557 assertEquals(PeriodType.yearMonthDayTime(), result.getPeriodType());
558 assertEquals(1, result.getYears());
559 assertEquals(2, result.getMonths());
560 assertEquals(0, result.getWeeks());
561 assertEquals(4, result.getDays());
562 assertEquals(5, result.getHours());
563 assertEquals(6, result.getMinutes());
564 assertEquals(7, result.getSeconds());
565 assertEquals(8, result.getMillis());
566 }
567
568
569 public void testWithFields1() {
570 Period test1 = new Period(1, 2, 3, 4, 5, 6, 7, 8);
571 Period test2 = new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType.millis());
572 Period result = test1.withFields(test2);
573
574 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test1);
575 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType.millis()), test2);
576 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 9), result);
577 }
578
579 public void testWithFields2() {
580 Period test1 = new Period(1, 2, 3, 4, 5, 6, 7, 8);
581 Period test2 = null;
582 Period result = test1.withFields(test2);
583
584 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test1);
585 assertSame(test1, result);
586 }
587
588 public void testWithFields3() {
589 Period test1 = new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType.millis());
590 Period test2 = new Period(1, 2, 3, 4, 5, 6, 7, 8);
591 try {
592 test1.withFields(test2);
593 fail();
594 } catch (IllegalArgumentException ex) {}
595 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType.millis()), test1);
596 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test2);
597 }
598
599
600 public void testWithField1() {
601 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
602 Period result = test.withField(DurationFieldType.years(), 6);
603
604 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test);
605 assertEquals(new Period(6, 2, 3, 4, 5, 6, 7, 8), result);
606 }
607
608 public void testWithField2() {
609 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
610 try {
611 test.withField(null, 6);
612 fail();
613 } catch (IllegalArgumentException ex) {}
614 }
615
616 public void testWithField3() {
617 Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType.time());
618 try {
619 test.withField(DurationFieldType.years(), 6);
620 fail();
621 } catch (IllegalArgumentException ex) {}
622 }
623
624 public void testWithField4() {
625 Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType.time());
626 Period result = test.withField(DurationFieldType.years(), 0);
627 assertEquals(test, result);
628 }
629
630
631 public void testWithFieldAdded1() {
632 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
633 Period result = test.withFieldAdded(DurationFieldType.years(), 6);
634
635 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test);
636 assertEquals(new Period(7, 2, 3, 4, 5, 6, 7, 8), result);
637 }
638
639 public void testWithFieldAdded2() {
640 Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
641 try {
642 test.withFieldAdded(null, 0);
643 fail();
644 } catch (IllegalArgumentException ex) {}
645 }
646
647 public void testWithFieldAdded3() {
648 Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType.time());
649 try {
650 test.withFieldAdded(DurationFieldType.years(), 6);
651 fail();
652 } catch (IllegalArgumentException ex) {}
653 }
654
655 public void testWithFieldAdded4() {
656 Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType.time());
657 Period result = test.withFieldAdded(DurationFieldType.years(), 0);
658 assertEquals(test, result);
659 }
660
661
662 public void testPeriodStatics() {
663 Period test;
664 test = Period.years(1);
665 assertEquals(test, new Period(1, 0, 0, 0, 0, 0, 0, 0, PeriodType.standard()));
666 test = Period.months(1);
667 assertEquals(test, new Period(0, 1, 0, 0, 0, 0, 0, 0, PeriodType.standard()));
668 test = Period.weeks(1);
669 assertEquals(test, new Period(0, 0, 1, 0, 0, 0, 0, 0, PeriodType.standard()));
670 test = Period.days(1);
671 assertEquals(test, new Period(0, 0, 0, 1, 0, 0, 0, 0, PeriodType.standard()));
672 test = Period.hours(1);
673 assertEquals(test, new Period(0, 0, 0, 0, 1, 0, 0, 0, PeriodType.standard()));
674 test = Period.minutes(1);
675 assertEquals(test, new Period(0, 0, 0, 0, 0, 1, 0, 0, PeriodType.standard()));
676 test = Period.seconds(1);
677 assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 1, 0, PeriodType.standard()));
678 test = Period.millis(1);
679 assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 0, 1, PeriodType.standard()));
680 }
681
682
683 public void testWith() {
684 Period test;
685 test = Period.years(5).withYears(1);
686 assertEquals(test, new Period(1, 0, 0, 0, 0, 0, 0, 0, PeriodType.standard()));
687 test = Period.months(5).withMonths(1);
688 assertEquals(test, new Period(0, 1, 0, 0, 0, 0, 0, 0, PeriodType.standard()));
689 test = Period.weeks(5).withWeeks(1);
690 assertEquals(test, new Period(0, 0, 1, 0, 0, 0, 0, 0, PeriodType.standard()));
691 test = Period.days(5).withDays(1);
692 assertEquals(test, new Period(0, 0, 0, 1, 0, 0, 0, 0, PeriodType.standard()));
693 test = Period.hours(5).withHours(1);
694 assertEquals(test, new Period(0, 0, 0, 0, 1, 0, 0, 0, PeriodType.standard()));
695 test = Period.minutes(5).withMinutes(1);
696 assertEquals(test, new Period(0, 0, 0, 0, 0, 1, 0, 0, PeriodType.standard()));
697 test = Period.seconds(5).withSeconds(1);
698 assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 1, 0, PeriodType.standard()));
699 test = Period.millis(5).withMillis(1);
700 assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 0, 1, PeriodType.standard()));
701
702 test = new Period(0L, PeriodType.millis());
703 try {
704 test.withYears(1);
705 fail();
706 } catch (UnsupportedOperationException ex) {}
707 }
708
709
710 public void testPlus() {
711 Period base = new Period(1, 2, 3, 4, 5, 6, 7, 8);
712 Period baseDaysOnly = new Period(0, 0, 0, 10, 0, 0, 0, 0, PeriodType.days());
713
714 Period test = base.plus((ReadablePeriod) null);
715 assertSame(base, test);
716
717 test = base.plus(Period.years(10));
718 assertEquals(11, test.getYears());
719 assertEquals(2, test.getMonths());
720 assertEquals(3, test.getWeeks());
721 assertEquals(4, test.getDays());
722 assertEquals(5, test.getHours());
723 assertEquals(6, test.getMinutes());
724 assertEquals(7, test.getSeconds());
725 assertEquals(8, test.getMillis());
726
727 test = base.plus(Years.years(10));
728 assertEquals(11, test.getYears());
729 assertEquals(2, test.getMonths());
730 assertEquals(3, test.getWeeks());
731 assertEquals(4, test.getDays());
732 assertEquals(5, test.getHours());
733 assertEquals(6, test.getMinutes());
734 assertEquals(7, test.getSeconds());
735 assertEquals(8, test.getMillis());
736
737 test = base.plus(Period.days(10));
738 assertEquals(1, test.getYears());
739 assertEquals(2, test.getMonths());
740 assertEquals(3, test.getWeeks());
741 assertEquals(14, test.getDays());
742 assertEquals(5, test.getHours());
743 assertEquals(6, test.getMinutes());
744 assertEquals(7, test.getSeconds());
745 assertEquals(8, test.getMillis());
746
747 test = baseDaysOnly.plus(Period.years(0));
748 assertEquals(0, test.getYears());
749 assertEquals(0, test.getMonths());
750 assertEquals(0, test.getWeeks());
751 assertEquals(10, test.getDays());
752 assertEquals(0, test.getHours());
753 assertEquals(0, test.getMinutes());
754 assertEquals(0, test.getSeconds());
755 assertEquals(0, test.getMillis());
756
757 test = baseDaysOnly.plus(baseDaysOnly);
758 assertEquals(0, test.getYears());
759 assertEquals(0, test.getMonths());
760 assertEquals(0, test.getWeeks());
761 assertEquals(20, test.getDays());
762 assertEquals(0, test.getHours());
763 assertEquals(0, test.getMinutes());
764 assertEquals(0, test.getSeconds());
765 assertEquals(0, test.getMillis());
766
767 try {
768 baseDaysOnly.plus(Period.years(1));
769 fail();
770 } catch (UnsupportedOperationException ex) {}
771
772 try {
773 Period.days(Integer.MAX_VALUE).plus(Period.days(1));
774 fail();
775 } catch (ArithmeticException ex) {}
776
777 try {
778 Period.days(Integer.MIN_VALUE).plus(Period.days(-1));
779 fail();
780 } catch (ArithmeticException ex) {}
781 }
782
783
784 public void testMinus() {
785 Period base = new Period(1, 2, 3, 4, 5, 6, 7, 8);
786 Period baseDaysOnly = new Period(0, 0, 0, 10, 0, 0, 0, 0, PeriodType.days());
787
788 Period test = base.minus((ReadablePeriod) null);
789 assertSame(base, test);
790
791 test = base.minus(Period.years(10));
792 assertEquals(-9, test.getYears());
793 assertEquals(2, test.getMonths());
794 assertEquals(3, test.getWeeks());
795 assertEquals(4, test.getDays());
796 assertEquals(5, test.getHours());
797 assertEquals(6, test.getMinutes());
798 assertEquals(7, test.getSeconds());
799 assertEquals(8, test.getMillis());
800
801 test = base.minus(Years.years(10));
802 assertEquals(-9, test.getYears());
803 assertEquals(2, test.getMonths());
804 assertEquals(3, test.getWeeks());
805 assertEquals(4, test.getDays());
806 assertEquals(5, test.getHours());
807 assertEquals(6, test.getMinutes());
808 assertEquals(7, test.getSeconds());
809 assertEquals(8, test.getMillis());
810
811 test = base.minus(Period.days(10));
812 assertEquals(1, test.getYears());
813 assertEquals(2, test.getMonths());
814 assertEquals(3, test.getWeeks());
815 assertEquals(-6, test.getDays());
816 assertEquals(5, test.getHours());
817 assertEquals(6, test.getMinutes());
818 assertEquals(7, test.getSeconds());
819 assertEquals(8, test.getMillis());
820
821 test = baseDaysOnly.minus(Period.years(0));
822 assertEquals(0, test.getYears());
823 assertEquals(0, test.getMonths());
824 assertEquals(0, test.getWeeks());
825 assertEquals(10, test.getDays());
826 assertEquals(0, test.getHours());
827 assertEquals(0, test.getMinutes());
828 assertEquals(0, test.getSeconds());
829 assertEquals(0, test.getMillis());
830
831 test = baseDaysOnly.minus(baseDaysOnly);
832 assertEquals(0, test.getYears());
833 assertEquals(0, test.getMonths());
834 assertEquals(0, test.getWeeks());
835 assertEquals(0, test.getDays());
836 assertEquals(0, test.getHours());
837 assertEquals(0, test.getMinutes());
838 assertEquals(0, test.getSeconds());
839 assertEquals(0, test.getMillis());
840
841 try {
842 baseDaysOnly.minus(Period.years(1));
843 fail();
844 } catch (UnsupportedOperationException ex) {}
845
846 try {
847 Period.days(Integer.MAX_VALUE).minus(Period.days(-1));
848 fail();
849 } catch (ArithmeticException ex) {}
850
851 try {
852 Period.days(Integer.MIN_VALUE).minus(Period.days(1));
853 fail();
854 } catch (ArithmeticException ex) {}
855 }
856
857
858 public void testPlusFields() {
859 Period test;
860 test = Period.years(1).plusYears(1);
861 assertEquals(new Period(2, 0, 0, 0, 0, 0, 0, 0, PeriodType.standard()), test);
862 test = Period.months(1).plusMonths(1);
863 assertEquals(new Period(0, 2, 0, 0, 0, 0, 0, 0, PeriodType.standard()), test);
864 test = Period.weeks(1).plusWeeks(1);
865 assertEquals(new Period(0, 0, 2, 0, 0, 0, 0, 0, PeriodType.standard()), test);
866 test = Period.days(1).plusDays(1);
867 assertEquals(new Period(0, 0, 0, 2, 0, 0, 0, 0, PeriodType.standard()), test);
868 test = Period.hours(1).plusHours(1);
869 assertEquals(new Period(0, 0, 0, 0, 2, 0, 0, 0, PeriodType.standard()), test);
870 test = Period.minutes(1).plusMinutes(1);
871 assertEquals(new Period(0, 0, 0, 0, 0, 2, 0, 0, PeriodType.standard()), test);
872 test = Period.seconds(1).plusSeconds(1);
873 assertEquals(new Period(0, 0, 0, 0, 0, 0, 2, 0, PeriodType.standard()), test);
874 test = Period.millis(1).plusMillis(1);
875 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 2, PeriodType.standard()), test);
876
877 test = new Period(0L, PeriodType.millis());
878 try {
879 test.plusYears(1);
880 fail();
881 } catch (UnsupportedOperationException ex) {}
882 }
883
884 public void testPlusFieldsZero() {
885 Period test, result;
886 test = Period.years(1);
887 result = test.plusYears(0);
888 assertSame(test, result);
889 test = Period.months(1);
890 result = test.plusMonths(0);
891 assertSame(test, result);
892 test = Period.weeks(1);
893 result = test.plusWeeks(0);
894 assertSame(test, result);
895 test = Period.days(1);
896 result = test.plusDays(0);
897 assertSame(test, result);
898 test = Period.hours(1);
899 result = test.plusHours(0);
900 assertSame(test, result);
901 test = Period.minutes(1);
902 result = test.plusMinutes(0);
903 assertSame(test, result);
904 test = Period.seconds(1);
905 result = test.plusSeconds(0);
906 assertSame(test, result);
907 test = Period.millis(1);
908 result = test.plusMillis(0);
909 assertSame(test, result);
910 }
911
912 public void testMinusFields() {
913 Period test;
914 test = Period.years(3).minusYears(1);
915 assertEquals(new Period(2, 0, 0, 0, 0, 0, 0, 0, PeriodType.standard()), test);
916 test = Period.months(3).minusMonths(1);
917 assertEquals(new Period(0, 2, 0, 0, 0, 0, 0, 0, PeriodType.standard()), test);
918 test = Period.weeks(3).minusWeeks(1);
919 assertEquals(new Period(0, 0, 2, 0, 0, 0, 0, 0, PeriodType.standard()), test);
920 test = Period.days(3).minusDays(1);
921 assertEquals(new Period(0, 0, 0, 2, 0, 0, 0, 0, PeriodType.standard()), test);
922 test = Period.hours(3).minusHours(1);
923 assertEquals(new Period(0, 0, 0, 0, 2, 0, 0, 0, PeriodType.standard()), test);
924 test = Period.minutes(3).minusMinutes(1);
925 assertEquals(new Period(0, 0, 0, 0, 0, 2, 0, 0, PeriodType.standard()), test);
926 test = Period.seconds(3).minusSeconds(1);
927 assertEquals(new Period(0, 0, 0, 0, 0, 0, 2, 0, PeriodType.standard()), test);
928 test = Period.millis(3).minusMillis(1);
929 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 2, PeriodType.standard()), test);
930
931 test = new Period(0L, PeriodType.millis());
932 try {
933 test.minusYears(1);
934 fail();
935 } catch (UnsupportedOperationException ex) {}
936 }
937
938
939 public void testMultipliedBy() {
940 Period base = new Period(1, 2, 3, 4, 5, 6, 7, 8);
941
942 Period test = base.multipliedBy(1);
943 assertSame(base, test);
944
945 test = base.multipliedBy(0);
946 assertEquals(Period.ZERO, test);
947
948 test = base.multipliedBy(2);
949 assertEquals(2, test.getYears());
950 assertEquals(4, test.getMonths());
951 assertEquals(6, test.getWeeks());
952 assertEquals(8, test.getDays());
953 assertEquals(10, test.getHours());
954 assertEquals(12, test.getMinutes());
955 assertEquals(14, test.getSeconds());
956 assertEquals(16, test.getMillis());
957
958 test = base.multipliedBy(3);
959 assertEquals(3, test.getYears());
960 assertEquals(6, test.getMonths());
961 assertEquals(9, test.getWeeks());
962 assertEquals(12, test.getDays());
963 assertEquals(15, test.getHours());
964 assertEquals(18, test.getMinutes());
965 assertEquals(21, test.getSeconds());
966 assertEquals(24, test.getMillis());
967
968 test = base.multipliedBy(-4);
969 assertEquals(-4, test.getYears());
970 assertEquals(-8, test.getMonths());
971 assertEquals(-12, test.getWeeks());
972 assertEquals(-16, test.getDays());
973 assertEquals(-20, test.getHours());
974 assertEquals(-24, test.getMinutes());
975 assertEquals(-28, test.getSeconds());
976 assertEquals(-32, test.getMillis());
977
978 try {
979 Period.days(Integer.MAX_VALUE).multipliedBy(2);
980 fail();
981 } catch (ArithmeticException ex) {}
982
983 try {
984 Period.days(Integer.MIN_VALUE).multipliedBy(2);
985 fail();
986 } catch (ArithmeticException ex) {}
987 }
988
989
990 public void testNegated() {
991 Period base = new Period(1, 2, 3, 4, 5, 6, 7, 8);
992
993 Period test = Period.ZERO.negated();
994 assertEquals(Period.ZERO, test);
995
996 test = base.negated();
997 assertEquals(-1, test.getYears());
998 assertEquals(-2, test.getMonths());
999 assertEquals(-3, test.getWeeks());
1000 assertEquals(-4, test.getDays());
1001 assertEquals(-5, test.getHours());
1002 assertEquals(-6, test.getMinutes());
1003 assertEquals(-7, test.getSeconds());
1004 assertEquals(-8, test.getMillis());
1005
1006 test = Period.days(Integer.MAX_VALUE).negated();
1007 assertEquals(-Integer.MAX_VALUE, test.getDays());
1008
1009 try {
1010 Period.days(Integer.MIN_VALUE).negated();
1011 fail();
1012 } catch (ArithmeticException ex) {}
1013 }
1014
1015
1016 public void testToStandardWeeks() {
1017 Period test = new Period(0, 0, 3, 4, 5, 6, 7, 8);
1018 assertEquals(3, test.toStandardWeeks().getWeeks());
1019
1020 test = new Period(0, 0, 3, 7, 0, 0, 0, 0);
1021 assertEquals(4, test.toStandardWeeks().getWeeks());
1022
1023 test = new Period(0, 0, 0, 6, 23, 59, 59, 1000);
1024 assertEquals(1, test.toStandardWeeks().getWeeks());
1025
1026 test = new Period(0, 0, Integer.MAX_VALUE, 0, 0, 0, 0, 0);
1027 assertEquals(Integer.MAX_VALUE, test.toStandardWeeks().getWeeks());
1028
1029 test = new Period(0, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
1030 long intMax = Integer.MAX_VALUE;
1031 BigInteger expected = BigInteger.valueOf(intMax);
1032 expected = expected.add(BigInteger.valueOf(intMax * DateTimeConstants.MILLIS_PER_SECOND));
1033 expected = expected.add(BigInteger.valueOf(intMax * DateTimeConstants.MILLIS_PER_MINUTE));
1034 expected = expected.add(BigInteger.valueOf(intMax * DateTimeConstants.MILLIS_PER_HOUR));
1035 expected = expected.add(BigInteger.valueOf(intMax * DateTimeConstants.MILLIS_PER_DAY));
1036 expected = expected.divide(BigInteger.valueOf(DateTimeConstants.MILLIS_PER_WEEK));
1037 assertTrue(expected.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) < 0);
1038 assertEquals(expected.longValue(), test.toStandardWeeks().getWeeks());
1039
1040 test = new Period(0, 0, Integer.MAX_VALUE, 7, 0, 0, 0, 0);
1041 try {
1042 test.toStandardWeeks();
1043 fail();
1044 } catch (ArithmeticException ex) {}
1045 }
1046
1047 public void testToStandardWeeks_years() {
1048 Period test = Period.years(1);
1049 try {
1050 test.toStandardWeeks();
1051 fail();
1052 } catch (UnsupportedOperationException ex) {}
1053
1054 test = Period.years(-1);
1055 try {
1056 test.toStandardWeeks();
1057 fail();
1058 } catch (UnsupportedOperationException ex) {}
1059
1060 test = Period.years(0);
1061 assertEquals(0, test.toStandardWeeks().getWeeks());
1062 }
1063
1064 public void testToStandardWeeks_months() {
1065 Period test = Period.months(1);
1066 try {
1067 test.toStandardWeeks();
1068 fail();
1069 } catch (UnsupportedOperationException ex) {}
1070
1071 test = Period.months(-1);
1072 try {
1073 test.toStandardWeeks();
1074 fail();
1075 } catch (UnsupportedOperationException ex) {}
1076
1077 test = Period.months(0);
1078 assertEquals(0, test.toStandardWeeks().getWeeks());
1079 }
1080
1081
1082 public void testToStandardDays() {
1083 Period test = new Period(0, 0, 0, 4, 5, 6, 7, 8);
1084 assertEquals(4, test.toStandardDays().getDays());
1085
1086 test = new Period(0, 0, 1, 4, 0, 0, 0, 0);
1087 assertEquals(11, test.toStandardDays().getDays());
1088
1089 test = new Period(0, 0, 0, 0, 23, 59, 59, 1000);
1090 assertEquals(1, test.toStandardDays().getDays());
1091
1092 test = new Period(0, 0, 0, Integer.MAX_VALUE, 0, 0, 0, 0);
1093 assertEquals(Integer.MAX_VALUE, test.toStandardDays().getDays());
1094
1095 test = new Period(0, 0, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
1096 long intMax = Integer.MAX_VALUE;
1097 BigInteger expected = BigInteger.valueOf(intMax);
1098 expected = expected.add(BigInteger.valueOf(intMax * DateTimeConstants.MILLIS_PER_SECOND));
1099 expected = expected.add(BigInteger.valueOf(intMax * DateTimeConstants.MILLIS_PER_MINUTE));
1100 expected = expected.add(BigInteger.valueOf(intMax * DateTimeConstants.MILLIS_PER_HOUR));
1101 expected = expected.divide(BigInteger.valueOf(DateTimeConstants.MILLIS_PER_DAY));
1102 assertTrue(expected.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) < 0);
1103 assertEquals(expected.longValue(), test.toStandardDays().getDays());
1104
1105 test = new Period(0, 0, 0, Integer.MAX_VALUE, 24, 0, 0, 0);
1106 try {
1107 test.toStandardDays();
1108 fail();
1109 } catch (ArithmeticException ex) {}
1110 }
1111
1112 public void testToStandardDays_years() {
1113 Period test = Period.years(1);
1114 try {
1115 test.toStandardDays();
1116 fail();
1117 } catch (UnsupportedOperationException ex) {}
1118
1119 test = Period.years(-1);
1120 try {
1121 test.toStandardDays();
1122 fail();
1123 } catch (UnsupportedOperationException ex) {}
1124
1125 test = Period.years(0);
1126 assertEquals(0, test.toStandardDays().getDays());
1127 }
1128
1129 public void testToStandardDays_months() {
1130 Period test = Period.months(1);
1131 try {
1132 test.toStandardDays();
1133 fail();
1134 } catch (UnsupportedOperationException ex) {}
1135
1136 test = Period.months(-1);
1137 try {
1138 test.toStandardDays();
1139 fail();
1140 } catch (UnsupportedOperationException ex) {}
1141
1142 test = Period.months(0);
1143 assertEquals(0, test.toStandardDays().getDays());
1144 }
1145
1146
1147 public void testToStandardHours() {
1148 Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8);
1149 assertEquals(5, test.toStandardHours().getHours());
1150
1151 test = new Period(0, 0, 0, 1, 5, 0, 0, 0);
1152 assertEquals(29, test.toStandardHours().getHours());
1153
1154 test = new Period(0, 0, 0, 0, 0, 59, 59, 1000);
1155 assertEquals(1, test.toStandardHours().getHours());
1156
1157 test = new Period(0, 0, 0, 0, Integer.MAX_VALUE, 0, 0, 0);
1158 assertEquals(Integer.MAX_VALUE, test.toStandardHours().getHours());
1159
1160 test = new Period(0, 0, 0, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
1161 long intMax = Integer.MAX_VALUE;
1162 BigInteger expected = BigInteger.valueOf(intMax);
1163 expected = expected.add(BigInteger.valueOf(intMax * DateTimeConstants.MILLIS_PER_SECOND));
1164 expected = expected.add(BigInteger.valueOf(intMax * DateTimeConstants.MILLIS_PER_MINUTE));
1165 expected = expected.divide(BigInteger.valueOf(DateTimeConstants.MILLIS_PER_HOUR));
1166 assertTrue(expected.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) < 0);
1167 assertEquals(expected.longValue(), test.toStandardHours().getHours());
1168
1169 test = new Period(0, 0, 0, 0, Integer.MAX_VALUE, 60, 0, 0);
1170 try {
1171 test.toStandardHours();
1172 fail();
1173 } catch (ArithmeticException ex) {}
1174 }
1175
1176 public void testToStandardHours_years() {
1177 Period test = Period.years(1);
1178 try {
1179 test.toStandardHours();
1180 fail();
1181 } catch (UnsupportedOperationException ex) {}
1182
1183 test = Period.years(-1);
1184 try {
1185 test.toStandardHours();
1186 fail();
1187 } catch (UnsupportedOperationException ex) {}
1188
1189 test = Period.years(0);
1190 assertEquals(0, test.toStandardHours().getHours());
1191 }
1192
1193 public void testToStandardHours_months() {
1194 Period test = Period.months(1);
1195 try {
1196 test.toStandardHours();
1197 fail();
1198 } catch (UnsupportedOperationException ex) {}
1199
1200 test = Period.months(-1);
1201 try {
1202 test.toStandardHours();
1203 fail();
1204 } catch (UnsupportedOperationException ex) {}
1205
1206 test = Period.months(0);
1207 assertEquals(0, test.toStandardHours().getHours());
1208 }
1209
1210
1211 public void testToStandardMinutes() {
1212 Period test = new Period(0, 0, 0, 0, 0, 6, 7, 8);
1213 assertEquals(6, test.toStandardMinutes().getMinutes());
1214
1215 test = new Period(0, 0, 0, 0, 1, 6, 0, 0);
1216 assertEquals(66, test.toStandardMinutes().getMinutes());
1217
1218 test = new Period(0, 0, 0, 0, 0, 0, 59, 1000);
1219 assertEquals(1, test.toStandardMinutes().getMinutes());
1220
1221 test = new Period(0, 0, 0, 0, 0, Integer.MAX_VALUE, 0, 0);
1222 assertEquals(Integer.MAX_VALUE, test.toStandardMinutes().getMinutes());
1223
1224 test = new Period(0, 0, 0, 0, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
1225 long intMax = Integer.MAX_VALUE;
1226 BigInteger expected = BigInteger.valueOf(intMax);
1227 expected = expected.add(BigInteger.valueOf(intMax * DateTimeConstants.MILLIS_PER_SECOND));
1228 expected = expected.divide(BigInteger.valueOf(DateTimeConstants.MILLIS_PER_MINUTE));
1229 assertTrue(expected.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) < 0);
1230 assertEquals(expected.longValue(), test.toStandardMinutes().getMinutes());
1231
1232 test = new Period(0, 0, 0, 0, 0, Integer.MAX_VALUE, 60, 0);
1233 try {
1234 test.toStandardMinutes();
1235 fail();
1236 } catch (ArithmeticException ex) {}
1237 }
1238
1239 public void testToStandardMinutes_years() {
1240 Period test = Period.years(1);
1241 try {
1242 test.toStandardMinutes();
1243 fail();
1244 } catch (UnsupportedOperationException ex) {}
1245
1246 test = Period.years(-1);
1247 try {
1248 test.toStandardMinutes();
1249 fail();
1250 } catch (UnsupportedOperationException ex) {}
1251
1252 test = Period.years(0);
1253 assertEquals(0, test.toStandardMinutes().getMinutes());
1254 }
1255
1256 public void testToStandardMinutes_months() {
1257 Period test = Period.months(1);
1258 try {
1259 test.toStandardMinutes();
1260 fail();
1261 } catch (UnsupportedOperationException ex) {}
1262
1263 test = Period.months(-1);
1264 try {
1265 test.toStandardMinutes();
1266 fail();
1267 } catch (UnsupportedOperationException ex) {}
1268
1269 test = Period.months(0);
1270 assertEquals(0, test.toStandardMinutes().getMinutes());
1271 }
1272
1273
1274 public void testToStandardSeconds() {
1275 Period test = new Period(0, 0, 0, 0, 0, 0, 7, 8);
1276 assertEquals(7, test.toStandardSeconds().getSeconds());
1277
1278 test = new Period(0, 0, 0, 0, 0, 1, 3, 0);
1279 assertEquals(63, test.toStandardSeconds().getSeconds());
1280
1281 test = new Period(0, 0, 0, 0, 0, 0, 0, 1000);
1282 assertEquals(1, test.toStandardSeconds().getSeconds());
1283
1284 test = new Period(0, 0, 0, 0, 0, 0, Integer.MAX_VALUE, 0);
1285 assertEquals(Integer.MAX_VALUE, test.toStandardSeconds().getSeconds());
1286
1287 test = new Period(0, 0, 0, 0, 0, 0, 20, Integer.MAX_VALUE);
1288 long expected = 20;
1289 expected += ((long) Integer.MAX_VALUE) / DateTimeConstants.MILLIS_PER_SECOND;
1290 assertEquals(expected, test.toStandardSeconds().getSeconds());
1291
1292 test = new Period(0, 0, 0, 0, 0, 0, Integer.MAX_VALUE, 1000);
1293 try {
1294 test.toStandardSeconds();
1295 fail();
1296 } catch (ArithmeticException ex) {}
1297 }
1298
1299 public void testToStandardSeconds_years() {
1300 Period test = Period.years(1);
1301 try {
1302 test.toStandardSeconds();
1303 fail();
1304 } catch (UnsupportedOperationException ex) {}
1305
1306 test = Period.years(-1);
1307 try {
1308 test.toStandardSeconds();
1309 fail();
1310 } catch (UnsupportedOperationException ex) {}
1311
1312 test = Period.years(0);
1313 assertEquals(0, test.toStandardSeconds().getSeconds());
1314 }
1315
1316 public void testToStandardSeconds_months() {
1317 Period test = Period.months(1);
1318 try {
1319 test.toStandardSeconds();
1320 fail();
1321 } catch (UnsupportedOperationException ex) {}
1322
1323 test = Period.months(-1);
1324 try {
1325 test.toStandardSeconds();
1326 fail();
1327 } catch (UnsupportedOperationException ex) {}
1328
1329 test = Period.months(0);
1330 assertEquals(0, test.toStandardSeconds().getSeconds());
1331 }
1332
1333
1334 public void testToStandardDuration() {
1335 Period test = new Period(0, 0, 0, 0, 0, 0, 0, 8);
1336 assertEquals(8, test.toStandardDuration().getMillis());
1337
1338 test = new Period(0, 0, 0, 0, 0, 0, 1, 20);
1339 assertEquals(1020, test.toStandardDuration().getMillis());
1340
1341 test = new Period(0, 0, 0, 0, 0, 0, 0, Integer.MAX_VALUE);
1342 assertEquals(Integer.MAX_VALUE, test.toStandardDuration().getMillis());
1343
1344 test = new Period(0, 0, 0, 0, 0, 10, 20, Integer.MAX_VALUE);
1345 long expected = Integer.MAX_VALUE;
1346 expected += 10L * ((long) DateTimeConstants.MILLIS_PER_MINUTE);
1347 expected += 20L * ((long) DateTimeConstants.MILLIS_PER_SECOND);
1348 assertEquals(expected, test.toStandardDuration().getMillis());
1349
1350
1351 BigInteger intMax = BigInteger.valueOf(Integer.MAX_VALUE);
1352 BigInteger exp = intMax;
1353 exp = exp.add(intMax.multiply(BigInteger.valueOf(DateTimeConstants.MILLIS_PER_SECOND)));
1354 exp = exp.add(intMax.multiply(BigInteger.valueOf(DateTimeConstants.MILLIS_PER_MINUTE)));
1355 exp = exp.add(intMax.multiply(BigInteger.valueOf(DateTimeConstants.MILLIS_PER_HOUR)));
1356 exp = exp.add(intMax.multiply(BigInteger.valueOf(DateTimeConstants.MILLIS_PER_DAY)));
1357 exp = exp.add(intMax.multiply(BigInteger.valueOf(DateTimeConstants.MILLIS_PER_WEEK)));
1358 assertTrue(exp.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) < 0);
1359
1360
1361
1362
1363
1364 }
1365
1366 public void testToStandardDuration_years() {
1367 Period test = Period.years(1);
1368 try {
1369 test.toStandardDuration();
1370 fail();
1371 } catch (UnsupportedOperationException ex) {}
1372
1373 test = Period.years(-1);
1374 try {
1375 test.toStandardDuration();
1376 fail();
1377 } catch (UnsupportedOperationException ex) {}
1378
1379 test = Period.years(0);
1380 assertEquals(0, test.toStandardDuration().getMillis());
1381 }
1382
1383 public void testToStandardDuration_months() {
1384 Period test = Period.months(1);
1385 try {
1386 test.toStandardDuration();
1387 fail();
1388 } catch (UnsupportedOperationException ex) {}
1389
1390 test = Period.months(-1);
1391 try {
1392 test.toStandardDuration();
1393 fail();
1394 } catch (UnsupportedOperationException ex) {}
1395
1396 test = Period.months(0);
1397 assertEquals(0, test.toStandardDuration().getMillis());
1398 }
1399
1400
1401 public void testNormalizedStandard_yearMonth1() {
1402 Period test = new Period(1, 15, 0, 0, 0, 0, 0, 0);
1403 Period result = test.normalizedStandard();
1404 assertEquals(new Period(1, 15, 0, 0, 0, 0, 0, 0), test);
1405 assertEquals(new Period(2, 3, 0, 0, 0, 0, 0, 0), result);
1406 }
1407
1408 public void testNormalizedStandard_yearMonth2() {
1409 Period test = new Period(Integer.MAX_VALUE, 15, 0, 0, 0, 0, 0, 0);
1410 try {
1411 test.normalizedStandard();
1412 fail();
1413 } catch (ArithmeticException ex) {}
1414 }
1415
1416 public void testNormalizedStandard_weekDay1() {
1417 Period test = new Period(0, 0, 1, 12, 0, 0, 0, 0);
1418 Period result = test.normalizedStandard();
1419 assertEquals(new Period(0, 0, 1, 12, 0, 0, 0, 0), test);
1420 assertEquals(new Period(0, 0, 2, 5, 0, 0, 0, 0), result);
1421 }
1422
1423 public void testNormalizedStandard_weekDay2() {
1424 Period test = new Period(0, 0, Integer.MAX_VALUE, 7, 0, 0, 0, 0);
1425 try {
1426 test.normalizedStandard();
1427 fail();
1428 } catch (ArithmeticException ex) {}
1429 }
1430
1431 public void testNormalizedStandard_yearMonthWeekDay() {
1432 Period test = new Period(1, 15, 1, 12, 0, 0, 0, 0);
1433 Period result = test.normalizedStandard();
1434 assertEquals(new Period(1, 15, 1, 12, 0, 0, 0, 0), test);
1435 assertEquals(new Period(2, 3, 2, 5, 0, 0, 0, 0), result);
1436 }
1437
1438 public void testNormalizedStandard_yearMonthDay() {
1439 Period test = new Period(1, 15, 0, 36, 0, 0, 0, 0);
1440 Period result = test.normalizedStandard();
1441 assertEquals(new Period(1, 15, 0, 36, 0, 0, 0, 0), test);
1442 assertEquals(new Period(2, 3, 5, 1, 0, 0, 0, 0), result);
1443 }
1444
1445 public void testNormalizedStandard_negative() {
1446 Period test = new Period(0, 0, 0, 0, 2, -10, 0, 0);
1447 Period result = test.normalizedStandard();
1448 assertEquals(new Period(0, 0, 0, 0, 2, -10, 0, 0), test);
1449 assertEquals(new Period(0, 0, 0, 0, 1, 50, 0, 0), result);
1450 }
1451
1452 public void testNormalizedStandard_fullNegative() {
1453 Period test = new Period(0, 0, 0, 0, 1, -70, 0, 0);
1454 Period result = test.normalizedStandard();
1455 assertEquals(new Period(0, 0, 0, 0, 1, -70, 0, 0), test);
1456 assertEquals(new Period(0, 0, 0, 0, 0, -10, 0, 0), result);
1457 }
1458
1459
1460 public void testNormalizedStandard_periodType_yearMonth1() {
1461 Period test = new Period(1, 15, 0, 0, 0, 0, 0, 0);
1462 Period result = test.normalizedStandard((PeriodType) null);
1463 assertEquals(new Period(1, 15, 0, 0, 0, 0, 0, 0), test);
1464 assertEquals(new Period(2, 3, 0, 0, 0, 0, 0, 0), result);
1465 }
1466
1467 public void testNormalizedStandard_periodType_yearMonth2() {
1468 Period test = new Period(Integer.MAX_VALUE, 15, 0, 0, 0, 0, 0, 0);
1469 try {
1470 test.normalizedStandard((PeriodType) null);
1471 fail();
1472 } catch (ArithmeticException ex) {}
1473 }
1474
1475 public void testNormalizedStandard_periodType_yearMonth3() {
1476 Period test = new Period(1, 15, 3, 4, 0, 0, 0, 0);
1477 try {
1478 test.normalizedStandard(PeriodType.dayTime());
1479 fail();
1480 } catch (UnsupportedOperationException ex) {}
1481 }
1482
1483 public void testNormalizedStandard_periodType_weekDay1() {
1484 Period test = new Period(0, 0, 1, 12, 0, 0, 0, 0);
1485 Period result = test.normalizedStandard((PeriodType) null);
1486 assertEquals(new Period(0, 0, 1, 12, 0, 0, 0, 0), test);
1487 assertEquals(new Period(0, 0, 2, 5, 0, 0, 0, 0), result);
1488 }
1489
1490 public void testNormalizedStandard_periodType_weekDay2() {
1491 Period test = new Period(0, 0, Integer.MAX_VALUE, 7, 0, 0, 0, 0);
1492 try {
1493 test.normalizedStandard((PeriodType) null);
1494 fail();
1495 } catch (ArithmeticException ex) {}
1496 }
1497
1498 public void testNormalizedStandard_periodType_weekDay3() {
1499 Period test = new Period(0, 0, 1, 12, 0, 0, 0, 0);
1500 Period result = test.normalizedStandard(PeriodType.dayTime());
1501 assertEquals(new Period(0, 0, 1, 12, 0, 0, 0, 0), test);
1502 assertEquals(new Period(0, 0, 0, 19, 0, 0, 0, 0, PeriodType.dayTime()), result);
1503 }
1504
1505 public void testNormalizedStandard_periodType_yearMonthWeekDay() {
1506 Period test = new Period(1, 15, 1, 12, 0, 0, 0, 0);
1507 Period result = test.normalizedStandard(PeriodType.yearMonthDayTime());
1508 assertEquals(new Period(1, 15, 1, 12, 0, 0, 0, 0), test);
1509 assertEquals(new Period(2, 3, 0, 19, 0, 0, 0, 0, PeriodType.yearMonthDayTime()), result);
1510 }
1511
1512 public void testNormalizedStandard_periodType_yearMonthDay() {
1513 Period test = new Period(1, 15, 0, 36, 27, 0, 0, 0);
1514 Period result = test.normalizedStandard(PeriodType.yearMonthDayTime());
1515 assertEquals(new Period(1, 15, 0, 36, 27, 0, 0, 0), test);
1516 assertEquals(new Period(2, 3, 0, 37, 3, 0, 0, 0, PeriodType.yearMonthDayTime()), result);
1517 }
1518
1519 }