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.util.Locale;
0019: import java.util.TimeZone;
0020:
0021: import junit.framework.TestCase;
0022: import junit.framework.TestSuite;
0023:
0024: import org.joda.time.chrono.ISOChronology;
0025:
0026: /**
0027: * This class is a JUnit test for MutableDuration.
0028: *
0029: * @author Stephen Colebourne
0030: */
0031: public class TestMutablePeriod_Updates extends TestCase {
0032: // Test in 2002/03 as time zones are more well known
0033: // (before the late 90's they were all over the place)
0034:
0035: private static final DateTimeZone PARIS = DateTimeZone
0036: .forID("Europe/Paris");
0037: private static final DateTimeZone LONDON = DateTimeZone
0038: .forID("Europe/London");
0039:
0040: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0041: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0042: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0043: + 365 + 365 + 366 + 365;
0044: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0045: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0046: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0047: + 365 + 365 + 366 + 365 + 365;
0048:
0049: // 2002-06-09
0050: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
0051: + 31L + 9L - 1L)
0052: * DateTimeConstants.MILLIS_PER_DAY;
0053:
0054: // 2002-04-05
0055: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
0056: * DateTimeConstants.MILLIS_PER_DAY + 12L
0057: * DateTimeConstants.MILLIS_PER_HOUR + 24L
0058: * DateTimeConstants.MILLIS_PER_MINUTE;
0059:
0060: // 2003-05-06
0061: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
0062: * DateTimeConstants.MILLIS_PER_DAY
0063: + 14L
0064: * DateTimeConstants.MILLIS_PER_HOUR
0065: + 28L
0066: * DateTimeConstants.MILLIS_PER_MINUTE;
0067:
0068: private DateTimeZone originalDateTimeZone = null;
0069: private TimeZone originalTimeZone = null;
0070: private Locale originalLocale = null;
0071:
0072: public static void main(String[] args) {
0073: junit.textui.TestRunner.run(suite());
0074: }
0075:
0076: public static TestSuite suite() {
0077: return new TestSuite(TestMutablePeriod_Updates.class);
0078: }
0079:
0080: public TestMutablePeriod_Updates(String name) {
0081: super (name);
0082: }
0083:
0084: protected void setUp() throws Exception {
0085: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
0086: originalDateTimeZone = DateTimeZone.getDefault();
0087: originalTimeZone = TimeZone.getDefault();
0088: originalLocale = Locale.getDefault();
0089: DateTimeZone.setDefault(LONDON);
0090: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
0091: Locale.setDefault(Locale.UK);
0092: }
0093:
0094: protected void tearDown() throws Exception {
0095: DateTimeUtils.setCurrentMillisSystem();
0096: DateTimeZone.setDefault(originalDateTimeZone);
0097: TimeZone.setDefault(originalTimeZone);
0098: Locale.setDefault(originalLocale);
0099: originalDateTimeZone = null;
0100: originalTimeZone = null;
0101: originalLocale = null;
0102: }
0103:
0104: //-----------------------------------------------------------------------
0105: public void testTest() {
0106: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
0107: TEST_TIME_NOW).toString());
0108: assertEquals("2002-04-05T12:24:00.000Z",
0109: new Instant(TEST_TIME1).toString());
0110: assertEquals("2003-05-06T14:28:00.000Z",
0111: new Instant(TEST_TIME2).toString());
0112: }
0113:
0114: //-----------------------------------------------------------------------
0115: public void testClear() {
0116: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0117: test.clear();
0118: assertEquals(new MutablePeriod(), test);
0119:
0120: test = new MutablePeriod(1, 2, 0, 4, 5, 6, 7, 8, PeriodType
0121: .yearMonthDayTime());
0122: test.clear();
0123: assertEquals(new MutablePeriod(PeriodType.yearMonthDayTime()),
0124: test);
0125: }
0126:
0127: //-----------------------------------------------------------------------
0128: public void testAddYears() {
0129: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0130: test.addYears(10);
0131: assertEquals(11, test.getYears());
0132:
0133: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0134: test.addYears(-10);
0135: assertEquals(-9, test.getYears());
0136:
0137: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0138: test.addYears(0);
0139: assertEquals(1, test.getYears());
0140: }
0141:
0142: //-----------------------------------------------------------------------
0143: public void testAddMonths() {
0144: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0145: test.addMonths(10);
0146: assertEquals(12, test.getMonths());
0147:
0148: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0149: test.addMonths(-10);
0150: assertEquals(-8, test.getMonths());
0151:
0152: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0153: test.addMonths(0);
0154: assertEquals(2, test.getMonths());
0155: }
0156:
0157: //-----------------------------------------------------------------------
0158: public void testAddWeeks() {
0159: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0160: test.addWeeks(10);
0161: assertEquals(13, test.getWeeks());
0162:
0163: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0164: test.addWeeks(-10);
0165: assertEquals(-7, test.getWeeks());
0166:
0167: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0168: test.addWeeks(0);
0169: assertEquals(3, test.getWeeks());
0170: }
0171:
0172: //-----------------------------------------------------------------------
0173: public void testAddDays() {
0174: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0175: test.addDays(10);
0176: assertEquals(14, test.getDays());
0177:
0178: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0179: test.addDays(-10);
0180: assertEquals(-6, test.getDays());
0181:
0182: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0183: test.addDays(0);
0184: assertEquals(4, test.getDays());
0185: }
0186:
0187: //-----------------------------------------------------------------------
0188: public void testAddHours() {
0189: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0190: test.addHours(10);
0191: assertEquals(15, test.getHours());
0192:
0193: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0194: test.addHours(-10);
0195: assertEquals(-5, test.getHours());
0196:
0197: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0198: test.addHours(0);
0199: assertEquals(5, test.getHours());
0200: }
0201:
0202: //-----------------------------------------------------------------------
0203: public void testAddMinutes() {
0204: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0205: test.addMinutes(10);
0206: assertEquals(16, test.getMinutes());
0207:
0208: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0209: test.addMinutes(-10);
0210: assertEquals(-4, test.getMinutes());
0211:
0212: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0213: test.addMinutes(0);
0214: assertEquals(6, test.getMinutes());
0215: }
0216:
0217: //-----------------------------------------------------------------------
0218: public void testAddSeconds() {
0219: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0220: test.addSeconds(10);
0221: assertEquals(17, test.getSeconds());
0222:
0223: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0224: test.addSeconds(-10);
0225: assertEquals(-3, test.getSeconds());
0226:
0227: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0228: test.addSeconds(0);
0229: assertEquals(7, test.getSeconds());
0230: }
0231:
0232: //-----------------------------------------------------------------------
0233: public void testAddMillis() {
0234: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0235: test.addMillis(10);
0236: assertEquals(18, test.getMillis());
0237:
0238: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0239: test.addMillis(-10);
0240: assertEquals(-2, test.getMillis());
0241:
0242: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0243: test.addMillis(0);
0244: assertEquals(8, test.getMillis());
0245: }
0246:
0247: //-----------------------------------------------------------------------
0248: public void testSetYears() {
0249: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0250: test.setYears(10);
0251: assertEquals(10, test.getYears());
0252:
0253: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0254: test.setYears(-10);
0255: assertEquals(-10, test.getYears());
0256:
0257: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0258: test.setYears(0);
0259: assertEquals(0, test.getYears());
0260:
0261: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0262: test.setYears(1);
0263: assertEquals(1, test.getYears());
0264:
0265: test = new MutablePeriod(0, 0, 0, 0, 0, 0, 0, 1, PeriodType
0266: .millis());
0267: try {
0268: test.setYears(1);
0269: fail();
0270: } catch (IllegalArgumentException ex) {
0271: }
0272: }
0273:
0274: //-----------------------------------------------------------------------
0275: public void testSetMonths() {
0276: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0277: test.setMonths(10);
0278: assertEquals(10, test.getMonths());
0279:
0280: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0281: test.setMonths(-10);
0282: assertEquals(-10, test.getMonths());
0283:
0284: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0285: test.setMonths(0);
0286: assertEquals(0, test.getMonths());
0287:
0288: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0289: test.setMonths(2);
0290: assertEquals(2, test.getMonths());
0291: }
0292:
0293: //-----------------------------------------------------------------------
0294: public void testSetWeeks() {
0295: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0296: test.setWeeks(10);
0297: assertEquals(10, test.getWeeks());
0298:
0299: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0300: test.setWeeks(-10);
0301: assertEquals(-10, test.getWeeks());
0302:
0303: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0304: test.setWeeks(0);
0305: assertEquals(0, test.getWeeks());
0306:
0307: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0308: test.setWeeks(3);
0309: assertEquals(3, test.getWeeks());
0310: }
0311:
0312: //-----------------------------------------------------------------------
0313: public void testSetDays() {
0314: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0315: test.setDays(10);
0316: assertEquals(10, test.getDays());
0317:
0318: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0319: test.setDays(-10);
0320: assertEquals(-10, test.getDays());
0321:
0322: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0323: test.setDays(0);
0324: assertEquals(0, test.getDays());
0325:
0326: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0327: test.setDays(4);
0328: assertEquals(4, test.getDays());
0329: }
0330:
0331: //-----------------------------------------------------------------------
0332: public void testSetHours() {
0333: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0334: test.setHours(10);
0335: assertEquals(10, test.getHours());
0336:
0337: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0338: test.setHours(-10);
0339: assertEquals(-10, test.getHours());
0340:
0341: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0342: test.setHours(0);
0343: assertEquals(0, test.getHours());
0344:
0345: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0346: test.setHours(5);
0347: assertEquals(5, test.getHours());
0348: }
0349:
0350: //-----------------------------------------------------------------------
0351: public void testSetMinutes() {
0352: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0353: test.setMinutes(10);
0354: assertEquals(10, test.getMinutes());
0355:
0356: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0357: test.setMinutes(-10);
0358: assertEquals(-10, test.getMinutes());
0359:
0360: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0361: test.setMinutes(0);
0362: assertEquals(0, test.getMinutes());
0363:
0364: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0365: test.setMinutes(6);
0366: assertEquals(6, test.getMinutes());
0367: }
0368:
0369: //-----------------------------------------------------------------------
0370: public void testSetSeconds() {
0371: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0372: test.setSeconds(10);
0373: assertEquals(10, test.getSeconds());
0374:
0375: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0376: test.setSeconds(-10);
0377: assertEquals(-10, test.getSeconds());
0378:
0379: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0380: test.setSeconds(0);
0381: assertEquals(0, test.getSeconds());
0382:
0383: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0384: test.setSeconds(7);
0385: assertEquals(7, test.getSeconds());
0386: }
0387:
0388: //-----------------------------------------------------------------------
0389: public void testSetMillis() {
0390: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0391: test.setMillis(10);
0392: assertEquals(10, test.getMillis());
0393:
0394: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0395: test.setMillis(-10);
0396: assertEquals(-10, test.getMillis());
0397:
0398: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0399: test.setMillis(0);
0400: assertEquals(0, test.getMillis());
0401:
0402: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0403: test.setMillis(8);
0404: assertEquals(8, test.getMillis());
0405: }
0406:
0407: //-----------------------------------------------------------------------
0408: public void testSet_Field() {
0409: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0410: test.set(DurationFieldType.years(), 10);
0411: assertEquals(10, test.getYears());
0412:
0413: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0414: try {
0415: test.set(null, 10);
0416: fail();
0417: } catch (IllegalArgumentException ex) {
0418: }
0419: }
0420:
0421: //-----------------------------------------------------------------------
0422: public void testAdd_Field() {
0423: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0424: test.add(DurationFieldType.years(), 10);
0425: assertEquals(11, test.getYears());
0426:
0427: test = new MutablePeriod(0, 0, 0, 0, 0, 0, 0, 1, PeriodType
0428: .millis());
0429: test.add(DurationFieldType.years(), 0);
0430: assertEquals(0, test.getYears());
0431: assertEquals(1, test.getMillis());
0432:
0433: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0434: try {
0435: test.add(null, 0);
0436: fail();
0437: } catch (IllegalArgumentException ex) {
0438: }
0439:
0440: test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0441: try {
0442: test.add(null, 10);
0443: fail();
0444: } catch (IllegalArgumentException ex) {
0445: }
0446: }
0447:
0448: //-----------------------------------------------------------------------
0449: public void testSetPeriod_8ints1() {
0450: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0451: test.setPeriod(11, 12, 13, 14, 15, 16, 17, 18);
0452: assertEquals(11, test.getYears());
0453: assertEquals(12, test.getMonths());
0454: assertEquals(13, test.getWeeks());
0455: assertEquals(14, test.getDays());
0456: assertEquals(15, test.getHours());
0457: assertEquals(16, test.getMinutes());
0458: assertEquals(17, test.getSeconds());
0459: assertEquals(18, test.getMillis());
0460: }
0461:
0462: public void testSetPeriod_8ints2() {
0463: MutablePeriod test = new MutablePeriod(100L, PeriodType
0464: .millis());
0465: try {
0466: test.setPeriod(11, 12, 13, 14, 15, 16, 17, 18);
0467: fail();
0468: } catch (IllegalArgumentException ex) {
0469: }
0470: assertEquals(0, test.getYears());
0471: assertEquals(0, test.getMonths());
0472: assertEquals(0, test.getWeeks());
0473: assertEquals(0, test.getDays());
0474: assertEquals(0, test.getHours());
0475: assertEquals(0, test.getMinutes());
0476: assertEquals(0, test.getSeconds());
0477: assertEquals(100, test.getMillis());
0478: }
0479:
0480: public void testSetPeriod_8ints3() {
0481: MutablePeriod test = new MutablePeriod(100L, PeriodType
0482: .millis());
0483: test.setPeriod(0, 0, 0, 0, 0, 0, 0, 18);
0484: assertEquals(0, test.getYears());
0485: assertEquals(0, test.getMonths());
0486: assertEquals(0, test.getWeeks());
0487: assertEquals(0, test.getDays());
0488: assertEquals(0, test.getHours());
0489: assertEquals(0, test.getMinutes());
0490: assertEquals(0, test.getSeconds());
0491: assertEquals(18, test.getMillis());
0492: }
0493:
0494: public void testSetPeriod_8ints4() {
0495: MutablePeriod test = new MutablePeriod(0, 0, 0, 0, 5, 6, 7, 8);
0496: test.setPeriod(11, 12, 13, 14, 15, 16, 17, 18);
0497: assertEquals(11, test.getYears());
0498: assertEquals(12, test.getMonths());
0499: assertEquals(13, test.getWeeks());
0500: assertEquals(14, test.getDays());
0501: assertEquals(15, test.getHours());
0502: assertEquals(16, test.getMinutes());
0503: assertEquals(17, test.getSeconds());
0504: assertEquals(18, test.getMillis());
0505: }
0506:
0507: //-----------------------------------------------------------------------
0508: public void testSetPeriod_RP1() {
0509: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0510: test
0511: .setPeriod(new MutablePeriod(11, 12, 13, 14, 15, 16,
0512: 17, 18));
0513: assertEquals(11, test.getYears());
0514: assertEquals(12, test.getMonths());
0515: assertEquals(13, test.getWeeks());
0516: assertEquals(14, test.getDays());
0517: assertEquals(15, test.getHours());
0518: assertEquals(16, test.getMinutes());
0519: assertEquals(17, test.getSeconds());
0520: assertEquals(18, test.getMillis());
0521: }
0522:
0523: public void testSetPeriod_RP2() {
0524: MutablePeriod test = new MutablePeriod(100L, PeriodType
0525: .millis());
0526: try {
0527: test.setPeriod(new MutablePeriod(11, 12, 13, 14, 15, 16,
0528: 17, 18));
0529: fail();
0530: } catch (IllegalArgumentException ex) {
0531: }
0532: assertEquals(0, test.getYears());
0533: assertEquals(0, test.getMonths());
0534: assertEquals(0, test.getWeeks());
0535: assertEquals(0, test.getDays());
0536: assertEquals(0, test.getHours());
0537: assertEquals(0, test.getMinutes());
0538: assertEquals(0, test.getSeconds());
0539: assertEquals(100, test.getMillis());
0540: }
0541:
0542: public void testSetPeriod_RP3() {
0543: MutablePeriod test = new MutablePeriod(100L, PeriodType
0544: .millis());
0545: test.setPeriod(new MutablePeriod(0, 0, 0, 0, 0, 0, 0, 18));
0546: assertEquals(0, test.getYears());
0547: assertEquals(0, test.getMonths());
0548: assertEquals(0, test.getWeeks());
0549: assertEquals(0, test.getDays());
0550: assertEquals(0, test.getHours());
0551: assertEquals(0, test.getMinutes());
0552: assertEquals(0, test.getSeconds());
0553: assertEquals(18, test.getMillis());
0554: }
0555:
0556: public void testSetPeriod_RP4() {
0557: MutablePeriod test = new MutablePeriod(0, 0, 0, 0, 5, 6, 7, 8);
0558: test
0559: .setPeriod(new MutablePeriod(11, 12, 13, 14, 15, 16,
0560: 17, 18));
0561: assertEquals(11, test.getYears());
0562: assertEquals(12, test.getMonths());
0563: assertEquals(13, test.getWeeks());
0564: assertEquals(14, test.getDays());
0565: assertEquals(15, test.getHours());
0566: assertEquals(16, test.getMinutes());
0567: assertEquals(17, test.getSeconds());
0568: assertEquals(18, test.getMillis());
0569: }
0570:
0571: public void testSetPeriod_RP5() {
0572: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0573: test.setPeriod((ReadablePeriod) null);
0574: assertEquals(0, test.getYears());
0575: assertEquals(0, test.getMonths());
0576: assertEquals(0, test.getWeeks());
0577: assertEquals(0, test.getDays());
0578: assertEquals(0, test.getHours());
0579: assertEquals(0, test.getMinutes());
0580: assertEquals(0, test.getSeconds());
0581: assertEquals(0, test.getMillis());
0582: }
0583:
0584: //-----------------------------------------------------------------------
0585: public void testSetPeriod_long_long1() {
0586: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0587: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0588: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0589: test.setPeriod(dt1.getMillis(), dt2.getMillis());
0590: assertEquals(1, test.getYears());
0591: assertEquals(1, test.getMonths());
0592: assertEquals(1, test.getWeeks());
0593: assertEquals(1, test.getDays());
0594: assertEquals(1, test.getHours());
0595: assertEquals(1, test.getMinutes());
0596: assertEquals(1, test.getSeconds());
0597: assertEquals(1, test.getMillis());
0598: }
0599:
0600: public void testSetPeriod_long_long2() {
0601: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0602: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0603: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0604: test.setPeriod(dt2.getMillis(), dt1.getMillis());
0605: assertEquals(-1, test.getYears());
0606: assertEquals(-1, test.getMonths());
0607: assertEquals(-1, test.getWeeks());
0608: assertEquals(-1, test.getDays());
0609: assertEquals(-1, test.getHours());
0610: assertEquals(-1, test.getMinutes());
0611: assertEquals(-1, test.getSeconds());
0612: assertEquals(-1, test.getMillis());
0613: }
0614:
0615: public void testSetPeriod_long_long3() {
0616: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0617: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0618: test.setPeriod(dt1.getMillis(), dt1.getMillis());
0619: assertEquals(0, test.getYears());
0620: assertEquals(0, test.getMonths());
0621: assertEquals(0, test.getWeeks());
0622: assertEquals(0, test.getDays());
0623: assertEquals(0, test.getHours());
0624: assertEquals(0, test.getMinutes());
0625: assertEquals(0, test.getSeconds());
0626: assertEquals(0, test.getMillis());
0627: }
0628:
0629: public void testSetPeriod_long_long_NoYears() {
0630: MutablePeriod test = new MutablePeriod(PeriodType.standard()
0631: .withYearsRemoved());
0632: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0633: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0634: test.setPeriod(dt1.getMillis(), dt2.getMillis());
0635: assertEquals(0, test.getYears());
0636: assertEquals(13, test.getMonths());
0637: assertEquals(1, test.getWeeks());
0638: assertEquals(1, test.getDays());
0639: assertEquals(1, test.getHours());
0640: assertEquals(1, test.getMinutes());
0641: assertEquals(1, test.getSeconds());
0642: assertEquals(1, test.getMillis());
0643: }
0644:
0645: public void testSetPeriod_long_long_NoMonths() {
0646: MutablePeriod test = new MutablePeriod(PeriodType.standard()
0647: .withMonthsRemoved());
0648: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0649: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0650: test.setPeriod(dt1.getMillis(), dt2.getMillis());
0651: assertEquals(1, test.getYears());
0652: assertEquals(0, test.getMonths());
0653: assertEquals(5, test.getWeeks());
0654: assertEquals(3, test.getDays());
0655: assertEquals(1, test.getHours());
0656: assertEquals(1, test.getMinutes());
0657: assertEquals(1, test.getSeconds());
0658: assertEquals(1, test.getMillis());
0659: }
0660:
0661: public void testSetPeriod_long_long_NoWeeks() {
0662: MutablePeriod test = new MutablePeriod(PeriodType.standard()
0663: .withWeeksRemoved());
0664: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0665: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0666: test.setPeriod(dt1.getMillis(), dt2.getMillis());
0667: assertEquals(1, test.getYears());
0668: assertEquals(1, test.getMonths());
0669: assertEquals(0, test.getWeeks());
0670: assertEquals(8, test.getDays());
0671: assertEquals(1, test.getHours());
0672: assertEquals(1, test.getMinutes());
0673: assertEquals(1, test.getSeconds());
0674: assertEquals(1, test.getMillis());
0675: }
0676:
0677: public void testSetPeriod_long_long_NoDays() {
0678: MutablePeriod test = new MutablePeriod(PeriodType.standard()
0679: .withDaysRemoved());
0680: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0681: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0682: test.setPeriod(dt1.getMillis(), dt2.getMillis());
0683: assertEquals(1, test.getYears());
0684: assertEquals(1, test.getMonths());
0685: assertEquals(1, test.getWeeks());
0686: assertEquals(0, test.getDays());
0687: assertEquals(25, test.getHours());
0688: assertEquals(1, test.getMinutes());
0689: assertEquals(1, test.getSeconds());
0690: assertEquals(1, test.getMillis());
0691: }
0692:
0693: public void testSetPeriod_long_long_NoHours() {
0694: MutablePeriod test = new MutablePeriod(PeriodType.standard()
0695: .withHoursRemoved());
0696: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0697: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0698: test.setPeriod(dt1.getMillis(), dt2.getMillis());
0699: assertEquals(1, test.getYears());
0700: assertEquals(1, test.getMonths());
0701: assertEquals(1, test.getWeeks());
0702: assertEquals(1, test.getDays());
0703: assertEquals(0, test.getHours());
0704: assertEquals(61, test.getMinutes());
0705: assertEquals(1, test.getSeconds());
0706: assertEquals(1, test.getMillis());
0707: }
0708:
0709: public void testSetPeriod_long_long_NoMinutes() {
0710: MutablePeriod test = new MutablePeriod(PeriodType.standard()
0711: .withMinutesRemoved());
0712: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0713: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0714: test.setPeriod(dt1.getMillis(), dt2.getMillis());
0715: assertEquals(1, test.getYears());
0716: assertEquals(1, test.getMonths());
0717: assertEquals(1, test.getWeeks());
0718: assertEquals(1, test.getDays());
0719: assertEquals(1, test.getHours());
0720: assertEquals(0, test.getMinutes());
0721: assertEquals(61, test.getSeconds());
0722: assertEquals(1, test.getMillis());
0723: }
0724:
0725: public void testSetPeriod_long_long_NoSeconds() {
0726: MutablePeriod test = new MutablePeriod(PeriodType.standard()
0727: .withSecondsRemoved());
0728: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0729: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0730: test.setPeriod(dt1.getMillis(), dt2.getMillis());
0731: assertEquals(1, test.getYears());
0732: assertEquals(1, test.getMonths());
0733: assertEquals(1, test.getWeeks());
0734: assertEquals(1, test.getDays());
0735: assertEquals(1, test.getHours());
0736: assertEquals(1, test.getMinutes());
0737: assertEquals(0, test.getSeconds());
0738: assertEquals(1001, test.getMillis());
0739: }
0740:
0741: public void testSetPeriod_long_long_NoMillis() {
0742: MutablePeriod test = new MutablePeriod(PeriodType.standard()
0743: .withMillisRemoved());
0744: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0745: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0746: test.setPeriod(dt1.getMillis(), dt2.getMillis());
0747: assertEquals(1, test.getYears());
0748: assertEquals(1, test.getMonths());
0749: assertEquals(1, test.getWeeks());
0750: assertEquals(1, test.getDays());
0751: assertEquals(1, test.getHours());
0752: assertEquals(1, test.getMinutes());
0753: assertEquals(1, test.getSeconds());
0754: assertEquals(0, test.getMillis());
0755: }
0756:
0757: //-----------------------------------------------------------------------
0758: public void testSetPeriod_RI_RI1() {
0759: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0760: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0761: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0762: test.setPeriod(dt1, dt2);
0763: assertEquals(1, test.getYears());
0764: assertEquals(1, test.getMonths());
0765: assertEquals(1, test.getWeeks());
0766: assertEquals(1, test.getDays());
0767: assertEquals(1, test.getHours());
0768: assertEquals(1, test.getMinutes());
0769: assertEquals(1, test.getSeconds());
0770: assertEquals(1, test.getMillis());
0771: }
0772:
0773: public void testSetPeriod_RI_RI2() {
0774: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0775: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0776: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0777: test.setPeriod(dt2, dt1);
0778: assertEquals(-1, test.getYears());
0779: assertEquals(-1, test.getMonths());
0780: assertEquals(-1, test.getWeeks());
0781: assertEquals(-1, test.getDays());
0782: assertEquals(-1, test.getHours());
0783: assertEquals(-1, test.getMinutes());
0784: assertEquals(-1, test.getSeconds());
0785: assertEquals(-1, test.getMillis());
0786: }
0787:
0788: public void testSetPeriod_RI_RI3() {
0789: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0790: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0791: test.setPeriod(dt1, dt1);
0792: assertEquals(0, test.getYears());
0793: assertEquals(0, test.getMonths());
0794: assertEquals(0, test.getWeeks());
0795: assertEquals(0, test.getDays());
0796: assertEquals(0, test.getHours());
0797: assertEquals(0, test.getMinutes());
0798: assertEquals(0, test.getSeconds());
0799: assertEquals(0, test.getMillis());
0800: }
0801:
0802: //-----------------------------------------------------------------------
0803: public void testSetPeriod_RInterval1() {
0804: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0805: DateTime dt1 = new DateTime(2002, 6, 9, 13, 15, 17, 19);
0806: DateTime dt2 = new DateTime(2003, 7, 17, 14, 16, 18, 20);
0807: test.setPeriod(new Interval(dt1, dt2));
0808: assertEquals(1, test.getYears());
0809: assertEquals(1, test.getMonths());
0810: assertEquals(1, test.getWeeks());
0811: assertEquals(1, test.getDays());
0812: assertEquals(1, test.getHours());
0813: assertEquals(1, test.getMinutes());
0814: assertEquals(1, test.getSeconds());
0815: assertEquals(1, test.getMillis());
0816: }
0817:
0818: public void testSetPeriod_RInterval2() {
0819: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0820: test.setPeriod((ReadableInterval) null);
0821: assertEquals(0, test.getYears());
0822: assertEquals(0, test.getMonths());
0823: assertEquals(0, test.getWeeks());
0824: assertEquals(0, test.getDays());
0825: assertEquals(0, test.getHours());
0826: assertEquals(0, test.getMinutes());
0827: assertEquals(0, test.getSeconds());
0828: assertEquals(0, test.getMillis());
0829: }
0830:
0831: //-----------------------------------------------------------------------
0832: public void testSetPeriod_long1() {
0833: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
0834: test.setPeriod(100L);
0835: assertEquals(0, test.getYears());
0836: assertEquals(0, test.getMonths());
0837: assertEquals(0, test.getWeeks());
0838: assertEquals(0, test.getDays());
0839: assertEquals(0, test.getHours());
0840: assertEquals(0, test.getMinutes());
0841: assertEquals(0, test.getSeconds());
0842: assertEquals(100, test.getMillis());
0843: }
0844:
0845: public void testSetPeriod_long2() {
0846: MutablePeriod test = new MutablePeriod();
0847: test.setPeriod((4L + (3L * 7L) + (2L * 30L) + 365L)
0848: * DateTimeConstants.MILLIS_PER_DAY + 5L
0849: * DateTimeConstants.MILLIS_PER_HOUR + 6L
0850: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
0851: * DateTimeConstants.MILLIS_PER_SECOND + 8L);
0852: // only time fields are precise
0853: assertEquals(0, test.getYears()); // (4 + (3 * 7) + (2 * 30) + 365) == 450
0854: assertEquals(0, test.getMonths());
0855: assertEquals(0, test.getWeeks());
0856: assertEquals(0, test.getDays());
0857: assertEquals((450 * 24) + 5, test.getHours());
0858: assertEquals(6, test.getMinutes());
0859: assertEquals(7, test.getSeconds());
0860: assertEquals(8, test.getMillis());
0861: }
0862:
0863: // public void testSetPeriod_long3() {
0864: // MutablePeriod test = new MutablePeriod(PeriodType.getPreciseYearMonthType());
0865: // test.setPeriod(
0866: // (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
0867: // 5L * DateTimeConstants.MILLIS_PER_HOUR +
0868: // 6L * DateTimeConstants.MILLIS_PER_MINUTE +
0869: // 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L);
0870: // assertEquals(1, test.getYears());
0871: // assertEquals(2, test.getMonths());
0872: // assertEquals(0, test.getWeeks());
0873: // assertEquals(25, test.getDays());
0874: // assertEquals(5, test.getHours());
0875: // assertEquals(6, test.getMinutes());
0876: // assertEquals(7, test.getSeconds());
0877: // assertEquals(8, test.getMillis());
0878: // }
0879: //
0880: // public void testSetPeriod_long4() {
0881: // MutablePeriod test = new MutablePeriod(PeriodType.getPreciseYearWeekType());
0882: // test.setPeriod(
0883: // (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
0884: // 5L * DateTimeConstants.MILLIS_PER_HOUR +
0885: // 6L * DateTimeConstants.MILLIS_PER_MINUTE +
0886: // 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L);
0887: // assertEquals(1, test.getYears());
0888: // assertEquals(0, test.getMonths());
0889: // assertEquals(12, test.getWeeks());
0890: // assertEquals(1, test.getDays());
0891: // assertEquals(5, test.getHours());
0892: // assertEquals(6, test.getMinutes());
0893: // assertEquals(7, test.getSeconds());
0894: // assertEquals(8, test.getMillis());
0895: // }
0896: //
0897: // public void testSetPeriod_long_NoYears() {
0898: // long ms =
0899: // (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
0900: // 5L * DateTimeConstants.MILLIS_PER_HOUR +
0901: // 6L * DateTimeConstants.MILLIS_PER_MINUTE +
0902: // 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
0903: // MutablePeriod test = new MutablePeriod(PeriodType.getPreciseYearMonthType().withYearsRemoved());
0904: // test.setPeriod(ms);
0905: // assertEquals(0, test.getYears());
0906: // assertEquals(15, test.getMonths()); // totalDays=365+85=450=15*30
0907: // assertEquals(0, test.getWeeks());
0908: // assertEquals(0, test.getDays());
0909: // assertEquals(5, test.getHours());
0910: // assertEquals(6, test.getMinutes());
0911: // assertEquals(7, test.getSeconds());
0912: // assertEquals(8, test.getMillis());
0913: // assertEquals(ms, test.toDurationMillis());
0914: // }
0915: //
0916: // public void testSetPeriod_long_NoMonths() {
0917: // long ms =
0918: // (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
0919: // 5L * DateTimeConstants.MILLIS_PER_HOUR +
0920: // 6L * DateTimeConstants.MILLIS_PER_MINUTE +
0921: // 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
0922: // MutablePeriod test = new MutablePeriod(PeriodType.getPreciseYearMonthType().withMonthsRemoved());
0923: // test.setPeriod(ms);
0924: // assertEquals(1, test.getYears());
0925: // assertEquals(0, test.getMonths());
0926: // assertEquals(0, test.getWeeks());
0927: // assertEquals(85, test.getDays());
0928: // assertEquals(5, test.getHours());
0929: // assertEquals(6, test.getMinutes());
0930: // assertEquals(7, test.getSeconds());
0931: // assertEquals(8, test.getMillis());
0932: // assertEquals(ms, test.toDurationMillis());
0933: // }
0934: //
0935: // public void testSetPeriod_long_NoWeeks() {
0936: // long ms =
0937: // (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
0938: // 5L * DateTimeConstants.MILLIS_PER_HOUR +
0939: // 6L * DateTimeConstants.MILLIS_PER_MINUTE +
0940: // 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
0941: // MutablePeriod test = new MutablePeriod(PeriodType.getPreciseYearWeekType().withWeeksRemoved());
0942: // test.setPeriod(ms);
0943: // assertEquals(1, test.getYears());
0944: // assertEquals(0, test.getMonths());
0945: // assertEquals(0, test.getWeeks());
0946: // assertEquals(85, test.getDays());
0947: // assertEquals(5, test.getHours());
0948: // assertEquals(6, test.getMinutes());
0949: // assertEquals(7, test.getSeconds());
0950: // assertEquals(8, test.getMillis());
0951: // assertEquals(ms, test.toDurationMillis());
0952: // }
0953: //
0954: // public void testSetPeriod_long_NoDays() {
0955: // long ms =
0956: // (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
0957: // 5L * DateTimeConstants.MILLIS_PER_HOUR +
0958: // 6L * DateTimeConstants.MILLIS_PER_MINUTE +
0959: // 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
0960: // MutablePeriod test = new MutablePeriod(PeriodType.getPreciseYearMonthType().withDaysRemoved());
0961: // test.setPeriod(ms);
0962: // assertEquals(1, test.getYears());
0963: // assertEquals(2, test.getMonths());
0964: // assertEquals(0, test.getWeeks());
0965: // assertEquals(0, test.getDays());
0966: // assertEquals(5 + 25 * 24, test.getHours());
0967: // assertEquals(6, test.getMinutes());
0968: // assertEquals(7, test.getSeconds());
0969: // assertEquals(8, test.getMillis());
0970: // assertEquals(ms, test.toDurationMillis());
0971: // }
0972: //
0973: // public void testSetPeriod_long_NoHours() {
0974: // long ms =
0975: // (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
0976: // 5L * DateTimeConstants.MILLIS_PER_HOUR +
0977: // 6L * DateTimeConstants.MILLIS_PER_MINUTE +
0978: // 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
0979: // MutablePeriod test = new MutablePeriod(PeriodType.getPreciseYearMonthType().withHoursRemoved());
0980: // test.setPeriod(ms);
0981: // assertEquals(1, test.getYears());
0982: // assertEquals(2, test.getMonths());
0983: // assertEquals(0, test.getWeeks());
0984: // assertEquals(25, test.getDays());
0985: // assertEquals(0, test.getHours());
0986: // assertEquals(6 + 5 * 60, test.getMinutes());
0987: // assertEquals(7, test.getSeconds());
0988: // assertEquals(8, test.getMillis());
0989: // assertEquals(ms, test.toDurationMillis());
0990: // }
0991: //
0992: // public void testSetPeriod_long_NoMinutes() {
0993: // long ms =
0994: // (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
0995: // 5L * DateTimeConstants.MILLIS_PER_HOUR +
0996: // 6L * DateTimeConstants.MILLIS_PER_MINUTE +
0997: // 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
0998: // MutablePeriod test = new MutablePeriod(PeriodType.getPreciseYearMonthType().withMinutesRemoved());
0999: // test.setPeriod(ms);
1000: // assertEquals(1, test.getYears());
1001: // assertEquals(2, test.getMonths());
1002: // assertEquals(0, test.getWeeks());
1003: // assertEquals(25, test.getDays());
1004: // assertEquals(5, test.getHours());
1005: // assertEquals(0, test.getMinutes());
1006: // assertEquals(7 + 6 * 60, test.getSeconds());
1007: // assertEquals(8, test.getMillis());
1008: // assertEquals(ms, test.toDurationMillis());
1009: // }
1010: //
1011: // public void testSetPeriod_long_NoSeconds() {
1012: // long ms =
1013: // (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
1014: // 5L * DateTimeConstants.MILLIS_PER_HOUR +
1015: // 6L * DateTimeConstants.MILLIS_PER_MINUTE +
1016: // 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
1017: // MutablePeriod test = new MutablePeriod(PeriodType.getPreciseYearMonthType().withSecondsRemoved());
1018: // test.setPeriod(ms);
1019: // assertEquals(1, test.getYears());
1020: // assertEquals(2, test.getMonths());
1021: // assertEquals(0, test.getWeeks());
1022: // assertEquals(25, test.getDays());
1023: // assertEquals(5, test.getHours());
1024: // assertEquals(6, test.getMinutes());
1025: // assertEquals(0, test.getSeconds());
1026: // assertEquals(8 + 7 * 1000, test.getMillis());
1027: // assertEquals(ms, test.toDurationMillis());
1028: // }
1029: //
1030: // public void testSetPeriod_long_NoMillis() {
1031: // long ms =
1032: // (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
1033: // 5L * DateTimeConstants.MILLIS_PER_HOUR +
1034: // 6L * DateTimeConstants.MILLIS_PER_MINUTE +
1035: // 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
1036: // MutablePeriod test = new MutablePeriod(PeriodType.getPreciseYearMonthType().withMillisRemoved());
1037: // test.setPeriod(ms);
1038: // assertEquals(1, test.getYears());
1039: // assertEquals(2, test.getMonths());
1040: // assertEquals(0, test.getWeeks());
1041: // assertEquals(25, test.getDays());
1042: // assertEquals(5, test.getHours());
1043: // assertEquals(6, test.getMinutes());
1044: // assertEquals(7, test.getSeconds());
1045: // assertEquals(0, test.getMillis());
1046: // assertEquals(ms - 8, test.toDurationMillis());
1047: // }
1048:
1049: //-----------------------------------------------------------------------
1050: public void testSetPeriod_RD1() {
1051: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
1052: test.setPeriod(new Duration(100L));
1053: assertEquals(0, test.getYears());
1054: assertEquals(0, test.getMonths());
1055: assertEquals(0, test.getWeeks());
1056: assertEquals(0, test.getDays());
1057: assertEquals(0, test.getHours());
1058: assertEquals(0, test.getMinutes());
1059: assertEquals(0, test.getSeconds());
1060: assertEquals(100, test.getMillis());
1061: }
1062:
1063: public void testSetPeriod_RD2() {
1064: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
1065: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
1066: * DateTimeConstants.MILLIS_PER_DAY + 5L
1067: * DateTimeConstants.MILLIS_PER_HOUR + 6L
1068: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
1069: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
1070: test.setPeriod(new Duration(length));
1071: // only time fields are precise
1072: assertEquals(0, test.getYears()); // (4 + (3 * 7) + (2 * 30) + 365) == 450
1073: assertEquals(0, test.getMonths());
1074: assertEquals(0, test.getWeeks());
1075: assertEquals(0, test.getDays());
1076: assertEquals((450 * 24) + 5, test.getHours());
1077: assertEquals(6, test.getMinutes());
1078: assertEquals(7, test.getSeconds());
1079: assertEquals(8, test.getMillis());
1080: }
1081:
1082: public void testSetPeriod_RD3() {
1083: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
1084: test.setPeriod((ReadableDuration) null);
1085: assertEquals(0, test.getYears());
1086: assertEquals(0, test.getMonths());
1087: assertEquals(0, test.getWeeks());
1088: assertEquals(0, test.getDays());
1089: assertEquals(0, test.getHours());
1090: assertEquals(0, test.getMinutes());
1091: assertEquals(0, test.getSeconds());
1092: assertEquals(0, test.getMillis());
1093: }
1094:
1095: //-----------------------------------------------------------------------
1096: public void testAdd_8ints1() {
1097: MutablePeriod test = new MutablePeriod(100L);
1098: test.add(1, 2, 3, 4, 5, 6, 7, 8);
1099: assertEquals(1, test.getYears());
1100: assertEquals(2, test.getMonths());
1101: assertEquals(3, test.getWeeks());
1102: assertEquals(4, test.getDays());
1103: assertEquals(5, test.getHours());
1104: assertEquals(6, test.getMinutes());
1105: assertEquals(7, test.getSeconds());
1106: assertEquals(108, test.getMillis());
1107: }
1108:
1109: public void testAdd_8ints2() {
1110: MutablePeriod test = new MutablePeriod(100L, PeriodType
1111: .yearMonthDayTime());
1112: try {
1113: test.add(1, 2, 3, 4, 5, 6, 7, 8);
1114: fail();
1115: } catch (IllegalArgumentException ex) {
1116: }
1117: assertEquals(0, test.getYears());
1118: assertEquals(0, test.getMonths());
1119: assertEquals(0, test.getWeeks());
1120: assertEquals(0, test.getDays());
1121: assertEquals(0, test.getHours());
1122: assertEquals(0, test.getMinutes());
1123: assertEquals(0, test.getSeconds());
1124: assertEquals(100, test.getMillis());
1125: }
1126:
1127: //-----------------------------------------------------------------------
1128: public void testAdd_long1() {
1129: MutablePeriod test = new MutablePeriod(100L);
1130: test.add(100L);
1131: assertEquals(0, test.getYears());
1132: assertEquals(0, test.getMonths());
1133: assertEquals(0, test.getWeeks());
1134: assertEquals(0, test.getDays());
1135: assertEquals(0, test.getHours());
1136: assertEquals(0, test.getMinutes());
1137: assertEquals(0, test.getSeconds());
1138: assertEquals(200, test.getMillis());
1139: }
1140:
1141: public void testAdd_long2() {
1142: MutablePeriod test = new MutablePeriod(100L, PeriodType
1143: .standard());
1144: long ms = (4L + (3L * 7L) + (2L * 30L) + 365L)
1145: * DateTimeConstants.MILLIS_PER_DAY + 5L
1146: * DateTimeConstants.MILLIS_PER_HOUR + 6L
1147: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
1148: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
1149: test.add(ms);
1150: // only time fields are precise
1151: assertEquals(0, test.getYears()); // (4 + (3 * 7) + (2 * 30) + 365) == 450
1152: assertEquals(0, test.getMonths());
1153: assertEquals(0, test.getWeeks());
1154: assertEquals(0, test.getDays());
1155: assertEquals((450 * 24) + 5, test.getHours());
1156: assertEquals(6, test.getMinutes());
1157: assertEquals(7, test.getSeconds());
1158: assertEquals(108, test.getMillis());
1159: }
1160:
1161: public void testAdd_long3() {
1162: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
1163: test.add(2100L);
1164: assertEquals(1, test.getYears());
1165: assertEquals(2, test.getMonths());
1166: assertEquals(3, test.getWeeks());
1167: assertEquals(4, test.getDays());
1168: assertEquals(5, test.getHours());
1169: assertEquals(6, test.getMinutes());
1170: assertEquals(9, test.getSeconds());
1171: assertEquals(108, test.getMillis());
1172: }
1173:
1174: //-----------------------------------------------------------------------
1175: public void testAdd_long_Chronology1() {
1176: MutablePeriod test = new MutablePeriod(100L);
1177: test.add(100L, ISOChronology.getInstance());
1178: assertEquals(0, test.getYears());
1179: assertEquals(0, test.getMonths());
1180: assertEquals(0, test.getWeeks());
1181: assertEquals(0, test.getDays());
1182: assertEquals(0, test.getHours());
1183: assertEquals(0, test.getMinutes());
1184: assertEquals(0, test.getSeconds());
1185: assertEquals(200, test.getMillis());
1186: }
1187:
1188: public void testAdd_long_Chronology2() {
1189: MutablePeriod test = new MutablePeriod(100L, PeriodType
1190: .standard());
1191: long ms = (4L + (3L * 7L) + (2L * 30L) + 365L)
1192: * DateTimeConstants.MILLIS_PER_DAY + 5L
1193: * DateTimeConstants.MILLIS_PER_HOUR + 6L
1194: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
1195: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
1196: test.add(ms, ISOChronology.getInstance());
1197: // only time fields are precise
1198: assertEquals(0, test.getYears()); // (4 + (3 * 7) + (2 * 30) + 365) == 450 days
1199: assertEquals(0, test.getMonths());
1200: assertEquals(0, test.getWeeks());
1201: assertEquals(0, test.getDays());
1202: assertEquals((450 * 24) + 5, test.getHours());
1203: assertEquals(6, test.getMinutes());
1204: assertEquals(7, test.getSeconds());
1205: assertEquals(108, test.getMillis());
1206: }
1207:
1208: public void testAdd_long_Chronology3() {
1209: MutablePeriod test = new MutablePeriod(100L, PeriodType
1210: .standard());
1211: long ms = (4L + (3L * 7L) + (2L * 30L) + 365L)
1212: * DateTimeConstants.MILLIS_PER_DAY + 5L
1213: * DateTimeConstants.MILLIS_PER_HOUR + 6L
1214: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
1215: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
1216: test.add(ms, ISOChronology.getInstanceUTC());
1217: // UTC, so weeks and day also precise
1218: assertEquals(0, test.getYears()); // (4 + (3 * 7) + (2 * 30) + 365) == 450 days
1219: assertEquals(0, test.getMonths());
1220: assertEquals(64, test.getWeeks());
1221: assertEquals(2, test.getDays());
1222: assertEquals(5, test.getHours());
1223: assertEquals(6, test.getMinutes());
1224: assertEquals(7, test.getSeconds());
1225: assertEquals(108, test.getMillis());
1226: }
1227:
1228: //-----------------------------------------------------------------------
1229: public void testAdd_RD1() {
1230: MutablePeriod test = new MutablePeriod(100L);
1231: test.add(new Duration(100L));
1232: assertEquals(0, test.getYears());
1233: assertEquals(0, test.getMonths());
1234: assertEquals(0, test.getWeeks());
1235: assertEquals(0, test.getDays());
1236: assertEquals(0, test.getHours());
1237: assertEquals(0, test.getMinutes());
1238: assertEquals(0, test.getSeconds());
1239: assertEquals(200, test.getMillis());
1240: }
1241:
1242: public void testAdd_RD2() {
1243: MutablePeriod test = new MutablePeriod(100L, PeriodType
1244: .yearMonthDayTime());
1245: long ms = (4L + (3L * 7L)) * DateTimeConstants.MILLIS_PER_DAY
1246: + 5L * DateTimeConstants.MILLIS_PER_HOUR + 6L
1247: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
1248: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
1249: test.add(new Duration(ms));
1250: assertEquals(0, test.getYears());
1251: assertEquals(0, test.getMonths());
1252: assertEquals(0, test.getWeeks());
1253: assertEquals(0, test.getDays());
1254: assertEquals((4 + (3 * 7)) * 24 + 5, test.getHours());
1255: assertEquals(6, test.getMinutes());
1256: assertEquals(7, test.getSeconds());
1257: assertEquals(108, test.getMillis());
1258: }
1259:
1260: public void testAdd_RD3() {
1261: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
1262: test.add((ReadableDuration) null);
1263: assertEquals(1, test.getYears());
1264: assertEquals(2, test.getMonths());
1265: assertEquals(3, test.getWeeks());
1266: assertEquals(4, test.getDays());
1267: assertEquals(5, test.getHours());
1268: assertEquals(6, test.getMinutes());
1269: assertEquals(7, test.getSeconds());
1270: assertEquals(8, test.getMillis());
1271: }
1272:
1273: //-----------------------------------------------------------------------
1274: public void testAdd_RP1() {
1275: MutablePeriod test = new MutablePeriod(100L);
1276: test.add(new Period(100L));
1277: assertEquals(0, test.getYears());
1278: assertEquals(0, test.getMonths());
1279: assertEquals(0, test.getWeeks());
1280: assertEquals(0, test.getDays());
1281: assertEquals(0, test.getHours());
1282: assertEquals(0, test.getMinutes());
1283: assertEquals(0, test.getSeconds());
1284: assertEquals(200, test.getMillis());
1285: }
1286:
1287: public void testAdd_RP2() {
1288: MutablePeriod test = new MutablePeriod(100L, PeriodType
1289: .standard()); // All type
1290: test.add(new Period(1, 2, 3, 4, 5, 6, 7, 0, PeriodType
1291: .standard().withMillisRemoved()));
1292: // add field value, ignore different types
1293: assertEquals(1, test.getYears());
1294: assertEquals(2, test.getMonths());
1295: assertEquals(3, test.getWeeks());
1296: assertEquals(4, test.getDays());
1297: assertEquals(5, test.getHours());
1298: assertEquals(6, test.getMinutes());
1299: assertEquals(7, test.getSeconds());
1300: assertEquals(100, test.getMillis());
1301: }
1302:
1303: public void testAdd_RP3() {
1304: MutablePeriod test = new MutablePeriod(100L, PeriodType
1305: .standard());
1306: test.add(new Period(0L));
1307: assertEquals(0, test.getYears());
1308: assertEquals(0, test.getMonths());
1309: assertEquals(0, test.getWeeks());
1310: assertEquals(0, test.getDays());
1311: assertEquals(0, test.getHours());
1312: assertEquals(0, test.getMinutes());
1313: assertEquals(0, test.getSeconds());
1314: assertEquals(100, test.getMillis());
1315: }
1316:
1317: public void testAdd_RP4() {
1318: MutablePeriod test = new MutablePeriod(1, 2, 0, 4, 5, 6, 7, 8,
1319: PeriodType.yearMonthDayTime());
1320: try {
1321: test.add(new Period(1, 2, 3, 4, 5, 6, 7, 8)); // cannot set weeks
1322: fail();
1323: } catch (IllegalArgumentException ex) {
1324: }
1325: assertEquals(1, test.getYears());
1326: assertEquals(2, test.getMonths());
1327: assertEquals(0, test.getWeeks());
1328: assertEquals(4, test.getDays());
1329: assertEquals(5, test.getHours());
1330: assertEquals(6, test.getMinutes());
1331: assertEquals(7, test.getSeconds());
1332: assertEquals(8, test.getMillis());
1333: }
1334:
1335: public void testAdd_RP5() {
1336: MutablePeriod test = new MutablePeriod(1, 2, 0, 4, 5, 6, 7, 8,
1337: PeriodType.yearMonthDayTime());
1338: test.add(new Period(1, 2, 0, 4, 5, 6, 7, 8)); // can set weeks as zero
1339: assertEquals(2, test.getYears());
1340: assertEquals(4, test.getMonths());
1341: assertEquals(0, test.getWeeks());
1342: assertEquals(8, test.getDays());
1343: assertEquals(10, test.getHours());
1344: assertEquals(12, test.getMinutes());
1345: assertEquals(14, test.getSeconds());
1346: assertEquals(16, test.getMillis());
1347: }
1348:
1349: public void testAdd_RP6() {
1350: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
1351: test.add((ReadablePeriod) null);
1352: assertEquals(1, test.getYears());
1353: assertEquals(2, test.getMonths());
1354: assertEquals(3, test.getWeeks());
1355: assertEquals(4, test.getDays());
1356: assertEquals(5, test.getHours());
1357: assertEquals(6, test.getMinutes());
1358: assertEquals(7, test.getSeconds());
1359: assertEquals(8, test.getMillis());
1360: }
1361:
1362: //-----------------------------------------------------------------------
1363: public void testAdd_RInterval1() {
1364: MutablePeriod test = new MutablePeriod(100L);
1365: test.add(new Interval(100L, 200L));
1366: assertEquals(0, test.getYears());
1367: assertEquals(0, test.getMonths());
1368: assertEquals(0, test.getWeeks());
1369: assertEquals(0, test.getDays());
1370: assertEquals(0, test.getHours());
1371: assertEquals(0, test.getMinutes());
1372: assertEquals(0, test.getSeconds());
1373: assertEquals(200, test.getMillis());
1374: }
1375:
1376: public void testAdd_RInterval2() {
1377: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
1378: DateTime dt2 = new DateTime(2005, 12, 18, 0, 0, 0, 8);
1379: MutablePeriod test = new MutablePeriod(100L); // All type
1380: test.add(new Interval(dt1, dt2));
1381: assertEquals(1, test.getYears()); // add field value from interval
1382: assertEquals(6, test.getMonths()); // add field value from interval
1383: assertEquals(1, test.getWeeks()); // add field value from interval
1384: assertEquals(2, test.getDays()); // add field value from interval
1385: assertEquals(0, test.getHours()); // time zone OK
1386: assertEquals(0, test.getMinutes());
1387: assertEquals(0, test.getSeconds());
1388: assertEquals(108, test.getMillis());
1389: }
1390:
1391: public void testAdd_RInterval3() {
1392: MutablePeriod test = new MutablePeriod(100L, PeriodType
1393: .yearMonthDayTime());
1394: test.add(new Interval(0L, 0L));
1395: assertEquals(0, test.getYears());
1396: assertEquals(0, test.getMonths());
1397: assertEquals(0, test.getWeeks());
1398: assertEquals(0, test.getDays());
1399: assertEquals(0, test.getHours());
1400: assertEquals(0, test.getMinutes());
1401: assertEquals(0, test.getSeconds());
1402: assertEquals(100, test.getMillis());
1403: }
1404:
1405: public void testAdd_RInterval4() {
1406: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
1407: DateTime dt2 = new DateTime(2005, 7, 17, 0, 0, 0, 8);
1408: MutablePeriod test = new MutablePeriod(100L, PeriodType
1409: .yearMonthDayTime());
1410: test.add(new Interval(dt1, dt2));
1411: assertEquals(1, test.getYears());
1412: assertEquals(1, test.getMonths());
1413: assertEquals(0, test.getWeeks()); // no weeks
1414: assertEquals(8, test.getDays()); // week added to days
1415: assertEquals(0, test.getHours());
1416: assertEquals(0, test.getMinutes());
1417: assertEquals(0, test.getSeconds());
1418: assertEquals(108, test.getMillis());
1419: }
1420:
1421: public void testAdd_RInterval5() {
1422: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
1423: test.add((ReadableInterval) null);
1424: assertEquals(1, test.getYears());
1425: assertEquals(2, test.getMonths());
1426: assertEquals(3, test.getWeeks());
1427: assertEquals(4, test.getDays());
1428: assertEquals(5, test.getHours());
1429: assertEquals(6, test.getMinutes());
1430: assertEquals(7, test.getSeconds());
1431: assertEquals(8, test.getMillis());
1432: }
1433:
1434: //-----------------------------------------------------------------------
1435: public void testMergePeriod_RP1() {
1436: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
1437: test.mergePeriod(new MutablePeriod(0, 0, 0, 14, 15, 16, 17, 18,
1438: PeriodType.dayTime()));
1439: assertEquals(1, test.getYears());
1440: assertEquals(2, test.getMonths());
1441: assertEquals(3, test.getWeeks());
1442: assertEquals(14, test.getDays());
1443: assertEquals(15, test.getHours());
1444: assertEquals(16, test.getMinutes());
1445: assertEquals(17, test.getSeconds());
1446: assertEquals(18, test.getMillis());
1447: }
1448:
1449: public void testMergePeriod_RP2() {
1450: MutablePeriod test = new MutablePeriod(100L, PeriodType
1451: .millis());
1452: try {
1453: test.mergePeriod(new MutablePeriod(11, 12, 13, 14, 15, 16,
1454: 17, 18));
1455: fail();
1456: } catch (IllegalArgumentException ex) {
1457: }
1458: assertEquals(0, test.getYears());
1459: assertEquals(0, test.getMonths());
1460: assertEquals(0, test.getWeeks());
1461: assertEquals(0, test.getDays());
1462: assertEquals(0, test.getHours());
1463: assertEquals(0, test.getMinutes());
1464: assertEquals(0, test.getSeconds());
1465: assertEquals(100, test.getMillis());
1466: }
1467:
1468: public void testMergePeriod_RP3() {
1469: MutablePeriod test = new MutablePeriod(100L, PeriodType
1470: .millis());
1471: test.mergePeriod(new MutablePeriod(0, 0, 0, 0, 0, 0, 0, 18));
1472: assertEquals(0, test.getYears());
1473: assertEquals(0, test.getMonths());
1474: assertEquals(0, test.getWeeks());
1475: assertEquals(0, test.getDays());
1476: assertEquals(0, test.getHours());
1477: assertEquals(0, test.getMinutes());
1478: assertEquals(0, test.getSeconds());
1479: assertEquals(18, test.getMillis());
1480: }
1481:
1482: public void testMergePeriod_RP4() {
1483: MutablePeriod test = new MutablePeriod(0, 0, 0, 0, 5, 6, 7, 8);
1484: test.mergePeriod(new MutablePeriod(11, 12, 13, 14, 15, 16, 17,
1485: 18));
1486: assertEquals(11, test.getYears());
1487: assertEquals(12, test.getMonths());
1488: assertEquals(13, test.getWeeks());
1489: assertEquals(14, test.getDays());
1490: assertEquals(15, test.getHours());
1491: assertEquals(16, test.getMinutes());
1492: assertEquals(17, test.getSeconds());
1493: assertEquals(18, test.getMillis());
1494: }
1495:
1496: public void testMergePeriod_RP5() {
1497: MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
1498: test.mergePeriod((ReadablePeriod) null);
1499: assertEquals(1, test.getYears());
1500: assertEquals(2, test.getMonths());
1501: assertEquals(3, test.getWeeks());
1502: assertEquals(4, test.getDays());
1503: assertEquals(5, test.getHours());
1504: assertEquals(6, test.getMinutes());
1505: assertEquals(7, test.getSeconds());
1506: assertEquals(8, test.getMillis());
1507: }
1508:
1509: }
|