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.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.util.Locale;
023: import java.util.TimeZone;
024:
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: import org.joda.time.base.AbstractDuration;
029: import org.joda.time.base.BaseDuration;
030: import org.joda.time.chrono.ISOChronology;
031:
032: /**
033: * This class is a Junit unit test for Duration.
034: *
035: * @author Stephen Colebourne
036: */
037: public class TestDuration_Basics extends TestCase {
038: // Test in 2002/03 as time zones are more well known
039: // (before the late 90's they were all over the place)
040:
041: private static final DateTimeZone PARIS = DateTimeZone
042: .forID("Europe/Paris");
043: private static final DateTimeZone LONDON = DateTimeZone
044: .forID("Europe/London");
045:
046: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
047: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
048: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
049: + 365 + 365 + 366 + 365;
050: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
051: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
052: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
053: + 365 + 365 + 366 + 365 + 365;
054:
055: // 2002-06-09
056: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
057: + 31L + 9L - 1L)
058: * DateTimeConstants.MILLIS_PER_DAY;
059:
060: // 2002-04-05
061: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
062: * DateTimeConstants.MILLIS_PER_DAY + 12L
063: * DateTimeConstants.MILLIS_PER_HOUR + 24L
064: * DateTimeConstants.MILLIS_PER_MINUTE;
065:
066: // 2003-05-06
067: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
068: * DateTimeConstants.MILLIS_PER_DAY
069: + 14L
070: * DateTimeConstants.MILLIS_PER_HOUR
071: + 28L
072: * DateTimeConstants.MILLIS_PER_MINUTE;
073:
074: private DateTimeZone originalDateTimeZone = null;
075: private TimeZone originalTimeZone = null;
076: private Locale originalLocale = null;
077:
078: public static void main(String[] args) {
079: junit.textui.TestRunner.run(suite());
080: }
081:
082: public static TestSuite suite() {
083: return new TestSuite(TestDuration_Basics.class);
084: }
085:
086: public TestDuration_Basics(String name) {
087: super (name);
088: }
089:
090: protected void setUp() throws Exception {
091: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
092: originalDateTimeZone = DateTimeZone.getDefault();
093: originalTimeZone = TimeZone.getDefault();
094: originalLocale = Locale.getDefault();
095: DateTimeZone.setDefault(LONDON);
096: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
097: Locale.setDefault(Locale.UK);
098: }
099:
100: protected void tearDown() throws Exception {
101: DateTimeUtils.setCurrentMillisSystem();
102: DateTimeZone.setDefault(originalDateTimeZone);
103: TimeZone.setDefault(originalTimeZone);
104: Locale.setDefault(originalLocale);
105: originalDateTimeZone = null;
106: originalTimeZone = null;
107: originalLocale = null;
108: }
109:
110: //-----------------------------------------------------------------------
111: public void testTest() {
112: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
113: TEST_TIME_NOW).toString());
114: assertEquals("2002-04-05T12:24:00.000Z",
115: new Instant(TEST_TIME1).toString());
116: assertEquals("2003-05-06T14:28:00.000Z",
117: new Instant(TEST_TIME2).toString());
118: }
119:
120: //-----------------------------------------------------------------------
121: public void testGetMillis() {
122: Duration test = new Duration(0L);
123: assertEquals(0, test.getMillis());
124:
125: test = new Duration(1234567890L);
126: assertEquals(1234567890L, test.getMillis());
127: }
128:
129: public void testEqualsHashCode() {
130: Duration test1 = new Duration(123L);
131: Duration test2 = new Duration(123L);
132: assertEquals(true, test1.equals(test2));
133: assertEquals(true, test2.equals(test1));
134: assertEquals(true, test1.equals(test1));
135: assertEquals(true, test2.equals(test2));
136: assertEquals(true, test1.hashCode() == test2.hashCode());
137: assertEquals(true, test1.hashCode() == test1.hashCode());
138: assertEquals(true, test2.hashCode() == test2.hashCode());
139:
140: Duration test3 = new Duration(321L);
141: assertEquals(false, test1.equals(test3));
142: assertEquals(false, test2.equals(test3));
143: assertEquals(false, test3.equals(test1));
144: assertEquals(false, test3.equals(test2));
145: assertEquals(false, test1.hashCode() == test3.hashCode());
146: assertEquals(false, test2.hashCode() == test3.hashCode());
147:
148: assertEquals(false, test1.equals("Hello"));
149: assertEquals(true, test1.equals(new MockDuration(123L)));
150: }
151:
152: class MockDuration extends AbstractDuration {
153: private final long iValue;
154:
155: public MockDuration(long value) {
156: super ();
157: iValue = value;
158: }
159:
160: public long getMillis() {
161: return iValue;
162: }
163: }
164:
165: public void testCompareTo() {
166: Duration test1 = new Duration(123L);
167: Duration test1a = new Duration(123L);
168: assertEquals(0, test1.compareTo(test1a));
169: assertEquals(0, test1a.compareTo(test1));
170: assertEquals(0, test1.compareTo(test1));
171: assertEquals(0, test1a.compareTo(test1a));
172:
173: Duration test2 = new Duration(321L);
174: assertEquals(-1, test1.compareTo(test2));
175: assertEquals(+1, test2.compareTo(test1));
176:
177: assertEquals(+1, test2.compareTo(new MockDuration(123L)));
178: assertEquals(0, test1.compareTo(new MockDuration(123L)));
179:
180: try {
181: test1.compareTo(null);
182: fail();
183: } catch (NullPointerException ex) {
184: }
185: try {
186: test1.compareTo(new Long(123L));
187: fail();
188: } catch (ClassCastException ex) {
189: }
190: }
191:
192: public void testIsEqual() {
193: Duration test1 = new Duration(123L);
194: Duration test1a = new Duration(123L);
195: assertEquals(true, test1.isEqual(test1a));
196: assertEquals(true, test1a.isEqual(test1));
197: assertEquals(true, test1.isEqual(test1));
198: assertEquals(true, test1a.isEqual(test1a));
199:
200: Duration test2 = new Duration(321L);
201: assertEquals(false, test1.isEqual(test2));
202: assertEquals(false, test2.isEqual(test1));
203:
204: assertEquals(false, test2.isEqual(new MockDuration(123L)));
205: assertEquals(true, test1.isEqual(new MockDuration(123L)));
206: assertEquals(false, test1.isEqual(null));
207: assertEquals(true, new Duration(0L).isEqual(null));
208: }
209:
210: public void testIsBefore() {
211: Duration test1 = new Duration(123L);
212: Duration test1a = new Duration(123L);
213: assertEquals(false, test1.isShorterThan(test1a));
214: assertEquals(false, test1a.isShorterThan(test1));
215: assertEquals(false, test1.isShorterThan(test1));
216: assertEquals(false, test1a.isShorterThan(test1a));
217:
218: Duration test2 = new Duration(321L);
219: assertEquals(true, test1.isShorterThan(test2));
220: assertEquals(false, test2.isShorterThan(test1));
221:
222: assertEquals(false, test2.isShorterThan(new MockDuration(123L)));
223: assertEquals(false, test1.isShorterThan(new MockDuration(123L)));
224: assertEquals(false, test1.isShorterThan(null));
225: assertEquals(false, new Duration(0L).isShorterThan(null));
226: }
227:
228: public void testIsAfter() {
229: Duration test1 = new Duration(123L);
230: Duration test1a = new Duration(123L);
231: assertEquals(false, test1.isLongerThan(test1a));
232: assertEquals(false, test1a.isLongerThan(test1));
233: assertEquals(false, test1.isLongerThan(test1));
234: assertEquals(false, test1a.isLongerThan(test1a));
235:
236: Duration test2 = new Duration(321L);
237: assertEquals(false, test1.isLongerThan(test2));
238: assertEquals(true, test2.isLongerThan(test1));
239:
240: assertEquals(true, test2.isLongerThan(new MockDuration(123L)));
241: assertEquals(false, test1.isLongerThan(new MockDuration(123L)));
242: assertEquals(true, test1.isLongerThan(null));
243: assertEquals(false, new Duration(0L).isLongerThan(null));
244: }
245:
246: //-----------------------------------------------------------------------
247: public void testSerialization() throws Exception {
248: Duration test = new Duration(123L);
249:
250: ByteArrayOutputStream baos = new ByteArrayOutputStream();
251: ObjectOutputStream oos = new ObjectOutputStream(baos);
252: oos.writeObject(test);
253: byte[] bytes = baos.toByteArray();
254: oos.close();
255:
256: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
257: ObjectInputStream ois = new ObjectInputStream(bais);
258: Duration result = (Duration) ois.readObject();
259: ois.close();
260:
261: assertEquals(test, result);
262: }
263:
264: //-----------------------------------------------------------------------
265: public void testToString() {
266: long length = (365L + 2L * 30L + 3L * 7L + 4L)
267: * DateTimeConstants.MILLIS_PER_DAY + 5L
268: * DateTimeConstants.MILLIS_PER_HOUR + 6L
269: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
270: * DateTimeConstants.MILLIS_PER_SECOND + 845L;
271: Duration test = new Duration(length);
272: assertEquals("PT" + (length / 1000) + "." + (length % 1000)
273: + "S", test.toString());
274:
275: test = new Duration(0L);
276: assertEquals("PT0S", test.toString());
277:
278: test = new Duration(12345L);
279: assertEquals("PT12.345S", test.toString());
280:
281: test = new Duration(-12345L);
282: assertEquals("PT-12.345S", test.toString());
283: }
284:
285: //-----------------------------------------------------------------------
286: public void testToDuration1() {
287: Duration test = new Duration(123L);
288: Duration result = test.toDuration();
289: assertSame(test, result);
290: }
291:
292: public void testToDuration2() {
293: MockDuration test = new MockDuration(123L);
294: Duration result = test.toDuration();
295: assertNotSame(test, result);
296: assertEquals(test, result);
297: }
298:
299: //-----------------------------------------------------------------------
300: public void testToPeriod() {
301: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
302: * DateTimeConstants.MILLIS_PER_DAY + 5L
303: * DateTimeConstants.MILLIS_PER_HOUR + 6L
304: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
305: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
306: Duration test = new Duration(length);
307: Period result = test.toPeriod();
308: assertEquals(new Period(test), result);
309: assertEquals(new Period(test.getMillis()), result);
310: }
311:
312: //-----------------------------------------------------------------------
313: public void testToPeriod_PeriodType() {
314: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
315: * DateTimeConstants.MILLIS_PER_DAY + 5L
316: * DateTimeConstants.MILLIS_PER_HOUR + 6L
317: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
318: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
319: Duration test = new Duration(length);
320: Period result = test.toPeriod(PeriodType.standard()
321: .withMillisRemoved());
322: assertEquals(new Period(test, PeriodType.standard()
323: .withMillisRemoved()), result);
324: assertEquals(new Period(test.getMillis(), PeriodType.standard()
325: .withMillisRemoved()), result);
326: }
327:
328: //-----------------------------------------------------------------------
329: public void testToPeriod_Chronology() {
330: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
331: * DateTimeConstants.MILLIS_PER_DAY + 5L
332: * DateTimeConstants.MILLIS_PER_HOUR + 6L
333: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
334: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
335: Duration test = new Duration(length);
336: Period result = test.toPeriod(ISOChronology.getInstanceUTC());
337: assertEquals(new Period(test, ISOChronology.getInstanceUTC()),
338: result);
339: assertEquals(new Period(test.getMillis(), ISOChronology
340: .getInstanceUTC()), result);
341: }
342:
343: //-----------------------------------------------------------------------
344: public void testToPeriod_PeriodType_Chronology() {
345: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
346: * DateTimeConstants.MILLIS_PER_DAY + 5L
347: * DateTimeConstants.MILLIS_PER_HOUR + 6L
348: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
349: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
350: Duration test = new Duration(length);
351: Period result = test.toPeriod(PeriodType.standard()
352: .withMillisRemoved(), ISOChronology.getInstanceUTC());
353: assertEquals(new Period(test, PeriodType.standard()
354: .withMillisRemoved(), ISOChronology.getInstanceUTC()),
355: result);
356: assertEquals(new Period(test.getMillis(), PeriodType.standard()
357: .withMillisRemoved(), ISOChronology.getInstanceUTC()),
358: result);
359: }
360:
361: //-----------------------------------------------------------------------
362: public void testToPeriodFrom() {
363: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
364: * DateTimeConstants.MILLIS_PER_DAY + 5L
365: * DateTimeConstants.MILLIS_PER_HOUR + 6L
366: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
367: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
368: Duration test = new Duration(length);
369: DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
370: Period result = test.toPeriodFrom(dt);
371: assertEquals(new Period(dt, test), result);
372: }
373:
374: //-----------------------------------------------------------------------
375: public void testToPeriodFrom_PeriodType() {
376: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
377: * DateTimeConstants.MILLIS_PER_DAY + 5L
378: * DateTimeConstants.MILLIS_PER_HOUR + 6L
379: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
380: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
381: Duration test = new Duration(length);
382: DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
383: Period result = test.toPeriodFrom(dt, PeriodType.standard()
384: .withMillisRemoved());
385: assertEquals(new Period(dt, test, PeriodType.standard()
386: .withMillisRemoved()), result);
387: }
388:
389: //-----------------------------------------------------------------------
390: public void testToPeriodTo() {
391: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
392: * DateTimeConstants.MILLIS_PER_DAY + 5L
393: * DateTimeConstants.MILLIS_PER_HOUR + 6L
394: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
395: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
396: Duration test = new Duration(length);
397: DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
398: Period result = test.toPeriodTo(dt);
399: assertEquals(new Period(test, dt), result);
400: }
401:
402: //-----------------------------------------------------------------------
403: public void testToPeriodTo_PeriodType() {
404: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
405: * DateTimeConstants.MILLIS_PER_DAY + 5L
406: * DateTimeConstants.MILLIS_PER_HOUR + 6L
407: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
408: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
409: Duration test = new Duration(length);
410: DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
411: Period result = test.toPeriodTo(dt, PeriodType.standard()
412: .withMillisRemoved());
413: assertEquals(new Period(test, dt, PeriodType.standard()
414: .withMillisRemoved()), result);
415: }
416:
417: //-----------------------------------------------------------------------
418: public void testToIntervalFrom() {
419: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
420: * DateTimeConstants.MILLIS_PER_DAY + 5L
421: * DateTimeConstants.MILLIS_PER_HOUR + 6L
422: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
423: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
424: Duration test = new Duration(length);
425: DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
426: Interval result = test.toIntervalFrom(dt);
427: assertEquals(new Interval(dt, test), result);
428: }
429:
430: //-----------------------------------------------------------------------
431: public void testToIntervalTo() {
432: long length = (4L + (3L * 7L) + (2L * 30L) + 365L)
433: * DateTimeConstants.MILLIS_PER_DAY + 5L
434: * DateTimeConstants.MILLIS_PER_HOUR + 6L
435: * DateTimeConstants.MILLIS_PER_MINUTE + 7L
436: * DateTimeConstants.MILLIS_PER_SECOND + 8L;
437: Duration test = new Duration(length);
438: DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
439: Interval result = test.toIntervalTo(dt);
440: assertEquals(new Interval(test, dt), result);
441: }
442:
443: //-----------------------------------------------------------------------
444: public void testWithMillis1() {
445: Duration test = new Duration(123L);
446: Duration result = test.withMillis(123L);
447: assertSame(test, result);
448: }
449:
450: public void testWithMillis2() {
451: Duration test = new Duration(123L);
452: Duration result = test.withMillis(1234567890L);
453: assertEquals(1234567890L, result.getMillis());
454: }
455:
456: //-----------------------------------------------------------------------
457: public void testWithDurationAdded_long_int1() {
458: Duration test = new Duration(123L);
459: Duration result = test.withDurationAdded(8000L, 1);
460: assertEquals(8123L, result.getMillis());
461: }
462:
463: public void testWithDurationAdded_long_int2() {
464: Duration test = new Duration(123L);
465: Duration result = test.withDurationAdded(8000L, 2);
466: assertEquals(16123L, result.getMillis());
467: }
468:
469: public void testWithDurationAdded_long_int3() {
470: Duration test = new Duration(123L);
471: Duration result = test.withDurationAdded(8000L, -1);
472: assertEquals((123L - 8000L), result.getMillis());
473: }
474:
475: public void testWithDurationAdded_long_int4() {
476: Duration test = new Duration(123L);
477: Duration result = test.withDurationAdded(0L, 1);
478: assertSame(test, result);
479: }
480:
481: public void testWithDurationAdded_long_int5() {
482: Duration test = new Duration(123L);
483: Duration result = test.withDurationAdded(8000L, 0);
484: assertSame(test, result);
485: }
486:
487: //-----------------------------------------------------------------------
488: public void testPlus_long1() {
489: Duration test = new Duration(123L);
490: Duration result = test.plus(8000L);
491: assertEquals(8123L, result.getMillis());
492: }
493:
494: public void testPlus_long2() {
495: Duration test = new Duration(123L);
496: Duration result = test.plus(0L);
497: assertSame(test, result);
498: }
499:
500: //-----------------------------------------------------------------------
501: public void testMinus_long1() {
502: Duration test = new Duration(123L);
503: Duration result = test.minus(8000L);
504: assertEquals(123L - 8000L, result.getMillis());
505: }
506:
507: public void testMinus_long2() {
508: Duration test = new Duration(123L);
509: Duration result = test.minus(0L);
510: assertSame(test, result);
511: }
512:
513: //-----------------------------------------------------------------------
514: public void testWithDurationAdded_RD_int1() {
515: Duration test = new Duration(123L);
516: Duration result = test
517: .withDurationAdded(new Duration(8000L), 1);
518: assertEquals(8123L, result.getMillis());
519: }
520:
521: public void testWithDurationAdded_RD_int2() {
522: Duration test = new Duration(123L);
523: Duration result = test
524: .withDurationAdded(new Duration(8000L), 2);
525: assertEquals(16123L, result.getMillis());
526: }
527:
528: public void testWithDurationAdded_RD_int3() {
529: Duration test = new Duration(123L);
530: Duration result = test.withDurationAdded(new Duration(8000L),
531: -1);
532: assertEquals((123L - 8000L), result.getMillis());
533: }
534:
535: public void testWithDurationAdded_RD_int4() {
536: Duration test = new Duration(123L);
537: Duration result = test.withDurationAdded(new Duration(0L), 1);
538: assertSame(test, result);
539: }
540:
541: public void testWithDurationAdded_RD_int5() {
542: Duration test = new Duration(123L);
543: Duration result = test
544: .withDurationAdded(new Duration(8000L), 0);
545: assertSame(test, result);
546: }
547:
548: public void testWithDurationAdded_RD_int6() {
549: Duration test = new Duration(123L);
550: Duration result = test.withDurationAdded(null, 0);
551: assertSame(test, result);
552: }
553:
554: //-----------------------------------------------------------------------
555: public void testPlus_RD1() {
556: Duration test = new Duration(123L);
557: Duration result = test.plus(new Duration(8000L));
558: assertEquals(8123L, result.getMillis());
559: }
560:
561: public void testPlus_RD2() {
562: Duration test = new Duration(123L);
563: Duration result = test.plus(new Duration(0L));
564: assertSame(test, result);
565: }
566:
567: public void testPlus_RD3() {
568: Duration test = new Duration(123L);
569: Duration result = test.plus(null);
570: assertSame(test, result);
571: }
572:
573: //-----------------------------------------------------------------------
574: public void testMinus_RD1() {
575: Duration test = new Duration(123L);
576: Duration result = test.minus(new Duration(8000L));
577: assertEquals(123L - 8000L, result.getMillis());
578: }
579:
580: public void testMinus_RD2() {
581: Duration test = new Duration(123L);
582: Duration result = test.minus(new Duration(0L));
583: assertSame(test, result);
584: }
585:
586: public void testMinus_RD3() {
587: Duration test = new Duration(123L);
588: Duration result = test.minus(null);
589: assertSame(test, result);
590: }
591:
592: //-----------------------------------------------------------------------
593: public void testMutableDuration() {
594: // no MutableDuration, so...
595: MockMutableDuration test = new MockMutableDuration(123L);
596: assertEquals(123L, test.getMillis());
597:
598: test.setMillis(2345L);
599: assertEquals(2345L, test.getMillis());
600: }
601:
602: static class MockMutableDuration extends BaseDuration {
603: public MockMutableDuration(long duration) {
604: super (duration);
605: }
606:
607: public void setMillis(long duration) {
608: super.setMillis(duration);
609: }
610: }
611:
612: }
|