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