0001: /*
0002: * Copyright 2001-2006 Stephen Colebourne
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.joda.time;
0017:
0018: import java.io.ByteArrayInputStream;
0019: import java.io.ByteArrayOutputStream;
0020: import java.io.ObjectInputStream;
0021: import java.io.ObjectOutputStream;
0022: import java.util.Locale;
0023: import java.util.TimeZone;
0024:
0025: import junit.framework.TestCase;
0026: import junit.framework.TestSuite;
0027:
0028: import org.joda.time.base.AbstractInterval;
0029: import org.joda.time.chrono.BuddhistChronology;
0030: import org.joda.time.chrono.CopticChronology;
0031: import org.joda.time.chrono.GJChronology;
0032: import org.joda.time.chrono.ISOChronology;
0033: import org.joda.time.chrono.LenientChronology;
0034:
0035: /**
0036: * This class is a Junit unit test for Instant.
0037: *
0038: * @author Stephen Colebourne
0039: */
0040: public class TestInterval_Basics extends TestCase {
0041: // Test in 2002/03 as time zones are more well known
0042: // (before the late 90's they were all over the place)
0043:
0044: private static final DateTimeZone MOSCOW = DateTimeZone
0045: .forID("Europe/Moscow");
0046: private static final DateTimeZone PARIS = DateTimeZone
0047: .forID("Europe/Paris");
0048: private static final DateTimeZone LONDON = DateTimeZone
0049: .forID("Europe/London");
0050: private static final Chronology COPTIC_PARIS = CopticChronology
0051: .getInstance(PARIS);
0052: private Interval interval37;
0053: private Interval interval33;
0054:
0055: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0056: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0057: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0058: + 365 + 365 + 366 + 365;
0059: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0060: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0061: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0062: + 365 + 365 + 366 + 365 + 365;
0063:
0064: // 2002-06-09
0065: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
0066: + 31L + 9L - 1L)
0067: * DateTimeConstants.MILLIS_PER_DAY;
0068:
0069: // 2002-04-05
0070: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
0071: * DateTimeConstants.MILLIS_PER_DAY + 12L
0072: * DateTimeConstants.MILLIS_PER_HOUR + 24L
0073: * DateTimeConstants.MILLIS_PER_MINUTE;
0074:
0075: // 2003-05-06
0076: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
0077: * DateTimeConstants.MILLIS_PER_DAY
0078: + 14L
0079: * DateTimeConstants.MILLIS_PER_HOUR
0080: + 28L
0081: * DateTimeConstants.MILLIS_PER_MINUTE;
0082:
0083: private DateTimeZone originalDateTimeZone = null;
0084: private TimeZone originalTimeZone = null;
0085: private Locale originalLocale = null;
0086:
0087: public static void main(String[] args) {
0088: junit.textui.TestRunner.run(suite());
0089: }
0090:
0091: public static TestSuite suite() {
0092: return new TestSuite(TestInterval_Basics.class);
0093: }
0094:
0095: public TestInterval_Basics(String name) {
0096: super (name);
0097: }
0098:
0099: protected void setUp() throws Exception {
0100: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
0101: originalDateTimeZone = DateTimeZone.getDefault();
0102: originalTimeZone = TimeZone.getDefault();
0103: originalLocale = Locale.getDefault();
0104: DateTimeZone.setDefault(PARIS);
0105: TimeZone.setDefault(PARIS.toTimeZone());
0106: Locale.setDefault(Locale.FRANCE);
0107: interval37 = new Interval(3, 7);
0108: interval33 = new Interval(3, 3);
0109: }
0110:
0111: protected void tearDown() throws Exception {
0112: DateTimeUtils.setCurrentMillisSystem();
0113: DateTimeZone.setDefault(originalDateTimeZone);
0114: TimeZone.setDefault(originalTimeZone);
0115: Locale.setDefault(originalLocale);
0116: originalDateTimeZone = null;
0117: originalTimeZone = null;
0118: originalLocale = null;
0119: }
0120:
0121: //-----------------------------------------------------------------------
0122: public void testTest() {
0123: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
0124: TEST_TIME_NOW).toString());
0125: assertEquals("2002-04-05T12:24:00.000Z",
0126: new Instant(TEST_TIME1).toString());
0127: assertEquals("2003-05-06T14:28:00.000Z",
0128: new Instant(TEST_TIME2).toString());
0129: }
0130:
0131: //-----------------------------------------------------------------------
0132: public void testGetMillis() {
0133: Interval test = new Interval(TEST_TIME1, TEST_TIME2);
0134: assertEquals(TEST_TIME1, test.getStartMillis());
0135: assertEquals(TEST_TIME1, test.getStart().getMillis());
0136: assertEquals(TEST_TIME2, test.getEndMillis());
0137: assertEquals(TEST_TIME2, test.getEnd().getMillis());
0138: assertEquals(TEST_TIME2 - TEST_TIME1, test.toDurationMillis());
0139: assertEquals(TEST_TIME2 - TEST_TIME1, test.toDuration()
0140: .getMillis());
0141: }
0142:
0143: public void testGetDuration1() {
0144: Interval test = new Interval(TEST_TIME1, TEST_TIME2);
0145: assertEquals(TEST_TIME2 - TEST_TIME1, test.toDurationMillis());
0146: assertEquals(TEST_TIME2 - TEST_TIME1, test.toDuration()
0147: .getMillis());
0148: }
0149:
0150: public void testGetDuration2() {
0151: Interval test = new Interval(TEST_TIME1, TEST_TIME1);
0152: assertSame(Duration.ZERO, test.toDuration());
0153: }
0154:
0155: public void testEqualsHashCode() {
0156: Interval test1 = new Interval(TEST_TIME1, TEST_TIME2);
0157: Interval test2 = new Interval(TEST_TIME1, TEST_TIME2);
0158: assertEquals(true, test1.equals(test2));
0159: assertEquals(true, test2.equals(test1));
0160: assertEquals(true, test1.equals(test1));
0161: assertEquals(true, test2.equals(test2));
0162: assertEquals(true, test1.hashCode() == test2.hashCode());
0163: assertEquals(true, test1.hashCode() == test1.hashCode());
0164: assertEquals(true, test2.hashCode() == test2.hashCode());
0165:
0166: Interval test3 = new Interval(TEST_TIME_NOW, TEST_TIME2);
0167: assertEquals(false, test1.equals(test3));
0168: assertEquals(false, test2.equals(test3));
0169: assertEquals(false, test3.equals(test1));
0170: assertEquals(false, test3.equals(test2));
0171: assertEquals(false, test1.hashCode() == test3.hashCode());
0172: assertEquals(false, test2.hashCode() == test3.hashCode());
0173:
0174: Interval test4 = new Interval(TEST_TIME1, TEST_TIME2,
0175: GJChronology.getInstance());
0176: assertEquals(true, test4.equals(test4));
0177: assertEquals(false, test1.equals(test4));
0178: assertEquals(false, test2.equals(test4));
0179: assertEquals(false, test4.equals(test1));
0180: assertEquals(false, test4.equals(test2));
0181: assertEquals(false, test1.hashCode() == test4.hashCode());
0182: assertEquals(false, test2.hashCode() == test4.hashCode());
0183:
0184: MutableInterval test5 = new MutableInterval(TEST_TIME1,
0185: TEST_TIME2);
0186: assertEquals(true, test1.equals(test5));
0187: assertEquals(true, test2.equals(test5));
0188: assertEquals(false, test3.equals(test5));
0189: assertEquals(true, test5.equals(test1));
0190: assertEquals(true, test5.equals(test2));
0191: assertEquals(false, test5.equals(test3));
0192: assertEquals(true, test1.hashCode() == test5.hashCode());
0193: assertEquals(true, test2.hashCode() == test5.hashCode());
0194: assertEquals(false, test3.hashCode() == test5.hashCode());
0195:
0196: assertEquals(false, test1.equals("Hello"));
0197: assertEquals(true, test1.equals(new MockInterval()));
0198: assertEquals(false, test1.equals(new DateTime(TEST_TIME1)));
0199: }
0200:
0201: class MockInterval extends AbstractInterval {
0202: public MockInterval() {
0203: super ();
0204: }
0205:
0206: public Chronology getChronology() {
0207: return ISOChronology.getInstance();
0208: }
0209:
0210: public long getStartMillis() {
0211: return TEST_TIME1;
0212: }
0213:
0214: public long getEndMillis() {
0215: return TEST_TIME2;
0216: }
0217: }
0218:
0219: public void testEqualsHashCodeLenient() {
0220: Interval test1 = new Interval(new DateTime(TEST_TIME1,
0221: LenientChronology.getInstance(COPTIC_PARIS)),
0222: new DateTime(TEST_TIME2, LenientChronology
0223: .getInstance(COPTIC_PARIS)));
0224: Interval test2 = new Interval(new DateTime(TEST_TIME1,
0225: LenientChronology.getInstance(COPTIC_PARIS)),
0226: new DateTime(TEST_TIME2, LenientChronology
0227: .getInstance(COPTIC_PARIS)));
0228: assertEquals(true, test1.equals(test2));
0229: assertEquals(true, test2.equals(test1));
0230: assertEquals(true, test1.equals(test1));
0231: assertEquals(true, test2.equals(test2));
0232: assertEquals(true, test1.hashCode() == test2.hashCode());
0233: assertEquals(true, test1.hashCode() == test1.hashCode());
0234: assertEquals(true, test2.hashCode() == test2.hashCode());
0235: }
0236:
0237: public void testEqualsHashCodeStrict() {
0238: Interval test1 = new Interval(new DateTime(TEST_TIME1,
0239: LenientChronology.getInstance(COPTIC_PARIS)),
0240: new DateTime(TEST_TIME2, LenientChronology
0241: .getInstance(COPTIC_PARIS)));
0242: Interval test2 = new Interval(new DateTime(TEST_TIME1,
0243: LenientChronology.getInstance(COPTIC_PARIS)),
0244: new DateTime(TEST_TIME2, LenientChronology
0245: .getInstance(COPTIC_PARIS)));
0246: assertEquals(true, test1.equals(test2));
0247: assertEquals(true, test2.equals(test1));
0248: assertEquals(true, test1.equals(test1));
0249: assertEquals(true, test2.equals(test2));
0250: assertEquals(true, test1.hashCode() == test2.hashCode());
0251: assertEquals(true, test1.hashCode() == test1.hashCode());
0252: assertEquals(true, test2.hashCode() == test2.hashCode());
0253: }
0254:
0255: //-----------------------------------------------------------------------
0256: public void test_useCase_ContainsOverlapAbutGap() {
0257: // this is a simple test to ensure that the use case of these methods is OK
0258: // when comparing any two intervals they can be in one and only one of these states
0259: // (a) have a gap between them, (b) abut or (c) overlap
0260: // contains is a subset of overlap
0261: Interval test1020 = new Interval(10, 20);
0262:
0263: // [4,8) [10,20) - gap
0264: Interval interval = new Interval(4, 8);
0265: assertNotNull(test1020.gap(interval));
0266: assertEquals(false, test1020.abuts(interval));
0267: assertEquals(false, test1020.overlaps(interval));
0268: assertEquals(false, test1020.contains(interval));
0269: assertNotNull(interval.gap(test1020));
0270: assertEquals(false, interval.abuts(test1020));
0271: assertEquals(false, interval.overlaps(test1020));
0272: assertEquals(false, interval.contains(test1020));
0273:
0274: // [6,10) [10,20) - abuts
0275: interval = new Interval(6, 10);
0276: assertNull(test1020.gap(interval));
0277: assertEquals(true, test1020.abuts(interval));
0278: assertEquals(false, test1020.overlaps(interval));
0279: assertEquals(false, test1020.contains(interval));
0280: assertNull(interval.gap(test1020));
0281: assertEquals(true, interval.abuts(test1020));
0282: assertEquals(false, interval.overlaps(test1020));
0283: assertEquals(false, interval.contains(test1020));
0284:
0285: // [8,12) [10,20) - overlaps
0286: interval = new Interval(8, 12);
0287: assertNull(test1020.gap(interval));
0288: assertEquals(false, test1020.abuts(interval));
0289: assertEquals(true, test1020.overlaps(interval));
0290: assertEquals(false, test1020.contains(interval));
0291: assertNull(interval.gap(test1020));
0292: assertEquals(false, interval.abuts(test1020));
0293: assertEquals(true, interval.overlaps(test1020));
0294: assertEquals(false, interval.contains(test1020));
0295:
0296: // [10,14) [10,20) - overlaps and contains-one-way
0297: interval = new Interval(10, 14);
0298: assertNull(test1020.gap(interval));
0299: assertEquals(false, test1020.abuts(interval));
0300: assertEquals(true, test1020.overlaps(interval));
0301: assertEquals(true, test1020.contains(interval));
0302: assertNull(interval.gap(test1020));
0303: assertEquals(false, interval.abuts(test1020));
0304: assertEquals(true, interval.overlaps(test1020));
0305: assertEquals(false, interval.contains(test1020));
0306:
0307: // [10,20) [10,20) - overlaps and contains-both-ways
0308: assertNull(test1020.gap(interval));
0309: assertEquals(false, test1020.abuts(test1020));
0310: assertEquals(true, test1020.overlaps(test1020));
0311: assertEquals(true, test1020.contains(test1020));
0312:
0313: // [10,20) [16,20) - overlaps and contains-one-way
0314: interval = new Interval(16, 20);
0315: assertNull(test1020.gap(interval));
0316: assertEquals(false, test1020.abuts(interval));
0317: assertEquals(true, test1020.overlaps(interval));
0318: assertEquals(true, test1020.contains(interval));
0319: assertNull(interval.gap(test1020));
0320: assertEquals(false, interval.abuts(test1020));
0321: assertEquals(true, interval.overlaps(test1020));
0322: assertEquals(false, interval.contains(test1020));
0323:
0324: // [10,20) [18,22) - overlaps
0325: interval = new Interval(18, 22);
0326: assertNull(test1020.gap(interval));
0327: assertEquals(false, test1020.abuts(interval));
0328: assertEquals(true, test1020.overlaps(interval));
0329: assertEquals(false, test1020.contains(interval));
0330: assertNull(interval.gap(test1020));
0331: assertEquals(false, interval.abuts(test1020));
0332: assertEquals(true, interval.overlaps(test1020));
0333: assertEquals(false, interval.contains(test1020));
0334:
0335: // [10,20) [20,24) - abuts
0336: interval = new Interval(20, 24);
0337: assertNull(test1020.gap(interval));
0338: assertEquals(true, test1020.abuts(interval));
0339: assertEquals(false, test1020.overlaps(interval));
0340: assertEquals(false, test1020.contains(interval));
0341: assertNull(interval.gap(test1020));
0342: assertEquals(true, interval.abuts(test1020));
0343: assertEquals(false, interval.overlaps(test1020));
0344: assertEquals(false, interval.contains(test1020));
0345:
0346: // [10,20) [22,26) - gap
0347: interval = new Interval(22, 26);
0348: assertNotNull(test1020.gap(interval));
0349: assertEquals(false, test1020.abuts(interval));
0350: assertEquals(false, test1020.overlaps(interval));
0351: assertEquals(false, test1020.contains(interval));
0352: assertNotNull(interval.gap(test1020));
0353: assertEquals(false, interval.abuts(test1020));
0354: assertEquals(false, interval.overlaps(test1020));
0355: assertEquals(false, interval.contains(test1020));
0356: }
0357:
0358: //-----------------------------------------------------------------------
0359: public void test_useCase_ContainsOverlapAbutGap_zeroDuration() {
0360: // this is a simple test to ensure that the use case of these methods
0361: // is OK when considering a zero duration inerval
0362: // when comparing any two intervals they can be in one and only one of these states
0363: // (a) have a gap between them, (b) abut or (c) overlap
0364: // contains is a subset of overlap
0365: Interval test1020 = new Interval(10, 20);
0366:
0367: // [8,8) [10,20) - gap
0368: Interval interval = new Interval(8, 8);
0369: assertNotNull(test1020.gap(interval));
0370: assertEquals(false, test1020.abuts(interval));
0371: assertEquals(false, test1020.overlaps(interval));
0372: assertEquals(false, test1020.contains(interval));
0373: assertNotNull(interval.gap(test1020));
0374: assertEquals(false, interval.abuts(test1020));
0375: assertEquals(false, interval.overlaps(test1020));
0376: assertEquals(false, interval.contains(test1020));
0377:
0378: // [10,10) [10,20) - abuts and contains-one-way
0379: interval = new Interval(10, 10);
0380: assertNull(test1020.gap(interval));
0381: assertEquals(true, test1020.abuts(interval));
0382: assertEquals(false, test1020.overlaps(interval)); // abuts, so can't overlap
0383: assertEquals(true, test1020.contains(interval)); // normal contains zero-duration
0384: assertNull(interval.gap(test1020));
0385: assertEquals(true, interval.abuts(test1020));
0386: assertEquals(false, interval.overlaps(test1020)); // abuts, so can't overlap
0387: assertEquals(false, interval.contains(test1020)); // zero-duration does not contain normal
0388:
0389: // [12,12) [10,20) - contains-one-way and overlaps
0390: interval = new Interval(12, 12);
0391: assertNull(test1020.gap(interval));
0392: assertEquals(false, test1020.abuts(interval));
0393: assertEquals(true, test1020.overlaps(interval));
0394: assertEquals(true, test1020.contains(interval)); // normal contains zero-duration
0395: assertNull(interval.gap(test1020));
0396: assertEquals(false, interval.abuts(test1020));
0397: assertEquals(true, interval.overlaps(test1020));
0398: assertEquals(false, interval.contains(test1020)); // zero-duration does not contain normal
0399:
0400: // [10,20) [20,20) - abuts
0401: interval = new Interval(20, 20);
0402: assertNull(test1020.gap(interval));
0403: assertEquals(true, test1020.abuts(interval));
0404: assertEquals(false, test1020.overlaps(interval));
0405: assertEquals(false, test1020.contains(interval));
0406: assertNull(interval.gap(test1020));
0407: assertEquals(true, interval.abuts(test1020));
0408: assertEquals(false, interval.overlaps(test1020));
0409: assertEquals(false, interval.contains(test1020));
0410:
0411: // [10,20) [22,22) - gap
0412: interval = new Interval(22, 22);
0413: assertNotNull(test1020.gap(interval));
0414: assertEquals(false, test1020.abuts(interval));
0415: assertEquals(false, test1020.overlaps(interval));
0416: assertEquals(false, test1020.contains(interval));
0417: assertNotNull(interval.gap(test1020));
0418: assertEquals(false, interval.abuts(test1020));
0419: assertEquals(false, interval.overlaps(test1020));
0420: assertEquals(false, interval.contains(test1020));
0421: }
0422:
0423: //-----------------------------------------------------------------------
0424: public void test_useCase_ContainsOverlapAbutGap_bothZeroDuration() {
0425: // this is a simple test to ensure that the use case of these methods
0426: // is OK when considering two zero duration inervals
0427: // this is the simplest case, as the two intervals either have a gap or not
0428: // if not, then they are equal and abut
0429: Interval test0808 = new Interval(8, 8);
0430: Interval test1010 = new Interval(10, 10);
0431:
0432: // [8,8) [10,10) - gap
0433: assertNotNull(test1010.gap(test0808));
0434: assertEquals(false, test1010.abuts(test0808));
0435: assertEquals(false, test1010.overlaps(test0808));
0436: assertEquals(false, test1010.contains(test0808));
0437: assertNotNull(test0808.gap(test1010));
0438: assertEquals(false, test0808.abuts(test1010));
0439: assertEquals(false, test0808.overlaps(test1010));
0440: assertEquals(false, test0808.contains(test1010));
0441:
0442: // [10,10) [10,10) - abuts
0443: assertNull(test1010.gap(test1010));
0444: assertEquals(true, test1010.abuts(test1010));
0445: assertEquals(false, test1010.overlaps(test1010));
0446: assertEquals(false, test1010.contains(test1010));
0447: }
0448:
0449: //-----------------------------------------------------------------------
0450: public void testContains_long() {
0451: assertEquals(false, interval37.contains(2)); // value before
0452: assertEquals(true, interval37.contains(3));
0453: assertEquals(true, interval37.contains(4));
0454: assertEquals(true, interval37.contains(5));
0455: assertEquals(true, interval37.contains(6));
0456: assertEquals(false, interval37.contains(7)); // value after
0457: assertEquals(false, interval37.contains(8)); // value after
0458: }
0459:
0460: public void testContains_long_zeroDuration() {
0461: assertEquals(false, interval33.contains(2)); // value before
0462: assertEquals(false, interval33.contains(3)); // zero length duration contains nothing
0463: assertEquals(false, interval33.contains(4)); // value after
0464: }
0465:
0466: //-----------------------------------------------------------------------
0467: public void testContainsNow() {
0468: DateTimeUtils.setCurrentMillisFixed(2);
0469: assertEquals(false, interval37.containsNow()); // value before
0470: DateTimeUtils.setCurrentMillisFixed(3);
0471: assertEquals(true, interval37.containsNow());
0472: DateTimeUtils.setCurrentMillisFixed(4);
0473: assertEquals(true, interval37.containsNow());
0474: DateTimeUtils.setCurrentMillisFixed(6);
0475: assertEquals(true, interval37.containsNow());
0476: DateTimeUtils.setCurrentMillisFixed(7);
0477: assertEquals(false, interval37.containsNow()); // value after
0478: DateTimeUtils.setCurrentMillisFixed(8);
0479: assertEquals(false, interval37.containsNow()); // value after
0480:
0481: DateTimeUtils.setCurrentMillisFixed(2);
0482: assertEquals(false, interval33.containsNow()); // value before
0483: DateTimeUtils.setCurrentMillisFixed(3);
0484: assertEquals(false, interval33.containsNow()); // zero length duration contains nothing
0485: DateTimeUtils.setCurrentMillisFixed(4);
0486: assertEquals(false, interval33.containsNow()); // value after
0487: }
0488:
0489: //-----------------------------------------------------------------------
0490: public void testContains_RI() {
0491: assertEquals(false, interval37.contains(new Instant(2))); // value before
0492: assertEquals(true, interval37.contains(new Instant(3)));
0493: assertEquals(true, interval37.contains(new Instant(4)));
0494: assertEquals(true, interval37.contains(new Instant(5)));
0495: assertEquals(true, interval37.contains(new Instant(6)));
0496: assertEquals(false, interval37.contains(new Instant(7))); // value after
0497: assertEquals(false, interval37.contains(new Instant(8))); // value after
0498: }
0499:
0500: public void testContains_RI_null() {
0501: DateTimeUtils.setCurrentMillisFixed(2);
0502: assertEquals(false, interval37.contains((ReadableInstant) null)); // value before
0503: DateTimeUtils.setCurrentMillisFixed(3);
0504: assertEquals(true, interval37.contains((ReadableInstant) null));
0505: DateTimeUtils.setCurrentMillisFixed(4);
0506: assertEquals(true, interval37.contains((ReadableInstant) null));
0507: DateTimeUtils.setCurrentMillisFixed(6);
0508: assertEquals(true, interval37.contains((ReadableInstant) null));
0509: DateTimeUtils.setCurrentMillisFixed(7);
0510: assertEquals(false, interval37.contains((ReadableInstant) null)); // value after
0511: DateTimeUtils.setCurrentMillisFixed(8);
0512: assertEquals(false, interval37.contains((ReadableInstant) null)); // value after
0513: }
0514:
0515: public void testContains_RI_zeroDuration() {
0516: assertEquals(false, interval33.contains(new Instant(2))); // value before
0517: assertEquals(false, interval33.contains(new Instant(3))); // zero length duration contains nothing
0518: assertEquals(false, interval33.contains(new Instant(4))); // value after
0519: }
0520:
0521: //-----------------------------------------------------------------------
0522: public void testContains_RInterval() {
0523: assertEquals(false, interval37.contains(new Interval(1, 2))); // gap before
0524: assertEquals(false, interval37.contains(new Interval(2, 2))); // gap before
0525:
0526: assertEquals(false, interval37.contains(new Interval(2, 3))); // abuts before
0527: assertEquals(true, interval37.contains(new Interval(3, 3)));
0528:
0529: assertEquals(false, interval37.contains(new Interval(2, 4))); // starts before
0530: assertEquals(true, interval37.contains(new Interval(3, 4)));
0531: assertEquals(true, interval37.contains(new Interval(4, 4)));
0532:
0533: assertEquals(false, interval37.contains(new Interval(2, 6))); // starts before
0534: assertEquals(true, interval37.contains(new Interval(3, 6)));
0535: assertEquals(true, interval37.contains(new Interval(4, 6)));
0536: assertEquals(true, interval37.contains(new Interval(5, 6)));
0537: assertEquals(true, interval37.contains(new Interval(6, 6)));
0538:
0539: assertEquals(false, interval37.contains(new Interval(2, 7))); // starts before
0540: assertEquals(true, interval37.contains(new Interval(3, 7)));
0541: assertEquals(true, interval37.contains(new Interval(4, 7)));
0542: assertEquals(true, interval37.contains(new Interval(5, 7)));
0543: assertEquals(true, interval37.contains(new Interval(6, 7)));
0544: assertEquals(false, interval37.contains(new Interval(7, 7))); // abuts after
0545:
0546: assertEquals(false, interval37.contains(new Interval(2, 8))); // ends after
0547: assertEquals(false, interval37.contains(new Interval(3, 8))); // ends after
0548: assertEquals(false, interval37.contains(new Interval(4, 8))); // ends after
0549: assertEquals(false, interval37.contains(new Interval(5, 8))); // ends after
0550: assertEquals(false, interval37.contains(new Interval(6, 8))); // ends after
0551: assertEquals(false, interval37.contains(new Interval(7, 8))); // abuts after
0552: assertEquals(false, interval37.contains(new Interval(8, 8))); // gap after
0553:
0554: assertEquals(false, interval37.contains(new Interval(8, 9))); // gap after
0555: assertEquals(false, interval37.contains(new Interval(9, 9))); // gap after
0556: }
0557:
0558: public void testContains_RInterval_null() {
0559: DateTimeUtils.setCurrentMillisFixed(2);
0560: assertEquals(false, interval37
0561: .contains((ReadableInterval) null)); // gap before
0562: DateTimeUtils.setCurrentMillisFixed(3);
0563: assertEquals(true, interval37.contains((ReadableInterval) null));
0564: DateTimeUtils.setCurrentMillisFixed(4);
0565: assertEquals(true, interval37.contains((ReadableInterval) null));
0566: DateTimeUtils.setCurrentMillisFixed(6);
0567: assertEquals(true, interval37.contains((ReadableInterval) null));
0568: DateTimeUtils.setCurrentMillisFixed(7);
0569: assertEquals(false, interval37
0570: .contains((ReadableInterval) null)); // abuts after
0571: DateTimeUtils.setCurrentMillisFixed(8);
0572: assertEquals(false, interval37
0573: .contains((ReadableInterval) null)); // gap after
0574: }
0575:
0576: public void testContains_RInterval_zeroDuration() {
0577: assertEquals(false, interval33.contains(interval33)); // zero length duration contains nothing
0578: assertEquals(false, interval33.contains(interval37)); // zero-duration cannot contain anything
0579: assertEquals(true, interval37.contains(interval33));
0580: assertEquals(false, interval33.contains(new Interval(1, 2))); // zero-duration cannot contain anything
0581: assertEquals(false, interval33.contains(new Interval(8, 9))); // zero-duration cannot contain anything
0582: assertEquals(false, interval33.contains(new Interval(1, 9))); // zero-duration cannot contain anything
0583:
0584: DateTimeUtils.setCurrentMillisFixed(2);
0585: assertEquals(false, interval33
0586: .contains((ReadableInterval) null)); // gap before
0587: DateTimeUtils.setCurrentMillisFixed(3);
0588: assertEquals(false, interval33
0589: .contains((ReadableInterval) null)); // zero length duration contains nothing
0590: DateTimeUtils.setCurrentMillisFixed(4);
0591: assertEquals(false, interval33
0592: .contains((ReadableInterval) null)); // gap after
0593: }
0594:
0595: //-----------------------------------------------------------------------
0596: public void testOverlaps_RInterval() {
0597: assertEquals(false, interval37.overlaps(new Interval(1, 2))); // gap before
0598: assertEquals(false, interval37.overlaps(new Interval(2, 2))); // gap before
0599:
0600: assertEquals(false, interval37.overlaps(new Interval(2, 3))); // abuts before
0601: assertEquals(false, interval37.overlaps(new Interval(3, 3))); // abuts before
0602:
0603: assertEquals(true, interval37.overlaps(new Interval(2, 4)));
0604: assertEquals(true, interval37.overlaps(new Interval(3, 4)));
0605: assertEquals(true, interval37.overlaps(new Interval(4, 4)));
0606:
0607: assertEquals(true, interval37.overlaps(new Interval(2, 6)));
0608: assertEquals(true, interval37.overlaps(new Interval(3, 6)));
0609: assertEquals(true, interval37.overlaps(new Interval(4, 6)));
0610: assertEquals(true, interval37.overlaps(new Interval(5, 6)));
0611: assertEquals(true, interval37.overlaps(new Interval(6, 6)));
0612:
0613: assertEquals(true, interval37.overlaps(new Interval(2, 7)));
0614: assertEquals(true, interval37.overlaps(new Interval(3, 7)));
0615: assertEquals(true, interval37.overlaps(new Interval(4, 7)));
0616: assertEquals(true, interval37.overlaps(new Interval(5, 7)));
0617: assertEquals(true, interval37.overlaps(new Interval(6, 7)));
0618: assertEquals(false, interval37.overlaps(new Interval(7, 7))); // abuts after
0619:
0620: assertEquals(true, interval37.overlaps(new Interval(2, 8)));
0621: assertEquals(true, interval37.overlaps(new Interval(3, 8)));
0622: assertEquals(true, interval37.overlaps(new Interval(4, 8)));
0623: assertEquals(true, interval37.overlaps(new Interval(5, 8)));
0624: assertEquals(true, interval37.overlaps(new Interval(6, 8)));
0625: assertEquals(false, interval37.overlaps(new Interval(7, 8))); // abuts after
0626: assertEquals(false, interval37.overlaps(new Interval(8, 8))); // gap after
0627:
0628: assertEquals(false, interval37.overlaps(new Interval(8, 9))); // gap after
0629: assertEquals(false, interval37.overlaps(new Interval(9, 9))); // gap after
0630: }
0631:
0632: public void testOverlaps_RInterval_null() {
0633: DateTimeUtils.setCurrentMillisFixed(2);
0634: assertEquals(false, interval37
0635: .overlaps((ReadableInterval) null)); // gap before
0636: DateTimeUtils.setCurrentMillisFixed(3);
0637: assertEquals(false, interval37
0638: .overlaps((ReadableInterval) null)); // abuts before
0639: DateTimeUtils.setCurrentMillisFixed(4);
0640: assertEquals(true, interval37.overlaps((ReadableInterval) null));
0641: DateTimeUtils.setCurrentMillisFixed(6);
0642: assertEquals(true, interval37.overlaps((ReadableInterval) null));
0643: DateTimeUtils.setCurrentMillisFixed(7);
0644: assertEquals(false, interval37
0645: .overlaps((ReadableInterval) null)); // abuts after
0646: DateTimeUtils.setCurrentMillisFixed(8);
0647: assertEquals(false, interval37
0648: .overlaps((ReadableInterval) null)); // gap after
0649:
0650: DateTimeUtils.setCurrentMillisFixed(3);
0651: assertEquals(false, interval33
0652: .overlaps((ReadableInterval) null)); // abuts before and after
0653: }
0654:
0655: public void testOverlaps_RInterval_zeroDuration() {
0656: assertEquals(false, interval33.overlaps(interval33)); // abuts before and after
0657: assertEquals(false, interval33.overlaps(interval37)); // abuts before
0658: assertEquals(false, interval37.overlaps(interval33)); // abuts before
0659: assertEquals(false, interval33.overlaps(new Interval(1, 2)));
0660: assertEquals(false, interval33.overlaps(new Interval(8, 9)));
0661: assertEquals(true, interval33.overlaps(new Interval(1, 9)));
0662: }
0663:
0664: //-----------------------------------------------------------------------
0665: public void testOverlap_RInterval() {
0666: assertEquals(null, interval37.overlap(new Interval(1, 2))); // gap before
0667: assertEquals(null, interval37.overlap(new Interval(2, 2))); // gap before
0668:
0669: assertEquals(null, interval37.overlap(new Interval(2, 3))); // abuts before
0670: assertEquals(null, interval37.overlap(new Interval(3, 3))); // abuts before
0671:
0672: assertEquals(new Interval(3, 4), interval37
0673: .overlap(new Interval(2, 4))); // truncated start
0674: assertEquals(new Interval(3, 4), interval37
0675: .overlap(new Interval(3, 4)));
0676: assertEquals(new Interval(4, 4), interval37
0677: .overlap(new Interval(4, 4)));
0678:
0679: assertEquals(new Interval(3, 7), interval37
0680: .overlap(new Interval(2, 7))); // truncated start
0681: assertEquals(new Interval(3, 7), interval37
0682: .overlap(new Interval(3, 7)));
0683: assertEquals(new Interval(4, 7), interval37
0684: .overlap(new Interval(4, 7)));
0685: assertEquals(new Interval(5, 7), interval37
0686: .overlap(new Interval(5, 7)));
0687: assertEquals(new Interval(6, 7), interval37
0688: .overlap(new Interval(6, 7)));
0689: assertEquals(null, interval37.overlap(new Interval(7, 7))); // abuts after
0690:
0691: assertEquals(new Interval(3, 7), interval37
0692: .overlap(new Interval(2, 8))); // truncated start and end
0693: assertEquals(new Interval(3, 7), interval37
0694: .overlap(new Interval(3, 8))); // truncated end
0695: assertEquals(new Interval(4, 7), interval37
0696: .overlap(new Interval(4, 8))); // truncated end
0697: assertEquals(new Interval(5, 7), interval37
0698: .overlap(new Interval(5, 8))); // truncated end
0699: assertEquals(new Interval(6, 7), interval37
0700: .overlap(new Interval(6, 8))); // truncated end
0701: assertEquals(null, interval37.overlap(new Interval(7, 8))); // abuts after
0702: assertEquals(null, interval37.overlap(new Interval(8, 8))); // gap after
0703: }
0704:
0705: public void testOverlap_RInterval_null() {
0706: DateTimeUtils.setCurrentMillisFixed(2);
0707: assertEquals(null, interval37.overlap((ReadableInterval) null)); // gap before
0708: DateTimeUtils.setCurrentMillisFixed(3);
0709: assertEquals(null, interval37.overlap((ReadableInterval) null)); // abuts before
0710: DateTimeUtils.setCurrentMillisFixed(4);
0711: assertEquals(new Interval(4, 4), interval37
0712: .overlap((ReadableInterval) null));
0713: DateTimeUtils.setCurrentMillisFixed(6);
0714: assertEquals(new Interval(6, 6), interval37
0715: .overlap((ReadableInterval) null));
0716: DateTimeUtils.setCurrentMillisFixed(7);
0717: assertEquals(null, interval37.overlap((ReadableInterval) null)); // abuts after
0718: DateTimeUtils.setCurrentMillisFixed(8);
0719: assertEquals(null, interval37.overlap((ReadableInterval) null)); // gap after
0720:
0721: DateTimeUtils.setCurrentMillisFixed(3);
0722: assertEquals(null, interval33.overlap((ReadableInterval) null)); // abuts before and after
0723: }
0724:
0725: public void testOverlap_RInterval_zone() {
0726: Interval testA = new Interval(new DateTime(3, LONDON),
0727: new DateTime(7, LONDON));
0728: assertEquals(ISOChronology.getInstance(LONDON), testA
0729: .getChronology());
0730:
0731: Interval testB = new Interval(new DateTime(4, MOSCOW),
0732: new DateTime(8, MOSCOW));
0733: assertEquals(ISOChronology.getInstance(MOSCOW), testB
0734: .getChronology());
0735:
0736: Interval resultAB = testA.overlap(testB);
0737: assertEquals(ISOChronology.getInstance(LONDON), resultAB
0738: .getChronology());
0739:
0740: Interval resultBA = testB.overlap(testA);
0741: assertEquals(ISOChronology.getInstance(MOSCOW), resultBA
0742: .getChronology());
0743: }
0744:
0745: public void testOverlap_RInterval_zoneUTC() {
0746: Interval testA = new Interval(new Instant(3), new Instant(7));
0747: assertEquals(ISOChronology.getInstanceUTC(), testA
0748: .getChronology());
0749:
0750: Interval testB = new Interval(new Instant(4), new Instant(8));
0751: assertEquals(ISOChronology.getInstanceUTC(), testB
0752: .getChronology());
0753:
0754: Interval result = testA.overlap(testB);
0755: assertEquals(ISOChronology.getInstanceUTC(), result
0756: .getChronology());
0757: }
0758:
0759: //-----------------------------------------------------------------------
0760: public void testGap_RInterval() {
0761: assertEquals(new Interval(1, 3), interval37.gap(new Interval(0,
0762: 1)));
0763: assertEquals(new Interval(1, 3), interval37.gap(new Interval(1,
0764: 1)));
0765:
0766: assertEquals(null, interval37.gap(new Interval(2, 3))); // abuts before
0767: assertEquals(null, interval37.gap(new Interval(3, 3))); // abuts before
0768:
0769: assertEquals(null, interval37.gap(new Interval(4, 6))); // overlaps
0770:
0771: assertEquals(null, interval37.gap(new Interval(3, 7))); // overlaps
0772: assertEquals(null, interval37.gap(new Interval(6, 7))); // overlaps
0773: assertEquals(null, interval37.gap(new Interval(7, 7))); // abuts after
0774:
0775: assertEquals(null, interval37.gap(new Interval(6, 8))); // overlaps
0776: assertEquals(null, interval37.gap(new Interval(7, 8))); // abuts after
0777: assertEquals(new Interval(7, 8), interval37.gap(new Interval(8,
0778: 8)));
0779:
0780: assertEquals(null, interval37.gap(new Interval(6, 9))); // overlaps
0781: assertEquals(null, interval37.gap(new Interval(7, 9))); // abuts after
0782: assertEquals(new Interval(7, 8), interval37.gap(new Interval(8,
0783: 9)));
0784: assertEquals(new Interval(7, 9), interval37.gap(new Interval(9,
0785: 9)));
0786: }
0787:
0788: public void testGap_RInterval_null() {
0789: DateTimeUtils.setCurrentMillisFixed(2);
0790: assertEquals(new Interval(2, 3), interval37
0791: .gap((ReadableInterval) null));
0792: DateTimeUtils.setCurrentMillisFixed(3);
0793: assertEquals(null, interval37.gap((ReadableInterval) null)); // abuts before
0794: DateTimeUtils.setCurrentMillisFixed(4);
0795: assertEquals(null, interval37.gap((ReadableInterval) null)); // overlaps
0796: DateTimeUtils.setCurrentMillisFixed(6);
0797: assertEquals(null, interval37.gap((ReadableInterval) null)); // overlaps
0798: DateTimeUtils.setCurrentMillisFixed(7);
0799: assertEquals(null, interval37.gap((ReadableInterval) null)); // abuts after
0800: DateTimeUtils.setCurrentMillisFixed(8);
0801: assertEquals(new Interval(7, 8), interval37
0802: .gap((ReadableInterval) null));
0803: }
0804:
0805: public void testGap_RInterval_zone() {
0806: Interval testA = new Interval(new DateTime(3, LONDON),
0807: new DateTime(7, LONDON));
0808: assertEquals(ISOChronology.getInstance(LONDON), testA
0809: .getChronology());
0810:
0811: Interval testB = new Interval(new DateTime(1, MOSCOW),
0812: new DateTime(2, MOSCOW));
0813: assertEquals(ISOChronology.getInstance(MOSCOW), testB
0814: .getChronology());
0815:
0816: Interval resultAB = testA.gap(testB);
0817: assertEquals(ISOChronology.getInstance(LONDON), resultAB
0818: .getChronology());
0819:
0820: Interval resultBA = testB.gap(testA);
0821: assertEquals(ISOChronology.getInstance(MOSCOW), resultBA
0822: .getChronology());
0823: }
0824:
0825: public void testGap_RInterval_zoneUTC() {
0826: Interval testA = new Interval(new Instant(3), new Instant(7));
0827: assertEquals(ISOChronology.getInstanceUTC(), testA
0828: .getChronology());
0829:
0830: Interval testB = new Interval(new Instant(1), new Instant(2));
0831: assertEquals(ISOChronology.getInstanceUTC(), testB
0832: .getChronology());
0833:
0834: Interval result = testA.gap(testB);
0835: assertEquals(ISOChronology.getInstanceUTC(), result
0836: .getChronology());
0837: }
0838:
0839: //-----------------------------------------------------------------------
0840: public void testAbuts_RInterval() {
0841: assertEquals(false, interval37.abuts(new Interval(1, 2))); // gap before
0842: assertEquals(false, interval37.abuts(new Interval(2, 2))); // gap before
0843:
0844: assertEquals(true, interval37.abuts(new Interval(2, 3)));
0845: assertEquals(true, interval37.abuts(new Interval(3, 3)));
0846:
0847: assertEquals(false, interval37.abuts(new Interval(2, 4))); // overlaps
0848: assertEquals(false, interval37.abuts(new Interval(3, 4))); // overlaps
0849: assertEquals(false, interval37.abuts(new Interval(4, 4))); // overlaps
0850:
0851: assertEquals(false, interval37.abuts(new Interval(2, 6))); // overlaps
0852: assertEquals(false, interval37.abuts(new Interval(3, 6))); // overlaps
0853: assertEquals(false, interval37.abuts(new Interval(4, 6))); // overlaps
0854: assertEquals(false, interval37.abuts(new Interval(5, 6))); // overlaps
0855: assertEquals(false, interval37.abuts(new Interval(6, 6))); // overlaps
0856:
0857: assertEquals(false, interval37.abuts(new Interval(2, 7))); // overlaps
0858: assertEquals(false, interval37.abuts(new Interval(3, 7))); // overlaps
0859: assertEquals(false, interval37.abuts(new Interval(4, 7))); // overlaps
0860: assertEquals(false, interval37.abuts(new Interval(5, 7))); // overlaps
0861: assertEquals(false, interval37.abuts(new Interval(6, 7))); // overlaps
0862: assertEquals(true, interval37.abuts(new Interval(7, 7)));
0863:
0864: assertEquals(false, interval37.abuts(new Interval(2, 8))); // overlaps
0865: assertEquals(false, interval37.abuts(new Interval(3, 8))); // overlaps
0866: assertEquals(false, interval37.abuts(new Interval(4, 8))); // overlaps
0867: assertEquals(false, interval37.abuts(new Interval(5, 8))); // overlaps
0868: assertEquals(false, interval37.abuts(new Interval(6, 8))); // overlaps
0869: assertEquals(true, interval37.abuts(new Interval(7, 8)));
0870: assertEquals(false, interval37.abuts(new Interval(8, 8))); // gap after
0871:
0872: assertEquals(false, interval37.abuts(new Interval(8, 9))); // gap after
0873: assertEquals(false, interval37.abuts(new Interval(9, 9))); // gap after
0874: }
0875:
0876: public void testAbuts_RInterval_null() {
0877: DateTimeUtils.setCurrentMillisFixed(2);
0878: assertEquals(false, interval37.abuts((ReadableInterval) null)); // gap before
0879: DateTimeUtils.setCurrentMillisFixed(3);
0880: assertEquals(true, interval37.abuts((ReadableInterval) null));
0881: DateTimeUtils.setCurrentMillisFixed(4);
0882: assertEquals(false, interval37.abuts((ReadableInterval) null)); // overlaps
0883: DateTimeUtils.setCurrentMillisFixed(6);
0884: assertEquals(false, interval37.abuts((ReadableInterval) null)); // overlaps
0885: DateTimeUtils.setCurrentMillisFixed(7);
0886: assertEquals(true, interval37.abuts((ReadableInterval) null));
0887: DateTimeUtils.setCurrentMillisFixed(8);
0888: assertEquals(false, interval37.abuts((ReadableInterval) null)); // gap after
0889: }
0890:
0891: // -----------------------------------------------------------------------
0892: public void testIsBefore_long() {
0893: assertEquals(false, interval37.isBefore(2));
0894: assertEquals(false, interval37.isBefore(3));
0895: assertEquals(false, interval37.isBefore(4));
0896: assertEquals(false, interval37.isBefore(5));
0897: assertEquals(false, interval37.isBefore(6));
0898: assertEquals(true, interval37.isBefore(7));
0899: assertEquals(true, interval37.isBefore(8));
0900: }
0901:
0902: public void testIsBeforeNow() {
0903: DateTimeUtils.setCurrentMillisFixed(2);
0904: assertEquals(false, interval37.isBeforeNow());
0905: DateTimeUtils.setCurrentMillisFixed(3);
0906: assertEquals(false, interval37.isBeforeNow());
0907: DateTimeUtils.setCurrentMillisFixed(4);
0908: assertEquals(false, interval37.isBeforeNow());
0909: DateTimeUtils.setCurrentMillisFixed(6);
0910: assertEquals(false, interval37.isBeforeNow());
0911: DateTimeUtils.setCurrentMillisFixed(7);
0912: assertEquals(true, interval37.isBeforeNow());
0913: DateTimeUtils.setCurrentMillisFixed(8);
0914: assertEquals(true, interval37.isBeforeNow());
0915: }
0916:
0917: public void testIsBefore_RI() {
0918: assertEquals(false, interval37.isBefore(new Instant(2)));
0919: assertEquals(false, interval37.isBefore(new Instant(3)));
0920: assertEquals(false, interval37.isBefore(new Instant(4)));
0921: assertEquals(false, interval37.isBefore(new Instant(5)));
0922: assertEquals(false, interval37.isBefore(new Instant(6)));
0923: assertEquals(true, interval37.isBefore(new Instant(7)));
0924: assertEquals(true, interval37.isBefore(new Instant(8)));
0925: }
0926:
0927: public void testIsBefore_RI_null() {
0928: DateTimeUtils.setCurrentMillisFixed(2);
0929: assertEquals(false, interval37.isBefore((ReadableInstant) null));
0930: DateTimeUtils.setCurrentMillisFixed(3);
0931: assertEquals(false, interval37.isBefore((ReadableInstant) null));
0932: DateTimeUtils.setCurrentMillisFixed(4);
0933: assertEquals(false, interval37.isBefore((ReadableInstant) null));
0934: DateTimeUtils.setCurrentMillisFixed(6);
0935: assertEquals(false, interval37.isBefore((ReadableInstant) null));
0936: DateTimeUtils.setCurrentMillisFixed(7);
0937: assertEquals(true, interval37.isBefore((ReadableInstant) null));
0938: DateTimeUtils.setCurrentMillisFixed(8);
0939: assertEquals(true, interval37.isBefore((ReadableInstant) null));
0940: }
0941:
0942: public void testIsBefore_RInterval() {
0943: assertEquals(false, interval37.isBefore(new Interval(
0944: Long.MIN_VALUE, 2)));
0945: assertEquals(false, interval37.isBefore(new Interval(
0946: Long.MIN_VALUE, 3)));
0947: assertEquals(false, interval37.isBefore(new Interval(
0948: Long.MIN_VALUE, 4)));
0949:
0950: assertEquals(false, interval37.isBefore(new Interval(6,
0951: Long.MAX_VALUE)));
0952: assertEquals(true, interval37.isBefore(new Interval(7,
0953: Long.MAX_VALUE)));
0954: assertEquals(true, interval37.isBefore(new Interval(8,
0955: Long.MAX_VALUE)));
0956: }
0957:
0958: public void testIsBefore_RInterval_null() {
0959: DateTimeUtils.setCurrentMillisFixed(2);
0960: assertEquals(false, interval37
0961: .isBefore((ReadableInterval) null));
0962: DateTimeUtils.setCurrentMillisFixed(3);
0963: assertEquals(false, interval37
0964: .isBefore((ReadableInterval) null));
0965: DateTimeUtils.setCurrentMillisFixed(4);
0966: assertEquals(false, interval37
0967: .isBefore((ReadableInterval) null));
0968: DateTimeUtils.setCurrentMillisFixed(6);
0969: assertEquals(false, interval37
0970: .isBefore((ReadableInterval) null));
0971: DateTimeUtils.setCurrentMillisFixed(7);
0972: assertEquals(true, interval37.isBefore((ReadableInterval) null));
0973: DateTimeUtils.setCurrentMillisFixed(8);
0974: assertEquals(true, interval37.isBefore((ReadableInterval) null));
0975: }
0976:
0977: //-----------------------------------------------------------------------
0978: public void testIsAfter_long() {
0979: assertEquals(true, interval37.isAfter(2));
0980: assertEquals(false, interval37.isAfter(3));
0981: assertEquals(false, interval37.isAfter(4));
0982: assertEquals(false, interval37.isAfter(5));
0983: assertEquals(false, interval37.isAfter(6));
0984: assertEquals(false, interval37.isAfter(7));
0985: assertEquals(false, interval37.isAfter(8));
0986: }
0987:
0988: public void testIsAfterNow() {
0989: DateTimeUtils.setCurrentMillisFixed(2);
0990: assertEquals(true, interval37.isAfterNow());
0991: DateTimeUtils.setCurrentMillisFixed(3);
0992: assertEquals(false, interval37.isAfterNow());
0993: DateTimeUtils.setCurrentMillisFixed(4);
0994: assertEquals(false, interval37.isAfterNow());
0995: DateTimeUtils.setCurrentMillisFixed(6);
0996: assertEquals(false, interval37.isAfterNow());
0997: DateTimeUtils.setCurrentMillisFixed(7);
0998: assertEquals(false, interval37.isAfterNow());
0999: DateTimeUtils.setCurrentMillisFixed(8);
1000: assertEquals(false, interval37.isAfterNow());
1001: }
1002:
1003: public void testIsAfter_RI() {
1004: assertEquals(true, interval37.isAfter(new Instant(2)));
1005: assertEquals(false, interval37.isAfter(new Instant(3)));
1006: assertEquals(false, interval37.isAfter(new Instant(4)));
1007: assertEquals(false, interval37.isAfter(new Instant(5)));
1008: assertEquals(false, interval37.isAfter(new Instant(6)));
1009: assertEquals(false, interval37.isAfter(new Instant(7)));
1010: assertEquals(false, interval37.isAfter(new Instant(8)));
1011: }
1012:
1013: public void testIsAfter_RI_null() {
1014: DateTimeUtils.setCurrentMillisFixed(2);
1015: assertEquals(true, interval37.isAfter((ReadableInstant) null));
1016: DateTimeUtils.setCurrentMillisFixed(3);
1017: assertEquals(false, interval37.isAfter((ReadableInstant) null));
1018: DateTimeUtils.setCurrentMillisFixed(4);
1019: assertEquals(false, interval37.isAfter((ReadableInstant) null));
1020: DateTimeUtils.setCurrentMillisFixed(6);
1021: assertEquals(false, interval37.isAfter((ReadableInstant) null));
1022: DateTimeUtils.setCurrentMillisFixed(7);
1023: assertEquals(false, interval37.isAfter((ReadableInstant) null));
1024: DateTimeUtils.setCurrentMillisFixed(8);
1025: assertEquals(false, interval37.isAfter((ReadableInstant) null));
1026: }
1027:
1028: public void testIsAfter_RInterval() {
1029: assertEquals(true, interval37.isAfter(new Interval(
1030: Long.MIN_VALUE, 2)));
1031: assertEquals(true, interval37.isAfter(new Interval(
1032: Long.MIN_VALUE, 3)));
1033: assertEquals(false, interval37.isAfter(new Interval(
1034: Long.MIN_VALUE, 4)));
1035:
1036: assertEquals(false, interval37.isAfter(new Interval(6,
1037: Long.MAX_VALUE)));
1038: assertEquals(false, interval37.isAfter(new Interval(7,
1039: Long.MAX_VALUE)));
1040: assertEquals(false, interval37.isAfter(new Interval(8,
1041: Long.MAX_VALUE)));
1042: }
1043:
1044: public void testIsAfter_RInterval_null() {
1045: DateTimeUtils.setCurrentMillisFixed(2);
1046: assertEquals(true, interval37.isAfter((ReadableInterval) null));
1047: DateTimeUtils.setCurrentMillisFixed(3);
1048: assertEquals(true, interval37.isAfter((ReadableInterval) null));
1049: DateTimeUtils.setCurrentMillisFixed(4);
1050: assertEquals(false, interval37.isAfter((ReadableInterval) null));
1051: DateTimeUtils.setCurrentMillisFixed(6);
1052: assertEquals(false, interval37.isAfter((ReadableInterval) null));
1053: DateTimeUtils.setCurrentMillisFixed(7);
1054: assertEquals(false, interval37.isAfter((ReadableInterval) null));
1055: DateTimeUtils.setCurrentMillisFixed(8);
1056: assertEquals(false, interval37.isAfter((ReadableInterval) null));
1057: }
1058:
1059: //-----------------------------------------------------------------------
1060: public void testToInterval1() {
1061: Interval test = new Interval(TEST_TIME1, TEST_TIME2,
1062: COPTIC_PARIS);
1063: Interval result = test.toInterval();
1064: assertSame(test, result);
1065: }
1066:
1067: //-----------------------------------------------------------------------
1068: public void testToMutableInterval1() {
1069: Interval test = new Interval(TEST_TIME1, TEST_TIME2,
1070: COPTIC_PARIS);
1071: MutableInterval result = test.toMutableInterval();
1072: assertEquals(test, result);
1073: }
1074:
1075: //-----------------------------------------------------------------------
1076: public void testToPeriod() {
1077: DateTime dt1 = new DateTime(2004, 6, 9, 7, 8, 9, 10,
1078: COPTIC_PARIS);
1079: DateTime dt2 = new DateTime(2005, 8, 13, 12, 14, 16, 18,
1080: COPTIC_PARIS);
1081: Interval base = new Interval(dt1, dt2);
1082:
1083: Period test = base.toPeriod();
1084: Period expected = new Period(dt1, dt2, PeriodType.standard());
1085: assertEquals(expected, test);
1086: }
1087:
1088: //-----------------------------------------------------------------------
1089: public void testToPeriod_PeriodType1() {
1090: DateTime dt1 = new DateTime(2004, 6, 9, 7, 8, 9, 10,
1091: COPTIC_PARIS);
1092: DateTime dt2 = new DateTime(2005, 8, 13, 12, 14, 16, 18,
1093: COPTIC_PARIS);
1094: Interval base = new Interval(dt1, dt2);
1095:
1096: Period test = base.toPeriod(null);
1097: Period expected = new Period(dt1, dt2, PeriodType.standard());
1098: assertEquals(expected, test);
1099: }
1100:
1101: public void testToPeriod_PeriodType2() {
1102: DateTime dt1 = new DateTime(2004, 6, 9, 7, 8, 9, 10);
1103: DateTime dt2 = new DateTime(2005, 8, 13, 12, 14, 16, 18);
1104: Interval base = new Interval(dt1, dt2);
1105:
1106: Period test = base.toPeriod(PeriodType.yearWeekDayTime());
1107: Period expected = new Period(dt1, dt2, PeriodType
1108: .yearWeekDayTime());
1109: assertEquals(expected, test);
1110: }
1111:
1112: //-----------------------------------------------------------------------
1113: public void testSerialization() throws Exception {
1114: Interval test = new Interval(TEST_TIME1, TEST_TIME2,
1115: COPTIC_PARIS);
1116:
1117: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1118: ObjectOutputStream oos = new ObjectOutputStream(baos);
1119: oos.writeObject(test);
1120: byte[] bytes = baos.toByteArray();
1121: oos.close();
1122:
1123: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
1124: ObjectInputStream ois = new ObjectInputStream(bais);
1125: Interval result = (Interval) ois.readObject();
1126: ois.close();
1127:
1128: assertEquals(test, result);
1129: }
1130:
1131: //-----------------------------------------------------------------------
1132: public void testToString() {
1133: DateTime dt1 = new DateTime(2004, 6, 9, 7, 8, 9, 10,
1134: DateTimeZone.UTC);
1135: DateTime dt2 = new DateTime(2005, 8, 13, 12, 14, 16, 18,
1136: DateTimeZone.UTC);
1137: Interval test = new Interval(dt1, dt2);
1138: assertEquals("2004-06-09T07:08:09.010/2005-08-13T12:14:16.018",
1139: test.toString());
1140: }
1141:
1142: //-----------------------------------------------------------------------
1143: public void testWithChronology1() {
1144: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1145: COPTIC_PARIS);
1146: Interval test = base.withChronology(BuddhistChronology
1147: .getInstance());
1148: assertEquals(new Interval(TEST_TIME1, TEST_TIME2,
1149: BuddhistChronology.getInstance()), test);
1150: }
1151:
1152: public void testWithChronology2() {
1153: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1154: COPTIC_PARIS);
1155: Interval test = base.withChronology(null);
1156: assertEquals(new Interval(TEST_TIME1, TEST_TIME2, ISOChronology
1157: .getInstance()), test);
1158: }
1159:
1160: public void testWithChronology3() {
1161: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1162: COPTIC_PARIS);
1163: Interval test = base.withChronology(COPTIC_PARIS);
1164: assertSame(base, test);
1165: }
1166:
1167: //-----------------------------------------------------------------------
1168: public void testWithStartMillis_long1() {
1169: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1170: COPTIC_PARIS);
1171: Interval test = base.withStartMillis(TEST_TIME1 - 1);
1172: assertEquals(new Interval(TEST_TIME1 - 1, TEST_TIME2,
1173: COPTIC_PARIS), test);
1174: }
1175:
1176: public void testWithStartMillis_long2() {
1177: Interval test = new Interval(TEST_TIME1, TEST_TIME2);
1178: try {
1179: test.withStartMillis(TEST_TIME2 + 1);
1180: fail();
1181: } catch (IllegalArgumentException ex) {
1182: }
1183: }
1184:
1185: public void testWithStartMillis_long3() {
1186: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1187: COPTIC_PARIS);
1188: Interval test = base.withStartMillis(TEST_TIME1);
1189: assertSame(base, test);
1190: }
1191:
1192: //-----------------------------------------------------------------------
1193: public void testWithStartInstant_RI1() {
1194: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1195: COPTIC_PARIS);
1196: Interval test = base.withStart(new Instant(TEST_TIME1 - 1));
1197: assertEquals(new Interval(TEST_TIME1 - 1, TEST_TIME2,
1198: COPTIC_PARIS), test);
1199: }
1200:
1201: public void testWithStartInstant_RI2() {
1202: Interval test = new Interval(TEST_TIME1, TEST_TIME2);
1203: try {
1204: test.withStart(new Instant(TEST_TIME2 + 1));
1205: fail();
1206: } catch (IllegalArgumentException ex) {
1207: }
1208: }
1209:
1210: public void testWithStartInstant_RI3() {
1211: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1212: COPTIC_PARIS);
1213: Interval test = base.withStart(null);
1214: assertEquals(new Interval(TEST_TIME_NOW, TEST_TIME2,
1215: COPTIC_PARIS), test);
1216: }
1217:
1218: //-----------------------------------------------------------------------
1219: public void testWithEndMillis_long1() {
1220: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1221: COPTIC_PARIS);
1222: Interval test = base.withEndMillis(TEST_TIME2 - 1);
1223: assertEquals(new Interval(TEST_TIME1, TEST_TIME2 - 1,
1224: COPTIC_PARIS), test);
1225: }
1226:
1227: public void testWithEndMillis_long2() {
1228: Interval test = new Interval(TEST_TIME1, TEST_TIME2);
1229: try {
1230: test.withEndMillis(TEST_TIME1 - 1);
1231: fail();
1232: } catch (IllegalArgumentException ex) {
1233: }
1234: }
1235:
1236: public void testWithEndMillis_long3() {
1237: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1238: COPTIC_PARIS);
1239: Interval test = base.withEndMillis(TEST_TIME2);
1240: assertSame(base, test);
1241: }
1242:
1243: //-----------------------------------------------------------------------
1244: public void testWithEndInstant_RI1() {
1245: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1246: COPTIC_PARIS);
1247: Interval test = base.withEnd(new Instant(TEST_TIME2 - 1));
1248: assertEquals(new Interval(TEST_TIME1, TEST_TIME2 - 1,
1249: COPTIC_PARIS), test);
1250: }
1251:
1252: public void testWithEndInstant_RI2() {
1253: Interval test = new Interval(TEST_TIME1, TEST_TIME2);
1254: try {
1255: test.withEnd(new Instant(TEST_TIME1 - 1));
1256: fail();
1257: } catch (IllegalArgumentException ex) {
1258: }
1259: }
1260:
1261: public void testWithEndInstant_RI3() {
1262: Interval base = new Interval(TEST_TIME1, TEST_TIME2,
1263: COPTIC_PARIS);
1264: Interval test = base.withEnd(null);
1265: assertEquals(new Interval(TEST_TIME1, TEST_TIME_NOW,
1266: COPTIC_PARIS), test);
1267: }
1268:
1269: //-----------------------------------------------------------------------
1270: public void testWithDurationAfterStart1() throws Throwable {
1271: Duration dur = new Duration(TEST_TIME2 - TEST_TIME_NOW);
1272: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME_NOW,
1273: COPTIC_PARIS);
1274: Interval test = base.withDurationAfterStart(dur);
1275:
1276: assertEquals(new Interval(TEST_TIME_NOW, TEST_TIME2,
1277: COPTIC_PARIS), test);
1278: }
1279:
1280: public void testWithDurationAfterStart2() throws Throwable {
1281: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME2,
1282: COPTIC_PARIS);
1283: Interval test = base.withDurationAfterStart(null);
1284:
1285: assertEquals(new Interval(TEST_TIME_NOW, TEST_TIME_NOW,
1286: COPTIC_PARIS), test);
1287: }
1288:
1289: public void testWithDurationAfterStart3() throws Throwable {
1290: Duration dur = new Duration(-1);
1291: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME_NOW);
1292: try {
1293: base.withDurationAfterStart(dur);
1294: fail();
1295: } catch (IllegalArgumentException ex) {
1296: }
1297: }
1298:
1299: public void testWithDurationAfterStart4() throws Throwable {
1300: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME2,
1301: COPTIC_PARIS);
1302: Interval test = base.withDurationAfterStart(base.toDuration());
1303:
1304: assertSame(base, test);
1305: }
1306:
1307: //-----------------------------------------------------------------------
1308: public void testWithDurationBeforeEnd1() throws Throwable {
1309: Duration dur = new Duration(TEST_TIME_NOW - TEST_TIME1);
1310: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME_NOW,
1311: COPTIC_PARIS);
1312: Interval test = base.withDurationBeforeEnd(dur);
1313:
1314: assertEquals(new Interval(TEST_TIME1, TEST_TIME_NOW,
1315: COPTIC_PARIS), test);
1316: }
1317:
1318: public void testWithDurationBeforeEnd2() throws Throwable {
1319: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME2,
1320: COPTIC_PARIS);
1321: Interval test = base.withDurationBeforeEnd(null);
1322:
1323: assertEquals(
1324: new Interval(TEST_TIME2, TEST_TIME2, COPTIC_PARIS),
1325: test);
1326: }
1327:
1328: public void testWithDurationBeforeEnd3() throws Throwable {
1329: Duration dur = new Duration(-1);
1330: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME_NOW);
1331: try {
1332: base.withDurationBeforeEnd(dur);
1333: fail();
1334: } catch (IllegalArgumentException ex) {
1335: }
1336: }
1337:
1338: public void testWithDurationBeforeEnd4() throws Throwable {
1339: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME2,
1340: COPTIC_PARIS);
1341: Interval test = base.withDurationBeforeEnd(base.toDuration());
1342:
1343: assertSame(base, test);
1344: }
1345:
1346: //-----------------------------------------------------------------------
1347: public void testWithPeriodAfterStart1() throws Throwable {
1348: DateTime dt = new DateTime(TEST_TIME_NOW, COPTIC_PARIS);
1349: Period dur = new Period(0, 6, 0, 0, 1, 0, 0, 0);
1350:
1351: Interval base = new Interval(dt, dt);
1352: Interval test = base.withPeriodAfterStart(dur);
1353: assertEquals(new Interval(dt, dur), test);
1354: }
1355:
1356: public void testWithPeriodAfterStart2() throws Throwable {
1357: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME2,
1358: COPTIC_PARIS);
1359: Interval test = base.withPeriodAfterStart(null);
1360:
1361: assertEquals(new Interval(TEST_TIME_NOW, TEST_TIME_NOW,
1362: COPTIC_PARIS), test);
1363: }
1364:
1365: public void testWithPeriodAfterStart3() throws Throwable {
1366: Period per = new Period(0, 0, 0, 0, 0, 0, 0, -1);
1367: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME_NOW);
1368: try {
1369: base.withPeriodAfterStart(per);
1370: fail();
1371: } catch (IllegalArgumentException ex) {
1372: }
1373: }
1374:
1375: //-----------------------------------------------------------------------
1376: public void testWithPeriodBeforeEnd1() throws Throwable {
1377: DateTime dt = new DateTime(TEST_TIME_NOW, COPTIC_PARIS);
1378: Period dur = new Period(0, 6, 0, 0, 1, 0, 0, 0);
1379:
1380: Interval base = new Interval(dt, dt);
1381: Interval test = base.withPeriodBeforeEnd(dur);
1382: assertEquals(new Interval(dur, dt), test);
1383: }
1384:
1385: public void testWithPeriodBeforeEnd2() throws Throwable {
1386: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME2,
1387: COPTIC_PARIS);
1388: Interval test = base.withPeriodBeforeEnd(null);
1389:
1390: assertEquals(
1391: new Interval(TEST_TIME2, TEST_TIME2, COPTIC_PARIS),
1392: test);
1393: }
1394:
1395: public void testWithPeriodBeforeEnd3() throws Throwable {
1396: Period per = new Period(0, 0, 0, 0, 0, 0, 0, -1);
1397: Interval base = new Interval(TEST_TIME_NOW, TEST_TIME_NOW);
1398: try {
1399: base.withPeriodBeforeEnd(per);
1400: fail();
1401: } catch (IllegalArgumentException ex) {
1402: }
1403: }
1404:
1405: }
|