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 testFactory_standardDays_long() throws Throwable {
108         Duration test = Duration.standardDays(1);
109         assertEquals(24L * 60L * 60L * 1000L, test.getMillis());
110         
111         test = Duration.standardDays(2);
112         assertEquals(2L * 24L * 60L * 60L * 1000L, test.getMillis());
113         
114         test = Duration.standardDays(0);
115         assertSame(Duration.ZERO, test);
116     }
117 
118     //-----------------------------------------------------------------------
119     public void testFactory_standardHours_long() throws Throwable {
120         Duration test = Duration.standardHours(1);
121         assertEquals(60L * 60L * 1000L, test.getMillis());
122         
123         test = Duration.standardHours(2);
124         assertEquals(2L * 60L * 60L * 1000L, test.getMillis());
125         
126         test = Duration.standardHours(0);
127         assertSame(Duration.ZERO, test);
128     }
129 
130     //-----------------------------------------------------------------------
131     public void testFactory_standardMinutes_long() throws Throwable {
132         Duration test = Duration.standardMinutes(1);
133         assertEquals(60L * 1000L, test.getMillis());
134         
135         test = Duration.standardMinutes(2);
136         assertEquals(2L * 60L * 1000L, test.getMillis());
137         
138         test = Duration.standardMinutes(0);
139         assertSame(Duration.ZERO, test);
140     }
141 
142     //-----------------------------------------------------------------------
143     public void testFactory_standardSeconds_long() throws Throwable {
144         Duration test = Duration.standardSeconds(1);
145         assertEquals(1000L, test.getMillis());
146         
147         test = Duration.standardSeconds(2);
148         assertEquals(2L * 1000L, test.getMillis());
149         
150         test = Duration.standardSeconds(0);
151         assertSame(Duration.ZERO, test);
152     }
153 
154     //-----------------------------------------------------------------------
155     public void testConstructor_long1() throws Throwable {
156         long length = 4 * DateTimeConstants.MILLIS_PER_DAY +
157                 5 * DateTimeConstants.MILLIS_PER_HOUR +
158                 6 * DateTimeConstants.MILLIS_PER_MINUTE +
159                 7 * DateTimeConstants.MILLIS_PER_SECOND + 8;
160         Duration test = new Duration(length);
161         assertEquals(length, test.getMillis());
162     }
163 
164     //-----------------------------------------------------------------------
165     public void testConstructor_long_long1() throws Throwable {
166         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
167         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
168         Duration test = new Duration(dt1.getMillis(), dt2.getMillis());
169         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
170     }
171 
172     //-----------------------------------------------------------------------
173     public void testConstructor_RI_RI1() throws Throwable {
174         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
175         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
176         Duration test = new Duration(dt1, dt2);
177         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
178     }
179 
180     public void testConstructor_RI_RI2() throws Throwable {
181         DateTime dt1 = null;  // 2002-06-09T01:00+01:00
182         DateTime dt2 = new DateTime(2005, 7, 17, 1, 1, 1, 1);
183         Duration test = new Duration(dt1, dt2);
184         assertEquals(dt2.getMillis() - TEST_TIME_NOW, test.getMillis());
185     }
186 
187     public void testConstructor_RI_RI3() throws Throwable {
188         DateTime dt1 = new DateTime(2005, 7, 17, 1, 1, 1, 1);
189         DateTime dt2 = null;  // 2002-06-09T01:00+01:00
190         Duration test = new Duration(dt1, dt2);
191         assertEquals(TEST_TIME_NOW - dt1.getMillis(), test.getMillis());
192     }
193 
194     public void testConstructor_RI_RI4() throws Throwable {
195         DateTime dt1 = null;  // 2002-06-09T01:00+01:00
196         DateTime dt2 = null;  // 2002-06-09T01:00+01:00
197         Duration test = new Duration(dt1, dt2);
198         assertEquals(0L, test.getMillis());
199     }
200 
201     //-----------------------------------------------------------------------
202     /***
203      * Test constructor (Object)
204      */
205     public void testConstructor_Object1() throws Throwable {
206         Duration test = new Duration("PT72.345S");
207         assertEquals(72345, test.getMillis());
208     }
209 
210     public void testConstructor_Object2() throws Throwable {
211         Duration test = new Duration((Object) null);
212         assertEquals(0L, test.getMillis());
213     }
214 
215     public void testConstructor_Object3() throws Throwable {
216         long length = 4 * DateTimeConstants.MILLIS_PER_DAY +
217                 5 * DateTimeConstants.MILLIS_PER_HOUR +
218                 6 * DateTimeConstants.MILLIS_PER_MINUTE +
219                 7 * DateTimeConstants.MILLIS_PER_SECOND + 8;
220         Long base = new Long(length);
221         Duration test = new Duration(base);
222         assertEquals(length, test.getMillis());
223     }
224 
225     public void testConstructor_Object4() throws Throwable {
226         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
227         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
228         Duration base = new Duration(dt1, dt2);
229         Duration test = new Duration(base);
230         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
231     }
232 
233     public void testConstructor_Object5() throws Throwable {
234         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
235         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
236         Interval base = new Interval(dt1, dt2);
237         Duration test = new Duration(base);
238         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
239     }
240 
241 }