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.Locale;
24
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 import org.joda.time.chrono.BuddhistChronology;
29 import org.joda.time.chrono.CopticChronology;
30 import org.joda.time.chrono.ISOChronology;
31 import org.joda.time.format.DateTimeFormat;
32 import org.joda.time.format.DateTimeFormatter;
33
34
35
36
37
38
39 public class TestPartial_Basics extends TestCase {
40
41 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
42 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
43 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
44 private static final int OFFSET = 1;
45 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS);
46 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON);
47 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO);
48 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC();
49 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
50 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON);
51 private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO);
52 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
53 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS);
54 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON);
55 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO);
56 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC();
57
58 private long TEST_TIME_NOW =
59 10L * DateTimeConstants.MILLIS_PER_HOUR
60 + 20L * DateTimeConstants.MILLIS_PER_MINUTE
61 + 30L * DateTimeConstants.MILLIS_PER_SECOND
62 + 40L;
63
64 private long TEST_TIME1 =
65 1L * DateTimeConstants.MILLIS_PER_HOUR
66 + 2L * DateTimeConstants.MILLIS_PER_MINUTE
67 + 3L * DateTimeConstants.MILLIS_PER_SECOND
68 + 4L;
69
70 private long TEST_TIME2 =
71 1L * DateTimeConstants.MILLIS_PER_DAY
72 + 5L * DateTimeConstants.MILLIS_PER_HOUR
73 + 6L * DateTimeConstants.MILLIS_PER_MINUTE
74 + 7L * DateTimeConstants.MILLIS_PER_SECOND
75 + 8L;
76
77 private DateTimeZone zone = null;
78
79 public static void main(String[] args) {
80 junit.textui.TestRunner.run(suite());
81 }
82
83 public static TestSuite suite() {
84 return new TestSuite(TestPartial_Basics.class);
85 }
86
87 public TestPartial_Basics(String name) {
88 super(name);
89 }
90
91 protected void setUp() throws Exception {
92 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
93 zone = DateTimeZone.getDefault();
94 DateTimeZone.setDefault(LONDON);
95 }
96
97 protected void tearDown() throws Exception {
98 DateTimeUtils.setCurrentMillisSystem();
99 DateTimeZone.setDefault(zone);
100 zone = null;
101 }
102
103
104 public void testGet() {
105 Partial test = createHourMinPartial();
106 assertEquals(10, test.get(DateTimeFieldType.hourOfDay()));
107 assertEquals(20, test.get(DateTimeFieldType.minuteOfHour()));
108 try {
109 test.get(null);
110 fail();
111 } catch (IllegalArgumentException ex) {}
112 try {
113 test.get(DateTimeFieldType.secondOfMinute());
114 fail();
115 } catch (IllegalArgumentException ex) {}
116 }
117
118 public void testSize() {
119 Partial test = createHourMinPartial();
120 assertEquals(2, test.size());
121 }
122
123 public void testGetFieldType() {
124 Partial test = createHourMinPartial();
125 assertSame(DateTimeFieldType.hourOfDay(), test.getFieldType(0));
126 assertSame(DateTimeFieldType.minuteOfHour(), test.getFieldType(1));
127 try {
128 test.getFieldType(-1);
129 } catch (IndexOutOfBoundsException ex) {}
130 try {
131 test.getFieldType(2);
132 } catch (IndexOutOfBoundsException ex) {}
133 }
134
135 public void testGetFieldTypes() {
136 Partial test = createHourMinPartial();
137 DateTimeFieldType[] fields = test.getFieldTypes();
138 assertEquals(2, fields.length);
139 assertSame(DateTimeFieldType.hourOfDay(), fields[0]);
140 assertSame(DateTimeFieldType.minuteOfHour(), fields[1]);
141 assertNotSame(test.getFieldTypes(), test.getFieldTypes());
142 }
143
144 public void testGetField() {
145 Partial test = createHourMinPartial(COPTIC_PARIS);
146 assertSame(CopticChronology.getInstanceUTC().hourOfDay(), test.getField(0));
147 assertSame(CopticChronology.getInstanceUTC().minuteOfHour(), test.getField(1));
148 try {
149 test.getField(-1);
150 } catch (IndexOutOfBoundsException ex) {}
151 try {
152 test.getField(5);
153 } catch (IndexOutOfBoundsException ex) {}
154 }
155
156 public void testGetFields() {
157 Partial test = createHourMinPartial(COPTIC_PARIS);
158 DateTimeField[] fields = test.getFields();
159 assertEquals(2, fields.length);
160 assertSame(CopticChronology.getInstanceUTC().hourOfDay(), fields[0]);
161 assertSame(CopticChronology.getInstanceUTC().minuteOfHour(), fields[1]);
162 assertNotSame(test.getFields(), test.getFields());
163 }
164
165 public void testGetValue() {
166 Partial test = createHourMinPartial(COPTIC_PARIS);
167 assertEquals(10, test.getValue(0));
168 assertEquals(20, test.getValue(1));
169 try {
170 test.getValue(-1);
171 } catch (IndexOutOfBoundsException ex) {}
172 try {
173 test.getValue(2);
174 } catch (IndexOutOfBoundsException ex) {}
175 }
176
177 public void testGetValues() {
178 Partial test = createHourMinPartial(COPTIC_PARIS);
179 int[] values = test.getValues();
180 assertEquals(2, values.length);
181 assertEquals(10, values[0]);
182 assertEquals(20, values[1]);
183 assertNotSame(test.getValues(), test.getValues());
184 }
185
186 public void testIsSupported() {
187 Partial test = createHourMinPartial(COPTIC_PARIS);
188 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfDay()));
189 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfHour()));
190 assertEquals(false, test.isSupported(DateTimeFieldType.secondOfMinute()));
191 assertEquals(false, test.isSupported(DateTimeFieldType.millisOfSecond()));
192 assertEquals(false, test.isSupported(DateTimeFieldType.dayOfMonth()));
193 }
194
195 public void testEqualsHashCode() {
196 Partial test1 = createHourMinPartial(COPTIC_PARIS);
197 Partial test2 = createHourMinPartial(COPTIC_PARIS);
198 assertEquals(true, test1.equals(test2));
199 assertEquals(true, test2.equals(test1));
200 assertEquals(true, test1.equals(test1));
201 assertEquals(true, test2.equals(test2));
202 assertEquals(true, test1.hashCode() == test2.hashCode());
203 assertEquals(true, test1.hashCode() == test1.hashCode());
204 assertEquals(true, test2.hashCode() == test2.hashCode());
205
206 Partial test3 = createHourMinPartial2(COPTIC_PARIS);
207 assertEquals(false, test1.equals(test3));
208 assertEquals(false, test2.equals(test3));
209 assertEquals(false, test3.equals(test1));
210 assertEquals(false, test3.equals(test2));
211 assertEquals(false, test1.hashCode() == test3.hashCode());
212 assertEquals(false, test2.hashCode() == test3.hashCode());
213
214 assertEquals(false, test1.equals("Hello"));
215 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE));
216 assertEquals(new TimeOfDay(10, 20, 30, 40), createTODPartial(ISO_UTC));
217 }
218
219
220 public void testCompareTo() {
221 Partial test1 = createHourMinPartial();
222 Partial test1a = createHourMinPartial();
223 assertEquals(0, test1.compareTo(test1a));
224 assertEquals(0, test1a.compareTo(test1));
225 assertEquals(0, test1.compareTo(test1));
226 assertEquals(0, test1a.compareTo(test1a));
227
228 Partial test2 = createHourMinPartial2(ISO_UTC);
229 assertEquals(-1, test1.compareTo(test2));
230 assertEquals(+1, test2.compareTo(test1));
231
232 Partial test3 = createHourMinPartial2(COPTIC_UTC);
233 assertEquals(-1, test1.compareTo(test3));
234 assertEquals(+1, test3.compareTo(test1));
235 assertEquals(0, test3.compareTo(test2));
236
237 assertEquals(0, new TimeOfDay(10, 20, 30, 40).compareTo(createTODPartial(ISO_UTC)));
238
239 try {
240 test1.compareTo(null);
241 fail();
242 } catch (NullPointerException ex) {}
243
244
245
246
247 try {
248 test1.compareTo(new YearMonthDay());
249 fail();
250 } catch (ClassCastException ex) {}
251 try {
252 createTODPartial(ISO_UTC).without(DateTimeFieldType.hourOfDay()).compareTo(new YearMonthDay());
253 fail();
254 } catch (ClassCastException ex) {}
255 }
256
257
258 public void testIsEqual_TOD() {
259 Partial test1 = createHourMinPartial();
260 Partial test1a = createHourMinPartial();
261 assertEquals(true, test1.isEqual(test1a));
262 assertEquals(true, test1a.isEqual(test1));
263 assertEquals(true, test1.isEqual(test1));
264 assertEquals(true, test1a.isEqual(test1a));
265
266 Partial test2 = createHourMinPartial2(ISO_UTC);
267 assertEquals(false, test1.isEqual(test2));
268 assertEquals(false, test2.isEqual(test1));
269
270 Partial test3 = createHourMinPartial2(COPTIC_UTC);
271 assertEquals(false, test1.isEqual(test3));
272 assertEquals(false, test3.isEqual(test1));
273 assertEquals(true, test3.isEqual(test2));
274
275 try {
276 createHourMinPartial().isEqual(null);
277 fail();
278 } catch (IllegalArgumentException ex) {}
279 }
280
281
282 public void testIsBefore_TOD() {
283 Partial test1 = createHourMinPartial();
284 Partial test1a = createHourMinPartial();
285 assertEquals(false, test1.isBefore(test1a));
286 assertEquals(false, test1a.isBefore(test1));
287 assertEquals(false, test1.isBefore(test1));
288 assertEquals(false, test1a.isBefore(test1a));
289
290 Partial test2 = createHourMinPartial2(ISO_UTC);
291 assertEquals(true, test1.isBefore(test2));
292 assertEquals(false, test2.isBefore(test1));
293
294 Partial test3 = createHourMinPartial2(COPTIC_UTC);
295 assertEquals(true, test1.isBefore(test3));
296 assertEquals(false, test3.isBefore(test1));
297 assertEquals(false, test3.isBefore(test2));
298
299 try {
300 createHourMinPartial().isBefore(null);
301 fail();
302 } catch (IllegalArgumentException ex) {}
303 }
304
305
306 public void testIsAfter_TOD() {
307 Partial test1 = createHourMinPartial();
308 Partial test1a = createHourMinPartial();
309 assertEquals(false, test1.isAfter(test1a));
310 assertEquals(false, test1a.isAfter(test1));
311 assertEquals(false, test1.isAfter(test1));
312 assertEquals(false, test1a.isAfter(test1a));
313
314 Partial test2 = createHourMinPartial2(ISO_UTC);
315 assertEquals(false, test1.isAfter(test2));
316 assertEquals(true, test2.isAfter(test1));
317
318 Partial test3 = createHourMinPartial2(COPTIC_UTC);
319 assertEquals(false, test1.isAfter(test3));
320 assertEquals(true, test3.isAfter(test1));
321 assertEquals(false, test3.isAfter(test2));
322
323 try {
324 createHourMinPartial().isAfter(null);
325 fail();
326 } catch (IllegalArgumentException ex) {}
327 }
328
329
330 public void testWithChronologyRetainFields_Chrono() {
331 Partial base = createHourMinPartial(COPTIC_PARIS);
332 Partial test = base.withChronologyRetainFields(BUDDHIST_TOKYO);
333 check(base, 10, 20);
334 assertEquals(COPTIC_UTC, base.getChronology());
335 check(test, 10, 20);
336 assertEquals(BUDDHIST_UTC, test.getChronology());
337 }
338
339 public void testWithChronologyRetainFields_sameChrono() {
340 Partial base = createHourMinPartial(COPTIC_PARIS);
341 Partial test = base.withChronologyRetainFields(COPTIC_TOKYO);
342 assertSame(base, test);
343 }
344
345 public void testWithChronologyRetainFields_nullChrono() {
346 Partial base = createHourMinPartial(COPTIC_PARIS);
347 Partial test = base.withChronologyRetainFields(null);
348 check(base, 10, 20);
349 assertEquals(COPTIC_UTC, base.getChronology());
350 check(test, 10, 20);
351 assertEquals(ISO_UTC, test.getChronology());
352 }
353
354
355 public void testWith1() {
356 Partial test = createHourMinPartial();
357 Partial result = test.with(DateTimeFieldType.hourOfDay(), 15);
358 check(test, 10, 20);
359 check(result, 15, 20);
360 }
361
362 public void testWith2() {
363 Partial test = createHourMinPartial();
364 try {
365 test.with(null, 6);
366 fail();
367 } catch (IllegalArgumentException ex) {}
368 check(test, 10, 20);
369 }
370
371 public void testWith3a() {
372 Partial test = createHourMinPartial();
373 Partial result = test.with(DateTimeFieldType.secondOfMinute(), 15);
374 check(test, 10, 20);
375 assertEquals(3, result.size());
376 assertEquals(true, result.isSupported(DateTimeFieldType.hourOfDay()));
377 assertEquals(true, result.isSupported(DateTimeFieldType.minuteOfHour()));
378 assertEquals(true, result.isSupported(DateTimeFieldType.secondOfMinute()));
379 assertEquals(DateTimeFieldType.hourOfDay(), result.getFieldType(0));
380 assertEquals(DateTimeFieldType.minuteOfHour(), result.getFieldType(1));
381 assertEquals(DateTimeFieldType.secondOfMinute(), result.getFieldType(2));
382 assertEquals(10, result.get(DateTimeFieldType.hourOfDay()));
383 assertEquals(20, result.get(DateTimeFieldType.minuteOfHour()));
384 assertEquals(15, result.get(DateTimeFieldType.secondOfMinute()));
385 }
386
387 public void testWith3b() {
388 Partial test = createHourMinPartial();
389 Partial result = test.with(DateTimeFieldType.minuteOfDay(), 15);
390 check(test, 10, 20);
391 assertEquals(3, result.size());
392 assertEquals(true, result.isSupported(DateTimeFieldType.hourOfDay()));
393 assertEquals(true, result.isSupported(DateTimeFieldType.minuteOfDay()));
394 assertEquals(true, result.isSupported(DateTimeFieldType.minuteOfHour()));
395 assertEquals(DateTimeFieldType.hourOfDay(), result.getFieldType(0));
396 assertEquals(DateTimeFieldType.minuteOfDay(), result.getFieldType(1));
397 assertEquals(DateTimeFieldType.minuteOfHour(), result.getFieldType(2));
398 assertEquals(10, result.get(DateTimeFieldType.hourOfDay()));
399 assertEquals(20, result.get(DateTimeFieldType.minuteOfHour()));
400 assertEquals(15, result.get(DateTimeFieldType.minuteOfDay()));
401 }
402
403 public void testWith3c() {
404 Partial test = createHourMinPartial();
405 Partial result = test.with(DateTimeFieldType.dayOfMonth(), 15);
406 check(test, 10, 20);
407 assertEquals(3, result.size());
408 assertEquals(true, result.isSupported(DateTimeFieldType.dayOfMonth()));
409 assertEquals(true, result.isSupported(DateTimeFieldType.hourOfDay()));
410 assertEquals(true, result.isSupported(DateTimeFieldType.minuteOfHour()));
411 assertEquals(DateTimeFieldType.dayOfMonth(), result.getFieldType(0));
412 assertEquals(DateTimeFieldType.hourOfDay(), result.getFieldType(1));
413 assertEquals(DateTimeFieldType.minuteOfHour(), result.getFieldType(2));
414 assertEquals(10, result.get(DateTimeFieldType.hourOfDay()));
415 assertEquals(20, result.get(DateTimeFieldType.minuteOfHour()));
416 assertEquals(15, result.get(DateTimeFieldType.dayOfMonth()));
417 }
418
419 public void testWith3d() {
420 Partial test = new Partial(DateTimeFieldType.year(), 2005);
421 Partial result = test.with(DateTimeFieldType.monthOfYear(), 6);
422 assertEquals(2, result.size());
423 assertEquals(2005, result.get(DateTimeFieldType.year()));
424 assertEquals(6, result.get(DateTimeFieldType.monthOfYear()));
425 }
426
427 public void testWith3e() {
428 Partial test = new Partial(DateTimeFieldType.era(), 1);
429 Partial result = test.with(DateTimeFieldType.halfdayOfDay(), 0);
430 assertEquals(2, result.size());
431 assertEquals(1, result.get(DateTimeFieldType.era()));
432 assertEquals(0, result.get(DateTimeFieldType.halfdayOfDay()));
433 assertEquals(0, result.indexOf(DateTimeFieldType.era()));
434 assertEquals(1, result.indexOf(DateTimeFieldType.halfdayOfDay()));
435 }
436
437 public void testWith3f() {
438 Partial test = new Partial(DateTimeFieldType.halfdayOfDay(), 0);
439 Partial result = test.with(DateTimeFieldType.era(), 1);
440 assertEquals(2, result.size());
441 assertEquals(1, result.get(DateTimeFieldType.era()));
442 assertEquals(0, result.get(DateTimeFieldType.halfdayOfDay()));
443 assertEquals(0, result.indexOf(DateTimeFieldType.era()));
444 assertEquals(1, result.indexOf(DateTimeFieldType.halfdayOfDay()));
445 }
446
447 public void testWith4() {
448 Partial test = createHourMinPartial();
449 Partial result = test.with(DateTimeFieldType.hourOfDay(), 10);
450 assertSame(test, result);
451 }
452
453
454 public void testWithout1() {
455 Partial test = createHourMinPartial();
456 Partial result = test.without(DateTimeFieldType.year());
457 check(test, 10, 20);
458 check(result, 10, 20);
459 }
460
461 public void testWithout2() {
462 Partial test = createHourMinPartial();
463 Partial result = test.without((DateTimeFieldType) null);
464 check(test, 10, 20);
465 check(result, 10, 20);
466 }
467
468 public void testWithout3() {
469 Partial test = createHourMinPartial();
470 Partial result = test.without(DateTimeFieldType.hourOfDay());
471 check(test, 10, 20);
472 assertEquals(1, result.size());
473 assertEquals(false, result.isSupported(DateTimeFieldType.hourOfDay()));
474 assertEquals(true, result.isSupported(DateTimeFieldType.minuteOfHour()));
475 assertEquals(DateTimeFieldType.minuteOfHour(), result.getFieldType(0));
476 }
477
478 public void testWithout4() {
479 Partial test = createHourMinPartial();
480 Partial result = test.without(DateTimeFieldType.minuteOfHour());
481 check(test, 10, 20);
482 assertEquals(1, result.size());
483 assertEquals(true, result.isSupported(DateTimeFieldType.hourOfDay()));
484 assertEquals(false, result.isSupported(DateTimeFieldType.minuteOfHour()));
485 assertEquals(DateTimeFieldType.hourOfDay(), result.getFieldType(0));
486 }
487
488 public void testWithout5() {
489 Partial test = new Partial(DateTimeFieldType.hourOfDay(), 12);
490 Partial result = test.without(DateTimeFieldType.hourOfDay());
491 assertEquals(0, result.size());
492 assertEquals(false, result.isSupported(DateTimeFieldType.hourOfDay()));
493 }
494
495
496 public void testWithField1() {
497 Partial test = createHourMinPartial();
498 Partial result = test.withField(DateTimeFieldType.hourOfDay(), 15);
499 check(test, 10, 20);
500 check(result, 15, 20);
501 }
502
503 public void testWithField2() {
504 Partial test = createHourMinPartial();
505 try {
506 test.withField(null, 6);
507 fail();
508 } catch (IllegalArgumentException ex) {}
509 check(test, 10, 20);
510 }
511
512 public void testWithField3() {
513 Partial test = createHourMinPartial();
514 try {
515 test.withField(DateTimeFieldType.dayOfMonth(), 6);
516 fail();
517 } catch (IllegalArgumentException ex) {}
518 check(test, 10, 20);
519 }
520
521 public void testWithField4() {
522 Partial test = createHourMinPartial();
523 Partial result = test.withField(DateTimeFieldType.hourOfDay(), 10);
524 assertSame(test, result);
525 }
526
527
528 public void testWithFieldAdded1() {
529 Partial test = createHourMinPartial();
530 Partial result = test.withFieldAdded(DurationFieldType.hours(), 6);
531
532 assertEquals(createHourMinPartial(), test);
533 check(test, 10, 20);
534 check(result, 16, 20);
535 }
536
537 public void testWithFieldAdded2() {
538 Partial test = createHourMinPartial();
539 try {
540 test.withFieldAdded(null, 0);
541 fail();
542 } catch (IllegalArgumentException ex) {}
543 check(test, 10, 20);
544 }
545
546 public void testWithFieldAdded3() {
547 Partial test = createHourMinPartial();
548 try {
549 test.withFieldAdded(null, 6);
550 fail();
551 } catch (IllegalArgumentException ex) {}
552 check(test, 10, 20);
553 }
554
555 public void testWithFieldAdded4() {
556 Partial test = createHourMinPartial();
557 Partial result = test.withFieldAdded(DurationFieldType.hours(), 0);
558 assertSame(test, result);
559 }
560
561 public void testWithFieldAdded5() {
562 Partial test = createHourMinPartial();
563 try {
564 test.withFieldAdded(DurationFieldType.days(), 6);
565 fail();
566 } catch (IllegalArgumentException ex) {}
567 check(test, 10, 20);
568 }
569
570 public void testWithFieldAdded6() {
571 Partial test = createHourMinPartial();
572 try {
573 test.withFieldAdded(DurationFieldType.hours(), 16);
574 fail();
575 } catch (IllegalArgumentException ex) {
576
577 }
578 check(test, 10, 20);
579 }
580
581 public void testWithFieldAdded7() {
582 Partial test = createHourMinPartial(23, 59, ISO_UTC);
583 try {
584 test.withFieldAdded(DurationFieldType.minutes(), 1);
585 fail();
586 } catch (IllegalArgumentException ex) {
587
588 }
589 check(test, 23, 59);
590
591 test = createHourMinPartial(23, 59, ISO_UTC);
592 try {
593 test.withFieldAdded(DurationFieldType.hours(), 1);
594 fail();
595 } catch (IllegalArgumentException ex) {
596
597 }
598 check(test, 23, 59);
599 }
600
601 public void testWithFieldAdded8() {
602 Partial test = createHourMinPartial(0, 0, ISO_UTC);
603 try {
604 test.withFieldAdded(DurationFieldType.minutes(), -1);
605 fail();
606 } catch (IllegalArgumentException ex) {
607
608 }
609 check(test, 0, 0);
610
611 test = createHourMinPartial(0, 0, ISO_UTC);
612 try {
613 test.withFieldAdded(DurationFieldType.hours(), -1);
614 fail();
615 } catch (IllegalArgumentException ex) {
616
617 }
618 check(test, 0, 0);
619 }
620
621
622 public void testWithFieldAddWrapped1() {
623 Partial test = createHourMinPartial();
624 Partial result = test.withFieldAddWrapped(DurationFieldType.hours(), 6);
625
626 assertEquals(createHourMinPartial(), test);
627 check(test, 10, 20);
628 check(result, 16, 20);
629 }
630
631 public void testWithFieldAddWrapped2() {
632 Partial test = createHourMinPartial();
633 try {
634 test.withFieldAddWrapped(null, 0);
635 fail();
636 } catch (IllegalArgumentException ex) {}
637 check(test, 10, 20);
638 }
639
640 public void testWithFieldAddWrapped3() {
641 Partial test = createHourMinPartial();
642 try {
643 test.withFieldAddWrapped(null, 6);
644 fail();
645 } catch (IllegalArgumentException ex) {}
646 check(test, 10, 20);
647 }
648
649 public void testWithFieldAddWrapped4() {
650 Partial test = createHourMinPartial();
651 Partial result = test.withFieldAddWrapped(DurationFieldType.hours(), 0);
652 assertSame(test, result);
653 }
654
655 public void testWithFieldAddWrapped5() {
656 Partial test = createHourMinPartial();
657 try {
658 test.withFieldAddWrapped(DurationFieldType.days(), 6);
659 fail();
660 } catch (IllegalArgumentException ex) {}
661 check(test, 10, 20);
662 }
663
664 public void testWithFieldAddWrapped6() {
665 Partial test = createHourMinPartial();
666 Partial result = test.withFieldAddWrapped(DurationFieldType.hours(), 16);
667
668 assertEquals(createHourMinPartial(), test);
669 check(test, 10, 20);
670 check(result, 2, 20);
671 }
672
673 public void testWithFieldAddWrapped7() {
674 Partial test = createHourMinPartial(23, 59, ISO_UTC);
675 Partial result = test.withFieldAddWrapped(DurationFieldType.minutes(), 1);
676 check(test, 23, 59);
677 check(result, 0, 0);
678
679 test = createHourMinPartial(23, 59, ISO_UTC);
680 result = test.withFieldAddWrapped(DurationFieldType.hours(), 1);
681 check(test, 23, 59);
682 check(result, 0, 59);
683 }
684
685 public void testWithFieldAddWrapped8() {
686 Partial test = createHourMinPartial(0, 0, ISO_UTC);
687 Partial result = test.withFieldAddWrapped(DurationFieldType.minutes(), -1);
688 check(test, 0, 0);
689 check(result, 23, 59);
690
691 test = createHourMinPartial(0, 0, ISO_UTC);
692 result = test.withFieldAddWrapped(DurationFieldType.hours(), -1);
693 check(test, 0, 0);
694 check(result, 23, 0);
695 }
696
697
698 public void testPlus_RP() {
699 Partial test = createHourMinPartial(BUDDHIST_LONDON);
700 Partial result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7, 8));
701 check(test, 10, 20);
702 check(result, 15, 26);
703
704 result = test.plus((ReadablePeriod) null);
705 assertSame(test, result);
706 }
707
708
709 public void testMinus_RP() {
710 Partial test = createHourMinPartial(BUDDHIST_LONDON);
711 Partial result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1));
712 check(test, 10, 20);
713 check(result, 9, 19);
714
715 result = test.minus((ReadablePeriod) null);
716 assertSame(test, result);
717 }
718
719
720 public void testToDateTime_RI() {
721 Partial base = createHourMinPartial(COPTIC_PARIS);
722 DateTime dt = new DateTime(0L);
723 assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString());
724
725 DateTime test = base.toDateTime(dt);
726 check(base, 10, 20);
727 assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString());
728 assertEquals("1970-01-01T10:20:00.000+01:00", test.toString());
729 }
730
731 public void testToDateTime_nullRI() {
732 Partial base = createHourMinPartial(1, 2, ISO_UTC);
733 DateTimeUtils.setCurrentMillisFixed(TEST_TIME2);
734
735 DateTime test = base.toDateTime((ReadableInstant) null);
736 check(base, 1, 2);
737 assertEquals("1970-01-02T01:02:07.008+01:00", test.toString());
738 }
739
740
741 public void testProperty() {
742 Partial test = createHourMinPartial();
743 assertNotNull(test.property(DateTimeFieldType.hourOfDay()));
744 assertNotNull(test.property(DateTimeFieldType.minuteOfHour()));
745 try {
746 test.property(DateTimeFieldType.secondOfDay());
747 fail();
748 } catch (IllegalArgumentException ex) {}
749 try {
750 test.property(null);
751 fail();
752 } catch (IllegalArgumentException ex) {}
753 }
754
755
756 public void testSerialization() throws Exception {
757 Partial test = createHourMinPartial(COPTIC_PARIS);
758
759 ByteArrayOutputStream baos = new ByteArrayOutputStream();
760 ObjectOutputStream oos = new ObjectOutputStream(baos);
761 oos.writeObject(test);
762 byte[] bytes = baos.toByteArray();
763 oos.close();
764
765 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
766 ObjectInputStream ois = new ObjectInputStream(bais);
767 Partial result = (Partial) ois.readObject();
768 ois.close();
769
770 assertEquals(test, result);
771 assertTrue(Arrays.equals(test.getValues(), result.getValues()));
772 assertTrue(Arrays.equals(test.getFields(), result.getFields()));
773 assertEquals(test.getChronology(), result.getChronology());
774 }
775
776
777 public void testGetFormatter1() {
778 Partial test = new Partial(DateTimeFieldType.year(), 2005);
779 assertEquals("2005", test.getFormatter().print(test));
780
781 test = test.with(DateTimeFieldType.monthOfYear(), 6);
782 assertEquals("2005-06", test.getFormatter().print(test));
783
784 test = test.with(DateTimeFieldType.dayOfMonth(), 25);
785 assertEquals("2005-06-25", test.getFormatter().print(test));
786
787 test = test.without(DateTimeFieldType.monthOfYear());
788 assertEquals("2005--25", test.getFormatter().print(test));
789 }
790
791 public void testGetFormatter2() {
792 Partial test = new Partial();
793 assertEquals(null, test.getFormatter());
794
795 test = test.with(DateTimeFieldType.era(), 1);
796 assertEquals(null, test.getFormatter());
797
798 test = test.with(DateTimeFieldType.halfdayOfDay(), 0);
799 assertEquals(null, test.getFormatter());
800 }
801
802 public void testGetFormatter3() {
803 Partial test = new Partial(DateTimeFieldType.dayOfWeek(), 5);
804 assertEquals("-W-5", test.getFormatter().print(test));
805
806
807 test = test.with(DateTimeFieldType.dayOfMonth(), 13);
808 assertEquals("---13", test.getFormatter().print(test));
809 }
810
811
812 public void testToString1() {
813 Partial test = createHourMinPartial();
814 assertEquals("10:20", test.toString());
815 }
816
817 public void testToString2() {
818 Partial test = new Partial();
819 assertEquals("[]", test.toString());
820 }
821
822 public void testToString3() {
823 Partial test = new Partial(DateTimeFieldType.year(), 2005);
824 assertEquals("2005", test.toString());
825
826 test = test.with(DateTimeFieldType.monthOfYear(), 6);
827 assertEquals("2005-06", test.toString());
828
829 test = test.with(DateTimeFieldType.dayOfMonth(), 25);
830 assertEquals("2005-06-25", test.toString());
831
832 test = test.without(DateTimeFieldType.monthOfYear());
833 assertEquals("2005--25", test.toString());
834 }
835
836 public void testToString4() {
837 Partial test = new Partial(DateTimeFieldType.dayOfWeek(), 5);
838 assertEquals("-W-5", test.toString());
839
840 test = test.with(DateTimeFieldType.dayOfMonth(), 13);
841 assertEquals("[dayOfMonth=13, dayOfWeek=5]", test.toString());
842 }
843
844 public void testToString5() {
845 Partial test = new Partial(DateTimeFieldType.era(), 1);
846 assertEquals("[era=1]", test.toString());
847
848 test = test.with(DateTimeFieldType.halfdayOfDay(), 0);
849 assertEquals("[era=1, halfdayOfDay=0]", test.toString());
850 }
851
852
853 public void testToString_String() {
854 Partial test = createHourMinPartial();
855 assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test.toString("yyyy HH"));
856 assertEquals("10:20", test.toString((String) null));
857 }
858
859
860 public void testToString_String_Locale() {
861 Partial test = createHourMinPartial();
862 assertEquals("10 20", test.toString("H m", Locale.ENGLISH));
863 assertEquals("10:20", test.toString(null, Locale.ENGLISH));
864 assertEquals("10 20", test.toString("H m", null));
865 assertEquals("10:20", test.toString(null, null));
866 }
867
868
869 public void testToString_DTFormatter() {
870 Partial test = createHourMinPartial();
871 assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test.toString(DateTimeFormat.forPattern("yyyy HH")));
872 assertEquals("10:20", test.toString((DateTimeFormatter) null));
873 }
874
875
876 private Partial createHourMinPartial() {
877 return createHourMinPartial(ISO_UTC);
878 }
879
880 private Partial createHourMinPartial(Chronology chrono) {
881 return createHourMinPartial(10, 20, chrono);
882 }
883
884 private Partial createHourMinPartial2(Chronology chrono) {
885 return createHourMinPartial(15, 20, chrono);
886 }
887
888 private Partial createHourMinPartial(int hour, int min, Chronology chrono) {
889 return new Partial(
890 new DateTimeFieldType[] {DateTimeFieldType.hourOfDay(), DateTimeFieldType.minuteOfHour()},
891 new int[] {hour, min},
892 chrono);
893 }
894
895 private Partial createTODPartial(Chronology chrono) {
896 return new Partial(
897 new DateTimeFieldType[] {
898 DateTimeFieldType.hourOfDay(), DateTimeFieldType.minuteOfHour(),
899 DateTimeFieldType.secondOfMinute(), DateTimeFieldType.millisOfSecond()},
900 new int[] {10, 20, 30, 40},
901 chrono);
902 }
903
904 private void check(Partial test, int hour, int min) {
905 assertEquals(test.toString(), hour, test.get(DateTimeFieldType.hourOfDay()));
906 assertEquals(test.toString(), min, test.get(DateTimeFieldType.minuteOfHour()));
907 }
908 }