KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > TestLocalDate_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.GregorianChronology;
27 import org.joda.time.chrono.ISOChronology;
28
29 /**
30  * This class is a Junit unit test for LocalDate.
31  *
32  * @author Stephen Colebourne
33  */

34 public class TestLocalDate_Constructors extends TestCase {
35
36     private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
37     private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
38     private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
39     private static final Chronology BUDDHIST_UTC = BuddhistChronology.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 JavaDoc[] args) {
59         junit.textui.TestRunner.run(suite());
60     }
61
62     public static TestSuite suite() {
63         return new TestSuite(TestLocalDate_Constructors.class);
64     }
65
66     public TestLocalDate_Constructors(String JavaDoc name) {
67         super(name);
68     }
69
70     protected void setUp() throws Exception JavaDoc {
71         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
72         zone = DateTimeZone.getDefault();
73         DateTimeZone.setDefault(LONDON);
74     }
75
76     protected void tearDown() throws Exception JavaDoc {
77         DateTimeUtils.setCurrentMillisSystem();
78         DateTimeZone.setDefault(zone);
79         zone = null;
80     }
81
82     //-----------------------------------------------------------------------
83
public void testFactory_FromCalendarFields() throws Exception JavaDoc {
84         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(1970, 1, 3, 4, 5, 6);
85         cal.set(Calendar.MILLISECOND, 7);
86         LocalDate expected = new LocalDate(1970, 2, 3);
87         assertEquals(expected, LocalDate.fromCalendarFields(cal));
88         try {
89             LocalDate.fromCalendarFields((Calendar JavaDoc) null);
90             fail();
91         } catch (IllegalArgumentException JavaDoc ex) {}
92     }
93
94     //-----------------------------------------------------------------------
95
public void testFactory_FromDateFields() throws Exception JavaDoc {
96         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(1970, 1, 3, 4, 5, 6);
97         cal.set(Calendar.MILLISECOND, 7);
98         LocalDate expected = new LocalDate(1970, 2, 3);
99         assertEquals(expected, LocalDate.fromDateFields(cal.getTime()));
100         try {
101             LocalDate.fromDateFields((Date JavaDoc) null);
102             fail();
103         } catch (IllegalArgumentException JavaDoc ex) {}
104     }
105
106     //-----------------------------------------------------------------------
107
public void testConstructor() throws Throwable JavaDoc {
108         LocalDate test = new LocalDate();
109         assertEquals(ISO_UTC, test.getChronology());
110         assertEquals(1970, test.getYear());
111         assertEquals(6, test.getMonthOfYear());
112         assertEquals(9, test.getDayOfMonth());
113     }
114
115     public void testConstructor_DateTimeZone() throws Throwable JavaDoc {
116         DateTime dt = new DateTime(2005, 6, 8, 23, 59, 0, 0, LONDON);
117         DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
118         // 23:59 in London is 00:59 the following day in Paris
119

120         LocalDate test = new LocalDate(LONDON);
121         assertEquals(ISO_UTC, test.getChronology());
122         assertEquals(2005, test.getYear());
123         assertEquals(6, test.getMonthOfYear());
124         assertEquals(8, test.getDayOfMonth());
125         
126         test = new LocalDate(PARIS);
127         assertEquals(ISO_UTC, test.getChronology());
128         assertEquals(2005, test.getYear());
129         assertEquals(6, test.getMonthOfYear());
130         assertEquals(9, test.getDayOfMonth());
131     }
132
133     public void testConstructor_nullDateTimeZone() throws Throwable JavaDoc {
134         DateTime dt = new DateTime(2005, 6, 8, 23, 59, 0, 0, LONDON);
135         DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
136         // 23:59 in London is 00:59 the following day in Paris
137

138         LocalDate test = new LocalDate((DateTimeZone) null);
139         assertEquals(ISO_UTC, test.getChronology());
140         assertEquals(2005, test.getYear());
141         assertEquals(6, test.getMonthOfYear());
142         assertEquals(8, test.getDayOfMonth());
143     }
144
145     public void testConstructor_Chronology() throws Throwable JavaDoc {
146         LocalDate test = new LocalDate(GREGORIAN_PARIS);
147         assertEquals(GREGORIAN_UTC, test.getChronology());
148         assertEquals(1970, test.getYear());
149         assertEquals(6, test.getMonthOfYear());
150         assertEquals(9, test.getDayOfMonth());
151     }
152
153     public void testConstructor_nullChronology() throws Throwable JavaDoc {
154         LocalDate test = new LocalDate((Chronology) null);
155         assertEquals(ISO_UTC, test.getChronology());
156         assertEquals(1970, test.getYear());
157         assertEquals(6, test.getMonthOfYear());
158         assertEquals(9, test.getDayOfMonth());
159     }
160
161     //-----------------------------------------------------------------------
162
public void testConstructor_long1() throws Throwable JavaDoc {
163         LocalDate test = new LocalDate(TEST_TIME1);
164         assertEquals(ISO_UTC, test.getChronology());
165         assertEquals(1970, test.getYear());
166         assertEquals(4, test.getMonthOfYear());
167         assertEquals(6, test.getDayOfMonth());
168     }
169
170     public void testConstructor_long2() throws Throwable JavaDoc {
171         LocalDate test = new LocalDate(TEST_TIME2);
172         assertEquals(ISO_UTC, test.getChronology());
173         assertEquals(1971, test.getYear());
174         assertEquals(5, test.getMonthOfYear());
175         assertEquals(7, test.getDayOfMonth());
176     }
177
178     public void testConstructor_long1_DateTimeZone() throws Throwable JavaDoc {
179         LocalDate test = new LocalDate(TEST_TIME1, PARIS);
180         assertEquals(ISO_UTC, test.getChronology());
181         assertEquals(1970, test.getYear());
182         assertEquals(4, test.getMonthOfYear());
183         assertEquals(6, test.getDayOfMonth());
184     }
185
186     public void testConstructor_long2_DateTimeZone() throws Throwable JavaDoc {
187         LocalDate test = new LocalDate(TEST_TIME2, PARIS);
188         assertEquals(ISO_UTC, test.getChronology());
189         assertEquals(1971, test.getYear());
190         assertEquals(5, test.getMonthOfYear());
191         assertEquals(7, test.getDayOfMonth());
192     }
193
194     public void testConstructor_long_nullDateTimeZone() throws Throwable JavaDoc {
195         LocalDate test = new LocalDate(TEST_TIME1, (DateTimeZone) null);
196         assertEquals(ISO_UTC, test.getChronology());
197         assertEquals(1970, test.getYear());
198         assertEquals(4, test.getMonthOfYear());
199         assertEquals(6, test.getDayOfMonth());
200     }
201
202     public void testConstructor_long1_Chronology() throws Throwable JavaDoc {
203         LocalDate test = new LocalDate(TEST_TIME1, GREGORIAN_PARIS);
204         assertEquals(GREGORIAN_UTC, test.getChronology());
205         assertEquals(1970, test.getYear());
206         assertEquals(4, test.getMonthOfYear());
207         assertEquals(6, test.getDayOfMonth());
208     }
209
210     public void testConstructor_long2_Chronology() throws Throwable JavaDoc {
211         LocalDate test = new LocalDate(TEST_TIME2, GREGORIAN_PARIS);
212         assertEquals(GREGORIAN_UTC, test.getChronology());
213         assertEquals(1971, test.getYear());
214         assertEquals(5, test.getMonthOfYear());
215         assertEquals(7, test.getDayOfMonth());
216     }
217
218     public void testConstructor_long_nullChronology() throws Throwable JavaDoc {
219         LocalDate test = new LocalDate(TEST_TIME1, (Chronology) null);
220         assertEquals(ISO_UTC, test.getChronology());
221         assertEquals(1970, test.getYear());
222         assertEquals(4, test.getMonthOfYear());
223         assertEquals(6, test.getDayOfMonth());
224     }
225
226     //-----------------------------------------------------------------------
227
public void testConstructor_Object1() throws Throwable JavaDoc {
228         Date JavaDoc date = new Date JavaDoc(TEST_TIME1);
229         LocalDate test = new LocalDate(date);
230         assertEquals(ISO_UTC, test.getChronology());
231         assertEquals(1970, test.getYear());
232         assertEquals(4, test.getMonthOfYear());
233         assertEquals(6, test.getDayOfMonth());
234     }
235
236     public void testConstructor_nullObject() throws Throwable JavaDoc {
237         LocalDate test = new LocalDate((Object JavaDoc) null);
238         assertEquals(ISO_UTC, test.getChronology());
239         assertEquals(1970, test.getYear());
240         assertEquals(6, test.getMonthOfYear());
241         assertEquals(9, test.getDayOfMonth());
242     }
243
244     public void testConstructor_ObjectString1() throws Throwable JavaDoc {
245         LocalDate test = new LocalDate("1972-04-06");
246         assertEquals(ISO_UTC, test.getChronology());
247         assertEquals(1972, test.getYear());
248         assertEquals(4, test.getMonthOfYear());
249         assertEquals(6, test.getDayOfMonth());
250     }
251
252     public void testConstructor_ObjectString2() throws Throwable JavaDoc {
253         LocalDate test = new LocalDate("1972-037");
254         assertEquals(ISO_UTC, test.getChronology());
255         assertEquals(1972, test.getYear());
256         assertEquals(2, test.getMonthOfYear());
257         assertEquals(6, test.getDayOfMonth());
258     }
259
260     public void testConstructor_ObjectString3() throws Throwable JavaDoc {
261         LocalDate test = new LocalDate("1972-02");
262         assertEquals(ISO_UTC, test.getChronology());
263         assertEquals(1972, test.getYear());
264         assertEquals(2, test.getMonthOfYear());
265         assertEquals(1, test.getDayOfMonth());
266     }
267
268     public void testConstructor_ObjectStringEx1() throws Throwable JavaDoc {
269         try {
270             new LocalDate("1970-04-06T+14:00");
271             fail();
272         } catch (IllegalArgumentException JavaDoc ex) {}
273     }
274
275     public void testConstructor_ObjectStringEx2() throws Throwable JavaDoc {
276         try {
277             new LocalDate("1970-04-06T10:20:30.040");
278             fail();
279         } catch (IllegalArgumentException JavaDoc ex) {}
280     }
281
282     public void testConstructor_ObjectStringEx3() throws Throwable JavaDoc {
283         try {
284             new LocalDate("1970-04-06T10:20:30.040+14:00");
285             fail();
286         } catch (IllegalArgumentException JavaDoc ex) {}
287     }
288
289     public void testConstructor_ObjectStringEx4() throws Throwable JavaDoc {
290         try {
291             new LocalDate("T10:20:30.040");
292             fail();
293         } catch (IllegalArgumentException JavaDoc ex) {}
294     }
295
296     public void testConstructor_ObjectStringEx5() throws Throwable JavaDoc {
297         try {
298             new LocalDate("T10:20:30.040+14:00");
299             fail();
300         } catch (IllegalArgumentException JavaDoc ex) {}
301     }
302
303     public void testConstructor_ObjectStringEx6() throws Throwable JavaDoc {
304         try {
305             new LocalDate("10:20:30.040");
306             fail();
307         } catch (IllegalArgumentException JavaDoc ex) {}
308     }
309
310     public void testConstructor_ObjectStringEx7() throws Throwable JavaDoc {
311         try {
312             new LocalDate("10:20:30.040+14:00");
313             fail();
314         } catch (IllegalArgumentException JavaDoc ex) {}
315     }
316
317     public void testConstructor_ObjectLocalDate() throws Throwable JavaDoc {
318         LocalDate date = new LocalDate(1970, 4, 6, BUDDHIST_UTC);
319         LocalDate test = new LocalDate(date);
320         assertEquals(BUDDHIST_UTC, test.getChronology());
321         assertEquals(1970, test.getYear());
322         assertEquals(4, test.getMonthOfYear());
323         assertEquals(6, test.getDayOfMonth());
324     }
325
326     public void testConstructor_ObjectLocalTime() throws Throwable JavaDoc {
327         LocalTime time = new LocalTime(10, 20, 30, 40, BUDDHIST_UTC);
328         try {
329             new LocalDate(time);
330             fail();
331         } catch (IllegalArgumentException JavaDoc ex) {}
332     }
333
334     public void testConstructor_ObjectLocalDateTime() throws Throwable JavaDoc {
335         LocalDateTime dt = new LocalDateTime(1970, 5, 6, 10, 20, 30, 40, BUDDHIST_UTC);
336         LocalDate test = new LocalDate(dt);
337         assertEquals(BUDDHIST_UTC, test.getChronology());
338         assertEquals(1970, test.getYear());
339         assertEquals(5, test.getMonthOfYear());
340         assertEquals(6, test.getDayOfMonth());
341     }
342
343     public void testConstructor_ObjectYearMonthDay() throws Throwable JavaDoc {
344         YearMonthDay date = new YearMonthDay(1970, 4, 6, BUDDHIST_UTC);
345         LocalDate test = new LocalDate(date);
346         assertEquals(BUDDHIST_UTC, test.getChronology());
347         assertEquals(1970, test.getYear());
348         assertEquals(4, test.getMonthOfYear());
349         assertEquals(6, test.getDayOfMonth());
350     }
351
352     //-----------------------------------------------------------------------
353
public void testConstructor_Object_DateTimeZone() throws Throwable JavaDoc {
354         Date JavaDoc date = new Date JavaDoc(TEST_TIME1);
355         LocalDate test = new LocalDate(date, PARIS);
356         assertEquals(ISO_UTC, test.getChronology());
357         assertEquals(1970, test.getYear());
358         assertEquals(4, test.getMonthOfYear());
359         assertEquals(6, test.getDayOfMonth());
360     }
361
362     public void testConstructor_nullObject_DateTimeZone() throws Throwable JavaDoc {
363         LocalDate test = new LocalDate((Object JavaDoc) null, PARIS);
364         assertEquals(ISO_UTC, test.getChronology());
365         assertEquals(1970, test.getYear());
366         assertEquals(6, test.getMonthOfYear());
367         assertEquals(9, test.getDayOfMonth());
368     }
369
370     public void testConstructor_Object_nullDateTimeZone() throws Throwable JavaDoc {
371         Date JavaDoc date = new Date JavaDoc(TEST_TIME1);
372         LocalDate test = new LocalDate(date, (DateTimeZone) null);
373         assertEquals(ISO_UTC, test.getChronology());
374         assertEquals(1970, test.getYear());
375         assertEquals(4, test.getMonthOfYear());
376         assertEquals(6, test.getDayOfMonth());
377     }
378
379     public void testConstructor_nullObject_nullDateTimeZone() throws Throwable JavaDoc {
380         LocalDate test = new LocalDate((Object JavaDoc) null, (DateTimeZone) null);
381         assertEquals(ISO_UTC, test.getChronology());
382         assertEquals(1970, test.getYear());
383         assertEquals(6, test.getMonthOfYear());
384         assertEquals(9, test.getDayOfMonth());
385     }
386
387     public void testConstructor_Object_Chronology() throws Throwable JavaDoc {
388         Date JavaDoc date = new Date JavaDoc(TEST_TIME1);
389         LocalDate test = new LocalDate(date, GREGORIAN_PARIS);
390         assertEquals(GREGORIAN_UTC, test.getChronology());
391         assertEquals(1970, test.getYear());
392         assertEquals(4, test.getMonthOfYear());
393         assertEquals(6, test.getDayOfMonth());
394     }
395
396     public void testConstructor_nullObject_Chronology() throws Throwable JavaDoc {
397         LocalDate test = new LocalDate((Object JavaDoc) null, GREGORIAN_PARIS);
398         assertEquals(GREGORIAN_UTC, test.getChronology());
399         assertEquals(1970, test.getYear());
400         assertEquals(6, test.getMonthOfYear());
401         assertEquals(9, test.getDayOfMonth());
402     }
403
404     public void testConstructor_Object_nullChronology() throws Throwable JavaDoc {
405         Date JavaDoc date = new Date JavaDoc(TEST_TIME1);
406         LocalDate test = new LocalDate(date, (Chronology) null);
407         assertEquals(ISO_UTC, test.getChronology());
408         assertEquals(1970, test.getYear());
409         assertEquals(4, test.getMonthOfYear());
410         assertEquals(6, test.getDayOfMonth());
411     }
412
413     public void testConstructor_nullObject_nullChronology() throws Throwable JavaDoc {
414         LocalDate test = new LocalDate((Object JavaDoc) null, (Chronology) null);
415         assertEquals(ISO_UTC, test.getChronology());
416         assertEquals(1970, test.getYear());
417         assertEquals(6, test.getMonthOfYear());
418         assertEquals(9, test.getDayOfMonth());
419     }
420
421     //-----------------------------------------------------------------------
422
public void testConstructor_int_int_int() throws Throwable JavaDoc {
423         LocalDate test = new LocalDate(1970, 6, 9);
424         assertEquals(ISO_UTC, test.getChronology());
425         assertEquals(1970, test.getYear());
426         assertEquals(6, test.getMonthOfYear());
427         assertEquals(9, test.getDayOfMonth());
428         try {
429             new LocalDate(Integer.MIN_VALUE, 6, 9);
430             fail();
431         } catch (IllegalArgumentException JavaDoc ex) {}
432         try {
433             new LocalDate(Integer.MAX_VALUE, 6, 9);
434             fail();
435         } catch (IllegalArgumentException JavaDoc ex) {}
436         try {
437             new LocalDate(1970, 0, 9);
438             fail();
439         } catch (IllegalArgumentException JavaDoc ex) {}
440         try {
441             new LocalDate(1970, 13, 9);
442             fail();
443         } catch (IllegalArgumentException JavaDoc ex) {}
444         try {
445             new LocalDate(1970, 6, 0);
446             fail();
447         } catch (IllegalArgumentException JavaDoc ex) {}
448         try {
449             new LocalDate(1970, 6, 31);
450             fail();
451         } catch (IllegalArgumentException JavaDoc ex) {}
452         new LocalDate(1970, 7, 31);
453         try {
454             new LocalDate(1970, 7, 32);
455             fail();
456         } catch (IllegalArgumentException JavaDoc ex) {}
457     }
458
459     public void testConstructor_int_int_int_Chronology() throws Throwable JavaDoc {
460         LocalDate test = new LocalDate(1970, 6, 9, GREGORIAN_PARIS);
461         assertEquals(GREGORIAN_UTC, test.getChronology());
462         assertEquals(1970, test.getYear());
463         assertEquals(6, test.getMonthOfYear());
464         assertEquals(9, test.getDayOfMonth());
465         try {
466             new LocalDate(Integer.MIN_VALUE, 6, 9, GREGORIAN_PARIS);
467             fail();
468         } catch (IllegalArgumentException JavaDoc ex) {}
469         try {
470             new LocalDate(Integer.MAX_VALUE, 6, 9, GREGORIAN_PARIS);
471             fail();
472         } catch (IllegalArgumentException JavaDoc ex) {}
473         try {
474             new LocalDate(1970, 0, 9, GREGORIAN_PARIS);
475             fail();
476         } catch (IllegalArgumentException JavaDoc ex) {}
477         try {
478             new LocalDate(1970, 13, 9, GREGORIAN_PARIS);
479             fail();
480         } catch (IllegalArgumentException JavaDoc ex) {}
481         try {
482             new LocalDate(1970, 6, 0, GREGORIAN_PARIS);
483             fail();
484         } catch (IllegalArgumentException JavaDoc ex) {}
485         try {
486             new LocalDate(1970, 6, 31, GREGORIAN_PARIS);
487             fail();
488         } catch (IllegalArgumentException JavaDoc ex) {}
489         new LocalDate(1970, 7, 31, GREGORIAN_PARIS);
490         try {
491             new LocalDate(1970, 7, 32, GREGORIAN_PARIS);
492             fail();
493         } catch (IllegalArgumentException JavaDoc ex) {}
494     }
495
496     public void testConstructor_int_int_int_nullChronology() throws Throwable JavaDoc {
497         LocalDate test = new LocalDate(1970, 6, 9, null);
498         assertEquals(ISO_UTC, test.getChronology());
499         assertEquals(1970, test.getYear());
500         assertEquals(6, test.getMonthOfYear());
501         assertEquals(9, test.getDayOfMonth());
502     }
503
504 }
505
Popular Tags