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.Date;
19  import java.util.Locale;
20  
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  import org.joda.time.chrono.GregorianChronology;
25  import org.joda.time.chrono.ISOChronology;
26  import org.joda.time.convert.ConverterManager;
27  import org.joda.time.convert.MockZeroNullIntegerConverter;
28  
29  /***
30   * This class is a Junit unit test for MutableDateTime.
31   *
32   * @author Stephen Colebourne
33   */
34  public class TestMutableDateTime_Constructors extends TestCase {
35      // Test in 2002/03 as time zones are more well known
36      // (before the late 90's they were all over the place)
37  
38      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
39      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
40      
41      long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
42                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
43                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
44                       366 + 365;
45      long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
46                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
47                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
48                       366 + 365 + 365;
49      
50      // 2002-06-09
51      private long TEST_TIME_NOW =
52              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
53              
54      // 2002-04-05
55      private long TEST_TIME1 =
56              (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
57              + 12L * DateTimeConstants.MILLIS_PER_HOUR
58              + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
59          
60      // 2003-05-06
61      private long TEST_TIME2 =
62              (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
63              + 14L * DateTimeConstants.MILLIS_PER_HOUR
64              + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
65          
66      private DateTimeZone zone = null;
67      private Locale locale = null;
68  
69      public static void main(String[] args) {
70          junit.textui.TestRunner.run(suite());
71      }
72  
73      public static TestSuite suite() {
74          return new TestSuite(TestMutableDateTime_Constructors.class);
75      }
76  
77      public TestMutableDateTime_Constructors(String name) {
78          super(name);
79      }
80  
81      protected void setUp() throws Exception {
82          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
83          zone = DateTimeZone.getDefault();
84          locale = Locale.getDefault();
85          DateTimeZone.setDefault(LONDON);
86          java.util.TimeZone.setDefault(LONDON.toTimeZone());
87          Locale.setDefault(Locale.UK);
88      }
89  
90      protected void tearDown() throws Exception {
91          DateTimeUtils.setCurrentMillisSystem();
92          DateTimeZone.setDefault(zone);
93          java.util.TimeZone.setDefault(zone.toTimeZone());
94          Locale.setDefault(locale);
95          zone = null;
96      }
97  
98      //-----------------------------------------------------------------------
99      public void testTest() {
100         assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString());
101         assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString());
102         assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString());
103     }
104 
105     //-----------------------------------------------------------------------
106     /***
107      * Test constructor ()
108      */
109     public void testConstructor() throws Throwable {
110         MutableDateTime test = new MutableDateTime();
111         assertEquals(ISOChronology.getInstance(), test.getChronology());
112         assertEquals(TEST_TIME_NOW, test.getMillis());
113     }
114 
115     /***
116      * Test constructor (DateTimeZone)
117      */
118     public void testConstructor_DateTimeZone() throws Throwable {
119         MutableDateTime test = new MutableDateTime(PARIS);
120         assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
121         assertEquals(TEST_TIME_NOW, test.getMillis());
122     }
123 
124     /***
125      * Test constructor (DateTimeZone=null)
126      */
127     public void testConstructor_nullDateTimeZone() throws Throwable {
128         MutableDateTime test = new MutableDateTime((DateTimeZone) null);
129         assertEquals(ISOChronology.getInstance(), test.getChronology());
130         assertEquals(TEST_TIME_NOW, test.getMillis());
131     }
132 
133     /***
134      * Test constructor (Chronology)
135      */
136     public void testConstructor_Chronology() throws Throwable {
137         MutableDateTime test = new MutableDateTime(GregorianChronology.getInstance());
138         assertEquals(GregorianChronology.getInstance(), test.getChronology());
139         assertEquals(TEST_TIME_NOW, test.getMillis());
140     }
141 
142     /***
143      * Test constructor (Chronology=null)
144      */
145     public void testConstructor_nullChronology() throws Throwable {
146         MutableDateTime test = new MutableDateTime((Chronology) null);
147         assertEquals(ISOChronology.getInstance(), test.getChronology());
148         assertEquals(TEST_TIME_NOW, test.getMillis());
149     }
150 
151     //-----------------------------------------------------------------------
152     /***
153      * Test constructor (long)
154      */
155     public void testConstructor_long1() throws Throwable {
156         MutableDateTime test = new MutableDateTime(TEST_TIME1);
157         assertEquals(ISOChronology.getInstance(), test.getChronology());
158         assertEquals(TEST_TIME1, test.getMillis());
159     }
160 
161     /***
162      * Test constructor (long)
163      */
164     public void testConstructor_long2() throws Throwable {
165         MutableDateTime test = new MutableDateTime(TEST_TIME2);
166         assertEquals(ISOChronology.getInstance(), test.getChronology());
167         assertEquals(TEST_TIME2, test.getMillis());
168     }
169 
170     /***
171      * Test constructor (long, DateTimeZone)
172      */
173     public void testConstructor_long1_DateTimeZone() throws Throwable {
174         MutableDateTime test = new MutableDateTime(TEST_TIME1, PARIS);
175         assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
176         assertEquals(TEST_TIME1, test.getMillis());
177     }
178 
179     /***
180      * Test constructor (long, DateTimeZone)
181      */
182     public void testConstructor_long2_DateTimeZone() throws Throwable {
183         MutableDateTime test = new MutableDateTime(TEST_TIME2, PARIS);
184         assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
185         assertEquals(TEST_TIME2, test.getMillis());
186     }
187 
188     /***
189      * Test constructor (long, DateTimeZone=null)
190      */
191     public void testConstructor_long_nullDateTimeZone() throws Throwable {
192         MutableDateTime test = new MutableDateTime(TEST_TIME1, (DateTimeZone) null);
193         assertEquals(ISOChronology.getInstance(), test.getChronology());
194         assertEquals(TEST_TIME1, test.getMillis());
195     }
196 
197     /***
198      * Test constructor (long, Chronology)
199      */
200     public void testConstructor_long1_Chronology() throws Throwable {
201         MutableDateTime test = new MutableDateTime(TEST_TIME1, GregorianChronology.getInstance());
202         assertEquals(GregorianChronology.getInstance(), test.getChronology());
203         assertEquals(TEST_TIME1, test.getMillis());
204     }
205 
206     /***
207      * Test constructor (long, Chronology)
208      */
209     public void testConstructor_long2_Chronology() throws Throwable {
210         MutableDateTime test = new MutableDateTime(TEST_TIME2, GregorianChronology.getInstance());
211         assertEquals(GregorianChronology.getInstance(), test.getChronology());
212         assertEquals(TEST_TIME2, test.getMillis());
213     }
214 
215     /***
216      * Test constructor (long, Chronology=null)
217      */
218     public void testConstructor_long_nullChronology() throws Throwable {
219         MutableDateTime test = new MutableDateTime(TEST_TIME1, (Chronology) null);
220         assertEquals(ISOChronology.getInstance(), test.getChronology());
221         assertEquals(TEST_TIME1, test.getMillis());
222     }
223 
224     //-----------------------------------------------------------------------
225     /***
226      * Test constructor (Object)
227      */
228     public void testConstructor_Object() throws Throwable {
229         Date date = new Date(TEST_TIME1);
230         MutableDateTime test = new MutableDateTime(date);
231         assertEquals(ISOChronology.getInstance(), test.getChronology());
232         assertEquals(TEST_TIME1, test.getMillis());
233     }
234 
235     /***
236      * Test constructor (Object)
237      */
238     public void testConstructor_invalidObject() throws Throwable {
239         try {
240             new MutableDateTime(new Object());
241             fail();
242         } catch (IllegalArgumentException ex) {}
243     }
244 
245     /***
246      * Test constructor (Object=null)
247      */
248     public void testConstructor_nullObject() throws Throwable {
249         MutableDateTime test = new MutableDateTime((Object) null);
250         assertEquals(ISOChronology.getInstance(), test.getChronology());
251         assertEquals(TEST_TIME_NOW, test.getMillis());
252     }
253 
254     /***
255      * Test constructor (Object=null)
256      */
257     public void testConstructor_badconverterObject() throws Throwable {
258         try {
259             ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
260             MutableDateTime test = new MutableDateTime(new Integer(0));
261             assertEquals(ISOChronology.getInstance(), test.getChronology());
262             assertEquals(0L, test.getMillis());
263         } finally {
264             ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
265         }
266     }
267 
268     /***
269      * Test constructor (Object, DateTimeZone)
270      */
271     public void testConstructor_Object_DateTimeZone() throws Throwable {
272         Date date = new Date(TEST_TIME1);
273         MutableDateTime test = new MutableDateTime(date, PARIS);
274         assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
275         assertEquals(TEST_TIME1, test.getMillis());
276     }
277 
278     /***
279      * Test constructor (Object, DateTimeZone)
280      */
281     public void testConstructor_invalidObject_DateTimeZone() throws Throwable {
282         try {
283             new MutableDateTime(new Object(), PARIS);
284             fail();
285         } catch (IllegalArgumentException ex) {}
286     }
287 
288     /***
289      * Test constructor (Object=null, DateTimeZone)
290      */
291     public void testConstructor_nullObject_DateTimeZone() throws Throwable {
292         MutableDateTime test = new MutableDateTime((Object) null, PARIS);
293         assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
294         assertEquals(TEST_TIME_NOW, test.getMillis());
295     }
296 
297     /***
298      * Test constructor (Object, DateTimeZone=null)
299      */
300     public void testConstructor_Object_nullDateTimeZone() throws Throwable {
301         Date date = new Date(TEST_TIME1);
302         MutableDateTime test = new MutableDateTime(date, (DateTimeZone) null);
303         assertEquals(ISOChronology.getInstance(), test.getChronology());
304         assertEquals(TEST_TIME1, test.getMillis());
305     }
306 
307     /***
308      * Test constructor (Object=null, DateTimeZone=null)
309      */
310     public void testConstructor_nullObject_nullDateTimeZone() throws Throwable {
311         MutableDateTime test = new MutableDateTime((Object) null, (DateTimeZone) null);
312         assertEquals(ISOChronology.getInstance(), test.getChronology());
313         assertEquals(TEST_TIME_NOW, test.getMillis());
314     }
315 
316     /***
317      * Test constructor (Object, DateTimeZone)
318      */
319     public void testConstructor_badconverterObject_DateTimeZone() throws Throwable {
320         try {
321             ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
322             MutableDateTime test = new MutableDateTime(new Integer(0), GregorianChronology.getInstance());
323             assertEquals(ISOChronology.getInstance(), test.getChronology());
324             assertEquals(0L, test.getMillis());
325         } finally {
326             ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
327         }
328     }
329 
330     /***
331      * Test constructor (Object, Chronology)
332      */
333     public void testConstructor_Object_Chronology() throws Throwable {
334         Date date = new Date(TEST_TIME1);
335         MutableDateTime test = new MutableDateTime(date, GregorianChronology.getInstance());
336         assertEquals(GregorianChronology.getInstance(), test.getChronology());
337         assertEquals(TEST_TIME1, test.getMillis());
338     }
339 
340     /***
341      * Test constructor (Object, Chronology)
342      */
343     public void testConstructor_invalidObject_Chronology() throws Throwable {
344         try {
345             new MutableDateTime(new Object(), GregorianChronology.getInstance());
346             fail();
347         } catch (IllegalArgumentException ex) {}
348     }
349 
350     /***
351      * Test constructor (Object=null, Chronology)
352      */
353     public void testConstructor_nullObject_Chronology() throws Throwable {
354         MutableDateTime test = new MutableDateTime((Object) null, GregorianChronology.getInstance());
355         assertEquals(GregorianChronology.getInstance(), test.getChronology());
356         assertEquals(TEST_TIME_NOW, test.getMillis());
357     }
358 
359     /***
360      * Test constructor (Object, Chronology=null)
361      */
362     public void testConstructor_Object_nullChronology() throws Throwable {
363         Date date = new Date(TEST_TIME1);
364         MutableDateTime test = new MutableDateTime(date, (Chronology) null);
365         assertEquals(ISOChronology.getInstance(), test.getChronology());
366         assertEquals(TEST_TIME1, test.getMillis());
367     }
368 
369     /***
370      * Test constructor (Object=null, Chronology=null)
371      */
372     public void testConstructor_nullObject_nullChronology() throws Throwable {
373         MutableDateTime test = new MutableDateTime((Object) null, (Chronology) null);
374         assertEquals(ISOChronology.getInstance(), test.getChronology());
375         assertEquals(TEST_TIME_NOW, test.getMillis());
376     }
377 
378     /***
379      * Test constructor (Object, Chronology)
380      */
381     public void testConstructor_badconverterObject_Chronology() throws Throwable {
382         try {
383             ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
384             MutableDateTime test = new MutableDateTime(new Integer(0), GregorianChronology.getInstance());
385             assertEquals(ISOChronology.getInstance(), test.getChronology());
386             assertEquals(0L, test.getMillis());
387         } finally {
388             ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
389         }
390     }
391 
392     //-----------------------------------------------------------------------
393     /***
394      * Test constructor (int, int, int)
395      */
396     public void testConstructor_int_int_int_int_int_int_int() throws Throwable {
397         MutableDateTime test = new MutableDateTime(2002, 6, 9, 1, 0, 0, 0);  // +01:00
398         assertEquals(ISOChronology.getInstance(), test.getChronology());
399         assertEquals(LONDON, test.getZone());
400         assertEquals(TEST_TIME_NOW, test.getMillis());
401         try {
402             new MutableDateTime(Integer.MIN_VALUE, 6, 9, 0, 0, 0, 0);
403             fail();
404         } catch (IllegalArgumentException ex) {}
405         try {
406             new MutableDateTime(Integer.MAX_VALUE, 6, 9, 0, 0, 0, 0);
407             fail();
408         } catch (IllegalArgumentException ex) {}
409         try {
410             new MutableDateTime(2002, 0, 9, 0, 0, 0, 0);
411             fail();
412         } catch (IllegalArgumentException ex) {}
413         try {
414             new MutableDateTime(2002, 13, 9, 0, 0, 0, 0);
415             fail();
416         } catch (IllegalArgumentException ex) {}
417         try {
418             new MutableDateTime(2002, 6, 0, 0, 0, 0, 0);
419             fail();
420         } catch (IllegalArgumentException ex) {}
421         try {
422             new MutableDateTime(2002, 6, 31, 0, 0, 0, 0);
423             fail();
424         } catch (IllegalArgumentException ex) {}
425         new MutableDateTime(2002, 7, 31, 0, 0, 0, 0);
426         try {
427             new MutableDateTime(2002, 7, 32, 0, 0, 0, 0);
428             fail();
429         } catch (IllegalArgumentException ex) {}
430     }
431 
432     /***
433      * Test constructor (int, int, int, DateTimeZone)
434      */
435     public void testConstructor_int_int_int_int_int_int_int_DateTimeZone() throws Throwable {
436         MutableDateTime test = new MutableDateTime(2002, 6, 9, 2, 0, 0, 0, PARIS);  // +02:00
437         assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
438         assertEquals(TEST_TIME_NOW, test.getMillis());
439         try {
440             new MutableDateTime(Integer.MIN_VALUE, 6, 9, 0, 0, 0, 0, PARIS);
441             fail();
442         } catch (IllegalArgumentException ex) {}
443         try {
444             new MutableDateTime(Integer.MAX_VALUE, 6, 9, 0, 0, 0, 0, PARIS);
445             fail();
446         } catch (IllegalArgumentException ex) {}
447         try {
448             new MutableDateTime(2002, 0, 9, 0, 0, 0, 0, PARIS);
449             fail();
450         } catch (IllegalArgumentException ex) {}
451         try {
452             new MutableDateTime(2002, 13, 9, 0, 0, 0, 0, PARIS);
453             fail();
454         } catch (IllegalArgumentException ex) {}
455         try {
456             new MutableDateTime(2002, 6, 0, 0, 0, 0, 0, PARIS);
457             fail();
458         } catch (IllegalArgumentException ex) {}
459         try {
460             new MutableDateTime(2002, 6, 31, 0, 0, 0, 0, PARIS);
461             fail();
462         } catch (IllegalArgumentException ex) {}
463         new MutableDateTime(2002, 7, 31, 0, 0, 0, 0, PARIS);
464         try {
465             new MutableDateTime(2002, 7, 32, 0, 0, 0, 0, PARIS);
466             fail();
467         } catch (IllegalArgumentException ex) {}
468     }
469 
470     /***
471      * Test constructor (int, int, int, DateTimeZone=null)
472      */
473     public void testConstructor_int_int_int_int_int_int_int_nullDateTimeZone() throws Throwable {
474         MutableDateTime test = new MutableDateTime(2002, 6, 9, 1, 0, 0, 0, (DateTimeZone) null);  // +01:00
475         assertEquals(ISOChronology.getInstance(), test.getChronology());
476         assertEquals(TEST_TIME_NOW, test.getMillis());
477     }
478 
479     /***
480      * Test constructor (int, int, int, Chronology)
481      */
482     public void testConstructor_int_int_int_int_int_int_int_Chronology() throws Throwable {
483         MutableDateTime test = new MutableDateTime(2002, 6, 9, 1, 0, 0, 0, GregorianChronology.getInstance());  // +01:00
484         assertEquals(GregorianChronology.getInstance(), test.getChronology());
485         assertEquals(TEST_TIME_NOW, test.getMillis());
486         try {
487             new MutableDateTime(Integer.MIN_VALUE, 6, 9, 0, 0, 0, 0, GregorianChronology.getInstance());
488             fail();
489         } catch (IllegalArgumentException ex) {}
490         try {
491             new MutableDateTime(Integer.MAX_VALUE, 6, 9, 0, 0, 0, 0, GregorianChronology.getInstance());
492             fail();
493         } catch (IllegalArgumentException ex) {}
494         try {
495             new MutableDateTime(2002, 0, 9, 0, 0, 0, 0, GregorianChronology.getInstance());
496             fail();
497         } catch (IllegalArgumentException ex) {}
498         try {
499             new MutableDateTime(2002, 13, 9, 0, 0, 0, 0, GregorianChronology.getInstance());
500             fail();
501         } catch (IllegalArgumentException ex) {}
502         try {
503             new MutableDateTime(2002, 6, 0, 0, 0, 0, 0, GregorianChronology.getInstance());
504             fail();
505         } catch (IllegalArgumentException ex) {}
506         try {
507             new MutableDateTime(2002, 6, 31, 0, 0, 0, 0, GregorianChronology.getInstance());
508             fail();
509         } catch (IllegalArgumentException ex) {}
510         new MutableDateTime(2002, 7, 31, 0, 0, 0, 0, GregorianChronology.getInstance());
511         try {
512             new MutableDateTime(2002, 7, 32, 0, 0, 0, 0, GregorianChronology.getInstance());
513             fail();
514         } catch (IllegalArgumentException ex) {}
515     }
516 
517     /***
518      * Test constructor (int, int, int, Chronology=null)
519      */
520     public void testConstructor_int_int_int_int_int_int_int_nullChronology() throws Throwable {
521         MutableDateTime test = new MutableDateTime(2002, 6, 9, 1, 0, 0, 0, (Chronology) null);  // +01:00
522         assertEquals(ISOChronology.getInstance(), test.getChronology());
523         assertEquals(TEST_TIME_NOW, test.getMillis());
524     }
525 
526 }