View Javadoc

1   /*
2    *  Copyright 2001-2009 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 YearMonth.
32   *
33   * @author Stephen Colebourne
34   */
35  public class TestYearMonth_Constructors extends TestCase {
36  
37      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
38      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
39      private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
40      private static final Chronology GREGORIAN_UTC = GregorianChronology.getInstanceUTC();
41      private static final Chronology GREGORIAN_PARIS = GregorianChronology.getInstance(PARIS);
42      
43      private long TEST_TIME_NOW =
44              (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
45              
46      private long TEST_TIME1 =
47          (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
48          + 12L * DateTimeConstants.MILLIS_PER_HOUR
49          + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
50          
51      private long TEST_TIME2 =
52          (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
53          + 14L * DateTimeConstants.MILLIS_PER_HOUR
54          + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
55          
56      private DateTimeZone zone = null;
57  
58      public static void main(String[] args) {
59          junit.textui.TestRunner.run(suite());
60      }
61  
62      public static TestSuite suite() {
63          return new TestSuite(TestYearMonth_Constructors.class);
64      }
65  
66      public TestYearMonth_Constructors(String name) {
67          super(name);
68      }
69  
70      protected void setUp() throws Exception {
71          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
72          zone = DateTimeZone.getDefault();
73          DateTimeZone.setDefault(LONDON);
74      }
75  
76      protected void tearDown() throws Exception {
77          DateTimeUtils.setCurrentMillisSystem();
78          DateTimeZone.setDefault(zone);
79          zone = null;
80      }
81  
82      //-----------------------------------------------------------------------
83      public void testParse_noFormatter() throws Throwable {
84          assertEquals(new YearMonth(2010, 6), YearMonth.parse("2010-06-30"));
85          assertEquals(new YearMonth(2010, 1), YearMonth.parse("2010-002"));
86      }
87  
88      public void testParse_formatter() throws Throwable {
89          DateTimeFormatter f = DateTimeFormat.forPattern("yyyy--MM").withChronology(ISOChronology.getInstance(PARIS));
90          assertEquals(new YearMonth(2010, 6), YearMonth.parse("2010--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          YearMonth expected = new YearMonth(1970, 2);
98          assertEquals(expected, YearMonth.fromCalendarFields(cal));
99          try {
100             YearMonth.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         YearMonth expected = new YearMonth(1970, 2);
110         assertEquals(expected, YearMonth.fromDateFields(cal.getTime()));
111         try {
112             YearMonth.fromDateFields(null);
113             fail();
114         } catch (IllegalArgumentException ex) {}
115     }
116 
117     //-----------------------------------------------------------------------
118     /**
119      * Test constructor ()
120      */
121     public void testConstructor() throws Throwable {
122         YearMonth test = new YearMonth();
123         assertEquals(ISO_UTC, test.getChronology());
124         assertEquals(1970, test.getYear());
125         assertEquals(6, test.getMonthOfYear());
126         assertEquals(test, YearMonth.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         YearMonth test = new YearMonth(LONDON);
138         assertEquals(ISO_UTC, test.getChronology());
139         assertEquals(2005, test.getYear());
140         assertEquals(6, test.getMonthOfYear());
141         assertEquals(test, YearMonth.now(LONDON));
142         
143         test = new YearMonth(PARIS);
144         assertEquals(ISO_UTC, test.getChronology());
145         assertEquals(2005, test.getYear());
146         assertEquals(7, test.getMonthOfYear());
147         assertEquals(test, YearMonth.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         YearMonth test = new YearMonth((DateTimeZone) null);
159         assertEquals(ISO_UTC, test.getChronology());
160         assertEquals(2005, test.getYear());
161         assertEquals(6, test.getMonthOfYear());
162     }
163 
164     /**
165      * Test constructor (Chronology)
166      */
167     public void testConstructor_Chronology() throws Throwable {
168         YearMonth test = new YearMonth(GREGORIAN_PARIS);
169         assertEquals(GREGORIAN_UTC, test.getChronology());
170         assertEquals(1970, test.getYear());
171         assertEquals(6, test.getMonthOfYear());
172         assertEquals(test, YearMonth.now(GREGORIAN_PARIS));
173     }
174 
175     /**
176      * Test constructor (Chronology=null)
177      */
178     public void testConstructor_nullChronology() throws Throwable {
179         YearMonth test = new YearMonth((Chronology) null);
180         assertEquals(ISO_UTC, test.getChronology());
181         assertEquals(1970, test.getYear());
182         assertEquals(6, test.getMonthOfYear());
183     }
184 
185     //-----------------------------------------------------------------------
186     /**
187      * Test constructor (long)
188      */
189     public void testConstructor_long1() throws Throwable {
190         YearMonth test = new YearMonth(TEST_TIME1);
191         assertEquals(ISO_UTC, test.getChronology());
192         assertEquals(1970, test.getYear());
193         assertEquals(4, test.getMonthOfYear());
194     }
195 
196     /**
197      * Test constructor (long)
198      */
199     public void testConstructor_long2() throws Throwable {
200         YearMonth test = new YearMonth(TEST_TIME2);
201         assertEquals(ISO_UTC, test.getChronology());
202         assertEquals(1971, test.getYear());
203         assertEquals(5, test.getMonthOfYear());
204     }
205 
206     /**
207      * Test constructor (long, Chronology)
208      */
209     public void testConstructor_long1_Chronology() throws Throwable {
210         YearMonth test = new YearMonth(TEST_TIME1, GREGORIAN_PARIS);
211         assertEquals(GREGORIAN_UTC, test.getChronology());
212         assertEquals(1970, test.getYear());
213         assertEquals(4, test.getMonthOfYear());
214     }
215 
216     /**
217      * Test constructor (long, Chronology)
218      */
219     public void testConstructor_long2_Chronology() throws Throwable {
220         YearMonth test = new YearMonth(TEST_TIME2, GREGORIAN_PARIS);
221         assertEquals(GREGORIAN_UTC, test.getChronology());
222         assertEquals(1971, test.getYear());
223         assertEquals(5, test.getMonthOfYear());
224     }
225 
226     /**
227      * Test constructor (long, Chronology=null)
228      */
229     public void testConstructor_long_nullChronology() throws Throwable {
230         YearMonth test = new YearMonth(TEST_TIME1, null);
231         assertEquals(ISO_UTC, test.getChronology());
232         assertEquals(1970, test.getYear());
233         assertEquals(4, test.getMonthOfYear());
234     }
235 
236     //-----------------------------------------------------------------------
237     public void testConstructor_Object() throws Throwable {
238         Date date = new Date(TEST_TIME1);
239         YearMonth test = new YearMonth(date);
240         assertEquals(ISO_UTC, test.getChronology());
241         assertEquals(1970, test.getYear());
242         assertEquals(4, test.getMonthOfYear());
243     }
244 
245     public void testConstructor_nullObject() throws Throwable {
246         YearMonth test = new YearMonth((Object) null);
247         assertEquals(ISO_UTC, test.getChronology());
248         assertEquals(1970, test.getYear());
249         assertEquals(6, test.getMonthOfYear());
250     }
251 
252     public void testConstructor_ObjectString1() throws Throwable {
253         YearMonth test = new YearMonth("1972-12");
254         assertEquals(ISO_UTC, test.getChronology());
255         assertEquals(1972, test.getYear());
256         assertEquals(12, test.getMonthOfYear());
257     }
258 
259     public void testConstructor_ObjectString5() throws Throwable {
260         YearMonth test = new YearMonth("10");
261         assertEquals(ISO_UTC, test.getChronology());
262         assertEquals(10, test.getYear());
263         assertEquals(1, test.getMonthOfYear());
264     }
265 
266     public void testConstructor_ObjectStringEx1() throws Throwable {
267         try {
268             new YearMonth("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 YearMonth("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 YearMonth("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 YearMonth("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         YearMonth test = new YearMonth(date, GREGORIAN_PARIS);
309         assertEquals(GREGORIAN_UTC, test.getChronology());
310         assertEquals(1970, test.getYear());
311         assertEquals(4, test.getMonthOfYear());
312     }
313 
314     /**
315      * Test constructor (Object=null, Chronology)
316      */
317     public void testConstructor_nullObject_Chronology() throws Throwable {
318         YearMonth test = new YearMonth((Object) null, GREGORIAN_PARIS);
319         assertEquals(GREGORIAN_UTC, test.getChronology());
320         assertEquals(1970, test.getYear());
321         assertEquals(6, test.getMonthOfYear());
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         YearMonth test = new YearMonth(date, null);
330         assertEquals(ISO_UTC, test.getChronology());
331         assertEquals(1970, test.getYear());
332         assertEquals(4, test.getMonthOfYear());
333     }
334 
335     /**
336      * Test constructor (Object=null, Chronology=null)
337      */
338     public void testConstructor_nullObject_nullChronology() throws Throwable {
339         YearMonth test = new YearMonth((Object) null, null);
340         assertEquals(ISO_UTC, test.getChronology());
341         assertEquals(1970, test.getYear());
342         assertEquals(6, test.getMonthOfYear());
343     }
344 
345     //-----------------------------------------------------------------------
346     /**
347      * Test constructor (int, int)
348      */
349     public void testConstructor_int_int() throws Throwable {
350         YearMonth test = new YearMonth(1970, 6);
351         assertEquals(ISO_UTC, test.getChronology());
352         assertEquals(1970, test.getYear());
353         assertEquals(6, test.getMonthOfYear());
354         try {
355             new YearMonth(Integer.MIN_VALUE, 6);
356             fail();
357         } catch (IllegalArgumentException ex) {}
358         try {
359             new YearMonth(Integer.MAX_VALUE, 6);
360             fail();
361         } catch (IllegalArgumentException ex) {}
362         try {
363             new YearMonth(1970, 0);
364             fail();
365         } catch (IllegalArgumentException ex) {}
366         try {
367             new YearMonth(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         YearMonth test = new YearMonth(1970, 6, GREGORIAN_PARIS);
377         assertEquals(GREGORIAN_UTC, test.getChronology());
378         assertEquals(1970, test.getYear());
379         assertEquals(6, test.getMonthOfYear());
380         try {
381             new YearMonth(Integer.MIN_VALUE, 6, GREGORIAN_PARIS);
382             fail();
383         } catch (IllegalArgumentException ex) {}
384         try {
385             new YearMonth(Integer.MAX_VALUE, 6, GREGORIAN_PARIS);
386             fail();
387         } catch (IllegalArgumentException ex) {}
388         try {
389             new YearMonth(1970, 0, GREGORIAN_PARIS);
390             fail();
391         } catch (IllegalArgumentException ex) {}
392         try {
393             new YearMonth(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         YearMonth test = new YearMonth(1970, 6, null);
403         assertEquals(ISO_UTC, test.getChronology());
404         assertEquals(1970, test.getYear());
405         assertEquals(6, test.getMonthOfYear());
406     }
407 
408 }