001 /*
002 * Copyright 2001-2009 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 LONDON = DateTimeZone.forID("Europe/London");
042
043 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
044 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
045 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
046 366 + 365;
047 long y2003days = 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 + 365 + 365 + 366 + 365 + 365 + 365 +
050 366 + 365 + 365;
051
052 // 2002-06-09
053 private long TEST_TIME_NOW =
054 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
055
056 // 2002-04-05
057 private long TEST_TIME1 =
058 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
059 + 12L * DateTimeConstants.MILLIS_PER_HOUR
060 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
061
062 // 2003-05-06
063 private long TEST_TIME2 =
064 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
065 + 14L * DateTimeConstants.MILLIS_PER_HOUR
066 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
067
068 private DateTimeZone originalDateTimeZone = null;
069 private TimeZone originalTimeZone = null;
070 private Locale originalLocale = null;
071
072 public static void main(String[] args) {
073 junit.textui.TestRunner.run(suite());
074 }
075
076 public static TestSuite suite() {
077 return new TestSuite(TestDuration_Basics.class);
078 }
079
080 public TestDuration_Basics(String name) {
081 super(name);
082 }
083
084 protected void setUp() throws Exception {
085 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
086 originalDateTimeZone = DateTimeZone.getDefault();
087 originalTimeZone = TimeZone.getDefault();
088 originalLocale = Locale.getDefault();
089 DateTimeZone.setDefault(LONDON);
090 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
091 Locale.setDefault(Locale.UK);
092 }
093
094 protected void tearDown() throws Exception {
095 DateTimeUtils.setCurrentMillisSystem();
096 DateTimeZone.setDefault(originalDateTimeZone);
097 TimeZone.setDefault(originalTimeZone);
098 Locale.setDefault(originalLocale);
099 originalDateTimeZone = null;
100 originalTimeZone = null;
101 originalLocale = null;
102 }
103
104 //-----------------------------------------------------------------------
105 public void testTest() {
106 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString());
107 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString());
108 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString());
109 }
110
111 //-----------------------------------------------------------------------
112 public void testGetMillis() {
113 Duration test = new Duration(0L);
114 assertEquals(0, test.getMillis());
115
116 test = new Duration(1234567890L);
117 assertEquals(1234567890L, test.getMillis());
118 }
119
120 public void testEqualsHashCode() {
121 Duration test1 = new Duration(123L);
122 Duration test2 = new Duration(123L);
123 assertEquals(true, test1.equals(test2));
124 assertEquals(true, test2.equals(test1));
125 assertEquals(true, test1.equals(test1));
126 assertEquals(true, test2.equals(test2));
127 assertEquals(true, test1.hashCode() == test2.hashCode());
128 assertEquals(true, test1.hashCode() == test1.hashCode());
129 assertEquals(true, test2.hashCode() == test2.hashCode());
130
131 Duration test3 = new Duration(321L);
132 assertEquals(false, test1.equals(test3));
133 assertEquals(false, test2.equals(test3));
134 assertEquals(false, test3.equals(test1));
135 assertEquals(false, test3.equals(test2));
136 assertEquals(false, test1.hashCode() == test3.hashCode());
137 assertEquals(false, test2.hashCode() == test3.hashCode());
138
139 assertEquals(false, test1.equals("Hello"));
140 assertEquals(true, test1.equals(new MockDuration(123L)));
141 }
142
143 class MockDuration extends AbstractDuration {
144 private final long iValue;
145 public MockDuration(long value) {
146 super();
147 iValue = value;
148 }
149 public long getMillis() {
150 return iValue;
151 }
152 }
153
154 public void testCompareTo() {
155 Duration test1 = new Duration(123L);
156 Duration test1a = new Duration(123L);
157 assertEquals(0, test1.compareTo(test1a));
158 assertEquals(0, test1a.compareTo(test1));
159 assertEquals(0, test1.compareTo(test1));
160 assertEquals(0, test1a.compareTo(test1a));
161
162 Duration test2 = new Duration(321L);
163 assertEquals(-1, test1.compareTo(test2));
164 assertEquals(+1, test2.compareTo(test1));
165
166 assertEquals(+1, test2.compareTo(new MockDuration(123L)));
167 assertEquals(0, test1.compareTo(new MockDuration(123L)));
168
169 try {
170 test1.compareTo(null);
171 fail();
172 } catch (NullPointerException ex) {}
173 // try {
174 // test1.compareTo(new Long(123L));
175 // fail();
176 // } catch (ClassCastException ex) {}
177 }
178
179 public void testIsEqual() {
180 Duration test1 = new Duration(123L);
181 Duration test1a = new Duration(123L);
182 assertEquals(true, test1.isEqual(test1a));
183 assertEquals(true, test1a.isEqual(test1));
184 assertEquals(true, test1.isEqual(test1));
185 assertEquals(true, test1a.isEqual(test1a));
186
187 Duration test2 = new Duration(321L);
188 assertEquals(false, test1.isEqual(test2));
189 assertEquals(false, test2.isEqual(test1));
190
191 assertEquals(false, test2.isEqual(new MockDuration(123L)));
192 assertEquals(true, test1.isEqual(new MockDuration(123L)));
193 assertEquals(false, test1.isEqual(null));
194 assertEquals(true, new Duration(0L).isEqual(null));
195 }
196
197 public void testIsBefore() {
198 Duration test1 = new Duration(123L);
199 Duration test1a = new Duration(123L);
200 assertEquals(false, test1.isShorterThan(test1a));
201 assertEquals(false, test1a.isShorterThan(test1));
202 assertEquals(false, test1.isShorterThan(test1));
203 assertEquals(false, test1a.isShorterThan(test1a));
204
205 Duration test2 = new Duration(321L);
206 assertEquals(true, test1.isShorterThan(test2));
207 assertEquals(false, test2.isShorterThan(test1));
208
209 assertEquals(false, test2.isShorterThan(new MockDuration(123L)));
210 assertEquals(false, test1.isShorterThan(new MockDuration(123L)));
211 assertEquals(false, test1.isShorterThan(null));
212 assertEquals(false, new Duration(0L).isShorterThan(null));
213 }
214
215 public void testIsAfter() {
216 Duration test1 = new Duration(123L);
217 Duration test1a = new Duration(123L);
218 assertEquals(false, test1.isLongerThan(test1a));
219 assertEquals(false, test1a.isLongerThan(test1));
220 assertEquals(false, test1.isLongerThan(test1));
221 assertEquals(false, test1a.isLongerThan(test1a));
222
223 Duration test2 = new Duration(321L);
224 assertEquals(false, test1.isLongerThan(test2));
225 assertEquals(true, test2.isLongerThan(test1));
226
227 assertEquals(true, test2.isLongerThan(new MockDuration(123L)));
228 assertEquals(false, test1.isLongerThan(new MockDuration(123L)));
229 assertEquals(true, test1.isLongerThan(null));
230 assertEquals(false, new Duration(0L).isLongerThan(null));
231 }
232
233 //-----------------------------------------------------------------------
234 public void testSerialization() throws Exception {
235 Duration test = new Duration(123L);
236
237 ByteArrayOutputStream baos = new ByteArrayOutputStream();
238 ObjectOutputStream oos = new ObjectOutputStream(baos);
239 oos.writeObject(test);
240 byte[] bytes = baos.toByteArray();
241 oos.close();
242
243 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
244 ObjectInputStream ois = new ObjectInputStream(bais);
245 Duration result = (Duration) ois.readObject();
246 ois.close();
247
248 assertEquals(test, result);
249 }
250
251 //-----------------------------------------------------------------------
252 public void testGetStandardSeconds() {
253 Duration test = new Duration(0L);
254 assertEquals(0, test.getStandardSeconds());
255 test = new Duration(1L);
256 assertEquals(0, test.getStandardSeconds());
257 test = new Duration(999L);
258 assertEquals(0, test.getStandardSeconds());
259 test = new Duration(1000L);
260 assertEquals(1, test.getStandardSeconds());
261 test = new Duration(1001L);
262 assertEquals(1, test.getStandardSeconds());
263 test = new Duration(1999L);
264 assertEquals(1, test.getStandardSeconds());
265 test = new Duration(2000L);
266 assertEquals(2, test.getStandardSeconds());
267 test = new Duration(-1L);
268 assertEquals(0, test.getStandardSeconds());
269 test = new Duration(-999L);
270 assertEquals(0, test.getStandardSeconds());
271 test = new Duration(-1000L);
272 assertEquals(-1, test.getStandardSeconds());
273 }
274
275 //-----------------------------------------------------------------------
276 public void testToString() {
277 long length = (365L + 2L * 30L + 3L * 7L + 4L) * DateTimeConstants.MILLIS_PER_DAY +
278 5L * DateTimeConstants.MILLIS_PER_HOUR +
279 6L * DateTimeConstants.MILLIS_PER_MINUTE +
280 7L * DateTimeConstants.MILLIS_PER_SECOND + 845L;
281 Duration test = new Duration(length);
282 assertEquals("PT" + (length / 1000) + "." + (length % 1000) + "S", test.toString());
283
284 assertEquals("PT0S", new Duration(0L).toString());
285 assertEquals("PT10S", new Duration(10000L).toString());
286 assertEquals("PT1S", new Duration(1000L).toString());
287 assertEquals("PT12.345S", new Duration(12345L).toString());
288 assertEquals("PT-12.345S", new Duration(-12345L).toString());
289 assertEquals("PT-1.123S", new Duration(-1123L).toString());
290 assertEquals("PT-0.123S", new Duration(-123L).toString());
291 assertEquals("PT-0.012S", new Duration(-12L).toString());
292 assertEquals("PT-0.001S", new Duration(-1L).toString());
293 }
294
295 //-----------------------------------------------------------------------
296 public void testToDuration1() {
297 Duration test = new Duration(123L);
298 Duration result = test.toDuration();
299 assertSame(test, result);
300 }
301
302 public void testToDuration2() {
303 MockDuration test = new MockDuration(123L);
304 Duration result = test.toDuration();
305 assertNotSame(test, result);
306 assertEquals(test, result);
307 }
308
309 //-----------------------------------------------------------------------
310 public void testToStandardDays() {
311 Duration test = new Duration(0L);
312 assertEquals(Days.days(0), test.toStandardDays());
313 test = new Duration(1L);
314 assertEquals(Days.days(0), test.toStandardDays());
315 test = new Duration(24 * 60 * 60000L - 1);
316 assertEquals(Days.days(0), test.toStandardDays());
317 test = new Duration(24 * 60 * 60000L);
318 assertEquals(Days.days(1), test.toStandardDays());
319 test = new Duration(24 * 60 * 60000L + 1);
320 assertEquals(Days.days(1), test.toStandardDays());
321 test = new Duration(2 * 24 * 60 * 60000L - 1);
322 assertEquals(Days.days(1), test.toStandardDays());
323 test = new Duration(2 * 24 * 60 * 60000L);
324 assertEquals(Days.days(2), test.toStandardDays());
325 test = new Duration(-1L);
326 assertEquals(Days.days(0), test.toStandardDays());
327 test = new Duration(-24 * 60 * 60000L + 1);
328 assertEquals(Days.days(0), test.toStandardDays());
329 test = new Duration(-24 * 60 * 60000L);
330 assertEquals(Days.days(-1), test.toStandardDays());
331 }
332
333 public void testToStandardDays_overflow() {
334 Duration test = new Duration((((long) Integer.MAX_VALUE) + 1) * 24L * 60L * 60000L);
335 try {
336 test.toStandardDays();
337 fail();
338 } catch (ArithmeticException ex) {
339 // expected
340 }
341 }
342
343 //-----------------------------------------------------------------------
344 public void testToStandardHours() {
345 Duration test = new Duration(0L);
346 assertEquals(Hours.hours(0), test.toStandardHours());
347 test = new Duration(1L);
348 assertEquals(Hours.hours(0), test.toStandardHours());
349 test = new Duration(3600000L - 1);
350 assertEquals(Hours.hours(0), test.toStandardHours());
351 test = new Duration(3600000L);
352 assertEquals(Hours.hours(1), test.toStandardHours());
353 test = new Duration(3600000L + 1);
354 assertEquals(Hours.hours(1), test.toStandardHours());
355 test = new Duration(2 * 3600000L - 1);
356 assertEquals(Hours.hours(1), test.toStandardHours());
357 test = new Duration(2 * 3600000L);
358 assertEquals(Hours.hours(2), test.toStandardHours());
359 test = new Duration(-1L);
360 assertEquals(Hours.hours(0), test.toStandardHours());
361 test = new Duration(-3600000L + 1);
362 assertEquals(Hours.hours(0), test.toStandardHours());
363 test = new Duration(-3600000L);
364 assertEquals(Hours.hours(-1), test.toStandardHours());
365 }
366
367 public void testToStandardHours_overflow() {
368 Duration test = new Duration(((long) Integer.MAX_VALUE) * 3600000L + 3600000L);
369 try {
370 test.toStandardHours();
371 fail();
372 } catch (ArithmeticException ex) {
373 // expected
374 }
375 }
376
377 //-----------------------------------------------------------------------
378 public void testToStandardMinutes() {
379 Duration test = new Duration(0L);
380 assertEquals(Minutes.minutes(0), test.toStandardMinutes());
381 test = new Duration(1L);
382 assertEquals(Minutes.minutes(0), test.toStandardMinutes());
383 test = new Duration(60000L - 1);
384 assertEquals(Minutes.minutes(0), test.toStandardMinutes());
385 test = new Duration(60000L);
386 assertEquals(Minutes.minutes(1), test.toStandardMinutes());
387 test = new Duration(60000L + 1);
388 assertEquals(Minutes.minutes(1), test.toStandardMinutes());
389 test = new Duration(2 * 60000L - 1);
390 assertEquals(Minutes.minutes(1), test.toStandardMinutes());
391 test = new Duration(2 * 60000L);
392 assertEquals(Minutes.minutes(2), test.toStandardMinutes());
393 test = new Duration(-1L);
394 assertEquals(Minutes.minutes(0), test.toStandardMinutes());
395 test = new Duration(-60000L + 1);
396 assertEquals(Minutes.minutes(0), test.toStandardMinutes());
397 test = new Duration(-60000L);
398 assertEquals(Minutes.minutes(-1), test.toStandardMinutes());
399 }
400
401 public void testToStandardMinutes_overflow() {
402 Duration test = new Duration(((long) Integer.MAX_VALUE) * 60000L + 60000L);
403 try {
404 test.toStandardMinutes();
405 fail();
406 } catch (ArithmeticException ex) {
407 // expected
408 }
409 }
410
411 //-----------------------------------------------------------------------
412 public void testToStandardSeconds() {
413 Duration test = new Duration(0L);
414 assertEquals(Seconds.seconds(0), test.toStandardSeconds());
415 test = new Duration(1L);
416 assertEquals(Seconds.seconds(0), test.toStandardSeconds());
417 test = new Duration(999L);
418 assertEquals(Seconds.seconds(0), test.toStandardSeconds());
419 test = new Duration(1000L);
420 assertEquals(Seconds.seconds(1), test.toStandardSeconds());
421 test = new Duration(1001L);
422 assertEquals(Seconds.seconds(1), test.toStandardSeconds());
423 test = new Duration(1999L);
424 assertEquals(Seconds.seconds(1), test.toStandardSeconds());
425 test = new Duration(2000L);
426 assertEquals(Seconds.seconds(2), test.toStandardSeconds());
427 test = new Duration(-1L);
428 assertEquals(Seconds.seconds(0), test.toStandardSeconds());
429 test = new Duration(-999L);
430 assertEquals(Seconds.seconds(0), test.toStandardSeconds());
431 test = new Duration(-1000L);
432 assertEquals(Seconds.seconds(-1), test.toStandardSeconds());
433 }
434
435 public void testToStandardSeconds_overflow() {
436 Duration test = new Duration(((long) Integer.MAX_VALUE) * 1000L + 1000L);
437 try {
438 test.toStandardSeconds();
439 fail();
440 } catch (ArithmeticException ex) {
441 // expected
442 }
443 }
444
445 //-----------------------------------------------------------------------
446 public void testToPeriod() {
447 DateTimeZone zone = DateTimeZone.getDefault();
448 try {
449 DateTimeZone.setDefault(DateTimeZone.forID("Europe/Paris"));
450 long length =
451 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
452 5L * DateTimeConstants.MILLIS_PER_HOUR +
453 6L * DateTimeConstants.MILLIS_PER_MINUTE +
454 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
455 Duration dur = new Duration(length);
456 Period test = dur.toPeriod();
457 assertEquals(0, test.getYears()); // (4 + (3 * 7) + (2 * 30) + 365) == 450
458 assertEquals(0, test.getMonths());
459 assertEquals(0, test.getWeeks());
460 assertEquals(0, test.getDays());
461 assertEquals((450 * 24) + 5, test.getHours());
462 assertEquals(6, test.getMinutes());
463 assertEquals(7, test.getSeconds());
464 assertEquals(8, test.getMillis());
465 } finally {
466 DateTimeZone.setDefault(zone);
467 }
468 }
469
470 public void testToPeriod_fixedZone() throws Throwable {
471 DateTimeZone zone = DateTimeZone.getDefault();
472 try {
473 DateTimeZone.setDefault(DateTimeZone.forOffsetHours(2));
474 long length =
475 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
476 5L * DateTimeConstants.MILLIS_PER_HOUR +
477 6L * DateTimeConstants.MILLIS_PER_MINUTE +
478 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
479 Duration dur = new Duration(length);
480 Period test = dur.toPeriod();
481 assertEquals(0, test.getYears()); // (4 + (3 * 7) + (2 * 30) + 365) == 450
482 assertEquals(0, test.getMonths());
483 assertEquals(0, test.getWeeks());
484 assertEquals(0, test.getDays());
485 assertEquals((450 * 24) + 5, test.getHours());
486 assertEquals(6, test.getMinutes());
487 assertEquals(7, test.getSeconds());
488 assertEquals(8, test.getMillis());
489 } finally {
490 DateTimeZone.setDefault(zone);
491 }
492 }
493
494 //-----------------------------------------------------------------------
495 public void testToPeriod_PeriodType() {
496 long length =
497 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
498 5L * DateTimeConstants.MILLIS_PER_HOUR +
499 6L * DateTimeConstants.MILLIS_PER_MINUTE +
500 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
501 Duration test = new Duration(length);
502 Period result = test.toPeriod(PeriodType.standard().withMillisRemoved());
503 assertEquals(new Period(test, PeriodType.standard().withMillisRemoved()), result);
504 assertEquals(new Period(test.getMillis(), PeriodType.standard().withMillisRemoved()), result);
505 }
506
507 //-----------------------------------------------------------------------
508 public void testToPeriod_Chronology() {
509 long length =
510 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
511 5L * DateTimeConstants.MILLIS_PER_HOUR +
512 6L * DateTimeConstants.MILLIS_PER_MINUTE +
513 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
514 Duration test = new Duration(length);
515 Period result = test.toPeriod(ISOChronology.getInstanceUTC());
516 assertEquals(new Period(test, ISOChronology.getInstanceUTC()), result);
517 assertEquals(new Period(test.getMillis(), ISOChronology.getInstanceUTC()), result);
518 }
519
520 //-----------------------------------------------------------------------
521 public void testToPeriod_PeriodType_Chronology() {
522 long length =
523 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
524 5L * DateTimeConstants.MILLIS_PER_HOUR +
525 6L * DateTimeConstants.MILLIS_PER_MINUTE +
526 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
527 Duration test = new Duration(length);
528 Period result = test.toPeriod(PeriodType.standard().withMillisRemoved(), ISOChronology.getInstanceUTC());
529 assertEquals(new Period(test, PeriodType.standard().withMillisRemoved(), ISOChronology.getInstanceUTC()), result);
530 assertEquals(new Period(test.getMillis(), PeriodType.standard().withMillisRemoved(), ISOChronology.getInstanceUTC()), result);
531 }
532
533 //-----------------------------------------------------------------------
534 public void testToPeriodFrom() {
535 long length =
536 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
537 5L * DateTimeConstants.MILLIS_PER_HOUR +
538 6L * DateTimeConstants.MILLIS_PER_MINUTE +
539 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
540 Duration test = new Duration(length);
541 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
542 Period result = test.toPeriodFrom(dt);
543 assertEquals(new Period(dt, test), result);
544 }
545
546 //-----------------------------------------------------------------------
547 public void testToPeriodFrom_PeriodType() {
548 long length =
549 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
550 5L * DateTimeConstants.MILLIS_PER_HOUR +
551 6L * DateTimeConstants.MILLIS_PER_MINUTE +
552 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
553 Duration test = new Duration(length);
554 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
555 Period result = test.toPeriodFrom(dt, PeriodType.standard().withMillisRemoved());
556 assertEquals(new Period(dt, test, PeriodType.standard().withMillisRemoved()), result);
557 }
558
559 //-----------------------------------------------------------------------
560 public void testToPeriodTo() {
561 long length =
562 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
563 5L * DateTimeConstants.MILLIS_PER_HOUR +
564 6L * DateTimeConstants.MILLIS_PER_MINUTE +
565 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
566 Duration test = new Duration(length);
567 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
568 Period result = test.toPeriodTo(dt);
569 assertEquals(new Period(test, dt), result);
570 }
571
572 //-----------------------------------------------------------------------
573 public void testToPeriodTo_PeriodType() {
574 long length =
575 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
576 5L * DateTimeConstants.MILLIS_PER_HOUR +
577 6L * DateTimeConstants.MILLIS_PER_MINUTE +
578 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
579 Duration test = new Duration(length);
580 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
581 Period result = test.toPeriodTo(dt, PeriodType.standard().withMillisRemoved());
582 assertEquals(new Period(test, dt, PeriodType.standard().withMillisRemoved()), result);
583 }
584
585 //-----------------------------------------------------------------------
586 public void testToIntervalFrom() {
587 long length =
588 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
589 5L * DateTimeConstants.MILLIS_PER_HOUR +
590 6L * DateTimeConstants.MILLIS_PER_MINUTE +
591 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
592 Duration test = new Duration(length);
593 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
594 Interval result = test.toIntervalFrom(dt);
595 assertEquals(new Interval(dt, test), result);
596 }
597
598 //-----------------------------------------------------------------------
599 public void testToIntervalTo() {
600 long length =
601 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY +
602 5L * DateTimeConstants.MILLIS_PER_HOUR +
603 6L * DateTimeConstants.MILLIS_PER_MINUTE +
604 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L;
605 Duration test = new Duration(length);
606 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0);
607 Interval result = test.toIntervalTo(dt);
608 assertEquals(new Interval(test, dt), result);
609 }
610
611 //-----------------------------------------------------------------------
612 public void testWithMillis1() {
613 Duration test = new Duration(123L);
614 Duration result = test.withMillis(123L);
615 assertSame(test, result);
616 }
617
618 public void testWithMillis2() {
619 Duration test = new Duration(123L);
620 Duration result = test.withMillis(1234567890L);
621 assertEquals(1234567890L, result.getMillis());
622 }
623
624 //-----------------------------------------------------------------------
625 public void testWithDurationAdded_long_int1() {
626 Duration test = new Duration(123L);
627 Duration result = test.withDurationAdded(8000L, 1);
628 assertEquals(8123L, result.getMillis());
629 }
630
631 public void testWithDurationAdded_long_int2() {
632 Duration test = new Duration(123L);
633 Duration result = test.withDurationAdded(8000L, 2);
634 assertEquals(16123L, result.getMillis());
635 }
636
637 public void testWithDurationAdded_long_int3() {
638 Duration test = new Duration(123L);
639 Duration result = test.withDurationAdded(8000L, -1);
640 assertEquals((123L - 8000L), result.getMillis());
641 }
642
643 public void testWithDurationAdded_long_int4() {
644 Duration test = new Duration(123L);
645 Duration result = test.withDurationAdded(0L, 1);
646 assertSame(test, result);
647 }
648
649 public void testWithDurationAdded_long_int5() {
650 Duration test = new Duration(123L);
651 Duration result = test.withDurationAdded(8000L, 0);
652 assertSame(test, result);
653 }
654
655 //-----------------------------------------------------------------------
656 public void testPlus_long1() {
657 Duration test = new Duration(123L);
658 Duration result = test.plus(8000L);
659 assertEquals(8123L, result.getMillis());
660 }
661
662 public void testPlus_long2() {
663 Duration test = new Duration(123L);
664 Duration result = test.plus(0L);
665 assertSame(test, result);
666 }
667
668 //-----------------------------------------------------------------------
669 public void testMinus_long1() {
670 Duration test = new Duration(123L);
671 Duration result = test.minus(8000L);
672 assertEquals(123L - 8000L, result.getMillis());
673 }
674
675 public void testMinus_long2() {
676 Duration test = new Duration(123L);
677 Duration result = test.minus(0L);
678 assertSame(test, result);
679 }
680
681 //-----------------------------------------------------------------------
682 public void testWithDurationAdded_RD_int1() {
683 Duration test = new Duration(123L);
684 Duration result = test.withDurationAdded(new Duration(8000L), 1);
685 assertEquals(8123L, result.getMillis());
686 }
687
688 public void testWithDurationAdded_RD_int2() {
689 Duration test = new Duration(123L);
690 Duration result = test.withDurationAdded(new Duration(8000L), 2);
691 assertEquals(16123L, result.getMillis());
692 }
693
694 public void testWithDurationAdded_RD_int3() {
695 Duration test = new Duration(123L);
696 Duration result = test.withDurationAdded(new Duration(8000L), -1);
697 assertEquals((123L - 8000L), result.getMillis());
698 }
699
700 public void testWithDurationAdded_RD_int4() {
701 Duration test = new Duration(123L);
702 Duration result = test.withDurationAdded(new Duration(0L), 1);
703 assertSame(test, result);
704 }
705
706 public void testWithDurationAdded_RD_int5() {
707 Duration test = new Duration(123L);
708 Duration result = test.withDurationAdded(new Duration(8000L), 0);
709 assertSame(test, result);
710 }
711
712 public void testWithDurationAdded_RD_int6() {
713 Duration test = new Duration(123L);
714 Duration result = test.withDurationAdded(null, 0);
715 assertSame(test, result);
716 }
717
718 //-----------------------------------------------------------------------
719 public void testPlus_RD1() {
720 Duration test = new Duration(123L);
721 Duration result = test.plus(new Duration(8000L));
722 assertEquals(8123L, result.getMillis());
723 }
724
725 public void testPlus_RD2() {
726 Duration test = new Duration(123L);
727 Duration result = test.plus(new Duration(0L));
728 assertSame(test, result);
729 }
730
731 public void testPlus_RD3() {
732 Duration test = new Duration(123L);
733 Duration result = test.plus(null);
734 assertSame(test, result);
735 }
736
737 //-----------------------------------------------------------------------
738 public void testMinus_RD1() {
739 Duration test = new Duration(123L);
740 Duration result = test.minus(new Duration(8000L));
741 assertEquals(123L - 8000L, result.getMillis());
742 }
743
744 public void testMinus_RD2() {
745 Duration test = new Duration(123L);
746 Duration result = test.minus(new Duration(0L));
747 assertSame(test, result);
748 }
749
750 public void testMinus_RD3() {
751 Duration test = new Duration(123L);
752 Duration result = test.minus(null);
753 assertSame(test, result);
754 }
755
756 //-----------------------------------------------------------------------
757 public void testMutableDuration() {
758 // no MutableDuration, so...
759 MockMutableDuration test = new MockMutableDuration(123L);
760 assertEquals(123L, test.getMillis());
761
762 test.setMillis(2345L);
763 assertEquals(2345L, test.getMillis());
764 }
765
766 static class MockMutableDuration extends BaseDuration {
767 public MockMutableDuration(long duration) {
768 super(duration);
769 }
770 public void setMillis(long duration) {
771 super.setMillis(duration);
772 }
773 }
774
775 }