View Javadoc

1   /*
2    *  Copyright 2001-2010 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.Calendar;
19  import java.util.Date;
20  import java.util.GregorianCalendar;
21  
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  
25  import org.joda.time.chrono.GregorianChronology;
26  import org.joda.time.chrono.ISOChronology;
27  import org.joda.time.format.DateTimeFormat;
28  import org.joda.time.format.DateTimeFormatter;
29  
30  /**
31   * This class is a Junit unit test for MonthDay. Based on {@link TestYearMonth_Constuctors} 
32   */
33  public class TestMonthDay_Constructors extends TestCase {
34  
35      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
36      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
37      private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
38      private static final Chronology GREGORIAN_UTC = GregorianChronology.getInstanceUTC();
39      private static final Chronology GREGORIAN_PARIS = GregorianChronology.getInstance(PARIS);
40      
41      private long TEST_TIME_NOW =
42              (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
43              
44      private long TEST_TIME1 =
45          (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
46          + 12L * DateTimeConstants.MILLIS_PER_HOUR
47          + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
48          
49      private long TEST_TIME2 =
50          (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
51          + 14L * DateTimeConstants.MILLIS_PER_HOUR
52          + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
53          
54      private DateTimeZone zone = null;
55  
56      public static void main(String[] args) {
57          junit.textui.TestRunner.run(suite());
58      }
59  
60      public static TestSuite suite() {
61          return new TestSuite(TestMonthDay_Constructors.class);
62      }
63  
64      public TestMonthDay_Constructors(String name) {
65          super(name);
66      }
67  
68      protected void setUp() throws Exception {
69          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
70          zone = DateTimeZone.getDefault();
71          DateTimeZone.setDefault(LONDON);
72      }
73  
74      protected void tearDown() throws Exception {
75          DateTimeUtils.setCurrentMillisSystem();
76          DateTimeZone.setDefault(zone);
77          zone = null;
78      }
79  
80      //-----------------------------------------------------------------------
81      public void testParse_noFormatter() throws Throwable {
82          assertEquals(new MonthDay(6, 30), MonthDay.parse("--06-30"));
83          assertEquals(new MonthDay(2, 29), MonthDay.parse("--02-29"));
84          assertEquals(new MonthDay(6, 30), MonthDay.parse("2010-06-30"));
85          assertEquals(new MonthDay(1, 2), MonthDay.parse("2010-002"));
86      }
87  
88      public void testParse_formatter() throws Throwable {
89          DateTimeFormatter f = DateTimeFormat.forPattern("yyyy--dd MM").withChronology(ISOChronology.getInstance(PARIS));
90          assertEquals(new MonthDay(6, 30), MonthDay.parse("2010--30 06", f));
91      }
92  
93      //-----------------------------------------------------------------------
94      public void testFactory_FromCalendarFields() throws Exception {
95          GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5, 6);
96          cal.set(Calendar.MILLISECOND, 7);
97          MonthDay expected = new MonthDay(2, 3);
98          assertEquals(expected, MonthDay.fromCalendarFields(cal));
99          try {
100             MonthDay.fromCalendarFields(null);
101             fail();
102         } catch (IllegalArgumentException ex) {}
103     }
104 
105     //-----------------------------------------------------------------------
106     public void testFactory_FromDateFields() throws Exception {
107         GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5, 6);
108         cal.set(Calendar.MILLISECOND, 7);
109         MonthDay expected = new MonthDay(2, 3);
110         assertEquals(expected, MonthDay.fromDateFields(cal.getTime()));
111         try {
112             MonthDay.fromDateFields(null);
113             fail();
114         } catch (IllegalArgumentException ex) {}
115     }
116 
117     //-----------------------------------------------------------------------
118     /**
119      * Test constructor ()
120      */
121     public void testConstructor() throws Throwable {
122         MonthDay test = new MonthDay();
123         assertEquals(ISO_UTC, test.getChronology());
124         assertEquals(6, test.getMonthOfYear());
125         assertEquals(9, test.getDayOfMonth());
126         assertEquals(test, MonthDay.now());
127     }
128 
129     /**
130      * Test constructor (DateTimeZone)
131      */
132     public void testConstructor_DateTimeZone() throws Throwable {
133         DateTime dt = new DateTime(2005, 6, 30, 23, 59, 0, 0, LONDON);
134         DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
135         // 23:59 in London is 00:59 the following day in Paris
136         
137         MonthDay test = new MonthDay(LONDON);
138         assertEquals(ISO_UTC, test.getChronology());
139         assertEquals(6, test.getMonthOfYear());
140         assertEquals(30, test.getDayOfMonth());
141         assertEquals(test, MonthDay.now(LONDON));
142         
143         test = new MonthDay(PARIS);
144         assertEquals(ISO_UTC, test.getChronology());
145         assertEquals(7, test.getMonthOfYear());
146         assertEquals(1, test.getDayOfMonth());
147         assertEquals(test, MonthDay.now(PARIS));
148     }
149 
150     /**
151      * Test constructor (DateTimeZone=null)
152      */
153     public void testConstructor_nullDateTimeZone() throws Throwable {
154         DateTime dt = new DateTime(2005, 6, 30, 23, 59, 0, 0, LONDON);
155         DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
156         // 23:59 in London is 00:59 the following day in Paris
157         
158         MonthDay test = new MonthDay((DateTimeZone) null);
159         assertEquals(ISO_UTC, test.getChronology());
160         assertEquals(6, test.getMonthOfYear());
161         assertEquals(30, test.getDayOfMonth());
162     }
163 
164     /**
165      * Test constructor (Chronology)
166      */
167     public void testConstructor_Chronology() throws Throwable {
168         MonthDay test = new MonthDay(GREGORIAN_PARIS);
169         assertEquals(GREGORIAN_UTC, test.getChronology());
170         assertEquals(6, test.getMonthOfYear());
171         assertEquals(9, test.getDayOfMonth());
172         assertEquals(test, MonthDay.now(GREGORIAN_PARIS));
173     }
174 
175     /**
176      * Test constructor (Chronology=null)
177      */
178     public void testConstructor_nullChronology() throws Throwable {
179         MonthDay test = new MonthDay((Chronology) null);
180         assertEquals(ISO_UTC, test.getChronology());
181         assertEquals(6, test.getMonthOfYear());
182         assertEquals(9, test.getDayOfMonth());
183     }
184 
185     //-----------------------------------------------------------------------
186     /**
187      * Test constructor (long)
188      */
189     public void testConstructor_long1() throws Throwable {
190         MonthDay test = new MonthDay(TEST_TIME1);
191         assertEquals(ISO_UTC, test.getChronology());
192         assertEquals(4, test.getMonthOfYear());
193         assertEquals(6, test.getDayOfMonth());
194     }
195 
196     /**
197      * Test constructor (long)
198      */
199     public void testConstructor_long2() throws Throwable {
200         MonthDay test = new MonthDay(TEST_TIME2);
201         assertEquals(ISO_UTC, test.getChronology());
202         assertEquals(5, test.getMonthOfYear());
203         assertEquals(7, test.getDayOfMonth());
204     }
205 
206     /**
207      * Test constructor (long, Chronology)
208      */
209     public void testConstructor_long1_Chronology() throws Throwable {
210         MonthDay test = new MonthDay(TEST_TIME1, GREGORIAN_PARIS);
211         assertEquals(GREGORIAN_UTC, test.getChronology());
212         assertEquals(4, test.getMonthOfYear());
213         assertEquals(6, test.getDayOfMonth());
214     }
215 
216     /**
217      * Test constructor (long, Chronology)
218      */
219     public void testConstructor_long2_Chronology() throws Throwable {
220         MonthDay test = new MonthDay(TEST_TIME2, GREGORIAN_PARIS);
221         assertEquals(GREGORIAN_UTC, test.getChronology());
222         assertEquals(5, test.getMonthOfYear());
223         assertEquals(7, test.getDayOfMonth());
224     }
225 
226     /**
227      * Test constructor (long, Chronology=null)
228      */
229     public void testConstructor_long_nullChronology() throws Throwable {
230         MonthDay test = new MonthDay(TEST_TIME1, null);
231         assertEquals(ISO_UTC, test.getChronology());
232         assertEquals(4, test.getMonthOfYear());
233         assertEquals(6, test.getDayOfMonth());
234     }
235 
236     //-----------------------------------------------------------------------
237     public void testConstructor_Object() throws Throwable {
238         Date date = new Date(TEST_TIME1);
239         MonthDay test = new MonthDay(date);
240         assertEquals(ISO_UTC, test.getChronology());
241         assertEquals(4, test.getMonthOfYear());
242         assertEquals(6, test.getDayOfMonth());
243     }
244 
245     public void testConstructor_nullObject() throws Throwable {
246         MonthDay test = new MonthDay((Object) null);
247         assertEquals(ISO_UTC, test.getChronology());
248         assertEquals(6, test.getMonthOfYear());
249         assertEquals(9, test.getDayOfMonth());
250     }
251 
252     public void testConstructor_ObjectString1() throws Throwable {
253         MonthDay test = new MonthDay("1972-12");
254         assertEquals(ISO_UTC, test.getChronology());
255         assertEquals(12, test.getMonthOfYear());
256         assertEquals(1, test.getDayOfMonth());
257     }
258 
259     public void testConstructor_ObjectString5() throws Throwable {
260         MonthDay test = new MonthDay("10");
261         assertEquals(ISO_UTC, test.getChronology());
262         assertEquals(1, test.getMonthOfYear());
263         assertEquals(1, test.getDayOfMonth());
264     }
265 
266     public void testConstructor_ObjectStringEx1() throws Throwable {
267         try {
268             new MonthDay("T10:20:30.040");
269             fail();
270         } catch (IllegalArgumentException ex) {
271             // expected
272         }
273     }
274 
275     public void testConstructor_ObjectStringEx2() throws Throwable {
276         try {
277             new MonthDay("T10:20:30.040+14:00");
278             fail();
279         } catch (IllegalArgumentException ex) {
280             // expected
281         }
282     }
283 
284     public void testConstructor_ObjectStringEx3() throws Throwable {
285         try {
286             new MonthDay("10:20:30.040");
287             fail();
288         } catch (IllegalArgumentException ex) {
289             // expected
290         }
291     }
292 
293     public void testConstructor_ObjectStringEx4() throws Throwable {
294         try {
295             new MonthDay("10:20:30.040+14:00");
296             fail();
297         } catch (IllegalArgumentException ex) {
298             // expected
299         }
300     }
301 
302     //-----------------------------------------------------------------------
303     /**
304      * Test constructor (Object, Chronology)
305      */
306     public void testConstructor_Object_Chronology() throws Throwable {
307         Date date = new Date(TEST_TIME1);
308         MonthDay test = new MonthDay(date, GREGORIAN_PARIS);
309         assertEquals(GREGORIAN_UTC, test.getChronology());
310         assertEquals(4, test.getMonthOfYear());
311         assertEquals(6, test.getDayOfMonth());
312     }
313 
314     /**
315      * Test constructor (Object=null, Chronology)
316      */
317     public void testConstructor_nullObject_Chronology() throws Throwable {
318         MonthDay test = new MonthDay((Object) null, GREGORIAN_PARIS);
319         assertEquals(GREGORIAN_UTC, test.getChronology());
320         assertEquals(6, test.getMonthOfYear());
321         assertEquals(9, test.getDayOfMonth());
322     }
323 
324     /**
325      * Test constructor (Object, Chronology=null)
326      */
327     public void testConstructor_Object_nullChronology() throws Throwable {
328         Date date = new Date(TEST_TIME1);
329         MonthDay test = new MonthDay(date, null);
330         assertEquals(ISO_UTC, test.getChronology());
331         assertEquals(4, test.getMonthOfYear());
332         assertEquals(6, test.getDayOfMonth());
333     }
334 
335     /**
336      * Test constructor (Object=null, Chronology=null)
337      */
338     public void testConstructor_nullObject_nullChronology() throws Throwable {
339         MonthDay test = new MonthDay((Object) null, null);
340         assertEquals(ISO_UTC, test.getChronology());
341         assertEquals(6, test.getMonthOfYear());
342         assertEquals(9, test.getDayOfMonth());
343     }
344 
345     //-----------------------------------------------------------------------
346     /**
347      * Test constructor (int, int)
348      */
349     public void testConstructor_int_int() throws Throwable {
350         MonthDay test = new MonthDay(6, 30);
351         assertEquals(ISO_UTC, test.getChronology());
352         assertEquals(6, test.getMonthOfYear());
353         assertEquals(30, test.getDayOfMonth());
354         try {
355             new MonthDay(Integer.MIN_VALUE, 6);
356             fail();
357         } catch (IllegalArgumentException ex) {}
358         try {
359             new MonthDay(Integer.MAX_VALUE, 6);
360             fail();
361         } catch (IllegalArgumentException ex) {}
362         try {
363             new MonthDay(1970, 0);
364             fail();
365         } catch (IllegalArgumentException ex) {}
366         try {
367             new MonthDay(1970, 13);
368             fail();
369         } catch (IllegalArgumentException ex) {}
370     }
371 
372     /**
373      * Test constructor (int, int, Chronology)
374      */
375     public void testConstructor_int_int_Chronology() throws Throwable {
376         MonthDay test = new MonthDay(6, 30, GREGORIAN_PARIS);
377         assertEquals(GREGORIAN_UTC, test.getChronology());
378         assertEquals(6, test.getMonthOfYear());
379         assertEquals(30, test.getDayOfMonth());
380         try {
381             new MonthDay(Integer.MIN_VALUE, 6, GREGORIAN_PARIS);
382             fail();
383         } catch (IllegalArgumentException ex) {}
384         try {
385             new MonthDay(Integer.MAX_VALUE, 6, GREGORIAN_PARIS);
386             fail();
387         } catch (IllegalArgumentException ex) {}
388         try {
389             new MonthDay(1970, 0, GREGORIAN_PARIS);
390             fail();
391         } catch (IllegalArgumentException ex) {}
392         try {
393             new MonthDay(1970, 13, GREGORIAN_PARIS);
394             fail();
395         } catch (IllegalArgumentException ex) {}
396     }
397 
398     /**
399      * Test constructor (int, int, Chronology=null)
400      */
401     public void testConstructor_int_int_nullChronology() throws Throwable {
402         MonthDay test = new MonthDay(6, 30, null);
403         assertEquals(ISO_UTC, test.getChronology());
404         assertEquals(6, test.getMonthOfYear());
405         assertEquals(30, test.getDayOfMonth());
406     }
407 
408 }