001 /*
002 * Copyright 2001-2009 Stephen Colebourne
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.joda.time;
017
018 import java.io.ByteArrayInputStream;
019 import java.io.ByteArrayOutputStream;
020 import java.io.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022 import java.util.Arrays;
023 import java.util.Locale;
024
025 import junit.framework.TestCase;
026 import junit.framework.TestSuite;
027
028 import org.joda.time.chrono.BuddhistChronology;
029 import org.joda.time.chrono.CopticChronology;
030 import org.joda.time.chrono.GregorianChronology;
031 import org.joda.time.format.DateTimeFormat;
032 import org.joda.time.format.DateTimeFormatter;
033
034 /**
035 * This class is a Junit unit test for LocalTime.
036 *
037 * @author Stephen Colebourne
038 */
039 public class TestLocalTime_Basics extends TestCase {
040
041 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
042 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
043 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
044 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS);
045 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON);
046 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO);
047 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC();
048 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON);
049
050 private long TEST_TIME_NOW =
051 10L * DateTimeConstants.MILLIS_PER_HOUR
052 + 20L * DateTimeConstants.MILLIS_PER_MINUTE
053 + 30L * DateTimeConstants.MILLIS_PER_SECOND
054 + 40L;
055
056 // private long TEST_TIME1 =
057 // 1L * DateTimeConstants.MILLIS_PER_HOUR
058 // + 2L * DateTimeConstants.MILLIS_PER_MINUTE
059 // + 3L * DateTimeConstants.MILLIS_PER_SECOND
060 // + 4L;
061
062 private long TEST_TIME2 =
063 1L * DateTimeConstants.MILLIS_PER_DAY
064 + 5L * DateTimeConstants.MILLIS_PER_HOUR
065 + 6L * DateTimeConstants.MILLIS_PER_MINUTE
066 + 7L * DateTimeConstants.MILLIS_PER_SECOND
067 + 8L;
068
069 private DateTimeZone zone = null;
070
071 public static void main(String[] args) {
072 junit.textui.TestRunner.run(suite());
073 }
074
075 public static TestSuite suite() {
076 return new TestSuite(TestLocalTime_Basics.class);
077 }
078
079 public TestLocalTime_Basics(String name) {
080 super(name);
081 }
082
083 protected void setUp() throws Exception {
084 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
085 zone = DateTimeZone.getDefault();
086 DateTimeZone.setDefault(LONDON);
087 }
088
089 protected void tearDown() throws Exception {
090 DateTimeUtils.setCurrentMillisSystem();
091 DateTimeZone.setDefault(zone);
092 zone = null;
093 }
094
095 //-----------------------------------------------------------------------
096 public void testGet_DateTimeFieldType() {
097 LocalTime test = new LocalTime(10, 20, 30, 40);
098 assertEquals(10, test.get(DateTimeFieldType.hourOfDay()));
099 assertEquals(20, test.get(DateTimeFieldType.minuteOfHour()));
100 assertEquals(30, test.get(DateTimeFieldType.secondOfMinute()));
101 assertEquals(40, test.get(DateTimeFieldType.millisOfSecond()));
102 assertEquals(TEST_TIME_NOW / 60000 , test.get(DateTimeFieldType.minuteOfDay()));
103 assertEquals(TEST_TIME_NOW / 1000 , test.get(DateTimeFieldType.secondOfDay()));
104 assertEquals(TEST_TIME_NOW , test.get(DateTimeFieldType.millisOfDay()));
105 assertEquals(10, test.get(DateTimeFieldType.hourOfHalfday()));
106 assertEquals(DateTimeConstants.AM, test.get(DateTimeFieldType.halfdayOfDay()));
107 test = new LocalTime(12, 30);
108 assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday()));
109 assertEquals(12, test.get(DateTimeFieldType.clockhourOfHalfday()));
110 assertEquals(12, test.get(DateTimeFieldType.clockhourOfDay()));
111 assertEquals(DateTimeConstants.PM, test.get(DateTimeFieldType.halfdayOfDay()));
112 test = new LocalTime(14, 30);
113 assertEquals(2, test.get(DateTimeFieldType.hourOfHalfday()));
114 assertEquals(2, test.get(DateTimeFieldType.clockhourOfHalfday()));
115 assertEquals(14, test.get(DateTimeFieldType.clockhourOfDay()));
116 assertEquals(DateTimeConstants.PM, test.get(DateTimeFieldType.halfdayOfDay()));
117 test = new LocalTime(0, 30);
118 assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday()));
119 assertEquals(12, test.get(DateTimeFieldType.clockhourOfHalfday()));
120 assertEquals(24, test.get(DateTimeFieldType.clockhourOfDay()));
121 assertEquals(DateTimeConstants.AM, test.get(DateTimeFieldType.halfdayOfDay()));
122 try {
123 test.get(null);
124 fail();
125 } catch (IllegalArgumentException ex) {}
126 try {
127 test.get(DateTimeFieldType.dayOfMonth());
128 fail();
129 } catch (IllegalArgumentException ex) {}
130 }
131
132 public void testSize() {
133 LocalTime test = new LocalTime(10, 20, 30, 40);
134 assertEquals(4, test.size());
135 }
136
137 public void testGetFieldType_int() {
138 LocalTime test = new LocalTime(10, 20, 30, 40);
139 assertSame(DateTimeFieldType.hourOfDay(), test.getFieldType(0));
140 assertSame(DateTimeFieldType.minuteOfHour(), test.getFieldType(1));
141 assertSame(DateTimeFieldType.secondOfMinute(), test.getFieldType(2));
142 assertSame(DateTimeFieldType.millisOfSecond(), test.getFieldType(3));
143 try {
144 test.getFieldType(-1);
145 } catch (IndexOutOfBoundsException ex) {}
146 try {
147 test.getFieldType(5);
148 } catch (IndexOutOfBoundsException ex) {}
149 }
150
151 public void testGetFieldTypes() {
152 LocalTime test = new LocalTime(10, 20, 30, 40);
153 DateTimeFieldType[] fields = test.getFieldTypes();
154 assertSame(DateTimeFieldType.hourOfDay(), fields[0]);
155 assertSame(DateTimeFieldType.minuteOfHour(), fields[1]);
156 assertSame(DateTimeFieldType.secondOfMinute(), fields[2]);
157 assertSame(DateTimeFieldType.millisOfSecond(), fields[3]);
158 assertNotSame(test.getFieldTypes(), test.getFieldTypes());
159 }
160
161 public void testGetField_int() {
162 LocalTime test = new LocalTime(10, 20, 30, 40, COPTIC_UTC);
163 assertSame(COPTIC_UTC.hourOfDay(), test.getField(0));
164 assertSame(COPTIC_UTC.minuteOfHour(), test.getField(1));
165 assertSame(COPTIC_UTC.secondOfMinute(), test.getField(2));
166 assertSame(COPTIC_UTC.millisOfSecond(), test.getField(3));
167 try {
168 test.getField(-1);
169 } catch (IndexOutOfBoundsException ex) {}
170 try {
171 test.getField(5);
172 } catch (IndexOutOfBoundsException ex) {}
173 }
174
175 public void testGetFields() {
176 LocalTime test = new LocalTime(10, 20, 30, 40, COPTIC_UTC);
177 DateTimeField[] fields = test.getFields();
178 assertSame(COPTIC_UTC.hourOfDay(), fields[0]);
179 assertSame(COPTIC_UTC.minuteOfHour(), fields[1]);
180 assertSame(COPTIC_UTC.secondOfMinute(), fields[2]);
181 assertSame(COPTIC_UTC.millisOfSecond(), fields[3]);
182 assertNotSame(test.getFields(), test.getFields());
183 }
184
185 public void testGetValue_int() {
186 LocalTime test = new LocalTime(10, 20, 30, 40, COPTIC_PARIS);
187 assertEquals(10, test.getValue(0));
188 assertEquals(20, test.getValue(1));
189 assertEquals(30, test.getValue(2));
190 assertEquals(40, test.getValue(3));
191 try {
192 test.getValue(-1);
193 } catch (IndexOutOfBoundsException ex) {}
194 try {
195 test.getValue(5);
196 } catch (IndexOutOfBoundsException ex) {}
197 }
198
199 public void testGetValues() {
200 LocalTime test = new LocalTime(10, 20, 30, 40, COPTIC_UTC);
201 int[] values = test.getValues();
202 assertEquals(10, values[0]);
203 assertEquals(20, values[1]);
204 assertEquals(30, values[2]);
205 assertEquals(40, values[3]);
206 assertNotSame(test.getValues(), test.getValues());
207 }
208
209 public void testIsSupported_DateTimeFieldType() {
210 LocalTime test = new LocalTime(10, 20, 30, 40);
211 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfDay()));
212 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfHour()));
213 assertEquals(true, test.isSupported(DateTimeFieldType.secondOfMinute()));
214 assertEquals(true, test.isSupported(DateTimeFieldType.millisOfSecond()));
215 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfDay()));
216 assertEquals(true, test.isSupported(DateTimeFieldType.secondOfDay()));
217 assertEquals(true, test.isSupported(DateTimeFieldType.millisOfDay()));
218
219 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfHalfday()));
220 assertEquals(true, test.isSupported(DateTimeFieldType.halfdayOfDay()));
221 assertEquals(true, test.isSupported(DateTimeFieldType.clockhourOfHalfday()));
222 assertEquals(true, test.isSupported(DateTimeFieldType.clockhourOfDay()));
223
224 assertEquals(false, test.isSupported(DateTimeFieldType.dayOfMonth()));
225 assertEquals(false, test.isSupported((DateTimeFieldType) null));
226
227 DateTimeFieldType d = new DateTimeFieldType("hours") {
228 public DurationFieldType getDurationType() {
229 return DurationFieldType.hours();
230 }
231 public DurationFieldType getRangeDurationType() {
232 return null;
233 }
234 public DateTimeField getField(Chronology chronology) {
235 return chronology.hourOfDay();
236 }
237 };
238 assertEquals(false, test.isSupported(d));
239
240 d = new DateTimeFieldType("hourOfYear") {
241 public DurationFieldType getDurationType() {
242 return DurationFieldType.hours();
243 }
244 public DurationFieldType getRangeDurationType() {
245 return DurationFieldType.years();
246 }
247 public DateTimeField getField(Chronology chronology) {
248 return chronology.hourOfDay();
249 }
250 };
251 assertEquals(false, test.isSupported(d));
252 }
253
254 public void testIsSupported_DurationFieldType() {
255 LocalTime test = new LocalTime(10, 20, 30, 40);
256 assertEquals(true, test.isSupported(DurationFieldType.hours()));
257 assertEquals(true, test.isSupported(DurationFieldType.minutes()));
258 assertEquals(true, test.isSupported(DurationFieldType.seconds()));
259 assertEquals(true, test.isSupported(DurationFieldType.millis()));
260 assertEquals(true, test.isSupported(DurationFieldType.halfdays()));
261
262 assertEquals(false, test.isSupported(DurationFieldType.days()));
263 assertEquals(false, test.isSupported((DurationFieldType) null));
264 }
265
266 public void testEqualsHashCode() {
267 LocalTime test1 = new LocalTime(10, 20, 30, 40, COPTIC_PARIS);
268 LocalTime test2 = new LocalTime(10, 20, 30, 40, COPTIC_PARIS);
269 assertEquals(true, test1.equals(test2));
270 assertEquals(true, test2.equals(test1));
271 assertEquals(true, test1.equals(test1));
272 assertEquals(true, test2.equals(test2));
273 assertEquals(true, test1.hashCode() == test2.hashCode());
274 assertEquals(true, test1.hashCode() == test1.hashCode());
275 assertEquals(true, test2.hashCode() == test2.hashCode());
276
277 LocalTime test3 = new LocalTime(15, 20, 30, 40);
278 assertEquals(false, test1.equals(test3));
279 assertEquals(false, test2.equals(test3));
280 assertEquals(false, test3.equals(test1));
281 assertEquals(false, test3.equals(test2));
282 assertEquals(false, test1.hashCode() == test3.hashCode());
283 assertEquals(false, test2.hashCode() == test3.hashCode());
284
285 assertEquals(false, test1.equals("Hello"));
286 assertEquals(true, test1.equals(new TimeOfDay(10, 20, 30, 40, COPTIC_UTC)));
287 assertEquals(true, test1.hashCode() == new TimeOfDay(10, 20, 30, 40, COPTIC_UTC).hashCode());
288 assertEquals(true, test1.equals(new MockInstant()));
289 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE));
290 }
291
292 class MockInstant extends MockPartial {
293 public Chronology getChronology() {
294 return COPTIC_UTC;
295 }
296 public DateTimeField[] getFields() {
297 return new DateTimeField[] {
298 COPTIC_UTC.hourOfDay(),
299 COPTIC_UTC.minuteOfHour(),
300 COPTIC_UTC.secondOfMinute(),
301 COPTIC_UTC.millisOfSecond(),
302 };
303 }
304 public int[] getValues() {
305 return new int[] {10, 20, 30, 40};
306 }
307 }
308
309 //-----------------------------------------------------------------------
310 public void testCompareTo() {
311 LocalTime test1 = new LocalTime(10, 20, 30, 40);
312 LocalTime test1a = new LocalTime(10, 20, 30, 40);
313 assertEquals(0, test1.compareTo(test1a));
314 assertEquals(0, test1a.compareTo(test1));
315 assertEquals(0, test1.compareTo(test1));
316 assertEquals(0, test1a.compareTo(test1a));
317
318 LocalTime test2 = new LocalTime(10, 20, 35, 40);
319 assertEquals(-1, test1.compareTo(test2));
320 assertEquals(+1, test2.compareTo(test1));
321
322 LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC());
323 assertEquals(-1, test1.compareTo(test3));
324 assertEquals(+1, test3.compareTo(test1));
325 assertEquals(0, test3.compareTo(test2));
326
327 DateTimeFieldType[] types = new DateTimeFieldType[] {
328 DateTimeFieldType.hourOfDay(),
329 DateTimeFieldType.minuteOfHour(),
330 DateTimeFieldType.secondOfMinute(),
331 DateTimeFieldType.millisOfSecond(),
332 };
333 int[] values = new int[] {10, 20, 30, 40};
334 Partial p = new Partial(types, values);
335 assertEquals(0, test1.compareTo(p));
336 assertEquals(0, test1.compareTo(new TimeOfDay(10, 20, 30, 40)));
337 try {
338 test1.compareTo(null);
339 fail();
340 } catch (NullPointerException ex) {}
341 // try {
342 // test1.compareTo(new Date());
343 // fail();
344 // } catch (ClassCastException ex) {}
345 }
346
347 //-----------------------------------------------------------------------
348 public void testIsEqual_LocalTime() {
349 LocalTime test1 = new LocalTime(10, 20, 30, 40);
350 LocalTime test1a = new LocalTime(10, 20, 30, 40);
351 assertEquals(true, test1.isEqual(test1a));
352 assertEquals(true, test1a.isEqual(test1));
353 assertEquals(true, test1.isEqual(test1));
354 assertEquals(true, test1a.isEqual(test1a));
355
356 LocalTime test2 = new LocalTime(10, 20, 35, 40);
357 assertEquals(false, test1.isEqual(test2));
358 assertEquals(false, test2.isEqual(test1));
359
360 LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC());
361 assertEquals(false, test1.isEqual(test3));
362 assertEquals(false, test3.isEqual(test1));
363 assertEquals(true, test3.isEqual(test2));
364
365 try {
366 new LocalTime(10, 20, 35, 40).isEqual(null);
367 fail();
368 } catch (IllegalArgumentException ex) {}
369 }
370
371 //-----------------------------------------------------------------------
372 public void testIsBefore_LocalTime() {
373 LocalTime test1 = new LocalTime(10, 20, 30, 40);
374 LocalTime test1a = new LocalTime(10, 20, 30, 40);
375 assertEquals(false, test1.isBefore(test1a));
376 assertEquals(false, test1a.isBefore(test1));
377 assertEquals(false, test1.isBefore(test1));
378 assertEquals(false, test1a.isBefore(test1a));
379
380 LocalTime test2 = new LocalTime(10, 20, 35, 40);
381 assertEquals(true, test1.isBefore(test2));
382 assertEquals(false, test2.isBefore(test1));
383
384 LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC());
385 assertEquals(true, test1.isBefore(test3));
386 assertEquals(false, test3.isBefore(test1));
387 assertEquals(false, test3.isBefore(test2));
388
389 try {
390 new LocalTime(10, 20, 35, 40).isBefore(null);
391 fail();
392 } catch (IllegalArgumentException ex) {}
393 }
394
395 //-----------------------------------------------------------------------
396 public void testIsAfter_LocalTime() {
397 LocalTime test1 = new LocalTime(10, 20, 30, 40);
398 LocalTime test1a = new LocalTime(10, 20, 30, 40);
399 assertEquals(false, test1.isAfter(test1a));
400 assertEquals(false, test1a.isAfter(test1));
401 assertEquals(false, test1.isAfter(test1));
402 assertEquals(false, test1a.isAfter(test1a));
403
404 LocalTime test2 = new LocalTime(10, 20, 35, 40);
405 assertEquals(false, test1.isAfter(test2));
406 assertEquals(true, test2.isAfter(test1));
407
408 LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC());
409 assertEquals(false, test1.isAfter(test3));
410 assertEquals(true, test3.isAfter(test1));
411 assertEquals(false, test3.isAfter(test2));
412
413 try {
414 new LocalTime(10, 20, 35, 40).isAfter(null);
415 fail();
416 } catch (IllegalArgumentException ex) {}
417 }
418
419 //-----------------------------------------------------------------------
420 public void testWithField_DateTimeFieldType_int_1() {
421 LocalTime test = new LocalTime(10, 20, 30, 40);
422 LocalTime result = test.withField(DateTimeFieldType.hourOfDay(), 15);
423
424 assertEquals(new LocalTime(10, 20, 30, 40), test);
425 assertEquals(new LocalTime(15, 20, 30, 40), result);
426 }
427
428 public void testWithField_DateTimeFieldType_int_2() {
429 LocalTime test = new LocalTime(10, 20, 30, 40);
430 try {
431 test.withField(null, 6);
432 fail();
433 } catch (IllegalArgumentException ex) {}
434 }
435
436 public void testWithField_DateTimeFieldType_int_3() {
437 LocalTime test = new LocalTime(10, 20, 30, 40);
438 try {
439 test.withField(DateTimeFieldType.dayOfMonth(), 6);
440 fail();
441 } catch (IllegalArgumentException ex) {}
442 }
443
444 public void testWithField_DateTimeFieldType_int_4() {
445 LocalTime test = new LocalTime(10, 20, 30, 40);
446 LocalTime result = test.withField(DateTimeFieldType.hourOfDay(), 10);
447 assertSame(test, result);
448 }
449
450 //-----------------------------------------------------------------------
451 public void testWithFieldAdded_DurationFieldType_int_1() {
452 LocalTime test = new LocalTime(10, 20, 30, 40);
453 LocalTime result = test.withFieldAdded(DurationFieldType.hours(), 6);
454
455 assertEquals(new LocalTime(10, 20, 30, 40), test);
456 assertEquals(new LocalTime(16, 20, 30, 40), result);
457 }
458
459 public void testWithFieldAdded_DurationFieldType_int_2() {
460 LocalTime test = new LocalTime(10, 20, 30, 40);
461 try {
462 test.withFieldAdded(null, 0);
463 fail();
464 } catch (IllegalArgumentException ex) {}
465 }
466
467 public void testWithFieldAdded_DurationFieldType_int_3() {
468 LocalTime test = new LocalTime(10, 20, 30, 40);
469 try {
470 test.withFieldAdded(null, 6);
471 fail();
472 } catch (IllegalArgumentException ex) {}
473 }
474
475 public void testWithFieldAdded_DurationFieldType_int_4() {
476 LocalTime test = new LocalTime(10, 20, 30, 40);
477 LocalTime result = test.withFieldAdded(DurationFieldType.hours(), 0);
478 assertSame(test, result);
479 }
480
481 public void testWithFieldAdded_DurationFieldType_int_5() {
482 LocalTime test = new LocalTime(10, 20, 30, 40);
483 try {
484 test.withFieldAdded(DurationFieldType.days(), 6);
485 fail();
486 } catch (IllegalArgumentException ex) {}
487 }
488
489 public void testWithFieldAdded_DurationFieldType_int_6() {
490 LocalTime test = new LocalTime(10, 20, 30, 40);
491 LocalTime result = test.withFieldAdded(DurationFieldType.hours(), 16);
492
493 assertEquals(new LocalTime(10, 20, 30, 40), test);
494 assertEquals(new LocalTime(2, 20, 30, 40), result);
495 }
496
497 public void testWithFieldAdded_DurationFieldType_int_7() {
498 LocalTime test = new LocalTime(23, 59, 59, 999);
499 LocalTime result = test.withFieldAdded(DurationFieldType.millis(), 1);
500 assertEquals(new LocalTime(0, 0, 0, 0), result);
501
502 test = new LocalTime(23, 59, 59, 999);
503 result = test.withFieldAdded(DurationFieldType.seconds(), 1);
504 assertEquals(new LocalTime(0, 0, 0, 999), result);
505
506 test = new LocalTime(23, 59, 59, 999);
507 result = test.withFieldAdded(DurationFieldType.minutes(), 1);
508 assertEquals(new LocalTime(0, 0, 59, 999), result);
509
510 test = new LocalTime(23, 59, 59, 999);
511 result = test.withFieldAdded(DurationFieldType.hours(), 1);
512 assertEquals(new LocalTime(0, 59, 59, 999), result);
513 }
514
515 public void testWithFieldAdded_DurationFieldType_int_8() {
516 LocalTime test = new LocalTime(0, 0, 0, 0);
517 LocalTime result = test.withFieldAdded(DurationFieldType.millis(), -1);
518 assertEquals(new LocalTime(23, 59, 59, 999), result);
519
520 test = new LocalTime(0, 0, 0, 0);
521 result = test.withFieldAdded(DurationFieldType.seconds(), -1);
522 assertEquals(new LocalTime(23, 59, 59, 0), result);
523
524 test = new LocalTime(0, 0, 0, 0);
525 result = test.withFieldAdded(DurationFieldType.minutes(), -1);
526 assertEquals(new LocalTime(23, 59, 0, 0), result);
527
528 test = new LocalTime(0, 0, 0, 0);
529 result = test.withFieldAdded(DurationFieldType.hours(), -1);
530 assertEquals(new LocalTime(23, 0, 0, 0), result);
531 }
532
533 //-----------------------------------------------------------------------
534 public void testPlus_RP() {
535 LocalTime test = new LocalTime(10, 20, 30, 40, BUDDHIST_LONDON);
536 LocalTime result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7, 8));
537 LocalTime expected = new LocalTime(15, 26, 37, 48, BUDDHIST_LONDON);
538 assertEquals(expected, result);
539
540 result = test.plus((ReadablePeriod) null);
541 assertSame(test, result);
542 }
543
544 public void testPlusHours_int() {
545 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON);
546 LocalTime result = test.plusHours(1);
547 LocalTime expected = new LocalTime(2, 2, 3, 4, BUDDHIST_LONDON);
548 assertEquals(expected, result);
549
550 result = test.plusHours(0);
551 assertSame(test, result);
552 }
553
554 public void testPlusMinutes_int() {
555 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON);
556 LocalTime result = test.plusMinutes(1);
557 LocalTime expected = new LocalTime(1, 3, 3, 4, BUDDHIST_LONDON);
558 assertEquals(expected, result);
559
560 result = test.plusMinutes(0);
561 assertSame(test, result);
562 }
563
564 public void testPlusSeconds_int() {
565 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON);
566 LocalTime result = test.plusSeconds(1);
567 LocalTime expected = new LocalTime(1, 2, 4, 4, BUDDHIST_LONDON);
568 assertEquals(expected, result);
569
570 result = test.plusSeconds(0);
571 assertSame(test, result);
572 }
573
574 public void testPlusMillis_int() {
575 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON);
576 LocalTime result = test.plusMillis(1);
577 LocalTime expected = new LocalTime(1, 2, 3, 5, BUDDHIST_LONDON);
578 assertEquals(expected, result);
579
580 result = test.plusMillis(0);
581 assertSame(test, result);
582 }
583
584 //-----------------------------------------------------------------------
585 public void testMinus_RP() {
586 LocalTime test = new LocalTime(10, 20, 30, 40, BUDDHIST_LONDON);
587 LocalTime result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1));
588 LocalTime expected = new LocalTime(9, 19, 29, 39, BUDDHIST_LONDON);
589 assertEquals(expected, result);
590
591 result = test.minus((ReadablePeriod) null);
592 assertSame(test, result);
593 }
594
595 public void testMinusHours_int() {
596 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON);
597 LocalTime result = test.minusHours(1);
598 LocalTime expected = new LocalTime(0, 2, 3, 4, BUDDHIST_LONDON);
599 assertEquals(expected, result);
600
601 result = test.minusHours(0);
602 assertSame(test, result);
603 }
604
605 public void testMinusMinutes_int() {
606 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON);
607 LocalTime result = test.minusMinutes(1);
608 LocalTime expected = new LocalTime(1, 1, 3, 4, BUDDHIST_LONDON);
609 assertEquals(expected, result);
610
611 result = test.minusMinutes(0);
612 assertSame(test, result);
613 }
614
615 public void testMinusSeconds_int() {
616 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON);
617 LocalTime result = test.minusSeconds(1);
618 LocalTime expected = new LocalTime(1, 2, 2, 4, BUDDHIST_LONDON);
619 assertEquals(expected, result);
620
621 result = test.minusSeconds(0);
622 assertSame(test, result);
623 }
624
625 public void testMinusMillis_int() {
626 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON);
627 LocalTime result = test.minusMillis(1);
628 LocalTime expected = new LocalTime(1, 2, 3, 3, BUDDHIST_LONDON);
629 assertEquals(expected, result);
630
631 result = test.minusMillis(0);
632 assertSame(test, result);
633 }
634
635 //-----------------------------------------------------------------------
636 public void testGetters() {
637 LocalTime test = new LocalTime(10, 20, 30, 40);
638 assertEquals(10, test.getHourOfDay());
639 assertEquals(20, test.getMinuteOfHour());
640 assertEquals(30, test.getSecondOfMinute());
641 assertEquals(40, test.getMillisOfSecond());
642 assertEquals(TEST_TIME_NOW, test.getMillisOfDay());
643 }
644
645 //-----------------------------------------------------------------------
646 public void testWithers() {
647 LocalTime test = new LocalTime(10, 20, 30, 40);
648 check(test.withHourOfDay(6), 6, 20, 30, 40);
649 check(test.withMinuteOfHour(6), 10, 6, 30, 40);
650 check(test.withSecondOfMinute(6), 10, 20, 6, 40);
651 check(test.withMillisOfSecond(6), 10, 20, 30, 6);
652 check(test.withMillisOfDay(61234), 0, 1, 1, 234);
653 try {
654 test.withHourOfDay(-1);
655 fail();
656 } catch (IllegalArgumentException ex) {}
657 try {
658 test.withHourOfDay(24);
659 fail();
660 } catch (IllegalArgumentException ex) {}
661 }
662
663 //-----------------------------------------------------------------------
664 public void testToDateTimeTodayDefaultZone() {
665 LocalTime base = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
666 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
667 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
668
669 DateTime test = base.toDateTimeToday();
670 check(base, 10, 20, 30, 40);
671 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON);
672 expected = expected.hourOfDay().setCopy(10);
673 expected = expected.minuteOfHour().setCopy(20);
674 expected = expected.secondOfMinute().setCopy(30);
675 expected = expected.millisOfSecond().setCopy(40);
676 assertEquals(expected, test);
677 }
678
679 //-----------------------------------------------------------------------
680 public void testToDateTimeToday_Zone() {
681 LocalTime base = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
682 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
683 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
684
685 DateTime test = base.toDateTimeToday(TOKYO);
686 check(base, 10, 20, 30, 40);
687 DateTime expected = new DateTime(dt.getMillis(), COPTIC_TOKYO);
688 expected = expected.hourOfDay().setCopy(10);
689 expected = expected.minuteOfHour().setCopy(20);
690 expected = expected.secondOfMinute().setCopy(30);
691 expected = expected.millisOfSecond().setCopy(40);
692 assertEquals(expected, test);
693 }
694
695 public void testToDateTimeToday_nullZone() {
696 LocalTime base = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
697 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
698 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
699
700 DateTime test = base.toDateTimeToday((DateTimeZone) null);
701 check(base, 10, 20, 30, 40);
702 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON);
703 expected = expected.hourOfDay().setCopy(10);
704 expected = expected.minuteOfHour().setCopy(20);
705 expected = expected.secondOfMinute().setCopy(30);
706 expected = expected.millisOfSecond().setCopy(40);
707 assertEquals(expected, test);
708 }
709
710 //-----------------------------------------------------------------------
711 public void testToDateTime_RI() {
712 LocalTime base = new LocalTime(10, 20, 30, 40, COPTIC_PARIS);
713 DateTime dt = new DateTime(0L); // LONDON zone
714 assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString());
715
716 DateTime test = base.toDateTime(dt);
717 check(base, 10, 20, 30, 40);
718 assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString());
719 assertEquals("1970-01-01T10:20:30.040+01:00", test.toString());
720 }
721
722 public void testToDateTime_nullRI() {
723 LocalTime base = new LocalTime(1, 2, 3, 4);
724 DateTimeUtils.setCurrentMillisFixed(TEST_TIME2);
725
726 DateTime test = base.toDateTime((ReadableInstant) null);
727 check(base, 1, 2, 3, 4);
728 assertEquals("1970-01-02T01:02:03.004+01:00", test.toString());
729 }
730
731 //-----------------------------------------------------------------------
732 public void testProperty() {
733 LocalTime test = new LocalTime(10, 20, 30, 40);
734 assertEquals(test.hourOfDay(), test.property(DateTimeFieldType.hourOfDay()));
735 assertEquals(test.minuteOfHour(), test.property(DateTimeFieldType.minuteOfHour()));
736 assertEquals(test.secondOfMinute(), test.property(DateTimeFieldType.secondOfMinute()));
737 assertEquals(test.millisOfSecond(), test.property(DateTimeFieldType.millisOfSecond()));
738 assertEquals(test.millisOfDay(), test.property(DateTimeFieldType.millisOfDay()));
739
740 assertEquals(test, test.property(DateTimeFieldType.minuteOfDay()).getLocalTime());
741 assertEquals(test, test.property(DateTimeFieldType.secondOfDay()).getLocalTime());
742 assertEquals(test, test.property(DateTimeFieldType.millisOfDay()).getLocalTime());
743 assertEquals(test, test.property(DateTimeFieldType.hourOfHalfday()).getLocalTime());
744 assertEquals(test, test.property(DateTimeFieldType.halfdayOfDay()).getLocalTime());
745 assertEquals(test, test.property(DateTimeFieldType.clockhourOfHalfday()).getLocalTime());
746 assertEquals(test, test.property(DateTimeFieldType.clockhourOfDay()).getLocalTime());
747
748 try {
749 test.property(DateTimeFieldType.dayOfWeek());
750 fail();
751 } catch (IllegalArgumentException ex) {}
752 try {
753 test.property(null);
754 fail();
755 } catch (IllegalArgumentException ex) {}
756 }
757
758 //-----------------------------------------------------------------------
759 public void testSerialization() throws Exception {
760 LocalTime test = new LocalTime(10, 20, 30, 40, COPTIC_PARIS);
761
762 ByteArrayOutputStream baos = new ByteArrayOutputStream();
763 ObjectOutputStream oos = new ObjectOutputStream(baos);
764 oos.writeObject(test);
765 byte[] bytes = baos.toByteArray();
766 oos.close();
767
768 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
769 ObjectInputStream ois = new ObjectInputStream(bais);
770 LocalTime result = (LocalTime) ois.readObject();
771 ois.close();
772
773 assertEquals(test, result);
774 assertTrue(Arrays.equals(test.getValues(), result.getValues()));
775 assertTrue(Arrays.equals(test.getFields(), result.getFields()));
776 assertEquals(test.getChronology(), result.getChronology());
777 }
778
779 //-----------------------------------------------------------------------
780 public void testToString() {
781 LocalTime test = new LocalTime(10, 20, 30, 40);
782 assertEquals("10:20:30.040", test.toString());
783 }
784
785 //-----------------------------------------------------------------------
786 public void testToString_String() {
787 LocalTime test = new LocalTime(10, 20, 30, 40);
788 assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test.toString("yyyy HH"));
789 assertEquals("10:20:30.040", test.toString((String) null));
790 }
791
792 //-----------------------------------------------------------------------
793 public void testToString_String_Locale() {
794 LocalTime test = new LocalTime(10, 20, 30, 40);
795 assertEquals("10 20", test.toString("H m", Locale.ENGLISH));
796 assertEquals("10:20:30.040", test.toString(null, Locale.ENGLISH));
797 assertEquals("10 20", test.toString("H m", null));
798 assertEquals("10:20:30.040", test.toString(null, null));
799 }
800
801 //-----------------------------------------------------------------------
802 public void testToString_DTFormatter() {
803 LocalTime test = new LocalTime(10, 20, 30, 40);
804 assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test.toString(DateTimeFormat.forPattern("yyyy HH")));
805 assertEquals("10:20:30.040", test.toString((DateTimeFormatter) null));
806 }
807
808 //-----------------------------------------------------------------------
809 private void check(LocalTime test, int hour, int min, int sec, int milli) {
810 assertEquals(hour, test.getHourOfDay());
811 assertEquals(min, test.getMinuteOfHour());
812 assertEquals(sec, test.getSecondOfMinute());
813 assertEquals(milli, test.getMillisOfSecond());
814 }
815 }