1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.util.Arrays;
23 import java.util.Date;
24 import java.util.Locale;
25
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29 import org.joda.time.chrono.BuddhistChronology;
30 import org.joda.time.chrono.CopticChronology;
31 import org.joda.time.chrono.GJChronology;
32 import org.joda.time.chrono.GregorianChronology;
33 import org.joda.time.chrono.ISOChronology;
34 import org.joda.time.chrono.LenientChronology;
35 import org.joda.time.chrono.StrictChronology;
36 import org.joda.time.format.DateTimeFormat;
37 import org.joda.time.format.DateTimeFormatter;
38
39 /***
40 * This class is a Junit unit test for LocalDate.
41 *
42 * @author Stephen Colebourne
43 */
44 public class TestLocalDate_Basics extends TestCase {
45
46 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
47 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
48 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
49 private static final int OFFSET = 1;
50 private static final GJChronology GJ_UTC = GJChronology.getInstanceUTC();
51 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS);
52 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON);
53 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO);
54 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC();
55 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
56 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON);
57 private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO);
58 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
59 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS);
60 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON);
61 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO);
62 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC();
63
64 /*** Mock zone simulating Asia/Gaza cutover at midnight 2007-04-01 */
65 private static long CUTOVER_GAZA = 1175378400000L;
66 private static int OFFSET_GAZA = 7200000;
67 private static final DateTimeZone MOCK_GAZA = new MockZone(CUTOVER_GAZA, OFFSET_GAZA);
68
69 private long TEST_TIME_NOW =
70 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
71
72 private long TEST_TIME1 =
73 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
74 + 12L * DateTimeConstants.MILLIS_PER_HOUR
75 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
76
77 private long TEST_TIME2 =
78 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY
79 + 14L * DateTimeConstants.MILLIS_PER_HOUR
80 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
81
82 private DateTimeZone zone = null;
83
84 public static void main(String[] args) {
85 junit.textui.TestRunner.run(suite());
86 }
87
88 public static TestSuite suite() {
89 return new TestSuite(TestLocalDate_Basics.class);
90 }
91
92 public TestLocalDate_Basics(String name) {
93 super(name);
94 }
95
96 protected void setUp() throws Exception {
97 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
98 zone = DateTimeZone.getDefault();
99 DateTimeZone.setDefault(LONDON);
100 }
101
102 protected void tearDown() throws Exception {
103 DateTimeUtils.setCurrentMillisSystem();
104 DateTimeZone.setDefault(zone);
105 zone = null;
106 }
107
108
109 public void testGet_DateTimeFieldType() {
110 LocalDate test = new LocalDate();
111 assertEquals(1970, test.get(DateTimeFieldType.year()));
112 assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
113 assertEquals(9, test.get(DateTimeFieldType.dayOfMonth()));
114 assertEquals(2, test.get(DateTimeFieldType.dayOfWeek()));
115 assertEquals(160, test.get(DateTimeFieldType.dayOfYear()));
116 assertEquals(24, test.get(DateTimeFieldType.weekOfWeekyear()));
117 assertEquals(1970, test.get(DateTimeFieldType.weekyear()));
118 try {
119 test.get(null);
120 fail();
121 } catch (IllegalArgumentException ex) {}
122 try {
123 test.get(DateTimeFieldType.hourOfDay());
124 fail();
125 } catch (IllegalArgumentException ex) {}
126 }
127
128 public void testSize() {
129 LocalDate test = new LocalDate();
130 assertEquals(3, test.size());
131 }
132
133 public void testGetFieldType_int() {
134 LocalDate test = new LocalDate(COPTIC_PARIS);
135 assertSame(DateTimeFieldType.year(), test.getFieldType(0));
136 assertSame(DateTimeFieldType.monthOfYear(), test.getFieldType(1));
137 assertSame(DateTimeFieldType.dayOfMonth(), test.getFieldType(2));
138 try {
139 test.getFieldType(-1);
140 } catch (IndexOutOfBoundsException ex) {}
141 try {
142 test.getFieldType(3);
143 } catch (IndexOutOfBoundsException ex) {}
144 }
145
146 public void testGetFieldTypes() {
147 LocalDate test = new LocalDate(COPTIC_PARIS);
148 DateTimeFieldType[] fields = test.getFieldTypes();
149 assertSame(DateTimeFieldType.year(), fields[0]);
150 assertSame(DateTimeFieldType.monthOfYear(), fields[1]);
151 assertSame(DateTimeFieldType.dayOfMonth(), fields[2]);
152 assertNotSame(test.getFieldTypes(), test.getFieldTypes());
153 }
154
155 public void testGetField_int() {
156 LocalDate test = new LocalDate(COPTIC_PARIS);
157 assertSame(COPTIC_UTC.year(), test.getField(0));
158 assertSame(COPTIC_UTC.monthOfYear(), test.getField(1));
159 assertSame(COPTIC_UTC.dayOfMonth(), test.getField(2));
160 try {
161 test.getField(-1);
162 } catch (IndexOutOfBoundsException ex) {}
163 try {
164 test.getField(3);
165 } catch (IndexOutOfBoundsException ex) {}
166 }
167
168 public void testGetFields() {
169 LocalDate test = new LocalDate(COPTIC_PARIS);
170 DateTimeField[] fields = test.getFields();
171 assertSame(COPTIC_UTC.year(), fields[0]);
172 assertSame(COPTIC_UTC.monthOfYear(), fields[1]);
173 assertSame(COPTIC_UTC.dayOfMonth(), fields[2]);
174 assertNotSame(test.getFields(), test.getFields());
175 }
176
177 public void testGetValue_int() {
178 LocalDate test = new LocalDate();
179 assertEquals(1970, test.getValue(0));
180 assertEquals(6, test.getValue(1));
181 assertEquals(9, test.getValue(2));
182 try {
183 test.getValue(-1);
184 } catch (IndexOutOfBoundsException ex) {}
185 try {
186 test.getValue(3);
187 } catch (IndexOutOfBoundsException ex) {}
188 }
189
190 public void testGetValues() {
191 LocalDate test = new LocalDate();
192 int[] values = test.getValues();
193 assertEquals(1970, values[0]);
194 assertEquals(6, values[1]);
195 assertEquals(9, values[2]);
196 assertNotSame(test.getValues(), test.getValues());
197 }
198
199 public void testIsSupported_DateTimeFieldType() {
200 LocalDate test = new LocalDate(COPTIC_PARIS);
201 assertEquals(true, test.isSupported(DateTimeFieldType.year()));
202 assertEquals(true, test.isSupported(DateTimeFieldType.monthOfYear()));
203 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfMonth()));
204 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfWeek()));
205 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear()));
206 assertEquals(true, test.isSupported(DateTimeFieldType.weekOfWeekyear()));
207 assertEquals(true, test.isSupported(DateTimeFieldType.weekyear()));
208 assertEquals(true, test.isSupported(DateTimeFieldType.yearOfCentury()));
209 assertEquals(true, test.isSupported(DateTimeFieldType.yearOfEra()));
210 assertEquals(true, test.isSupported(DateTimeFieldType.centuryOfEra()));
211 assertEquals(true, test.isSupported(DateTimeFieldType.weekyearOfCentury()));
212 assertEquals(true, test.isSupported(DateTimeFieldType.era()));
213 assertEquals(false, test.isSupported(DateTimeFieldType.hourOfDay()));
214 assertEquals(false, test.isSupported((DateTimeFieldType) null));
215 }
216
217 public void testIsSupported_DurationFieldType() {
218 LocalDate test = new LocalDate(1970, 6, 9);
219 assertEquals(false, test.isSupported(DurationFieldType.eras()));
220 assertEquals(true, test.isSupported(DurationFieldType.centuries()));
221 assertEquals(true, test.isSupported(DurationFieldType.years()));
222 assertEquals(true, test.isSupported(DurationFieldType.months()));
223 assertEquals(true, test.isSupported(DurationFieldType.weekyears()));
224 assertEquals(true, test.isSupported(DurationFieldType.weeks()));
225 assertEquals(true, test.isSupported(DurationFieldType.days()));
226
227 assertEquals(false, test.isSupported(DurationFieldType.hours()));
228 assertEquals(false, test.isSupported((DurationFieldType) null));
229 }
230
231 public void testEqualsHashCode() {
232 LocalDate test1 = new LocalDate(1970, 6, 9, COPTIC_PARIS);
233 LocalDate test2 = new LocalDate(1970, 6, 9, COPTIC_PARIS);
234 assertEquals(true, test1.equals(test2));
235 assertEquals(true, test2.equals(test1));
236 assertEquals(true, test1.equals(test1));
237 assertEquals(true, test2.equals(test2));
238 assertEquals(true, test1.hashCode() == test2.hashCode());
239 assertEquals(true, test1.hashCode() == test1.hashCode());
240 assertEquals(true, test2.hashCode() == test2.hashCode());
241
242 LocalDate test3 = new LocalDate(1971, 6, 9);
243 assertEquals(false, test1.equals(test3));
244 assertEquals(false, test2.equals(test3));
245 assertEquals(false, test3.equals(test1));
246 assertEquals(false, test3.equals(test2));
247 assertEquals(false, test1.hashCode() == test3.hashCode());
248 assertEquals(false, test2.hashCode() == test3.hashCode());
249
250 assertEquals(false, test1.equals("Hello"));
251 assertEquals(true, test1.equals(new MockInstant()));
252 assertEquals(true, test1.equals(new YearMonthDay(1970, 6, 9, COPTIC_PARIS)));
253 assertEquals(true, test1.hashCode() == new YearMonthDay(1970, 6, 9, COPTIC_PARIS).hashCode());
254 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE));
255 }
256
257 class MockInstant extends MockPartial {
258 public Chronology getChronology() {
259 return COPTIC_UTC;
260 }
261 public DateTimeField[] getFields() {
262 return new DateTimeField[] {
263 COPTIC_UTC.year(),
264 COPTIC_UTC.monthOfYear(),
265 COPTIC_UTC.dayOfMonth(),
266 };
267 }
268 public int[] getValues() {
269 return new int[] {1970, 6, 9};
270 }
271 }
272
273 public void testEqualsHashCodeLenient() {
274 LocalDate test1 = new LocalDate(1970, 6, 9, LenientChronology.getInstance(COPTIC_PARIS));
275 LocalDate test2 = new LocalDate(1970, 6, 9, LenientChronology.getInstance(COPTIC_PARIS));
276 assertEquals(true, test1.equals(test2));
277 assertEquals(true, test2.equals(test1));
278 assertEquals(true, test1.equals(test1));
279 assertEquals(true, test2.equals(test2));
280 assertEquals(true, test1.hashCode() == test2.hashCode());
281 assertEquals(true, test1.hashCode() == test1.hashCode());
282 assertEquals(true, test2.hashCode() == test2.hashCode());
283 }
284
285 public void testEqualsHashCodeStrict() {
286 LocalDate test1 = new LocalDate(1970, 6, 9, StrictChronology.getInstance(COPTIC_PARIS));
287 LocalDate test2 = new LocalDate(1970, 6, 9, StrictChronology.getInstance(COPTIC_PARIS));
288 assertEquals(true, test1.equals(test2));
289 assertEquals(true, test2.equals(test1));
290 assertEquals(true, test1.equals(test1));
291 assertEquals(true, test2.equals(test2));
292 assertEquals(true, test1.hashCode() == test2.hashCode());
293 assertEquals(true, test1.hashCode() == test1.hashCode());
294 assertEquals(true, test2.hashCode() == test2.hashCode());
295 }
296
297
298 public void testCompareTo() {
299 LocalDate test1 = new LocalDate(2005, 6, 2);
300 LocalDate test1a = new LocalDate(2005, 6, 2);
301 assertEquals(0, test1.compareTo(test1a));
302 assertEquals(0, test1a.compareTo(test1));
303 assertEquals(0, test1.compareTo(test1));
304 assertEquals(0, test1a.compareTo(test1a));
305
306 LocalDate test2 = new LocalDate(2005, 7, 2);
307 assertEquals(-1, test1.compareTo(test2));
308 assertEquals(+1, test2.compareTo(test1));
309
310 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC());
311 assertEquals(-1, test1.compareTo(test3));
312 assertEquals(+1, test3.compareTo(test1));
313 assertEquals(0, test3.compareTo(test2));
314
315 DateTimeFieldType[] types = new DateTimeFieldType[] {
316 DateTimeFieldType.year(),
317 DateTimeFieldType.monthOfYear(),
318 DateTimeFieldType.dayOfMonth(),
319 };
320 int[] values = new int[] {2005, 6, 2};
321 Partial p = new Partial(types, values);
322 assertEquals(0, test1.compareTo(p));
323 assertEquals(0, test1.compareTo(new YearMonthDay(2005, 6, 2)));
324 try {
325 test1.compareTo(null);
326 fail();
327 } catch (NullPointerException ex) {}
328 try {
329 test1.compareTo(new Date());
330 fail();
331 } catch (ClassCastException ex) {}
332 try {
333 test1.compareTo(new TimeOfDay());
334 fail();
335 } catch (ClassCastException ex) {}
336 Partial partial = new Partial()
337 .with(DateTimeFieldType.centuryOfEra(), 1)
338 .with(DateTimeFieldType.halfdayOfDay(), 0)
339 .with(DateTimeFieldType.dayOfMonth(), 9);
340 try {
341 new LocalDate(1970, 6, 9).compareTo(partial);
342 fail();
343 } catch (ClassCastException ex) {}
344 }
345
346
347 public void testIsEqual_LocalDate() {
348 LocalDate test1 = new LocalDate(2005, 6, 2);
349 LocalDate test1a = new LocalDate(2005, 6, 2);
350 assertEquals(true, test1.isEqual(test1a));
351 assertEquals(true, test1a.isEqual(test1));
352 assertEquals(true, test1.isEqual(test1));
353 assertEquals(true, test1a.isEqual(test1a));
354
355 LocalDate test2 = new LocalDate(2005, 7, 2);
356 assertEquals(false, test1.isEqual(test2));
357 assertEquals(false, test2.isEqual(test1));
358
359 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC());
360 assertEquals(false, test1.isEqual(test3));
361 assertEquals(false, test3.isEqual(test1));
362 assertEquals(true, test3.isEqual(test2));
363
364 try {
365 new LocalDate(2005, 7, 2).isEqual(null);
366 fail();
367 } catch (IllegalArgumentException ex) {}
368 }
369
370
371 public void testIsBefore_LocalDate() {
372 LocalDate test1 = new LocalDate(2005, 6, 2);
373 LocalDate test1a = new LocalDate(2005, 6, 2);
374 assertEquals(false, test1.isBefore(test1a));
375 assertEquals(false, test1a.isBefore(test1));
376 assertEquals(false, test1.isBefore(test1));
377 assertEquals(false, test1a.isBefore(test1a));
378
379 LocalDate test2 = new LocalDate(2005, 7, 2);
380 assertEquals(true, test1.isBefore(test2));
381 assertEquals(false, test2.isBefore(test1));
382
383 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC());
384 assertEquals(true, test1.isBefore(test3));
385 assertEquals(false, test3.isBefore(test1));
386 assertEquals(false, test3.isBefore(test2));
387
388 try {
389 new LocalDate(2005, 7, 2).isBefore(null);
390 fail();
391 } catch (IllegalArgumentException ex) {}
392 }
393
394
395 public void testIsAfter_LocalDate() {
396 LocalDate test1 = new LocalDate(2005, 6, 2);
397 LocalDate test1a = new LocalDate(2005, 6, 2);
398 assertEquals(false, test1.isAfter(test1a));
399 assertEquals(false, test1a.isAfter(test1));
400 assertEquals(false, test1.isAfter(test1));
401 assertEquals(false, test1a.isAfter(test1a));
402
403 LocalDate test2 = new LocalDate(2005, 7, 2);
404 assertEquals(false, test1.isAfter(test2));
405 assertEquals(true, test2.isAfter(test1));
406
407 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC());
408 assertEquals(false, test1.isAfter(test3));
409 assertEquals(true, test3.isAfter(test1));
410 assertEquals(false, test3.isAfter(test2));
411
412 try {
413 new LocalDate(2005, 7, 2).isAfter(null);
414 fail();
415 } catch (IllegalArgumentException ex) {}
416 }
417
418
419 public void testWithField_DateTimeFieldType_int_1() {
420 LocalDate test = new LocalDate(2004, 6, 9);
421 LocalDate result = test.withField(DateTimeFieldType.year(), 2006);
422
423 assertEquals(new LocalDate(2004, 6, 9), test);
424 assertEquals(new LocalDate(2006, 6, 9), result);
425 }
426
427 public void testWithField_DateTimeFieldType_int_2() {
428 LocalDate test = new LocalDate(2004, 6, 9);
429 try {
430 test.withField(null, 6);
431 fail();
432 } catch (IllegalArgumentException ex) {}
433 }
434
435 public void testWithField_DateTimeFieldType_int_3() {
436 LocalDate test = new LocalDate(2004, 6, 9);
437 try {
438 test.withField(DateTimeFieldType.hourOfDay(), 6);
439 fail();
440 } catch (IllegalArgumentException ex) {}
441 }
442
443 public void testWithField_DateTimeFieldType_int_4() {
444 LocalDate test = new LocalDate(2004, 6, 9);
445 LocalDate result = test.withField(DateTimeFieldType.year(), 2004);
446 assertEquals(new LocalDate(2004, 6, 9), test);
447 assertSame(test, result);
448 }
449
450
451 public void testWithFieldAdded_DurationFieldType_int_1() {
452 LocalDate test = new LocalDate(2004, 6, 9);
453 LocalDate result = test.withFieldAdded(DurationFieldType.years(), 6);
454
455 assertEquals(new LocalDate(2004, 6, 9), test);
456 assertEquals(new LocalDate(2010, 6, 9), result);
457 }
458
459 public void testWithFieldAdded_DurationFieldType_int_2() {
460 LocalDate test = new LocalDate(2004, 6, 9);
461 try {
462 test.withFieldAdded(null, 0);
463 fail();
464 } catch (IllegalArgumentException ex) {}
465 }
466
467 public void testWithFieldAdded_DurationFieldType_int_3() {
468 LocalDate test = new LocalDate(2004, 6, 9);
469 try {
470 test.withFieldAdded(null, 6);
471 fail();
472 } catch (IllegalArgumentException ex) {}
473 }
474
475 public void testWithFieldAdded_DurationFieldType_int_4() {
476 LocalDate test = new LocalDate(2004, 6, 9);
477 LocalDate result = test.withFieldAdded(DurationFieldType.years(), 0);
478 assertSame(test, result);
479 }
480
481 public void testWithFieldAdded_DurationFieldType_int_5() {
482 LocalDate test = new LocalDate(2004, 6, 9);
483 try {
484 test.withFieldAdded(DurationFieldType.hours(), 6);
485 fail();
486 } catch (IllegalArgumentException ex) {}
487 }
488
489
490 public void testPlus_RP() {
491 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON);
492 LocalDate result = test.plus(new Period(1, 2, 3, 4, 29, 6, 7, 8));
493 LocalDate expected = new LocalDate(2003, 7, 28, BUDDHIST_LONDON);
494 assertEquals(expected, result);
495
496 result = test.plus((ReadablePeriod) null);
497 assertSame(test, result);
498 }
499
500 public void testPlusYears_int() {
501 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON);
502 LocalDate result = test.plusYears(1);
503 LocalDate expected = new LocalDate(2003, 5, 3, BUDDHIST_LONDON);
504 assertEquals(expected, result);
505
506 result = test.plusYears(0);
507 assertSame(test, result);
508 }
509
510 public void testPlusMonths_int() {
511 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON);
512 LocalDate result = test.plusMonths(1);
513 LocalDate expected = new LocalDate(2002, 6, 3, BUDDHIST_LONDON);
514 assertEquals(expected, result);
515
516 result = test.plusMonths(0);
517 assertSame(test, result);
518 }
519
520 public void testPlusWeeks_int() {
521 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON);
522 LocalDate result = test.plusWeeks(1);
523 LocalDate expected = new LocalDate(2002, 5, 10, BUDDHIST_LONDON);
524 assertEquals(expected, result);
525
526 result = test.plusWeeks(0);
527 assertSame(test, result);
528 }
529
530 public void testPlusDays_int() {
531 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON);
532 LocalDate result = test.plusDays(1);
533 LocalDate expected = new LocalDate(2002, 5, 4, BUDDHIST_LONDON);
534 assertEquals(expected, result);
535
536 result = test.plusDays(0);
537 assertSame(test, result);
538 }
539
540
541 public void testMinus_RP() {
542 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON);
543 LocalDate result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1));
544
545
546
547
548 LocalDate expected = new LocalDate(2001, 3, 26, BUDDHIST_LONDON);
549 assertEquals(expected, result);
550
551 result = test.minus((ReadablePeriod) null);
552 assertSame(test, result);
553 }
554
555 public void testMinusYears_int() {
556 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON);
557 LocalDate result = test.minusYears(1);
558 LocalDate expected = new LocalDate(2001, 5, 3, BUDDHIST_LONDON);
559 assertEquals(expected, result);
560
561 result = test.minusYears(0);
562 assertSame(test, result);
563 }
564
565 public void testMinusMonths_int() {
566 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON);
567 LocalDate result = test.minusMonths(1);
568 LocalDate expected = new LocalDate(2002, 4, 3, BUDDHIST_LONDON);
569 assertEquals(expected, result);
570
571 result = test.minusMonths(0);
572 assertSame(test, result);
573 }
574
575 public void testMinusWeeks_int() {
576 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON);
577 LocalDate result = test.minusWeeks(1);
578 LocalDate expected = new LocalDate(2002, 4, 26, BUDDHIST_LONDON);
579 assertEquals(expected, result);
580
581 result = test.minusWeeks(0);
582 assertSame(test, result);
583 }
584
585 public void testMinusDays_int() {
586 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON);
587 LocalDate result = test.minusDays(1);
588 LocalDate expected = new LocalDate(2002, 5, 2, BUDDHIST_LONDON);
589 assertEquals(expected, result);
590
591 result = test.minusDays(0);
592 assertSame(test, result);
593 }
594
595
596 public void testGetters() {
597 LocalDate test = new LocalDate(1970, 6, 9, GJ_UTC);
598 assertEquals(1970, test.getYear());
599 assertEquals(6, test.getMonthOfYear());
600 assertEquals(9, test.getDayOfMonth());
601 assertEquals(160, test.getDayOfYear());
602 assertEquals(2, test.getDayOfWeek());
603 assertEquals(24, test.getWeekOfWeekyear());
604 assertEquals(1970, test.getWeekyear());
605 assertEquals(70, test.getYearOfCentury());
606 assertEquals(20, test.getCenturyOfEra());
607 assertEquals(1970, test.getYearOfEra());
608 assertEquals(DateTimeConstants.AD, test.getEra());
609 }
610
611
612 public void testWithers() {
613 LocalDate test = new LocalDate(1970, 6, 9, GJ_UTC);
614 check(test.withYear(2000), 2000, 6, 9);
615 check(test.withMonthOfYear(2), 1970, 2, 9);
616 check(test.withDayOfMonth(2), 1970, 6, 2);
617 check(test.withDayOfYear(6), 1970, 1, 6);
618 check(test.withDayOfWeek(6), 1970, 6, 13);
619 check(test.withWeekOfWeekyear(6), 1970, 2, 3);
620 check(test.withWeekyear(1971), 1971, 6, 15);
621 check(test.withYearOfCentury(60), 1960, 6, 9);
622 check(test.withCenturyOfEra(21), 2070, 6, 9);
623 check(test.withYearOfEra(1066), 1066, 6, 9);
624 check(test.withEra(DateTimeConstants.BC), -1970, 6, 9);
625 try {
626 test.withMonthOfYear(0);
627 fail();
628 } catch (IllegalArgumentException ex) {}
629 try {
630 test.withMonthOfYear(13);
631 fail();
632 } catch (IllegalArgumentException ex) {}
633 }
634
635
636 public void testToDateTimeAtStartOfDay() {
637 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
638
639 DateTime test = base.toDateTimeAtStartOfDay();
640 check(base, 2005, 6, 9);
641 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test);
642 }
643
644 public void testToDateTimeAtStartOfDay_avoidDST() {
645 LocalDate base = new LocalDate(2007, 4, 1);
646
647 DateTimeZone.setDefault(MOCK_GAZA);
648 DateTime test = base.toDateTimeAtStartOfDay();
649 check(base, 2007, 4, 1);
650 assertEquals(new DateTime(2007, 4, 1, 1, 0, 0, 0, MOCK_GAZA), test);
651 }
652
653
654 public void testToDateTimeAtStartOfDay_Zone() {
655 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
656
657 DateTime test = base.toDateTimeAtStartOfDay(TOKYO);
658 check(base, 2005, 6, 9);
659 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_TOKYO), test);
660 }
661
662 public void testToDateTimeAtStartOfDay_Zone_avoidDST() {
663 LocalDate base = new LocalDate(2007, 4, 1);
664
665 DateTime test = base.toDateTimeAtStartOfDay(MOCK_GAZA);
666 check(base, 2007, 4, 1);
667 assertEquals(new DateTime(2007, 4, 1, 1, 0, 0, 0, MOCK_GAZA), test);
668 }
669
670 public void testToDateTimeAtStartOfDay_nullZone() {
671 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
672
673 DateTime test = base.toDateTimeAtStartOfDay((DateTimeZone) null);
674 check(base, 2005, 6, 9);
675 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test);
676 }
677
678
679 public void testToDateTimeAtMidnight() {
680 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
681
682 DateTime test = base.toDateTimeAtMidnight();
683 check(base, 2005, 6, 9);
684 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test);
685 }
686
687
688 public void testToDateTimeAtMidnight_Zone() {
689 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
690
691 DateTime test = base.toDateTimeAtMidnight(TOKYO);
692 check(base, 2005, 6, 9);
693 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_TOKYO), test);
694 }
695
696 public void testToDateTimeAtMidnight_nullZone() {
697 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
698
699 DateTime test = base.toDateTimeAtMidnight((DateTimeZone) null);
700 check(base, 2005, 6, 9);
701 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test);
702 }
703
704
705 public void testToDateTimeAtCurrentTime() {
706 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
707 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
708 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
709
710 DateTime test = base.toDateTimeAtCurrentTime();
711 check(base, 2005, 6, 9);
712 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON);
713 expected = expected.year().setCopy(2005);
714 expected = expected.monthOfYear().setCopy(6);
715 expected = expected.dayOfMonth().setCopy(9);
716 assertEquals(expected, test);
717 }
718
719
720 public void testToDateTimeAtCurrentTime_Zone() {
721 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
722 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
723 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
724
725 DateTime test = base.toDateTimeAtCurrentTime(TOKYO);
726 check(base, 2005, 6, 9);
727 DateTime expected = new DateTime(dt.getMillis(), COPTIC_TOKYO);
728 expected = expected.year().setCopy(2005);
729 expected = expected.monthOfYear().setCopy(6);
730 expected = expected.dayOfMonth().setCopy(9);
731 assertEquals(expected, test);
732 }
733
734 public void testToDateTimeAtCurrentTime_nullZone() {
735 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
736 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
737 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
738
739 DateTime test = base.toDateTimeAtCurrentTime((DateTimeZone) null);
740 check(base, 2005, 6, 9);
741 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON);
742 expected = expected.year().setCopy(2005);
743 expected = expected.monthOfYear().setCopy(6);
744 expected = expected.dayOfMonth().setCopy(9);
745 assertEquals(expected, test);
746 }
747
748
749 public void testToLocalDateTime_LocalTime() {
750 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
751 LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO);
752
753 LocalDateTime test = base.toLocalDateTime(tod);
754 check(base, 2005, 6, 9);
755 LocalDateTime expected = new LocalDateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_UTC);
756 assertEquals(expected, test);
757 }
758
759 public void testToLocalDateTime_nullLocalTime() {
760 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
761
762 try {
763 base.toLocalDateTime((LocalTime) null);
764 fail();
765 } catch (IllegalArgumentException ex) {
766
767 }
768 }
769
770 public void testToLocalDateTime_wrongChronologyLocalTime() {
771 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
772 LocalTime tod = new LocalTime(12, 13, 14, 15, BUDDHIST_PARIS);
773
774 try {
775 base.toLocalDateTime(tod);
776 fail();
777 } catch (IllegalArgumentException ex) {
778
779 }
780 }
781
782
783 public void testToDateTime_LocalTime() {
784 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
785 LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO);
786
787 DateTime test = base.toDateTime(tod);
788 check(base, 2005, 6, 9);
789 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON);
790 assertEquals(expected, test);
791 }
792
793 public void testToDateTime_nullLocalTime() {
794 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
795 long now = new DateTime(2004, 5, 8, 12, 13, 14, 15, COPTIC_LONDON).getMillis();
796 DateTimeUtils.setCurrentMillisFixed(now);
797
798 DateTime test = base.toDateTime((LocalTime) null);
799 check(base, 2005, 6, 9);
800 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON);
801 assertEquals(expected, test);
802 }
803
804
805 public void testToDateTime_LocalTime_Zone() {
806 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
807 LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO);
808
809 DateTime test = base.toDateTime(tod, TOKYO);
810 check(base, 2005, 6, 9);
811 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_TOKYO);
812 assertEquals(expected, test);
813 }
814
815 public void testToDateTime_LocalTime_nullZone() {
816 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
817 LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO);
818
819 DateTime test = base.toDateTime(tod, null);
820 check(base, 2005, 6, 9);
821 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON);
822 assertEquals(expected, test);
823 }
824
825 public void testToDateTime_nullLocalTime_Zone() {
826 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
827 long now = new DateTime(2004, 5, 8, 12, 13, 14, 15, COPTIC_TOKYO).getMillis();
828 DateTimeUtils.setCurrentMillisFixed(now);
829
830 DateTime test = base.toDateTime((LocalTime) null, TOKYO);
831 check(base, 2005, 6, 9);
832 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_TOKYO);
833 assertEquals(expected, test);
834 }
835
836 public void testToDateTime_wrongChronoLocalTime_Zone() {
837 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
838 LocalTime tod = new LocalTime(12, 13, 14, 15, BUDDHIST_TOKYO);
839
840 try {
841 base.toDateTime(tod, LONDON);
842 fail();
843 } catch (IllegalArgumentException ex) {}
844 }
845
846
847 public void testToDateMidnight() {
848 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
849
850 DateMidnight test = base.toDateMidnight();
851 check(base, 2005, 6, 9);
852 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test);
853 }
854
855
856 public void testToDateMidnight_Zone() {
857 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
858
859 DateMidnight test = base.toDateMidnight(TOKYO);
860 check(base, 2005, 6, 9);
861 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_TOKYO), test);
862 }
863
864 public void testToDateMidnight_nullZone() {
865 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
866
867 DateMidnight test = base.toDateMidnight((DateTimeZone) null);
868 check(base, 2005, 6, 9);
869 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test);
870 }
871
872
873 public void testToDateTime_RI() {
874 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
875 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7);
876
877 DateTime test = base.toDateTime(dt);
878 check(base, 2005, 6, 9);
879 DateTime expected = dt;
880 expected = expected.year().setCopy(2005);
881 expected = expected.monthOfYear().setCopy(6);
882 expected = expected.dayOfMonth().setCopy(9);
883 assertEquals(expected, test);
884 }
885
886 public void testToDateTime_nullRI() {
887 LocalDate base = new LocalDate(2005, 6, 9);
888 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7);
889 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
890
891 DateTime test = base.toDateTime((ReadableInstant) null);
892 check(base, 2005, 6, 9);
893 DateTime expected = dt;
894 expected = expected.year().setCopy(2005);
895 expected = expected.monthOfYear().setCopy(6);
896 expected = expected.dayOfMonth().setCopy(9);
897 assertEquals(expected, test);
898 }
899
900
901 public void testToInterval() {
902 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
903 Interval test = base.toInterval();
904 check(base, 2005, 6, 9);
905 DateTime start = base.toDateTimeAtMidnight();
906 DateTime end = start.plus(Period.days(1));
907 Interval expected = new Interval(start, end);
908 assertEquals(expected, test);
909 }
910
911
912 public void testToInterval_Zone() {
913 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
914 Interval test = base.toInterval(TOKYO);
915 check(base, 2005, 6, 9);
916 DateTime start = base.toDateTimeAtMidnight(TOKYO);
917 DateTime end = start.plus(Period.days(1));
918 Interval expected = new Interval(start, end);
919 assertEquals(expected, test);
920 }
921
922 public void testToInterval_nullZone() {
923 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS);
924 Interval test = base.toInterval(null);
925 check(base, 2005, 6, 9);
926 DateTime start = base.toDateTimeAtMidnight(LONDON);
927 DateTime end = start.plus(Period.days(1));
928 Interval expected = new Interval(start, end);
929 assertEquals(expected, test);
930 }
931
932
933 public void testProperty() {
934 LocalDate test = new LocalDate(2005, 6, 9, GJ_UTC);
935 assertEquals(test.year(), test.property(DateTimeFieldType.year()));
936 assertEquals(test.monthOfYear(), test.property(DateTimeFieldType.monthOfYear()));
937 assertEquals(test.dayOfMonth(), test.property(DateTimeFieldType.dayOfMonth()));
938 assertEquals(test.dayOfWeek(), test.property(DateTimeFieldType.dayOfWeek()));
939 assertEquals(test.dayOfYear(), test.property(DateTimeFieldType.dayOfYear()));
940 assertEquals(test.weekOfWeekyear(), test.property(DateTimeFieldType.weekOfWeekyear()));
941 assertEquals(test.weekyear(), test.property(DateTimeFieldType.weekyear()));
942 assertEquals(test.yearOfCentury(), test.property(DateTimeFieldType.yearOfCentury()));
943 assertEquals(test.yearOfEra(), test.property(DateTimeFieldType.yearOfEra()));
944 assertEquals(test.centuryOfEra(), test.property(DateTimeFieldType.centuryOfEra()));
945 assertEquals(test.era(), test.property(DateTimeFieldType.era()));
946 try {
947 test.property(DateTimeFieldType.millisOfDay());
948 fail();
949 } catch (IllegalArgumentException ex) {}
950 try {
951 test.property(null);
952 fail();
953 } catch (IllegalArgumentException ex) {}
954 }
955
956
957 public void testSerialization() throws Exception {
958 LocalDate test = new LocalDate(1972, 6, 9, COPTIC_PARIS);
959
960 ByteArrayOutputStream baos = new ByteArrayOutputStream();
961 ObjectOutputStream oos = new ObjectOutputStream(baos);
962 oos.writeObject(test);
963 byte[] bytes = baos.toByteArray();
964 oos.close();
965
966 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
967 ObjectInputStream ois = new ObjectInputStream(bais);
968 LocalDate result = (LocalDate) ois.readObject();
969 ois.close();
970
971 assertEquals(test, result);
972 assertTrue(Arrays.equals(test.getValues(), result.getValues()));
973 assertTrue(Arrays.equals(test.getFields(), result.getFields()));
974 assertEquals(test.getChronology(), result.getChronology());
975 }
976
977
978 public void testToString() {
979 LocalDate test = new LocalDate(2002, 6, 9);
980 assertEquals("2002-06-09", test.toString());
981 }
982
983
984 public void testToString_String() {
985 LocalDate test = new LocalDate(2002, 6, 9);
986 assertEquals("2002 \ufffd\ufffd", test.toString("yyyy HH"));
987 assertEquals("2002-06-09", test.toString((String) null));
988 }
989
990
991 public void testToString_String_Locale() {
992 LocalDate test = new LocalDate(1970, 6, 9);
993 assertEquals("Tue 9/6", test.toString("EEE d/M", Locale.ENGLISH));
994 assertEquals("mar. 9/6", test.toString("EEE d/M", Locale.FRENCH));
995 assertEquals("1970-06-09", test.toString(null, Locale.ENGLISH));
996 assertEquals("Tue 9/6", test.toString("EEE d/M", null));
997 assertEquals("1970-06-09", test.toString(null, null));
998 }
999
1000
1001 public void testToString_DTFormatter() {
1002 LocalDate test = new LocalDate(2002, 6, 9);
1003 assertEquals("2002 \ufffd\ufffd", test.toString(DateTimeFormat.forPattern("yyyy HH")));
1004 assertEquals("2002-06-09", test.toString((DateTimeFormatter) null));
1005 }
1006
1007
1008 private void check(LocalDate test, int hour, int min, int sec) {
1009 assertEquals(hour, test.getYear());
1010 assertEquals(min, test.getMonthOfYear());
1011 assertEquals(sec, test.getDayOfMonth());
1012 }
1013 }