0001: /*
0002: * Copyright 2001-2005 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.math.BigInteger;
0023: import java.util.Arrays;
0024: import java.util.Locale;
0025: import java.util.TimeZone;
0026:
0027: import junit.framework.TestCase;
0028: import junit.framework.TestSuite;
0029:
0030: import org.joda.time.base.BasePeriod;
0031: import org.joda.time.format.PeriodFormat;
0032: import org.joda.time.format.PeriodFormatter;
0033:
0034: /**
0035: * This class is a Junit unit test for Duration.
0036: *
0037: * @author Stephen Colebourne
0038: */
0039: public class TestPeriod_Basics extends TestCase {
0040: // Test in 2002/03 as time zones are more well known
0041: // (before the late 90's they were all over the place)
0042:
0043: //private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
0044: private static final DateTimeZone LONDON = DateTimeZone
0045: .forID("Europe/London");
0046:
0047: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0048: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0049: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0050: + 365 + 365 + 366 + 365;
0051: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0052: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0053: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0054: + 365 + 365 + 366 + 365 + 365;
0055:
0056: // 2002-06-09
0057: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
0058: + 31L + 9L - 1L)
0059: * DateTimeConstants.MILLIS_PER_DAY;
0060:
0061: // 2002-04-05
0062: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
0063: * DateTimeConstants.MILLIS_PER_DAY + 12L
0064: * DateTimeConstants.MILLIS_PER_HOUR + 24L
0065: * DateTimeConstants.MILLIS_PER_MINUTE;
0066:
0067: // 2003-05-06
0068: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
0069: * DateTimeConstants.MILLIS_PER_DAY
0070: + 14L
0071: * DateTimeConstants.MILLIS_PER_HOUR
0072: + 28L
0073: * DateTimeConstants.MILLIS_PER_MINUTE;
0074:
0075: private DateTimeZone originalDateTimeZone = null;
0076: private TimeZone originalTimeZone = null;
0077: private Locale originalLocale = null;
0078:
0079: public static void main(String[] args) {
0080: junit.textui.TestRunner.run(suite());
0081: }
0082:
0083: public static TestSuite suite() {
0084: return new TestSuite(TestPeriod_Basics.class);
0085: }
0086:
0087: public TestPeriod_Basics(String name) {
0088: super (name);
0089: }
0090:
0091: protected void setUp() throws Exception {
0092: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
0093: originalDateTimeZone = DateTimeZone.getDefault();
0094: originalTimeZone = TimeZone.getDefault();
0095: originalLocale = Locale.getDefault();
0096: DateTimeZone.setDefault(LONDON);
0097: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
0098: Locale.setDefault(Locale.UK);
0099: }
0100:
0101: protected void tearDown() throws Exception {
0102: DateTimeUtils.setCurrentMillisSystem();
0103: DateTimeZone.setDefault(originalDateTimeZone);
0104: TimeZone.setDefault(originalTimeZone);
0105: Locale.setDefault(originalLocale);
0106: originalDateTimeZone = null;
0107: originalTimeZone = null;
0108: originalLocale = null;
0109: }
0110:
0111: //-----------------------------------------------------------------------
0112: public void testTest() {
0113: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
0114: TEST_TIME_NOW).toString());
0115: assertEquals("2002-04-05T12:24:00.000Z",
0116: new Instant(TEST_TIME1).toString());
0117: assertEquals("2003-05-06T14:28:00.000Z",
0118: new Instant(TEST_TIME2).toString());
0119: }
0120:
0121: //-----------------------------------------------------------------------
0122: public void testGetPeriodType() {
0123: Period test = new Period(0L);
0124: assertEquals(PeriodType.standard(), test.getPeriodType());
0125: }
0126:
0127: public void testGetMethods() {
0128: Period test = new Period(0L);
0129: assertEquals(0, test.getYears());
0130: assertEquals(0, test.getMonths());
0131: assertEquals(0, test.getWeeks());
0132: assertEquals(0, test.getDays());
0133: assertEquals(0, test.getHours());
0134: assertEquals(0, test.getMinutes());
0135: assertEquals(0, test.getSeconds());
0136: assertEquals(0, test.getMillis());
0137: }
0138:
0139: public void testValueIndexMethods() {
0140: Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType
0141: .yearDayTime());
0142: assertEquals(6, test.size());
0143: assertEquals(1, test.getValue(0));
0144: assertEquals(4, test.getValue(1));
0145: assertEquals(5, test.getValue(2));
0146: assertEquals(6, test.getValue(3));
0147: assertEquals(7, test.getValue(4));
0148: assertEquals(8, test.getValue(5));
0149: assertEquals(true, Arrays.equals(
0150: new int[] { 1, 4, 5, 6, 7, 8 }, test.getValues()));
0151: }
0152:
0153: public void testTypeIndexMethods() {
0154: Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType
0155: .yearDayTime());
0156: assertEquals(6, test.size());
0157: assertEquals(DurationFieldType.years(), test.getFieldType(0));
0158: assertEquals(DurationFieldType.days(), test.getFieldType(1));
0159: assertEquals(DurationFieldType.hours(), test.getFieldType(2));
0160: assertEquals(DurationFieldType.minutes(), test.getFieldType(3));
0161: assertEquals(DurationFieldType.seconds(), test.getFieldType(4));
0162: assertEquals(DurationFieldType.millis(), test.getFieldType(5));
0163: assertEquals(true, Arrays.equals(
0164: new DurationFieldType[] { DurationFieldType.years(),
0165: DurationFieldType.days(),
0166: DurationFieldType.hours(),
0167: DurationFieldType.minutes(),
0168: DurationFieldType.seconds(),
0169: DurationFieldType.millis() }, test
0170: .getFieldTypes()));
0171: }
0172:
0173: public void testIsSupported() {
0174: Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType
0175: .yearDayTime());
0176: assertEquals(true, test.isSupported(DurationFieldType.years()));
0177: assertEquals(false, test
0178: .isSupported(DurationFieldType.months()));
0179: assertEquals(false, test.isSupported(DurationFieldType.weeks()));
0180: assertEquals(true, test.isSupported(DurationFieldType.days()));
0181: assertEquals(true, test.isSupported(DurationFieldType.hours()));
0182: assertEquals(true, test
0183: .isSupported(DurationFieldType.minutes()));
0184: assertEquals(true, test
0185: .isSupported(DurationFieldType.seconds()));
0186: assertEquals(true, test.isSupported(DurationFieldType.millis()));
0187: }
0188:
0189: public void testIndexOf() {
0190: Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType
0191: .yearDayTime());
0192: assertEquals(0, test.indexOf(DurationFieldType.years()));
0193: assertEquals(-1, test.indexOf(DurationFieldType.months()));
0194: assertEquals(-1, test.indexOf(DurationFieldType.weeks()));
0195: assertEquals(1, test.indexOf(DurationFieldType.days()));
0196: assertEquals(2, test.indexOf(DurationFieldType.hours()));
0197: assertEquals(3, test.indexOf(DurationFieldType.minutes()));
0198: assertEquals(4, test.indexOf(DurationFieldType.seconds()));
0199: assertEquals(5, test.indexOf(DurationFieldType.millis()));
0200: }
0201:
0202: public void testGet() {
0203: Period test = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType
0204: .yearDayTime());
0205: assertEquals(1, test.get(DurationFieldType.years()));
0206: assertEquals(0, test.get(DurationFieldType.months()));
0207: assertEquals(0, test.get(DurationFieldType.weeks()));
0208: assertEquals(4, test.get(DurationFieldType.days()));
0209: assertEquals(5, test.get(DurationFieldType.hours()));
0210: assertEquals(6, test.get(DurationFieldType.minutes()));
0211: assertEquals(7, test.get(DurationFieldType.seconds()));
0212: assertEquals(8, test.get(DurationFieldType.millis()));
0213: }
0214:
0215: public void testEqualsHashCode() {
0216: Period test1 = new Period(123L);
0217: Period test2 = new Period(123L);
0218: assertEquals(true, test1.equals(test2));
0219: assertEquals(true, test2.equals(test1));
0220: assertEquals(true, test1.equals(test1));
0221: assertEquals(true, test2.equals(test2));
0222: assertEquals(true, test1.hashCode() == test2.hashCode());
0223: assertEquals(true, test1.hashCode() == test1.hashCode());
0224: assertEquals(true, test2.hashCode() == test2.hashCode());
0225:
0226: Period test3 = new Period(321L);
0227: assertEquals(false, test1.equals(test3));
0228: assertEquals(false, test2.equals(test3));
0229: assertEquals(false, test3.equals(test1));
0230: assertEquals(false, test3.equals(test2));
0231: assertEquals(false, test1.hashCode() == test3.hashCode());
0232: assertEquals(false, test2.hashCode() == test3.hashCode());
0233:
0234: assertEquals(false, test1.equals("Hello"));
0235: assertEquals(true, test1.equals(new MockPeriod(123L)));
0236: assertEquals(false, test1.equals(new Period(123L, PeriodType
0237: .dayTime())));
0238: }
0239:
0240: class MockPeriod extends BasePeriod {
0241: public MockPeriod(long value) {
0242: super (value, null, null);
0243: }
0244: }
0245:
0246: //-----------------------------------------------------------------------
0247: public void testSerialization() throws Exception {
0248: Period test = new Period(123L);
0249:
0250: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0251: ObjectOutputStream oos = new ObjectOutputStream(baos);
0252: oos.writeObject(test);
0253: byte[] bytes = baos.toByteArray();
0254: oos.close();
0255:
0256: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0257: ObjectInputStream ois = new ObjectInputStream(bais);
0258: Period result = (Period) ois.readObject();
0259: ois.close();
0260:
0261: assertEquals(test, result);
0262: }
0263:
0264: // //-----------------------------------------------------------------------
0265: // public void testAddTo1() {
0266: // long expected = TEST_TIME_NOW;
0267: // expected = ISOChronology.getInstance().years().add(expected, 1);
0268: // expected = ISOChronology.getInstance().months().add(expected, 2);
0269: // expected = ISOChronology.getInstance().weeks().add(expected, 3);
0270: // expected = ISOChronology.getInstance().days().add(expected, 4);
0271: // expected = ISOChronology.getInstance().hours().add(expected, 5);
0272: // expected = ISOChronology.getInstance().minutes().add(expected, 6);
0273: // expected = ISOChronology.getInstance().seconds().add(expected, 7);
0274: // expected = ISOChronology.getInstance().millis().add(expected, 8);
0275: //
0276: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0277: // long added = test.addTo(TEST_TIME_NOW, 1);
0278: // assertEquals(expected, added);
0279: // }
0280: //
0281: // public void testAddTo2() {
0282: // long expected = TEST_TIME_NOW;
0283: // expected = ISOChronology.getInstance().years().add(expected, -2);
0284: // expected = ISOChronology.getInstance().months().add(expected, -4);
0285: // expected = ISOChronology.getInstance().weeks().add(expected, -6);
0286: // expected = ISOChronology.getInstance().days().add(expected, -8);
0287: // expected = ISOChronology.getInstance().hours().add(expected, -10);
0288: // expected = ISOChronology.getInstance().minutes().add(expected, -12);
0289: // expected = ISOChronology.getInstance().seconds().add(expected, -14);
0290: // expected = ISOChronology.getInstance().millis().add(expected, -16);
0291: //
0292: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0293: // long added = test.addTo(TEST_TIME_NOW, -2);
0294: // assertEquals(expected, added);
0295: // }
0296: //
0297: // public void testAddTo3() {
0298: // long expected = TEST_TIME_NOW;
0299: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0300: // long added = test.addTo(TEST_TIME_NOW, 0);
0301: // assertEquals(expected, added);
0302: // }
0303: //
0304: // public void testAddTo4() {
0305: // long expected = TEST_TIME_NOW + 100L;
0306: // Period test = new Period(100L);
0307: // long added = test.addTo(TEST_TIME_NOW, 1);
0308: // assertEquals(expected, added);
0309: // }
0310: //
0311: // //-----------------------------------------------------------------------
0312: // public void testAddToWithChronology1() {
0313: // long expected = TEST_TIME_NOW;
0314: // expected = ISOChronology.getInstance().years().add(expected, 1);
0315: // expected = ISOChronology.getInstance().months().add(expected, 2);
0316: // expected = ISOChronology.getInstance().weeks().add(expected, 3);
0317: // expected = ISOChronology.getInstance().days().add(expected, 4);
0318: // expected = ISOChronology.getInstance().hours().add(expected, 5);
0319: // expected = ISOChronology.getInstance().minutes().add(expected, 6);
0320: // expected = ISOChronology.getInstance().seconds().add(expected, 7);
0321: // expected = ISOChronology.getInstance().millis().add(expected, 8);
0322: //
0323: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0324: // long added = test.addTo(TEST_TIME_NOW, 1, ISOChronology.getInstance());
0325: // assertEquals(expected, added);
0326: // }
0327: //
0328: // public void testAddToWithChronology2() {
0329: // long expected = TEST_TIME_NOW;
0330: // expected = ISOChronology.getInstanceUTC().years().add(expected, -2);
0331: // expected = ISOChronology.getInstanceUTC().months().add(expected, -4);
0332: // expected = ISOChronology.getInstanceUTC().weeks().add(expected, -6);
0333: // expected = ISOChronology.getInstanceUTC().days().add(expected, -8);
0334: // expected = ISOChronology.getInstanceUTC().hours().add(expected, -10);
0335: // expected = ISOChronology.getInstanceUTC().minutes().add(expected, -12);
0336: // expected = ISOChronology.getInstanceUTC().seconds().add(expected, -14);
0337: // expected = ISOChronology.getInstanceUTC().millis().add(expected, -16);
0338: //
0339: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
0340: // long added = test.addTo(TEST_TIME_NOW, -2, ISOChronology.getInstanceUTC()); // local specified so use it
0341: // assertEquals(expected, added);
0342: // }
0343: //
0344: // public void testAddToWithChronology3() {
0345: // long expected = TEST_TIME_NOW;
0346: // expected = ISOChronology.getInstance().years().add(expected, -2);
0347: // expected = ISOChronology.getInstance().months().add(expected, -4);
0348: // expected = ISOChronology.getInstance().weeks().add(expected, -6);
0349: // expected = ISOChronology.getInstance().days().add(expected, -8);
0350: // expected = ISOChronology.getInstance().hours().add(expected, -10);
0351: // expected = ISOChronology.getInstance().minutes().add(expected, -12);
0352: // expected = ISOChronology.getInstance().seconds().add(expected, -14);
0353: // expected = ISOChronology.getInstance().millis().add(expected, -16);
0354: //
0355: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
0356: // long added = test.addTo(TEST_TIME_NOW, -2, null); // no chrono specified so use default
0357: // assertEquals(expected, added);
0358: // }
0359: //
0360: // //-----------------------------------------------------------------------
0361: // public void testAddToRI1() {
0362: // long expected = TEST_TIME_NOW;
0363: // expected = ISOChronology.getInstance().years().add(expected, 1);
0364: // expected = ISOChronology.getInstance().months().add(expected, 2);
0365: // expected = ISOChronology.getInstance().weeks().add(expected, 3);
0366: // expected = ISOChronology.getInstance().days().add(expected, 4);
0367: // expected = ISOChronology.getInstance().hours().add(expected, 5);
0368: // expected = ISOChronology.getInstance().minutes().add(expected, 6);
0369: // expected = ISOChronology.getInstance().seconds().add(expected, 7);
0370: // expected = ISOChronology.getInstance().millis().add(expected, 8);
0371: //
0372: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0373: // DateTime added = test.addTo(new Instant(), 1); // Instant has no time zone, use default
0374: // assertEquals(expected, added.getMillis());
0375: // assertEquals(ISOChronology.getInstance(), added.getChronology());
0376: // }
0377: //
0378: // public void testAddToRI2() {
0379: // long expected = TEST_TIME_NOW;
0380: // expected = ISOChronology.getInstance().years().add(expected, -2);
0381: // expected = ISOChronology.getInstance().months().add(expected, -4);
0382: // expected = ISOChronology.getInstance().weeks().add(expected, -6);
0383: // expected = ISOChronology.getInstance().days().add(expected, -8);
0384: // expected = ISOChronology.getInstance().hours().add(expected, -10);
0385: // expected = ISOChronology.getInstance().minutes().add(expected, -12);
0386: // expected = ISOChronology.getInstance().seconds().add(expected, -14);
0387: // expected = ISOChronology.getInstance().millis().add(expected, -16);
0388: //
0389: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
0390: // DateTime added = test.addTo(new Instant(), -2); // Instant has no time zone, use default
0391: // assertEquals(expected, added.getMillis());
0392: // assertEquals(ISOChronology.getInstance(), added.getChronology());
0393: // }
0394: //
0395: // public void testAddToRI3() {
0396: // long expected = TEST_TIME_NOW;
0397: // expected = ISOChronology.getInstanceUTC().years().add(expected, -2);
0398: // expected = ISOChronology.getInstanceUTC().months().add(expected, -4);
0399: // expected = ISOChronology.getInstanceUTC().weeks().add(expected, -6);
0400: // expected = ISOChronology.getInstanceUTC().days().add(expected, -8);
0401: // expected = ISOChronology.getInstanceUTC().hours().add(expected, -10);
0402: // expected = ISOChronology.getInstanceUTC().minutes().add(expected, -12);
0403: // expected = ISOChronology.getInstanceUTC().seconds().add(expected, -14);
0404: // expected = ISOChronology.getInstanceUTC().millis().add(expected, -16);
0405: //
0406: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
0407: // DateTime added = test.addTo(new DateTime(ISOChronology.getInstanceUTC()), -2); // DateTime has UTC time zone
0408: // assertEquals(expected, added.getMillis());
0409: // assertEquals(ISOChronology.getInstanceUTC(), added.getChronology());
0410: // }
0411: //
0412: // public void testAddToRI4() {
0413: // long expected = TEST_TIME_NOW;
0414: // expected = ISOChronology.getInstance(PARIS).years().add(expected, -2);
0415: // expected = ISOChronology.getInstance(PARIS).months().add(expected, -4);
0416: // expected = ISOChronology.getInstance(PARIS).weeks().add(expected, -6);
0417: // expected = ISOChronology.getInstance(PARIS).days().add(expected, -8);
0418: // expected = ISOChronology.getInstance(PARIS).hours().add(expected, -10);
0419: // expected = ISOChronology.getInstance(PARIS).minutes().add(expected, -12);
0420: // expected = ISOChronology.getInstance(PARIS).seconds().add(expected, -14);
0421: // expected = ISOChronology.getInstance(PARIS).millis().add(expected, -16);
0422: //
0423: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
0424: // DateTime added = test.addTo(new DateTime(PARIS), -2); // DateTime has PARIS time zone
0425: // assertEquals(expected, added.getMillis());
0426: // assertEquals(ISOChronology.getInstance(PARIS), added.getChronology());
0427: // }
0428: //
0429: // public void testAddToRI5() {
0430: // long expected = TEST_TIME_NOW;
0431: // expected = ISOChronology.getInstance().years().add(expected, -2);
0432: // expected = ISOChronology.getInstance().months().add(expected, -4);
0433: // expected = ISOChronology.getInstance().weeks().add(expected, -6);
0434: // expected = ISOChronology.getInstance().days().add(expected, -8);
0435: // expected = ISOChronology.getInstance().hours().add(expected, -10);
0436: // expected = ISOChronology.getInstance().minutes().add(expected, -12);
0437: // expected = ISOChronology.getInstance().seconds().add(expected, -14);
0438: // expected = ISOChronology.getInstance().millis().add(expected, -16);
0439: //
0440: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
0441: // DateTime added = test.addTo(null, -2); // null has no time zone, use default
0442: // assertEquals(expected, added.getMillis());
0443: // assertEquals(ISOChronology.getInstance(), added.getChronology());
0444: // }
0445: //
0446: // //-----------------------------------------------------------------------
0447: // public void testAddIntoRWI1() {
0448: // long expected = TEST_TIME_NOW;
0449: // expected = ISOChronology.getInstance().years().add(expected, 1);
0450: // expected = ISOChronology.getInstance().months().add(expected, 2);
0451: // expected = ISOChronology.getInstance().weeks().add(expected, 3);
0452: // expected = ISOChronology.getInstance().days().add(expected, 4);
0453: // expected = ISOChronology.getInstance().hours().add(expected, 5);
0454: // expected = ISOChronology.getInstance().minutes().add(expected, 6);
0455: // expected = ISOChronology.getInstance().seconds().add(expected, 7);
0456: // expected = ISOChronology.getInstance().millis().add(expected, 8);
0457: //
0458: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0459: // MutableDateTime mdt = new MutableDateTime();
0460: // test.addInto(mdt, 1);
0461: // assertEquals(expected, mdt.getMillis());
0462: // }
0463: //
0464: // public void testAddIntoRWI2() {
0465: // long expected = TEST_TIME_NOW;
0466: // expected = ISOChronology.getInstance().years().add(expected, -2);
0467: // expected = ISOChronology.getInstance().months().add(expected, -4);
0468: // expected = ISOChronology.getInstance().weeks().add(expected, -6);
0469: // expected = ISOChronology.getInstance().days().add(expected, -8);
0470: // expected = ISOChronology.getInstance().hours().add(expected, -10);
0471: // expected = ISOChronology.getInstance().minutes().add(expected, -12);
0472: // expected = ISOChronology.getInstance().seconds().add(expected, -14);
0473: // expected = ISOChronology.getInstance().millis().add(expected, -16);
0474: //
0475: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType.standard());
0476: // MutableDateTime mdt = new MutableDateTime();
0477: // test.addInto(mdt, -2); // MutableDateTime has a chronology, use it
0478: // assertEquals(expected, mdt.getMillis());
0479: // }
0480: //
0481: // public void testAddIntoRWI3() {
0482: // Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0483: // try {
0484: // test.addInto(null, 1);
0485: // fail();
0486: // } catch (IllegalArgumentException ex) {}
0487: // }
0488:
0489: //-----------------------------------------------------------------------
0490: public void testToString() {
0491: Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0492: assertEquals("P1Y2M3W4DT5H6M7.008S", test.toString());
0493:
0494: test = new Period(0, 0, 0, 0, 0, 0, 0, 0);
0495: assertEquals("PT0S", test.toString());
0496:
0497: test = new Period(12345L);
0498: assertEquals("PT12.345S", test.toString());
0499: }
0500:
0501: //-----------------------------------------------------------------------
0502: public void testToString_PeriodFormatter() {
0503: Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0504: assertEquals(
0505: "1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, 7 seconds and 8 milliseconds",
0506: test.toString(PeriodFormat.getDefault()));
0507:
0508: test = new Period(0, 0, 0, 0, 0, 0, 0, 0);
0509: assertEquals("0 milliseconds", test.toString(PeriodFormat
0510: .getDefault()));
0511: }
0512:
0513: public void testToString_nullPeriodFormatter() {
0514: Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0515: assertEquals("P1Y2M3W4DT5H6M7.008S", test
0516: .toString((PeriodFormatter) null));
0517: }
0518:
0519: //-----------------------------------------------------------------------
0520: public void testToPeriod() {
0521: Period test = new Period(123L);
0522: Period result = test.toPeriod();
0523: assertSame(test, result);
0524: }
0525:
0526: public void testToMutablePeriod() {
0527: Period test = new Period(123L);
0528: MutablePeriod result = test.toMutablePeriod();
0529: assertEquals(test, result);
0530: }
0531:
0532: //-----------------------------------------------------------------------
0533: // public void testToDurationMillisFrom() {
0534: // Period test = new Period(123L);
0535: // assertEquals(123L, test.toDurationMillisFrom(0L, null));
0536: // }
0537:
0538: public void testToDurationFrom() {
0539: Period test = new Period(123L);
0540: assertEquals(new Duration(123L), test
0541: .toDurationFrom(new Instant(0L)));
0542: }
0543:
0544: public void testToDurationTo() {
0545: Period test = new Period(123L);
0546: assertEquals(new Duration(123L), test.toDurationTo(new Instant(
0547: 123L)));
0548: }
0549:
0550: //-----------------------------------------------------------------------
0551: public void testWithPeriodType1() {
0552: Period test = new Period(123L);
0553: Period result = test.withPeriodType(PeriodType.standard());
0554: assertSame(test, result);
0555: }
0556:
0557: public void testWithPeriodType2() {
0558: Period test = new Period(3123L);
0559: Period result = test.withPeriodType(PeriodType.dayTime());
0560: assertEquals(3, result.getSeconds());
0561: assertEquals(123, result.getMillis());
0562: assertEquals(PeriodType.dayTime(), result.getPeriodType());
0563: }
0564:
0565: public void testWithPeriodType3() {
0566: Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8, PeriodType
0567: .standard());
0568: try {
0569: test.withPeriodType(PeriodType.dayTime());
0570: fail();
0571: } catch (IllegalArgumentException ex) {
0572: }
0573: }
0574:
0575: public void testWithPeriodType4() {
0576: Period test = new Period(3123L);
0577: Period result = test.withPeriodType(null);
0578: assertEquals(3, result.getSeconds());
0579: assertEquals(123, result.getMillis());
0580: assertEquals(PeriodType.standard(), result.getPeriodType());
0581: }
0582:
0583: public void testWithPeriodType5() {
0584: Period test = new Period(1, 2, 0, 4, 5, 6, 7, 8, PeriodType
0585: .standard());
0586: Period result = test.withPeriodType(PeriodType
0587: .yearMonthDayTime());
0588: assertEquals(PeriodType.yearMonthDayTime(), result
0589: .getPeriodType());
0590: assertEquals(1, result.getYears());
0591: assertEquals(2, result.getMonths());
0592: assertEquals(0, result.getWeeks());
0593: assertEquals(4, result.getDays());
0594: assertEquals(5, result.getHours());
0595: assertEquals(6, result.getMinutes());
0596: assertEquals(7, result.getSeconds());
0597: assertEquals(8, result.getMillis());
0598: }
0599:
0600: //-----------------------------------------------------------------------
0601: public void testWithFields1() {
0602: Period test1 = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0603: Period test2 = new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType
0604: .millis());
0605: Period result = test1.withFields(test2);
0606:
0607: assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test1);
0608: assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType
0609: .millis()), test2);
0610: assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 9), result);
0611: }
0612:
0613: public void testWithFields2() {
0614: Period test1 = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0615: Period test2 = null;
0616: Period result = test1.withFields(test2);
0617:
0618: assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test1);
0619: assertSame(test1, result);
0620: }
0621:
0622: public void testWithFields3() {
0623: Period test1 = new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType
0624: .millis());
0625: Period test2 = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0626: try {
0627: test1.withFields(test2);
0628: fail();
0629: } catch (IllegalArgumentException ex) {
0630: }
0631: assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 9, PeriodType
0632: .millis()), test1);
0633: assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test2);
0634: }
0635:
0636: //-----------------------------------------------------------------------
0637: public void testWithField1() {
0638: Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0639: Period result = test.withField(DurationFieldType.years(), 6);
0640:
0641: assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test);
0642: assertEquals(new Period(6, 2, 3, 4, 5, 6, 7, 8), result);
0643: }
0644:
0645: public void testWithField2() {
0646: Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0647: try {
0648: test.withField(null, 6);
0649: fail();
0650: } catch (IllegalArgumentException ex) {
0651: }
0652: }
0653:
0654: public void testWithField3() {
0655: Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType
0656: .time());
0657: try {
0658: test.withField(DurationFieldType.years(), 6);
0659: fail();
0660: } catch (IllegalArgumentException ex) {
0661: }
0662: }
0663:
0664: public void testWithField4() {
0665: Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType
0666: .time());
0667: Period result = test.withField(DurationFieldType.years(), 0);
0668: assertEquals(test, result);
0669: }
0670:
0671: //-----------------------------------------------------------------------
0672: public void testWithFieldAdded1() {
0673: Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0674: Period result = test.withFieldAdded(DurationFieldType.years(),
0675: 6);
0676:
0677: assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), test);
0678: assertEquals(new Period(7, 2, 3, 4, 5, 6, 7, 8), result);
0679: }
0680:
0681: public void testWithFieldAdded2() {
0682: Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0683: try {
0684: test.withFieldAdded(null, 0);
0685: fail();
0686: } catch (IllegalArgumentException ex) {
0687: }
0688: }
0689:
0690: public void testWithFieldAdded3() {
0691: Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType
0692: .time());
0693: try {
0694: test.withFieldAdded(DurationFieldType.years(), 6);
0695: fail();
0696: } catch (IllegalArgumentException ex) {
0697: }
0698: }
0699:
0700: public void testWithFieldAdded4() {
0701: Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8, PeriodType
0702: .time());
0703: Period result = test.withFieldAdded(DurationFieldType.years(),
0704: 0);
0705: assertEquals(test, result);
0706: }
0707:
0708: //-----------------------------------------------------------------------
0709: public void testPeriodStatics() {
0710: Period test;
0711: test = Period.years(1);
0712: assertEquals(test, new Period(1, 0, 0, 0, 0, 0, 0, 0,
0713: PeriodType.standard()));
0714: test = Period.months(1);
0715: assertEquals(test, new Period(0, 1, 0, 0, 0, 0, 0, 0,
0716: PeriodType.standard()));
0717: test = Period.weeks(1);
0718: assertEquals(test, new Period(0, 0, 1, 0, 0, 0, 0, 0,
0719: PeriodType.standard()));
0720: test = Period.days(1);
0721: assertEquals(test, new Period(0, 0, 0, 1, 0, 0, 0, 0,
0722: PeriodType.standard()));
0723: test = Period.hours(1);
0724: assertEquals(test, new Period(0, 0, 0, 0, 1, 0, 0, 0,
0725: PeriodType.standard()));
0726: test = Period.minutes(1);
0727: assertEquals(test, new Period(0, 0, 0, 0, 0, 1, 0, 0,
0728: PeriodType.standard()));
0729: test = Period.seconds(1);
0730: assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 1, 0,
0731: PeriodType.standard()));
0732: test = Period.millis(1);
0733: assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 0, 1,
0734: PeriodType.standard()));
0735: }
0736:
0737: //-----------------------------------------------------------------------
0738: public void testWith() {
0739: Period test;
0740: test = Period.years(5).withYears(1);
0741: assertEquals(test, new Period(1, 0, 0, 0, 0, 0, 0, 0,
0742: PeriodType.standard()));
0743: test = Period.months(5).withMonths(1);
0744: assertEquals(test, new Period(0, 1, 0, 0, 0, 0, 0, 0,
0745: PeriodType.standard()));
0746: test = Period.weeks(5).withWeeks(1);
0747: assertEquals(test, new Period(0, 0, 1, 0, 0, 0, 0, 0,
0748: PeriodType.standard()));
0749: test = Period.days(5).withDays(1);
0750: assertEquals(test, new Period(0, 0, 0, 1, 0, 0, 0, 0,
0751: PeriodType.standard()));
0752: test = Period.hours(5).withHours(1);
0753: assertEquals(test, new Period(0, 0, 0, 0, 1, 0, 0, 0,
0754: PeriodType.standard()));
0755: test = Period.minutes(5).withMinutes(1);
0756: assertEquals(test, new Period(0, 0, 0, 0, 0, 1, 0, 0,
0757: PeriodType.standard()));
0758: test = Period.seconds(5).withSeconds(1);
0759: assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 1, 0,
0760: PeriodType.standard()));
0761: test = Period.millis(5).withMillis(1);
0762: assertEquals(test, new Period(0, 0, 0, 0, 0, 0, 0, 1,
0763: PeriodType.standard()));
0764:
0765: test = new Period(0L, PeriodType.millis());
0766: try {
0767: test.withYears(1);
0768: fail();
0769: } catch (UnsupportedOperationException ex) {
0770: }
0771: }
0772:
0773: //-----------------------------------------------------------------------
0774: public void testPlus() {
0775: Period base = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0776: Period baseDaysOnly = new Period(0, 0, 0, 10, 0, 0, 0, 0,
0777: PeriodType.days());
0778:
0779: Period test = base.plus((ReadablePeriod) null);
0780: assertSame(base, test);
0781:
0782: test = base.plus(Period.years(10));
0783: assertEquals(11, test.getYears());
0784: assertEquals(2, test.getMonths());
0785: assertEquals(3, test.getWeeks());
0786: assertEquals(4, test.getDays());
0787: assertEquals(5, test.getHours());
0788: assertEquals(6, test.getMinutes());
0789: assertEquals(7, test.getSeconds());
0790: assertEquals(8, test.getMillis());
0791:
0792: test = base.plus(Years.years(10));
0793: assertEquals(11, test.getYears());
0794: assertEquals(2, test.getMonths());
0795: assertEquals(3, test.getWeeks());
0796: assertEquals(4, test.getDays());
0797: assertEquals(5, test.getHours());
0798: assertEquals(6, test.getMinutes());
0799: assertEquals(7, test.getSeconds());
0800: assertEquals(8, test.getMillis());
0801:
0802: test = base.plus(Period.days(10));
0803: assertEquals(1, test.getYears());
0804: assertEquals(2, test.getMonths());
0805: assertEquals(3, test.getWeeks());
0806: assertEquals(14, test.getDays());
0807: assertEquals(5, test.getHours());
0808: assertEquals(6, test.getMinutes());
0809: assertEquals(7, test.getSeconds());
0810: assertEquals(8, test.getMillis());
0811:
0812: test = baseDaysOnly.plus(Period.years(0));
0813: assertEquals(0, test.getYears());
0814: assertEquals(0, test.getMonths());
0815: assertEquals(0, test.getWeeks());
0816: assertEquals(10, test.getDays());
0817: assertEquals(0, test.getHours());
0818: assertEquals(0, test.getMinutes());
0819: assertEquals(0, test.getSeconds());
0820: assertEquals(0, test.getMillis());
0821:
0822: test = baseDaysOnly.plus(baseDaysOnly);
0823: assertEquals(0, test.getYears());
0824: assertEquals(0, test.getMonths());
0825: assertEquals(0, test.getWeeks());
0826: assertEquals(20, test.getDays());
0827: assertEquals(0, test.getHours());
0828: assertEquals(0, test.getMinutes());
0829: assertEquals(0, test.getSeconds());
0830: assertEquals(0, test.getMillis());
0831:
0832: try {
0833: baseDaysOnly.plus(Period.years(1));
0834: fail();
0835: } catch (UnsupportedOperationException ex) {
0836: }
0837:
0838: try {
0839: Period.days(Integer.MAX_VALUE).plus(Period.days(1));
0840: fail();
0841: } catch (ArithmeticException ex) {
0842: }
0843:
0844: try {
0845: Period.days(Integer.MIN_VALUE).plus(Period.days(-1));
0846: fail();
0847: } catch (ArithmeticException ex) {
0848: }
0849: }
0850:
0851: //-----------------------------------------------------------------------
0852: public void testMinus() {
0853: Period base = new Period(1, 2, 3, 4, 5, 6, 7, 8);
0854: Period baseDaysOnly = new Period(0, 0, 0, 10, 0, 0, 0, 0,
0855: PeriodType.days());
0856:
0857: Period test = base.minus((ReadablePeriod) null);
0858: assertSame(base, test);
0859:
0860: test = base.minus(Period.years(10));
0861: assertEquals(-9, test.getYears());
0862: assertEquals(2, test.getMonths());
0863: assertEquals(3, test.getWeeks());
0864: assertEquals(4, test.getDays());
0865: assertEquals(5, test.getHours());
0866: assertEquals(6, test.getMinutes());
0867: assertEquals(7, test.getSeconds());
0868: assertEquals(8, test.getMillis());
0869:
0870: test = base.minus(Years.years(10));
0871: assertEquals(-9, test.getYears());
0872: assertEquals(2, test.getMonths());
0873: assertEquals(3, test.getWeeks());
0874: assertEquals(4, test.getDays());
0875: assertEquals(5, test.getHours());
0876: assertEquals(6, test.getMinutes());
0877: assertEquals(7, test.getSeconds());
0878: assertEquals(8, test.getMillis());
0879:
0880: test = base.minus(Period.days(10));
0881: assertEquals(1, test.getYears());
0882: assertEquals(2, test.getMonths());
0883: assertEquals(3, test.getWeeks());
0884: assertEquals(-6, test.getDays());
0885: assertEquals(5, test.getHours());
0886: assertEquals(6, test.getMinutes());
0887: assertEquals(7, test.getSeconds());
0888: assertEquals(8, test.getMillis());
0889:
0890: test = baseDaysOnly.minus(Period.years(0));
0891: assertEquals(0, test.getYears());
0892: assertEquals(0, test.getMonths());
0893: assertEquals(0, test.getWeeks());
0894: assertEquals(10, test.getDays());
0895: assertEquals(0, test.getHours());
0896: assertEquals(0, test.getMinutes());
0897: assertEquals(0, test.getSeconds());
0898: assertEquals(0, test.getMillis());
0899:
0900: test = baseDaysOnly.minus(baseDaysOnly);
0901: assertEquals(0, test.getYears());
0902: assertEquals(0, test.getMonths());
0903: assertEquals(0, test.getWeeks());
0904: assertEquals(0, test.getDays());
0905: assertEquals(0, test.getHours());
0906: assertEquals(0, test.getMinutes());
0907: assertEquals(0, test.getSeconds());
0908: assertEquals(0, test.getMillis());
0909:
0910: try {
0911: baseDaysOnly.minus(Period.years(1));
0912: fail();
0913: } catch (UnsupportedOperationException ex) {
0914: }
0915:
0916: try {
0917: Period.days(Integer.MAX_VALUE).minus(Period.days(-1));
0918: fail();
0919: } catch (ArithmeticException ex) {
0920: }
0921:
0922: try {
0923: Period.days(Integer.MIN_VALUE).minus(Period.days(1));
0924: fail();
0925: } catch (ArithmeticException ex) {
0926: }
0927: }
0928:
0929: //-----------------------------------------------------------------------
0930: public void testPlusFields() {
0931: Period test;
0932: test = Period.years(1).plusYears(1);
0933: assertEquals(new Period(2, 0, 0, 0, 0, 0, 0, 0, PeriodType
0934: .standard()), test);
0935: test = Period.months(1).plusMonths(1);
0936: assertEquals(new Period(0, 2, 0, 0, 0, 0, 0, 0, PeriodType
0937: .standard()), test);
0938: test = Period.weeks(1).plusWeeks(1);
0939: assertEquals(new Period(0, 0, 2, 0, 0, 0, 0, 0, PeriodType
0940: .standard()), test);
0941: test = Period.days(1).plusDays(1);
0942: assertEquals(new Period(0, 0, 0, 2, 0, 0, 0, 0, PeriodType
0943: .standard()), test);
0944: test = Period.hours(1).plusHours(1);
0945: assertEquals(new Period(0, 0, 0, 0, 2, 0, 0, 0, PeriodType
0946: .standard()), test);
0947: test = Period.minutes(1).plusMinutes(1);
0948: assertEquals(new Period(0, 0, 0, 0, 0, 2, 0, 0, PeriodType
0949: .standard()), test);
0950: test = Period.seconds(1).plusSeconds(1);
0951: assertEquals(new Period(0, 0, 0, 0, 0, 0, 2, 0, PeriodType
0952: .standard()), test);
0953: test = Period.millis(1).plusMillis(1);
0954: assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 2, PeriodType
0955: .standard()), test);
0956:
0957: test = new Period(0L, PeriodType.millis());
0958: try {
0959: test.plusYears(1);
0960: fail();
0961: } catch (UnsupportedOperationException ex) {
0962: }
0963: }
0964:
0965: public void testPlusFieldsZero() {
0966: Period test, result;
0967: test = Period.years(1);
0968: result = test.plusYears(0);
0969: assertSame(test, result);
0970: test = Period.months(1);
0971: result = test.plusMonths(0);
0972: assertSame(test, result);
0973: test = Period.weeks(1);
0974: result = test.plusWeeks(0);
0975: assertSame(test, result);
0976: test = Period.days(1);
0977: result = test.plusDays(0);
0978: assertSame(test, result);
0979: test = Period.hours(1);
0980: result = test.plusHours(0);
0981: assertSame(test, result);
0982: test = Period.minutes(1);
0983: result = test.plusMinutes(0);
0984: assertSame(test, result);
0985: test = Period.seconds(1);
0986: result = test.plusSeconds(0);
0987: assertSame(test, result);
0988: test = Period.millis(1);
0989: result = test.plusMillis(0);
0990: assertSame(test, result);
0991: }
0992:
0993: public void testMinusFields() {
0994: Period test;
0995: test = Period.years(3).minusYears(1);
0996: assertEquals(new Period(2, 0, 0, 0, 0, 0, 0, 0, PeriodType
0997: .standard()), test);
0998: test = Period.months(3).minusMonths(1);
0999: assertEquals(new Period(0, 2, 0, 0, 0, 0, 0, 0, PeriodType
1000: .standard()), test);
1001: test = Period.weeks(3).minusWeeks(1);
1002: assertEquals(new Period(0, 0, 2, 0, 0, 0, 0, 0, PeriodType
1003: .standard()), test);
1004: test = Period.days(3).minusDays(1);
1005: assertEquals(new Period(0, 0, 0, 2, 0, 0, 0, 0, PeriodType
1006: .standard()), test);
1007: test = Period.hours(3).minusHours(1);
1008: assertEquals(new Period(0, 0, 0, 0, 2, 0, 0, 0, PeriodType
1009: .standard()), test);
1010: test = Period.minutes(3).minusMinutes(1);
1011: assertEquals(new Period(0, 0, 0, 0, 0, 2, 0, 0, PeriodType
1012: .standard()), test);
1013: test = Period.seconds(3).minusSeconds(1);
1014: assertEquals(new Period(0, 0, 0, 0, 0, 0, 2, 0, PeriodType
1015: .standard()), test);
1016: test = Period.millis(3).minusMillis(1);
1017: assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 2, PeriodType
1018: .standard()), test);
1019:
1020: test = new Period(0L, PeriodType.millis());
1021: try {
1022: test.minusYears(1);
1023: fail();
1024: } catch (UnsupportedOperationException ex) {
1025: }
1026: }
1027:
1028: //-----------------------------------------------------------------------
1029: public void testToStandardWeeks() {
1030: Period test = new Period(0, 0, 3, 4, 5, 6, 7, 8);
1031: assertEquals(3, test.toStandardWeeks().getWeeks());
1032:
1033: test = new Period(0, 0, 3, 7, 0, 0, 0, 0);
1034: assertEquals(4, test.toStandardWeeks().getWeeks());
1035:
1036: test = new Period(0, 0, 0, 6, 23, 59, 59, 1000);
1037: assertEquals(1, test.toStandardWeeks().getWeeks());
1038:
1039: test = new Period(0, 0, Integer.MAX_VALUE, 0, 0, 0, 0, 0);
1040: assertEquals(Integer.MAX_VALUE, test.toStandardWeeks()
1041: .getWeeks());
1042:
1043: test = new Period(0, 0, 0, Integer.MAX_VALUE,
1044: Integer.MAX_VALUE, Integer.MAX_VALUE,
1045: Integer.MAX_VALUE, Integer.MAX_VALUE);
1046: long intMax = Integer.MAX_VALUE;
1047: BigInteger expected = BigInteger.valueOf(intMax);
1048: expected = expected.add(BigInteger.valueOf(intMax
1049: * DateTimeConstants.MILLIS_PER_SECOND));
1050: expected = expected.add(BigInteger.valueOf(intMax
1051: * DateTimeConstants.MILLIS_PER_MINUTE));
1052: expected = expected.add(BigInteger.valueOf(intMax
1053: * DateTimeConstants.MILLIS_PER_HOUR));
1054: expected = expected.add(BigInteger.valueOf(intMax
1055: * DateTimeConstants.MILLIS_PER_DAY));
1056: expected = expected.divide(BigInteger
1057: .valueOf(DateTimeConstants.MILLIS_PER_WEEK));
1058: assertTrue(expected.compareTo(BigInteger
1059: .valueOf(Long.MAX_VALUE)) < 0);
1060: assertEquals(expected.longValue(), test.toStandardWeeks()
1061: .getWeeks());
1062:
1063: test = new Period(0, 0, Integer.MAX_VALUE, 7, 0, 0, 0, 0);
1064: try {
1065: test.toStandardWeeks();
1066: fail();
1067: } catch (ArithmeticException ex) {
1068: }
1069: }
1070:
1071: public void testToStandardWeeks_years() {
1072: Period test = Period.years(1);
1073: try {
1074: test.toStandardWeeks();
1075: fail();
1076: } catch (UnsupportedOperationException ex) {
1077: }
1078:
1079: test = Period.years(-1);
1080: try {
1081: test.toStandardWeeks();
1082: fail();
1083: } catch (UnsupportedOperationException ex) {
1084: }
1085:
1086: test = Period.years(0);
1087: assertEquals(0, test.toStandardWeeks().getWeeks());
1088: }
1089:
1090: public void testToStandardWeeks_months() {
1091: Period test = Period.months(1);
1092: try {
1093: test.toStandardWeeks();
1094: fail();
1095: } catch (UnsupportedOperationException ex) {
1096: }
1097:
1098: test = Period.months(-1);
1099: try {
1100: test.toStandardWeeks();
1101: fail();
1102: } catch (UnsupportedOperationException ex) {
1103: }
1104:
1105: test = Period.months(0);
1106: assertEquals(0, test.toStandardWeeks().getWeeks());
1107: }
1108:
1109: //-----------------------------------------------------------------------
1110: public void testToStandardDays() {
1111: Period test = new Period(0, 0, 0, 4, 5, 6, 7, 8);
1112: assertEquals(4, test.toStandardDays().getDays());
1113:
1114: test = new Period(0, 0, 1, 4, 0, 0, 0, 0);
1115: assertEquals(11, test.toStandardDays().getDays());
1116:
1117: test = new Period(0, 0, 0, 0, 23, 59, 59, 1000);
1118: assertEquals(1, test.toStandardDays().getDays());
1119:
1120: test = new Period(0, 0, 0, Integer.MAX_VALUE, 0, 0, 0, 0);
1121: assertEquals(Integer.MAX_VALUE, test.toStandardDays().getDays());
1122:
1123: test = new Period(0, 0, 0, 0, Integer.MAX_VALUE,
1124: Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
1125: long intMax = Integer.MAX_VALUE;
1126: BigInteger expected = BigInteger.valueOf(intMax);
1127: expected = expected.add(BigInteger.valueOf(intMax
1128: * DateTimeConstants.MILLIS_PER_SECOND));
1129: expected = expected.add(BigInteger.valueOf(intMax
1130: * DateTimeConstants.MILLIS_PER_MINUTE));
1131: expected = expected.add(BigInteger.valueOf(intMax
1132: * DateTimeConstants.MILLIS_PER_HOUR));
1133: expected = expected.divide(BigInteger
1134: .valueOf(DateTimeConstants.MILLIS_PER_DAY));
1135: assertTrue(expected.compareTo(BigInteger
1136: .valueOf(Long.MAX_VALUE)) < 0);
1137: assertEquals(expected.longValue(), test.toStandardDays()
1138: .getDays());
1139:
1140: test = new Period(0, 0, 0, Integer.MAX_VALUE, 24, 0, 0, 0);
1141: try {
1142: test.toStandardDays();
1143: fail();
1144: } catch (ArithmeticException ex) {
1145: }
1146: }
1147:
1148: public void testToStandardDays_years() {
1149: Period test = Period.years(1);
1150: try {
1151: test.toStandardDays();
1152: fail();
1153: } catch (UnsupportedOperationException ex) {
1154: }
1155:
1156: test = Period.years(-1);
1157: try {
1158: test.toStandardDays();
1159: fail();
1160: } catch (UnsupportedOperationException ex) {
1161: }
1162:
1163: test = Period.years(0);
1164: assertEquals(0, test.toStandardDays().getDays());
1165: }
1166:
1167: public void testToStandardDays_months() {
1168: Period test = Period.months(1);
1169: try {
1170: test.toStandardDays();
1171: fail();
1172: } catch (UnsupportedOperationException ex) {
1173: }
1174:
1175: test = Period.months(-1);
1176: try {
1177: test.toStandardDays();
1178: fail();
1179: } catch (UnsupportedOperationException ex) {
1180: }
1181:
1182: test = Period.months(0);
1183: assertEquals(0, test.toStandardDays().getDays());
1184: }
1185:
1186: //-----------------------------------------------------------------------
1187: public void testToStandardHours() {
1188: Period test = new Period(0, 0, 0, 0, 5, 6, 7, 8);
1189: assertEquals(5, test.toStandardHours().getHours());
1190:
1191: test = new Period(0, 0, 0, 1, 5, 0, 0, 0);
1192: assertEquals(29, test.toStandardHours().getHours());
1193:
1194: test = new Period(0, 0, 0, 0, 0, 59, 59, 1000);
1195: assertEquals(1, test.toStandardHours().getHours());
1196:
1197: test = new Period(0, 0, 0, 0, Integer.MAX_VALUE, 0, 0, 0);
1198: assertEquals(Integer.MAX_VALUE, test.toStandardHours()
1199: .getHours());
1200:
1201: test = new Period(0, 0, 0, 0, 0, Integer.MAX_VALUE,
1202: Integer.MAX_VALUE, Integer.MAX_VALUE);
1203: long intMax = Integer.MAX_VALUE;
1204: BigInteger expected = BigInteger.valueOf(intMax);
1205: expected = expected.add(BigInteger.valueOf(intMax
1206: * DateTimeConstants.MILLIS_PER_SECOND));
1207: expected = expected.add(BigInteger.valueOf(intMax
1208: * DateTimeConstants.MILLIS_PER_MINUTE));
1209: expected = expected.divide(BigInteger
1210: .valueOf(DateTimeConstants.MILLIS_PER_HOUR));
1211: assertTrue(expected.compareTo(BigInteger
1212: .valueOf(Long.MAX_VALUE)) < 0);
1213: assertEquals(expected.longValue(), test.toStandardHours()
1214: .getHours());
1215:
1216: test = new Period(0, 0, 0, 0, Integer.MAX_VALUE, 60, 0, 0);
1217: try {
1218: test.toStandardHours();
1219: fail();
1220: } catch (ArithmeticException ex) {
1221: }
1222: }
1223:
1224: public void testToStandardHours_years() {
1225: Period test = Period.years(1);
1226: try {
1227: test.toStandardHours();
1228: fail();
1229: } catch (UnsupportedOperationException ex) {
1230: }
1231:
1232: test = Period.years(-1);
1233: try {
1234: test.toStandardHours();
1235: fail();
1236: } catch (UnsupportedOperationException ex) {
1237: }
1238:
1239: test = Period.years(0);
1240: assertEquals(0, test.toStandardHours().getHours());
1241: }
1242:
1243: public void testToStandardHours_months() {
1244: Period test = Period.months(1);
1245: try {
1246: test.toStandardHours();
1247: fail();
1248: } catch (UnsupportedOperationException ex) {
1249: }
1250:
1251: test = Period.months(-1);
1252: try {
1253: test.toStandardHours();
1254: fail();
1255: } catch (UnsupportedOperationException ex) {
1256: }
1257:
1258: test = Period.months(0);
1259: assertEquals(0, test.toStandardHours().getHours());
1260: }
1261:
1262: //-----------------------------------------------------------------------
1263: public void testToStandardMinutes() {
1264: Period test = new Period(0, 0, 0, 0, 0, 6, 7, 8);
1265: assertEquals(6, test.toStandardMinutes().getMinutes());
1266:
1267: test = new Period(0, 0, 0, 0, 1, 6, 0, 0);
1268: assertEquals(66, test.toStandardMinutes().getMinutes());
1269:
1270: test = new Period(0, 0, 0, 0, 0, 0, 59, 1000);
1271: assertEquals(1, test.toStandardMinutes().getMinutes());
1272:
1273: test = new Period(0, 0, 0, 0, 0, Integer.MAX_VALUE, 0, 0);
1274: assertEquals(Integer.MAX_VALUE, test.toStandardMinutes()
1275: .getMinutes());
1276:
1277: test = new Period(0, 0, 0, 0, 0, 0, Integer.MAX_VALUE,
1278: Integer.MAX_VALUE);
1279: long intMax = Integer.MAX_VALUE;
1280: BigInteger expected = BigInteger.valueOf(intMax);
1281: expected = expected.add(BigInteger.valueOf(intMax
1282: * DateTimeConstants.MILLIS_PER_SECOND));
1283: expected = expected.divide(BigInteger
1284: .valueOf(DateTimeConstants.MILLIS_PER_MINUTE));
1285: assertTrue(expected.compareTo(BigInteger
1286: .valueOf(Long.MAX_VALUE)) < 0);
1287: assertEquals(expected.longValue(), test.toStandardMinutes()
1288: .getMinutes());
1289:
1290: test = new Period(0, 0, 0, 0, 0, Integer.MAX_VALUE, 60, 0);
1291: try {
1292: test.toStandardMinutes();
1293: fail();
1294: } catch (ArithmeticException ex) {
1295: }
1296: }
1297:
1298: public void testToStandardMinutes_years() {
1299: Period test = Period.years(1);
1300: try {
1301: test.toStandardMinutes();
1302: fail();
1303: } catch (UnsupportedOperationException ex) {
1304: }
1305:
1306: test = Period.years(-1);
1307: try {
1308: test.toStandardMinutes();
1309: fail();
1310: } catch (UnsupportedOperationException ex) {
1311: }
1312:
1313: test = Period.years(0);
1314: assertEquals(0, test.toStandardMinutes().getMinutes());
1315: }
1316:
1317: public void testToStandardMinutes_months() {
1318: Period test = Period.months(1);
1319: try {
1320: test.toStandardMinutes();
1321: fail();
1322: } catch (UnsupportedOperationException ex) {
1323: }
1324:
1325: test = Period.months(-1);
1326: try {
1327: test.toStandardMinutes();
1328: fail();
1329: } catch (UnsupportedOperationException ex) {
1330: }
1331:
1332: test = Period.months(0);
1333: assertEquals(0, test.toStandardMinutes().getMinutes());
1334: }
1335:
1336: //-----------------------------------------------------------------------
1337: public void testToStandardSeconds() {
1338: Period test = new Period(0, 0, 0, 0, 0, 0, 7, 8);
1339: assertEquals(7, test.toStandardSeconds().getSeconds());
1340:
1341: test = new Period(0, 0, 0, 0, 0, 1, 3, 0);
1342: assertEquals(63, test.toStandardSeconds().getSeconds());
1343:
1344: test = new Period(0, 0, 0, 0, 0, 0, 0, 1000);
1345: assertEquals(1, test.toStandardSeconds().getSeconds());
1346:
1347: test = new Period(0, 0, 0, 0, 0, 0, Integer.MAX_VALUE, 0);
1348: assertEquals(Integer.MAX_VALUE, test.toStandardSeconds()
1349: .getSeconds());
1350:
1351: test = new Period(0, 0, 0, 0, 0, 0, 20, Integer.MAX_VALUE);
1352: long expected = 20;
1353: expected += ((long) Integer.MAX_VALUE)
1354: / DateTimeConstants.MILLIS_PER_SECOND;
1355: assertEquals(expected, test.toStandardSeconds().getSeconds());
1356:
1357: test = new Period(0, 0, 0, 0, 0, 0, Integer.MAX_VALUE, 1000);
1358: try {
1359: test.toStandardSeconds();
1360: fail();
1361: } catch (ArithmeticException ex) {
1362: }
1363: }
1364:
1365: public void testToStandardSeconds_years() {
1366: Period test = Period.years(1);
1367: try {
1368: test.toStandardSeconds();
1369: fail();
1370: } catch (UnsupportedOperationException ex) {
1371: }
1372:
1373: test = Period.years(-1);
1374: try {
1375: test.toStandardSeconds();
1376: fail();
1377: } catch (UnsupportedOperationException ex) {
1378: }
1379:
1380: test = Period.years(0);
1381: assertEquals(0, test.toStandardSeconds().getSeconds());
1382: }
1383:
1384: public void testToStandardSeconds_months() {
1385: Period test = Period.months(1);
1386: try {
1387: test.toStandardSeconds();
1388: fail();
1389: } catch (UnsupportedOperationException ex) {
1390: }
1391:
1392: test = Period.months(-1);
1393: try {
1394: test.toStandardSeconds();
1395: fail();
1396: } catch (UnsupportedOperationException ex) {
1397: }
1398:
1399: test = Period.months(0);
1400: assertEquals(0, test.toStandardSeconds().getSeconds());
1401: }
1402:
1403: //-----------------------------------------------------------------------
1404: public void testToStandardDuration() {
1405: Period test = new Period(0, 0, 0, 0, 0, 0, 0, 8);
1406: assertEquals(8, test.toStandardDuration().getMillis());
1407:
1408: test = new Period(0, 0, 0, 0, 0, 0, 1, 20);
1409: assertEquals(1020, test.toStandardDuration().getMillis());
1410:
1411: test = new Period(0, 0, 0, 0, 0, 0, 0, Integer.MAX_VALUE);
1412: assertEquals(Integer.MAX_VALUE, test.toStandardDuration()
1413: .getMillis());
1414:
1415: test = new Period(0, 0, 0, 0, 0, 10, 20, Integer.MAX_VALUE);
1416: long expected = Integer.MAX_VALUE;
1417: expected += 10L * ((long) DateTimeConstants.MILLIS_PER_MINUTE);
1418: expected += 20L * ((long) DateTimeConstants.MILLIS_PER_SECOND);
1419: assertEquals(expected, test.toStandardDuration().getMillis());
1420:
1421: // proof that overflow does not occur
1422: BigInteger intMax = BigInteger.valueOf(Integer.MAX_VALUE);
1423: BigInteger exp = intMax;
1424: exp = exp.add(intMax.multiply(BigInteger
1425: .valueOf(DateTimeConstants.MILLIS_PER_SECOND)));
1426: exp = exp.add(intMax.multiply(BigInteger
1427: .valueOf(DateTimeConstants.MILLIS_PER_MINUTE)));
1428: exp = exp.add(intMax.multiply(BigInteger
1429: .valueOf(DateTimeConstants.MILLIS_PER_HOUR)));
1430: exp = exp.add(intMax.multiply(BigInteger
1431: .valueOf(DateTimeConstants.MILLIS_PER_DAY)));
1432: exp = exp.add(intMax.multiply(BigInteger
1433: .valueOf(DateTimeConstants.MILLIS_PER_WEEK)));
1434: assertTrue(exp.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) < 0);
1435: // test = new Period(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
1436: // try {
1437: // test.toStandardDuration();
1438: // fail();
1439: // } catch (ArithmeticException ex) {}
1440: }
1441:
1442: public void testToStandardDuration_years() {
1443: Period test = Period.years(1);
1444: try {
1445: test.toStandardDuration();
1446: fail();
1447: } catch (UnsupportedOperationException ex) {
1448: }
1449:
1450: test = Period.years(-1);
1451: try {
1452: test.toStandardDuration();
1453: fail();
1454: } catch (UnsupportedOperationException ex) {
1455: }
1456:
1457: test = Period.years(0);
1458: assertEquals(0, test.toStandardDuration().getMillis());
1459: }
1460:
1461: public void testToStandardDuration_months() {
1462: Period test = Period.months(1);
1463: try {
1464: test.toStandardDuration();
1465: fail();
1466: } catch (UnsupportedOperationException ex) {
1467: }
1468:
1469: test = Period.months(-1);
1470: try {
1471: test.toStandardDuration();
1472: fail();
1473: } catch (UnsupportedOperationException ex) {
1474: }
1475:
1476: test = Period.months(0);
1477: assertEquals(0, test.toStandardDuration().getMillis());
1478: }
1479:
1480: //-----------------------------------------------------------------------
1481: public void testNormalizedStandard_yearMonth1() {
1482: Period test = new Period(1, 15, 0, 0, 0, 0, 0, 0);
1483: Period result = test.normalizedStandard();
1484: assertEquals(new Period(1, 15, 0, 0, 0, 0, 0, 0), test);
1485: assertEquals(new Period(2, 3, 0, 0, 0, 0, 0, 0), result);
1486: }
1487:
1488: public void testNormalizedStandard_yearMonth2() {
1489: Period test = new Period(Integer.MAX_VALUE, 15, 0, 0, 0, 0, 0,
1490: 0);
1491: try {
1492: test.normalizedStandard();
1493: fail();
1494: } catch (ArithmeticException ex) {
1495: }
1496: }
1497:
1498: public void testNormalizedStandard_weekDay1() {
1499: Period test = new Period(0, 0, 1, 12, 0, 0, 0, 0);
1500: Period result = test.normalizedStandard();
1501: assertEquals(new Period(0, 0, 1, 12, 0, 0, 0, 0), test);
1502: assertEquals(new Period(0, 0, 2, 5, 0, 0, 0, 0), result);
1503: }
1504:
1505: public void testNormalizedStandard_weekDay2() {
1506: Period test = new Period(0, 0, Integer.MAX_VALUE, 7, 0, 0, 0, 0);
1507: try {
1508: test.normalizedStandard();
1509: fail();
1510: } catch (ArithmeticException ex) {
1511: }
1512: }
1513:
1514: public void testNormalizedStandard_yearMonthWeekDay() {
1515: Period test = new Period(1, 15, 1, 12, 0, 0, 0, 0);
1516: Period result = test.normalizedStandard();
1517: assertEquals(new Period(1, 15, 1, 12, 0, 0, 0, 0), test);
1518: assertEquals(new Period(2, 3, 2, 5, 0, 0, 0, 0), result);
1519: }
1520:
1521: public void testNormalizedStandard_yearMonthDay() {
1522: Period test = new Period(1, 15, 0, 36, 0, 0, 0, 0);
1523: Period result = test.normalizedStandard();
1524: assertEquals(new Period(1, 15, 0, 36, 0, 0, 0, 0), test);
1525: assertEquals(new Period(2, 3, 5, 1, 0, 0, 0, 0), result);
1526: }
1527:
1528: public void testNormalizedStandard_negative() {
1529: Period test = new Period(0, 0, 0, 0, 2, -10, 0, 0);
1530: Period result = test.normalizedStandard();
1531: assertEquals(new Period(0, 0, 0, 0, 2, -10, 0, 0), test);
1532: assertEquals(new Period(0, 0, 0, 0, 1, 50, 0, 0), result);
1533: }
1534:
1535: public void testNormalizedStandard_fullNegative() {
1536: Period test = new Period(0, 0, 0, 0, 1, -70, 0, 0);
1537: Period result = test.normalizedStandard();
1538: assertEquals(new Period(0, 0, 0, 0, 1, -70, 0, 0), test);
1539: assertEquals(new Period(0, 0, 0, 0, 0, -10, 0, 0), result);
1540: }
1541:
1542: //-----------------------------------------------------------------------
1543: public void testNormalizedStandard_periodType_yearMonth1() {
1544: Period test = new Period(1, 15, 0, 0, 0, 0, 0, 0);
1545: Period result = test.normalizedStandard((PeriodType) null);
1546: assertEquals(new Period(1, 15, 0, 0, 0, 0, 0, 0), test);
1547: assertEquals(new Period(2, 3, 0, 0, 0, 0, 0, 0), result);
1548: }
1549:
1550: public void testNormalizedStandard_periodType_yearMonth2() {
1551: Period test = new Period(Integer.MAX_VALUE, 15, 0, 0, 0, 0, 0,
1552: 0);
1553: try {
1554: test.normalizedStandard((PeriodType) null);
1555: fail();
1556: } catch (ArithmeticException ex) {
1557: }
1558: }
1559:
1560: public void testNormalizedStandard_periodType_yearMonth3() {
1561: Period test = new Period(1, 15, 3, 4, 0, 0, 0, 0);
1562: try {
1563: test.normalizedStandard(PeriodType.dayTime());
1564: fail();
1565: } catch (UnsupportedOperationException ex) {
1566: }
1567: }
1568:
1569: public void testNormalizedStandard_periodType_weekDay1() {
1570: Period test = new Period(0, 0, 1, 12, 0, 0, 0, 0);
1571: Period result = test.normalizedStandard((PeriodType) null);
1572: assertEquals(new Period(0, 0, 1, 12, 0, 0, 0, 0), test);
1573: assertEquals(new Period(0, 0, 2, 5, 0, 0, 0, 0), result);
1574: }
1575:
1576: public void testNormalizedStandard_periodType_weekDay2() {
1577: Period test = new Period(0, 0, Integer.MAX_VALUE, 7, 0, 0, 0, 0);
1578: try {
1579: test.normalizedStandard((PeriodType) null);
1580: fail();
1581: } catch (ArithmeticException ex) {
1582: }
1583: }
1584:
1585: public void testNormalizedStandard_periodType_weekDay3() {
1586: Period test = new Period(0, 0, 1, 12, 0, 0, 0, 0);
1587: Period result = test.normalizedStandard(PeriodType.dayTime());
1588: assertEquals(new Period(0, 0, 1, 12, 0, 0, 0, 0), test);
1589: assertEquals(new Period(0, 0, 0, 19, 0, 0, 0, 0, PeriodType
1590: .dayTime()), result);
1591: }
1592:
1593: public void testNormalizedStandard_periodType_yearMonthWeekDay() {
1594: Period test = new Period(1, 15, 1, 12, 0, 0, 0, 0);
1595: Period result = test.normalizedStandard(PeriodType
1596: .yearMonthDayTime());
1597: assertEquals(new Period(1, 15, 1, 12, 0, 0, 0, 0), test);
1598: assertEquals(new Period(2, 3, 0, 19, 0, 0, 0, 0, PeriodType
1599: .yearMonthDayTime()), result);
1600: }
1601:
1602: public void testNormalizedStandard_periodType_yearMonthDay() {
1603: Period test = new Period(1, 15, 0, 36, 27, 0, 0, 0);
1604: Period result = test.normalizedStandard(PeriodType
1605: .yearMonthDayTime());
1606: assertEquals(new Period(1, 15, 0, 36, 27, 0, 0, 0), test);
1607: assertEquals(new Period(2, 3, 0, 37, 3, 0, 0, 0, PeriodType
1608: .yearMonthDayTime()), result);
1609: }
1610:
1611: }
|