1 16 package org.joda.time; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.util.Locale ; 23 import java.util.TimeZone ; 24 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 28 import org.joda.time.base.AbstractDuration; 29 import org.joda.time.base.BaseDuration; 30 import org.joda.time.chrono.ISOChronology; 31 32 37 public class TestDuration_Basics extends TestCase { 38 41 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 42 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 43 44 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 45 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 46 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 47 366 + 365; 48 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 49 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 50 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 51 366 + 365 + 365; 52 53 private long TEST_TIME_NOW = 55 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 56 57 private long TEST_TIME1 = 59 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY 60 + 12L * DateTimeConstants.MILLIS_PER_HOUR 61 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 62 63 private long TEST_TIME2 = 65 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 66 + 14L * DateTimeConstants.MILLIS_PER_HOUR 67 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 68 69 private DateTimeZone originalDateTimeZone = null; 70 private TimeZone originalTimeZone = null; 71 private Locale originalLocale = null; 72 73 public static void main(String [] args) { 74 junit.textui.TestRunner.run(suite()); 75 } 76 77 public static TestSuite suite() { 78 return new TestSuite(TestDuration_Basics.class); 79 } 80 81 public TestDuration_Basics(String name) { 82 super(name); 83 } 84 85 protected void setUp() throws Exception { 86 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 87 originalDateTimeZone = DateTimeZone.getDefault(); 88 originalTimeZone = TimeZone.getDefault(); 89 originalLocale = Locale.getDefault(); 90 DateTimeZone.setDefault(LONDON); 91 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 92 Locale.setDefault(Locale.UK); 93 } 94 95 protected void tearDown() throws Exception { 96 DateTimeUtils.setCurrentMillisSystem(); 97 DateTimeZone.setDefault(originalDateTimeZone); 98 TimeZone.setDefault(originalTimeZone); 99 Locale.setDefault(originalLocale); 100 originalDateTimeZone = null; 101 originalTimeZone = null; 102 originalLocale = null; 103 } 104 105 public void testTest() { 107 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString()); 108 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString()); 109 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString()); 110 } 111 112 public void testGetMillis() { 114 Duration test = new Duration(0L); 115 assertEquals(0, test.getMillis()); 116 117 test = new Duration(1234567890L); 118 assertEquals(1234567890L, test.getMillis()); 119 } 120 121 public void testEqualsHashCode() { 122 Duration test1 = new Duration(123L); 123 Duration test2 = new Duration(123L); 124 assertEquals(true, test1.equals(test2)); 125 assertEquals(true, test2.equals(test1)); 126 assertEquals(true, test1.equals(test1)); 127 assertEquals(true, test2.equals(test2)); 128 assertEquals(true, test1.hashCode() == test2.hashCode()); 129 assertEquals(true, test1.hashCode() == test1.hashCode()); 130 assertEquals(true, test2.hashCode() == test2.hashCode()); 131 132 Duration test3 = new Duration(321L); 133 assertEquals(false, test1.equals(test3)); 134 assertEquals(false, test2.equals(test3)); 135 assertEquals(false, test3.equals(test1)); 136 assertEquals(false, test3.equals(test2)); 137 assertEquals(false, test1.hashCode() == test3.hashCode()); 138 assertEquals(false, test2.hashCode() == test3.hashCode()); 139 140 assertEquals(false, test1.equals("Hello")); 141 assertEquals(true, test1.equals(new MockDuration(123L))); 142 } 143 144 class MockDuration extends AbstractDuration { 145 private final long iValue; 146 public MockDuration(long value) { 147 super(); 148 iValue = value; 149 } 150 public long getMillis() { 151 return iValue; 152 } 153 } 154 155 public void testCompareTo() { 156 Duration test1 = new Duration(123L); 157 Duration test1a = new Duration(123L); 158 assertEquals(0, test1.compareTo(test1a)); 159 assertEquals(0, test1a.compareTo(test1)); 160 assertEquals(0, test1.compareTo(test1)); 161 assertEquals(0, test1a.compareTo(test1a)); 162 163 Duration test2 = new Duration(321L); 164 assertEquals(-1, test1.compareTo(test2)); 165 assertEquals(+1, test2.compareTo(test1)); 166 167 assertEquals(+1, test2.compareTo(new MockDuration(123L))); 168 assertEquals(0, test1.compareTo(new MockDuration(123L))); 169 170 try { 171 test1.compareTo(null); 172 fail(); 173 } catch (NullPointerException ex) {} 174 try { 175 test1.compareTo(new Long (123L)); 176 fail(); 177 } catch (ClassCastException ex) {} 178 } 179 180 public void testIsEqual() { 181 Duration test1 = new Duration(123L); 182 Duration test1a = new Duration(123L); 183 assertEquals(true, test1.isEqual(test1a)); 184 assertEquals(true, test1a.isEqual(test1)); 185 assertEquals(true, test1.isEqual(test1)); 186 assertEquals(true, test1a.isEqual(test1a)); 187 188 Duration test2 = new Duration(321L); 189 assertEquals(false, test1.isEqual(test2)); 190 assertEquals(false, test2.isEqual(test1)); 191 192 assertEquals(false, test2.isEqual(new MockDuration(123L))); 193 assertEquals(true, test1.isEqual(new MockDuration(123L))); 194 assertEquals(false, test1.isEqual(null)); 195 assertEquals(true, new Duration(0L).isEqual(null)); 196 } 197 198 public void testIsBefore() { 199 Duration test1 = new Duration(123L); 200 Duration test1a = new Duration(123L); 201 assertEquals(false, test1.isShorterThan(test1a)); 202 assertEquals(false, test1a.isShorterThan(test1)); 203 assertEquals(false, test1.isShorterThan(test1)); 204 assertEquals(false, test1a.isShorterThan(test1a)); 205 206 Duration test2 = new Duration(321L); 207 assertEquals(true, test1.isShorterThan(test2)); 208 assertEquals(false, test2.isShorterThan(test1)); 209 210 assertEquals(false, test2.isShorterThan(new MockDuration(123L))); 211 assertEquals(false, test1.isShorterThan(new MockDuration(123L))); 212 assertEquals(false, test1.isShorterThan(null)); 213 assertEquals(false, new Duration(0L).isShorterThan(null)); 214 } 215 216 public void testIsAfter() { 217 Duration test1 = new Duration(123L); 218 Duration test1a = new Duration(123L); 219 assertEquals(false, test1.isLongerThan(test1a)); 220 assertEquals(false, test1a.isLongerThan(test1)); 221 assertEquals(false, test1.isLongerThan(test1)); 222 assertEquals(false, test1a.isLongerThan(test1a)); 223 224 Duration test2 = new Duration(321L); 225 assertEquals(false, test1.isLongerThan(test2)); 226 assertEquals(true, test2.isLongerThan(test1)); 227 228 assertEquals(true, test2.isLongerThan(new MockDuration(123L))); 229 assertEquals(false, test1.isLongerThan(new MockDuration(123L))); 230 assertEquals(true, test1.isLongerThan(null)); 231 assertEquals(false, new Duration(0L).isLongerThan(null)); 232 } 233 234 public void testSerialization() throws Exception { 236 Duration test = new Duration(123L); 237 238 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 239 ObjectOutputStream oos = new ObjectOutputStream (baos); 240 oos.writeObject(test); 241 byte[] bytes = baos.toByteArray(); 242 oos.close(); 243 244 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 245 ObjectInputStream ois = new ObjectInputStream (bais); 246 Duration result = (Duration) ois.readObject(); 247 ois.close(); 248 249 assertEquals(test, result); 250 } 251 252 public void testToString() { 254 long length = (365L + 2L * 30L + 3L * 7L + 4L) * DateTimeConstants.MILLIS_PER_DAY + 255 5L * DateTimeConstants.MILLIS_PER_HOUR + 256 6L * DateTimeConstants.MILLIS_PER_MINUTE + 257 7L * DateTimeConstants.MILLIS_PER_SECOND + 845L; 258 Duration test = new Duration(length); 259 assertEquals("PT" + (length / 1000) + "." + (length % 1000) + "S", test.toString()); 260 261 test = new Duration(0L); 262 assertEquals("PT0S", test.toString()); 263 264 test = new Duration(12345L); 265 assertEquals("PT12.345S", test.toString()); 266 267 test = new Duration(-12345L); 268 assertEquals("PT-12.345S", test.toString()); 269 } 270 271 public void testToDuration1() { 273 Duration test = new Duration(123L); 274 Duration result = test.toDuration(); 275 assertSame(test, result); 276 } 277 278 public void testToDuration2() { 279 MockDuration test = new MockDuration(123L); 280 Duration result = test.toDuration(); 281 assertNotSame(test, result); 282 assertEquals(test, result); 283 } 284 285 public void testToPeriod() { 287 long length = 288 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY + 289 5L * DateTimeConstants.MILLIS_PER_HOUR + 290 6L * DateTimeConstants.MILLIS_PER_MINUTE + 291 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L; 292 Duration test = new Duration(length); 293 Period result = test.toPeriod(); 294 assertEquals(new Period(test), result); 295 assertEquals(new Period(test.getMillis()), result); 296 } 297 298 public void testToPeriod_PeriodType() { 300 long length = 301 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY + 302 5L * DateTimeConstants.MILLIS_PER_HOUR + 303 6L * DateTimeConstants.MILLIS_PER_MINUTE + 304 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L; 305 Duration test = new Duration(length); 306 Period result = test.toPeriod(PeriodType.standard().withMillisRemoved()); 307 assertEquals(new Period(test, PeriodType.standard().withMillisRemoved()), result); 308 assertEquals(new Period(test.getMillis(), PeriodType.standard().withMillisRemoved()), result); 309 } 310 311 public void testToPeriod_Chronology() { 313 long length = 314 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY + 315 5L * DateTimeConstants.MILLIS_PER_HOUR + 316 6L * DateTimeConstants.MILLIS_PER_MINUTE + 317 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L; 318 Duration test = new Duration(length); 319 Period result = test.toPeriod(ISOChronology.getInstanceUTC()); 320 assertEquals(new Period(test, ISOChronology.getInstanceUTC()), result); 321 assertEquals(new Period(test.getMillis(), ISOChronology.getInstanceUTC()), result); 322 } 323 324 public void testToPeriod_PeriodType_Chronology() { 326 long length = 327 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY + 328 5L * DateTimeConstants.MILLIS_PER_HOUR + 329 6L * DateTimeConstants.MILLIS_PER_MINUTE + 330 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L; 331 Duration test = new Duration(length); 332 Period result = test.toPeriod(PeriodType.standard().withMillisRemoved(), ISOChronology.getInstanceUTC()); 333 assertEquals(new Period(test, PeriodType.standard().withMillisRemoved(), ISOChronology.getInstanceUTC()), result); 334 assertEquals(new Period(test.getMillis(), PeriodType.standard().withMillisRemoved(), ISOChronology.getInstanceUTC()), result); 335 } 336 337 public void testToPeriodFrom() { 339 long length = 340 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY + 341 5L * DateTimeConstants.MILLIS_PER_HOUR + 342 6L * DateTimeConstants.MILLIS_PER_MINUTE + 343 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L; 344 Duration test = new Duration(length); 345 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0); 346 Period result = test.toPeriodFrom(dt); 347 assertEquals(new Period(dt, test), result); 348 } 349 350 public void testToPeriodFrom_PeriodType() { 352 long length = 353 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY + 354 5L * DateTimeConstants.MILLIS_PER_HOUR + 355 6L * DateTimeConstants.MILLIS_PER_MINUTE + 356 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L; 357 Duration test = new Duration(length); 358 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0); 359 Period result = test.toPeriodFrom(dt, PeriodType.standard().withMillisRemoved()); 360 assertEquals(new Period(dt, test, PeriodType.standard().withMillisRemoved()), result); 361 } 362 363 public void testToPeriodTo() { 365 long length = 366 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY + 367 5L * DateTimeConstants.MILLIS_PER_HOUR + 368 6L * DateTimeConstants.MILLIS_PER_MINUTE + 369 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L; 370 Duration test = new Duration(length); 371 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0); 372 Period result = test.toPeriodTo(dt); 373 assertEquals(new Period(test, dt), result); 374 } 375 376 public void testToPeriodTo_PeriodType() { 378 long length = 379 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY + 380 5L * DateTimeConstants.MILLIS_PER_HOUR + 381 6L * DateTimeConstants.MILLIS_PER_MINUTE + 382 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L; 383 Duration test = new Duration(length); 384 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0); 385 Period result = test.toPeriodTo(dt, PeriodType.standard().withMillisRemoved()); 386 assertEquals(new Period(test, dt, PeriodType.standard().withMillisRemoved()), result); 387 } 388 389 public void testToIntervalFrom() { 391 long length = 392 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY + 393 5L * DateTimeConstants.MILLIS_PER_HOUR + 394 6L * DateTimeConstants.MILLIS_PER_MINUTE + 395 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L; 396 Duration test = new Duration(length); 397 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0); 398 Interval result = test.toIntervalFrom(dt); 399 assertEquals(new Interval(dt, test), result); 400 } 401 402 public void testToIntervalTo() { 404 long length = 405 (4L + (3L * 7L) + (2L * 30L) + 365L) * DateTimeConstants.MILLIS_PER_DAY + 406 5L * DateTimeConstants.MILLIS_PER_HOUR + 407 6L * DateTimeConstants.MILLIS_PER_MINUTE + 408 7L * DateTimeConstants.MILLIS_PER_SECOND + 8L; 409 Duration test = new Duration(length); 410 DateTime dt = new DateTime(2004, 6, 9, 0, 0, 0, 0); 411 Interval result = test.toIntervalTo(dt); 412 assertEquals(new Interval(test, dt), result); 413 } 414 415 public void testWithMillis1() { 417 Duration test = new Duration(123L); 418 Duration result = test.withMillis(123L); 419 assertSame(test, result); 420 } 421 422 public void testWithMillis2() { 423 Duration test = new Duration(123L); 424 Duration result = test.withMillis(1234567890L); 425 assertEquals(1234567890L, result.getMillis()); 426 } 427 428 public void testWithDurationAdded_long_int1() { 430 Duration test = new Duration(123L); 431 Duration result = test.withDurationAdded(8000L, 1); 432 assertEquals(8123L, result.getMillis()); 433 } 434 435 public void testWithDurationAdded_long_int2() { 436 Duration test = new Duration(123L); 437 Duration result = test.withDurationAdded(8000L, 2); 438 assertEquals(16123L, result.getMillis()); 439 } 440 441 public void testWithDurationAdded_long_int3() { 442 Duration test = new Duration(123L); 443 Duration result = test.withDurationAdded(8000L, -1); 444 assertEquals((123L - 8000L), result.getMillis()); 445 } 446 447 public void testWithDurationAdded_long_int4() { 448 Duration test = new Duration(123L); 449 Duration result = test.withDurationAdded(0L, 1); 450 assertSame(test, result); 451 } 452 453 public void testWithDurationAdded_long_int5() { 454 Duration test = new Duration(123L); 455 Duration result = test.withDurationAdded(8000L, 0); 456 assertSame(test, result); 457 } 458 459 public void testPlus_long1() { 461 Duration test = new Duration(123L); 462 Duration result = test.plus(8000L); 463 assertEquals(8123L, result.getMillis()); 464 } 465 466 public void testPlus_long2() { 467 Duration test = new Duration(123L); 468 Duration result = test.plus(0L); 469 assertSame(test, result); 470 } 471 472 public void testMinus_long1() { 474 Duration test = new Duration(123L); 475 Duration result = test.minus(8000L); 476 assertEquals(123L - 8000L, result.getMillis()); 477 } 478 479 public void testMinus_long2() { 480 Duration test = new Duration(123L); 481 Duration result = test.minus(0L); 482 assertSame(test, result); 483 } 484 485 public void testWithDurationAdded_RD_int1() { 487 Duration test = new Duration(123L); 488 Duration result = test.withDurationAdded(new Duration(8000L), 1); 489 assertEquals(8123L, result.getMillis()); 490 } 491 492 public void testWithDurationAdded_RD_int2() { 493 Duration test = new Duration(123L); 494 Duration result = test.withDurationAdded(new Duration(8000L), 2); 495 assertEquals(16123L, result.getMillis()); 496 } 497 498 public void testWithDurationAdded_RD_int3() { 499 Duration test = new Duration(123L); 500 Duration result = test.withDurationAdded(new Duration(8000L), -1); 501 assertEquals((123L - 8000L), result.getMillis()); 502 } 503 504 public void testWithDurationAdded_RD_int4() { 505 Duration test = new Duration(123L); 506 Duration result = test.withDurationAdded(new Duration(0L), 1); 507 assertSame(test, result); 508 } 509 510 public void testWithDurationAdded_RD_int5() { 511 Duration test = new Duration(123L); 512 Duration result = test.withDurationAdded(new Duration(8000L), 0); 513 assertSame(test, result); 514 } 515 516 public void testWithDurationAdded_RD_int6() { 517 Duration test = new Duration(123L); 518 Duration result = test.withDurationAdded(null, 0); 519 assertSame(test, result); 520 } 521 522 public void testPlus_RD1() { 524 Duration test = new Duration(123L); 525 Duration result = test.plus(new Duration(8000L)); 526 assertEquals(8123L, result.getMillis()); 527 } 528 529 public void testPlus_RD2() { 530 Duration test = new Duration(123L); 531 Duration result = test.plus(new Duration(0L)); 532 assertSame(test, result); 533 } 534 535 public void testPlus_RD3() { 536 Duration test = new Duration(123L); 537 Duration result = test.plus(null); 538 assertSame(test, result); 539 } 540 541 public void testMinus_RD1() { 543 Duration test = new Duration(123L); 544 Duration result = test.minus(new Duration(8000L)); 545 assertEquals(123L - 8000L, result.getMillis()); 546 } 547 548 public void testMinus_RD2() { 549 Duration test = new Duration(123L); 550 Duration result = test.minus(new Duration(0L)); 551 assertSame(test, result); 552 } 553 554 public void testMinus_RD3() { 555 Duration test = new Duration(123L); 556 Duration result = test.minus(null); 557 assertSame(test, result); 558 } 559 560 public void testMutableDuration() { 562 MockMutableDuration test = new MockMutableDuration(123L); 564 assertEquals(123L, test.getMillis()); 565 566 test.setMillis(2345L); 567 assertEquals(2345L, test.getMillis()); 568 } 569 570 static class MockMutableDuration extends BaseDuration { 571 public MockMutableDuration(long duration) { 572 super(duration); 573 } 574 public void setMillis(long duration) { 575 super.setMillis(duration); 576 } 577 } 578 579 } 580 | Popular Tags |