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