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.util.Locale;
23  import java.util.TimeZone;
24  
25  import junit.framework.TestCase;
26  import junit.framework.TestSuite;
27  
28  import org.joda.time.base.BasePeriod;
29  
30  /**
31   * This class is a Junit unit test for MutableDuration.
32   *
33   * @author Stephen Colebourne
34   */
35  public class TestMutablePeriod_Basics extends TestCase {
36      // Test in 2002/03 as time zones are more well known
37      // (before the late 90's they were all over the place)
38  
39      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
40      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
41      
42      long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
43                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
44                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
45                       366 + 365;
46      long y2003days = 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 + 365;
50      
51      // 2002-06-09
52      private long TEST_TIME_NOW =
53              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
54              
55      // 2002-04-05
56      private long TEST_TIME1 =
57              (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
58              + 12L * DateTimeConstants.MILLIS_PER_HOUR
59              + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
60          
61      // 2003-05-06
62      private long TEST_TIME2 =
63              (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
64              + 14L * DateTimeConstants.MILLIS_PER_HOUR
65              + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
66      
67      private DateTimeZone originalDateTimeZone = null;
68      private TimeZone originalTimeZone = null;
69      private Locale originalLocale = null;
70  
71      public static void main(String[] args) {
72          junit.textui.TestRunner.run(suite());
73      }
74  
75      public static TestSuite suite() {
76          return new TestSuite(TestMutablePeriod_Basics.class);
77      }
78  
79      public TestMutablePeriod_Basics(String name) {
80          super(name);
81      }
82  
83      protected void setUp() throws Exception {
84          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
85          originalDateTimeZone = DateTimeZone.getDefault();
86          originalTimeZone = TimeZone.getDefault();
87          originalLocale = Locale.getDefault();
88          DateTimeZone.setDefault(LONDON);
89          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
90          Locale.setDefault(Locale.UK);
91      }
92  
93      protected void tearDown() throws Exception {
94          DateTimeUtils.setCurrentMillisSystem();
95          DateTimeZone.setDefault(originalDateTimeZone);
96          TimeZone.setDefault(originalTimeZone);
97          Locale.setDefault(originalLocale);
98          originalDateTimeZone = null;
99          originalTimeZone = null;
100         originalLocale = null;
101     }
102 
103     //-----------------------------------------------------------------------
104     public void testTest() {
105         assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString());
106         assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString());
107         assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString());
108     }
109 
110     //-----------------------------------------------------------------------
111     public void testGetPeriodType() {
112         MutablePeriod test = new MutablePeriod();
113         assertEquals(PeriodType.standard(), test.getPeriodType());
114     }
115 
116     public void testGetMethods() {
117         MutablePeriod test = new MutablePeriod();
118         assertEquals(0, test.getYears());
119         assertEquals(0, test.getMonths());
120         assertEquals(0, test.getDays());
121         assertEquals(0, test.getWeeks());
122         assertEquals(0, test.getDays());
123         assertEquals(0, test.getHours());
124         assertEquals(0, test.getMinutes());
125         assertEquals(0, test.getSeconds());
126         assertEquals(0, test.getMillis());
127     }
128 
129     public void testEqualsHashCode() {
130         MutablePeriod test1 = new MutablePeriod(123L);
131         MutablePeriod test2 = new MutablePeriod(123L);
132         assertEquals(true, test1.equals(test2));
133         assertEquals(true, test2.equals(test1));
134         assertEquals(true, test1.equals(test1));
135         assertEquals(true, test2.equals(test2));
136         assertEquals(true, test1.hashCode() == test2.hashCode());
137         assertEquals(true, test1.hashCode() == test1.hashCode());
138         assertEquals(true, test2.hashCode() == test2.hashCode());
139         
140         MutablePeriod test3 = new MutablePeriod(321L);
141         assertEquals(false, test1.equals(test3));
142         assertEquals(false, test2.equals(test3));
143         assertEquals(false, test3.equals(test1));
144         assertEquals(false, test3.equals(test2));
145         assertEquals(false, test1.hashCode() == test3.hashCode());
146         assertEquals(false, test2.hashCode() == test3.hashCode());
147         
148         assertEquals(false, test1.equals("Hello"));
149         assertEquals(true, test1.equals(new MockMutablePeriod(123L)));
150         assertEquals(false, test1.equals(new Period(123L, PeriodType.dayTime())));
151     }
152     
153     class MockMutablePeriod extends BasePeriod {
154         public MockMutablePeriod(long value) {
155             super(value, null, null);
156         }
157     }
158 
159     //-----------------------------------------------------------------------
160     public void testSerialization() throws Exception {
161         MutablePeriod test = new MutablePeriod(123L);
162         
163         ByteArrayOutputStream baos = new ByteArrayOutputStream();
164         ObjectOutputStream oos = new ObjectOutputStream(baos);
165         oos.writeObject(test);
166         byte[] bytes = baos.toByteArray();
167         oos.close();
168         
169         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
170         ObjectInputStream ois = new ObjectInputStream(bais);
171         MutablePeriod result = (MutablePeriod) ois.readObject();
172         ois.close();
173         
174         assertEquals(test, result);
175     }
176 
177 //    //-----------------------------------------------------------------------
178 //    public void testAddTo1() {
179 //        long expected = TEST_TIME_NOW;
180 //        expected = ISOChronology.getInstance().years().add(expected, 1);
181 //        expected = ISOChronology.getInstance().months().add(expected, 2);
182 //        expected = ISOChronology.getInstance().weeks().add(expected, 3);
183 //        expected = ISOChronology.getInstance().days().add(expected, 4);
184 //        expected = ISOChronology.getInstance().hours().add(expected, 5);
185 //        expected = ISOChronology.getInstance().minutes().add(expected, 6);
186 //        expected = ISOChronology.getInstance().seconds().add(expected, 7);
187 //        expected = ISOChronology.getInstance().millis().add(expected, 8);
188 //        
189 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
190 //        long added = test.addTo(TEST_TIME_NOW, 1);
191 //        assertEquals(expected, added);
192 //    }
193 //    
194 //    public void testAddTo2() {
195 //        long expected = TEST_TIME_NOW;
196 //        expected = ISOChronology.getInstance().years().add(expected, -2);
197 //        expected = ISOChronology.getInstance().months().add(expected, -4);
198 //        expected = ISOChronology.getInstance().weeks().add(expected, -6);
199 //        expected = ISOChronology.getInstance().days().add(expected, -8);
200 //        expected = ISOChronology.getInstance().hours().add(expected, -10);
201 //        expected = ISOChronology.getInstance().minutes().add(expected, -12);
202 //        expected = ISOChronology.getInstance().seconds().add(expected, -14);
203 //        expected = ISOChronology.getInstance().millis().add(expected, -16);
204 //        
205 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
206 //        long added = test.addTo(TEST_TIME_NOW, -2);
207 //        assertEquals(expected, added);
208 //    }
209 //    
210 //    public void testAddTo3() {
211 //        long expected = TEST_TIME_NOW;
212 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
213 //        long added = test.addTo(TEST_TIME_NOW, 0);
214 //        assertEquals(expected, added);
215 //    }
216 //    
217 //    public void testAddTo4() {
218 //        long expected = TEST_TIME_NOW + 100L;
219 //        MutablePeriod test = new MutablePeriod(100L);
220 //        long added = test.addTo(TEST_TIME_NOW, 1);
221 //        assertEquals(expected, added);
222 //    }
223 //    
224 //    //-----------------------------------------------------------------------
225 //    public void testAddToWithChronology1() {
226 //        long expected = TEST_TIME_NOW;
227 //        expected = ISOChronology.getInstance().years().add(expected, 1);
228 //        expected = ISOChronology.getInstance().months().add(expected, 2);
229 //        expected = ISOChronology.getInstance().weeks().add(expected, 3);
230 //        expected = ISOChronology.getInstance().days().add(expected, 4);
231 //        expected = ISOChronology.getInstance().hours().add(expected, 5);
232 //        expected = ISOChronology.getInstance().minutes().add(expected, 6);
233 //        expected = ISOChronology.getInstance().seconds().add(expected, 7);
234 //        expected = ISOChronology.getInstance().millis().add(expected, 8);
235 //        
236 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
237 //        long added = test.addTo(TEST_TIME_NOW, 1, ISOChronology.getInstance());
238 //        assertEquals(expected, added);
239 //    }
240 //    
241 //    public void testAddToWithChronology2() {
242 //        long expected = TEST_TIME_NOW;
243 //        expected = ISOChronology.getInstanceUTC().years().add(expected, -2);
244 //        expected = ISOChronology.getInstanceUTC().months().add(expected, -4);
245 //        expected = ISOChronology.getInstanceUTC().weeks().add(expected, -6);
246 //        expected = ISOChronology.getInstanceUTC().days().add(expected, -8);
247 //        expected = ISOChronology.getInstanceUTC().hours().add(expected, -10);
248 //        expected = ISOChronology.getInstanceUTC().minutes().add(expected, -12);
249 //        expected = ISOChronology.getInstanceUTC().seconds().add(expected, -14);
250 //        expected = ISOChronology.getInstanceUTC().millis().add(expected, -16);
251 //        
252 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
253 //        long added = test.addTo(TEST_TIME_NOW, -2, ISOChronology.getInstanceUTC());  // chrono specified so use it
254 //        assertEquals(expected, added);
255 //    }
256 //    
257 //    public void testAddToWithChronology3() {
258 //        long expected = TEST_TIME_NOW;
259 //        expected = ISOChronology.getInstance().years().add(expected, -2);
260 //        expected = ISOChronology.getInstance().months().add(expected, -4);
261 //        expected = ISOChronology.getInstance().weeks().add(expected, -6);
262 //        expected = ISOChronology.getInstance().days().add(expected, -8);
263 //        expected = ISOChronology.getInstance().hours().add(expected, -10);
264 //        expected = ISOChronology.getInstance().minutes().add(expected, -12);
265 //        expected = ISOChronology.getInstance().seconds().add(expected, -14);
266 //        expected = ISOChronology.getInstance().millis().add(expected, -16);
267 //        
268 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
269 //        long added = test.addTo(TEST_TIME_NOW, -2, null);  // no chrono so use default
270 //        assertEquals(expected, added);
271 //    }
272 //    
273 //    //-----------------------------------------------------------------------
274 //    public void testAddToRI1() {
275 //        long expected = TEST_TIME_NOW;
276 //        expected = ISOChronology.getInstance().years().add(expected, 1);
277 //        expected = ISOChronology.getInstance().months().add(expected, 2);
278 //        expected = ISOChronology.getInstance().weeks().add(expected, 3);
279 //        expected = ISOChronology.getInstance().days().add(expected, 4);
280 //        expected = ISOChronology.getInstance().hours().add(expected, 5);
281 //        expected = ISOChronology.getInstance().minutes().add(expected, 6);
282 //        expected = ISOChronology.getInstance().seconds().add(expected, 7);
283 //        expected = ISOChronology.getInstance().millis().add(expected, 8);
284 //        
285 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
286 //        DateTime added = test.addTo(new Instant(), 1);  // Instant has no time zone, use default
287 //        assertEquals(expected, added.getMillis());
288 //        assertEquals(ISOChronology.getInstance(), added.getChronology());
289 //    }
290 //    
291 //    public void testAddToRI2() {
292 //        long expected = TEST_TIME_NOW;
293 //        expected = ISOChronology.getInstance().years().add(expected, -2);
294 //        expected = ISOChronology.getInstance().months().add(expected, -4);
295 //        expected = ISOChronology.getInstance().weeks().add(expected, -6);
296 //        expected = ISOChronology.getInstance().days().add(expected, -8);
297 //        expected = ISOChronology.getInstance().hours().add(expected, -10);
298 //        expected = ISOChronology.getInstance().minutes().add(expected, -12);
299 //        expected = ISOChronology.getInstance().seconds().add(expected, -14);
300 //        expected = ISOChronology.getInstance().millis().add(expected, -16);
301 //        
302 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
303 //        DateTime added = test.addTo(new Instant(), -2);  // Instant has no time zone, use default
304 //        assertEquals(expected, added.getMillis());
305 //        assertEquals(ISOChronology.getInstance(), added.getChronology());
306 //    }
307 //    
308 //    public void testAddToRI3() {
309 //        long expected = TEST_TIME_NOW;
310 //        expected = ISOChronology.getInstanceUTC().years().add(expected, -2);
311 //        expected = ISOChronology.getInstanceUTC().months().add(expected, -4);
312 //        expected = ISOChronology.getInstanceUTC().weeks().add(expected, -6);
313 //        expected = ISOChronology.getInstanceUTC().days().add(expected, -8);
314 //        expected = ISOChronology.getInstanceUTC().hours().add(expected, -10);
315 //        expected = ISOChronology.getInstanceUTC().minutes().add(expected, -12);
316 //        expected = ISOChronology.getInstanceUTC().seconds().add(expected, -14);
317 //        expected = ISOChronology.getInstanceUTC().millis().add(expected, -16);
318 //        
319 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
320 //        DateTime added = test.addTo(new DateTime(ISOChronology.getInstanceUTC()), -2);  // DateTime has UTC time zone
321 //        assertEquals(expected, added.getMillis());
322 //        assertEquals(ISOChronology.getInstanceUTC(), added.getChronology());
323 //    }
324 //    
325 //    public void testAddToRI4() {
326 //        long expected = TEST_TIME_NOW;
327 //        expected = ISOChronology.getInstance(PARIS).years().add(expected, -2);
328 //        expected = ISOChronology.getInstance(PARIS).months().add(expected, -4);
329 //        expected = ISOChronology.getInstance(PARIS).weeks().add(expected, -6);
330 //        expected = ISOChronology.getInstance(PARIS).days().add(expected, -8);
331 //        expected = ISOChronology.getInstance(PARIS).hours().add(expected, -10);
332 //        expected = ISOChronology.getInstance(PARIS).minutes().add(expected, -12);
333 //        expected = ISOChronology.getInstance(PARIS).seconds().add(expected, -14);
334 //        expected = ISOChronology.getInstance(PARIS).millis().add(expected, -16);
335 //        
336 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
337 //        DateTime added = test.addTo(new DateTime(PARIS), -2);  // DateTime has PARIS time zone
338 //        assertEquals(expected, added.getMillis());
339 //        assertEquals(ISOChronology.getInstance(PARIS), added.getChronology());
340 //    }
341 //    
342 //    public void testAddToRI5() {
343 //        long expected = TEST_TIME_NOW;
344 //        expected = ISOChronology.getInstance().years().add(expected, -2);
345 //        expected = ISOChronology.getInstance().months().add(expected, -4);
346 //        expected = ISOChronology.getInstance().weeks().add(expected, -6);
347 //        expected = ISOChronology.getInstance().days().add(expected, -8);
348 //        expected = ISOChronology.getInstance().hours().add(expected, -10);
349 //        expected = ISOChronology.getInstance().minutes().add(expected, -12);
350 //        expected = ISOChronology.getInstance().seconds().add(expected, -14);
351 //        expected = ISOChronology.getInstance().millis().add(expected, -16);
352 //        
353 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
354 //        DateTime added = test.addTo(null, -2);  // null has no time zone, use default
355 //        assertEquals(expected, added.getMillis());
356 //        assertEquals(ISOChronology.getInstance(), added.getChronology());
357 //    }
358 //    
359 //    //-----------------------------------------------------------------------
360 //    public void testAddIntoRWI1() {
361 //        long expected = TEST_TIME_NOW;
362 //        expected = ISOChronology.getInstance().years().add(expected, 1);
363 //        expected = ISOChronology.getInstance().months().add(expected, 2);
364 //        expected = ISOChronology.getInstance().weeks().add(expected, 3);
365 //        expected = ISOChronology.getInstance().days().add(expected, 4);
366 //        expected = ISOChronology.getInstance().hours().add(expected, 5);
367 //        expected = ISOChronology.getInstance().minutes().add(expected, 6);
368 //        expected = ISOChronology.getInstance().seconds().add(expected, 7);
369 //        expected = ISOChronology.getInstance().millis().add(expected, 8);
370 //        
371 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
372 //        MutableDateTime mdt = new MutableDateTime();
373 //        test.addInto(mdt, 1);
374 //        assertEquals(expected, mdt.getMillis());
375 //    }
376 //    
377 //    public void testAddIntoRWI2() {
378 //        long expected = TEST_TIME_NOW;
379 //        expected = ISOChronology.getInstance().years().add(expected, -2);
380 //        expected = ISOChronology.getInstance().months().add(expected, -4);
381 //        expected = ISOChronology.getInstance().weeks().add(expected, -6);
382 //        expected = ISOChronology.getInstance().days().add(expected, -8);
383 //        expected = ISOChronology.getInstance().hours().add(expected, -10);
384 //        expected = ISOChronology.getInstance().minutes().add(expected, -12);
385 //        expected = ISOChronology.getInstance().seconds().add(expected, -14);
386 //        expected = ISOChronology.getInstance().millis().add(expected, -16);
387 //        
388 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
389 //        MutableDateTime mdt = new MutableDateTime();
390 //        test.addInto(mdt, -2);  // MutableDateTime has a chronology, use it
391 //        assertEquals(expected, mdt.getMillis());
392 //    }
393 //    
394 //    public void testAddIntoRWI3() {
395 //        MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
396 //        try {
397 //            test.addInto(null, 1);
398 //            fail();
399 //        } catch (IllegalArgumentException ex) {}
400 //    }
401     
402     //-----------------------------------------------------------------------
403     public void testToString() {
404         MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
405         assertEquals("P1Y2M3W4DT5H6M7.008S", test.toString());
406         
407         test = new MutablePeriod(0, 0, 0, 0, 0, 0, 0, 0);
408         assertEquals("PT0S", test.toString());
409         
410         test = new MutablePeriod(12345L);
411         assertEquals("PT12.345S", test.toString());
412     }
413 
414     //-----------------------------------------------------------------------
415     public void testToPeriod() {
416         MutablePeriod test = new MutablePeriod(123L);
417         Period result = test.toPeriod();
418         assertEquals(test, result);
419     }
420 
421     public void testToMutablePeriod() {
422         MutablePeriod test = new MutablePeriod(123L);
423         MutablePeriod result = test.toMutablePeriod();
424         assertEquals(test, result);
425     }
426 
427     //-----------------------------------------------------------------------
428 //    public void testToDurationMillisFrom() {
429 //        MutablePeriod test = new MutablePeriod(123L);
430 //        assertEquals(123L, test.toDurationMillisFrom(0L, null));
431 //    }
432 
433     public void testToDurationFrom() {
434         MutablePeriod test = new MutablePeriod(123L);
435         assertEquals(new Duration(123L), test.toDurationFrom(new Instant(0L)));
436     }
437 
438     //-----------------------------------------------------------------------
439     public void testCopy() {
440         MutablePeriod test = new MutablePeriod(123L);
441         MutablePeriod copy = test.copy();
442         assertEquals(test.getPeriodType(), copy.getPeriodType());
443         assertEquals(test, copy);
444     }
445 
446     //-----------------------------------------------------------------------
447     public void testClone() {
448         MutablePeriod test = new MutablePeriod(123L);
449         MutablePeriod copy = (MutablePeriod) test.clone();
450         assertEquals(test.getPeriodType(), copy.getPeriodType());
451         assertEquals(test, copy);
452     }
453     
454 }