View Javadoc

1   /*
2    *  Copyright 2001-2006 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  
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  /**
27   * This class is a Junit unit test for Days.
28   *
29   * @author Stephen Colebourne
30   */
31  public class TestDays extends TestCase {
32      // Test in 2002/03 as time zones are more well known
33      // (before the late 90's they were all over the place)
34      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
35  
36      public static void main(String[] args) {
37          junit.textui.TestRunner.run(suite());
38      }
39  
40      public static TestSuite suite() {
41          return new TestSuite(TestDays.class);
42      }
43  
44      public TestDays(String name) {
45          super(name);
46      }
47  
48      protected void setUp() throws Exception {
49      }
50  
51      protected void tearDown() throws Exception {
52      }
53  
54      //-----------------------------------------------------------------------
55      public void testConstants() {
56          assertEquals(0, Days.ZERO.getDays());
57          assertEquals(1, Days.ONE.getDays());
58          assertEquals(2, Days.TWO.getDays());
59          assertEquals(3, Days.THREE.getDays());
60          assertEquals(4, Days.FOUR.getDays());
61          assertEquals(5, Days.FIVE.getDays());
62          assertEquals(6, Days.SIX.getDays());
63          assertEquals(7, Days.SEVEN.getDays());
64          assertEquals(Integer.MAX_VALUE, Days.MAX_VALUE.getDays());
65          assertEquals(Integer.MIN_VALUE, Days.MIN_VALUE.getDays());
66      }
67  
68      //-----------------------------------------------------------------------
69      public void testFactory_days_int() {
70          assertSame(Days.ZERO, Days.days(0));
71          assertSame(Days.ONE, Days.days(1));
72          assertSame(Days.TWO, Days.days(2));
73          assertSame(Days.THREE, Days.days(3));
74          assertSame(Days.FOUR, Days.days(4));
75          assertSame(Days.FIVE, Days.days(5));
76          assertSame(Days.SIX, Days.days(6));
77          assertSame(Days.SEVEN, Days.days(7));
78          assertSame(Days.MAX_VALUE, Days.days(Integer.MAX_VALUE));
79          assertSame(Days.MIN_VALUE, Days.days(Integer.MIN_VALUE));
80          assertEquals(-1, Days.days(-1).getDays());
81          assertEquals(8, Days.days(8).getDays());
82      }
83  
84      //-----------------------------------------------------------------------
85      public void testFactory_daysBetween_RInstant() {
86          DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
87          DateTime end1 = new DateTime(2006, 6, 12, 12, 0, 0, 0, PARIS);
88          DateTime end2 = new DateTime(2006, 6, 15, 18, 0, 0, 0, PARIS);
89          
90          assertEquals(3, Days.daysBetween(start, end1).getDays());
91          assertEquals(0, Days.daysBetween(start, start).getDays());
92          assertEquals(0, Days.daysBetween(end1, end1).getDays());
93          assertEquals(-3, Days.daysBetween(end1, start).getDays());
94          assertEquals(6, Days.daysBetween(start, end2).getDays());
95      }
96  
97      public void testFactory_daysBetween_RPartial() {
98          LocalDate start = new LocalDate(2006, 6, 9);
99          LocalDate end1 = new LocalDate(2006, 6, 12);
100         YearMonthDay end2 = new YearMonthDay(2006, 6, 15);
101         
102         assertEquals(3, Days.daysBetween(start, end1).getDays());
103         assertEquals(0, Days.daysBetween(start, start).getDays());
104         assertEquals(0, Days.daysBetween(end1, end1).getDays());
105         assertEquals(-3, Days.daysBetween(end1, start).getDays());
106         assertEquals(6, Days.daysBetween(start, end2).getDays());
107     }
108 
109     public void testFactory_daysIn_RInterval() {
110         DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
111         DateTime end1 = new DateTime(2006, 6, 12, 12, 0, 0, 0, PARIS);
112         DateTime end2 = new DateTime(2006, 6, 15, 18, 0, 0, 0, PARIS);
113         
114         assertEquals(0, Days.daysIn((ReadableInterval) null).getDays());
115         assertEquals(3, Days.daysIn(new Interval(start, end1)).getDays());
116         assertEquals(0, Days.daysIn(new Interval(start, start)).getDays());
117         assertEquals(0, Days.daysIn(new Interval(end1, end1)).getDays());
118         assertEquals(6, Days.daysIn(new Interval(start, end2)).getDays());
119     }
120 
121     public void testFactory_standardDaysIn_RPeriod() {
122         assertEquals(0, Days.standardDaysIn((ReadablePeriod) null).getDays());
123         assertEquals(0, Days.standardDaysIn(Period.ZERO).getDays());
124         assertEquals(1, Days.standardDaysIn(new Period(0, 0, 0, 1, 0, 0, 0, 0)).getDays());
125         assertEquals(123, Days.standardDaysIn(Period.days(123)).getDays());
126         assertEquals(-987, Days.standardDaysIn(Period.days(-987)).getDays());
127         assertEquals(1, Days.standardDaysIn(Period.hours(47)).getDays());
128         assertEquals(2, Days.standardDaysIn(Period.hours(48)).getDays());
129         assertEquals(2, Days.standardDaysIn(Period.hours(49)).getDays());
130         assertEquals(14, Days.standardDaysIn(Period.weeks(2)).getDays());
131         try {
132             Days.standardDaysIn(Period.months(1));
133             fail();
134         } catch (IllegalArgumentException ex) {
135             // expeceted
136         }
137     }
138 
139     public void testFactory_parseDays_String() {
140         assertEquals(0, Days.parseDays((String) null).getDays());
141         assertEquals(0, Days.parseDays("P0D").getDays());
142         assertEquals(1, Days.parseDays("P1D").getDays());
143         assertEquals(-3, Days.parseDays("P-3D").getDays());
144         assertEquals(2, Days.parseDays("P0Y0M2D").getDays());
145         assertEquals(2, Days.parseDays("P2DT0H0M").getDays());
146         try {
147             Days.parseDays("P1Y1D");
148             fail();
149         } catch (IllegalArgumentException ex) {
150             // expeceted
151         }
152         try {
153             Days.parseDays("P1DT1H");
154             fail();
155         } catch (IllegalArgumentException ex) {
156             // expeceted
157         }
158     }
159 
160     //-----------------------------------------------------------------------
161     public void testGetMethods() {
162         Days test = Days.days(20);
163         assertEquals(20, test.getDays());
164     }
165 
166     public void testGetFieldType() {
167         Days test = Days.days(20);
168         assertEquals(DurationFieldType.days(), test.getFieldType());
169     }
170 
171     public void testGetPeriodType() {
172         Days test = Days.days(20);
173         assertEquals(PeriodType.days(), test.getPeriodType());
174     }
175 
176     //-----------------------------------------------------------------------
177     public void testIsGreaterThan() {
178         assertEquals(true, Days.THREE.isGreaterThan(Days.TWO));
179         assertEquals(false, Days.THREE.isGreaterThan(Days.THREE));
180         assertEquals(false, Days.TWO.isGreaterThan(Days.THREE));
181         assertEquals(true, Days.ONE.isGreaterThan(null));
182         assertEquals(false, Days.days(-1).isGreaterThan(null));
183     }
184 
185     public void testIsLessThan() {
186         assertEquals(false, Days.THREE.isLessThan(Days.TWO));
187         assertEquals(false, Days.THREE.isLessThan(Days.THREE));
188         assertEquals(true, Days.TWO.isLessThan(Days.THREE));
189         assertEquals(false, Days.ONE.isLessThan(null));
190         assertEquals(true, Days.days(-1).isLessThan(null));
191     }
192 
193     //-----------------------------------------------------------------------
194     public void testToString() {
195         Days test = Days.days(20);
196         assertEquals("P20D", test.toString());
197         
198         test = Days.days(-20);
199         assertEquals("P-20D", test.toString());
200     }
201 
202     //-----------------------------------------------------------------------
203     public void testSerialization() throws Exception {
204         Days test = Days.SEVEN;
205         
206         ByteArrayOutputStream baos = new ByteArrayOutputStream();
207         ObjectOutputStream oos = new ObjectOutputStream(baos);
208         oos.writeObject(test);
209         byte[] bytes = baos.toByteArray();
210         oos.close();
211         
212         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
213         ObjectInputStream ois = new ObjectInputStream(bais);
214         Days result = (Days) ois.readObject();
215         ois.close();
216         
217         assertSame(test, result);
218     }
219 
220     //-----------------------------------------------------------------------
221     public void testToStandardWeeks() {
222         Days test = Days.days(14);
223         Weeks expected = Weeks.weeks(2);
224         assertEquals(expected, test.toStandardWeeks());
225     }
226 
227     public void testToStandardHours() {
228         Days test = Days.days(2);
229         Hours expected = Hours.hours(2 * 24);
230         assertEquals(expected, test.toStandardHours());
231         
232         try {
233             Days.MAX_VALUE.toStandardHours();
234             fail();
235         } catch (ArithmeticException ex) {
236             // expected
237         }
238     }
239 
240     public void testToStandardMinutes() {
241         Days test = Days.days(2);
242         Minutes expected = Minutes.minutes(2 * 24 * 60);
243         assertEquals(expected, test.toStandardMinutes());
244         
245         try {
246             Days.MAX_VALUE.toStandardMinutes();
247             fail();
248         } catch (ArithmeticException ex) {
249             // expected
250         }
251     }
252 
253     public void testToStandardSeconds() {
254         Days test = Days.days(2);
255         Seconds expected = Seconds.seconds(2 * 24 * 60 * 60);
256         assertEquals(expected, test.toStandardSeconds());
257         
258         try {
259             Days.MAX_VALUE.toStandardSeconds();
260             fail();
261         } catch (ArithmeticException ex) {
262             // expected
263         }
264     }
265 
266     public void testToStandardDuration() {
267         Days test = Days.days(20);
268         Duration expected = new Duration(20L * DateTimeConstants.MILLIS_PER_DAY);
269         assertEquals(expected, test.toStandardDuration());
270         
271         expected = new Duration(((long) Integer.MAX_VALUE) * DateTimeConstants.MILLIS_PER_DAY);
272         assertEquals(expected, Days.MAX_VALUE.toStandardDuration());
273     }
274 
275     //-----------------------------------------------------------------------
276     public void testPlus_int() {
277         Days test2 = Days.days(2);
278         Days result = test2.plus(3);
279         assertEquals(2, test2.getDays());
280         assertEquals(5, result.getDays());
281         
282         assertEquals(1, Days.ONE.plus(0).getDays());
283         
284         try {
285             Days.MAX_VALUE.plus(1);
286             fail();
287         } catch (ArithmeticException ex) {
288             // expected
289         }
290     }
291 
292     public void testPlus_Days() {
293         Days test2 = Days.days(2);
294         Days test3 = Days.days(3);
295         Days result = test2.plus(test3);
296         assertEquals(2, test2.getDays());
297         assertEquals(3, test3.getDays());
298         assertEquals(5, result.getDays());
299         
300         assertEquals(1, Days.ONE.plus(Days.ZERO).getDays());
301         assertEquals(1, Days.ONE.plus((Days) null).getDays());
302         
303         try {
304             Days.MAX_VALUE.plus(Days.ONE);
305             fail();
306         } catch (ArithmeticException ex) {
307             // expected
308         }
309     }
310 
311     public void testMinus_int() {
312         Days test2 = Days.days(2);
313         Days result = test2.minus(3);
314         assertEquals(2, test2.getDays());
315         assertEquals(-1, result.getDays());
316         
317         assertEquals(1, Days.ONE.minus(0).getDays());
318         
319         try {
320             Days.MIN_VALUE.minus(1);
321             fail();
322         } catch (ArithmeticException ex) {
323             // expected
324         }
325     }
326 
327     public void testMinus_Days() {
328         Days test2 = Days.days(2);
329         Days test3 = Days.days(3);
330         Days result = test2.minus(test3);
331         assertEquals(2, test2.getDays());
332         assertEquals(3, test3.getDays());
333         assertEquals(-1, result.getDays());
334         
335         assertEquals(1, Days.ONE.minus(Days.ZERO).getDays());
336         assertEquals(1, Days.ONE.minus((Days) null).getDays());
337         
338         try {
339             Days.MIN_VALUE.minus(Days.ONE);
340             fail();
341         } catch (ArithmeticException ex) {
342             // expected
343         }
344     }
345 
346     public void testMultipliedBy_int() {
347         Days test = Days.days(2);
348         assertEquals(6, test.multipliedBy(3).getDays());
349         assertEquals(2, test.getDays());
350         assertEquals(-6, test.multipliedBy(-3).getDays());
351         assertSame(test, test.multipliedBy(1));
352         
353         Days halfMax = Days.days(Integer.MAX_VALUE / 2 + 1);
354         try {
355             halfMax.multipliedBy(2);
356             fail();
357         } catch (ArithmeticException ex) {
358             // expected
359         }
360     }
361 
362     public void testDividedBy_int() {
363         Days test = Days.days(12);
364         assertEquals(6, test.dividedBy(2).getDays());
365         assertEquals(12, test.getDays());
366         assertEquals(4, test.dividedBy(3).getDays());
367         assertEquals(3, test.dividedBy(4).getDays());
368         assertEquals(2, test.dividedBy(5).getDays());
369         assertEquals(2, test.dividedBy(6).getDays());
370         assertSame(test, test.dividedBy(1));
371         
372         try {
373             Days.ONE.dividedBy(0);
374             fail();
375         } catch (ArithmeticException ex) {
376             // expected
377         }
378     }
379 
380     public void testNegated() {
381         Days test = Days.days(12);
382         assertEquals(-12, test.negated().getDays());
383         assertEquals(12, test.getDays());
384         
385         try {
386             Days.MIN_VALUE.negated();
387             fail();
388         } catch (ArithmeticException ex) {
389             // expected
390         }
391     }
392 
393     //-----------------------------------------------------------------------
394     public void testAddToLocalDate() {
395         Days test = Days.days(20);
396         LocalDate date = new LocalDate(2006, 6, 1);
397         LocalDate expected = new LocalDate(2006, 6, 21);
398         assertEquals(expected, date.plus(test));
399     }
400 
401 }