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