View Javadoc

1   /*
2    *  Copyright 2001-2005 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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   * This class is a Junit unit test for Duration.
36   *
37   * @author Stephen Colebourne
38   */
39  public class TestPeriod_Basics extends TestCase {
40      // Test in 2002/03 as time zones are more well known
41      // (before the late 90's they were all over the place)
42  
43      //private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
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      // 2002-06-09
56      private long TEST_TIME_NOW =
57              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
58              
59      // 2002-04-05
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      // 2003-05-06
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 //    public void testAddTo1() {
245 //        long expected = TEST_TIME_NOW;
246 //        expected = ISOChronology.getInstance().years().add(expected, 1);
247 //        expected = ISOChronology.getInstance().months().add(expected, 2);
248 //        expected = ISOChronology.getInstance().weeks().add(expected, 3);
249 //        expected = ISOChronology.getInstance().days().add(expected, 4);
250 //        expected = ISOChronology.getInstance().hours().add(expected, 5);
251 //        expected = ISOChronology.getInstance().minutes().add(expected, 6);
252 //        expected = ISOChronology.getInstance().seconds().add(expected, 7);
253 //        expected = ISOChronology.getInstance().millis().add(expected, 8);
254 //        
255 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
256 //        long added = test.addTo(TEST_TIME_NOW, 1);
257 //        assertEquals(expected, added);
258 //    }
259 //    
260 //    public void testAddTo2() {
261 //        long expected = TEST_TIME_NOW;
262 //        expected = ISOChronology.getInstance().years().add(expected, -2);
263 //        expected = ISOChronology.getInstance().months().add(expected, -4);
264 //        expected = ISOChronology.getInstance().weeks().add(expected, -6);
265 //        expected = ISOChronology.getInstance().days().add(expected, -8);
266 //        expected = ISOChronology.getInstance().hours().add(expected, -10);
267 //        expected = ISOChronology.getInstance().minutes().add(expected, -12);
268 //        expected = ISOChronology.getInstance().seconds().add(expected, -14);
269 //        expected = ISOChronology.getInstance().millis().add(expected, -16);
270 //        
271 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
272 //        long added = test.addTo(TEST_TIME_NOW, -2);
273 //        assertEquals(expected, added);
274 //    }
275 //    
276 //    public void testAddTo3() {
277 //        long expected = TEST_TIME_NOW;
278 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
279 //        long added = test.addTo(TEST_TIME_NOW, 0);
280 //        assertEquals(expected, added);
281 //    }
282 //    
283 //    public void testAddTo4() {
284 //        long expected = TEST_TIME_NOW + 100L;
285 //        Period test = new Period(100L);
286 //        long added = test.addTo(TEST_TIME_NOW, 1);
287 //        assertEquals(expected, added);
288 //    }
289 //    
290 //    //-----------------------------------------------------------------------
291 //    public void testAddToWithChronology1() {
292 //        long expected = TEST_TIME_NOW;
293 //        expected = ISOChronology.getInstance().years().add(expected, 1);
294 //        expected = ISOChronology.getInstance().months().add(expected, 2);
295 //        expected = ISOChronology.getInstance().weeks().add(expected, 3);
296 //        expected = ISOChronology.getInstance().days().add(expected, 4);
297 //        expected = ISOChronology.getInstance().hours().add(expected, 5);
298 //        expected = ISOChronology.getInstance().minutes().add(expected, 6);
299 //        expected = ISOChronology.getInstance().seconds().add(expected, 7);
300 //        expected = ISOChronology.getInstance().millis().add(expected, 8);
301 //        
302 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
303 //        long added = test.addTo(TEST_TIME_NOW, 1, ISOChronology.getInstance());
304 //        assertEquals(expected, added);
305 //    }
306 //    
307 //    public void testAddToWithChronology2() {
308 //        long expected = TEST_TIME_NOW;
309 //        expected = ISOChronology.getInstanceUTC().years().add(expected, -2);
310 //        expected = ISOChronology.getInstanceUTC().months().add(expected, -4);
311 //        expected = ISOChronology.getInstanceUTC().weeks().add(expected, -6);
312 //        expected = ISOChronology.getInstanceUTC().days().add(expected, -8);
313 //        expected = ISOChronology.getInstanceUTC().hours().add(expected, -10);
314 //        expected = ISOChronology.getInstanceUTC().minutes().add(expected, -12);
315 //        expected = ISOChronology.getInstanceUTC().seconds().add(expected, -14);
316 //        expected = ISOChronology.getInstanceUTC().millis().add(expected, -16);
317 //        
318 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
319 //        long added = test.addTo(TEST_TIME_NOW, -2, ISOChronology.getInstanceUTC());  // local specified so use it
320 //        assertEquals(expected, added);
321 //    }
322 //    
323 //    public void testAddToWithChronology3() {
324 //        long expected = TEST_TIME_NOW;
325 //        expected = ISOChronology.getInstance().years().add(expected, -2);
326 //        expected = ISOChronology.getInstance().months().add(expected, -4);
327 //        expected = ISOChronology.getInstance().weeks().add(expected, -6);
328 //        expected = ISOChronology.getInstance().days().add(expected, -8);
329 //        expected = ISOChronology.getInstance().hours().add(expected, -10);
330 //        expected = ISOChronology.getInstance().minutes().add(expected, -12);
331 //        expected = ISOChronology.getInstance().seconds().add(expected, -14);
332 //        expected = ISOChronology.getInstance().millis().add(expected, -16);
333 //        
334 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
335 //        long added = test.addTo(TEST_TIME_NOW, -2, null);  // no chrono specified so use default
336 //        assertEquals(expected, added);
337 //    }
338 //    
339 //    //-----------------------------------------------------------------------
340 //    public void testAddToRI1() {
341 //        long expected = TEST_TIME_NOW;
342 //        expected = ISOChronology.getInstance().years().add(expected, 1);
343 //        expected = ISOChronology.getInstance().months().add(expected, 2);
344 //        expected = ISOChronology.getInstance().weeks().add(expected, 3);
345 //        expected = ISOChronology.getInstance().days().add(expected, 4);
346 //        expected = ISOChronology.getInstance().hours().add(expected, 5);
347 //        expected = ISOChronology.getInstance().minutes().add(expected, 6);
348 //        expected = ISOChronology.getInstance().seconds().add(expected, 7);
349 //        expected = ISOChronology.getInstance().millis().add(expected, 8);
350 //        
351 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
352 //        DateTime added = test.addTo(new Instant(), 1);  // Instant has no time zone, use default
353 //        assertEquals(expected, added.getMillis());
354 //        assertEquals(ISOChronology.getInstance(), added.getChronology());
355 //    }
356 //    
357 //    public void testAddToRI2() {
358 //        long expected = TEST_TIME_NOW;
359 //        expected = ISOChronology.getInstance().years().add(expected, -2);
360 //        expected = ISOChronology.getInstance().months().add(expected, -4);
361 //        expected = ISOChronology.getInstance().weeks().add(expected, -6);
362 //        expected = ISOChronology.getInstance().days().add(expected, -8);
363 //        expected = ISOChronology.getInstance().hours().add(expected, -10);
364 //        expected = ISOChronology.getInstance().minutes().add(expected, -12);
365 //        expected = ISOChronology.getInstance().seconds().add(expected, -14);
366 //        expected = ISOChronology.getInstance().millis().add(expected, -16);
367 //        
368 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
369 //        DateTime added = test.addTo(new Instant(), -2);  // Instant has no time zone, use default
370 //        assertEquals(expected, added.getMillis());
371 //        assertEquals(ISOChronology.getInstance(), added.getChronology());
372 //    }
373 //    
374 //    public void testAddToRI3() {
375 //        long expected = TEST_TIME_NOW;
376 //        expected = ISOChronology.getInstanceUTC().years().add(expected, -2);
377 //        expected = ISOChronology.getInstanceUTC().months().add(expected, -4);
378 //        expected = ISOChronology.getInstanceUTC().weeks().add(expected, -6);
379 //        expected = ISOChronology.getInstanceUTC().days().add(expected, -8);
380 //        expected = ISOChronology.getInstanceUTC().hours().add(expected, -10);
381 //        expected = ISOChronology.getInstanceUTC().minutes().add(expected, -12);
382 //        expected = ISOChronology.getInstanceUTC().seconds().add(expected, -14);
383 //        expected = ISOChronology.getInstanceUTC().millis().add(expected, -16);
384 //        
385 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
386 //        DateTime added = test.addTo(new DateTime(ISOChronology.getInstanceUTC()), -2);  // DateTime has UTC time zone
387 //        assertEquals(expected, added.getMillis());
388 //        assertEquals(ISOChronology.getInstanceUTC(), added.getChronology());
389 //    }
390 //    
391 //    public void testAddToRI4() {
392 //        long expected = TEST_TIME_NOW;
393 //        expected = ISOChronology.getInstance(PARIS).years().add(expected, -2);
394 //        expected = ISOChronology.getInstance(PARIS).months().add(expected, -4);
395 //        expected = ISOChronology.getInstance(PARIS).weeks().add(expected, -6);
396 //        expected = ISOChronology.getInstance(PARIS).days().add(expected, -8);
397 //        expected = ISOChronology.getInstance(PARIS).hours().add(expected, -10);
398 //        expected = ISOChronology.getInstance(PARIS).minutes().add(expected, -12);
399 //        expected = ISOChronology.getInstance(PARIS).seconds().add(expected, -14);
400 //        expected = ISOChronology.getInstance(PARIS).millis().add(expected, -16);
401 //        
402 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
403 //        DateTime added = test.addTo(new DateTime(PARIS), -2);  // DateTime has PARIS time zone
404 //        assertEquals(expected, added.getMillis());
405 //        assertEquals(ISOChronology.getInstance(PARIS), added.getChronology());
406 //    }
407 //    
408 //    public void testAddToRI5() {
409 //        long expected = TEST_TIME_NOW;
410 //        expected = ISOChronology.getInstance().years().add(expected, -2);
411 //        expected = ISOChronology.getInstance().months().add(expected, -4);
412 //        expected = ISOChronology.getInstance().weeks().add(expected, -6);
413 //        expected = ISOChronology.getInstance().days().add(expected, -8);
414 //        expected = ISOChronology.getInstance().hours().add(expected, -10);
415 //        expected = ISOChronology.getInstance().minutes().add(expected, -12);
416 //        expected = ISOChronology.getInstance().seconds().add(expected, -14);
417 //        expected = ISOChronology.getInstance().millis().add(expected, -16);
418 //        
419 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
420 //        DateTime added = test.addTo(null, -2);  // null has no time zone, use default
421 //        assertEquals(expected, added.getMillis());
422 //        assertEquals(ISOChronology.getInstance(), added.getChronology());
423 //    }
424 //    
425 //    //-----------------------------------------------------------------------
426 //    public void testAddIntoRWI1() {
427 //        long expected = TEST_TIME_NOW;
428 //        expected = ISOChronology.getInstance().years().add(expected, 1);
429 //        expected = ISOChronology.getInstance().months().add(expected, 2);
430 //        expected = ISOChronology.getInstance().weeks().add(expected, 3);
431 //        expected = ISOChronology.getInstance().days().add(expected, 4);
432 //        expected = ISOChronology.getInstance().hours().add(expected, 5);
433 //        expected = ISOChronology.getInstance().minutes().add(expected, 6);
434 //        expected = ISOChronology.getInstance().seconds().add(expected, 7);
435 //        expected = ISOChronology.getInstance().millis().add(expected, 8);
436 //        
437 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
438 //        MutableDateTime mdt = new MutableDateTime();
439 //        test.addInto(mdt, 1);
440 //        assertEquals(expected, mdt.getMillis());
441 //    }
442 //    
443 //    public void testAddIntoRWI2() {
444 //        long expected = TEST_TIME_NOW;
445 //        expected = ISOChronology.getInstance().years().add(expected, -2);
446 //        expected = ISOChronology.getInstance().months().add(expected, -4);
447 //        expected = ISOChronology.getInstance().weeks().add(expected, -6);
448 //        expected = ISOChronology.getInstance().days().add(expected, -8);
449 //        expected = ISOChronology.getInstance().hours().add(expected, -10);
450 //        expected = ISOChronology.getInstance().minutes().add(expected, -12);
451 //        expected = ISOChronology.getInstance().seconds().add(expected, -14);
452 //        expected = ISOChronology.getInstance().millis().add(expected, -16);
453 //        
454 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
455 //        MutableDateTime mdt = new MutableDateTime();
456 //        test.addInto(mdt, -2);  // MutableDateTime has a chronology, use it
457 //        assertEquals(expected, mdt.getMillis());
458 //    }
459 //    
460 //    public void testAddIntoRWI3() {
461 //        Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
462 //        try {
463 //            test.addInto(null, 1);
464 //            fail();
465 //        } catch (IllegalArgumentException ex) {}
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 //    public void testToDurationMillisFrom() {
509 //        Period test = new Period(123L);
510 //        assertEquals(123L, test.toDurationMillisFrom(0L, null));
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         // proof that overflow does not occur
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 //        test = new Period(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
1360 //        try {
1361 //            test.toStandardDuration();
1362 //            fail();
1363 //        } catch (ArithmeticException ex) {}
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 }