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.Calendar;
0023: import java.util.Date;
0024: import java.util.GregorianCalendar;
0025: import java.util.Locale;
0026: import java.util.TimeZone;
0027:
0028: import junit.framework.TestCase;
0029: import junit.framework.TestSuite;
0030:
0031: import org.joda.time.base.AbstractInstant;
0032: import org.joda.time.chrono.BaseChronology;
0033: import org.joda.time.chrono.BuddhistChronology;
0034: import org.joda.time.chrono.CopticChronology;
0035: import org.joda.time.chrono.GJChronology;
0036: import org.joda.time.chrono.GregorianChronology;
0037: import org.joda.time.chrono.ISOChronology;
0038: import org.joda.time.field.UnsupportedDateTimeField;
0039: import org.joda.time.field.UnsupportedDurationField;
0040: import org.joda.time.format.DateTimeFormat;
0041: import org.joda.time.format.DateTimeFormatter;
0042:
0043: /**
0044: * This class is a Junit unit test for DateTime.
0045: *
0046: * @author Stephen Colebourne
0047: */
0048: public class TestDateTime_Basics extends TestCase {
0049: // Test in 2002/03 as time zones are more well known
0050: // (before the late 90's they were all over the place)
0051:
0052: private static final DateTimeZone PARIS = DateTimeZone
0053: .forID("Europe/Paris");
0054: private static final DateTimeZone LONDON = DateTimeZone
0055: .forID("Europe/London");
0056:
0057: // the default time zone is set to LONDON in setUp()
0058: // we have to hard code LONDON here (instead of ISOChronology.getInstance() etc.)
0059: // as TestAll sets up a different time zone for better all-round testing
0060: private static final ISOChronology ISO_UTC = ISOChronology
0061: .getInstanceUTC();
0062: private static final ISOChronology ISO_DEFAULT = ISOChronology
0063: .getInstance(LONDON);
0064: private static final ISOChronology ISO_PARIS = ISOChronology
0065: .getInstance(PARIS);
0066: private static final GJChronology GJ_DEFAULT = GJChronology
0067: .getInstance(LONDON);
0068: private static final GregorianChronology GREGORIAN_DEFAULT = GregorianChronology
0069: .getInstance(LONDON);
0070: private static final GregorianChronology GREGORIAN_PARIS = GregorianChronology
0071: .getInstance(PARIS);
0072: private static final BuddhistChronology BUDDHIST_UTC = BuddhistChronology
0073: .getInstanceUTC();
0074: private static final BuddhistChronology BUDDHIST_DEFAULT = BuddhistChronology
0075: .getInstance(LONDON);
0076: private static final CopticChronology COPTIC_DEFAULT = CopticChronology
0077: .getInstance(LONDON);
0078:
0079: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0080: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0081: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0082: + 365 + 365 + 366 + 365;
0083: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0084: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0085: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0086: + 365 + 365 + 366 + 365 + 365;
0087:
0088: // 2002-06-09
0089: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
0090: + 31L + 9L - 1L)
0091: * DateTimeConstants.MILLIS_PER_DAY;
0092:
0093: // 2002-04-05
0094: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
0095: * DateTimeConstants.MILLIS_PER_DAY + 12L
0096: * DateTimeConstants.MILLIS_PER_HOUR + 24L
0097: * DateTimeConstants.MILLIS_PER_MINUTE;
0098:
0099: // 2003-05-06
0100: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
0101: * DateTimeConstants.MILLIS_PER_DAY
0102: + 14L
0103: * DateTimeConstants.MILLIS_PER_HOUR
0104: + 28L
0105: * DateTimeConstants.MILLIS_PER_MINUTE;
0106:
0107: private DateTimeZone originalDateTimeZone = null;
0108: private TimeZone originalTimeZone = null;
0109: private Locale originalLocale = null;
0110:
0111: public static void main(String[] args) {
0112: junit.textui.TestRunner.run(suite());
0113: }
0114:
0115: public static TestSuite suite() {
0116: return new TestSuite(TestDateTime_Basics.class);
0117: }
0118:
0119: public TestDateTime_Basics(String name) {
0120: super (name);
0121: }
0122:
0123: protected void setUp() throws Exception {
0124: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
0125: originalDateTimeZone = DateTimeZone.getDefault();
0126: originalTimeZone = TimeZone.getDefault();
0127: originalLocale = Locale.getDefault();
0128: DateTimeZone.setDefault(LONDON);
0129: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
0130: Locale.setDefault(Locale.UK);
0131: }
0132:
0133: protected void tearDown() throws Exception {
0134: DateTimeUtils.setCurrentMillisSystem();
0135: DateTimeZone.setDefault(originalDateTimeZone);
0136: TimeZone.setDefault(originalTimeZone);
0137: Locale.setDefault(originalLocale);
0138: originalDateTimeZone = null;
0139: originalTimeZone = null;
0140: originalLocale = null;
0141: }
0142:
0143: //-----------------------------------------------------------------------
0144: public void testTest() {
0145: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
0146: TEST_TIME_NOW).toString());
0147: assertEquals("2002-04-05T12:24:00.000Z",
0148: new Instant(TEST_TIME1).toString());
0149: assertEquals("2003-05-06T14:28:00.000Z",
0150: new Instant(TEST_TIME2).toString());
0151: }
0152:
0153: //-----------------------------------------------------------------------
0154: public void testGet_DateTimeField() {
0155: DateTime test = new DateTime();
0156: assertEquals(1, test.get(ISO_DEFAULT.era()));
0157: assertEquals(20, test.get(ISO_DEFAULT.centuryOfEra()));
0158: assertEquals(2, test.get(ISO_DEFAULT.yearOfCentury()));
0159: assertEquals(2002, test.get(ISO_DEFAULT.yearOfEra()));
0160: assertEquals(2002, test.get(ISO_DEFAULT.year()));
0161: assertEquals(6, test.get(ISO_DEFAULT.monthOfYear()));
0162: assertEquals(9, test.get(ISO_DEFAULT.dayOfMonth()));
0163: assertEquals(2002, test.get(ISO_DEFAULT.weekyear()));
0164: assertEquals(23, test.get(ISO_DEFAULT.weekOfWeekyear()));
0165: assertEquals(7, test.get(ISO_DEFAULT.dayOfWeek()));
0166: assertEquals(160, test.get(ISO_DEFAULT.dayOfYear()));
0167: assertEquals(0, test.get(ISO_DEFAULT.halfdayOfDay()));
0168: assertEquals(1, test.get(ISO_DEFAULT.hourOfHalfday()));
0169: assertEquals(1, test.get(ISO_DEFAULT.clockhourOfDay()));
0170: assertEquals(1, test.get(ISO_DEFAULT.clockhourOfHalfday()));
0171: assertEquals(1, test.get(ISO_DEFAULT.hourOfDay()));
0172: assertEquals(0, test.get(ISO_DEFAULT.minuteOfHour()));
0173: assertEquals(60, test.get(ISO_DEFAULT.minuteOfDay()));
0174: assertEquals(0, test.get(ISO_DEFAULT.secondOfMinute()));
0175: assertEquals(60 * 60, test.get(ISO_DEFAULT.secondOfDay()));
0176: assertEquals(0, test.get(ISO_DEFAULT.millisOfSecond()));
0177: assertEquals(60 * 60 * 1000, test
0178: .get(ISO_DEFAULT.millisOfDay()));
0179: try {
0180: test.get((DateTimeField) null);
0181: fail();
0182: } catch (IllegalArgumentException ex) {
0183: }
0184: }
0185:
0186: public void testGet_DateTimeFieldType() {
0187: DateTime test = new DateTime();
0188: assertEquals(1, test.get(DateTimeFieldType.era()));
0189: assertEquals(20, test.get(DateTimeFieldType.centuryOfEra()));
0190: assertEquals(2, test.get(DateTimeFieldType.yearOfCentury()));
0191: assertEquals(2002, test.get(DateTimeFieldType.yearOfEra()));
0192: assertEquals(2002, test.get(DateTimeFieldType.year()));
0193: assertEquals(6, test.get(DateTimeFieldType.monthOfYear()));
0194: assertEquals(9, test.get(DateTimeFieldType.dayOfMonth()));
0195: assertEquals(2002, test.get(DateTimeFieldType.weekyear()));
0196: assertEquals(23, test.get(DateTimeFieldType.weekOfWeekyear()));
0197: assertEquals(7, test.get(DateTimeFieldType.dayOfWeek()));
0198: assertEquals(160, test.get(DateTimeFieldType.dayOfYear()));
0199: assertEquals(0, test.get(DateTimeFieldType.halfdayOfDay()));
0200: assertEquals(1, test.get(DateTimeFieldType.hourOfHalfday()));
0201: assertEquals(1, test.get(DateTimeFieldType.clockhourOfDay()));
0202: assertEquals(1, test
0203: .get(DateTimeFieldType.clockhourOfHalfday()));
0204: assertEquals(1, test.get(DateTimeFieldType.hourOfDay()));
0205: assertEquals(0, test.get(DateTimeFieldType.minuteOfHour()));
0206: assertEquals(60, test.get(DateTimeFieldType.minuteOfDay()));
0207: assertEquals(0, test.get(DateTimeFieldType.secondOfMinute()));
0208: assertEquals(60 * 60, test.get(DateTimeFieldType.secondOfDay()));
0209: assertEquals(0, test.get(DateTimeFieldType.millisOfSecond()));
0210: assertEquals(60 * 60 * 1000, test.get(DateTimeFieldType
0211: .millisOfDay()));
0212: try {
0213: test.get((DateTimeFieldType) null);
0214: fail();
0215: } catch (IllegalArgumentException ex) {
0216: }
0217: }
0218:
0219: public void testIsSupported_DateTimeFieldType() {
0220: DateTime test = new DateTime();
0221: assertEquals(true, test.isSupported(DateTimeFieldType.era()));
0222: assertEquals(true, test.isSupported(DateTimeFieldType
0223: .centuryOfEra()));
0224: assertEquals(true, test.isSupported(DateTimeFieldType
0225: .yearOfCentury()));
0226: assertEquals(true, test.isSupported(DateTimeFieldType
0227: .yearOfEra()));
0228: assertEquals(true, test.isSupported(DateTimeFieldType.year()));
0229: assertEquals(true, test.isSupported(DateTimeFieldType
0230: .monthOfYear()));
0231: assertEquals(true, test.isSupported(DateTimeFieldType
0232: .dayOfMonth()));
0233: assertEquals(true, test.isSupported(DateTimeFieldType
0234: .weekyear()));
0235: assertEquals(true, test.isSupported(DateTimeFieldType
0236: .weekOfWeekyear()));
0237: assertEquals(true, test.isSupported(DateTimeFieldType
0238: .dayOfWeek()));
0239: assertEquals(true, test.isSupported(DateTimeFieldType
0240: .dayOfYear()));
0241: assertEquals(true, test.isSupported(DateTimeFieldType
0242: .halfdayOfDay()));
0243: assertEquals(true, test.isSupported(DateTimeFieldType
0244: .hourOfHalfday()));
0245: assertEquals(true, test.isSupported(DateTimeFieldType
0246: .clockhourOfDay()));
0247: assertEquals(true, test.isSupported(DateTimeFieldType
0248: .clockhourOfHalfday()));
0249: assertEquals(true, test.isSupported(DateTimeFieldType
0250: .hourOfDay()));
0251: assertEquals(true, test.isSupported(DateTimeFieldType
0252: .minuteOfHour()));
0253: assertEquals(true, test.isSupported(DateTimeFieldType
0254: .minuteOfDay()));
0255: assertEquals(true, test.isSupported(DateTimeFieldType
0256: .secondOfMinute()));
0257: assertEquals(true, test.isSupported(DateTimeFieldType
0258: .secondOfDay()));
0259: assertEquals(true, test.isSupported(DateTimeFieldType
0260: .millisOfSecond()));
0261: assertEquals(true, test.isSupported(DateTimeFieldType
0262: .millisOfDay()));
0263: assertEquals(false, test.isSupported(null));
0264: }
0265:
0266: //-----------------------------------------------------------------------
0267: public void testGetters() {
0268: DateTime test = new DateTime();
0269:
0270: assertEquals(ISO_DEFAULT, test.getChronology());
0271: assertEquals(LONDON, test.getZone());
0272: assertEquals(TEST_TIME_NOW, test.getMillis());
0273:
0274: assertEquals(1, test.getEra());
0275: assertEquals(20, test.getCenturyOfEra());
0276: assertEquals(2, test.getYearOfCentury());
0277: assertEquals(2002, test.getYearOfEra());
0278: assertEquals(2002, test.getYear());
0279: assertEquals(6, test.getMonthOfYear());
0280: assertEquals(9, test.getDayOfMonth());
0281: assertEquals(2002, test.getWeekyear());
0282: assertEquals(23, test.getWeekOfWeekyear());
0283: assertEquals(7, test.getDayOfWeek());
0284: assertEquals(160, test.getDayOfYear());
0285: assertEquals(1, test.getHourOfDay());
0286: assertEquals(0, test.getMinuteOfHour());
0287: assertEquals(60, test.getMinuteOfDay());
0288: assertEquals(0, test.getSecondOfMinute());
0289: assertEquals(60 * 60, test.getSecondOfDay());
0290: assertEquals(0, test.getMillisOfSecond());
0291: assertEquals(60 * 60 * 1000, test.getMillisOfDay());
0292: }
0293:
0294: public void testWithers() {
0295: DateTime test = new DateTime(1970, 6, 9, 10, 20, 30, 40,
0296: GJ_DEFAULT);
0297: check(test.withYear(2000), 2000, 6, 9, 10, 20, 30, 40);
0298: check(test.withMonthOfYear(2), 1970, 2, 9, 10, 20, 30, 40);
0299: check(test.withDayOfMonth(2), 1970, 6, 2, 10, 20, 30, 40);
0300: check(test.withDayOfYear(6), 1970, 1, 6, 10, 20, 30, 40);
0301: check(test.withDayOfWeek(6), 1970, 6, 13, 10, 20, 30, 40);
0302: check(test.withWeekOfWeekyear(6), 1970, 2, 3, 10, 20, 30, 40);
0303: check(test.withWeekyear(1971), 1971, 6, 15, 10, 20, 30, 40);
0304: check(test.withYearOfCentury(60), 1960, 6, 9, 10, 20, 30, 40);
0305: check(test.withCenturyOfEra(21), 2070, 6, 9, 10, 20, 30, 40);
0306: check(test.withYearOfEra(1066), 1066, 6, 9, 10, 20, 30, 40);
0307: check(test.withEra(DateTimeConstants.BC), -1970, 6, 9, 10, 20,
0308: 30, 40);
0309: check(test.withHourOfDay(6), 1970, 6, 9, 6, 20, 30, 40);
0310: check(test.withMinuteOfHour(6), 1970, 6, 9, 10, 6, 30, 40);
0311: check(test.withSecondOfMinute(6), 1970, 6, 9, 10, 20, 6, 40);
0312: check(test.withMillisOfSecond(6), 1970, 6, 9, 10, 20, 30, 6);
0313: check(test.withMillisOfDay(61234), 1970, 6, 9, 0, 1, 1, 234);
0314:
0315: try {
0316: test.withMonthOfYear(0);
0317: fail();
0318: } catch (IllegalArgumentException ex) {
0319: }
0320: try {
0321: test.withMonthOfYear(13);
0322: fail();
0323: } catch (IllegalArgumentException ex) {
0324: }
0325: }
0326:
0327: //-----------------------------------------------------------------------
0328: public void testEqualsHashCode() {
0329: DateTime test1 = new DateTime(TEST_TIME1);
0330: DateTime test2 = new DateTime(TEST_TIME1);
0331: assertEquals(true, test1.equals(test2));
0332: assertEquals(true, test2.equals(test1));
0333: assertEquals(true, test1.equals(test1));
0334: assertEquals(true, test2.equals(test2));
0335: assertEquals(true, test1.hashCode() == test2.hashCode());
0336: assertEquals(true, test1.hashCode() == test1.hashCode());
0337: assertEquals(true, test2.hashCode() == test2.hashCode());
0338:
0339: DateTime test3 = new DateTime(TEST_TIME2);
0340: assertEquals(false, test1.equals(test3));
0341: assertEquals(false, test2.equals(test3));
0342: assertEquals(false, test3.equals(test1));
0343: assertEquals(false, test3.equals(test2));
0344: assertEquals(false, test1.hashCode() == test3.hashCode());
0345: assertEquals(false, test2.hashCode() == test3.hashCode());
0346:
0347: assertEquals(false, test1.equals("Hello"));
0348: assertEquals(true, test1.equals(new MockInstant()));
0349: assertEquals(false, test1.equals(new DateTime(TEST_TIME1,
0350: GREGORIAN_DEFAULT)));
0351: assertEquals(true, new DateTime(TEST_TIME1,
0352: new MockEqualsChronology()).equals(new DateTime(
0353: TEST_TIME1, new MockEqualsChronology())));
0354: assertEquals(false, new DateTime(TEST_TIME1,
0355: new MockEqualsChronology()).equals(new DateTime(
0356: TEST_TIME1, ISO_DEFAULT)));
0357: }
0358:
0359: class MockInstant extends AbstractInstant {
0360: public String toString() {
0361: return null;
0362: }
0363:
0364: public long getMillis() {
0365: return TEST_TIME1;
0366: }
0367:
0368: public Chronology getChronology() {
0369: return ISO_DEFAULT;
0370: }
0371: }
0372:
0373: class MockEqualsChronology extends BaseChronology {
0374: public boolean equals(Object obj) {
0375: return obj instanceof MockEqualsChronology;
0376: }
0377:
0378: public DateTimeZone getZone() {
0379: return null;
0380: }
0381:
0382: public Chronology withUTC() {
0383: return this ;
0384: }
0385:
0386: public Chronology withZone(DateTimeZone zone) {
0387: return this ;
0388: }
0389:
0390: public String toString() {
0391: return "";
0392: }
0393: }
0394:
0395: public void testCompareTo() {
0396: DateTime test1 = new DateTime(TEST_TIME1);
0397: DateTime test1a = new DateTime(TEST_TIME1);
0398: assertEquals(0, test1.compareTo(test1a));
0399: assertEquals(0, test1a.compareTo(test1));
0400: assertEquals(0, test1.compareTo(test1));
0401: assertEquals(0, test1a.compareTo(test1a));
0402:
0403: DateTime test2 = new DateTime(TEST_TIME2);
0404: assertEquals(-1, test1.compareTo(test2));
0405: assertEquals(+1, test2.compareTo(test1));
0406:
0407: DateTime test3 = new DateTime(TEST_TIME2, GREGORIAN_PARIS);
0408: assertEquals(-1, test1.compareTo(test3));
0409: assertEquals(+1, test3.compareTo(test1));
0410: assertEquals(0, test3.compareTo(test2));
0411:
0412: assertEquals(+1, test2.compareTo(new MockInstant()));
0413: assertEquals(0, test1.compareTo(new MockInstant()));
0414:
0415: try {
0416: test1.compareTo(null);
0417: fail();
0418: } catch (NullPointerException ex) {
0419: }
0420: try {
0421: test1.compareTo(new Date());
0422: fail();
0423: } catch (ClassCastException ex) {
0424: }
0425: }
0426:
0427: //-----------------------------------------------------------------------
0428: public void testIsEqual_long() {
0429: assertEquals(false, new DateTime(TEST_TIME1)
0430: .isEqual(TEST_TIME2));
0431: assertEquals(true, new DateTime(TEST_TIME1).isEqual(TEST_TIME1));
0432: assertEquals(false, new DateTime(TEST_TIME2)
0433: .isEqual(TEST_TIME1));
0434: }
0435:
0436: public void testIsEqualNow() {
0437: assertEquals(false, new DateTime(TEST_TIME_NOW - 1)
0438: .isEqualNow());
0439: assertEquals(true, new DateTime(TEST_TIME_NOW).isEqualNow());
0440: assertEquals(false, new DateTime(TEST_TIME_NOW + 1)
0441: .isEqualNow());
0442: }
0443:
0444: public void testIsEqual_RI() {
0445: DateTime test1 = new DateTime(TEST_TIME1);
0446: DateTime test1a = new DateTime(TEST_TIME1);
0447: assertEquals(true, test1.isEqual(test1a));
0448: assertEquals(true, test1a.isEqual(test1));
0449: assertEquals(true, test1.isEqual(test1));
0450: assertEquals(true, test1a.isEqual(test1a));
0451:
0452: DateTime test2 = new DateTime(TEST_TIME2);
0453: assertEquals(false, test1.isEqual(test2));
0454: assertEquals(false, test2.isEqual(test1));
0455:
0456: DateTime test3 = new DateTime(TEST_TIME2, GREGORIAN_PARIS);
0457: assertEquals(false, test1.isEqual(test3));
0458: assertEquals(false, test3.isEqual(test1));
0459: assertEquals(true, test3.isEqual(test2));
0460:
0461: assertEquals(false, test2.isEqual(new MockInstant()));
0462: assertEquals(true, test1.isEqual(new MockInstant()));
0463:
0464: assertEquals(false, new DateTime(TEST_TIME_NOW + 1)
0465: .isEqual(null));
0466: assertEquals(true, new DateTime(TEST_TIME_NOW).isEqual(null));
0467: assertEquals(false, new DateTime(TEST_TIME_NOW - 1)
0468: .isEqual(null));
0469: }
0470:
0471: //-----------------------------------------------------------------------
0472: public void testIsBefore_long() {
0473: assertEquals(true, new DateTime(TEST_TIME1)
0474: .isBefore(TEST_TIME2));
0475: assertEquals(false, new DateTime(TEST_TIME1)
0476: .isBefore(TEST_TIME1));
0477: assertEquals(false, new DateTime(TEST_TIME2)
0478: .isBefore(TEST_TIME1));
0479: }
0480:
0481: public void testIsBeforeNow() {
0482: assertEquals(true, new DateTime(TEST_TIME_NOW - 1)
0483: .isBeforeNow());
0484: assertEquals(false, new DateTime(TEST_TIME_NOW).isBeforeNow());
0485: assertEquals(false, new DateTime(TEST_TIME_NOW + 1)
0486: .isBeforeNow());
0487: }
0488:
0489: public void testIsBefore_RI() {
0490: DateTime test1 = new DateTime(TEST_TIME1);
0491: DateTime test1a = new DateTime(TEST_TIME1);
0492: assertEquals(false, test1.isBefore(test1a));
0493: assertEquals(false, test1a.isBefore(test1));
0494: assertEquals(false, test1.isBefore(test1));
0495: assertEquals(false, test1a.isBefore(test1a));
0496:
0497: DateTime test2 = new DateTime(TEST_TIME2);
0498: assertEquals(true, test1.isBefore(test2));
0499: assertEquals(false, test2.isBefore(test1));
0500:
0501: DateTime test3 = new DateTime(TEST_TIME2, GREGORIAN_PARIS);
0502: assertEquals(true, test1.isBefore(test3));
0503: assertEquals(false, test3.isBefore(test1));
0504: assertEquals(false, test3.isBefore(test2));
0505:
0506: assertEquals(false, test2.isBefore(new MockInstant()));
0507: assertEquals(false, test1.isBefore(new MockInstant()));
0508:
0509: assertEquals(false, new DateTime(TEST_TIME_NOW + 1)
0510: .isBefore(null));
0511: assertEquals(false, new DateTime(TEST_TIME_NOW).isBefore(null));
0512: assertEquals(true, new DateTime(TEST_TIME_NOW - 1)
0513: .isBefore(null));
0514: }
0515:
0516: //-----------------------------------------------------------------------
0517: public void testIsAfter_long() {
0518: assertEquals(false, new DateTime(TEST_TIME1)
0519: .isAfter(TEST_TIME2));
0520: assertEquals(false, new DateTime(TEST_TIME1)
0521: .isAfter(TEST_TIME1));
0522: assertEquals(true, new DateTime(TEST_TIME2).isAfter(TEST_TIME1));
0523: }
0524:
0525: public void testIsAfterNow() {
0526: assertEquals(false, new DateTime(TEST_TIME_NOW - 1)
0527: .isAfterNow());
0528: assertEquals(false, new DateTime(TEST_TIME_NOW).isAfterNow());
0529: assertEquals(true, new DateTime(TEST_TIME_NOW + 1).isAfterNow());
0530: }
0531:
0532: public void testIsAfter_RI() {
0533: DateTime test1 = new DateTime(TEST_TIME1);
0534: DateTime test1a = new DateTime(TEST_TIME1);
0535: assertEquals(false, test1.isAfter(test1a));
0536: assertEquals(false, test1a.isAfter(test1));
0537: assertEquals(false, test1.isAfter(test1));
0538: assertEquals(false, test1a.isAfter(test1a));
0539:
0540: DateTime test2 = new DateTime(TEST_TIME2);
0541: assertEquals(false, test1.isAfter(test2));
0542: assertEquals(true, test2.isAfter(test1));
0543:
0544: DateTime test3 = new DateTime(TEST_TIME2, GREGORIAN_PARIS);
0545: assertEquals(false, test1.isAfter(test3));
0546: assertEquals(true, test3.isAfter(test1));
0547: assertEquals(false, test3.isAfter(test2));
0548:
0549: assertEquals(true, test2.isAfter(new MockInstant()));
0550: assertEquals(false, test1.isAfter(new MockInstant()));
0551:
0552: assertEquals(true, new DateTime(TEST_TIME_NOW + 1)
0553: .isAfter(null));
0554: assertEquals(false, new DateTime(TEST_TIME_NOW).isAfter(null));
0555: assertEquals(false, new DateTime(TEST_TIME_NOW - 1)
0556: .isAfter(null));
0557: }
0558:
0559: //-----------------------------------------------------------------------
0560: public void testSerialization() throws Exception {
0561: DateTime test = new DateTime(TEST_TIME_NOW);
0562:
0563: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0564: ObjectOutputStream oos = new ObjectOutputStream(baos);
0565: oos.writeObject(test);
0566: byte[] bytes = baos.toByteArray();
0567: oos.close();
0568:
0569: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0570: ObjectInputStream ois = new ObjectInputStream(bais);
0571: DateTime result = (DateTime) ois.readObject();
0572: ois.close();
0573:
0574: assertEquals(test, result);
0575: }
0576:
0577: //-----------------------------------------------------------------------
0578: public void testToString() {
0579: DateTime test = new DateTime(TEST_TIME_NOW);
0580: assertEquals("2002-06-09T01:00:00.000+01:00", test.toString());
0581:
0582: test = new DateTime(TEST_TIME_NOW, PARIS);
0583: assertEquals("2002-06-09T02:00:00.000+02:00", test.toString());
0584: }
0585:
0586: public void testToString_String() {
0587: DateTime test = new DateTime(TEST_TIME_NOW);
0588: assertEquals("2002 01", test.toString("yyyy HH"));
0589: assertEquals("2002-06-09T01:00:00.000+01:00", test
0590: .toString((String) null));
0591: }
0592:
0593: public void testToString_String_Locale() {
0594: DateTime test = new DateTime(TEST_TIME_NOW);
0595: assertEquals("Sun 9/6", test
0596: .toString("EEE d/M", Locale.ENGLISH));
0597: assertEquals("dim. 9/6", test
0598: .toString("EEE d/M", Locale.FRENCH));
0599: assertEquals("2002-06-09T01:00:00.000+01:00", test.toString(
0600: null, Locale.ENGLISH));
0601: assertEquals("Sun 9/6", test.toString("EEE d/M", null));
0602: assertEquals("2002-06-09T01:00:00.000+01:00", test.toString(
0603: null, null));
0604: }
0605:
0606: public void testToString_DTFormatter() {
0607: DateMidnight test = new DateMidnight(TEST_TIME_NOW);
0608: assertEquals("2002 00", test.toString(DateTimeFormat
0609: .forPattern("yyyy HH")));
0610: assertEquals("2002-06-09T00:00:00.000+01:00", test
0611: .toString((DateTimeFormatter) null));
0612: }
0613:
0614: //-----------------------------------------------------------------------
0615: public void testToInstant() {
0616: DateTime test = new DateTime(TEST_TIME1);
0617: Instant result = test.toInstant();
0618: assertEquals(TEST_TIME1, result.getMillis());
0619: }
0620:
0621: public void testToDateTime() {
0622: DateTime test = new DateTime(TEST_TIME1);
0623: DateTime result = test.toDateTime();
0624: assertSame(test, result);
0625: }
0626:
0627: public void testToDateTimeISO() {
0628: DateTime test = new DateTime(TEST_TIME1);
0629: DateTime result = test.toDateTimeISO();
0630: assertSame(test, result);
0631:
0632: test = new DateTime(TEST_TIME1, ISO_PARIS);
0633: result = test.toDateTimeISO();
0634: assertSame(DateTime.class, result.getClass());
0635: assertSame(ISOChronology.class, result.getChronology()
0636: .getClass());
0637: assertEquals(test.getMillis(), result.getMillis());
0638: assertEquals(ISO_PARIS, result.getChronology());
0639: assertNotSame(test, result);
0640:
0641: test = new DateTime(TEST_TIME1, BUDDHIST_DEFAULT);
0642: result = test.toDateTimeISO();
0643: assertSame(DateTime.class, result.getClass());
0644: assertSame(ISOChronology.class, result.getChronology()
0645: .getClass());
0646: assertEquals(test.getMillis(), result.getMillis());
0647: assertEquals(ISO_DEFAULT, result.getChronology());
0648: assertNotSame(test, result);
0649:
0650: test = new DateTime(TEST_TIME1, new MockNullZoneChronology());
0651: result = test.toDateTimeISO();
0652: assertSame(DateTime.class, result.getClass());
0653: assertSame(ISOChronology.class, result.getChronology()
0654: .getClass());
0655: assertEquals(test.getMillis(), result.getMillis());
0656: assertEquals(ISO_DEFAULT, result.getChronology());
0657: assertNotSame(test, result);
0658: }
0659:
0660: public void testToDateTime_DateTimeZone() {
0661: DateTime test = new DateTime(TEST_TIME1);
0662: DateTime result = test.toDateTime(LONDON);
0663: assertSame(test, result);
0664:
0665: test = new DateTime(TEST_TIME1);
0666: result = test.toDateTime(PARIS);
0667: assertEquals(test.getMillis(), result.getMillis());
0668: assertEquals(PARIS, result.getZone());
0669:
0670: test = new DateTime(TEST_TIME1, PARIS);
0671: result = test.toDateTime((DateTimeZone) null);
0672: assertEquals(test.getMillis(), result.getMillis());
0673: assertEquals(LONDON, result.getZone());
0674:
0675: test = new DateTime(TEST_TIME1);
0676: result = test.toDateTime((DateTimeZone) null);
0677: assertSame(test, result);
0678: }
0679:
0680: public void testToDateTime_Chronology() {
0681: DateTime test = new DateTime(TEST_TIME1);
0682: DateTime result = test.toDateTime(ISO_DEFAULT);
0683: assertSame(test, result);
0684:
0685: test = new DateTime(TEST_TIME1);
0686: result = test.toDateTime(GREGORIAN_PARIS);
0687: assertEquals(test.getMillis(), result.getMillis());
0688: assertEquals(GREGORIAN_PARIS, result.getChronology());
0689:
0690: test = new DateTime(TEST_TIME1, GREGORIAN_PARIS);
0691: result = test.toDateTime((Chronology) null);
0692: assertEquals(test.getMillis(), result.getMillis());
0693: assertEquals(ISO_DEFAULT, result.getChronology());
0694:
0695: test = new DateTime(TEST_TIME1);
0696: result = test.toDateTime((Chronology) null);
0697: assertSame(test, result);
0698: }
0699:
0700: public void testToMutableDateTime() {
0701: DateTime test = new DateTime(TEST_TIME1, PARIS);
0702: MutableDateTime result = test.toMutableDateTime();
0703: assertEquals(test.getMillis(), result.getMillis());
0704: assertEquals(ISO_PARIS, result.getChronology());
0705: }
0706:
0707: public void testToMutableDateTimeISO() {
0708: DateTime test = new DateTime(TEST_TIME1, PARIS);
0709: MutableDateTime result = test.toMutableDateTimeISO();
0710: assertSame(MutableDateTime.class, result.getClass());
0711: assertSame(ISOChronology.class, result.getChronology()
0712: .getClass());
0713: assertEquals(test.getMillis(), result.getMillis());
0714: assertEquals(ISO_PARIS, result.getChronology());
0715: }
0716:
0717: public void testToMutableDateTime_DateTimeZone() {
0718: DateTime test = new DateTime(TEST_TIME1);
0719: MutableDateTime result = test.toMutableDateTime(LONDON);
0720: assertEquals(test.getMillis(), result.getMillis());
0721: assertEquals(ISO_DEFAULT, result.getChronology());
0722:
0723: test = new DateTime(TEST_TIME1);
0724: result = test.toMutableDateTime(PARIS);
0725: assertEquals(test.getMillis(), result.getMillis());
0726: assertEquals(ISO_PARIS, result.getChronology());
0727:
0728: test = new DateTime(TEST_TIME1, PARIS);
0729: result = test.toMutableDateTime((DateTimeZone) null);
0730: assertEquals(test.getMillis(), result.getMillis());
0731: assertEquals(ISO_DEFAULT, result.getChronology());
0732:
0733: test = new DateTime(TEST_TIME1);
0734: result = test.toMutableDateTime((DateTimeZone) null);
0735: assertEquals(test.getMillis(), result.getMillis());
0736: assertEquals(ISO_DEFAULT, result.getChronology());
0737: }
0738:
0739: public void testToMutableDateTime_Chronology() {
0740: DateTime test = new DateTime(TEST_TIME1);
0741: MutableDateTime result = test.toMutableDateTime(ISO_DEFAULT);
0742: assertEquals(test.getMillis(), result.getMillis());
0743: assertEquals(ISO_DEFAULT, result.getChronology());
0744:
0745: test = new DateTime(TEST_TIME1);
0746: result = test.toMutableDateTime(GREGORIAN_PARIS);
0747: assertEquals(test.getMillis(), result.getMillis());
0748: assertEquals(GREGORIAN_PARIS, result.getChronology());
0749:
0750: test = new DateTime(TEST_TIME1, GREGORIAN_PARIS);
0751: result = test.toMutableDateTime((Chronology) null);
0752: assertEquals(test.getMillis(), result.getMillis());
0753: assertEquals(ISO_DEFAULT, result.getChronology());
0754:
0755: test = new DateTime(TEST_TIME1);
0756: result = test.toMutableDateTime((Chronology) null);
0757: assertEquals(test.getMillis(), result.getMillis());
0758: assertEquals(ISO_DEFAULT, result.getChronology());
0759: }
0760:
0761: public void testToDate() {
0762: DateTime test = new DateTime(TEST_TIME1);
0763: Date result = test.toDate();
0764: assertEquals(test.getMillis(), result.getTime());
0765: }
0766:
0767: public void testToCalendar_Locale() {
0768: DateTime test = new DateTime(TEST_TIME1);
0769: Calendar result = test.toCalendar(null);
0770: assertEquals(test.getMillis(), result.getTime().getTime());
0771: assertEquals(TimeZone.getTimeZone("Europe/London"), result
0772: .getTimeZone());
0773:
0774: test = new DateTime(TEST_TIME1, PARIS);
0775: result = test.toCalendar(null);
0776: assertEquals(test.getMillis(), result.getTime().getTime());
0777: assertEquals(TimeZone.getTimeZone("Europe/Paris"), result
0778: .getTimeZone());
0779:
0780: test = new DateTime(TEST_TIME1, PARIS);
0781: result = test.toCalendar(Locale.UK);
0782: assertEquals(test.getMillis(), result.getTime().getTime());
0783: assertEquals(TimeZone.getTimeZone("Europe/Paris"), result
0784: .getTimeZone());
0785: }
0786:
0787: public void testToGregorianCalendar() {
0788: DateTime test = new DateTime(TEST_TIME1);
0789: GregorianCalendar result = test.toGregorianCalendar();
0790: assertEquals(test.getMillis(), result.getTime().getTime());
0791: assertEquals(TimeZone.getTimeZone("Europe/London"), result
0792: .getTimeZone());
0793:
0794: test = new DateTime(TEST_TIME1, PARIS);
0795: result = test.toGregorianCalendar();
0796: assertEquals(test.getMillis(), result.getTime().getTime());
0797: assertEquals(TimeZone.getTimeZone("Europe/Paris"), result
0798: .getTimeZone());
0799: }
0800:
0801: //-----------------------------------------------------------------------
0802: public void testToDateMidnight() {
0803: DateTime base = new DateTime(TEST_TIME1, COPTIC_DEFAULT);
0804: DateMidnight test = base.toDateMidnight();
0805: assertEquals(new DateMidnight(base, COPTIC_DEFAULT), test);
0806: }
0807:
0808: public void testToYearMonthDay() {
0809: DateTime base = new DateTime(TEST_TIME1, COPTIC_DEFAULT);
0810: YearMonthDay test = base.toYearMonthDay();
0811: assertEquals(new YearMonthDay(TEST_TIME1, COPTIC_DEFAULT), test);
0812: }
0813:
0814: public void testToTimeOfDay() {
0815: DateTime base = new DateTime(TEST_TIME1, COPTIC_DEFAULT);
0816: TimeOfDay test = base.toTimeOfDay();
0817: assertEquals(new TimeOfDay(TEST_TIME1, COPTIC_DEFAULT), test);
0818: }
0819:
0820: public void testToLocalDateTime() {
0821: DateTime base = new DateTime(TEST_TIME1, COPTIC_DEFAULT);
0822: LocalDateTime test = base.toLocalDateTime();
0823: assertEquals(new LocalDateTime(TEST_TIME1, COPTIC_DEFAULT),
0824: test);
0825: }
0826:
0827: public void testToLocalDate() {
0828: DateTime base = new DateTime(TEST_TIME1, COPTIC_DEFAULT);
0829: LocalDate test = base.toLocalDate();
0830: assertEquals(new LocalDate(TEST_TIME1, COPTIC_DEFAULT), test);
0831: }
0832:
0833: public void testToLocalTime() {
0834: DateTime base = new DateTime(TEST_TIME1, COPTIC_DEFAULT);
0835: LocalTime test = base.toLocalTime();
0836: assertEquals(new LocalTime(TEST_TIME1, COPTIC_DEFAULT), test);
0837: }
0838:
0839: //-----------------------------------------------------------------------
0840: public void testWithMillis_long() {
0841: DateTime test = new DateTime(TEST_TIME1);
0842: DateTime result = test.withMillis(TEST_TIME2);
0843: assertEquals(TEST_TIME2, result.getMillis());
0844: assertEquals(test.getChronology(), result.getChronology());
0845:
0846: test = new DateTime(TEST_TIME1, GREGORIAN_PARIS);
0847: result = test.withMillis(TEST_TIME2);
0848: assertEquals(TEST_TIME2, result.getMillis());
0849: assertEquals(test.getChronology(), result.getChronology());
0850:
0851: test = new DateTime(TEST_TIME1);
0852: result = test.withMillis(TEST_TIME1);
0853: assertSame(test, result);
0854: }
0855:
0856: public void testWithChronology_Chronology() {
0857: DateTime test = new DateTime(TEST_TIME1);
0858: DateTime result = test.withChronology(GREGORIAN_PARIS);
0859: assertEquals(test.getMillis(), result.getMillis());
0860: assertEquals(GREGORIAN_PARIS, result.getChronology());
0861:
0862: test = new DateTime(TEST_TIME1, GREGORIAN_PARIS);
0863: result = test.withChronology(null);
0864: assertEquals(test.getMillis(), result.getMillis());
0865: assertEquals(ISO_DEFAULT, result.getChronology());
0866:
0867: test = new DateTime(TEST_TIME1);
0868: result = test.withChronology(null);
0869: assertEquals(test.getMillis(), result.getMillis());
0870: assertEquals(ISO_DEFAULT, result.getChronology());
0871:
0872: test = new DateTime(TEST_TIME1);
0873: result = test.withChronology(ISO_DEFAULT);
0874: assertSame(test, result);
0875: }
0876:
0877: public void testWithZone_DateTimeZone() {
0878: DateTime test = new DateTime(TEST_TIME1);
0879: DateTime result = test.withZone(PARIS);
0880: assertEquals(test.getMillis(), result.getMillis());
0881: assertEquals(ISO_PARIS, result.getChronology());
0882:
0883: test = new DateTime(TEST_TIME1, GREGORIAN_PARIS);
0884: result = test.withZone(null);
0885: assertEquals(test.getMillis(), result.getMillis());
0886: assertEquals(GREGORIAN_DEFAULT, result.getChronology());
0887:
0888: test = new DateTime(TEST_TIME1);
0889: result = test.withZone(null);
0890: assertSame(test, result);
0891: }
0892:
0893: public void testWithZoneRetainFields_DateTimeZone() {
0894: DateTime test = new DateTime(TEST_TIME1);
0895: DateTime result = test.withZoneRetainFields(PARIS);
0896: assertEquals(test.getMillis()
0897: - DateTimeConstants.MILLIS_PER_HOUR, result.getMillis());
0898: assertEquals(ISO_PARIS, result.getChronology());
0899:
0900: test = new DateTime(TEST_TIME1);
0901: result = test.withZoneRetainFields(LONDON);
0902: assertSame(test, result);
0903:
0904: test = new DateTime(TEST_TIME1);
0905: result = test.withZoneRetainFields(null);
0906: assertSame(test, result);
0907:
0908: test = new DateTime(TEST_TIME1, GREGORIAN_PARIS);
0909: result = test.withZoneRetainFields(null);
0910: assertEquals(test.getMillis()
0911: + DateTimeConstants.MILLIS_PER_HOUR, result.getMillis());
0912: assertEquals(GREGORIAN_DEFAULT, result.getChronology());
0913:
0914: test = new DateTime(TEST_TIME1, new MockNullZoneChronology());
0915: result = test.withZoneRetainFields(LONDON);
0916: assertSame(test, result);
0917: }
0918:
0919: //-----------------------------------------------------------------------
0920: public void testWithDate_int_int_int() {
0921: DateTime test = new DateTime(2002, 4, 5, 1, 2, 3, 4, ISO_UTC);
0922: DateTime result = test.withDate(2003, 5, 6);
0923: DateTime expected = new DateTime(2003, 5, 6, 1, 2, 3, 4,
0924: ISO_UTC);
0925: assertEquals(expected, result);
0926:
0927: test = new DateTime(TEST_TIME1);
0928: try {
0929: test.withDate(2003, 13, 1);
0930: fail();
0931: } catch (IllegalArgumentException ex) {
0932: }
0933: }
0934:
0935: public void testWithTime_int_int_int() {
0936: DateTime test = new DateTime(TEST_TIME1 - 12345L, BUDDHIST_UTC);
0937: DateTime result = test.withTime(12, 24, 0, 0);
0938: assertEquals(TEST_TIME1, result.getMillis());
0939: assertEquals(BUDDHIST_UTC, result.getChronology());
0940:
0941: test = new DateTime(TEST_TIME1);
0942: try {
0943: test.withTime(25, 1, 1, 1);
0944: fail();
0945: } catch (IllegalArgumentException ex) {
0946: }
0947: }
0948:
0949: public void testWithFields_RPartial() {
0950: DateTime test = new DateTime(2004, 5, 6, 7, 8, 9, 0);
0951: DateTime result = test.withFields(new YearMonthDay(2003, 4, 5));
0952: DateTime expected = new DateTime(2003, 4, 5, 7, 8, 9, 0);
0953: assertEquals(expected, result);
0954:
0955: test = new DateTime(TEST_TIME1);
0956: result = test.withFields(null);
0957: assertSame(test, result);
0958: }
0959:
0960: //-----------------------------------------------------------------------
0961: public void testWithField1() {
0962: DateTime test = new DateTime(2004, 6, 9, 0, 0, 0, 0);
0963: DateTime result = test
0964: .withField(DateTimeFieldType.year(), 2006);
0965:
0966: assertEquals(new DateTime(2004, 6, 9, 0, 0, 0, 0), test);
0967: assertEquals(new DateTime(2006, 6, 9, 0, 0, 0, 0), result);
0968: }
0969:
0970: public void testWithField2() {
0971: DateTime test = new DateTime(2004, 6, 9, 0, 0, 0, 0);
0972: try {
0973: test.withField(null, 6);
0974: fail();
0975: } catch (IllegalArgumentException ex) {
0976: }
0977: }
0978:
0979: //-----------------------------------------------------------------------
0980: public void testWithFieldAdded1() {
0981: DateTime test = new DateTime(2004, 6, 9, 0, 0, 0, 0);
0982: DateTime result = test.withFieldAdded(
0983: DurationFieldType.years(), 6);
0984:
0985: assertEquals(new DateTime(2004, 6, 9, 0, 0, 0, 0), test);
0986: assertEquals(new DateTime(2010, 6, 9, 0, 0, 0, 0), result);
0987: }
0988:
0989: public void testWithFieldAdded2() {
0990: DateTime test = new DateTime(2004, 6, 9, 0, 0, 0, 0);
0991: try {
0992: test.withFieldAdded(null, 0);
0993: fail();
0994: } catch (IllegalArgumentException ex) {
0995: }
0996: }
0997:
0998: public void testWithFieldAdded3() {
0999: DateTime test = new DateTime(2004, 6, 9, 0, 0, 0, 0);
1000: try {
1001: test.withFieldAdded(null, 6);
1002: fail();
1003: } catch (IllegalArgumentException ex) {
1004: }
1005: }
1006:
1007: public void testWithFieldAdded4() {
1008: DateTime test = new DateTime(2004, 6, 9, 0, 0, 0, 0);
1009: DateTime result = test.withFieldAdded(
1010: DurationFieldType.years(), 0);
1011: assertSame(test, result);
1012: }
1013:
1014: //-----------------------------------------------------------------------
1015: public void testWithDurationAdded_long_int() {
1016: DateTime test = new DateTime(TEST_TIME1, BUDDHIST_DEFAULT);
1017: DateTime result = test.withDurationAdded(123456789L, 1);
1018: DateTime expected = new DateTime(TEST_TIME1 + 123456789L,
1019: BUDDHIST_DEFAULT);
1020: assertEquals(expected, result);
1021:
1022: result = test.withDurationAdded(123456789L, 0);
1023: assertSame(test, result);
1024:
1025: result = test.withDurationAdded(123456789L, 2);
1026: expected = new DateTime(TEST_TIME1 + (2L * 123456789L),
1027: BUDDHIST_DEFAULT);
1028: assertEquals(expected, result);
1029:
1030: result = test.withDurationAdded(123456789L, -3);
1031: expected = new DateTime(TEST_TIME1 - (3L * 123456789L),
1032: BUDDHIST_DEFAULT);
1033: assertEquals(expected, result);
1034: }
1035:
1036: //-----------------------------------------------------------------------
1037: public void testWithDurationAdded_RD_int() {
1038: DateTime test = new DateTime(TEST_TIME1, BUDDHIST_DEFAULT);
1039: DateTime result = test.withDurationAdded(new Duration(
1040: 123456789L), 1);
1041: DateTime expected = new DateTime(TEST_TIME1 + 123456789L,
1042: BUDDHIST_DEFAULT);
1043: assertEquals(expected, result);
1044:
1045: result = test.withDurationAdded(null, 1);
1046: assertSame(test, result);
1047:
1048: result = test.withDurationAdded(new Duration(123456789L), 0);
1049: assertSame(test, result);
1050:
1051: result = test.withDurationAdded(new Duration(123456789L), 2);
1052: expected = new DateTime(TEST_TIME1 + (2L * 123456789L),
1053: BUDDHIST_DEFAULT);
1054: assertEquals(expected, result);
1055:
1056: result = test.withDurationAdded(new Duration(123456789L), -3);
1057: expected = new DateTime(TEST_TIME1 - (3L * 123456789L),
1058: BUDDHIST_DEFAULT);
1059: assertEquals(expected, result);
1060: }
1061:
1062: //-----------------------------------------------------------------------
1063: public void testWithDurationAdded_RP_int() {
1064: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1065: BUDDHIST_DEFAULT);
1066: DateTime result = test.withPeriodAdded(new Period(1, 2, 3, 4,
1067: 5, 6, 7, 8), 1);
1068: DateTime expected = new DateTime(2003, 7, 28, 6, 8, 10, 12,
1069: BUDDHIST_DEFAULT);
1070: assertEquals(expected, result);
1071:
1072: result = test.withPeriodAdded(null, 1);
1073: assertSame(test, result);
1074:
1075: result = test.withPeriodAdded(
1076: new Period(1, 2, 3, 4, 5, 6, 7, 8), 0);
1077: assertSame(test, result);
1078:
1079: result = test.withPeriodAdded(
1080: new Period(1, 2, 0, 4, 5, 6, 7, 8), 3);
1081: expected = new DateTime(2005, 11, 15, 16, 20, 24, 28,
1082: BUDDHIST_DEFAULT);
1083: assertEquals(expected, result);
1084:
1085: result = test.withPeriodAdded(
1086: new Period(1, 2, 0, 1, 1, 2, 3, 4), -1);
1087: expected = new DateTime(2001, 3, 2, 0, 0, 0, 0,
1088: BUDDHIST_DEFAULT);
1089: assertEquals(expected, result);
1090: }
1091:
1092: //-----------------------------------------------------------------------
1093: public void testPlus_long() {
1094: DateTime test = new DateTime(TEST_TIME1, BUDDHIST_DEFAULT);
1095: DateTime result = test.plus(123456789L);
1096: DateTime expected = new DateTime(TEST_TIME1 + 123456789L,
1097: BUDDHIST_DEFAULT);
1098: assertEquals(expected, result);
1099: }
1100:
1101: public void testPlus_RD() {
1102: DateTime test = new DateTime(TEST_TIME1, BUDDHIST_DEFAULT);
1103: DateTime result = test.plus(new Duration(123456789L));
1104: DateTime expected = new DateTime(TEST_TIME1 + 123456789L,
1105: BUDDHIST_DEFAULT);
1106: assertEquals(expected, result);
1107:
1108: result = test.plus((ReadableDuration) null);
1109: assertSame(test, result);
1110: }
1111:
1112: public void testPlus_RP() {
1113: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1114: BUDDHIST_DEFAULT);
1115: DateTime result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7, 8));
1116: DateTime expected = new DateTime(2003, 7, 28, 6, 8, 10, 12,
1117: BUDDHIST_DEFAULT);
1118: assertEquals(expected, result);
1119:
1120: result = test.plus((ReadablePeriod) null);
1121: assertSame(test, result);
1122: }
1123:
1124: public void testPlusYears_int() {
1125: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1126: BUDDHIST_DEFAULT);
1127: DateTime result = test.plusYears(1);
1128: DateTime expected = new DateTime(2003, 5, 3, 1, 2, 3, 4,
1129: BUDDHIST_DEFAULT);
1130: assertEquals(expected, result);
1131:
1132: result = test.plusYears(0);
1133: assertSame(test, result);
1134: }
1135:
1136: public void testPlusMonths_int() {
1137: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1138: BUDDHIST_DEFAULT);
1139: DateTime result = test.plusMonths(1);
1140: DateTime expected = new DateTime(2002, 6, 3, 1, 2, 3, 4,
1141: BUDDHIST_DEFAULT);
1142: assertEquals(expected, result);
1143:
1144: result = test.plusMonths(0);
1145: assertSame(test, result);
1146: }
1147:
1148: public void testPlusWeeks_int() {
1149: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1150: BUDDHIST_DEFAULT);
1151: DateTime result = test.plusWeeks(1);
1152: DateTime expected = new DateTime(2002, 5, 10, 1, 2, 3, 4,
1153: BUDDHIST_DEFAULT);
1154: assertEquals(expected, result);
1155:
1156: result = test.plusWeeks(0);
1157: assertSame(test, result);
1158: }
1159:
1160: public void testPlusDays_int() {
1161: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1162: BUDDHIST_DEFAULT);
1163: DateTime result = test.plusDays(1);
1164: DateTime expected = new DateTime(2002, 5, 4, 1, 2, 3, 4,
1165: BUDDHIST_DEFAULT);
1166: assertEquals(expected, result);
1167:
1168: result = test.plusDays(0);
1169: assertSame(test, result);
1170: }
1171:
1172: public void testPlusHours_int() {
1173: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1174: BUDDHIST_DEFAULT);
1175: DateTime result = test.plusHours(1);
1176: DateTime expected = new DateTime(2002, 5, 3, 2, 2, 3, 4,
1177: BUDDHIST_DEFAULT);
1178: assertEquals(expected, result);
1179:
1180: result = test.plusHours(0);
1181: assertSame(test, result);
1182: }
1183:
1184: public void testPlusMinutes_int() {
1185: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1186: BUDDHIST_DEFAULT);
1187: DateTime result = test.plusMinutes(1);
1188: DateTime expected = new DateTime(2002, 5, 3, 1, 3, 3, 4,
1189: BUDDHIST_DEFAULT);
1190: assertEquals(expected, result);
1191:
1192: result = test.plusMinutes(0);
1193: assertSame(test, result);
1194: }
1195:
1196: public void testPlusSeconds_int() {
1197: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1198: BUDDHIST_DEFAULT);
1199: DateTime result = test.plusSeconds(1);
1200: DateTime expected = new DateTime(2002, 5, 3, 1, 2, 4, 4,
1201: BUDDHIST_DEFAULT);
1202: assertEquals(expected, result);
1203:
1204: result = test.plusSeconds(0);
1205: assertSame(test, result);
1206: }
1207:
1208: public void testPlusMillis_int() {
1209: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1210: BUDDHIST_DEFAULT);
1211: DateTime result = test.plusMillis(1);
1212: DateTime expected = new DateTime(2002, 5, 3, 1, 2, 3, 5,
1213: BUDDHIST_DEFAULT);
1214: assertEquals(expected, result);
1215:
1216: result = test.plusMillis(0);
1217: assertSame(test, result);
1218: }
1219:
1220: //-----------------------------------------------------------------------
1221: public void testMinus_long() {
1222: DateTime test = new DateTime(TEST_TIME1, BUDDHIST_DEFAULT);
1223: DateTime result = test.minus(123456789L);
1224: DateTime expected = new DateTime(TEST_TIME1 - 123456789L,
1225: BUDDHIST_DEFAULT);
1226: assertEquals(expected, result);
1227: }
1228:
1229: public void testMinus_RD() {
1230: DateTime test = new DateTime(TEST_TIME1, BUDDHIST_DEFAULT);
1231: DateTime result = test.minus(new Duration(123456789L));
1232: DateTime expected = new DateTime(TEST_TIME1 - 123456789L,
1233: BUDDHIST_DEFAULT);
1234: assertEquals(expected, result);
1235:
1236: result = test.minus((ReadableDuration) null);
1237: assertSame(test, result);
1238: }
1239:
1240: public void testMinus_RP() {
1241: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1242: BUDDHIST_DEFAULT);
1243: DateTime result = test
1244: .minus(new Period(1, 1, 1, 1, 1, 1, 1, 1));
1245: DateTime expected = new DateTime(2001, 3, 26, 0, 1, 2, 3,
1246: BUDDHIST_DEFAULT);
1247: assertEquals(expected, result);
1248:
1249: result = test.minus((ReadablePeriod) null);
1250: assertSame(test, result);
1251: }
1252:
1253: public void testMinusYears_int() {
1254: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1255: BUDDHIST_DEFAULT);
1256: DateTime result = test.minusYears(1);
1257: DateTime expected = new DateTime(2001, 5, 3, 1, 2, 3, 4,
1258: BUDDHIST_DEFAULT);
1259: assertEquals(expected, result);
1260:
1261: result = test.minusYears(0);
1262: assertSame(test, result);
1263: }
1264:
1265: public void testMinusMonths_int() {
1266: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1267: BUDDHIST_DEFAULT);
1268: DateTime result = test.minusMonths(1);
1269: DateTime expected = new DateTime(2002, 4, 3, 1, 2, 3, 4,
1270: BUDDHIST_DEFAULT);
1271: assertEquals(expected, result);
1272:
1273: result = test.minusMonths(0);
1274: assertSame(test, result);
1275: }
1276:
1277: public void testMinusWeeks_int() {
1278: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1279: BUDDHIST_DEFAULT);
1280: DateTime result = test.minusWeeks(1);
1281: DateTime expected = new DateTime(2002, 4, 26, 1, 2, 3, 4,
1282: BUDDHIST_DEFAULT);
1283: assertEquals(expected, result);
1284:
1285: result = test.minusWeeks(0);
1286: assertSame(test, result);
1287: }
1288:
1289: public void testMinusDays_int() {
1290: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1291: BUDDHIST_DEFAULT);
1292: DateTime result = test.minusDays(1);
1293: DateTime expected = new DateTime(2002, 5, 2, 1, 2, 3, 4,
1294: BUDDHIST_DEFAULT);
1295: assertEquals(expected, result);
1296:
1297: result = test.minusDays(0);
1298: assertSame(test, result);
1299: }
1300:
1301: public void testMinusHours_int() {
1302: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1303: BUDDHIST_DEFAULT);
1304: DateTime result = test.minusHours(1);
1305: DateTime expected = new DateTime(2002, 5, 3, 0, 2, 3, 4,
1306: BUDDHIST_DEFAULT);
1307: assertEquals(expected, result);
1308:
1309: result = test.minusHours(0);
1310: assertSame(test, result);
1311: }
1312:
1313: public void testMinusMinutes_int() {
1314: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1315: BUDDHIST_DEFAULT);
1316: DateTime result = test.minusMinutes(1);
1317: DateTime expected = new DateTime(2002, 5, 3, 1, 1, 3, 4,
1318: BUDDHIST_DEFAULT);
1319: assertEquals(expected, result);
1320:
1321: result = test.minusMinutes(0);
1322: assertSame(test, result);
1323: }
1324:
1325: public void testMinusSeconds_int() {
1326: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1327: BUDDHIST_DEFAULT);
1328: DateTime result = test.minusSeconds(1);
1329: DateTime expected = new DateTime(2002, 5, 3, 1, 2, 2, 4,
1330: BUDDHIST_DEFAULT);
1331: assertEquals(expected, result);
1332:
1333: result = test.minusSeconds(0);
1334: assertSame(test, result);
1335: }
1336:
1337: public void testMinusMillis_int() {
1338: DateTime test = new DateTime(2002, 5, 3, 1, 2, 3, 4,
1339: BUDDHIST_DEFAULT);
1340: DateTime result = test.minusMillis(1);
1341: DateTime expected = new DateTime(2002, 5, 3, 1, 2, 3, 3,
1342: BUDDHIST_DEFAULT);
1343: assertEquals(expected, result);
1344:
1345: result = test.minusMillis(0);
1346: assertSame(test, result);
1347: }
1348:
1349: //-----------------------------------------------------------------------
1350: public void testProperty() {
1351: DateTime test = new DateTime();
1352: assertEquals(test.year(), test.property(DateTimeFieldType
1353: .year()));
1354: assertEquals(test.dayOfWeek(), test.property(DateTimeFieldType
1355: .dayOfWeek()));
1356: assertEquals(test.secondOfMinute(), test
1357: .property(DateTimeFieldType.secondOfMinute()));
1358: assertEquals(test.millisOfSecond(), test
1359: .property(DateTimeFieldType.millisOfSecond()));
1360: DateTimeFieldType bad = new DateTimeFieldType("bad") {
1361: public DurationFieldType getDurationType() {
1362: return DurationFieldType.weeks();
1363: }
1364:
1365: public DurationFieldType getRangeDurationType() {
1366: return null;
1367: }
1368:
1369: public DateTimeField getField(Chronology chronology) {
1370: return UnsupportedDateTimeField.getInstance(this ,
1371: UnsupportedDurationField
1372: .getInstance(getDurationType()));
1373: }
1374: };
1375: try {
1376: test.property(bad);
1377: fail();
1378: } catch (IllegalArgumentException ex) {
1379: }
1380: try {
1381: test.property(null);
1382: fail();
1383: } catch (IllegalArgumentException ex) {
1384: }
1385: }
1386:
1387: //-----------------------------------------------------------------------
1388: private void check(DateTime test, int year, int month, int day,
1389: int hour, int min, int sec, int mil) {
1390: assertEquals(year, test.getYear());
1391: assertEquals(month, test.getMonthOfYear());
1392: assertEquals(day, test.getDayOfMonth());
1393: assertEquals(hour, test.getHourOfDay());
1394: assertEquals(min, test.getMinuteOfHour());
1395: assertEquals(sec, test.getSecondOfMinute());
1396: assertEquals(mil, test.getMillisOfSecond());
1397: }
1398:
1399: }
|