0001: /*
0002: * Copyright 2001-2006 Stephen Colebourne
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.joda.time;
0017:
0018: import java.io.ByteArrayInputStream;
0019: import java.io.ByteArrayOutputStream;
0020: import java.io.ObjectInputStream;
0021: import java.io.ObjectOutputStream;
0022: import java.util.Arrays;
0023: import java.util.Date;
0024: import java.util.Locale;
0025:
0026: import junit.framework.TestCase;
0027: import junit.framework.TestSuite;
0028:
0029: import org.joda.time.chrono.BuddhistChronology;
0030: import org.joda.time.chrono.CopticChronology;
0031: import org.joda.time.chrono.GregorianChronology;
0032: import org.joda.time.chrono.ISOChronology;
0033: import org.joda.time.format.DateTimeFormat;
0034: import org.joda.time.format.DateTimeFormatter;
0035:
0036: /**
0037: * This class is a Junit unit test for TimeOfDay.
0038: *
0039: * @author Stephen Colebourne
0040: */
0041: public class TestTimeOfDay_Basics extends TestCase {
0042:
0043: private static final DateTimeZone PARIS = DateTimeZone
0044: .forID("Europe/Paris");
0045: private static final DateTimeZone LONDON = DateTimeZone
0046: .forID("Europe/London");
0047: private static final DateTimeZone TOKYO = DateTimeZone
0048: .forID("Asia/Tokyo");
0049: private static final int OFFSET = 1;
0050: private static final Chronology COPTIC_PARIS = CopticChronology
0051: .getInstance(PARIS);
0052: private static final Chronology COPTIC_LONDON = CopticChronology
0053: .getInstance(LONDON);
0054: private static final Chronology COPTIC_TOKYO = CopticChronology
0055: .getInstance(TOKYO);
0056: private static final Chronology COPTIC_UTC = CopticChronology
0057: .getInstanceUTC();
0058: private static final Chronology ISO_PARIS = ISOChronology
0059: .getInstance(PARIS);
0060: private static final Chronology ISO_LONDON = ISOChronology
0061: .getInstance(LONDON);
0062: private static final Chronology ISO_TOKYO = ISOChronology
0063: .getInstance(TOKYO);
0064: private static final Chronology ISO_UTC = ISOChronology
0065: .getInstanceUTC();
0066: private static final Chronology BUDDHIST_PARIS = BuddhistChronology
0067: .getInstance(PARIS);
0068: private static final Chronology BUDDHIST_LONDON = BuddhistChronology
0069: .getInstance(LONDON);
0070: private static final Chronology BUDDHIST_TOKYO = BuddhistChronology
0071: .getInstance(TOKYO);
0072: private static final Chronology BUDDHIST_UTC = BuddhistChronology
0073: .getInstanceUTC();
0074:
0075: private long TEST_TIME_NOW = 10L
0076: * DateTimeConstants.MILLIS_PER_HOUR + 20L
0077: * DateTimeConstants.MILLIS_PER_MINUTE + 30L
0078: * DateTimeConstants.MILLIS_PER_SECOND + 40L;
0079:
0080: private long TEST_TIME1 = 1L * DateTimeConstants.MILLIS_PER_HOUR
0081: + 2L * DateTimeConstants.MILLIS_PER_MINUTE + 3L
0082: * DateTimeConstants.MILLIS_PER_SECOND + 4L;
0083:
0084: private long TEST_TIME2 = 1L * DateTimeConstants.MILLIS_PER_DAY
0085: + 5L * DateTimeConstants.MILLIS_PER_HOUR + 6L
0086: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
0087: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
0088:
0089: private DateTimeZone zone = null;
0090:
0091: public static void main(String[] args) {
0092: junit.textui.TestRunner.run(suite());
0093: }
0094:
0095: public static TestSuite suite() {
0096: return new TestSuite(TestTimeOfDay_Basics.class);
0097: }
0098:
0099: public TestTimeOfDay_Basics(String name) {
0100: super (name);
0101: }
0102:
0103: protected void setUp() throws Exception {
0104: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
0105: zone = DateTimeZone.getDefault();
0106: DateTimeZone.setDefault(LONDON);
0107: }
0108:
0109: protected void tearDown() throws Exception {
0110: DateTimeUtils.setCurrentMillisSystem();
0111: DateTimeZone.setDefault(zone);
0112: zone = null;
0113: }
0114:
0115: //-----------------------------------------------------------------------
0116: public void testGet() {
0117: TimeOfDay test = new TimeOfDay();
0118: assertEquals(10 + OFFSET, test.get(DateTimeFieldType
0119: .hourOfDay()));
0120: assertEquals(20, test.get(DateTimeFieldType.minuteOfHour()));
0121: assertEquals(30, test.get(DateTimeFieldType.secondOfMinute()));
0122: assertEquals(40, test.get(DateTimeFieldType.millisOfSecond()));
0123: try {
0124: test.get(null);
0125: fail();
0126: } catch (IllegalArgumentException ex) {
0127: }
0128: try {
0129: test.get(DateTimeFieldType.dayOfMonth());
0130: fail();
0131: } catch (IllegalArgumentException ex) {
0132: }
0133: }
0134:
0135: public void testSize() {
0136: TimeOfDay test = new TimeOfDay();
0137: assertEquals(4, test.size());
0138: }
0139:
0140: public void testGetFieldType() {
0141: TimeOfDay test = new TimeOfDay(COPTIC_PARIS);
0142: assertSame(DateTimeFieldType.hourOfDay(), test.getFieldType(0));
0143: assertSame(DateTimeFieldType.minuteOfHour(), test
0144: .getFieldType(1));
0145: assertSame(DateTimeFieldType.secondOfMinute(), test
0146: .getFieldType(2));
0147: assertSame(DateTimeFieldType.millisOfSecond(), test
0148: .getFieldType(3));
0149: try {
0150: test.getFieldType(-1);
0151: } catch (IndexOutOfBoundsException ex) {
0152: }
0153: try {
0154: test.getFieldType(5);
0155: } catch (IndexOutOfBoundsException ex) {
0156: }
0157: }
0158:
0159: public void testGetFieldTypes() {
0160: TimeOfDay test = new TimeOfDay(COPTIC_PARIS);
0161: DateTimeFieldType[] fields = test.getFieldTypes();
0162: assertSame(DateTimeFieldType.hourOfDay(), fields[0]);
0163: assertSame(DateTimeFieldType.minuteOfHour(), fields[1]);
0164: assertSame(DateTimeFieldType.secondOfMinute(), fields[2]);
0165: assertSame(DateTimeFieldType.millisOfSecond(), fields[3]);
0166: assertNotSame(test.getFieldTypes(), test.getFieldTypes());
0167: }
0168:
0169: public void testGetField() {
0170: TimeOfDay test = new TimeOfDay(COPTIC_PARIS);
0171: assertSame(CopticChronology.getInstanceUTC().hourOfDay(), test
0172: .getField(0));
0173: assertSame(CopticChronology.getInstanceUTC().minuteOfHour(),
0174: test.getField(1));
0175: assertSame(CopticChronology.getInstanceUTC().secondOfMinute(),
0176: test.getField(2));
0177: assertSame(CopticChronology.getInstanceUTC().millisOfSecond(),
0178: test.getField(3));
0179: try {
0180: test.getField(-1);
0181: } catch (IndexOutOfBoundsException ex) {
0182: }
0183: try {
0184: test.getField(5);
0185: } catch (IndexOutOfBoundsException ex) {
0186: }
0187: }
0188:
0189: public void testGetFields() {
0190: TimeOfDay test = new TimeOfDay(COPTIC_PARIS);
0191: DateTimeField[] fields = test.getFields();
0192: assertSame(CopticChronology.getInstanceUTC().hourOfDay(),
0193: fields[0]);
0194: assertSame(CopticChronology.getInstanceUTC().minuteOfHour(),
0195: fields[1]);
0196: assertSame(CopticChronology.getInstanceUTC().secondOfMinute(),
0197: fields[2]);
0198: assertSame(CopticChronology.getInstanceUTC().millisOfSecond(),
0199: fields[3]);
0200: assertNotSame(test.getFields(), test.getFields());
0201: }
0202:
0203: public void testGetValue() {
0204: TimeOfDay test = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS);
0205: assertEquals(10, test.getValue(0));
0206: assertEquals(20, test.getValue(1));
0207: assertEquals(30, test.getValue(2));
0208: assertEquals(40, test.getValue(3));
0209: try {
0210: test.getValue(-1);
0211: } catch (IndexOutOfBoundsException ex) {
0212: }
0213: try {
0214: test.getValue(5);
0215: } catch (IndexOutOfBoundsException ex) {
0216: }
0217: }
0218:
0219: public void testGetValues() {
0220: TimeOfDay test = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS);
0221: int[] values = test.getValues();
0222: assertEquals(10, values[0]);
0223: assertEquals(20, values[1]);
0224: assertEquals(30, values[2]);
0225: assertEquals(40, values[3]);
0226: assertNotSame(test.getValues(), test.getValues());
0227: }
0228:
0229: public void testIsSupported() {
0230: TimeOfDay test = new TimeOfDay(COPTIC_PARIS);
0231: assertEquals(true, test.isSupported(DateTimeFieldType
0232: .hourOfDay()));
0233: assertEquals(true, test.isSupported(DateTimeFieldType
0234: .minuteOfHour()));
0235: assertEquals(true, test.isSupported(DateTimeFieldType
0236: .secondOfMinute()));
0237: assertEquals(true, test.isSupported(DateTimeFieldType
0238: .millisOfSecond()));
0239: assertEquals(false, test.isSupported(DateTimeFieldType
0240: .dayOfMonth()));
0241: }
0242:
0243: public void testEqualsHashCode() {
0244: TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS);
0245: TimeOfDay test2 = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS);
0246: assertEquals(true, test1.equals(test2));
0247: assertEquals(true, test2.equals(test1));
0248: assertEquals(true, test1.equals(test1));
0249: assertEquals(true, test2.equals(test2));
0250: assertEquals(true, test1.hashCode() == test2.hashCode());
0251: assertEquals(true, test1.hashCode() == test1.hashCode());
0252: assertEquals(true, test2.hashCode() == test2.hashCode());
0253:
0254: TimeOfDay test3 = new TimeOfDay(15, 20, 30, 40);
0255: assertEquals(false, test1.equals(test3));
0256: assertEquals(false, test2.equals(test3));
0257: assertEquals(false, test3.equals(test1));
0258: assertEquals(false, test3.equals(test2));
0259: assertEquals(false, test1.hashCode() == test3.hashCode());
0260: assertEquals(false, test2.hashCode() == test3.hashCode());
0261:
0262: assertEquals(false, test1.equals("Hello"));
0263: assertEquals(true, test1.equals(new MockInstant()));
0264: assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE));
0265: }
0266:
0267: class MockInstant extends MockPartial {
0268: public Chronology getChronology() {
0269: return CopticChronology.getInstanceUTC();
0270: }
0271:
0272: public DateTimeField[] getFields() {
0273: return new DateTimeField[] {
0274: CopticChronology.getInstanceUTC().hourOfDay(),
0275: CopticChronology.getInstanceUTC().minuteOfHour(),
0276: CopticChronology.getInstanceUTC().secondOfMinute(),
0277: CopticChronology.getInstanceUTC().millisOfSecond(), };
0278: }
0279:
0280: public int[] getValues() {
0281: return new int[] { 10, 20, 30, 40 };
0282: }
0283: }
0284:
0285: //-----------------------------------------------------------------------
0286: public void testCompareTo() {
0287: TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40);
0288: TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40);
0289: assertEquals(0, test1.compareTo(test1a));
0290: assertEquals(0, test1a.compareTo(test1));
0291: assertEquals(0, test1.compareTo(test1));
0292: assertEquals(0, test1a.compareTo(test1a));
0293:
0294: TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40);
0295: assertEquals(-1, test1.compareTo(test2));
0296: assertEquals(+1, test2.compareTo(test1));
0297:
0298: TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40,
0299: GregorianChronology.getInstanceUTC());
0300: assertEquals(-1, test1.compareTo(test3));
0301: assertEquals(+1, test3.compareTo(test1));
0302: assertEquals(0, test3.compareTo(test2));
0303:
0304: DateTimeFieldType[] types = new DateTimeFieldType[] {
0305: DateTimeFieldType.hourOfDay(),
0306: DateTimeFieldType.minuteOfHour(),
0307: DateTimeFieldType.secondOfMinute(),
0308: DateTimeFieldType.millisOfSecond(), };
0309: int[] values = new int[] { 10, 20, 30, 40 };
0310: Partial p = new Partial(types, values);
0311: assertEquals(0, test1.compareTo(p));
0312: try {
0313: test1.compareTo(null);
0314: fail();
0315: } catch (NullPointerException ex) {
0316: }
0317: try {
0318: test1.compareTo(new Date());
0319: fail();
0320: } catch (ClassCastException ex) {
0321: }
0322: }
0323:
0324: //-----------------------------------------------------------------------
0325: public void testIsEqual_TOD() {
0326: TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40);
0327: TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40);
0328: assertEquals(true, test1.isEqual(test1a));
0329: assertEquals(true, test1a.isEqual(test1));
0330: assertEquals(true, test1.isEqual(test1));
0331: assertEquals(true, test1a.isEqual(test1a));
0332:
0333: TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40);
0334: assertEquals(false, test1.isEqual(test2));
0335: assertEquals(false, test2.isEqual(test1));
0336:
0337: TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40,
0338: GregorianChronology.getInstanceUTC());
0339: assertEquals(false, test1.isEqual(test3));
0340: assertEquals(false, test3.isEqual(test1));
0341: assertEquals(true, test3.isEqual(test2));
0342:
0343: try {
0344: new TimeOfDay(10, 20, 35, 40).isEqual(null);
0345: fail();
0346: } catch (IllegalArgumentException ex) {
0347: }
0348: }
0349:
0350: //-----------------------------------------------------------------------
0351: public void testIsBefore_TOD() {
0352: TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40);
0353: TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40);
0354: assertEquals(false, test1.isBefore(test1a));
0355: assertEquals(false, test1a.isBefore(test1));
0356: assertEquals(false, test1.isBefore(test1));
0357: assertEquals(false, test1a.isBefore(test1a));
0358:
0359: TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40);
0360: assertEquals(true, test1.isBefore(test2));
0361: assertEquals(false, test2.isBefore(test1));
0362:
0363: TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40,
0364: GregorianChronology.getInstanceUTC());
0365: assertEquals(true, test1.isBefore(test3));
0366: assertEquals(false, test3.isBefore(test1));
0367: assertEquals(false, test3.isBefore(test2));
0368:
0369: try {
0370: new TimeOfDay(10, 20, 35, 40).isBefore(null);
0371: fail();
0372: } catch (IllegalArgumentException ex) {
0373: }
0374: }
0375:
0376: //-----------------------------------------------------------------------
0377: public void testIsAfter_TOD() {
0378: TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40);
0379: TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40);
0380: assertEquals(false, test1.isAfter(test1a));
0381: assertEquals(false, test1a.isAfter(test1));
0382: assertEquals(false, test1.isAfter(test1));
0383: assertEquals(false, test1a.isAfter(test1a));
0384:
0385: TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40);
0386: assertEquals(false, test1.isAfter(test2));
0387: assertEquals(true, test2.isAfter(test1));
0388:
0389: TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40,
0390: GregorianChronology.getInstanceUTC());
0391: assertEquals(false, test1.isAfter(test3));
0392: assertEquals(true, test3.isAfter(test1));
0393: assertEquals(false, test3.isAfter(test2));
0394:
0395: try {
0396: new TimeOfDay(10, 20, 35, 40).isAfter(null);
0397: fail();
0398: } catch (IllegalArgumentException ex) {
0399: }
0400: }
0401:
0402: //-----------------------------------------------------------------------
0403: public void testWithChronologyRetainFields_Chrono() {
0404: TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS);
0405: TimeOfDay test = base
0406: .withChronologyRetainFields(BUDDHIST_TOKYO);
0407: check(base, 10, 20, 30, 40);
0408: assertEquals(COPTIC_UTC, base.getChronology());
0409: check(test, 10, 20, 30, 40);
0410: assertEquals(BUDDHIST_UTC, test.getChronology());
0411: }
0412:
0413: public void testWithChronologyRetainFields_sameChrono() {
0414: TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS);
0415: TimeOfDay test = base.withChronologyRetainFields(COPTIC_TOKYO);
0416: assertSame(base, test);
0417: }
0418:
0419: public void testWithChronologyRetainFields_nullChrono() {
0420: TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS);
0421: TimeOfDay test = base.withChronologyRetainFields(null);
0422: check(base, 10, 20, 30, 40);
0423: assertEquals(COPTIC_UTC, base.getChronology());
0424: check(test, 10, 20, 30, 40);
0425: assertEquals(ISO_UTC, test.getChronology());
0426: }
0427:
0428: //-----------------------------------------------------------------------
0429: public void testWithField1() {
0430: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0431: TimeOfDay result = test.withField(
0432: DateTimeFieldType.hourOfDay(), 15);
0433:
0434: assertEquals(new TimeOfDay(10, 20, 30, 40), test);
0435: assertEquals(new TimeOfDay(15, 20, 30, 40), result);
0436: }
0437:
0438: public void testWithField2() {
0439: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0440: try {
0441: test.withField(null, 6);
0442: fail();
0443: } catch (IllegalArgumentException ex) {
0444: }
0445: }
0446:
0447: public void testWithField3() {
0448: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0449: try {
0450: test.withField(DateTimeFieldType.dayOfMonth(), 6);
0451: fail();
0452: } catch (IllegalArgumentException ex) {
0453: }
0454: }
0455:
0456: public void testWithField4() {
0457: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0458: TimeOfDay result = test.withField(
0459: DateTimeFieldType.hourOfDay(), 10);
0460: assertSame(test, result);
0461: }
0462:
0463: //-----------------------------------------------------------------------
0464: public void testWithFieldAdded1() {
0465: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0466: TimeOfDay result = test.withFieldAdded(DurationFieldType
0467: .hours(), 6);
0468:
0469: assertEquals(new TimeOfDay(10, 20, 30, 40), test);
0470: assertEquals(new TimeOfDay(16, 20, 30, 40), result);
0471: }
0472:
0473: public void testWithFieldAdded2() {
0474: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0475: try {
0476: test.withFieldAdded(null, 0);
0477: fail();
0478: } catch (IllegalArgumentException ex) {
0479: }
0480: }
0481:
0482: public void testWithFieldAdded3() {
0483: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0484: try {
0485: test.withFieldAdded(null, 6);
0486: fail();
0487: } catch (IllegalArgumentException ex) {
0488: }
0489: }
0490:
0491: public void testWithFieldAdded4() {
0492: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0493: TimeOfDay result = test.withFieldAdded(DurationFieldType
0494: .hours(), 0);
0495: assertSame(test, result);
0496: }
0497:
0498: public void testWithFieldAdded5() {
0499: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0500: try {
0501: test.withFieldAdded(DurationFieldType.days(), 6);
0502: fail();
0503: } catch (IllegalArgumentException ex) {
0504: }
0505: }
0506:
0507: public void testWithFieldAdded6() {
0508: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0509: TimeOfDay result = test.withFieldAdded(DurationFieldType
0510: .hours(), 16);
0511:
0512: assertEquals(new TimeOfDay(10, 20, 30, 40), test);
0513: assertEquals(new TimeOfDay(2, 20, 30, 40), result);
0514: }
0515:
0516: public void testWithFieldAdded7() {
0517: TimeOfDay test = new TimeOfDay(23, 59, 59, 999);
0518: TimeOfDay result = test.withFieldAdded(DurationFieldType
0519: .millis(), 1);
0520: assertEquals(new TimeOfDay(0, 0, 0, 0), result);
0521:
0522: test = new TimeOfDay(23, 59, 59, 999);
0523: result = test.withFieldAdded(DurationFieldType.seconds(), 1);
0524: assertEquals(new TimeOfDay(0, 0, 0, 999), result);
0525:
0526: test = new TimeOfDay(23, 59, 59, 999);
0527: result = test.withFieldAdded(DurationFieldType.minutes(), 1);
0528: assertEquals(new TimeOfDay(0, 0, 59, 999), result);
0529:
0530: test = new TimeOfDay(23, 59, 59, 999);
0531: result = test.withFieldAdded(DurationFieldType.hours(), 1);
0532: assertEquals(new TimeOfDay(0, 59, 59, 999), result);
0533: }
0534:
0535: public void testWithFieldAdded8() {
0536: TimeOfDay test = new TimeOfDay(0, 0, 0, 0);
0537: TimeOfDay result = test.withFieldAdded(DurationFieldType
0538: .millis(), -1);
0539: assertEquals(new TimeOfDay(23, 59, 59, 999), result);
0540:
0541: test = new TimeOfDay(0, 0, 0, 0);
0542: result = test.withFieldAdded(DurationFieldType.seconds(), -1);
0543: assertEquals(new TimeOfDay(23, 59, 59, 0), result);
0544:
0545: test = new TimeOfDay(0, 0, 0, 0);
0546: result = test.withFieldAdded(DurationFieldType.minutes(), -1);
0547: assertEquals(new TimeOfDay(23, 59, 0, 0), result);
0548:
0549: test = new TimeOfDay(0, 0, 0, 0);
0550: result = test.withFieldAdded(DurationFieldType.hours(), -1);
0551: assertEquals(new TimeOfDay(23, 0, 0, 0), result);
0552: }
0553:
0554: //-----------------------------------------------------------------------
0555: public void testPlus_RP() {
0556: TimeOfDay test = new TimeOfDay(10, 20, 30, 40,
0557: BuddhistChronology.getInstance());
0558: TimeOfDay result = test
0559: .plus(new Period(1, 2, 3, 4, 5, 6, 7, 8));
0560: TimeOfDay expected = new TimeOfDay(15, 26, 37, 48,
0561: BuddhistChronology.getInstance());
0562: assertEquals(expected, result);
0563:
0564: result = test.plus((ReadablePeriod) null);
0565: assertSame(test, result);
0566: }
0567:
0568: public void testPlusHours_int() {
0569: TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology
0570: .getInstance());
0571: TimeOfDay result = test.plusHours(1);
0572: TimeOfDay expected = new TimeOfDay(2, 2, 3, 4,
0573: BuddhistChronology.getInstance());
0574: assertEquals(expected, result);
0575:
0576: result = test.plusHours(0);
0577: assertSame(test, result);
0578: }
0579:
0580: public void testPlusMinutes_int() {
0581: TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology
0582: .getInstance());
0583: TimeOfDay result = test.plusMinutes(1);
0584: TimeOfDay expected = new TimeOfDay(1, 3, 3, 4,
0585: BuddhistChronology.getInstance());
0586: assertEquals(expected, result);
0587:
0588: result = test.plusMinutes(0);
0589: assertSame(test, result);
0590: }
0591:
0592: public void testPlusSeconds_int() {
0593: TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology
0594: .getInstance());
0595: TimeOfDay result = test.plusSeconds(1);
0596: TimeOfDay expected = new TimeOfDay(1, 2, 4, 4,
0597: BuddhistChronology.getInstance());
0598: assertEquals(expected, result);
0599:
0600: result = test.plusSeconds(0);
0601: assertSame(test, result);
0602: }
0603:
0604: public void testPlusMillis_int() {
0605: TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology
0606: .getInstance());
0607: TimeOfDay result = test.plusMillis(1);
0608: TimeOfDay expected = new TimeOfDay(1, 2, 3, 5,
0609: BuddhistChronology.getInstance());
0610: assertEquals(expected, result);
0611:
0612: result = test.plusMillis(0);
0613: assertSame(test, result);
0614: }
0615:
0616: //-----------------------------------------------------------------------
0617: public void testMinus_RP() {
0618: TimeOfDay test = new TimeOfDay(10, 20, 30, 40,
0619: BuddhistChronology.getInstance());
0620: TimeOfDay result = test
0621: .minus(new Period(1, 1, 1, 1, 1, 1, 1, 1));
0622: TimeOfDay expected = new TimeOfDay(9, 19, 29, 39,
0623: BuddhistChronology.getInstance());
0624: assertEquals(expected, result);
0625:
0626: result = test.minus((ReadablePeriod) null);
0627: assertSame(test, result);
0628: }
0629:
0630: public void testMinusHours_int() {
0631: TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology
0632: .getInstance());
0633: TimeOfDay result = test.minusHours(1);
0634: TimeOfDay expected = new TimeOfDay(0, 2, 3, 4,
0635: BuddhistChronology.getInstance());
0636: assertEquals(expected, result);
0637:
0638: result = test.minusHours(0);
0639: assertSame(test, result);
0640: }
0641:
0642: public void testMinusMinutes_int() {
0643: TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology
0644: .getInstance());
0645: TimeOfDay result = test.minusMinutes(1);
0646: TimeOfDay expected = new TimeOfDay(1, 1, 3, 4,
0647: BuddhistChronology.getInstance());
0648: assertEquals(expected, result);
0649:
0650: result = test.minusMinutes(0);
0651: assertSame(test, result);
0652: }
0653:
0654: public void testMinusSeconds_int() {
0655: TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology
0656: .getInstance());
0657: TimeOfDay result = test.minusSeconds(1);
0658: TimeOfDay expected = new TimeOfDay(1, 2, 2, 4,
0659: BuddhistChronology.getInstance());
0660: assertEquals(expected, result);
0661:
0662: result = test.minusSeconds(0);
0663: assertSame(test, result);
0664: }
0665:
0666: public void testMinusMillis_int() {
0667: TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology
0668: .getInstance());
0669: TimeOfDay result = test.minusMillis(1);
0670: TimeOfDay expected = new TimeOfDay(1, 2, 3, 3,
0671: BuddhistChronology.getInstance());
0672: assertEquals(expected, result);
0673:
0674: result = test.minusMillis(0);
0675: assertSame(test, result);
0676: }
0677:
0678: //-----------------------------------------------------------------------
0679: public void testToLocalTime() {
0680: TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_UTC);
0681: LocalTime test = base.toLocalTime();
0682: assertEquals(new LocalTime(10, 20, 30, 40, COPTIC_UTC), test);
0683: }
0684:
0685: //-----------------------------------------------------------------------
0686: public void testToDateTimeToday() {
0687: TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
0688: DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
0689: DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
0690:
0691: DateTime test = base.toDateTimeToday();
0692: check(base, 10, 20, 30, 40);
0693: DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON);
0694: expected = expected.hourOfDay().setCopy(10);
0695: expected = expected.minuteOfHour().setCopy(20);
0696: expected = expected.secondOfMinute().setCopy(30);
0697: expected = expected.millisOfSecond().setCopy(40);
0698: assertEquals(expected, test);
0699: }
0700:
0701: //-----------------------------------------------------------------------
0702: public void testToDateTimeToday_Zone() {
0703: TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
0704: DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
0705: DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
0706:
0707: DateTime test = base.toDateTimeToday(TOKYO);
0708: check(base, 10, 20, 30, 40);
0709: DateTime expected = new DateTime(dt.getMillis(), COPTIC_TOKYO);
0710: expected = expected.hourOfDay().setCopy(10);
0711: expected = expected.minuteOfHour().setCopy(20);
0712: expected = expected.secondOfMinute().setCopy(30);
0713: expected = expected.millisOfSecond().setCopy(40);
0714: assertEquals(expected, test);
0715: }
0716:
0717: public void testToDateTimeToday_nullZone() {
0718: TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
0719: DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9);
0720: DateTimeUtils.setCurrentMillisFixed(dt.getMillis());
0721:
0722: DateTime test = base.toDateTimeToday((DateTimeZone) null);
0723: check(base, 10, 20, 30, 40);
0724: DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON);
0725: expected = expected.hourOfDay().setCopy(10);
0726: expected = expected.minuteOfHour().setCopy(20);
0727: expected = expected.secondOfMinute().setCopy(30);
0728: expected = expected.millisOfSecond().setCopy(40);
0729: assertEquals(expected, test);
0730: }
0731:
0732: // Removed as too complex
0733: // /**
0734: // * Merges two partial together, taking account of the different chronologies.
0735: // *
0736: // * @param main the main partial
0737: // * @param base the partial to use as a base to merge on top of
0738: // * @param instant the instant to start from and to use for missing fields
0739: // * @return the merged instant
0740: // */
0741: // public long merge(ReadablePartial main, ReadablePartial base, long instant) {
0742: // DateTimeZone zone = main.getChronology().getZone();
0743: // instant = base.getChronology().withZone(zone).set(base, instant);
0744: // return set(main, instant);
0745: // }
0746: //
0747: // //-----------------------------------------------------------------------
0748: // /**
0749: // * Converts this object to a DateTime using a YearMonthDay to fill in the
0750: // * missing fields and using the default time zone.
0751: // * This instance is immutable and unaffected by this method call.
0752: // * <p>
0753: // * The resulting chronology is determined by the chronology of this
0754: // * TimeOfDay plus the time zone.
0755: // * <p>
0756: // * This method makes use of the chronology of the specified YearMonthDay
0757: // * in the calculation. This can be significant when mixing chronologies.
0758: // * If the YearMonthDay is in the same chronology as this instance the
0759: // * method will perform exactly as you might expect.
0760: // * <p>
0761: // * If the chronologies differ, then both this TimeOfDay and the YearMonthDay
0762: // * are converted to the destination chronology and then merged. As a result
0763: // * it may be the case that the year, monthOfYear and dayOfMonth fields on
0764: // * the result are different from the values returned by the methods on the
0765: // * YearMonthDay.
0766: // * <p>
0767: // * See {@link DateTime#withFields(ReadablePartial)} for an algorithm that
0768: // * ignores the chronology.
0769: // *
0770: // * @param date the date to use, null means today
0771: // * @return the DateTime instance
0772: // */
0773: // public DateTime toDateTime(YearMonthDay date) {
0774: // return toDateTime(date, null);
0775: // }
0776: //
0777: // /**
0778: // * Converts this object to a DateTime using a YearMonthDay to fill in the
0779: // * missing fields.
0780: // * This instance is immutable and unaffected by this method call.
0781: // * <p>
0782: // * The resulting chronology is determined by the chronology of this
0783: // * TimeOfDay plus the time zone.
0784: // * <p>
0785: // * This method makes use of the chronology of the specified YearMonthDay
0786: // * in the calculation. This can be significant when mixing chronologies.
0787: // * If the YearMonthDay is in the same chronology as this instance the
0788: // * method will perform exactly as you might expect.
0789: // * <p>
0790: // * If the chronologies differ, then both this TimeOfDay and the YearMonthDay
0791: // * are converted to the destination chronology and then merged. As a result
0792: // * it may be the case that the year, monthOfYear and dayOfMonth fields on
0793: // * the result are different from the values returned by the methods on the
0794: // * YearMonthDay.
0795: // * <p>
0796: // * See {@link DateTime#withFields(ReadablePartial)} for an algorithm that
0797: // * ignores the chronology and just assigns the fields.
0798: // *
0799: // * @param date the date to use, null means today
0800: // * @param zone the zone to get the DateTime in, null means default
0801: // * @return the DateTime instance
0802: // */
0803: // public DateTime toDateTime(YearMonthDay date, DateTimeZone zone) {
0804: // Chronology chrono = getChronology().withZone(zone);
0805: // if (date == null) {
0806: // DateTime dt = new DateTime(chrono);
0807: // return dt.withFields(this);
0808: // } else {
0809: // long millis = chrono.merge(this, date, DateTimeUtils.currentTimeMillis());
0810: // return new DateTime(millis, chrono);
0811: // }
0812: // }
0813: //
0814: // //-----------------------------------------------------------------------
0815: // public void testToDateTime_YMD() {
0816: // TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
0817: // YearMonthDay ymd = new YearMonthDay(new DateMidnight(2004, 6, 9), BUDDHIST_TOKYO);
0818: //
0819: // DateTime test = base.toDateTime(ymd);
0820: // check(base, 10, 20, 30, 40);
0821: // DateTime expected = new DateTime(ymd.toDateMidnight(LONDON), COPTIC_LONDON);
0822: // expected = expected.hourOfDay().setCopy(10);
0823: // expected = expected.minuteOfHour().setCopy(20);
0824: // expected = expected.secondOfMinute().setCopy(30);
0825: // expected = expected.millisOfSecond().setCopy(40);
0826: // assertEquals(expected, test);
0827: // }
0828: //
0829: // public void testToDateTime_nullYMD() {
0830: // TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
0831: //
0832: // DateTime test = base.toDateTime((YearMonthDay) null);
0833: // check(base, 10, 20, 30, 40);
0834: // DateTime expected = new DateTime(COPTIC_LONDON);
0835: // expected = expected.hourOfDay().setCopy(10);
0836: // expected = expected.minuteOfHour().setCopy(20);
0837: // expected = expected.secondOfMinute().setCopy(30);
0838: // expected = expected.millisOfSecond().setCopy(40);
0839: // assertEquals(expected, test);
0840: // }
0841: //
0842: // //-----------------------------------------------------------------------
0843: // public void testToDateTime_YMD_Zone() {
0844: // TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
0845: // YearMonthDay ymd = new YearMonthDay(new DateMidnight(2004, 6, 9), BUDDHIST_LONDON);
0846: //
0847: // DateTime test = base.toDateTime(ymd, TOKYO);
0848: // check(base, 10, 20, 30, 40);
0849: // DateTime expected = new DateTime(ymd.toDateMidnight(TOKYO), COPTIC_TOKYO);
0850: // expected = expected.hourOfDay().setCopy(10);
0851: // expected = expected.minuteOfHour().setCopy(20);
0852: // expected = expected.secondOfMinute().setCopy(30);
0853: // expected = expected.millisOfSecond().setCopy(40);
0854: // assertEquals(expected, test);
0855: // }
0856: //
0857: // public void testToDateTime_YMD_nullZone() {
0858: // TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
0859: // YearMonthDay ymd = new YearMonthDay(new DateMidnight(2004, 6, 9), BUDDHIST_LONDON);
0860: //
0861: // DateTime test = base.toDateTime(ymd, null);
0862: // check(base, 10, 20, 30, 40);
0863: // DateTime expected = new DateTime(ymd.toDateMidnight(LONDON), COPTIC_LONDON);
0864: // expected = expected.hourOfDay().setCopy(10);
0865: // expected = expected.minuteOfHour().setCopy(20);
0866: // expected = expected.secondOfMinute().setCopy(30);
0867: // expected = expected.millisOfSecond().setCopy(40);
0868: // assertEquals(expected, test);
0869: // }
0870: //
0871: // public void testToDateTime_nullYMD_Zone() {
0872: // TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); // PARIS irrelevant
0873: //
0874: // DateTime test = base.toDateTime((YearMonthDay) null, TOKYO);
0875: // check(base, 10, 20, 30, 40);
0876: // DateTime expected = new DateTime(COPTIC_TOKYO);
0877: // expected = expected.hourOfDay().setCopy(10);
0878: // expected = expected.minuteOfHour().setCopy(20);
0879: // expected = expected.secondOfMinute().setCopy(30);
0880: // expected = expected.millisOfSecond().setCopy(40);
0881: // assertEquals(expected, test);
0882: // }
0883:
0884: //-----------------------------------------------------------------------
0885: public void testToDateTime_RI() {
0886: TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS);
0887: DateTime dt = new DateTime(0L); // LONDON zone
0888: assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString());
0889:
0890: DateTime test = base.toDateTime(dt);
0891: check(base, 10, 20, 30, 40);
0892: assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString());
0893: assertEquals("1970-01-01T10:20:30.040+01:00", test.toString());
0894: }
0895:
0896: public void testToDateTime_nullRI() {
0897: TimeOfDay base = new TimeOfDay(1, 2, 3, 4);
0898: DateTimeUtils.setCurrentMillisFixed(TEST_TIME2);
0899:
0900: DateTime test = base.toDateTime((ReadableInstant) null);
0901: check(base, 1, 2, 3, 4);
0902: assertEquals("1970-01-02T01:02:03.004+01:00", test.toString());
0903: }
0904:
0905: //-----------------------------------------------------------------------
0906: public void testWithers() {
0907: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0908: check(test.withHourOfDay(6), 6, 20, 30, 40);
0909: check(test.withMinuteOfHour(6), 10, 6, 30, 40);
0910: check(test.withSecondOfMinute(6), 10, 20, 6, 40);
0911: check(test.withMillisOfSecond(6), 10, 20, 30, 6);
0912: try {
0913: test.withHourOfDay(-1);
0914: fail();
0915: } catch (IllegalArgumentException ex) {
0916: }
0917: try {
0918: test.withHourOfDay(24);
0919: fail();
0920: } catch (IllegalArgumentException ex) {
0921: }
0922: }
0923:
0924: //-----------------------------------------------------------------------
0925: public void testProperty() {
0926: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0927: assertEquals(test.hourOfDay(), test.property(DateTimeFieldType
0928: .hourOfDay()));
0929: assertEquals(test.minuteOfHour(), test
0930: .property(DateTimeFieldType.minuteOfHour()));
0931: assertEquals(test.secondOfMinute(), test
0932: .property(DateTimeFieldType.secondOfMinute()));
0933: assertEquals(test.millisOfSecond(), test
0934: .property(DateTimeFieldType.millisOfSecond()));
0935: try {
0936: test.property(DateTimeFieldType.millisOfDay());
0937: fail();
0938: } catch (IllegalArgumentException ex) {
0939: }
0940: try {
0941: test.property(null);
0942: fail();
0943: } catch (IllegalArgumentException ex) {
0944: }
0945: }
0946:
0947: //-----------------------------------------------------------------------
0948: public void testSerialization() throws Exception {
0949: TimeOfDay test = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS);
0950:
0951: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0952: ObjectOutputStream oos = new ObjectOutputStream(baos);
0953: oos.writeObject(test);
0954: byte[] bytes = baos.toByteArray();
0955: oos.close();
0956:
0957: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0958: ObjectInputStream ois = new ObjectInputStream(bais);
0959: TimeOfDay result = (TimeOfDay) ois.readObject();
0960: ois.close();
0961:
0962: assertEquals(test, result);
0963: assertTrue(Arrays.equals(test.getValues(), result.getValues()));
0964: assertTrue(Arrays.equals(test.getFields(), result.getFields()));
0965: assertEquals(test.getChronology(), result.getChronology());
0966: }
0967:
0968: //-----------------------------------------------------------------------
0969: public void testToString() {
0970: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0971: assertEquals("T10:20:30.040", test.toString());
0972: }
0973:
0974: //-----------------------------------------------------------------------
0975: public void testToString_String() {
0976: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0977: assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test
0978: .toString("yyyy HH"));
0979: assertEquals("T10:20:30.040", test.toString((String) null));
0980: }
0981:
0982: //-----------------------------------------------------------------------
0983: public void testToString_String_Locale() {
0984: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0985: assertEquals("10 20", test.toString("H m", Locale.ENGLISH));
0986: assertEquals("T10:20:30.040", test.toString(null,
0987: Locale.ENGLISH));
0988: assertEquals("10 20", test.toString("H m", null));
0989: assertEquals("T10:20:30.040", test.toString(null, null));
0990: }
0991:
0992: //-----------------------------------------------------------------------
0993: public void testToString_DTFormatter() {
0994: TimeOfDay test = new TimeOfDay(10, 20, 30, 40);
0995: assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test
0996: .toString(DateTimeFormat.forPattern("yyyy HH")));
0997: assertEquals("T10:20:30.040", test
0998: .toString((DateTimeFormatter) null));
0999: }
1000:
1001: //-----------------------------------------------------------------------
1002: private void check(TimeOfDay test, int hour, int min, int sec,
1003: int milli) {
1004: assertEquals(hour, test.getHourOfDay());
1005: assertEquals(min, test.getMinuteOfHour());
1006: assertEquals(sec, test.getSecondOfMinute());
1007: assertEquals(milli, test.getMillisOfSecond());
1008: }
1009: }
|