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