001: /*
002: * Copyright 2001-2006 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time;
017:
018: import java.util.Locale;
019: import java.util.TimeZone;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.chrono.BuddhistChronology;
025: import org.joda.time.chrono.CopticChronology;
026: import org.joda.time.chrono.GJChronology;
027: import org.joda.time.chrono.ISOChronology;
028: import org.joda.time.convert.ConverterManager;
029: import org.joda.time.convert.IntervalConverter;
030:
031: /**
032: * This class is a JUnit test for Interval.
033: *
034: * @author Stephen Colebourne
035: */
036: public class TestInterval_Constructors extends TestCase {
037: // Test in 2002/03 as time zones are more well known
038: // (before the late 90's they were all over the place)
039:
040: private static final DateTimeZone PARIS = DateTimeZone
041: .forID("Europe/Paris");
042: private static final DateTimeZone LONDON = DateTimeZone
043: .forID("Europe/London");
044:
045: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
046: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
047: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
048: + 365 + 365 + 366 + 365;
049: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
050: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
051: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
052: + 365 + 365 + 366 + 365 + 365;
053:
054: // 2002-06-09
055: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
056: + 31L + 9L - 1L)
057: * DateTimeConstants.MILLIS_PER_DAY;
058:
059: // // 2002-04-05
060: // private long TEST_TIME1 =
061: // (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
062: // + 12L * DateTimeConstants.MILLIS_PER_HOUR
063: // + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
064: //
065: // // 2003-05-06
066: // private long TEST_TIME2 =
067: // (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
068: // + 14L * DateTimeConstants.MILLIS_PER_HOUR
069: // + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
070:
071: private DateTimeZone originalDateTimeZone = null;
072: private TimeZone originalTimeZone = null;
073: private Locale originalLocale = null;
074:
075: public static void main(String[] args) {
076: junit.textui.TestRunner.run(suite());
077: }
078:
079: public static TestSuite suite() {
080: return new TestSuite(TestInterval_Constructors.class);
081: }
082:
083: public TestInterval_Constructors(String name) {
084: super (name);
085: }
086:
087: protected void setUp() throws Exception {
088: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
089: originalDateTimeZone = DateTimeZone.getDefault();
090: originalTimeZone = TimeZone.getDefault();
091: originalLocale = Locale.getDefault();
092: DateTimeZone.setDefault(PARIS);
093: TimeZone.setDefault(PARIS.toTimeZone());
094: Locale.setDefault(Locale.FRANCE);
095: }
096:
097: protected void tearDown() throws Exception {
098: DateTimeUtils.setCurrentMillisSystem();
099: DateTimeZone.setDefault(originalDateTimeZone);
100: TimeZone.setDefault(originalTimeZone);
101: Locale.setDefault(originalLocale);
102: originalDateTimeZone = null;
103: originalTimeZone = null;
104: originalLocale = null;
105: }
106:
107: //-----------------------------------------------------------------------
108: public void testConstructor_long_long1() throws Throwable {
109: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
110: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
111: Interval test = new Interval(dt1.getMillis(), dt2.getMillis());
112: assertEquals(dt1.getMillis(), test.getStartMillis());
113: assertEquals(dt2.getMillis(), test.getEndMillis());
114: assertEquals(ISOChronology.getInstance(), test.getChronology());
115: }
116:
117: public void testConstructor_long_long2() throws Throwable {
118: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
119: Interval test = new Interval(dt1.getMillis(), dt1.getMillis());
120: assertEquals(dt1.getMillis(), test.getStartMillis());
121: assertEquals(dt1.getMillis(), test.getEndMillis());
122: assertEquals(ISOChronology.getInstance(), test.getChronology());
123: }
124:
125: public void testConstructor_long_long3() throws Throwable {
126: DateTime dt1 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
127: DateTime dt2 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
128: try {
129: new Interval(dt1.getMillis(), dt2.getMillis());
130: fail();
131: } catch (IllegalArgumentException ex) {
132: }
133: }
134:
135: //-----------------------------------------------------------------------
136: public void testConstructor_long_long_Zone() throws Throwable {
137: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
138: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
139: Interval test = new Interval(dt1.getMillis(), dt2.getMillis(),
140: LONDON);
141: assertEquals(dt1.getMillis(), test.getStartMillis());
142: assertEquals(dt2.getMillis(), test.getEndMillis());
143: assertEquals(ISOChronology.getInstance(LONDON), test
144: .getChronology());
145: }
146:
147: public void testConstructor_long_long_nullZone() throws Throwable {
148: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
149: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
150: Interval test = new Interval(dt1.getMillis(), dt2.getMillis(),
151: (DateTimeZone) null);
152: assertEquals(dt1.getMillis(), test.getStartMillis());
153: assertEquals(dt2.getMillis(), test.getEndMillis());
154: assertEquals(ISOChronology.getInstance(), test.getChronology());
155: }
156:
157: //-----------------------------------------------------------------------
158: public void testConstructor_long_long_Chronology() throws Throwable {
159: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
160: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
161: Interval test = new Interval(dt1.getMillis(), dt2.getMillis(),
162: GJChronology.getInstance());
163: assertEquals(dt1.getMillis(), test.getStartMillis());
164: assertEquals(dt2.getMillis(), test.getEndMillis());
165: assertEquals(GJChronology.getInstance(), test.getChronology());
166: }
167:
168: public void testConstructor_long_long_nullChronology()
169: throws Throwable {
170: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
171: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
172: Interval test = new Interval(dt1.getMillis(), dt2.getMillis(),
173: (Chronology) null);
174: assertEquals(dt1.getMillis(), test.getStartMillis());
175: assertEquals(dt2.getMillis(), test.getEndMillis());
176: assertEquals(ISOChronology.getInstance(), test.getChronology());
177: }
178:
179: //-----------------------------------------------------------------------
180: public void testConstructor_RI_RI1() throws Throwable {
181: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
182: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
183: Interval test = new Interval(dt1, dt2);
184: assertEquals(dt1.getMillis(), test.getStartMillis());
185: assertEquals(dt2.getMillis(), test.getEndMillis());
186: }
187:
188: public void testConstructor_RI_RI2() throws Throwable {
189: Instant dt1 = new Instant(new DateTime(2004, 6, 9, 0, 0, 0, 0));
190: Instant dt2 = new Instant(new DateTime(2005, 7, 10, 1, 1, 1, 1));
191: Interval test = new Interval(dt1, dt2);
192: assertEquals(dt1.getMillis(), test.getStartMillis());
193: assertEquals(dt2.getMillis(), test.getEndMillis());
194: }
195:
196: public void testConstructor_RI_RI3() throws Throwable {
197: Interval test = new Interval((ReadableInstant) null,
198: (ReadableInstant) null);
199: assertEquals(TEST_TIME_NOW, test.getStartMillis());
200: assertEquals(TEST_TIME_NOW, test.getEndMillis());
201: }
202:
203: public void testConstructor_RI_RI4() throws Throwable {
204: DateTime dt1 = new DateTime(2000, 6, 9, 0, 0, 0, 0);
205: Interval test = new Interval(dt1, (ReadableInstant) null);
206: assertEquals(dt1.getMillis(), test.getStartMillis());
207: assertEquals(TEST_TIME_NOW, test.getEndMillis());
208: }
209:
210: public void testConstructor_RI_RI5() throws Throwable {
211: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
212: Interval test = new Interval((ReadableInstant) null, dt2);
213: assertEquals(TEST_TIME_NOW, test.getStartMillis());
214: assertEquals(dt2.getMillis(), test.getEndMillis());
215: }
216:
217: public void testConstructor_RI_RI6() throws Throwable {
218: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
219: Interval test = new Interval(dt1, dt1);
220: assertEquals(dt1.getMillis(), test.getStartMillis());
221: assertEquals(dt1.getMillis(), test.getEndMillis());
222: }
223:
224: public void testConstructor_RI_RI7() throws Throwable {
225: DateTime dt1 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
226: DateTime dt2 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
227: try {
228: new Interval(dt1, dt2);
229: fail();
230: } catch (IllegalArgumentException ex) {
231: }
232: }
233:
234: public void testConstructor_RI_RI_chronoStart() throws Throwable {
235: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0,
236: GJChronology.getInstance());
237: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
238: Interval test = new Interval(dt1, dt2);
239: assertEquals(dt1.getMillis(), test.getStartMillis());
240: assertEquals(dt2.getMillis(), test.getEndMillis());
241: assertEquals(GJChronology.getInstance(), test.getChronology());
242: }
243:
244: public void testConstructor_RI_RI_chronoEnd() throws Throwable {
245: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
246: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1,
247: GJChronology.getInstance());
248: Interval test = new Interval(dt1, dt2);
249: assertEquals(dt1.getMillis(), test.getStartMillis());
250: assertEquals(dt2.getMillis(), test.getEndMillis());
251: assertEquals(ISOChronology.getInstance(), test.getChronology());
252: }
253:
254: public void testConstructor_RI_RI_zones() throws Throwable {
255: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0, LONDON);
256: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1, PARIS);
257: Interval test = new Interval(dt1, dt2);
258: assertEquals(dt1.getMillis(), test.getStartMillis());
259: assertEquals(dt2.getMillis(), test.getEndMillis());
260: assertEquals(ISOChronology.getInstance(LONDON), test
261: .getChronology());
262: }
263:
264: public void testConstructor_RI_RI_instant() throws Throwable {
265: Instant dt1 = new Instant(12345678L);
266: Instant dt2 = new Instant(22345678L);
267: Interval test = new Interval(dt1, dt2);
268: assertEquals(12345678L, test.getStartMillis());
269: assertEquals(22345678L, test.getEndMillis());
270: assertEquals(ISOChronology.getInstanceUTC(), test
271: .getChronology());
272: }
273:
274: //-----------------------------------------------------------------------
275: public void testConstructor_RI_RP1() throws Throwable {
276: DateTime dt = new DateTime(TEST_TIME_NOW);
277: Period dur = new Period(0, 6, 0, 0, 1, 0, 0, 0);
278: long result = TEST_TIME_NOW;
279: result = ISOChronology.getInstance().months().add(result, 6);
280: result = ISOChronology.getInstance().hours().add(result, 1);
281:
282: Interval test = new Interval(dt, dur);
283: assertEquals(dt.getMillis(), test.getStartMillis());
284: assertEquals(result, test.getEndMillis());
285: }
286:
287: public void testConstructor_RI_RP2() throws Throwable {
288: Instant dt = new Instant(new DateTime(TEST_TIME_NOW));
289: Period dur = new Period(0, 6, 0, 3, 1, 0, 0, 0);
290: long result = TEST_TIME_NOW;
291: result = ISOChronology.getInstanceUTC().months().add(result, 6);
292: result = ISOChronology.getInstanceUTC().days().add(result, 3);
293: result = ISOChronology.getInstanceUTC().hours().add(result, 1);
294:
295: Interval test = new Interval(dt, dur);
296: assertEquals(dt.getMillis(), test.getStartMillis());
297: assertEquals(result, test.getEndMillis());
298: }
299:
300: public void testConstructor_RI_RP3() throws Throwable {
301: DateTime dt = new DateTime(TEST_TIME_NOW, CopticChronology
302: .getInstanceUTC());
303: Period dur = new Period(0, 6, 0, 3, 1, 0, 0, 0, PeriodType
304: .standard());
305: long result = TEST_TIME_NOW;
306: result = CopticChronology.getInstanceUTC().months().add(result,
307: 6);
308: result = CopticChronology.getInstanceUTC().days()
309: .add(result, 3);
310: result = CopticChronology.getInstanceUTC().hours().add(result,
311: 1);
312:
313: Interval test = new Interval(dt, dur);
314: assertEquals(dt.getMillis(), test.getStartMillis());
315: assertEquals(result, test.getEndMillis());
316: }
317:
318: public void testConstructor_RI_RP4() throws Throwable {
319: DateTime dt = new DateTime(TEST_TIME_NOW);
320: Period dur = new Period(
321: 1 * DateTimeConstants.MILLIS_PER_HOUR + 23L);
322: long result = TEST_TIME_NOW;
323: result = ISOChronology.getInstance().hours().add(result, 1);
324: result = ISOChronology.getInstance().millis().add(result, 23);
325:
326: Interval test = new Interval(dt, dur);
327: assertEquals(dt.getMillis(), test.getStartMillis());
328: assertEquals(result, test.getEndMillis());
329: }
330:
331: public void testConstructor_RI_RP5() throws Throwable {
332: Interval test = new Interval((ReadableInstant) null,
333: (ReadablePeriod) null);
334: assertEquals(TEST_TIME_NOW, test.getStartMillis());
335: assertEquals(TEST_TIME_NOW, test.getEndMillis());
336: }
337:
338: public void testConstructor_RI_RP6() throws Throwable {
339: DateTime dt = new DateTime(TEST_TIME_NOW);
340: Interval test = new Interval(dt, (ReadablePeriod) null);
341: assertEquals(dt.getMillis(), test.getStartMillis());
342: assertEquals(dt.getMillis(), test.getEndMillis());
343: }
344:
345: public void testConstructor_RI_RP7() throws Throwable {
346: Period dur = new Period(0, 6, 0, 0, 1, 0, 0, 0);
347: long result = TEST_TIME_NOW;
348: result = ISOChronology.getInstance().monthOfYear().add(result,
349: 6);
350: result = ISOChronology.getInstance().hourOfDay().add(result, 1);
351:
352: Interval test = new Interval((ReadableInstant) null, dur);
353: assertEquals(TEST_TIME_NOW, test.getStartMillis());
354: assertEquals(result, test.getEndMillis());
355: }
356:
357: public void testConstructor_RI_RP8() throws Throwable {
358: DateTime dt = new DateTime(TEST_TIME_NOW);
359: Period dur = new Period(0, 0, 0, 0, 0, 0, 0, -1);
360: try {
361: new Interval(dt, dur);
362: fail();
363: } catch (IllegalArgumentException ex) {
364: }
365: }
366:
367: //-----------------------------------------------------------------------
368: public void testConstructor_RP_RI1() throws Throwable {
369: DateTime dt = new DateTime(TEST_TIME_NOW);
370: Period dur = new Period(0, 6, 0, 0, 1, 0, 0, 0);
371: long result = TEST_TIME_NOW;
372: result = ISOChronology.getInstance().months().add(result, -6);
373: result = ISOChronology.getInstance().hours().add(result, -1);
374:
375: Interval test = new Interval(dur, dt);
376: assertEquals(result, test.getStartMillis());
377: assertEquals(dt.getMillis(), test.getEndMillis());
378: }
379:
380: public void testConstructor_RP_RI2() throws Throwable {
381: Instant dt = new Instant(new DateTime(TEST_TIME_NOW));
382: Period dur = new Period(0, 6, 0, 3, 1, 0, 0, 0);
383: long result = TEST_TIME_NOW;
384: result = ISOChronology.getInstanceUTC().months()
385: .add(result, -6);
386: result = ISOChronology.getInstanceUTC().days().add(result, -3);
387: result = ISOChronology.getInstanceUTC().hours().add(result, -1);
388:
389: Interval test = new Interval(dur, dt);
390: assertEquals(result, test.getStartMillis());
391: assertEquals(dt.getMillis(), test.getEndMillis());
392: }
393:
394: public void testConstructor_RP_RI3() throws Throwable {
395: DateTime dt = new DateTime(TEST_TIME_NOW, CopticChronology
396: .getInstanceUTC());
397: Period dur = new Period(0, 6, 0, 3, 1, 0, 0, 0, PeriodType
398: .standard());
399: long result = TEST_TIME_NOW;
400: result = CopticChronology.getInstanceUTC().months().add(result,
401: -6);
402: result = CopticChronology.getInstanceUTC().days().add(result,
403: -3);
404: result = CopticChronology.getInstanceUTC().hours().add(result,
405: -1);
406:
407: Interval test = new Interval(dur, dt);
408: assertEquals(result, test.getStartMillis());
409: assertEquals(dt.getMillis(), test.getEndMillis());
410: }
411:
412: public void testConstructor_RP_RI4() throws Throwable {
413: DateTime dt = new DateTime(TEST_TIME_NOW);
414: Period dur = new Period(
415: 1 * DateTimeConstants.MILLIS_PER_HOUR + 23L);
416: long result = TEST_TIME_NOW;
417: result = ISOChronology.getInstance().hours().add(result, -1);
418: result = ISOChronology.getInstance().millis().add(result, -23);
419:
420: Interval test = new Interval(dur, dt);
421: assertEquals(result, test.getStartMillis());
422: assertEquals(dt.getMillis(), test.getEndMillis());
423: }
424:
425: public void testConstructor_RP_RI5() throws Throwable {
426: Interval test = new Interval((ReadablePeriod) null,
427: (ReadableInstant) null);
428: assertEquals(TEST_TIME_NOW, test.getStartMillis());
429: assertEquals(TEST_TIME_NOW, test.getEndMillis());
430: }
431:
432: public void testConstructor_RP_RI6() throws Throwable {
433: DateTime dt = new DateTime(TEST_TIME_NOW);
434: Interval test = new Interval((ReadablePeriod) null, dt);
435: assertEquals(dt.getMillis(), test.getStartMillis());
436: assertEquals(dt.getMillis(), test.getEndMillis());
437: }
438:
439: public void testConstructor_RP_RI7() throws Throwable {
440: Period dur = new Period(0, 6, 0, 0, 1, 0, 0, 0);
441: long result = TEST_TIME_NOW;
442: result = ISOChronology.getInstance().monthOfYear().add(result,
443: -6);
444: result = ISOChronology.getInstance().hourOfDay()
445: .add(result, -1);
446:
447: Interval test = new Interval(dur, (ReadableInstant) null);
448: assertEquals(result, test.getStartMillis());
449: assertEquals(TEST_TIME_NOW, test.getEndMillis());
450: }
451:
452: public void testConstructor_RP_RI8() throws Throwable {
453: DateTime dt = new DateTime(TEST_TIME_NOW);
454: Period dur = new Period(0, 0, 0, 0, 0, 0, 0, -1);
455: try {
456: new Interval(dur, dt);
457: fail();
458: } catch (IllegalArgumentException ex) {
459: }
460: }
461:
462: //-----------------------------------------------------------------------
463: public void testConstructor_RI_RD1() throws Throwable {
464: long result = TEST_TIME_NOW;
465: result = ISOChronology.getInstance().months().add(result, 6);
466: result = ISOChronology.getInstance().hours().add(result, 1);
467:
468: DateTime dt = new DateTime(TEST_TIME_NOW);
469: Duration dur = new Duration(result - TEST_TIME_NOW);
470:
471: Interval test = new Interval(dt, dur);
472: assertEquals(dt.getMillis(), test.getStartMillis());
473: assertEquals(result, test.getEndMillis());
474: }
475:
476: public void testConstructor_RI_RD2() throws Throwable {
477: Interval test = new Interval((ReadableInstant) null,
478: (ReadableDuration) null);
479: assertEquals(TEST_TIME_NOW, test.getStartMillis());
480: assertEquals(TEST_TIME_NOW, test.getEndMillis());
481: }
482:
483: public void testConstructor_RI_RD3() throws Throwable {
484: DateTime dt = new DateTime(TEST_TIME_NOW);
485: Interval test = new Interval(dt, (ReadableDuration) null);
486: assertEquals(dt.getMillis(), test.getStartMillis());
487: assertEquals(dt.getMillis(), test.getEndMillis());
488: }
489:
490: public void testConstructor_RI_RD4() throws Throwable {
491: long result = TEST_TIME_NOW;
492: result = ISOChronology.getInstance().monthOfYear().add(result,
493: 6);
494: result = ISOChronology.getInstance().hourOfDay().add(result, 1);
495:
496: Duration dur = new Duration(result - TEST_TIME_NOW);
497:
498: Interval test = new Interval((ReadableInstant) null, dur);
499: assertEquals(TEST_TIME_NOW, test.getStartMillis());
500: assertEquals(result, test.getEndMillis());
501: }
502:
503: public void testConstructor_RI_RD5() throws Throwable {
504: DateTime dt = new DateTime(TEST_TIME_NOW);
505: Duration dur = new Duration(-1);
506: try {
507: new Interval(dt, dur);
508: fail();
509: } catch (IllegalArgumentException ex) {
510: }
511: }
512:
513: //-----------------------------------------------------------------------
514: public void testConstructor_RD_RI1() throws Throwable {
515: long result = TEST_TIME_NOW;
516: result = ISOChronology.getInstance().months().add(result, -6);
517: result = ISOChronology.getInstance().hours().add(result, -1);
518:
519: DateTime dt = new DateTime(TEST_TIME_NOW);
520: Duration dur = new Duration(TEST_TIME_NOW - result);
521:
522: Interval test = new Interval(dur, dt);
523: assertEquals(result, test.getStartMillis());
524: assertEquals(dt.getMillis(), test.getEndMillis());
525: }
526:
527: public void testConstructor_RD_RI2() throws Throwable {
528: Interval test = new Interval((ReadableDuration) null,
529: (ReadableInstant) null);
530: assertEquals(TEST_TIME_NOW, test.getStartMillis());
531: assertEquals(TEST_TIME_NOW, test.getEndMillis());
532: }
533:
534: public void testConstructor_RD_RI3() throws Throwable {
535: DateTime dt = new DateTime(TEST_TIME_NOW);
536: Interval test = new Interval((ReadableDuration) null, dt);
537: assertEquals(dt.getMillis(), test.getStartMillis());
538: assertEquals(dt.getMillis(), test.getEndMillis());
539: }
540:
541: public void testConstructor_RD_RI4() throws Throwable {
542: long result = TEST_TIME_NOW;
543: result = ISOChronology.getInstance().monthOfYear().add(result,
544: -6);
545: result = ISOChronology.getInstance().hourOfDay()
546: .add(result, -1);
547:
548: Duration dur = new Duration(TEST_TIME_NOW - result);
549:
550: Interval test = new Interval(dur, (ReadableInstant) null);
551: assertEquals(result, test.getStartMillis());
552: assertEquals(TEST_TIME_NOW, test.getEndMillis());
553: }
554:
555: public void testConstructor_RD_RI5() throws Throwable {
556: DateTime dt = new DateTime(TEST_TIME_NOW);
557: Duration dur = new Duration(-1);
558: try {
559: new Interval(dur, dt);
560: fail();
561: } catch (IllegalArgumentException ex) {
562: }
563: }
564:
565: //-----------------------------------------------------------------------
566: public void testConstructor_Object1() throws Throwable {
567: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
568: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
569: Interval test = new Interval(dt1.toString() + '/'
570: + dt2.toString());
571: assertEquals(dt1.getMillis(), test.getStartMillis());
572: assertEquals(dt2.getMillis(), test.getEndMillis());
573: }
574:
575: public void testConstructor_Object2() throws Throwable {
576: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
577: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
578: Interval base = new Interval(dt1, dt2);
579:
580: Interval test = new Interval(base);
581: assertEquals(dt1.getMillis(), test.getStartMillis());
582: assertEquals(dt2.getMillis(), test.getEndMillis());
583: }
584:
585: public void testConstructor_Object3() throws Throwable {
586: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
587: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
588: MutableInterval base = new MutableInterval(dt1, dt2);
589:
590: Interval test = new Interval(base);
591: assertEquals(dt1.getMillis(), test.getStartMillis());
592: assertEquals(dt2.getMillis(), test.getEndMillis());
593: }
594:
595: public void testConstructor_Object4() throws Throwable {
596: MockInterval base = new MockInterval();
597: Interval test = new Interval(base);
598: assertEquals(base.getStartMillis(), test.getStartMillis());
599: assertEquals(base.getEndMillis(), test.getEndMillis());
600: }
601:
602: public void testConstructor_Object5() throws Throwable {
603: IntervalConverter oldConv = ConverterManager.getInstance()
604: .getIntervalConverter("");
605: IntervalConverter conv = new IntervalConverter() {
606: public boolean isReadableInterval(Object object,
607: Chronology chrono) {
608: return false;
609: }
610:
611: public void setInto(ReadWritableInterval interval,
612: Object object, Chronology chrono) {
613: interval.setChronology(chrono);
614: interval.setInterval(1234L, 5678L);
615: }
616:
617: public Class getSupportedType() {
618: return String.class;
619: }
620: };
621: try {
622: ConverterManager.getInstance().addIntervalConverter(conv);
623: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
624: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
625: Interval test = new Interval(dt1.toString() + '/'
626: + dt2.toString());
627: assertEquals(1234L, test.getStartMillis());
628: assertEquals(5678L, test.getEndMillis());
629: } finally {
630: ConverterManager.getInstance()
631: .addIntervalConverter(oldConv);
632: }
633: }
634:
635: public void testConstructor_Object6() throws Throwable {
636: IntervalConverter oldConv = ConverterManager.getInstance()
637: .getIntervalConverter(new Interval(0L, 0L));
638: IntervalConverter conv = new IntervalConverter() {
639: public boolean isReadableInterval(Object object,
640: Chronology chrono) {
641: return false;
642: }
643:
644: public void setInto(ReadWritableInterval interval,
645: Object object, Chronology chrono) {
646: interval.setChronology(chrono);
647: interval.setInterval(1234L, 5678L);
648: }
649:
650: public Class getSupportedType() {
651: return ReadableInterval.class;
652: }
653: };
654: try {
655: ConverterManager.getInstance().addIntervalConverter(conv);
656: Interval base = new Interval(-1000L, 1000L);
657: Interval test = new Interval(base);
658: assertEquals(1234L, test.getStartMillis());
659: assertEquals(5678L, test.getEndMillis());
660: } finally {
661: ConverterManager.getInstance()
662: .addIntervalConverter(oldConv);
663: }
664: }
665:
666: class MockInterval implements ReadableInterval {
667: public Chronology getChronology() {
668: return ISOChronology.getInstance();
669: }
670:
671: public long getStartMillis() {
672: return 1234L;
673: }
674:
675: public DateTime getStart() {
676: return new DateTime(1234L);
677: }
678:
679: public long getEndMillis() {
680: return 5678L;
681: }
682:
683: public DateTime getEnd() {
684: return new DateTime(5678L);
685: }
686:
687: public long toDurationMillis() {
688: return (5678L - 1234L);
689: }
690:
691: public Duration toDuration() {
692: return new Duration(5678L - 1234L);
693: }
694:
695: public boolean contains(long millisInstant) {
696: return false;
697: }
698:
699: public boolean containsNow() {
700: return false;
701: }
702:
703: public boolean contains(ReadableInstant instant) {
704: return false;
705: }
706:
707: public boolean contains(ReadableInterval interval) {
708: return false;
709: }
710:
711: public boolean overlaps(ReadableInterval interval) {
712: return false;
713: }
714:
715: public boolean isBefore(ReadableInstant instant) {
716: return false;
717: }
718:
719: public boolean isBefore(ReadableInterval interval) {
720: return false;
721: }
722:
723: public boolean isAfter(ReadableInstant instant) {
724: return false;
725: }
726:
727: public boolean isAfter(ReadableInterval interval) {
728: return false;
729: }
730:
731: public Interval toInterval() {
732: return null;
733: }
734:
735: public MutableInterval toMutableInterval() {
736: return null;
737: }
738:
739: public Period toPeriod() {
740: return null;
741: }
742:
743: public Period toPeriod(PeriodType type) {
744: return null;
745: }
746: }
747:
748: //-----------------------------------------------------------------------
749: public void testConstructor_Object_Chronology1() throws Throwable {
750: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
751: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
752: Interval base = new Interval(dt1, dt2);
753:
754: Interval test = new Interval(base, BuddhistChronology
755: .getInstance());
756: assertEquals(dt1.getMillis(), test.getStartMillis());
757: assertEquals(dt2.getMillis(), test.getEndMillis());
758: assertEquals(BuddhistChronology.getInstance(), test
759: .getChronology());
760: }
761:
762: public void testConstructor_Object_Chronology2() throws Throwable {
763: DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0);
764: DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1);
765: Interval base = new Interval(dt1, dt2);
766:
767: Interval test = new Interval(base, null);
768: assertEquals(dt1.getMillis(), test.getStartMillis());
769: assertEquals(dt2.getMillis(), test.getEndMillis());
770: assertEquals(ISOChronology.getInstance(), test.getChronology());
771: }
772:
773: }
|