KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2006 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 JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.GregorianCalendar JavaDoc;
21
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import org.joda.time.chrono.BuddhistChronology;
26 import org.joda.time.chrono.CopticChronology;
27 import org.joda.time.chrono.GregorianChronology;
28 import org.joda.time.chrono.ISOChronology;
29
30 /**
31  * This class is a Junit unit test for YearMonthDay.
32  *
33  * @author Stephen Colebourne
34  */

35 public class TestYearMonthDay_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 COPTIC_UTC = CopticChronology.getInstanceUTC();
40     private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
41     private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC();
42     private static final Chronology GREGORIAN_UTC = GregorianChronology.getInstanceUTC();
43     private static final Chronology GREGORIAN_PARIS = GregorianChronology.getInstance(PARIS);
44     
45     private long TEST_TIME_NOW =
46             (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
47             
48     private long TEST_TIME1 =
49         (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
50         + 12L * DateTimeConstants.MILLIS_PER_HOUR
51         + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
52         
53     private long TEST_TIME2 =
54         (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
55         + 14L * DateTimeConstants.MILLIS_PER_HOUR
56         + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
57         
58     private DateTimeZone zone = null;
59
60     public static void main(String JavaDoc[] args) {
61         junit.textui.TestRunner.run(suite());
62     }
63
64     public static TestSuite suite() {
65         return new TestSuite(TestYearMonthDay_Constructors.class);
66     }
67
68     public TestYearMonthDay_Constructors(String JavaDoc name) {
69         super(name);
70     }
71
72     protected void setUp() throws Exception JavaDoc {
73         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
74         zone = DateTimeZone.getDefault();
75         DateTimeZone.setDefault(LONDON);
76     }
77
78     protected void tearDown() throws Exception JavaDoc {
79         DateTimeUtils.setCurrentMillisSystem();
80         DateTimeZone.setDefault(zone);
81         zone = null;
82     }
83
84     //-----------------------------------------------------------------------
85
public void testFactory_FromCalendarFields() throws Exception JavaDoc {
86         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(1970, 1, 3, 4, 5, 6);
87         cal.set(Calendar.MILLISECOND, 7);
88         YearMonthDay expected = new YearMonthDay(1970, 2, 3);
89         assertEquals(expected, YearMonthDay.fromCalendarFields(cal));
90         try {
91             YearMonthDay.fromCalendarFields(null);
92             fail();
93         } catch (IllegalArgumentException JavaDoc ex) {}
94     }
95
96     //-----------------------------------------------------------------------
97
public void testFactory_FromDateFields() throws Exception JavaDoc {
98         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(1970, 1, 3, 4, 5, 6);
99         cal.set(Calendar.MILLISECOND, 7);
100         YearMonthDay expected = new YearMonthDay(1970, 2, 3);
101         assertEquals(expected, YearMonthDay.fromDateFields(cal.getTime()));
102         try {
103             YearMonthDay.fromDateFields(null);
104             fail();
105         } catch (IllegalArgumentException JavaDoc ex) {}
106     }
107
108     //-----------------------------------------------------------------------
109
/**
110      * Test constructor ()
111      */

112     public void testConstructor() throws Throwable JavaDoc {
113         YearMonthDay test = new YearMonthDay();
114         assertEquals(ISO_UTC, test.getChronology());
115         assertEquals(1970, test.getYear());
116         assertEquals(6, test.getMonthOfYear());
117         assertEquals(9, test.getDayOfMonth());
118     }
119
120     /**
121      * Test constructor (DateTimeZone)
122      */

123     public void testConstructor_DateTimeZone() throws Throwable JavaDoc {
124         DateTime dt = new DateTime(2005, 6, 8, 23, 59, 0, 0, LONDON);
125         DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
126         // 23:59 in London is 00:59 the following day in Paris
127

128         YearMonthDay test = new YearMonthDay(LONDON);
129         assertEquals(ISO_UTC, test.getChronology());
130         assertEquals(2005, test.getYear());
131         assertEquals(6, test.getMonthOfYear());
132         assertEquals(8, test.getDayOfMonth());
133         
134         test = new YearMonthDay(PARIS);
135         assertEquals(ISO_UTC, test.getChronology());
136         assertEquals(2005, test.getYear());
137         assertEquals(6, test.getMonthOfYear());
138         assertEquals(9, test.getDayOfMonth());
139     }
140
141     /**
142      * Test constructor (DateTimeZone=null)
143      */

144     public void testConstructor_nullDateTimeZone() throws Throwable JavaDoc {
145         DateTime dt = new DateTime(2005, 6, 8, 23, 59, 0, 0, LONDON);
146         DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
147         // 23:59 in London is 00:59 the following day in Paris
148

149         YearMonthDay test = new YearMonthDay((DateTimeZone) null);
150         assertEquals(ISO_UTC, test.getChronology());
151         assertEquals(2005, test.getYear());
152         assertEquals(6, test.getMonthOfYear());
153         assertEquals(8, test.getDayOfMonth());
154     }
155
156     /**
157      * Test constructor (Chronology)
158      */

159     public void testConstructor_Chronology() throws Throwable JavaDoc {
160         YearMonthDay test = new YearMonthDay(GREGORIAN_PARIS);
161         assertEquals(GREGORIAN_UTC, test.getChronology());
162         assertEquals(1970, test.getYear());
163         assertEquals(6, test.getMonthOfYear());
164         assertEquals(9, test.getDayOfMonth());
165     }
166
167     /**
168      * Test constructor (Chronology=null)
169      */

170     public void testConstructor_nullChronology() throws Throwable JavaDoc {
171         YearMonthDay test = new YearMonthDay((Chronology) null);
172         assertEquals(ISO_UTC, test.getChronology());
173         assertEquals(1970, test.getYear());
174         assertEquals(6, test.getMonthOfYear());
175         assertEquals(9, test.getDayOfMonth());
176     }
177
178     //-----------------------------------------------------------------------
179
/**
180      * Test constructor (long)
181      */

182     public void testConstructor_long1() throws Throwable JavaDoc {
183         YearMonthDay test = new YearMonthDay(TEST_TIME1);
184         assertEquals(ISO_UTC, test.getChronology());
185         assertEquals(1970, test.getYear());
186         assertEquals(4, test.getMonthOfYear());
187         assertEquals(6, test.getDayOfMonth());
188     }
189
190     /**
191      * Test constructor (long)
192      */

193     public void testConstructor_long2() throws Throwable JavaDoc {
194         YearMonthDay test = new YearMonthDay(TEST_TIME2);
195         assertEquals(ISO_UTC, test.getChronology());
196         assertEquals(1971, test.getYear());
197         assertEquals(5, test.getMonthOfYear());
198         assertEquals(7, test.getDayOfMonth());
199     }
200
201     /**
202      * Test constructor (long, Chronology)
203      */

204     public void testConstructor_long1_Chronology() throws Throwable JavaDoc {
205         YearMonthDay test = new YearMonthDay(TEST_TIME1, GREGORIAN_PARIS);
206         assertEquals(GREGORIAN_UTC, test.getChronology());
207         assertEquals(1970, test.getYear());
208         assertEquals(4, test.getMonthOfYear());
209         assertEquals(6, test.getDayOfMonth());
210     }
211
212     /**
213      * Test constructor (long, Chronology)
214      */

215     public void testConstructor_long2_Chronology() throws Throwable JavaDoc {
216         YearMonthDay test = new YearMonthDay(TEST_TIME2, GREGORIAN_PARIS);
217         assertEquals(GREGORIAN_UTC, test.getChronology());
218         assertEquals(1971, test.getYear());
219         assertEquals(5, test.getMonthOfYear());
220         assertEquals(7, test.getDayOfMonth());
221     }
222
223     /**
224      * Test constructor (long, Chronology=null)
225      */

226     public void testConstructor_long_nullChronology() throws Throwable JavaDoc {
227         YearMonthDay test = new YearMonthDay(TEST_TIME1, null);
228         assertEquals(ISO_UTC, test.getChronology());
229         assertEquals(1970, test.getYear());
230         assertEquals(4, test.getMonthOfYear());
231         assertEquals(6, test.getDayOfMonth());
232     }
233
234     //-----------------------------------------------------------------------
235
public void testConstructor_Object() throws Throwable JavaDoc {
236         Date JavaDoc date = new Date JavaDoc(TEST_TIME1);
237         YearMonthDay test = new YearMonthDay(date);
238         assertEquals(ISO_UTC, test.getChronology());
239         assertEquals(1970, test.getYear());
240         assertEquals(4, test.getMonthOfYear());
241         assertEquals(6, test.getDayOfMonth());
242     }
243
244     public void testConstructor_nullObject() throws Throwable JavaDoc {
245         YearMonthDay test = new YearMonthDay((Object JavaDoc) null);
246         assertEquals(ISO_UTC, test.getChronology());
247         assertEquals(1970, test.getYear());
248         assertEquals(6, test.getMonthOfYear());
249         assertEquals(9, test.getDayOfMonth());
250     }
251
252     public void testConstructor_ObjectString1() throws Throwable JavaDoc {
253         YearMonthDay test = new YearMonthDay("1972-12-03");
254         assertEquals(ISO_UTC, test.getChronology());
255         assertEquals(1972, test.getYear());
256         assertEquals(12, test.getMonthOfYear());
257         assertEquals(3, test.getDayOfMonth());
258     }
259
260     public void testConstructor_ObjectString2() throws Throwable JavaDoc {
261         YearMonthDay test = new YearMonthDay("1972-12-03T+14:00");
262         assertEquals(ISO_UTC, test.getChronology());
263         assertEquals(1972, test.getYear());
264         assertEquals(12, test.getMonthOfYear());
265         assertEquals(2, test.getDayOfMonth()); // timezone
266
}
267
268     public void testConstructor_ObjectString3() throws Throwable JavaDoc {
269         YearMonthDay test = new YearMonthDay("1972-12-03T10:20:30.040");
270         assertEquals(ISO_UTC, test.getChronology());
271         assertEquals(1972, test.getYear());
272         assertEquals(12, test.getMonthOfYear());
273         assertEquals(3, test.getDayOfMonth());
274     }
275
276     public void testConstructor_ObjectString4() throws Throwable JavaDoc {
277         YearMonthDay test = new YearMonthDay("1972-12-03T10:20:30.040+14:00");
278         assertEquals(ISO_UTC, test.getChronology());
279         assertEquals(1972, test.getYear());
280         assertEquals(12, test.getMonthOfYear());
281         assertEquals(2, test.getDayOfMonth()); // timezone
282
}
283
284     public void testConstructor_ObjectString5() throws Throwable JavaDoc {
285         YearMonthDay test = new YearMonthDay("10");
286         assertEquals(ISO_UTC, test.getChronology());
287         assertEquals(10, test.getYear());
288         assertEquals(1, test.getMonthOfYear());
289         assertEquals(1, test.getDayOfMonth());
290     }
291
292     public void testConstructor_ObjectStringEx1() throws Throwable JavaDoc {
293         try {
294             new YearMonthDay("T10:20:30.040");
295             fail();
296         } catch (IllegalArgumentException JavaDoc ex) {
297             // expected
298
}
299     }
300
301     public void testConstructor_ObjectStringEx2() throws Throwable JavaDoc {
302         try {
303             new YearMonthDay("T10:20:30.040+14:00");
304             fail();
305         } catch (IllegalArgumentException JavaDoc ex) {
306             // expected
307
}
308     }
309
310     public void testConstructor_ObjectStringEx3() throws Throwable JavaDoc {
311         try {
312             new YearMonthDay("10:20:30.040");
313             fail();
314         } catch (IllegalArgumentException JavaDoc ex) {
315             // expected
316
}
317     }
318
319     public void testConstructor_ObjectStringEx4() throws Throwable JavaDoc {
320         try {
321             new YearMonthDay("10:20:30.040+14:00");
322             fail();
323         } catch (IllegalArgumentException JavaDoc ex) {
324             // expected
325
}
326     }
327
328     //-----------------------------------------------------------------------
329
/**
330      * Test constructor (Object, Chronology)
331      */

332     public void testConstructor_Object_Chronology() throws Throwable JavaDoc {
333         Date JavaDoc date = new Date JavaDoc(TEST_TIME1);
334         YearMonthDay test = new YearMonthDay(date, GREGORIAN_PARIS);
335         assertEquals(GREGORIAN_UTC, test.getChronology());
336         assertEquals(1970, test.getYear());
337         assertEquals(4, test.getMonthOfYear());
338         assertEquals(6, test.getDayOfMonth());
339     }
340
341     /**
342      * Test constructor (Object=null, Chronology)
343      */

344     public void testConstructor_nullObject_Chronology() throws Throwable JavaDoc {
345         YearMonthDay test = new YearMonthDay((Object JavaDoc) null, GREGORIAN_PARIS);
346         assertEquals(GREGORIAN_UTC, test.getChronology());
347         assertEquals(1970, test.getYear());
348         assertEquals(6, test.getMonthOfYear());
349         assertEquals(9, test.getDayOfMonth());
350     }
351
352     /**
353      * Test constructor (Object, Chronology=null)
354      */

355     public void testConstructor_Object_nullChronology() throws Throwable JavaDoc {
356         Date JavaDoc date = new Date JavaDoc(TEST_TIME1);
357         YearMonthDay test = new YearMonthDay(date, null);
358         assertEquals(ISO_UTC, test.getChronology());
359         assertEquals(1970, test.getYear());
360         assertEquals(4, test.getMonthOfYear());
361         assertEquals(6, test.getDayOfMonth());
362     }
363
364     /**
365      * Test constructor (Object=null, Chronology=null)
366      */

367     public void testConstructor_nullObject_nullChronology() throws Throwable JavaDoc {
368         YearMonthDay test = new YearMonthDay((Object JavaDoc) null, null);
369         assertEquals(ISO_UTC, test.getChronology());
370         assertEquals(1970, test.getYear());
371         assertEquals(6, test.getMonthOfYear());
372         assertEquals(9, test.getDayOfMonth());
373     }
374
375     //-----------------------------------------------------------------------
376
/**
377      * Test constructor (int, int, int)
378      */

379     public void testConstructor_int_int_int() throws Throwable JavaDoc {
380         YearMonthDay test = new YearMonthDay(1970, 6, 9);
381         assertEquals(ISO_UTC, test.getChronology());
382         assertEquals(1970, test.getYear());
383         assertEquals(6, test.getMonthOfYear());
384         assertEquals(9, test.getDayOfMonth());
385         try {
386             new YearMonthDay(Integer.MIN_VALUE, 6, 9);
387             fail();
388         } catch (IllegalArgumentException JavaDoc ex) {}
389         try {
390             new YearMonthDay(Integer.MAX_VALUE, 6, 9);
391             fail();
392         } catch (IllegalArgumentException JavaDoc ex) {}
393         try {
394             new YearMonthDay(1970, 0, 9);
395             fail();
396         } catch (IllegalArgumentException JavaDoc ex) {}
397         try {
398             new YearMonthDay(1970, 13, 9);
399             fail();
400         } catch (IllegalArgumentException JavaDoc ex) {}
401         try {
402             new YearMonthDay(1970, 6, 0);
403             fail();
404         } catch (IllegalArgumentException JavaDoc ex) {}
405         try {
406             new YearMonthDay(1970, 6, 31);
407             fail();
408         } catch (IllegalArgumentException JavaDoc ex) {}
409         new YearMonthDay(1970, 7, 31);
410         try {
411             new YearMonthDay(1970, 7, 32);
412             fail();
413         } catch (IllegalArgumentException JavaDoc ex) {}
414     }
415
416     /**
417      * Test constructor (int, int, int, Chronology)
418      */

419     public void testConstructor_int_int_int_Chronology() throws Throwable JavaDoc {
420         YearMonthDay test = new YearMonthDay(1970, 6, 9, GREGORIAN_PARIS);
421         assertEquals(GREGORIAN_UTC, test.getChronology());
422         assertEquals(1970, test.getYear());
423         assertEquals(6, test.getMonthOfYear());
424         assertEquals(9, test.getDayOfMonth());
425         try {
426             new YearMonthDay(Integer.MIN_VALUE, 6, 9, GREGORIAN_PARIS);
427             fail();
428         } catch (IllegalArgumentException JavaDoc ex) {}
429         try {
430             new YearMonthDay(Integer.MAX_VALUE, 6, 9, GREGORIAN_PARIS);
431             fail();
432         } catch (IllegalArgumentException JavaDoc ex) {}
433         try {
434             new YearMonthDay(1970, 0, 9, GREGORIAN_PARIS);
435             fail();
436         } catch (IllegalArgumentException JavaDoc ex) {}
437         try {
438             new YearMonthDay(1970, 13, 9, GREGORIAN_PARIS);
439             fail();
440         } catch (IllegalArgumentException JavaDoc ex) {}
441         try {
442             new YearMonthDay(1970, 6, 0, GREGORIAN_PARIS);
443             fail();
444         } catch (IllegalArgumentException JavaDoc ex) {}
445         try {
446             new YearMonthDay(1970, 6, 31, GREGORIAN_PARIS);
447             fail();
448         } catch (IllegalArgumentException JavaDoc ex) {}
449         new YearMonthDay(1970, 7, 31, GREGORIAN_PARIS);
450         try {
451             new YearMonthDay(1970, 7, 32, GREGORIAN_PARIS);
452             fail();
453         } catch (IllegalArgumentException JavaDoc ex) {}
454     }
455
456     /**
457      * Test constructor (int, int, int, Chronology=null)
458      */

459     public void testConstructor_int_int_int_nullChronology() throws Throwable JavaDoc {
460         YearMonthDay test = new YearMonthDay(1970, 6, 9, null);
461         assertEquals(ISO_UTC, test.getChronology());
462         assertEquals(1970, test.getYear());
463         assertEquals(6, test.getMonthOfYear());
464         assertEquals(9, test.getDayOfMonth());
465     }
466
467 }
468
Popular Tags