1 16 package org.joda.time; 17 18 import java.util.Locale ; 19 import java.util.TimeZone ; 20 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 import org.joda.time.chrono.BuddhistChronology; 25 import org.joda.time.chrono.CopticChronology; 26 import org.joda.time.chrono.GJChronology; 27 import org.joda.time.chrono.ISOChronology; 28 import org.joda.time.convert.ConverterManager; 29 import org.joda.time.convert.IntervalConverter; 30 31 36 public class TestInterval_Constructors extends TestCase { 37 40 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 41 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 42 43 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 44 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 45 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 46 366 + 365; 47 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 48 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 49 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 50 366 + 365 + 365; 51 52 private long TEST_TIME_NOW = 54 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 55 56 68 private DateTimeZone originalDateTimeZone = null; 69 private TimeZone originalTimeZone = null; 70 private Locale originalLocale = null; 71 72 public static void main(String [] args) { 73 junit.textui.TestRunner.run(suite()); 74 } 75 76 public static TestSuite suite() { 77 return new TestSuite(TestInterval_Constructors.class); 78 } 79 80 public TestInterval_Constructors(String name) { 81 super(name); 82 } 83 84 protected void setUp() throws Exception { 85 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 86 originalDateTimeZone = DateTimeZone.getDefault(); 87 originalTimeZone = TimeZone.getDefault(); 88 originalLocale = Locale.getDefault(); 89 DateTimeZone.setDefault(PARIS); 90 TimeZone.setDefault(PARIS.toTimeZone()); 91 Locale.setDefault(Locale.FRANCE); 92 } 93 94 protected void tearDown() throws Exception { 95 DateTimeUtils.setCurrentMillisSystem(); 96 DateTimeZone.setDefault(originalDateTimeZone); 97 TimeZone.setDefault(originalTimeZone); 98 Locale.setDefault(originalLocale); 99 originalDateTimeZone = null; 100 originalTimeZone = null; 101 originalLocale = null; 102 } 103 104 public void testConstructor_long_long1() throws Throwable { 106 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 107 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 108 Interval test = new Interval(dt1.getMillis(), dt2.getMillis()); 109 assertEquals(dt1.getMillis(), test.getStartMillis()); 110 assertEquals(dt2.getMillis(), test.getEndMillis()); 111 assertEquals(ISOChronology.getInstance(), test.getChronology()); 112 } 113 114 public void testConstructor_long_long2() throws Throwable { 115 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 116 Interval test = new Interval(dt1.getMillis(), dt1.getMillis()); 117 assertEquals(dt1.getMillis(), test.getStartMillis()); 118 assertEquals(dt1.getMillis(), test.getEndMillis()); 119 assertEquals(ISOChronology.getInstance(), test.getChronology()); 120 } 121 122 public void testConstructor_long_long3() throws Throwable { 123 DateTime dt1 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 124 DateTime dt2 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 125 try { 126 new Interval(dt1.getMillis(), dt2.getMillis()); 127 fail(); 128 } catch (IllegalArgumentException ex) {} 129 } 130 131 public void testConstructor_long_long_Chronology1() throws Throwable { 133 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 134 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 135 Interval test = new Interval(dt1.getMillis(), dt2.getMillis(), GJChronology.getInstance()); 136 assertEquals(dt1.getMillis(), test.getStartMillis()); 137 assertEquals(dt2.getMillis(), test.getEndMillis()); 138 assertEquals(GJChronology.getInstance(), test.getChronology()); 139 } 140 141 public void testConstructor_long_long_Chronology2() throws Throwable { 142 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 143 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 144 Interval test = new Interval(dt1.getMillis(), dt2.getMillis(), null); 145 assertEquals(dt1.getMillis(), test.getStartMillis()); 146 assertEquals(dt2.getMillis(), test.getEndMillis()); 147 assertEquals(ISOChronology.getInstance(), test.getChronology()); 148 } 149 150 public void testConstructor_RI_RI1() throws Throwable { 152 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 153 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 154 Interval test = new Interval(dt1, dt2); 155 assertEquals(dt1.getMillis(), test.getStartMillis()); 156 assertEquals(dt2.getMillis(), test.getEndMillis()); 157 } 158 159 public void testConstructor_RI_RI2() throws Throwable { 160 Instant dt1 = new Instant(new DateTime(2004, 6, 9, 0, 0, 0, 0)); 161 Instant dt2 = new Instant(new DateTime(2005, 7, 10, 1, 1, 1, 1)); 162 Interval test = new Interval(dt1, dt2); 163 assertEquals(dt1.getMillis(), test.getStartMillis()); 164 assertEquals(dt2.getMillis(), test.getEndMillis()); 165 } 166 167 public void testConstructor_RI_RI3() throws Throwable { 168 Interval test = new Interval((ReadableInstant) null, (ReadableInstant) null); 169 assertEquals(TEST_TIME_NOW, test.getStartMillis()); 170 assertEquals(TEST_TIME_NOW, test.getEndMillis()); 171 } 172 173 public void testConstructor_RI_RI4() throws Throwable { 174 DateTime dt1 = new DateTime(2000, 6, 9, 0, 0, 0, 0); 175 Interval test = new Interval(dt1, (ReadableInstant) null); 176 assertEquals(dt1.getMillis(), test.getStartMillis()); 177 assertEquals(TEST_TIME_NOW, test.getEndMillis()); 178 } 179 180 public void testConstructor_RI_RI5() throws Throwable { 181 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 182 Interval test = new Interval((ReadableInstant) null, dt2); 183 assertEquals(TEST_TIME_NOW, test.getStartMillis()); 184 assertEquals(dt2.getMillis(), test.getEndMillis()); 185 } 186 187 public void testConstructor_RI_RI6() throws Throwable { 188 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 189 Interval test = new Interval(dt1, dt1); 190 assertEquals(dt1.getMillis(), test.getStartMillis()); 191 assertEquals(dt1.getMillis(), test.getEndMillis()); 192 } 193 194 public void testConstructor_RI_RI7() throws Throwable { 195 DateTime dt1 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 196 DateTime dt2 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 197 try { 198 new Interval(dt1, dt2); 199 fail(); 200 } catch (IllegalArgumentException ex) {} 201 } 202 203 public void testConstructor_RI_RI_chronoStart() throws Throwable { 204 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0, GJChronology.getInstance()); 205 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 206 Interval test = new Interval(dt1, dt2); 207 assertEquals(dt1.getMillis(), test.getStartMillis()); 208 assertEquals(dt2.getMillis(), test.getEndMillis()); 209 assertEquals(GJChronology.getInstance(), test.getChronology()); 210 } 211 212 public void testConstructor_RI_RI_chronoEnd() throws Throwable { 213 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 214 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1, GJChronology.getInstance()); 215 Interval test = new Interval(dt1, dt2); 216 assertEquals(dt1.getMillis(), test.getStartMillis()); 217 assertEquals(dt2.getMillis(), test.getEndMillis()); 218 assertEquals(ISOChronology.getInstance(), test.getChronology()); 219 } 220 221 public void testConstructor_RI_RI_zones() throws Throwable { 222 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0, LONDON); 223 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1, PARIS); 224 Interval test = new Interval(dt1, dt2); 225 assertEquals(dt1.getMillis(), test.getStartMillis()); 226 assertEquals(dt2.getMillis(), test.getEndMillis()); 227 assertEquals(ISOChronology.getInstance(LONDON), test.getChronology()); 228 } 229 230 public void testConstructor_RI_RI_instant() throws Throwable { 231 Instant dt1 = new Instant(12345678L); 232 Instant dt2 = new Instant(22345678L); 233 Interval test = new Interval(dt1, dt2); 234 assertEquals(12345678L, test.getStartMillis()); 235 assertEquals(22345678L, test.getEndMillis()); 236 assertEquals(ISOChronology.getInstanceUTC(), test.getChronology()); 237 } 238 239 public void testConstructor_RI_RP1() throws Throwable { 241 DateTime dt = new DateTime(TEST_TIME_NOW); 242 Period dur = new Period(0, 6, 0, 0, 1, 0, 0, 0); 243 long result = TEST_TIME_NOW; 244 result = ISOChronology.getInstance().months().add(result, 6); 245 result = ISOChronology.getInstance().hours().add(result, 1); 246 247 Interval test = new Interval(dt, dur); 248 assertEquals(dt.getMillis(), test.getStartMillis()); 249 assertEquals(result, test.getEndMillis()); 250 } 251 252 public void testConstructor_RI_RP2() throws Throwable { 253 Instant dt = new Instant(new DateTime(TEST_TIME_NOW)); 254 Period dur = new Period(0, 6, 0, 3, 1, 0, 0, 0); 255 long result = TEST_TIME_NOW; 256 result = ISOChronology.getInstanceUTC().months().add(result, 6); 257 result = ISOChronology.getInstanceUTC().days().add(result, 3); 258 result = ISOChronology.getInstanceUTC().hours().add(result, 1); 259 260 Interval test = new Interval(dt, dur); 261 assertEquals(dt.getMillis(), test.getStartMillis()); 262 assertEquals(result, test.getEndMillis()); 263 } 264 265 public void testConstructor_RI_RP3() throws Throwable { 266 DateTime dt = new DateTime(TEST_TIME_NOW, CopticChronology.getInstanceUTC()); 267 Period dur = new Period(0, 6, 0, 3, 1, 0, 0, 0, PeriodType.standard()); 268 long result = TEST_TIME_NOW; 269 result = CopticChronology.getInstanceUTC().months().add(result, 6); 270 result = CopticChronology.getInstanceUTC().days().add(result, 3); 271 result = CopticChronology.getInstanceUTC().hours().add(result, 1); 272 273 Interval test = new Interval(dt, dur); 274 assertEquals(dt.getMillis(), test.getStartMillis()); 275 assertEquals(result, test.getEndMillis()); 276 } 277 278 public void testConstructor_RI_RP4() throws Throwable { 279 DateTime dt = new DateTime(TEST_TIME_NOW); 280 Period dur = new Period(1 * DateTimeConstants.MILLIS_PER_HOUR + 23L); 281 long result = TEST_TIME_NOW; 282 result = ISOChronology.getInstance().hours().add(result, 1); 283 result = ISOChronology.getInstance().millis().add(result, 23); 284 285 Interval test = new Interval(dt, dur); 286 assertEquals(dt.getMillis(), test.getStartMillis()); 287 assertEquals(result, test.getEndMillis()); 288 } 289 290 public void testConstructor_RI_RP5() throws Throwable { 291 Interval test = new Interval((ReadableInstant) null, (ReadablePeriod) null); 292 assertEquals(TEST_TIME_NOW, test.getStartMillis()); 293 assertEquals(TEST_TIME_NOW, test.getEndMillis()); 294 } 295 296 public void testConstructor_RI_RP6() throws Throwable { 297 DateTime dt = new DateTime(TEST_TIME_NOW); 298 Interval test = new Interval(dt, (ReadablePeriod) null); 299 assertEquals(dt.getMillis(), test.getStartMillis()); 300 assertEquals(dt.getMillis(), test.getEndMillis()); 301 } 302 303 public void testConstructor_RI_RP7() throws Throwable { 304 Period dur = new Period(0, 6, 0, 0, 1, 0, 0, 0); 305 long result = TEST_TIME_NOW; 306 result = ISOChronology.getInstance().monthOfYear().add(result, 6); 307 result = ISOChronology.getInstance().hourOfDay().add(result, 1); 308 309 Interval test = new Interval((ReadableInstant) null, dur); 310 assertEquals(TEST_TIME_NOW, test.getStartMillis()); 311 assertEquals(result, test.getEndMillis()); 312 } 313 314 public void testConstructor_RI_RP8() throws Throwable { 315 DateTime dt = new DateTime(TEST_TIME_NOW); 316 Period dur = new Period(0, 0, 0, 0, 0, 0, 0, -1); 317 try { 318 new Interval(dt, dur); 319 fail(); 320 } catch (IllegalArgumentException ex) {} 321 } 322 323 public void testConstructor_RP_RI1() throws Throwable { 325 DateTime dt = new DateTime(TEST_TIME_NOW); 326 Period dur = new Period(0, 6, 0, 0, 1, 0, 0, 0); 327 long result = TEST_TIME_NOW; 328 result = ISOChronology.getInstance().months().add(result, -6); 329 result = ISOChronology.getInstance().hours().add(result, -1); 330 331 Interval test = new Interval(dur, dt); 332 assertEquals(result, test.getStartMillis()); 333 assertEquals(dt.getMillis(), test.getEndMillis()); 334 } 335 336 public void testConstructor_RP_RI2() throws Throwable { 337 Instant dt = new Instant(new DateTime(TEST_TIME_NOW)); 338 Period dur = new Period(0, 6, 0, 3, 1, 0, 0, 0); 339 long result = TEST_TIME_NOW; 340 result = ISOChronology.getInstanceUTC().months().add(result, -6); 341 result = ISOChronology.getInstanceUTC().days().add(result, -3); 342 result = ISOChronology.getInstanceUTC().hours().add(result, -1); 343 344 Interval test = new Interval(dur, dt); 345 assertEquals(result, test.getStartMillis()); 346 assertEquals(dt.getMillis(), test.getEndMillis()); 347 } 348 349 public void testConstructor_RP_RI3() throws Throwable { 350 DateTime dt = new DateTime(TEST_TIME_NOW, CopticChronology.getInstanceUTC()); 351 Period dur = new Period(0, 6, 0, 3, 1, 0, 0, 0, PeriodType.standard()); 352 long result = TEST_TIME_NOW; 353 result = CopticChronology.getInstanceUTC().months().add(result, -6); 354 result = CopticChronology.getInstanceUTC().days().add(result, -3); 355 result = CopticChronology.getInstanceUTC().hours().add(result, -1); 356 357 Interval test = new Interval(dur, dt); 358 assertEquals(result, test.getStartMillis()); 359 assertEquals(dt.getMillis(), test.getEndMillis()); 360 } 361 362 public void testConstructor_RP_RI4() throws Throwable { 363 DateTime dt = new DateTime(TEST_TIME_NOW); 364 Period dur = new Period(1 * DateTimeConstants.MILLIS_PER_HOUR + 23L); 365 long result = TEST_TIME_NOW; 366 result = ISOChronology.getInstance().hours().add(result, -1); 367 result = ISOChronology.getInstance().millis().add(result, -23); 368 369 Interval test = new Interval(dur, dt); 370 assertEquals(result, test.getStartMillis()); 371 assertEquals(dt.getMillis(), test.getEndMillis()); 372 } 373 374 public void testConstructor_RP_RI5() throws Throwable { 375 Interval test = new Interval((ReadablePeriod) null, (ReadableInstant) null); 376 assertEquals(TEST_TIME_NOW, test.getStartMillis()); 377 assertEquals(TEST_TIME_NOW, test.getEndMillis()); 378 } 379 380 public void testConstructor_RP_RI6() throws Throwable { 381 DateTime dt = new DateTime(TEST_TIME_NOW); 382 Interval test = new Interval((ReadablePeriod) null, dt); 383 assertEquals(dt.getMillis(), test.getStartMillis()); 384 assertEquals(dt.getMillis(), test.getEndMillis()); 385 } 386 387 public void testConstructor_RP_RI7() throws Throwable { 388 Period dur = new Period(0, 6, 0, 0, 1, 0, 0, 0); 389 long result = TEST_TIME_NOW; 390 result = ISOChronology.getInstance().monthOfYear().add(result, -6); 391 result = ISOChronology.getInstance().hourOfDay().add(result, -1); 392 393 Interval test = new Interval(dur, (ReadableInstant) null); 394 assertEquals(result, test.getStartMillis()); 395 assertEquals(TEST_TIME_NOW, test.getEndMillis()); 396 } 397 398 public void testConstructor_RP_RI8() throws Throwable { 399 DateTime dt = new DateTime(TEST_TIME_NOW); 400 Period dur = new Period(0, 0, 0, 0, 0, 0, 0, -1); 401 try { 402 new Interval(dur, dt); 403 fail(); 404 } catch (IllegalArgumentException ex) {} 405 } 406 407 public void testConstructor_RI_RD1() throws Throwable { 409 long result = TEST_TIME_NOW; 410 result = ISOChronology.getInstance().months().add(result, 6); 411 result = ISOChronology.getInstance().hours().add(result, 1); 412 413 DateTime dt = new DateTime(TEST_TIME_NOW); 414 Duration dur = new Duration(result - TEST_TIME_NOW); 415 416 Interval test = new Interval(dt, dur); 417 assertEquals(dt.getMillis(), test.getStartMillis()); 418 assertEquals(result, test.getEndMillis()); 419 } 420 421 public void testConstructor_RI_RD2() throws Throwable { 422 Interval test = new Interval((ReadableInstant) null, (ReadableDuration) null); 423 assertEquals(TEST_TIME_NOW, test.getStartMillis()); 424 assertEquals(TEST_TIME_NOW, test.getEndMillis()); 425 } 426 427 public void testConstructor_RI_RD3() throws Throwable { 428 DateTime dt = new DateTime(TEST_TIME_NOW); 429 Interval test = new Interval(dt, (ReadableDuration) null); 430 assertEquals(dt.getMillis(), test.getStartMillis()); 431 assertEquals(dt.getMillis(), test.getEndMillis()); 432 } 433 434 public void testConstructor_RI_RD4() throws Throwable { 435 long result = TEST_TIME_NOW; 436 result = ISOChronology.getInstance().monthOfYear().add(result, 6); 437 result = ISOChronology.getInstance().hourOfDay().add(result, 1); 438 439 Duration dur = new Duration(result - TEST_TIME_NOW); 440 441 Interval test = new Interval((ReadableInstant) null, dur); 442 assertEquals(TEST_TIME_NOW, test.getStartMillis()); 443 assertEquals(result, test.getEndMillis()); 444 } 445 446 public void testConstructor_RI_RD5() throws Throwable { 447 DateTime dt = new DateTime(TEST_TIME_NOW); 448 Duration dur = new Duration(-1); 449 try { 450 new Interval(dt, dur); 451 fail(); 452 } catch (IllegalArgumentException ex) {} 453 } 454 455 public void testConstructor_RD_RI1() throws Throwable { 457 long result = TEST_TIME_NOW; 458 result = ISOChronology.getInstance().months().add(result, -6); 459 result = ISOChronology.getInstance().hours().add(result, -1); 460 461 DateTime dt = new DateTime(TEST_TIME_NOW); 462 Duration dur = new Duration(TEST_TIME_NOW - result); 463 464 Interval test = new Interval(dur, dt); 465 assertEquals(result, test.getStartMillis()); 466 assertEquals(dt.getMillis(), test.getEndMillis()); 467 } 468 469 public void testConstructor_RD_RI2() throws Throwable { 470 Interval test = new Interval((ReadableDuration) null, (ReadableInstant) null); 471 assertEquals(TEST_TIME_NOW, test.getStartMillis()); 472 assertEquals(TEST_TIME_NOW, test.getEndMillis()); 473 } 474 475 public void testConstructor_RD_RI3() throws Throwable { 476 DateTime dt = new DateTime(TEST_TIME_NOW); 477 Interval test = new Interval((ReadableDuration) null, dt); 478 assertEquals(dt.getMillis(), test.getStartMillis()); 479 assertEquals(dt.getMillis(), test.getEndMillis()); 480 } 481 482 public void testConstructor_RD_RI4() throws Throwable { 483 long result = TEST_TIME_NOW; 484 result = ISOChronology.getInstance().monthOfYear().add(result, -6); 485 result = ISOChronology.getInstance().hourOfDay().add(result, -1); 486 487 Duration dur = new Duration(TEST_TIME_NOW - result); 488 489 Interval test = new Interval(dur, (ReadableInstant) null); 490 assertEquals(result, test.getStartMillis()); 491 assertEquals(TEST_TIME_NOW, test.getEndMillis()); 492 } 493 494 public void testConstructor_RD_RI5() throws Throwable { 495 DateTime dt = new DateTime(TEST_TIME_NOW); 496 Duration dur = new Duration(-1); 497 try { 498 new Interval(dur, dt); 499 fail(); 500 } catch (IllegalArgumentException ex) {} 501 } 502 503 public void testConstructor_Object1() throws Throwable { 505 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 506 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 507 Interval test = new Interval(dt1.toString() + '/' + dt2.toString()); 508 assertEquals(dt1.getMillis(), test.getStartMillis()); 509 assertEquals(dt2.getMillis(), test.getEndMillis()); 510 } 511 512 public void testConstructor_Object2() throws Throwable { 513 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 514 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 515 Interval base = new Interval(dt1, dt2); 516 517 Interval test = new Interval(base); 518 assertEquals(dt1.getMillis(), test.getStartMillis()); 519 assertEquals(dt2.getMillis(), test.getEndMillis()); 520 } 521 522 public void testConstructor_Object3() throws Throwable { 523 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 524 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 525 MutableInterval base = new MutableInterval(dt1, dt2); 526 527 Interval test = new Interval(base); 528 assertEquals(dt1.getMillis(), test.getStartMillis()); 529 assertEquals(dt2.getMillis(), test.getEndMillis()); 530 } 531 532 public void testConstructor_Object4() throws Throwable { 533 MockInterval base = new MockInterval(); 534 Interval test = new Interval(base); 535 assertEquals(base.getStartMillis(), test.getStartMillis()); 536 assertEquals(base.getEndMillis(), test.getEndMillis()); 537 } 538 539 public void testConstructor_Object5() throws Throwable { 540 IntervalConverter oldConv = ConverterManager.getInstance().getIntervalConverter(""); 541 IntervalConverter conv = new IntervalConverter() { 542 public boolean isReadableInterval(Object object, Chronology chrono) { 543 return false; 544 } 545 public void setInto(ReadWritableInterval interval, Object object, Chronology chrono) { 546 interval.setChronology(chrono); 547 interval.setInterval(1234L, 5678L); 548 } 549 public Class getSupportedType() { 550 return String .class; 551 } 552 }; 553 try { 554 ConverterManager.getInstance().addIntervalConverter(conv); 555 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 556 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 557 Interval test = new Interval(dt1.toString() + '/' + dt2.toString()); 558 assertEquals(1234L, test.getStartMillis()); 559 assertEquals(5678L, test.getEndMillis()); 560 } finally { 561 ConverterManager.getInstance().addIntervalConverter(oldConv); 562 } 563 } 564 565 public void testConstructor_Object6() throws Throwable { 566 IntervalConverter oldConv = ConverterManager.getInstance().getIntervalConverter(new Interval(0L, 0L)); 567 IntervalConverter conv = new IntervalConverter() { 568 public boolean isReadableInterval(Object object, Chronology chrono) { 569 return false; 570 } 571 public void setInto(ReadWritableInterval interval, Object object, Chronology chrono) { 572 interval.setChronology(chrono); 573 interval.setInterval(1234L, 5678L); 574 } 575 public Class getSupportedType() { 576 return ReadableInterval.class; 577 } 578 }; 579 try { 580 ConverterManager.getInstance().addIntervalConverter(conv); 581 Interval base = new Interval(-1000L, 1000L); 582 Interval test = new Interval(base); 583 assertEquals(1234L, test.getStartMillis()); 584 assertEquals(5678L, test.getEndMillis()); 585 } finally { 586 ConverterManager.getInstance().addIntervalConverter(oldConv); 587 } 588 } 589 590 class MockInterval implements ReadableInterval { 591 public Chronology getChronology() { 592 return ISOChronology.getInstance(); 593 } 594 public long getStartMillis() { 595 return 1234L; 596 } 597 public DateTime getStart() { 598 return new DateTime(1234L); 599 } 600 public long getEndMillis() { 601 return 5678L; 602 } 603 public DateTime getEnd() { 604 return new DateTime(5678L); 605 } 606 public long toDurationMillis() { 607 return (5678L - 1234L); 608 } 609 public Duration toDuration() { 610 return new Duration(5678L - 1234L); 611 } 612 public boolean contains(long millisInstant) { 613 return false; 614 } 615 public boolean containsNow() { 616 return false; 617 } 618 public boolean contains(ReadableInstant instant) { 619 return false; 620 } 621 public boolean contains(ReadableInterval interval) { 622 return false; 623 } 624 public boolean overlaps(ReadableInterval interval) { 625 return false; 626 } 627 public boolean isBefore(ReadableInstant instant) { 628 return false; 629 } 630 public boolean isBefore(ReadableInterval interval) { 631 return false; 632 } 633 public boolean isAfter(ReadableInstant instant) { 634 return false; 635 } 636 public boolean isAfter(ReadableInterval interval) { 637 return false; 638 } 639 public Interval toInterval() { 640 return null; 641 } 642 public MutableInterval toMutableInterval() { 643 return null; 644 } 645 public Period toPeriod() { 646 return null; 647 } 648 public Period toPeriod(PeriodType type) { 649 return null; 650 } 651 } 652 653 public void testConstructor_Object_Chronology1() throws Throwable { 655 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 656 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 657 Interval base = new Interval(dt1, dt2); 658 659 Interval test = new Interval(base, BuddhistChronology.getInstance()); 660 assertEquals(dt1.getMillis(), test.getStartMillis()); 661 assertEquals(dt2.getMillis(), test.getEndMillis()); 662 assertEquals(BuddhistChronology.getInstance(), test.getChronology()); 663 } 664 665 public void testConstructor_Object_Chronology2() throws Throwable { 666 DateTime dt1 = new DateTime(2004, 6, 9, 0, 0, 0, 0); 667 DateTime dt2 = new DateTime(2005, 7, 10, 1, 1, 1, 1); 668 Interval base = new Interval(dt1, dt2); 669 670 Interval test = new Interval(base, null); 671 assertEquals(dt1.getMillis(), test.getStartMillis()); 672 assertEquals(dt2.getMillis(), test.getEndMillis()); 673 assertEquals(ISOChronology.getInstance(), test.getChronology()); 674 } 675 676 } 677 | Popular Tags |