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.util.Locale;
19  import java.util.TimeZone;
20  
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  /**
25   * This class is a JUnit test for Duration.
26   *
27   * @author Stephen Colebourne
28   */
29  public class TestDuration_Constructors extends TestCase {
30      // Test in 2002/03 as time zones are more well known
31      // (before the late 90's they were all over the place)
32  
33      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
34      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
35      
36      long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
37                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
38                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
39                       366 + 365;
40      long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
41                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
42                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
43                       366 + 365 + 365;
44      
45      // 2002-06-09
46      private long TEST_TIME_NOW =
47              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
48              
49      // 2002-04-05
50      private long TEST_TIME1 =
51              (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
52              + 12L * DateTimeConstants.MILLIS_PER_HOUR
53              + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
54          
55      // 2003-05-06
56      private long TEST_TIME2 =
57              (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
58              + 14L * DateTimeConstants.MILLIS_PER_HOUR
59              + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
60      
61      private DateTimeZone originalDateTimeZone = null;
62      private TimeZone originalTimeZone = null;
63      private Locale originalLocale = null;
64  
65      public static void main(String[] args) {
66          junit.textui.TestRunner.run(suite());
67      }
68  
69      public static TestSuite suite() {
70          return new TestSuite(TestDuration_Constructors.class);
71      }
72  
73      public TestDuration_Constructors(String name) {
74          super(name);
75      }
76  
77      protected void setUp() throws Exception {
78          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
79          originalDateTimeZone = DateTimeZone.getDefault();
80          originalTimeZone = TimeZone.getDefault();
81          originalLocale = Locale.getDefault();
82          DateTimeZone.setDefault(LONDON);
83          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
84          Locale.setDefault(Locale.UK);
85      }
86  
87      protected void tearDown() throws Exception {
88          DateTimeUtils.setCurrentMillisSystem();
89          DateTimeZone.setDefault(originalDateTimeZone);
90          TimeZone.setDefault(originalTimeZone);
91          Locale.setDefault(originalLocale);
92          originalDateTimeZone = null;
93          originalTimeZone = null;
94          originalLocale = null;
95      }
96  
97      //-----------------------------------------------------------------------
98      /**
99       * Test constructor ()
100      */
101     public void testZERO() throws Throwable {
102         Duration test = Duration.ZERO;
103         assertEquals(0, test.getMillis());
104     }
105 
106     //-----------------------------------------------------------------------
107     public void testParse_noFormatter() throws Throwable {
108         assertEquals(new Duration(3200), Duration.parse("PT3.2S"));
109         assertEquals(new Duration(6000), Duration.parse("PT6S"));
110     }
111 
112     //-----------------------------------------------------------------------
113     public void testFactory_standardDays_long() throws Throwable {
114         Duration test = Duration.standardDays(1);
115         assertEquals(24L * 60L * 60L * 1000L, test.getMillis());
116         
117         test = Duration.standardDays(2);
118         assertEquals(2L * 24L * 60L * 60L * 1000L, test.getMillis());
119         
120         test = Duration.standardDays(0);
121         assertSame(Duration.ZERO, test);
122     }
123 
124     //-----------------------------------------------------------------------
125     public void testFactory_standardHours_long() throws Throwable {
126         Duration test = Duration.standardHours(1);
127         assertEquals(60L * 60L * 1000L, test.getMillis());
128         
129         test = Duration.standardHours(2);
130         assertEquals(2L * 60L * 60L * 1000L, test.getMillis());
131         
132         test = Duration.standardHours(0);
133         assertSame(Duration.ZERO, test);
134     }
135 
136     //-----------------------------------------------------------------------
137     public void testFactory_standardMinutes_long() throws Throwable {
138         Duration test = Duration.standardMinutes(1);
139         assertEquals(60L * 1000L, test.getMillis());
140         
141         test = Duration.standardMinutes(2);
142         assertEquals(2L * 60L * 1000L, test.getMillis());
143         
144         test = Duration.standardMinutes(0);
145         assertSame(Duration.ZERO, test);
146     }
147 
148     //-----------------------------------------------------------------------
149     public void testFactory_standardSeconds_long() throws Throwable {
150         Duration test = Duration.standardSeconds(1);
151         assertEquals(1000L, test.getMillis());
152         
153         test = Duration.standardSeconds(2);
154         assertEquals(2L * 1000L, test.getMillis());
155         
156         test = Duration.standardSeconds(0);
157         assertSame(Duration.ZERO, test);
158     }
159 
160     //-----------------------------------------------------------------------
161     public void testFactory_millis_long() throws Throwable {
162         Duration test = Duration.millis(1);
163         assertEquals(1L, test.getMillis());
164         
165         test = Duration.millis(2);
166         assertEquals(2L, test.getMillis());
167         
168         test = Duration.millis(0);
169         assertSame(Duration.ZERO, test);
170     }
171 
172     //-----------------------------------------------------------------------
173     public void testConstructor_long1() throws Throwable {
174         long length = 4 * DateTimeConstants.MILLIS_PER_DAY +
175                 5 * DateTimeConstants.MILLIS_PER_HOUR +
176                 6 * DateTimeConstants.MILLIS_PER_MINUTE +
177                 7 * DateTimeConstants.MILLIS_PER_SECOND + 8;
178         Duration test = new Duration(length);
179         assertEquals(length, test.getMillis());
180     }
181 
182     //-----------------------------------------------------------------------
183     public void testConstructor_long_long1() throws Throwable {
184         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
185         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
186         Duration test = new Duration(dt1.getMillis(), dt2.getMillis());
187         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
188     }
189 
190     //-----------------------------------------------------------------------
191     public void testConstructor_RI_RI1() throws Throwable {
192         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
193         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
194         Duration test = new Duration(dt1, dt2);
195         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
196     }
197 
198     public void testConstructor_RI_RI2() throws Throwable {
199         DateTime dt1 = null;  // 2002-06-09T01:00+01:00
200         DateTime dt2 = new DateTime(2005, 7, 17, 1, 1, 1, 1);
201         Duration test = new Duration(dt1, dt2);
202         assertEquals(dt2.getMillis() - TEST_TIME_NOW, test.getMillis());
203     }
204 
205     public void testConstructor_RI_RI3() throws Throwable {
206         DateTime dt1 = new DateTime(2005, 7, 17, 1, 1, 1, 1);
207         DateTime dt2 = null;  // 2002-06-09T01:00+01:00
208         Duration test = new Duration(dt1, dt2);
209         assertEquals(TEST_TIME_NOW - dt1.getMillis(), test.getMillis());
210     }
211 
212     public void testConstructor_RI_RI4() throws Throwable {
213         DateTime dt1 = null;  // 2002-06-09T01:00+01:00
214         DateTime dt2 = null;  // 2002-06-09T01:00+01:00
215         Duration test = new Duration(dt1, dt2);
216         assertEquals(0L, test.getMillis());
217     }
218 
219     //-----------------------------------------------------------------------
220     /**
221      * Test constructor (Object)
222      */
223     public void testConstructor_Object1() throws Throwable {
224         Duration test = new Duration("PT72.345S");
225         assertEquals(72345, test.getMillis());
226     }
227 
228     public void testConstructor_Object2() throws Throwable {
229         Duration test = new Duration((Object) null);
230         assertEquals(0L, test.getMillis());
231     }
232 
233     public void testConstructor_Object3() throws Throwable {
234         long length = 4 * DateTimeConstants.MILLIS_PER_DAY +
235                 5 * DateTimeConstants.MILLIS_PER_HOUR +
236                 6 * DateTimeConstants.MILLIS_PER_MINUTE +
237                 7 * DateTimeConstants.MILLIS_PER_SECOND + 8;
238         Long base = new Long(length);
239         Duration test = new Duration(base);
240         assertEquals(length, test.getMillis());
241     }
242 
243     public void testConstructor_Object4() throws Throwable {
244         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
245         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
246         Duration base = new Duration(dt1, dt2);
247         Duration test = new Duration(base);
248         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
249     }
250 
251     public void testConstructor_Object5() throws Throwable {
252         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
253         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
254         Interval base = new Interval(dt1, dt2);
255         Duration test = new Duration(base);
256         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
257     }
258 
259 }