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  import org.joda.time.base.AbstractInterval;
25  import org.joda.time.chrono.ISOChronology;
26  
27  /**
28   * This class is a Junit unit test for Instant.
29   *
30   * @author Stephen Colebourne
31   */
32  public class TestMutableInterval_Updates extends TestCase {
33      // Test in 2002/03 as time zones are more well known
34      // (before the late 90's they were all over the place)
35  
36      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
37      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
38      
39      long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
40                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
41                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
42                       366 + 365;
43      long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
44                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
45                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
46                       366 + 365 + 365;
47      
48      // 2002-06-09
49      private long TEST_TIME_NOW =
50              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
51              
52      // 2002-04-05
53      private long TEST_TIME1 =
54              (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
55              + 12L * DateTimeConstants.MILLIS_PER_HOUR
56              + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
57          
58      // 2003-05-06
59      private long TEST_TIME2 =
60              (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
61              + 14L * DateTimeConstants.MILLIS_PER_HOUR
62              + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
63          
64      private DateTimeZone originalDateTimeZone = null;
65      private TimeZone originalTimeZone = null;
66      private Locale originalLocale = null;
67  
68      public static void main(String[] args) {
69          junit.textui.TestRunner.run(suite());
70      }
71  
72      public static TestSuite suite() {
73          return new TestSuite(TestMutableInterval_Updates.class);
74      }
75  
76      public TestMutableInterval_Updates(String name) {
77          super(name);
78      }
79  
80      protected void setUp() throws Exception {
81          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
82          originalDateTimeZone = DateTimeZone.getDefault();
83          originalTimeZone = TimeZone.getDefault();
84          originalLocale = Locale.getDefault();
85          DateTimeZone.setDefault(LONDON);
86          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
87          Locale.setDefault(Locale.UK);
88      }
89  
90      protected void tearDown() throws Exception {
91          DateTimeUtils.setCurrentMillisSystem();
92          DateTimeZone.setDefault(originalDateTimeZone);
93          TimeZone.setDefault(originalTimeZone);
94          Locale.setDefault(originalLocale);
95          originalDateTimeZone = null;
96          originalTimeZone = null;
97          originalLocale = null;
98      }
99  
100     //-----------------------------------------------------------------------
101     public void testTest() {
102         assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString());
103         assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString());
104         assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString());
105     }
106 
107     //-----------------------------------------------------------------------
108     public void testSetInterval_long_long1() {
109         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
110         test.setInterval(TEST_TIME1 - 1, TEST_TIME2 + 1);
111         assertEquals(TEST_TIME1 - 1, test.getStartMillis());
112         assertEquals(TEST_TIME2 + 1, test.getEndMillis());
113     }
114 
115     public void testSetInterval_long_long2() {
116         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
117         try {
118             test.setInterval(TEST_TIME1 - 1, TEST_TIME1 - 2);
119             fail();
120         } catch (IllegalArgumentException ex) {}
121     }
122 
123     //-----------------------------------------------------------------------
124     public void testSetInterval_RI_RI1() {
125         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
126         test.setInterval(new Instant(TEST_TIME1 - 1), new Instant(TEST_TIME2 + 1));
127         assertEquals(TEST_TIME1 - 1, test.getStartMillis());
128         assertEquals(TEST_TIME2 + 1, test.getEndMillis());
129     }
130 
131     public void testSetInterval_RI_RI2() {
132         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
133         try {
134             test.setInterval(new Instant(TEST_TIME1 - 1), new Instant(TEST_TIME1 - 2));
135             fail();
136         } catch (IllegalArgumentException ex) {}
137     }
138 
139     public void testSetInterval_RI_RI3() {
140         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
141         test.setInterval(null, new Instant(TEST_TIME2 + 1));
142         assertEquals(TEST_TIME_NOW, test.getStartMillis());
143         assertEquals(TEST_TIME2 + 1, test.getEndMillis());
144     }
145 
146     public void testSetInterval_RI_RI4() {
147         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
148         test.setInterval(new Instant(TEST_TIME1 - 1), null);
149         assertEquals(TEST_TIME1 - 1, test.getStartMillis());
150         assertEquals(TEST_TIME_NOW, test.getEndMillis());
151     }
152 
153     public void testSetInterval_RI_RI5() {
154         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
155         test.setInterval(null, null);
156         assertEquals(TEST_TIME_NOW, test.getStartMillis());
157         assertEquals(TEST_TIME_NOW, test.getEndMillis());
158     }
159 
160     //-----------------------------------------------------------------------
161     public void testSetInterval_RInterval1() {
162         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
163         test.setInterval(new Interval(TEST_TIME1 - 1, TEST_TIME2 + 1));
164         assertEquals(TEST_TIME1 - 1, test.getStartMillis());
165         assertEquals(TEST_TIME2 + 1, test.getEndMillis());
166     }
167 
168     public void testSetInterval_RInterval2() {
169         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
170         try {
171             test.setInterval(new MockBadInterval());
172             fail();
173         } catch (IllegalArgumentException ex) {}
174     }
175     
176     class MockBadInterval extends AbstractInterval {
177         public Chronology getChronology() {
178             return ISOChronology.getInstance();
179         }
180         public long getStartMillis() {
181             return TEST_TIME1 - 1;
182         }
183         public long getEndMillis() {
184             return TEST_TIME1 - 2;
185         }
186     }
187 
188     public void testSetInterval_RInterval3() {
189         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
190         try {
191             test.setInterval(null);
192             fail();
193         } catch (IllegalArgumentException ex) {}
194     }
195     
196     //-----------------------------------------------------------------------
197     public void testSetStartMillis_long1() {
198         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
199         test.setStartMillis(TEST_TIME1 - 1);
200         assertEquals(TEST_TIME1 - 1, test.getStartMillis());
201         assertEquals(TEST_TIME2, test.getEndMillis());
202     }
203 
204     public void testSetStartMillis_long2() {
205         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
206         try {
207             test.setStartMillis(TEST_TIME2 + 1);
208             fail();
209         } catch (IllegalArgumentException ex) {}
210     }
211 
212     //-----------------------------------------------------------------------
213     public void testSetStart_RI1() {
214         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
215         test.setStart(new Instant(TEST_TIME1 - 1));
216         assertEquals(TEST_TIME1 - 1, test.getStartMillis());
217         assertEquals(TEST_TIME2, test.getEndMillis());
218     }
219 
220     public void testSetStart_RI2() {
221         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
222         try {
223             test.setStart(new Instant(TEST_TIME2 + 1));
224             fail();
225         } catch (IllegalArgumentException ex) {}
226     }
227 
228     public void testSetStart_RI3() {
229         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
230         test.setStart(null);
231         assertEquals(TEST_TIME_NOW, test.getStartMillis());
232         assertEquals(TEST_TIME2, test.getEndMillis());
233     }
234 
235     //-----------------------------------------------------------------------
236     public void testSetEndMillis_long1() {
237         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
238         test.setEndMillis(TEST_TIME2 + 1);
239         assertEquals(TEST_TIME1, test.getStartMillis());
240         assertEquals(TEST_TIME2 + 1, test.getEndMillis());
241     }
242 
243     public void testSetEndMillis_long2() {
244         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
245         try {
246             test.setEndMillis(TEST_TIME1 - 1);
247             fail();
248         } catch (IllegalArgumentException ex) {}
249     }
250 
251     //-----------------------------------------------------------------------
252     public void testSetEnd_RI1() {
253         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
254         test.setEnd(new Instant(TEST_TIME2 + 1));
255         assertEquals(TEST_TIME1, test.getStartMillis());
256         assertEquals(TEST_TIME2 + 1, test.getEndMillis());
257     }
258 
259     public void testSetEnd_RI2() {
260         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
261         try {
262             test.setEnd(new Instant(TEST_TIME1 - 1));
263             fail();
264         } catch (IllegalArgumentException ex) {}
265     }
266 
267     public void testSetEnd_RI3() {
268         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
269         test.setEnd(null);
270         assertEquals(TEST_TIME1, test.getStartMillis());
271         assertEquals(TEST_TIME_NOW, test.getEndMillis());
272     }
273 
274     //-----------------------------------------------------------------------
275     public void testSetDurationAfterStart_long1() {
276         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
277         test.setDurationAfterStart(123L);
278         assertEquals(TEST_TIME1, test.getStartMillis());
279         assertEquals(TEST_TIME1 + 123L, test.getEndMillis());
280     }
281 
282     public void testSeDurationAfterStart_long2() {
283         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
284         try {
285             test.setDurationAfterStart(-1);
286             fail();
287         } catch (IllegalArgumentException ex) {}
288     }
289 
290     //-----------------------------------------------------------------------
291     public void testSetDurationAfterStart_RI1() {
292         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
293         test.setDurationAfterStart(new Duration(123L));
294         assertEquals(TEST_TIME1, test.getStartMillis());
295         assertEquals(TEST_TIME1 + 123L, test.getEndMillis());
296     }
297 
298     public void testSeDurationAfterStart_RI2() {
299         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
300         try {
301             test.setDurationAfterStart(new Duration(-1));
302             fail();
303         } catch (IllegalArgumentException ex) {}
304     }
305 
306     public void testSetDurationAfterStart_RI3() {
307         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
308         test.setDurationAfterStart(null);
309         assertEquals(TEST_TIME1, test.getStartMillis());
310         assertEquals(TEST_TIME1, test.getEndMillis());
311     }
312 
313     //-----------------------------------------------------------------------
314     public void testSetDurationBeforeEnd_long1() {
315         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
316         test.setDurationBeforeEnd(123L);
317         assertEquals(TEST_TIME2 - 123L, test.getStartMillis());
318         assertEquals(TEST_TIME2, test.getEndMillis());
319     }
320 
321     public void testSeDurationBeforeEnd_long2() {
322         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
323         try {
324             test.setDurationBeforeEnd(-1);
325             fail();
326         } catch (IllegalArgumentException ex) {}
327     }
328 
329     //-----------------------------------------------------------------------
330     public void testSetDurationBeforeEnd_RI1() {
331         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
332         test.setDurationBeforeEnd(new Duration(123L));
333         assertEquals(TEST_TIME2 - 123L, test.getStartMillis());
334         assertEquals(TEST_TIME2, test.getEndMillis());
335     }
336 
337     public void testSeDurationBeforeEnd_RI2() {
338         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
339         try {
340             test.setDurationBeforeEnd(new Duration(-1));
341             fail();
342         } catch (IllegalArgumentException ex) {}
343     }
344 
345     public void testSetDurationBeforeEnd_RI3() {
346         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
347         test.setDurationBeforeEnd(null);
348         assertEquals(TEST_TIME2, test.getStartMillis());
349         assertEquals(TEST_TIME2, test.getEndMillis());
350     }
351 
352     //-----------------------------------------------------------------------
353     public void testSetPeriodAfterStart_RI1() {
354         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
355         test.setPeriodAfterStart(new Period(123L));
356         assertEquals(TEST_TIME1, test.getStartMillis());
357         assertEquals(TEST_TIME1 + 123L, test.getEndMillis());
358     }
359 
360     public void testSePeriodAfterStart_RI2() {
361         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
362         try {
363             test.setPeriodAfterStart(new Period(-1L));
364             fail();
365         } catch (IllegalArgumentException ex) {}
366     }
367 
368     public void testSetPeriodAfterStart_RI3() {
369         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
370         test.setPeriodAfterStart(null);
371         assertEquals(TEST_TIME1, test.getStartMillis());
372         assertEquals(TEST_TIME1, test.getEndMillis());
373     }
374 
375     //-----------------------------------------------------------------------
376     public void testSetPeriodBeforeEnd_RI1() {
377         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
378         test.setPeriodBeforeEnd(new Period(123L));
379         assertEquals(TEST_TIME2 - 123L, test.getStartMillis());
380         assertEquals(TEST_TIME2, test.getEndMillis());
381     }
382 
383     public void testSePeriodBeforeEnd_RI2() {
384         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
385         try {
386             test.setPeriodBeforeEnd(new Period(-1L));
387             fail();
388         } catch (IllegalArgumentException ex) {}
389     }
390 
391     public void testSetPeriodBeforeEnd_RI3() {
392         MutableInterval test = new MutableInterval(TEST_TIME1, TEST_TIME2);
393         test.setPeriodBeforeEnd(null);
394         assertEquals(TEST_TIME2, test.getStartMillis());
395         assertEquals(TEST_TIME2, test.getEndMillis());
396     }
397 
398 }