KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > TestDuration_Constructors


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 JavaDoc;
19 import java.util.TimeZone JavaDoc;
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 JavaDoc originalTimeZone = null;
63     private Locale JavaDoc originalLocale = null;
64
65     public static void main(String JavaDoc[] 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 JavaDoc name) {
74         super(name);
75     }
76
77     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
102         Duration test = Duration.ZERO;
103         assertEquals(0, test.getMillis());
104     }
105
106     //-----------------------------------------------------------------------
107
public void testConstructor_long1() throws Throwable JavaDoc {
108         long length = 4 * DateTimeConstants.MILLIS_PER_DAY +
109                 5 * DateTimeConstants.MILLIS_PER_HOUR +
110                 6 * DateTimeConstants.MILLIS_PER_MINUTE +
111                 7 * DateTimeConstants.MILLIS_PER_SECOND + 8;
112         Duration test = new Duration(length);
113         assertEquals(length, test.getMillis());
114     }
115
116     //-----------------------------------------------------------------------
117
public void testConstructor_long_long1() throws Throwable JavaDoc {
118         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
119         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
120         Duration test = new Duration(dt1.getMillis(), dt2.getMillis());
121         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
122     }
123
124     //-----------------------------------------------------------------------
125
public void testConstructor_RI_RI1() throws Throwable JavaDoc {
126         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
127         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
128         Duration test = new Duration(dt1, dt2);
129         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
130     }
131
132     public void testConstructor_RI_RI2() throws Throwable JavaDoc {
133         DateTime dt1 = null; // 2002-06-09T01:00+01:00
134
DateTime dt2 = new DateTime(2005, 7, 17, 1, 1, 1, 1);
135         Duration test = new Duration(dt1, dt2);
136         assertEquals(dt2.getMillis() - TEST_TIME_NOW, test.getMillis());
137     }
138
139     public void testConstructor_RI_RI3() throws Throwable JavaDoc {
140         DateTime dt1 = new DateTime(2005, 7, 17, 1, 1, 1, 1);
141         DateTime dt2 = null; // 2002-06-09T01:00+01:00
142
Duration test = new Duration(dt1, dt2);
143         assertEquals(TEST_TIME_NOW - dt1.getMillis(), test.getMillis());
144     }
145
146     public void testConstructor_RI_RI4() throws Throwable JavaDoc {
147         DateTime dt1 = null; // 2002-06-09T01:00+01:00
148
DateTime dt2 = null; // 2002-06-09T01:00+01:00
149
Duration test = new Duration(dt1, dt2);
150         assertEquals(0L, test.getMillis());
151     }
152
153     //-----------------------------------------------------------------------
154
/**
155      * Test constructor (Object)
156      */

157     public void testConstructor_Object1() throws Throwable JavaDoc {
158         Duration test = new Duration("PT72.345S");
159         assertEquals(72345, test.getMillis());
160     }
161
162     public void testConstructor_Object2() throws Throwable JavaDoc {
163         Duration test = new Duration((Object JavaDoc) null);
164         assertEquals(0L, test.getMillis());
165     }
166
167     public void testConstructor_Object3() throws Throwable JavaDoc {
168         long length = 4 * DateTimeConstants.MILLIS_PER_DAY +
169                 5 * DateTimeConstants.MILLIS_PER_HOUR +
170                 6 * DateTimeConstants.MILLIS_PER_MINUTE +
171                 7 * DateTimeConstants.MILLIS_PER_SECOND + 8;
172         Long JavaDoc base = new Long JavaDoc(length);
173         Duration test = new Duration(base);
174         assertEquals(length, test.getMillis());
175     }
176
177     public void testConstructor_Object4() throws Throwable JavaDoc {
178         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
179         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
180         Duration base = new Duration(dt1, dt2);
181         Duration test = new Duration(base);
182         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
183     }
184
185     public void testConstructor_Object5() throws Throwable JavaDoc {
186         DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
187         DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
188         Interval base = new Interval(dt1, dt2);
189         Duration test = new Duration(base);
190         assertEquals(dt2.getMillis() - dt1.getMillis(), test.getMillis());
191     }
192
193 }
194
Popular Tags