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.Date;
19 import java.util.Locale;
20
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23
24 import org.joda.time.chrono.GregorianChronology;
25 import org.joda.time.chrono.ISOChronology;
26 import org.joda.time.convert.ConverterManager;
27 import org.joda.time.convert.MockZeroNullIntegerConverter;
28 import org.joda.time.format.DateTimeFormat;
29
30
31
32
33
34
35 public class TestDateMidnight_Constructors extends TestCase {
36
37
38
39 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
40 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
41
42 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
43 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
44 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
45 366 + 365;
46 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
47 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
48 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
49 366 + 365 + 365;
50
51
52 private long TEST_TIME_NOW_UTC =
53 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
54 private long TEST_TIME_NOW_LONDON =
55 TEST_TIME_NOW_UTC - DateTimeConstants.MILLIS_PER_HOUR;
56 private long TEST_TIME_NOW_PARIS =
57 TEST_TIME_NOW_UTC - 2*DateTimeConstants.MILLIS_PER_HOUR;
58
59
60 private long TEST_TIME1_UTC =
61 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
62 + 12L * DateTimeConstants.MILLIS_PER_HOUR
63 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
64 private long TEST_TIME1_LONDON =
65 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
66 - DateTimeConstants.MILLIS_PER_HOUR;
67 private long TEST_TIME1_PARIS =
68 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
69 - 2*DateTimeConstants.MILLIS_PER_HOUR;
70
71
72 private long TEST_TIME2_UTC =
73 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
74 + 14L * DateTimeConstants.MILLIS_PER_HOUR
75 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
76 private long TEST_TIME2_LONDON =
77 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
78 - DateTimeConstants.MILLIS_PER_HOUR;
79 private long TEST_TIME2_PARIS =
80 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
81 - 2*DateTimeConstants.MILLIS_PER_HOUR;
82
83 private DateTimeZone zone = null;
84 private Locale locale = null;
85
86 public static void main(String[] args) {
87 junit.textui.TestRunner.run(suite());
88 }
89
90 public static TestSuite suite() {
91 return new TestSuite(TestDateMidnight_Constructors.class);
92 }
93
94 public TestDateMidnight_Constructors(String name) {
95 super(name);
96 }
97
98 protected void setUp() throws Exception {
99 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW_UTC);
100 zone = DateTimeZone.getDefault();
101 locale = Locale.getDefault();
102 DateTimeZone.setDefault(LONDON);
103 Locale.setDefault(Locale.UK);
104 }
105
106 protected void tearDown() throws Exception {
107 DateTimeUtils.setCurrentMillisSystem();
108 DateTimeZone.setDefault(zone);
109 Locale.setDefault(locale);
110 zone = null;
111 }
112
113
114 public void testTest() {
115 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW_UTC).toString());
116 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1_UTC).toString());
117 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2_UTC).toString());
118 }
119
120
121
122
123
124 public void test_now() throws Throwable {
125 DateMidnight test = DateMidnight.now();
126 assertEquals(ISOChronology.getInstance(), test.getChronology());
127 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
128 assertEquals(2002, test.getYear());
129 assertEquals(6, test.getMonthOfYear());
130 assertEquals(9, test.getDayOfMonth());
131 }
132
133
134
135
136 public void test_now_DateTimeZone() throws Throwable {
137 DateMidnight test = DateMidnight.now(PARIS);
138 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
139 assertEquals(TEST_TIME_NOW_PARIS, test.getMillis());
140 }
141
142
143
144
145 public void test_now_nullDateTimeZone() throws Throwable {
146 try {
147 DateMidnight.now((DateTimeZone) null);
148 fail();
149 } catch (NullPointerException ex) {}
150 }
151
152
153
154
155 public void test_now_Chronology() throws Throwable {
156 DateMidnight test = DateMidnight.now(GregorianChronology.getInstance());
157 assertEquals(GregorianChronology.getInstance(), test.getChronology());
158 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
159 }
160
161
162
163
164 public void test_now_nullChronology() throws Throwable {
165 try {
166 DateMidnight.now((Chronology) null);
167 fail();
168 } catch (NullPointerException ex) {}
169 }
170
171
172 public void testParse_noFormatter() throws Throwable {
173 assertEquals(new DateMidnight(2010, 6, 30, ISOChronology.getInstance(LONDON)), DateMidnight.parse("2010-06-30"));
174 assertEquals(new DateMidnight(2010, 1, 2, ISOChronology.getInstance(LONDON)), DateMidnight.parse("2010-002"));
175 }
176
177 public void testParse_formatter() throws Throwable {
178 assertEquals(new DateMidnight(2010, 6, 30, ISOChronology.getInstance(LONDON)), DateMidnight.parse("2010--30 06", DateTimeFormat.forPattern("yyyy--dd MM")));
179 }
180
181
182
183
184
185 public void testConstructor() throws Throwable {
186 DateMidnight test = new DateMidnight();
187 assertEquals(ISOChronology.getInstance(), test.getChronology());
188 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
189 assertEquals(2002, test.getYear());
190 assertEquals(6, test.getMonthOfYear());
191 assertEquals(9, test.getDayOfMonth());
192 }
193
194
195
196
197 public void testConstructor_DateTimeZone() throws Throwable {
198 DateMidnight test = new DateMidnight(PARIS);
199 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
200 assertEquals(TEST_TIME_NOW_PARIS, test.getMillis());
201 }
202
203
204
205
206 public void testConstructor_nullDateTimeZone() throws Throwable {
207 DateMidnight test = new DateMidnight((DateTimeZone) null);
208 assertEquals(ISOChronology.getInstance(), test.getChronology());
209 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
210 }
211
212
213
214
215 public void testConstructor_Chronology() throws Throwable {
216 DateMidnight test = new DateMidnight(GregorianChronology.getInstance());
217 assertEquals(GregorianChronology.getInstance(), test.getChronology());
218 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
219 }
220
221
222
223
224 public void testConstructor_nullChronology() throws Throwable {
225 DateMidnight test = new DateMidnight((Chronology) null);
226 assertEquals(ISOChronology.getInstance(), test.getChronology());
227 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
228 }
229
230
231
232
233
234 public void testConstructor_long1() throws Throwable {
235 DateMidnight test = new DateMidnight(TEST_TIME1_UTC);
236 assertEquals(ISOChronology.getInstance(), test.getChronology());
237 assertEquals(TEST_TIME1_LONDON, test.getMillis());
238 }
239
240
241
242
243 public void testConstructor_long2() throws Throwable {
244 DateMidnight test = new DateMidnight(TEST_TIME2_UTC);
245 assertEquals(ISOChronology.getInstance(), test.getChronology());
246 assertEquals(TEST_TIME2_LONDON, test.getMillis());
247 }
248
249
250
251
252 public void testConstructor_long1_DateTimeZone() throws Throwable {
253 DateMidnight test = new DateMidnight(TEST_TIME1_UTC, PARIS);
254 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
255 assertEquals(TEST_TIME1_PARIS, test.getMillis());
256 }
257
258
259
260
261 public void testConstructor_long2_DateTimeZone() throws Throwable {
262 DateMidnight test = new DateMidnight(TEST_TIME2_UTC, PARIS);
263 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
264 assertEquals(TEST_TIME2_PARIS, test.getMillis());
265 }
266
267
268
269
270 public void testConstructor_long_nullDateTimeZone() throws Throwable {
271 DateMidnight test = new DateMidnight(TEST_TIME1_UTC, (DateTimeZone) null);
272 assertEquals(ISOChronology.getInstance(), test.getChronology());
273 assertEquals(TEST_TIME1_LONDON, test.getMillis());
274 }
275
276
277
278
279 public void testConstructor_long1_Chronology() throws Throwable {
280 DateMidnight test = new DateMidnight(TEST_TIME1_UTC, GregorianChronology.getInstance());
281 assertEquals(GregorianChronology.getInstance(), test.getChronology());
282 assertEquals(TEST_TIME1_LONDON, test.getMillis());
283 }
284
285
286
287
288 public void testConstructor_long2_Chronology() throws Throwable {
289 DateMidnight test = new DateMidnight(TEST_TIME2_UTC, GregorianChronology.getInstance());
290 assertEquals(GregorianChronology.getInstance(), test.getChronology());
291 assertEquals(TEST_TIME2_LONDON, test.getMillis());
292 }
293
294
295
296
297 public void testConstructor_long_nullChronology() throws Throwable {
298 DateMidnight test = new DateMidnight(TEST_TIME1_UTC, (Chronology) null);
299 assertEquals(ISOChronology.getInstance(), test.getChronology());
300 assertEquals(TEST_TIME1_LONDON, test.getMillis());
301 }
302
303
304
305
306
307 public void testConstructor_Object() throws Throwable {
308 Date date = new Date(TEST_TIME1_UTC);
309 DateMidnight test = new DateMidnight(date);
310 assertEquals(ISOChronology.getInstance(), test.getChronology());
311 assertEquals(TEST_TIME1_LONDON, test.getMillis());
312 }
313
314
315
316
317 public void testConstructor_invalidObject() throws Throwable {
318 try {
319 new DateMidnight(new Object());
320 fail();
321 } catch (IllegalArgumentException ex) {}
322 }
323
324
325
326
327 public void testConstructor_nullObject() throws Throwable {
328 DateMidnight test = new DateMidnight((Object) null);
329 assertEquals(ISOChronology.getInstance(), test.getChronology());
330 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
331 }
332
333
334
335
336 public void testConstructor_badconverterObject() throws Throwable {
337 try {
338 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
339 DateMidnight test = new DateMidnight(new Integer(0));
340 assertEquals(ISOChronology.getInstance(), test.getChronology());
341 assertEquals(0L - DateTimeConstants.MILLIS_PER_HOUR, test.getMillis());
342 } finally {
343 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
344 }
345 }
346
347
348
349
350 public void testConstructor_Object_DateTimeZone() throws Throwable {
351 Date date = new Date(TEST_TIME1_UTC);
352 DateMidnight test = new DateMidnight(date, PARIS);
353 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
354 assertEquals(TEST_TIME1_PARIS, test.getMillis());
355 }
356
357
358
359
360 public void testConstructor_invalidObject_DateTimeZone() throws Throwable {
361 try {
362 new DateMidnight(new Object(), PARIS);
363 fail();
364 } catch (IllegalArgumentException ex) {}
365 }
366
367
368
369
370 public void testConstructor_nullObject_DateTimeZone() throws Throwable {
371 DateMidnight test = new DateMidnight((Object) null, PARIS);
372 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
373 assertEquals(TEST_TIME_NOW_PARIS, test.getMillis());
374 }
375
376
377
378
379 public void testConstructor_Object_nullDateTimeZone() throws Throwable {
380 Date date = new Date(TEST_TIME1_UTC);
381 DateMidnight test = new DateMidnight(date, (DateTimeZone) null);
382 assertEquals(ISOChronology.getInstance(), test.getChronology());
383 assertEquals(TEST_TIME1_LONDON, test.getMillis());
384 }
385
386
387
388
389 public void testConstructor_nullObject_nullDateTimeZone() throws Throwable {
390 DateMidnight test = new DateMidnight((Object) null, (DateTimeZone) null);
391 assertEquals(ISOChronology.getInstance(), test.getChronology());
392 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
393 }
394
395
396
397
398 public void testConstructor_badconverterObject_DateTimeZone() throws Throwable {
399 try {
400 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
401 DateMidnight test = new DateMidnight(new Integer(0), GregorianChronology.getInstance());
402 assertEquals(ISOChronology.getInstance(), test.getChronology());
403 assertEquals(0L - DateTimeConstants.MILLIS_PER_HOUR, test.getMillis());
404 } finally {
405 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
406 }
407 }
408
409
410
411
412 public void testConstructor_Object_Chronology() throws Throwable {
413 Date date = new Date(TEST_TIME1_UTC);
414 DateMidnight test = new DateMidnight(date, GregorianChronology.getInstance());
415 assertEquals(GregorianChronology.getInstance(), test.getChronology());
416 assertEquals(TEST_TIME1_LONDON, test.getMillis());
417 }
418
419
420
421
422 public void testConstructor_invalidObject_Chronology() throws Throwable {
423 try {
424 new DateMidnight(new Object(), GregorianChronology.getInstance());
425 fail();
426 } catch (IllegalArgumentException ex) {}
427 }
428
429
430
431
432 public void testConstructor_nullObject_Chronology() throws Throwable {
433 DateMidnight test = new DateMidnight((Object) null, GregorianChronology.getInstance());
434 assertEquals(GregorianChronology.getInstance(), test.getChronology());
435 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
436 }
437
438
439
440
441 public void testConstructor_Object_nullChronology() throws Throwable {
442 Date date = new Date(TEST_TIME1_UTC);
443 DateMidnight test = new DateMidnight(date, (Chronology) null);
444 assertEquals(ISOChronology.getInstance(), test.getChronology());
445 assertEquals(TEST_TIME1_LONDON, test.getMillis());
446 }
447
448
449
450
451 public void testConstructor_nullObject_nullChronology() throws Throwable {
452 DateMidnight test = new DateMidnight((Object) null, (Chronology) null);
453 assertEquals(ISOChronology.getInstance(), test.getChronology());
454 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
455 }
456
457
458
459
460 public void testConstructor_badconverterObject_Chronology() throws Throwable {
461 try {
462 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
463 DateMidnight test = new DateMidnight(new Integer(0), GregorianChronology.getInstance());
464 assertEquals(ISOChronology.getInstance(), test.getChronology());
465 assertEquals(0L - DateTimeConstants.MILLIS_PER_HOUR, test.getMillis());
466 } finally {
467 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE);
468 }
469 }
470
471
472
473
474
475 public void testConstructor_int_int_int() throws Throwable {
476 DateMidnight test = new DateMidnight(2002, 6, 9);
477 assertEquals(ISOChronology.getInstance(), test.getChronology());
478 assertEquals(LONDON, test.getZone());
479 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
480 assertEquals(2002, test.getYear());
481 assertEquals(6, test.getMonthOfYear());
482 assertEquals(9, test.getDayOfMonth());
483 try {
484 new DateMidnight(Integer.MIN_VALUE, 6, 9);
485 fail();
486 } catch (IllegalArgumentException ex) {}
487 try {
488 new DateMidnight(Integer.MAX_VALUE, 6, 9);
489 fail();
490 } catch (IllegalArgumentException ex) {}
491 try {
492 new DateMidnight(2002, 0, 9);
493 fail();
494 } catch (IllegalArgumentException ex) {}
495 try {
496 new DateMidnight(2002, 13, 9);
497 fail();
498 } catch (IllegalArgumentException ex) {}
499 try {
500 new DateMidnight(2002, 6, 0);
501 fail();
502 } catch (IllegalArgumentException ex) {}
503 try {
504 new DateMidnight(2002, 6, 31);
505 fail();
506 } catch (IllegalArgumentException ex) {}
507 new DateMidnight(2002, 7, 31);
508 try {
509 new DateMidnight(2002, 7, 32);
510 fail();
511 } catch (IllegalArgumentException ex) {}
512 }
513
514
515
516
517 public void testConstructor_int_int_int_DateTimeZone() throws Throwable {
518 DateMidnight test = new DateMidnight(2002, 6, 9, PARIS);
519 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology());
520 assertEquals(TEST_TIME_NOW_PARIS, test.getMillis());
521 assertEquals(2002, test.getYear());
522 assertEquals(6, test.getMonthOfYear());
523 assertEquals(9, test.getDayOfMonth());
524 try {
525 new DateMidnight(Integer.MIN_VALUE, 6, 9, PARIS);
526 fail();
527 } catch (IllegalArgumentException ex) {}
528 try {
529 new DateMidnight(Integer.MAX_VALUE, 6, 9, PARIS);
530 fail();
531 } catch (IllegalArgumentException ex) {}
532 try {
533 new DateMidnight(2002, 0, 9, PARIS);
534 fail();
535 } catch (IllegalArgumentException ex) {}
536 try {
537 new DateMidnight(2002, 13, 9, PARIS);
538 fail();
539 } catch (IllegalArgumentException ex) {}
540 try {
541 new DateMidnight(2002, 6, 0, PARIS);
542 fail();
543 } catch (IllegalArgumentException ex) {}
544 try {
545 new DateMidnight(2002, 6, 31, PARIS);
546 fail();
547 } catch (IllegalArgumentException ex) {}
548 new DateMidnight(2002, 7, 31, PARIS);
549 try {
550 new DateMidnight(2002, 7, 32, PARIS);
551 fail();
552 } catch (IllegalArgumentException ex) {}
553 }
554
555
556
557
558 public void testConstructor_int_int_int_nullDateTimeZone() throws Throwable {
559 DateMidnight test = new DateMidnight(2002, 6, 9, (DateTimeZone) null);
560 assertEquals(ISOChronology.getInstance(), test.getChronology());
561 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
562 assertEquals(2002, test.getYear());
563 assertEquals(6, test.getMonthOfYear());
564 assertEquals(9, test.getDayOfMonth());
565 }
566
567
568
569
570 public void testConstructor_int_int_int_Chronology() throws Throwable {
571 DateMidnight test = new DateMidnight(2002, 6, 9, GregorianChronology.getInstance());
572 assertEquals(GregorianChronology.getInstance(), test.getChronology());
573 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
574 assertEquals(2002, test.getYear());
575 assertEquals(6, test.getMonthOfYear());
576 assertEquals(9, test.getDayOfMonth());
577 try {
578 new DateMidnight(Integer.MIN_VALUE, 6, 9, GregorianChronology.getInstance());
579 fail();
580 } catch (IllegalArgumentException ex) {}
581 try {
582 new DateMidnight(Integer.MAX_VALUE, 6, 9, GregorianChronology.getInstance());
583 fail();
584 } catch (IllegalArgumentException ex) {}
585 try {
586 new DateMidnight(2002, 0, 9, GregorianChronology.getInstance());
587 fail();
588 } catch (IllegalArgumentException ex) {}
589 try {
590 new DateMidnight(2002, 13, 9, GregorianChronology.getInstance());
591 fail();
592 } catch (IllegalArgumentException ex) {}
593 try {
594 new DateMidnight(2002, 6, 0, GregorianChronology.getInstance());
595 fail();
596 } catch (IllegalArgumentException ex) {}
597 try {
598 new DateMidnight(2002, 6, 31, GregorianChronology.getInstance());
599 fail();
600 } catch (IllegalArgumentException ex) {}
601 new DateMidnight(2002, 7, 31, GregorianChronology.getInstance());
602 try {
603 new DateMidnight(2002, 7, 32, GregorianChronology.getInstance());
604 fail();
605 } catch (IllegalArgumentException ex) {}
606 }
607
608
609
610
611 public void testConstructor_int_int_int_nullChronology() throws Throwable {
612 DateMidnight test = new DateMidnight(2002, 6, 9, (Chronology) null);
613 assertEquals(ISOChronology.getInstance(), test.getChronology());
614 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis());
615 assertEquals(2002, test.getYear());
616 assertEquals(6, test.getMonthOfYear());
617 assertEquals(9, test.getDayOfMonth());
618 }
619
620 }