1 16 package org.joda.time; 17 18 import java.util.Date ; 19 import java.util.Locale ; 20 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 import org.joda.time.chrono.GregorianChronology; 25 import org.joda.time.chrono.ISOChronology; 26 import org.joda.time.convert.ConverterManager; 27 import org.joda.time.convert.MockZeroNullIntegerConverter; 28 29 34 public class TestDateTime_Constructors extends TestCase { 35 38 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 39 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 40 41 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 42 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 43 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 44 366 + 365; 45 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 46 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 47 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 48 366 + 365 + 365; 49 50 private long TEST_TIME_NOW = 52 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 53 54 private long TEST_TIME1 = 56 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY 57 + 12L * DateTimeConstants.MILLIS_PER_HOUR 58 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 59 60 private long TEST_TIME2 = 62 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 63 + 14L * DateTimeConstants.MILLIS_PER_HOUR 64 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 65 66 private DateTimeZone zone = null; 67 private Locale locale = null; 68 69 public static void main(String [] args) { 70 junit.textui.TestRunner.run(suite()); 71 } 72 73 public static TestSuite suite() { 74 return new TestSuite(TestDateTime_Constructors.class); 75 } 76 77 public TestDateTime_Constructors(String name) { 78 super(name); 79 } 80 81 protected void setUp() throws Exception { 82 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 83 zone = DateTimeZone.getDefault(); 84 locale = Locale.getDefault(); 85 DateTimeZone.setDefault(LONDON); 86 java.util.TimeZone.setDefault(LONDON.toTimeZone()); 87 Locale.setDefault(Locale.UK); 88 } 89 90 protected void tearDown() throws Exception { 91 DateTimeUtils.setCurrentMillisSystem(); 92 DateTimeZone.setDefault(zone); 93 java.util.TimeZone.setDefault(zone.toTimeZone()); 94 Locale.setDefault(locale); 95 zone = null; 96 } 97 98 public void testTest() { 100 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString()); 101 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString()); 102 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString()); 103 } 104 105 109 public void testConstructor() throws Throwable { 110 DateTime test = new DateTime(); 111 assertEquals(ISOChronology.getInstance(), test.getChronology()); 112 assertEquals(TEST_TIME_NOW, test.getMillis()); 113 } 114 115 118 public void testConstructor_DateTimeZone() throws Throwable { 119 DateTime test = new DateTime(PARIS); 120 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 121 assertEquals(TEST_TIME_NOW, test.getMillis()); 122 } 123 124 127 public void testConstructor_nullDateTimeZone() throws Throwable { 128 DateTime test = new DateTime((DateTimeZone) null); 129 assertEquals(ISOChronology.getInstance(), test.getChronology()); 130 assertEquals(TEST_TIME_NOW, test.getMillis()); 131 } 132 133 136 public void testConstructor_Chronology() throws Throwable { 137 DateTime test = new DateTime(GregorianChronology.getInstance()); 138 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 139 assertEquals(TEST_TIME_NOW, test.getMillis()); 140 } 141 142 145 public void testConstructor_nullChronology() throws Throwable { 146 DateTime test = new DateTime((Chronology) null); 147 assertEquals(ISOChronology.getInstance(), test.getChronology()); 148 assertEquals(TEST_TIME_NOW, test.getMillis()); 149 } 150 151 155 public void testConstructor_long1() throws Throwable { 156 DateTime test = new DateTime(TEST_TIME1); 157 assertEquals(ISOChronology.getInstance(), test.getChronology()); 158 assertEquals(TEST_TIME1, test.getMillis()); 159 } 160 161 164 public void testConstructor_long2() throws Throwable { 165 DateTime test = new DateTime(TEST_TIME2); 166 assertEquals(ISOChronology.getInstance(), test.getChronology()); 167 assertEquals(TEST_TIME2, test.getMillis()); 168 } 169 170 173 public void testConstructor_long1_DateTimeZone() throws Throwable { 174 DateTime test = new DateTime(TEST_TIME1, PARIS); 175 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 176 assertEquals(TEST_TIME1, test.getMillis()); 177 } 178 179 182 public void testConstructor_long2_DateTimeZone() throws Throwable { 183 DateTime test = new DateTime(TEST_TIME2, PARIS); 184 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 185 assertEquals(TEST_TIME2, test.getMillis()); 186 } 187 188 191 public void testConstructor_long_nullDateTimeZone() throws Throwable { 192 DateTime test = new DateTime(TEST_TIME1, (DateTimeZone) null); 193 assertEquals(ISOChronology.getInstance(), test.getChronology()); 194 assertEquals(TEST_TIME1, test.getMillis()); 195 } 196 197 200 public void testConstructor_long1_Chronology() throws Throwable { 201 DateTime test = new DateTime(TEST_TIME1, GregorianChronology.getInstance()); 202 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 203 assertEquals(TEST_TIME1, test.getMillis()); 204 } 205 206 209 public void testConstructor_long2_Chronology() throws Throwable { 210 DateTime test = new DateTime(TEST_TIME2, GregorianChronology.getInstance()); 211 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 212 assertEquals(TEST_TIME2, test.getMillis()); 213 } 214 215 218 public void testConstructor_long_nullChronology() throws Throwable { 219 DateTime test = new DateTime(TEST_TIME1, (Chronology) null); 220 assertEquals(ISOChronology.getInstance(), test.getChronology()); 221 assertEquals(TEST_TIME1, test.getMillis()); 222 } 223 224 228 public void testConstructor_Object() throws Throwable { 229 Date date = new Date (TEST_TIME1); 230 DateTime test = new DateTime(date); 231 assertEquals(ISOChronology.getInstance(), test.getChronology()); 232 assertEquals(TEST_TIME1, test.getMillis()); 233 } 234 235 238 public void testConstructor_invalidObject() throws Throwable { 239 try { 240 new DateTime(new Object ()); 241 fail(); 242 } catch (IllegalArgumentException ex) {} 243 } 244 245 248 public void testConstructor_nullObject() throws Throwable { 249 DateTime test = new DateTime((Object ) null); 250 assertEquals(ISOChronology.getInstance(), test.getChronology()); 251 assertEquals(TEST_TIME_NOW, test.getMillis()); 252 } 253 254 257 public void testConstructor_badconverterObject() throws Throwable { 258 try { 259 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 260 DateTime test = new DateTime(new Integer (0)); 261 assertEquals(ISOChronology.getInstance(), test.getChronology()); 262 assertEquals(0L, test.getMillis()); 263 } finally { 264 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 265 } 266 } 267 268 public void testConstructor_ObjectString1() throws Throwable { 269 DateTime test = new DateTime("1972-12-03"); 270 assertEquals(ISOChronology.getInstance(), test.getChronology()); 271 assertEquals(1972, test.getYear()); 272 assertEquals(12, test.getMonthOfYear()); 273 assertEquals(3, test.getDayOfMonth()); 274 assertEquals(0, test.getHourOfDay()); 275 assertEquals(0, test.getMinuteOfHour()); 276 assertEquals(0, test.getSecondOfMinute()); 277 assertEquals(0, test.getMillisOfSecond()); 278 } 279 280 public void testConstructor_ObjectString2() throws Throwable { 281 DateTime test = new DateTime("2006-06-03T+14:00"); 282 assertEquals(ISOChronology.getInstance(), test.getChronology()); 283 assertEquals(2006, test.getYear()); 284 assertEquals(6, test.getMonthOfYear()); 285 assertEquals(2, test.getDayOfMonth()); assertEquals(11, test.getHourOfDay()); assertEquals(0, test.getMinuteOfHour()); 288 assertEquals(0, test.getSecondOfMinute()); 289 assertEquals(0, test.getMillisOfSecond()); 290 } 291 292 public void testConstructor_ObjectString3() throws Throwable { 293 DateTime test = new DateTime("1972-12-03T10:20:30.040"); 294 assertEquals(ISOChronology.getInstance(), test.getChronology()); 295 assertEquals(1972, test.getYear()); 296 assertEquals(12, test.getMonthOfYear()); 297 assertEquals(3, test.getDayOfMonth()); 298 assertEquals(10, test.getHourOfDay()); 299 assertEquals(20, test.getMinuteOfHour()); 300 assertEquals(30, test.getSecondOfMinute()); 301 assertEquals(40, test.getMillisOfSecond()); 302 } 303 304 public void testConstructor_ObjectString4() throws Throwable { 305 DateTime test = new DateTime("2006-06-03T10:20:30.040+14:00"); 306 assertEquals(ISOChronology.getInstance(), test.getChronology()); 307 assertEquals(2006, test.getYear()); 308 assertEquals(6, test.getMonthOfYear()); 309 assertEquals(2, test.getDayOfMonth()); assertEquals(21, test.getHourOfDay()); assertEquals(20, test.getMinuteOfHour()); 312 assertEquals(30, test.getSecondOfMinute()); 313 assertEquals(40, test.getMillisOfSecond()); 314 } 315 316 public void testConstructor_ObjectString5() throws Throwable { 317 DateTime test = new DateTime("T10:20:30.040"); 318 assertEquals(ISOChronology.getInstance(), test.getChronology()); 319 assertEquals(1970, test.getYear()); 320 assertEquals(1, test.getMonthOfYear()); 321 assertEquals(1, test.getDayOfMonth()); 322 assertEquals(10, test.getHourOfDay()); 323 assertEquals(20, test.getMinuteOfHour()); 324 assertEquals(30, test.getSecondOfMinute()); 325 assertEquals(40, test.getMillisOfSecond()); 326 } 327 328 public void testConstructor_ObjectString6() throws Throwable { 329 DateTime test = new DateTime("T10:20:30.040+14:00"); 330 assertEquals(ISOChronology.getInstance(), test.getChronology()); 331 assertEquals(1969, test.getYear()); assertEquals(12, test.getMonthOfYear()); assertEquals(31, test.getDayOfMonth()); assertEquals(21, test.getHourOfDay()); assertEquals(20, test.getMinuteOfHour()); 336 assertEquals(30, test.getSecondOfMinute()); 337 assertEquals(40, test.getMillisOfSecond()); 338 } 339 340 public void testConstructor_ObjectString7() throws Throwable { 341 DateTime test = new DateTime("10"); 342 assertEquals(ISOChronology.getInstance(), test.getChronology()); 343 assertEquals(10, test.getYear()); 344 assertEquals(1, test.getMonthOfYear()); 345 assertEquals(1, test.getDayOfMonth()); 346 assertEquals(0, test.getHourOfDay()); 347 assertEquals(0, test.getMinuteOfHour()); 348 assertEquals(0, test.getSecondOfMinute()); 349 assertEquals(0, test.getMillisOfSecond()); 350 } 351 352 public void testConstructor_ObjectStringEx1() throws Throwable { 353 try { 354 new DateTime("10:20:30.040"); 355 fail(); 356 } catch (IllegalArgumentException ex) { 357 } 359 } 360 361 public void testConstructor_ObjectStringEx2() throws Throwable { 362 try { 363 new DateTime("10:20:30.040+14:00"); 364 fail(); 365 } catch (IllegalArgumentException ex) { 366 } 368 } 369 370 374 public void testConstructor_Object_DateTimeZone() throws Throwable { 375 Date date = new Date (TEST_TIME1); 376 DateTime test = new DateTime(date, PARIS); 377 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 378 assertEquals(TEST_TIME1, test.getMillis()); 379 } 380 381 384 public void testConstructor_invalidObject_DateTimeZone() throws Throwable { 385 try { 386 new DateTime(new Object (), PARIS); 387 fail(); 388 } catch (IllegalArgumentException ex) {} 389 } 390 391 394 public void testConstructor_nullObject_DateTimeZone() throws Throwable { 395 DateTime test = new DateTime((Object ) null, PARIS); 396 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 397 assertEquals(TEST_TIME_NOW, test.getMillis()); 398 } 399 400 403 public void testConstructor_Object_nullDateTimeZone() throws Throwable { 404 Date date = new Date (TEST_TIME1); 405 DateTime test = new DateTime(date, (DateTimeZone) null); 406 assertEquals(ISOChronology.getInstance(), test.getChronology()); 407 assertEquals(TEST_TIME1, test.getMillis()); 408 } 409 410 413 public void testConstructor_nullObject_nullDateTimeZone() throws Throwable { 414 DateTime test = new DateTime((Object ) null, (DateTimeZone) null); 415 assertEquals(ISOChronology.getInstance(), test.getChronology()); 416 assertEquals(TEST_TIME_NOW, test.getMillis()); 417 } 418 419 422 public void testConstructor_badconverterObject_DateTimeZone() throws Throwable { 423 try { 424 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 425 DateTime test = new DateTime(new Integer (0), GregorianChronology.getInstance()); 426 assertEquals(ISOChronology.getInstance(), test.getChronology()); 427 assertEquals(0L, test.getMillis()); 428 } finally { 429 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 430 } 431 } 432 433 436 public void testConstructor_Object_Chronology() throws Throwable { 437 Date date = new Date (TEST_TIME1); 438 DateTime test = new DateTime(date, GregorianChronology.getInstance()); 439 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 440 assertEquals(TEST_TIME1, test.getMillis()); 441 } 442 443 446 public void testConstructor_invalidObject_Chronology() throws Throwable { 447 try { 448 new DateTime(new Object (), GregorianChronology.getInstance()); 449 fail(); 450 } catch (IllegalArgumentException ex) {} 451 } 452 453 456 public void testConstructor_nullObject_Chronology() throws Throwable { 457 DateTime test = new DateTime((Object ) null, GregorianChronology.getInstance()); 458 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 459 assertEquals(TEST_TIME_NOW, test.getMillis()); 460 } 461 462 465 public void testConstructor_Object_nullChronology() throws Throwable { 466 Date date = new Date (TEST_TIME1); 467 DateTime test = new DateTime(date, (Chronology) null); 468 assertEquals(ISOChronology.getInstance(), test.getChronology()); 469 assertEquals(TEST_TIME1, test.getMillis()); 470 } 471 472 475 public void testConstructor_nullObject_nullChronology() throws Throwable { 476 DateTime test = new DateTime((Object ) null, (Chronology) null); 477 assertEquals(ISOChronology.getInstance(), test.getChronology()); 478 assertEquals(TEST_TIME_NOW, test.getMillis()); 479 } 480 481 484 public void testConstructor_badconverterObject_Chronology() throws Throwable { 485 try { 486 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 487 DateTime test = new DateTime(new Integer (0), GregorianChronology.getInstance()); 488 assertEquals(ISOChronology.getInstance(), test.getChronology()); 489 assertEquals(0L, test.getMillis()); 490 } finally { 491 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 492 } 493 } 494 495 499 public void testConstructor_int_int_int_int_int_int_int() throws Throwable { 500 DateTime test = new DateTime(2002, 6, 9, 1, 0, 0, 0); assertEquals(ISOChronology.getInstance(), test.getChronology()); 502 assertEquals(LONDON, test.getZone()); 503 assertEquals(TEST_TIME_NOW, test.getMillis()); 504 try { 505 new DateTime(Integer.MIN_VALUE, 6, 9, 0, 0, 0, 0); 506 fail(); 507 } catch (IllegalArgumentException ex) {} 508 try { 509 new DateTime(Integer.MAX_VALUE, 6, 9, 0, 0, 0, 0); 510 fail(); 511 } catch (IllegalArgumentException ex) {} 512 try { 513 new DateTime(2002, 0, 9, 0, 0, 0, 0); 514 fail(); 515 } catch (IllegalArgumentException ex) {} 516 try { 517 new DateTime(2002, 13, 9, 0, 0, 0, 0); 518 fail(); 519 } catch (IllegalArgumentException ex) {} 520 try { 521 new DateTime(2002, 6, 0, 0, 0, 0, 0); 522 fail(); 523 } catch (IllegalArgumentException ex) {} 524 try { 525 new DateTime(2002, 6, 31, 0, 0, 0, 0); 526 fail(); 527 } catch (IllegalArgumentException ex) {} 528 new DateTime(2002, 7, 31, 0, 0, 0, 0); 529 try { 530 new DateTime(2002, 7, 32, 0, 0, 0, 0); 531 fail(); 532 } catch (IllegalArgumentException ex) {} 533 } 534 535 538 public void testConstructor_int_int_int_int_int_int_int_DateTimeZone() throws Throwable { 539 DateTime test = new DateTime(2002, 6, 9, 2, 0, 0, 0, PARIS); assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 541 assertEquals(TEST_TIME_NOW, test.getMillis()); 542 try { 543 new DateTime(Integer.MIN_VALUE, 6, 9, 0, 0, 0, 0, PARIS); 544 fail(); 545 } catch (IllegalArgumentException ex) {} 546 try { 547 new DateTime(Integer.MAX_VALUE, 6, 9, 0, 0, 0, 0, PARIS); 548 fail(); 549 } catch (IllegalArgumentException ex) {} 550 try { 551 new DateTime(2002, 0, 9, 0, 0, 0, 0, PARIS); 552 fail(); 553 } catch (IllegalArgumentException ex) {} 554 try { 555 new DateTime(2002, 13, 9, 0, 0, 0, 0, PARIS); 556 fail(); 557 } catch (IllegalArgumentException ex) {} 558 try { 559 new DateTime(2002, 6, 0, 0, 0, 0, 0, PARIS); 560 fail(); 561 } catch (IllegalArgumentException ex) {} 562 try { 563 new DateTime(2002, 6, 31, 0, 0, 0, 0, PARIS); 564 fail(); 565 } catch (IllegalArgumentException ex) {} 566 new DateTime(2002, 7, 31, 0, 0, 0, 0, PARIS); 567 try { 568 new DateTime(2002, 7, 32, 0, 0, 0, 0, PARIS); 569 fail(); 570 } catch (IllegalArgumentException ex) {} 571 } 572 573 576 public void testConstructor_int_int_int_int_int_int_int_nullDateTimeZone() throws Throwable { 577 DateTime test = new DateTime(2002, 6, 9, 1, 0, 0, 0, (DateTimeZone) null); assertEquals(ISOChronology.getInstance(), test.getChronology()); 579 assertEquals(TEST_TIME_NOW, test.getMillis()); 580 } 581 582 585 public void testConstructor_int_int_int_int_int_int_int_Chronology() throws Throwable { 586 DateTime test = new DateTime(2002, 6, 9, 1, 0, 0, 0, GregorianChronology.getInstance()); assertEquals(GregorianChronology.getInstance(), test.getChronology()); 588 assertEquals(TEST_TIME_NOW, test.getMillis()); 589 try { 590 new DateTime(Integer.MIN_VALUE, 6, 9, 0, 0, 0, 0, GregorianChronology.getInstance()); 591 fail(); 592 } catch (IllegalArgumentException ex) {} 593 try { 594 new DateTime(Integer.MAX_VALUE, 6, 9, 0, 0, 0, 0, GregorianChronology.getInstance()); 595 fail(); 596 } catch (IllegalArgumentException ex) {} 597 try { 598 new DateTime(2002, 0, 9, 0, 0, 0, 0, GregorianChronology.getInstance()); 599 fail(); 600 } catch (IllegalArgumentException ex) {} 601 try { 602 new DateTime(2002, 13, 9, 0, 0, 0, 0, GregorianChronology.getInstance()); 603 fail(); 604 } catch (IllegalArgumentException ex) {} 605 try { 606 new DateTime(2002, 6, 0, 0, 0, 0, 0, GregorianChronology.getInstance()); 607 fail(); 608 } catch (IllegalArgumentException ex) {} 609 try { 610 new DateTime(2002, 6, 31, 0, 0, 0, 0, GregorianChronology.getInstance()); 611 fail(); 612 } catch (IllegalArgumentException ex) {} 613 new DateTime(2002, 7, 31, 0, 0, 0, 0, GregorianChronology.getInstance()); 614 try { 615 new DateTime(2002, 7, 32, 0, 0, 0, 0, GregorianChronology.getInstance()); 616 fail(); 617 } catch (IllegalArgumentException ex) {} 618 } 619 620 623 public void testConstructor_int_int_int_int_int_int_int_nullChronology() throws Throwable { 624 DateTime test = new DateTime(2002, 6, 9, 1, 0, 0, 0, (Chronology) null); assertEquals(ISOChronology.getInstance(), test.getChronology()); 626 assertEquals(TEST_TIME_NOW, test.getMillis()); 627 } 628 629 } 630 | Popular Tags |