1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.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 private long TEST_TIME1_ROUNDED =
51 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY;
52 private long TEST_TIME2 =
53 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
54 + 14L * DateTimeConstants.MILLIS_PER_HOUR
55 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
56
57 private DateTimeZone zone = null;
58
59 public static void main(String[] args) {
60 junit.textui.TestRunner.run(suite());
61 }
62
63 public static TestSuite suite() {
64 return new TestSuite(TestLocalDate_Constructors.class);
65 }
66
67 public TestLocalDate_Constructors(String name) {
68 super(name);
69 }
70
71 protected void setUp() throws Exception {
72 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
73 zone = DateTimeZone.getDefault();
74 DateTimeZone.setDefault(LONDON);
75 }
76
77 protected void tearDown() throws Exception {
78 DateTimeUtils.setCurrentMillisSystem();
79 DateTimeZone.setDefault(zone);
80 zone = null;
81 }
82
83
84 public void testFactory_FromCalendarFields() throws Exception {
85 GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5, 6);
86 cal.set(Calendar.MILLISECOND, 7);
87 LocalDate expected = new LocalDate(1970, 2, 3);
88 assertEquals(expected, LocalDate.fromCalendarFields(cal));
89 try {
90 LocalDate.fromCalendarFields((Calendar) null);
91 fail();
92 } catch (IllegalArgumentException ex) {}
93 }
94
95
96 public void testFactory_FromDateFields() throws Exception {
97 GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5, 6);
98 cal.set(Calendar.MILLISECOND, 7);
99 LocalDate expected = new LocalDate(1970, 2, 3);
100 assertEquals(expected, LocalDate.fromDateFields(cal.getTime()));
101 try {
102 LocalDate.fromDateFields((Date) null);
103 fail();
104 } catch (IllegalArgumentException ex) {}
105 }
106
107
108 public void testConstructor() throws Throwable {
109 LocalDate test = new LocalDate();
110 assertEquals(ISO_UTC, test.getChronology());
111 assertEquals(1970, test.getYear());
112 assertEquals(6, test.getMonthOfYear());
113 assertEquals(9, test.getDayOfMonth());
114 }
115
116 public void testConstructor_DateTimeZone() throws Throwable {
117 DateTime dt = new DateTime(2005, 6, 8, 23, 59, 0, 0, LONDON);
118 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
119
120
121 LocalDate test = new LocalDate(LONDON);
122 assertEquals(ISO_UTC, test.getChronology());
123 assertEquals(2005, test.getYear());
124 assertEquals(6, test.getMonthOfYear());
125 assertEquals(8, test.getDayOfMonth());
126
127 test = new LocalDate(PARIS);
128 assertEquals(ISO_UTC, test.getChronology());
129 assertEquals(2005, test.getYear());
130 assertEquals(6, test.getMonthOfYear());
131 assertEquals(9, test.getDayOfMonth());
132 }
133
134 public void testConstructor_nullDateTimeZone() throws Throwable {
135 DateTime dt = new DateTime(2005, 6, 8, 23, 59, 0, 0, LONDON);
136 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
137
138
139 LocalDate test = new LocalDate((DateTimeZone) null);
140 assertEquals(ISO_UTC, test.getChronology());
141 assertEquals(2005, test.getYear());
142 assertEquals(6, test.getMonthOfYear());
143 assertEquals(8, test.getDayOfMonth());
144 }
145
146 public void testConstructor_Chronology() throws Throwable {
147 LocalDate test = new LocalDate(GREGORIAN_PARIS);
148 assertEquals(GREGORIAN_UTC, test.getChronology());
149 assertEquals(1970, test.getYear());
150 assertEquals(6, test.getMonthOfYear());
151 assertEquals(9, test.getDayOfMonth());
152 }
153
154 public void testConstructor_nullChronology() throws Throwable {
155 LocalDate test = new LocalDate((Chronology) null);
156 assertEquals(ISO_UTC, test.getChronology());
157 assertEquals(1970, test.getYear());
158 assertEquals(6, test.getMonthOfYear());
159 assertEquals(9, test.getDayOfMonth());
160 }
161
162
163 public void testConstructor_long1() throws Throwable {
164 LocalDate test = new LocalDate(TEST_TIME1);
165 assertEquals(ISO_UTC, test.getChronology());
166 assertEquals(1970, test.getYear());
167 assertEquals(4, test.getMonthOfYear());
168 assertEquals(6, test.getDayOfMonth());
169 }
170
171 public void testConstructor_long2() throws Throwable {
172 LocalDate test = new LocalDate(TEST_TIME2);
173 assertEquals(ISO_UTC, test.getChronology());
174 assertEquals(1971, test.getYear());
175 assertEquals(5, test.getMonthOfYear());
176 assertEquals(7, test.getDayOfMonth());
177 }
178
179 public void testConstructor_long1_DateTimeZone() throws Throwable {
180 LocalDate test = new LocalDate(TEST_TIME1, PARIS);
181 assertEquals(ISO_UTC, test.getChronology());
182 assertEquals(1970, test.getYear());
183 assertEquals(4, test.getMonthOfYear());
184 assertEquals(6, test.getDayOfMonth());
185 assertEquals(TEST_TIME1_ROUNDED, test.getLocalMillis());
186 }
187
188 public void testConstructor_long2_DateTimeZone() throws Throwable {
189 LocalDate test = new LocalDate(TEST_TIME2, PARIS);
190 assertEquals(ISO_UTC, test.getChronology());
191 assertEquals(1971, test.getYear());
192 assertEquals(5, test.getMonthOfYear());
193 assertEquals(7, test.getDayOfMonth());
194 }
195
196 public void testConstructor_long3_DateTimeZone() throws Throwable {
197 DateTime dt = new DateTime(2006, 6, 9, 0, 0, 0, 0, PARIS);
198 DateTime dtUTC = new DateTime(2006, 6, 9, 0, 0, 0, 0, DateTimeZone.UTC);
199
200 LocalDate test = new LocalDate(dt.getMillis(), PARIS);
201 assertEquals(ISO_UTC, test.getChronology());
202 assertEquals(2006, test.getYear());
203 assertEquals(6, test.getMonthOfYear());
204 assertEquals(9, test.getDayOfMonth());
205 assertEquals(dtUTC.getMillis(), test.getLocalMillis());
206 }
207
208 public void testConstructor_long4_DateTimeZone() throws Throwable {
209 DateTime dt = new DateTime(2006, 6, 9, 23, 59, 59, 999, PARIS);
210 DateTime dtUTC = new DateTime(2006, 6, 9, 0, 0, 0, 0, DateTimeZone.UTC);
211
212 LocalDate test = new LocalDate(dt.getMillis(), PARIS);
213 assertEquals(ISO_UTC, test.getChronology());
214 assertEquals(2006, test.getYear());
215 assertEquals(6, test.getMonthOfYear());
216 assertEquals(9, test.getDayOfMonth());
217 assertEquals(dtUTC.getMillis(), test.getLocalMillis());
218 }
219
220 public void testConstructor_long_nullDateTimeZone() throws Throwable {
221 LocalDate test = new LocalDate(TEST_TIME1, (DateTimeZone) null);
222 assertEquals(ISO_UTC, test.getChronology());
223 assertEquals(1970, test.getYear());
224 assertEquals(4, test.getMonthOfYear());
225 assertEquals(6, test.getDayOfMonth());
226 }
227
228 public void testConstructor_long1_Chronology() throws Throwable {
229 LocalDate test = new LocalDate(TEST_TIME1, GREGORIAN_PARIS);
230 assertEquals(GREGORIAN_UTC, test.getChronology());
231 assertEquals(1970, test.getYear());
232 assertEquals(4, test.getMonthOfYear());
233 assertEquals(6, test.getDayOfMonth());
234 }
235
236 public void testConstructor_long2_Chronology() throws Throwable {
237 LocalDate test = new LocalDate(TEST_TIME2, GREGORIAN_PARIS);
238 assertEquals(GREGORIAN_UTC, test.getChronology());
239 assertEquals(1971, test.getYear());
240 assertEquals(5, test.getMonthOfYear());
241 assertEquals(7, test.getDayOfMonth());
242 }
243
244 public void testConstructor_long_nullChronology() throws Throwable {
245 LocalDate test = new LocalDate(TEST_TIME1, (Chronology) null);
246 assertEquals(ISO_UTC, test.getChronology());
247 assertEquals(1970, test.getYear());
248 assertEquals(4, test.getMonthOfYear());
249 assertEquals(6, test.getDayOfMonth());
250 }
251
252
253 public void testConstructor_Object1() throws Throwable {
254 Date date = new Date(TEST_TIME1);
255 LocalDate test = new LocalDate(date);
256 assertEquals(ISO_UTC, test.getChronology());
257 assertEquals(1970, test.getYear());
258 assertEquals(4, test.getMonthOfYear());
259 assertEquals(6, test.getDayOfMonth());
260 }
261
262 public void testConstructor_nullObject() throws Throwable {
263 LocalDate test = new LocalDate((Object) null);
264 assertEquals(ISO_UTC, test.getChronology());
265 assertEquals(1970, test.getYear());
266 assertEquals(6, test.getMonthOfYear());
267 assertEquals(9, test.getDayOfMonth());
268 }
269
270 public void testConstructor_ObjectString1() throws Throwable {
271 LocalDate test = new LocalDate("1972-04-06");
272 assertEquals(ISO_UTC, test.getChronology());
273 assertEquals(1972, test.getYear());
274 assertEquals(4, test.getMonthOfYear());
275 assertEquals(6, test.getDayOfMonth());
276 }
277
278 public void testConstructor_ObjectString2() throws Throwable {
279 LocalDate test = new LocalDate("1972-037");
280 assertEquals(ISO_UTC, test.getChronology());
281 assertEquals(1972, test.getYear());
282 assertEquals(2, test.getMonthOfYear());
283 assertEquals(6, test.getDayOfMonth());
284 }
285
286 public void testConstructor_ObjectString3() throws Throwable {
287 LocalDate test = new LocalDate("1972-02");
288 assertEquals(ISO_UTC, test.getChronology());
289 assertEquals(1972, test.getYear());
290 assertEquals(2, test.getMonthOfYear());
291 assertEquals(1, test.getDayOfMonth());
292 }
293
294 public void testConstructor_ObjectStringEx1() throws Throwable {
295 try {
296 new LocalDate("1970-04-06T+14:00");
297 fail();
298 } catch (IllegalArgumentException ex) {}
299 }
300
301 public void testConstructor_ObjectStringEx2() throws Throwable {
302 try {
303 new LocalDate("1970-04-06T10:20:30.040");
304 fail();
305 } catch (IllegalArgumentException ex) {}
306 }
307
308 public void testConstructor_ObjectStringEx3() throws Throwable {
309 try {
310 new LocalDate("1970-04-06T10:20:30.040+14:00");
311 fail();
312 } catch (IllegalArgumentException ex) {}
313 }
314
315 public void testConstructor_ObjectStringEx4() throws Throwable {
316 try {
317 new LocalDate("T10:20:30.040");
318 fail();
319 } catch (IllegalArgumentException ex) {}
320 }
321
322 public void testConstructor_ObjectStringEx5() throws Throwable {
323 try {
324 new LocalDate("T10:20:30.040+14:00");
325 fail();
326 } catch (IllegalArgumentException ex) {}
327 }
328
329 public void testConstructor_ObjectStringEx6() throws Throwable {
330 try {
331 new LocalDate("10:20:30.040");
332 fail();
333 } catch (IllegalArgumentException ex) {}
334 }
335
336 public void testConstructor_ObjectStringEx7() throws Throwable {
337 try {
338 new LocalDate("10:20:30.040+14:00");
339 fail();
340 } catch (IllegalArgumentException ex) {}
341 }
342
343 public void testConstructor_ObjectLocalDate() throws Throwable {
344 LocalDate date = new LocalDate(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 public void testConstructor_ObjectLocalTime() throws Throwable {
353 LocalTime time = new LocalTime(10, 20, 30, 40, BUDDHIST_UTC);
354 try {
355 new LocalDate(time);
356 fail();
357 } catch (IllegalArgumentException ex) {}
358 }
359
360 public void testConstructor_ObjectLocalDateTime() throws Throwable {
361 LocalDateTime dt = new LocalDateTime(1970, 5, 6, 10, 20, 30, 40, BUDDHIST_UTC);
362 LocalDate test = new LocalDate(dt);
363 assertEquals(BUDDHIST_UTC, test.getChronology());
364 assertEquals(1970, test.getYear());
365 assertEquals(5, test.getMonthOfYear());
366 assertEquals(6, test.getDayOfMonth());
367 }
368
369 public void testConstructor_ObjectYearMonthDay() throws Throwable {
370 YearMonthDay date = new YearMonthDay(1970, 4, 6, BUDDHIST_UTC);
371 LocalDate test = new LocalDate(date);
372 assertEquals(BUDDHIST_UTC, test.getChronology());
373 assertEquals(1970, test.getYear());
374 assertEquals(4, test.getMonthOfYear());
375 assertEquals(6, test.getDayOfMonth());
376 }
377
378
379 public void testConstructor_Object_DateTimeZone() throws Throwable {
380 Date date = new Date(TEST_TIME1);
381 LocalDate test = new LocalDate(date, PARIS);
382 assertEquals(ISO_UTC, test.getChronology());
383 assertEquals(1970, test.getYear());
384 assertEquals(4, test.getMonthOfYear());
385 assertEquals(6, test.getDayOfMonth());
386 }
387
388 public void testConstructor_nullObject_DateTimeZone() throws Throwable {
389 LocalDate test = new LocalDate((Object) null, PARIS);
390 assertEquals(ISO_UTC, test.getChronology());
391 assertEquals(1970, test.getYear());
392 assertEquals(6, test.getMonthOfYear());
393 assertEquals(9, test.getDayOfMonth());
394 }
395
396 public void testConstructor_Object_nullDateTimeZone() throws Throwable {
397 Date date = new Date(TEST_TIME1);
398 LocalDate test = new LocalDate(date, (DateTimeZone) null);
399 assertEquals(ISO_UTC, test.getChronology());
400 assertEquals(1970, test.getYear());
401 assertEquals(4, test.getMonthOfYear());
402 assertEquals(6, test.getDayOfMonth());
403 }
404
405 public void testConstructor_nullObject_nullDateTimeZone() throws Throwable {
406 LocalDate test = new LocalDate((Object) null, (DateTimeZone) null);
407 assertEquals(ISO_UTC, test.getChronology());
408 assertEquals(1970, test.getYear());
409 assertEquals(6, test.getMonthOfYear());
410 assertEquals(9, test.getDayOfMonth());
411 }
412
413 public void testConstructor_Object_Chronology() throws Throwable {
414 Date date = new Date(TEST_TIME1);
415 LocalDate test = new LocalDate(date, GREGORIAN_PARIS);
416 assertEquals(GREGORIAN_UTC, test.getChronology());
417 assertEquals(1970, test.getYear());
418 assertEquals(4, test.getMonthOfYear());
419 assertEquals(6, test.getDayOfMonth());
420 }
421
422 public void testConstructor_nullObject_Chronology() throws Throwable {
423 LocalDate test = new LocalDate((Object) null, GREGORIAN_PARIS);
424 assertEquals(GREGORIAN_UTC, test.getChronology());
425 assertEquals(1970, test.getYear());
426 assertEquals(6, test.getMonthOfYear());
427 assertEquals(9, test.getDayOfMonth());
428 }
429
430 public void testConstructor_Object_nullChronology() throws Throwable {
431 Date date = new Date(TEST_TIME1);
432 LocalDate test = new LocalDate(date, (Chronology) null);
433 assertEquals(ISO_UTC, test.getChronology());
434 assertEquals(1970, test.getYear());
435 assertEquals(4, test.getMonthOfYear());
436 assertEquals(6, test.getDayOfMonth());
437 }
438
439 public void testConstructor_nullObject_nullChronology() throws Throwable {
440 LocalDate test = new LocalDate((Object) null, (Chronology) null);
441 assertEquals(ISO_UTC, test.getChronology());
442 assertEquals(1970, test.getYear());
443 assertEquals(6, test.getMonthOfYear());
444 assertEquals(9, test.getDayOfMonth());
445 }
446
447
448 public void testConstructor_int_int_int() throws Throwable {
449 LocalDate test = new LocalDate(1970, 6, 9);
450 assertEquals(ISO_UTC, test.getChronology());
451 assertEquals(1970, test.getYear());
452 assertEquals(6, test.getMonthOfYear());
453 assertEquals(9, test.getDayOfMonth());
454 try {
455 new LocalDate(Integer.MIN_VALUE, 6, 9);
456 fail();
457 } catch (IllegalArgumentException ex) {}
458 try {
459 new LocalDate(Integer.MAX_VALUE, 6, 9);
460 fail();
461 } catch (IllegalArgumentException ex) {}
462 try {
463 new LocalDate(1970, 0, 9);
464 fail();
465 } catch (IllegalArgumentException ex) {}
466 try {
467 new LocalDate(1970, 13, 9);
468 fail();
469 } catch (IllegalArgumentException ex) {}
470 try {
471 new LocalDate(1970, 6, 0);
472 fail();
473 } catch (IllegalArgumentException ex) {}
474 try {
475 new LocalDate(1970, 6, 31);
476 fail();
477 } catch (IllegalArgumentException ex) {}
478 new LocalDate(1970, 7, 31);
479 try {
480 new LocalDate(1970, 7, 32);
481 fail();
482 } catch (IllegalArgumentException ex) {}
483 }
484
485 public void testConstructor_int_int_int_Chronology() throws Throwable {
486 LocalDate test = new LocalDate(1970, 6, 9, GREGORIAN_PARIS);
487 assertEquals(GREGORIAN_UTC, test.getChronology());
488 assertEquals(1970, test.getYear());
489 assertEquals(6, test.getMonthOfYear());
490 assertEquals(9, test.getDayOfMonth());
491 try {
492 new LocalDate(Integer.MIN_VALUE, 6, 9, GREGORIAN_PARIS);
493 fail();
494 } catch (IllegalArgumentException ex) {}
495 try {
496 new LocalDate(Integer.MAX_VALUE, 6, 9, GREGORIAN_PARIS);
497 fail();
498 } catch (IllegalArgumentException ex) {}
499 try {
500 new LocalDate(1970, 0, 9, GREGORIAN_PARIS);
501 fail();
502 } catch (IllegalArgumentException ex) {}
503 try {
504 new LocalDate(1970, 13, 9, GREGORIAN_PARIS);
505 fail();
506 } catch (IllegalArgumentException ex) {}
507 try {
508 new LocalDate(1970, 6, 0, GREGORIAN_PARIS);
509 fail();
510 } catch (IllegalArgumentException ex) {}
511 try {
512 new LocalDate(1970, 6, 31, GREGORIAN_PARIS);
513 fail();
514 } catch (IllegalArgumentException ex) {}
515 new LocalDate(1970, 7, 31, GREGORIAN_PARIS);
516 try {
517 new LocalDate(1970, 7, 32, GREGORIAN_PARIS);
518 fail();
519 } catch (IllegalArgumentException ex) {}
520 }
521
522 public void testConstructor_int_int_int_nullChronology() throws Throwable {
523 LocalDate test = new LocalDate(1970, 6, 9, null);
524 assertEquals(ISO_UTC, test.getChronology());
525 assertEquals(1970, test.getYear());
526 assertEquals(6, test.getMonthOfYear());
527 assertEquals(9, test.getDayOfMonth());
528 }
529
530 }