001 /*
002 * Copyright 2001-2006 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.util.Calendar;
019 import java.util.Date;
020 import java.util.GregorianCalendar;
021
022 import junit.framework.TestCase;
023 import junit.framework.TestSuite;
024
025 import org.joda.time.chrono.BuddhistChronology;
026 import org.joda.time.chrono.GJChronology;
027 import org.joda.time.chrono.ISOChronology;
028 import org.joda.time.chrono.JulianChronology;
029 import org.joda.time.format.DateTimeFormat;
030 import org.joda.time.format.DateTimeFormatter;
031
032 /**
033 * This class is a Junit unit test for LocalTime.
034 *
035 * @author Stephen Colebourne
036 */
037 public class TestLocalTime_Constructors extends TestCase {
038
039 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
040 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
041 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
042 private static final DateTimeZone NEW_YORK = DateTimeZone.forID("America/New_York");
043 private static final ISOChronology ISO_UTC = ISOChronology.getInstanceUTC();
044 private static final JulianChronology JULIAN_LONDON = JulianChronology.getInstance(LONDON);
045 private static final JulianChronology JULIAN_PARIS = JulianChronology.getInstance(PARIS);
046 private static final JulianChronology JULIAN_UTC = JulianChronology.getInstanceUTC();
047 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC();
048 private static final int OFFSET_LONDON = LONDON.getOffset(0L) / DateTimeConstants.MILLIS_PER_HOUR;
049 private static final int OFFSET_PARIS = PARIS.getOffset(0L) / DateTimeConstants.MILLIS_PER_HOUR;
050
051 private long TEST_TIME_NOW =
052 10L * DateTimeConstants.MILLIS_PER_HOUR
053 + 20L * DateTimeConstants.MILLIS_PER_MINUTE
054 + 30L * DateTimeConstants.MILLIS_PER_SECOND
055 + 40L;
056
057 private long TEST_TIME1 =
058 1L * DateTimeConstants.MILLIS_PER_HOUR
059 + 2L * DateTimeConstants.MILLIS_PER_MINUTE
060 + 3L * DateTimeConstants.MILLIS_PER_SECOND
061 + 4L;
062
063 private long TEST_TIME2 =
064 1L * DateTimeConstants.MILLIS_PER_DAY
065 + 5L * DateTimeConstants.MILLIS_PER_HOUR
066 + 6L * DateTimeConstants.MILLIS_PER_MINUTE
067 + 7L * DateTimeConstants.MILLIS_PER_SECOND
068 + 8L;
069
070 private DateTimeZone zone = null;
071
072 public static void main(String[] args) {
073 junit.textui.TestRunner.run(suite());
074 }
075
076 public static TestSuite suite() {
077 return new TestSuite(TestLocalTime_Constructors.class);
078 }
079
080 public TestLocalTime_Constructors(String name) {
081 super(name);
082 }
083
084 protected void setUp() throws Exception {
085 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
086 zone = DateTimeZone.getDefault();
087 DateTimeZone.setDefault(LONDON);
088 java.util.TimeZone.setDefault(LONDON.toTimeZone());
089 }
090
091 protected void tearDown() throws Exception {
092 DateTimeUtils.setCurrentMillisSystem();
093 DateTimeZone.setDefault(zone);
094 java.util.TimeZone.setDefault(zone.toTimeZone());
095 zone = null;
096 }
097
098 //-----------------------------------------------------------------------
099 /**
100 * Test constructor ()
101 */
102 public void testConstantMidnight() throws Throwable {
103 LocalTime test = LocalTime.MIDNIGHT;
104 assertEquals(ISO_UTC, test.getChronology());
105 assertEquals(0, test.getHourOfDay());
106 assertEquals(0, test.getMinuteOfHour());
107 assertEquals(0, test.getSecondOfMinute());
108 assertEquals(0, test.getMillisOfSecond());
109 }
110
111 //-----------------------------------------------------------------------
112 public void testParse_noFormatter() throws Throwable {
113 assertEquals(new LocalTime(1, 20), LocalTime.parse("01:20"));
114 assertEquals(new LocalTime(14, 50, 30, 432), LocalTime.parse("14:50:30.432"));
115 }
116
117 public void testParse_formatter() throws Throwable {
118 DateTimeFormatter f = DateTimeFormat.forPattern("HH mm").withChronology(ISOChronology.getInstance(PARIS));
119 assertEquals(new LocalTime(13, 30), LocalTime.parse("13 30", f));
120 }
121
122 //-----------------------------------------------------------------------
123 public void testFactory_FromCalendarFields_Calendar() throws Exception {
124 GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5, 6);
125 cal.set(Calendar.MILLISECOND, 7);
126 LocalTime expected = new LocalTime(4, 5, 6, 7);
127 assertEquals(expected, LocalTime.fromCalendarFields(cal));
128 try {
129 LocalTime.fromCalendarFields((Calendar) null);
130 fail();
131 } catch (IllegalArgumentException ex) {}
132 }
133
134 //-----------------------------------------------------------------------
135 public void testFactory_FromDateFields_after1970() throws Exception {
136 GregorianCalendar cal = new GregorianCalendar(1970, 1, 3, 4, 5, 6);
137 cal.set(Calendar.MILLISECOND, 7);
138 LocalTime expected = new LocalTime(4, 5, 6, 7);
139 assertEquals(expected, LocalTime.fromDateFields(cal.getTime()));
140 }
141
142 public void testFactory_FromDateFields_before1970() throws Exception {
143 GregorianCalendar cal = new GregorianCalendar(1969, 1, 3, 4, 5, 6);
144 cal.set(Calendar.MILLISECOND, 7);
145 LocalTime expected = new LocalTime(4, 5, 6, 7);
146 assertEquals(expected, LocalTime.fromDateFields(cal.getTime()));
147 }
148
149 public void testFactory_FromDateFields_null() throws Exception {
150 try {
151 LocalTime.fromDateFields((Date) null);
152 fail();
153 } catch (IllegalArgumentException ex) {}
154 }
155
156 //-----------------------------------------------------------------------
157 public void testFactoryMillisOfDay_long() throws Throwable {
158 LocalTime test = LocalTime.fromMillisOfDay(TEST_TIME1);
159 assertEquals(ISO_UTC, test.getChronology());
160 assertEquals(1, test.getHourOfDay());
161 assertEquals(2, test.getMinuteOfHour());
162 assertEquals(3, test.getSecondOfMinute());
163 assertEquals(4, test.getMillisOfSecond());
164 }
165
166 //-----------------------------------------------------------------------
167 public void testFactoryMillisOfDay_long_Chronology() throws Throwable {
168 LocalTime test = LocalTime.fromMillisOfDay(TEST_TIME1, JULIAN_LONDON);
169 assertEquals(JULIAN_UTC, test.getChronology());
170 assertEquals(1, test.getHourOfDay());
171 assertEquals(2, test.getMinuteOfHour());
172 assertEquals(3, test.getSecondOfMinute());
173 assertEquals(4, test.getMillisOfSecond());
174 }
175
176 public void testFactoryMillisOfDay_long_nullChronology() throws Throwable {
177 LocalTime test = LocalTime.fromMillisOfDay(TEST_TIME1, null);
178 assertEquals(ISO_UTC, test.getChronology());
179 assertEquals(1, test.getHourOfDay());
180 assertEquals(2, test.getMinuteOfHour());
181 assertEquals(3, test.getSecondOfMinute());
182 assertEquals(4, test.getMillisOfSecond());
183 }
184
185 //-----------------------------------------------------------------------
186 public void testConstructor() throws Throwable {
187 LocalTime test = new LocalTime();
188 assertEquals(ISO_UTC, test.getChronology());
189 assertEquals(10 + OFFSET_LONDON, test.getHourOfDay());
190 assertEquals(20, test.getMinuteOfHour());
191 assertEquals(30, test.getSecondOfMinute());
192 assertEquals(40, test.getMillisOfSecond());
193 assertEquals(test, LocalTime.now());
194 }
195
196 //-----------------------------------------------------------------------
197 public void testConstructor_DateTimeZone() throws Throwable {
198 DateTime dt = new DateTime(2005, 6, 8, 23, 59, 30, 40, LONDON);
199 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
200 // 23:59 in London is 00:59 the following day in Paris
201
202 LocalTime test = new LocalTime(LONDON);
203 assertEquals(ISO_UTC, test.getChronology());
204 assertEquals(23, test.getHourOfDay());
205 assertEquals(59, test.getMinuteOfHour());
206 assertEquals(30, test.getSecondOfMinute());
207 assertEquals(40, test.getMillisOfSecond());
208 assertEquals(test, LocalTime.now(LONDON));
209
210 test = new LocalTime(PARIS);
211 assertEquals(ISO_UTC, test.getChronology());
212 assertEquals(0, test.getHourOfDay());
213 assertEquals(59, test.getMinuteOfHour());
214 assertEquals(30, test.getSecondOfMinute());
215 assertEquals(40, test.getMillisOfSecond());
216 assertEquals(test, LocalTime.now(PARIS));
217 }
218
219 public void testConstructor_nullDateTimeZone() throws Throwable {
220 DateTime dt = new DateTime(2005, 6, 8, 23, 59, 30, 40, LONDON);
221 DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
222 // 23:59 in London is 00:59 the following day in Paris
223
224 LocalTime test = new LocalTime((DateTimeZone) null);
225 assertEquals(ISO_UTC, test.getChronology());
226 assertEquals(23, test.getHourOfDay());
227 assertEquals(59, test.getMinuteOfHour());
228 assertEquals(30, test.getSecondOfMinute());
229 assertEquals(40, test.getMillisOfSecond());
230 }
231
232 //-----------------------------------------------------------------------
233 public void testConstructor_Chronology() throws Throwable {
234 LocalTime test = new LocalTime(JULIAN_LONDON);
235 assertEquals(JULIAN_UTC, test.getChronology());
236 assertEquals(10 + OFFSET_LONDON, test.getHourOfDay());
237 assertEquals(20, test.getMinuteOfHour());
238 assertEquals(30, test.getSecondOfMinute());
239 assertEquals(40, test.getMillisOfSecond());
240 assertEquals(test, LocalTime.now(JULIAN_LONDON));
241 }
242
243 public void testConstructor_nullChronology() throws Throwable {
244 LocalTime test = new LocalTime((Chronology) null);
245 assertEquals(ISO_UTC, test.getChronology());
246 assertEquals(10 + OFFSET_LONDON, test.getHourOfDay());
247 assertEquals(20, test.getMinuteOfHour());
248 assertEquals(30, test.getSecondOfMinute());
249 assertEquals(40, test.getMillisOfSecond());
250 }
251
252 //-----------------------------------------------------------------------
253 public void testConstructor_long1() throws Throwable {
254 LocalTime test = new LocalTime(TEST_TIME1);
255 assertEquals(ISO_UTC, test.getChronology());
256 assertEquals(1 + OFFSET_LONDON, test.getHourOfDay());
257 assertEquals(2, test.getMinuteOfHour());
258 assertEquals(3, test.getSecondOfMinute());
259 assertEquals(4, test.getMillisOfSecond());
260 }
261
262 public void testConstructor_long2() throws Throwable {
263 LocalTime test = new LocalTime(TEST_TIME2);
264 assertEquals(ISO_UTC, test.getChronology());
265 assertEquals(5 + OFFSET_LONDON, test.getHourOfDay());
266 assertEquals(6, test.getMinuteOfHour());
267 assertEquals(7, test.getSecondOfMinute());
268 assertEquals(8, test.getMillisOfSecond());
269 }
270
271 //-----------------------------------------------------------------------
272 public void testConstructor_long_DateTimeZone() throws Throwable {
273 LocalTime test = new LocalTime(TEST_TIME1, PARIS);
274 assertEquals(ISO_UTC, test.getChronology());
275 assertEquals(1 + OFFSET_PARIS, test.getHourOfDay());
276 assertEquals(2, test.getMinuteOfHour());
277 assertEquals(3, test.getSecondOfMinute());
278 assertEquals(4, test.getMillisOfSecond());
279 }
280
281 public void testConstructor_long_DateTimeZone_2() throws Throwable {
282 DateTime dt = new DateTime(2007, 6, 9, 1, 2, 3, 4, PARIS);
283 DateTime dtUTC = new DateTime(1970, 1, 1, 1, 2, 3, 4, DateTimeZone.UTC);
284
285 LocalTime test = new LocalTime(dt.getMillis(), PARIS);
286 assertEquals(ISO_UTC, test.getChronology());
287 assertEquals(1, test.getHourOfDay());
288 assertEquals(2, test.getMinuteOfHour());
289 assertEquals(3, test.getSecondOfMinute());
290 assertEquals(4, test.getMillisOfSecond());
291 assertEquals(dtUTC.getMillis(), test.getLocalMillis());
292 }
293
294 public void testConstructor_long_nullDateTimeZone() throws Throwable {
295 LocalTime test = new LocalTime(TEST_TIME1, (DateTimeZone) null);
296 assertEquals(ISO_UTC, test.getChronology());
297 assertEquals(1 + OFFSET_LONDON, test.getHourOfDay());
298 assertEquals(2, test.getMinuteOfHour());
299 assertEquals(3, test.getSecondOfMinute());
300 assertEquals(4, test.getMillisOfSecond());
301 }
302
303 //-----------------------------------------------------------------------
304 public void testConstructor_long1_Chronology() throws Throwable {
305 LocalTime test = new LocalTime(TEST_TIME1, JULIAN_PARIS);
306 assertEquals(JULIAN_UTC, test.getChronology());
307 assertEquals(1 + OFFSET_PARIS, test.getHourOfDay());
308 assertEquals(2, test.getMinuteOfHour());
309 assertEquals(3, test.getSecondOfMinute());
310 assertEquals(4, test.getMillisOfSecond());
311 }
312
313 public void testConstructor_long2_Chronology() throws Throwable {
314 LocalTime test = new LocalTime(TEST_TIME2, JULIAN_LONDON);
315 assertEquals(JULIAN_UTC, test.getChronology());
316 assertEquals(5 + OFFSET_LONDON, test.getHourOfDay());
317 assertEquals(6, test.getMinuteOfHour());
318 assertEquals(7, test.getSecondOfMinute());
319 assertEquals(8, test.getMillisOfSecond());
320 }
321
322 public void testConstructor_long_nullChronology() throws Throwable {
323 LocalTime test = new LocalTime(TEST_TIME1, (Chronology) null);
324 assertEquals(ISO_UTC, test.getChronology());
325 assertEquals(1 + OFFSET_LONDON, test.getHourOfDay());
326 assertEquals(2, test.getMinuteOfHour());
327 assertEquals(3, test.getSecondOfMinute());
328 assertEquals(4, test.getMillisOfSecond());
329 }
330
331 //-----------------------------------------------------------------------
332 public void testConstructor_Object1() throws Throwable {
333 Date date = new Date(TEST_TIME1);
334 LocalTime test = new LocalTime(date);
335 assertEquals(ISO_UTC, test.getChronology());
336 assertEquals(1 + OFFSET_LONDON, test.getHourOfDay());
337 assertEquals(2, test.getMinuteOfHour());
338 assertEquals(3, test.getSecondOfMinute());
339 assertEquals(4, test.getMillisOfSecond());
340 }
341
342 public void testConstructor_Object2() throws Throwable {
343 Calendar cal = new GregorianCalendar();
344 cal.setTime(new Date(TEST_TIME1));
345 LocalTime test = new LocalTime(cal);
346 assertEquals(GJChronology.getInstanceUTC(), test.getChronology());
347 assertEquals(1 + OFFSET_LONDON, test.getHourOfDay());
348 assertEquals(2, test.getMinuteOfHour());
349 assertEquals(3, test.getSecondOfMinute());
350 assertEquals(4, test.getMillisOfSecond());
351 }
352
353 public void testConstructor_nullObject() throws Throwable {
354 LocalTime test = new LocalTime((Object) null);
355 assertEquals(ISO_UTC, test.getChronology());
356 assertEquals(10 + OFFSET_LONDON, test.getHourOfDay());
357 assertEquals(20, test.getMinuteOfHour());
358 assertEquals(30, test.getSecondOfMinute());
359 assertEquals(40, test.getMillisOfSecond());
360 }
361
362 public void testConstructor_ObjectString1() throws Throwable {
363 LocalTime test = new LocalTime("10:20:30.040");
364 assertEquals(ISO_UTC, test.getChronology());
365 assertEquals(10, test.getHourOfDay());
366 assertEquals(20, test.getMinuteOfHour());
367 assertEquals(30, test.getSecondOfMinute());
368 assertEquals(40, test.getMillisOfSecond());
369 }
370
371 public void testConstructor_ObjectString1Tokyo() throws Throwable {
372 DateTimeZone.setDefault(TOKYO);
373 LocalTime test = new LocalTime("10:20:30.040");
374 assertEquals(ISO_UTC, test.getChronology());
375 assertEquals(10, test.getHourOfDay());
376 assertEquals(20, test.getMinuteOfHour());
377 assertEquals(30, test.getSecondOfMinute());
378 assertEquals(40, test.getMillisOfSecond());
379 }
380
381 public void testConstructor_ObjectString1NewYork() throws Throwable {
382 DateTimeZone.setDefault(NEW_YORK);
383 LocalTime test = new LocalTime("10:20:30.040");
384 assertEquals(ISO_UTC, test.getChronology());
385 assertEquals(10, test.getHourOfDay());
386 assertEquals(20, test.getMinuteOfHour());
387 assertEquals(30, test.getSecondOfMinute());
388 assertEquals(40, test.getMillisOfSecond());
389 }
390
391 public void testConstructor_ObjectString2() throws Throwable {
392 LocalTime test = new LocalTime("T10:20:30.040");
393 assertEquals(ISO_UTC, test.getChronology());
394 assertEquals(10, test.getHourOfDay());
395 assertEquals(20, test.getMinuteOfHour());
396 assertEquals(30, test.getSecondOfMinute());
397 assertEquals(40, test.getMillisOfSecond());
398 }
399
400 public void testConstructor_ObjectString3() throws Throwable {
401 LocalTime test = new LocalTime("10:20");
402 assertEquals(ISO_UTC, test.getChronology());
403 assertEquals(10, test.getHourOfDay());
404 assertEquals(20, test.getMinuteOfHour());
405 assertEquals(0, test.getSecondOfMinute());
406 assertEquals(0, test.getMillisOfSecond());
407 }
408
409 public void testConstructor_ObjectString4() throws Throwable {
410 LocalTime test = new LocalTime("10");
411 assertEquals(ISO_UTC, test.getChronology());
412 assertEquals(10, test.getHourOfDay());
413 assertEquals(0, test.getMinuteOfHour());
414 assertEquals(0, test.getSecondOfMinute());
415 assertEquals(0, test.getMillisOfSecond());
416 }
417
418 public void testConstructor_ObjectStringEx1() throws Throwable {
419 try {
420 new LocalTime("1970-04-06");
421 fail();
422 } catch (IllegalArgumentException ex) {}
423 }
424
425 public void testConstructor_ObjectStringEx2() throws Throwable {
426 try {
427 new LocalTime("1970-04-06T+14:00");
428 fail();
429 } catch (IllegalArgumentException ex) {}
430 }
431
432 public void testConstructor_ObjectStringEx3() throws Throwable {
433 try {
434 new LocalTime("1970-04-06T10:20:30.040");
435 fail();
436 } catch (IllegalArgumentException ex) {}
437 }
438
439 public void testConstructor_ObjectStringEx4() throws Throwable {
440 try {
441 new LocalTime("1970-04-06T10:20:30.040+14:00");
442 fail();
443 } catch (IllegalArgumentException ex) {}
444 }
445
446 public void testConstructor_ObjectStringEx5() throws Throwable {
447 try {
448 new LocalTime("T10:20:30.040+04:00");
449 fail();
450 } catch (IllegalArgumentException ex) {}
451 }
452
453 public void testConstructor_ObjectStringEx6() throws Throwable {
454 try {
455 new LocalTime("10:20:30.040+04:00");
456 fail();
457 } catch (IllegalArgumentException ex) {}
458 }
459
460 public void testConstructor_ObjectLocalTime() throws Throwable {
461 LocalTime time = new LocalTime(10, 20, 30, 40, BUDDHIST_UTC);
462 LocalTime test = new LocalTime(time);
463 assertEquals(BUDDHIST_UTC, test.getChronology());
464 assertEquals(10, test.getHourOfDay());
465 assertEquals(20, test.getMinuteOfHour());
466 assertEquals(30, test.getSecondOfMinute());
467 assertEquals(40, test.getMillisOfSecond());
468 }
469
470 public void testConstructor_ObjectLocalDate() throws Throwable {
471 LocalDate date = new LocalDate(1970, 4, 6, BUDDHIST_UTC);
472 try {
473 new LocalTime(date);
474 fail();
475 } catch (IllegalArgumentException ex) {}
476 }
477
478 public void testConstructor_ObjectLocalDateTime() throws Throwable {
479 LocalDateTime dt = new LocalDateTime(1970, 5, 6, 10, 20, 30, 40, BUDDHIST_UTC);
480 LocalTime test = new LocalTime(dt);
481 assertEquals(BUDDHIST_UTC, test.getChronology());
482 assertEquals(10, test.getHourOfDay());
483 assertEquals(20, test.getMinuteOfHour());
484 assertEquals(30, test.getSecondOfMinute());
485 assertEquals(40, test.getMillisOfSecond());
486 }
487
488 public void testConstructor_ObjectTimeOfDay() throws Throwable {
489 TimeOfDay time = new TimeOfDay(10, 20, 30, 40, BUDDHIST_UTC);
490 LocalTime test = new LocalTime(time);
491 assertEquals(BUDDHIST_UTC, test.getChronology());
492 assertEquals(10, test.getHourOfDay());
493 assertEquals(20, test.getMinuteOfHour());
494 assertEquals(30, test.getSecondOfMinute());
495 assertEquals(40, test.getMillisOfSecond());
496 }
497
498 //-----------------------------------------------------------------------
499 public void testConstructor_Object1_DateTimeZone() throws Throwable {
500 Date date = new Date(TEST_TIME1);
501 LocalTime test = new LocalTime(date, PARIS);
502 assertEquals(ISO_UTC, test.getChronology());
503 assertEquals(1 + OFFSET_PARIS, test.getHourOfDay());
504 assertEquals(2, test.getMinuteOfHour());
505 assertEquals(3, test.getSecondOfMinute());
506 assertEquals(4, test.getMillisOfSecond());
507 }
508
509 public void testConstructor_ObjectString_DateTimeZoneLondon() throws Throwable {
510 LocalTime test = new LocalTime("04:20", LONDON);
511 assertEquals(4, test.getHourOfDay());
512 assertEquals(20, test.getMinuteOfHour());
513 }
514
515 public void testConstructor_ObjectString_DateTimeZoneTokyo() throws Throwable {
516 LocalTime test = new LocalTime("04:20", TOKYO);
517 assertEquals(ISO_UTC, test.getChronology());
518 assertEquals(4, test.getHourOfDay());
519 assertEquals(20, test.getMinuteOfHour());
520 }
521
522 public void testConstructor_ObjectString_DateTimeZoneNewYork() throws Throwable {
523 LocalTime test = new LocalTime("04:20", NEW_YORK);
524 assertEquals(ISO_UTC, test.getChronology());
525 assertEquals(4, test.getHourOfDay());
526 assertEquals(20, test.getMinuteOfHour());
527 }
528
529 public void testConstructor_nullObject_DateTimeZone() throws Throwable {
530 LocalTime test = new LocalTime((Object) null, PARIS);
531 assertEquals(ISO_UTC, test.getChronology());
532 assertEquals(10 + OFFSET_PARIS, test.getHourOfDay());
533 assertEquals(20, test.getMinuteOfHour());
534 assertEquals(30, test.getSecondOfMinute());
535 assertEquals(40, test.getMillisOfSecond());
536 }
537
538 public void testConstructor_Object_nullDateTimeZone() throws Throwable {
539 Date date = new Date(TEST_TIME1);
540 LocalTime test = new LocalTime(date, (DateTimeZone) null);
541 assertEquals(ISO_UTC, test.getChronology());
542 assertEquals(1 + OFFSET_LONDON, test.getHourOfDay());
543 assertEquals(2, test.getMinuteOfHour());
544 assertEquals(3, test.getSecondOfMinute());
545 assertEquals(4, test.getMillisOfSecond());
546 }
547
548 public void testConstructor_nullObject_nullDateTimeZone() throws Throwable {
549 LocalTime test = new LocalTime((Object) null, (DateTimeZone) null);
550 assertEquals(ISO_UTC, test.getChronology());
551 assertEquals(10 + OFFSET_LONDON, test.getHourOfDay());
552 assertEquals(20, test.getMinuteOfHour());
553 assertEquals(30, test.getSecondOfMinute());
554 assertEquals(40, test.getMillisOfSecond());
555 }
556
557 //-----------------------------------------------------------------------
558 public void testConstructor_Object1_Chronology() throws Throwable {
559 Date date = new Date(TEST_TIME1);
560 LocalTime test = new LocalTime(date, JULIAN_LONDON);
561 assertEquals(JULIAN_UTC, test.getChronology());
562 assertEquals(1 + OFFSET_LONDON, test.getHourOfDay());
563 assertEquals(2, test.getMinuteOfHour());
564 assertEquals(3, test.getSecondOfMinute());
565 assertEquals(4, test.getMillisOfSecond());
566 }
567
568 public void testConstructor_Object2_Chronology() throws Throwable {
569 LocalTime test = new LocalTime("T10:20");
570 assertEquals(10, test.getHourOfDay());
571 assertEquals(20, test.getMinuteOfHour());
572 assertEquals(0, test.getSecondOfMinute());
573 assertEquals(0, test.getMillisOfSecond());
574
575 try {
576 new LocalTime("T1020");
577 fail();
578 } catch (IllegalArgumentException ex) {}
579 }
580
581 public void testConstructor_nullObject_Chronology() throws Throwable {
582 LocalTime test = new LocalTime((Object) null, JULIAN_LONDON);
583 assertEquals(JULIAN_UTC, test.getChronology());
584 assertEquals(10 + OFFSET_LONDON, test.getHourOfDay());
585 assertEquals(20, test.getMinuteOfHour());
586 assertEquals(30, test.getSecondOfMinute());
587 assertEquals(40, test.getMillisOfSecond());
588 }
589
590 public void testConstructor_Object_nullChronology() throws Throwable {
591 Date date = new Date(TEST_TIME1);
592 LocalTime test = new LocalTime(date, (Chronology) null);
593 assertEquals(ISO_UTC, test.getChronology());
594 assertEquals(1 + OFFSET_LONDON, test.getHourOfDay());
595 assertEquals(2, test.getMinuteOfHour());
596 assertEquals(3, test.getSecondOfMinute());
597 assertEquals(4, test.getMillisOfSecond());
598 }
599
600 public void testConstructor_nullObject_nullChronology() throws Throwable {
601 LocalTime test = new LocalTime((Object) null, (Chronology) null);
602 assertEquals(ISO_UTC, test.getChronology());
603 assertEquals(10 + OFFSET_LONDON, test.getHourOfDay());
604 assertEquals(20, test.getMinuteOfHour());
605 assertEquals(30, test.getSecondOfMinute());
606 assertEquals(40, test.getMillisOfSecond());
607 }
608
609 //-----------------------------------------------------------------------
610 public void testConstructor_int_int() throws Throwable {
611 LocalTime test = new LocalTime(10, 20);
612 assertEquals(ISO_UTC, test.getChronology());
613 assertEquals(10, test.getHourOfDay());
614 assertEquals(20, test.getMinuteOfHour());
615 assertEquals(0, test.getSecondOfMinute());
616 assertEquals(0, test.getMillisOfSecond());
617 try {
618 new LocalTime(-1, 20);
619 fail();
620 } catch (IllegalArgumentException ex) {}
621 try {
622 new LocalTime(24, 20);
623 fail();
624 } catch (IllegalArgumentException ex) {}
625 try {
626 new LocalTime(10, -1);
627 fail();
628 } catch (IllegalArgumentException ex) {}
629 try {
630 new LocalTime(10, 60);
631 fail();
632 } catch (IllegalArgumentException ex) {}
633 }
634
635 public void testConstructor_int_int_int() throws Throwable {
636 LocalTime test = new LocalTime(10, 20, 30);
637 assertEquals(ISO_UTC, test.getChronology());
638 assertEquals(10, test.getHourOfDay());
639 assertEquals(20, test.getMinuteOfHour());
640 assertEquals(30, test.getSecondOfMinute());
641 assertEquals(0, test.getMillisOfSecond());
642 try {
643 new LocalTime(-1, 20, 30);
644 fail();
645 } catch (IllegalArgumentException ex) {}
646 try {
647 new LocalTime(24, 20, 30);
648 fail();
649 } catch (IllegalArgumentException ex) {}
650 try {
651 new LocalTime(10, -1, 30);
652 fail();
653 } catch (IllegalArgumentException ex) {}
654 try {
655 new LocalTime(10, 60, 30);
656 fail();
657 } catch (IllegalArgumentException ex) {}
658 try {
659 new LocalTime(10, 20, -1);
660 fail();
661 } catch (IllegalArgumentException ex) {}
662 try {
663 new LocalTime(10, 20, 60);
664 fail();
665 } catch (IllegalArgumentException ex) {}
666 }
667
668 public void testConstructor_int_int_int_int() throws Throwable {
669 LocalTime test = new LocalTime(10, 20, 30, 40);
670 assertEquals(ISO_UTC, test.getChronology());
671 assertEquals(10, test.getHourOfDay());
672 assertEquals(20, test.getMinuteOfHour());
673 assertEquals(30, test.getSecondOfMinute());
674 assertEquals(40, test.getMillisOfSecond());
675 try {
676 new LocalTime(-1, 20, 30, 40);
677 fail();
678 } catch (IllegalArgumentException ex) {}
679 try {
680 new LocalTime(24, 20, 30, 40);
681 fail();
682 } catch (IllegalArgumentException ex) {}
683 try {
684 new LocalTime(10, -1, 30, 40);
685 fail();
686 } catch (IllegalArgumentException ex) {}
687 try {
688 new LocalTime(10, 60, 30, 40);
689 fail();
690 } catch (IllegalArgumentException ex) {}
691 try {
692 new LocalTime(10, 20, -1, 40);
693 fail();
694 } catch (IllegalArgumentException ex) {}
695 try {
696 new LocalTime(10, 20, 60, 40);
697 fail();
698 } catch (IllegalArgumentException ex) {}
699 try {
700 new LocalTime(10, 20, 30, -1);
701 fail();
702 } catch (IllegalArgumentException ex) {}
703 try {
704 new LocalTime(10, 20, 30, 1000);
705 fail();
706 } catch (IllegalArgumentException ex) {}
707 }
708
709 public void testConstructor_int_int_int_int_Chronology() throws Throwable {
710 LocalTime test = new LocalTime(10, 20, 30, 40, JULIAN_LONDON);
711 assertEquals(JULIAN_UTC, test.getChronology());
712 assertEquals(10, test.getHourOfDay());
713 assertEquals(20, test.getMinuteOfHour());
714 assertEquals(30, test.getSecondOfMinute());
715 assertEquals(40, test.getMillisOfSecond());
716 try {
717 new LocalTime(-1, 20, 30, 40, JULIAN_LONDON);
718 fail();
719 } catch (IllegalArgumentException ex) {}
720 try {
721 new LocalTime(24, 20, 30, 40, JULIAN_LONDON);
722 fail();
723 } catch (IllegalArgumentException ex) {}
724 try {
725 new LocalTime(10, -1, 30, 40, JULIAN_LONDON);
726 fail();
727 } catch (IllegalArgumentException ex) {}
728 try {
729 new LocalTime(10, 60, 30, 40, JULIAN_LONDON);
730 fail();
731 } catch (IllegalArgumentException ex) {}
732 try {
733 new LocalTime(10, 20, -1, 40, JULIAN_LONDON);
734 fail();
735 } catch (IllegalArgumentException ex) {}
736 try {
737 new LocalTime(10, 20, 60, 40, JULIAN_LONDON);
738 fail();
739 } catch (IllegalArgumentException ex) {}
740 try {
741 new LocalTime(10, 20, 30, -1, JULIAN_LONDON);
742 fail();
743 } catch (IllegalArgumentException ex) {}
744 try {
745 new LocalTime(10, 20, 30, 1000, JULIAN_LONDON);
746 fail();
747 } catch (IllegalArgumentException ex) {}
748 }
749
750 public void testConstructor_int_int_int_int_nullChronology() throws Throwable {
751 LocalTime test = new LocalTime(10, 20, 30, 40, null);
752 assertEquals(ISO_UTC, test.getChronology());
753 assertEquals(10, test.getHourOfDay());
754 assertEquals(20, test.getMinuteOfHour());
755 assertEquals(30, test.getSecondOfMinute());
756 assertEquals(40, test.getMillisOfSecond());
757 }
758
759 }