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 TestDateMidnight_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_UTC = 52 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 53 private long TEST_TIME_NOW_LONDON = 54 TEST_TIME_NOW_UTC - DateTimeConstants.MILLIS_PER_HOUR; 55 private long TEST_TIME_NOW_PARIS = 56 TEST_TIME_NOW_UTC - 2*DateTimeConstants.MILLIS_PER_HOUR; 57 58 private long TEST_TIME1_UTC = 60 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY 61 + 12L * DateTimeConstants.MILLIS_PER_HOUR 62 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 63 private long TEST_TIME1_LONDON = 64 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY 65 - DateTimeConstants.MILLIS_PER_HOUR; 66 private long TEST_TIME1_PARIS = 67 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY 68 - 2*DateTimeConstants.MILLIS_PER_HOUR; 69 70 private long TEST_TIME2_UTC = 72 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 73 + 14L * DateTimeConstants.MILLIS_PER_HOUR 74 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 75 private long TEST_TIME2_LONDON = 76 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 77 - DateTimeConstants.MILLIS_PER_HOUR; 78 private long TEST_TIME2_PARIS = 79 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 80 - 2*DateTimeConstants.MILLIS_PER_HOUR; 81 82 private DateTimeZone zone = null; 83 private Locale locale = null; 84 85 public static void main(String [] args) { 86 junit.textui.TestRunner.run(suite()); 87 } 88 89 public static TestSuite suite() { 90 return new TestSuite(TestDateMidnight_Constructors.class); 91 } 92 93 public TestDateMidnight_Constructors(String name) { 94 super(name); 95 } 96 97 protected void setUp() throws Exception { 98 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW_UTC); 99 zone = DateTimeZone.getDefault(); 100 locale = Locale.getDefault(); 101 DateTimeZone.setDefault(LONDON); 102 Locale.setDefault(Locale.UK); 103 } 104 105 protected void tearDown() throws Exception { 106 DateTimeUtils.setCurrentMillisSystem(); 107 DateTimeZone.setDefault(zone); 108 Locale.setDefault(locale); 109 zone = null; 110 } 111 112 public void testTest() { 114 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW_UTC).toString()); 115 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1_UTC).toString()); 116 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2_UTC).toString()); 117 } 118 119 123 public void testConstructor() throws Throwable { 124 DateMidnight test = new DateMidnight(); 125 assertEquals(ISOChronology.getInstance(), test.getChronology()); 126 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 127 assertEquals(2002, test.getYear()); 128 assertEquals(6, test.getMonthOfYear()); 129 assertEquals(9, test.getDayOfMonth()); 130 } 131 132 135 public void testConstructor_DateTimeZone() throws Throwable { 136 DateMidnight test = new DateMidnight(PARIS); 137 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 138 assertEquals(TEST_TIME_NOW_PARIS, test.getMillis()); 139 } 140 141 144 public void testConstructor_nullDateTimeZone() throws Throwable { 145 DateMidnight test = new DateMidnight((DateTimeZone) null); 146 assertEquals(ISOChronology.getInstance(), test.getChronology()); 147 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 148 } 149 150 153 public void testConstructor_Chronology() throws Throwable { 154 DateMidnight test = new DateMidnight(GregorianChronology.getInstance()); 155 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 156 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 157 } 158 159 162 public void testConstructor_nullChronology() throws Throwable { 163 DateMidnight test = new DateMidnight((Chronology) null); 164 assertEquals(ISOChronology.getInstance(), test.getChronology()); 165 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 166 } 167 168 172 public void testConstructor_long1() throws Throwable { 173 DateMidnight test = new DateMidnight(TEST_TIME1_UTC); 174 assertEquals(ISOChronology.getInstance(), test.getChronology()); 175 assertEquals(TEST_TIME1_LONDON, test.getMillis()); 176 } 177 178 181 public void testConstructor_long2() throws Throwable { 182 DateMidnight test = new DateMidnight(TEST_TIME2_UTC); 183 assertEquals(ISOChronology.getInstance(), test.getChronology()); 184 assertEquals(TEST_TIME2_LONDON, test.getMillis()); 185 } 186 187 190 public void testConstructor_long1_DateTimeZone() throws Throwable { 191 DateMidnight test = new DateMidnight(TEST_TIME1_UTC, PARIS); 192 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 193 assertEquals(TEST_TIME1_PARIS, test.getMillis()); 194 } 195 196 199 public void testConstructor_long2_DateTimeZone() throws Throwable { 200 DateMidnight test = new DateMidnight(TEST_TIME2_UTC, PARIS); 201 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 202 assertEquals(TEST_TIME2_PARIS, test.getMillis()); 203 } 204 205 208 public void testConstructor_long_nullDateTimeZone() throws Throwable { 209 DateMidnight test = new DateMidnight(TEST_TIME1_UTC, (DateTimeZone) null); 210 assertEquals(ISOChronology.getInstance(), test.getChronology()); 211 assertEquals(TEST_TIME1_LONDON, test.getMillis()); 212 } 213 214 217 public void testConstructor_long1_Chronology() throws Throwable { 218 DateMidnight test = new DateMidnight(TEST_TIME1_UTC, GregorianChronology.getInstance()); 219 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 220 assertEquals(TEST_TIME1_LONDON, test.getMillis()); 221 } 222 223 226 public void testConstructor_long2_Chronology() throws Throwable { 227 DateMidnight test = new DateMidnight(TEST_TIME2_UTC, GregorianChronology.getInstance()); 228 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 229 assertEquals(TEST_TIME2_LONDON, test.getMillis()); 230 } 231 232 235 public void testConstructor_long_nullChronology() throws Throwable { 236 DateMidnight test = new DateMidnight(TEST_TIME1_UTC, (Chronology) null); 237 assertEquals(ISOChronology.getInstance(), test.getChronology()); 238 assertEquals(TEST_TIME1_LONDON, test.getMillis()); 239 } 240 241 245 public void testConstructor_Object() throws Throwable { 246 Date date = new Date (TEST_TIME1_UTC); 247 DateMidnight test = new DateMidnight(date); 248 assertEquals(ISOChronology.getInstance(), test.getChronology()); 249 assertEquals(TEST_TIME1_LONDON, test.getMillis()); 250 } 251 252 255 public void testConstructor_invalidObject() throws Throwable { 256 try { 257 new DateMidnight(new Object ()); 258 fail(); 259 } catch (IllegalArgumentException ex) {} 260 } 261 262 265 public void testConstructor_nullObject() throws Throwable { 266 DateMidnight test = new DateMidnight((Object ) null); 267 assertEquals(ISOChronology.getInstance(), test.getChronology()); 268 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 269 } 270 271 274 public void testConstructor_badconverterObject() throws Throwable { 275 try { 276 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 277 DateMidnight test = new DateMidnight(new Integer (0)); 278 assertEquals(ISOChronology.getInstance(), test.getChronology()); 279 assertEquals(0L - DateTimeConstants.MILLIS_PER_HOUR, test.getMillis()); 280 } finally { 281 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 282 } 283 } 284 285 288 public void testConstructor_Object_DateTimeZone() throws Throwable { 289 Date date = new Date (TEST_TIME1_UTC); 290 DateMidnight test = new DateMidnight(date, PARIS); 291 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 292 assertEquals(TEST_TIME1_PARIS, test.getMillis()); 293 } 294 295 298 public void testConstructor_invalidObject_DateTimeZone() throws Throwable { 299 try { 300 new DateMidnight(new Object (), PARIS); 301 fail(); 302 } catch (IllegalArgumentException ex) {} 303 } 304 305 308 public void testConstructor_nullObject_DateTimeZone() throws Throwable { 309 DateMidnight test = new DateMidnight((Object ) null, PARIS); 310 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 311 assertEquals(TEST_TIME_NOW_PARIS, test.getMillis()); 312 } 313 314 317 public void testConstructor_Object_nullDateTimeZone() throws Throwable { 318 Date date = new Date (TEST_TIME1_UTC); 319 DateMidnight test = new DateMidnight(date, (DateTimeZone) null); 320 assertEquals(ISOChronology.getInstance(), test.getChronology()); 321 assertEquals(TEST_TIME1_LONDON, test.getMillis()); 322 } 323 324 327 public void testConstructor_nullObject_nullDateTimeZone() throws Throwable { 328 DateMidnight test = new DateMidnight((Object ) null, (DateTimeZone) null); 329 assertEquals(ISOChronology.getInstance(), test.getChronology()); 330 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 331 } 332 333 336 public void testConstructor_badconverterObject_DateTimeZone() throws Throwable { 337 try { 338 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 339 DateMidnight test = new DateMidnight(new Integer (0), GregorianChronology.getInstance()); 340 assertEquals(ISOChronology.getInstance(), test.getChronology()); 341 assertEquals(0L - DateTimeConstants.MILLIS_PER_HOUR, test.getMillis()); 342 } finally { 343 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 344 } 345 } 346 347 350 public void testConstructor_Object_Chronology() throws Throwable { 351 Date date = new Date (TEST_TIME1_UTC); 352 DateMidnight test = new DateMidnight(date, GregorianChronology.getInstance()); 353 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 354 assertEquals(TEST_TIME1_LONDON, test.getMillis()); 355 } 356 357 360 public void testConstructor_invalidObject_Chronology() throws Throwable { 361 try { 362 new DateMidnight(new Object (), GregorianChronology.getInstance()); 363 fail(); 364 } catch (IllegalArgumentException ex) {} 365 } 366 367 370 public void testConstructor_nullObject_Chronology() throws Throwable { 371 DateMidnight test = new DateMidnight((Object ) null, GregorianChronology.getInstance()); 372 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 373 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 374 } 375 376 379 public void testConstructor_Object_nullChronology() throws Throwable { 380 Date date = new Date (TEST_TIME1_UTC); 381 DateMidnight test = new DateMidnight(date, (Chronology) null); 382 assertEquals(ISOChronology.getInstance(), test.getChronology()); 383 assertEquals(TEST_TIME1_LONDON, test.getMillis()); 384 } 385 386 389 public void testConstructor_nullObject_nullChronology() throws Throwable { 390 DateMidnight test = new DateMidnight((Object ) null, (Chronology) null); 391 assertEquals(ISOChronology.getInstance(), test.getChronology()); 392 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 393 } 394 395 398 public void testConstructor_badconverterObject_Chronology() throws Throwable { 399 try { 400 ConverterManager.getInstance().addInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 401 DateMidnight test = new DateMidnight(new Integer (0), GregorianChronology.getInstance()); 402 assertEquals(ISOChronology.getInstance(), test.getChronology()); 403 assertEquals(0L - DateTimeConstants.MILLIS_PER_HOUR, test.getMillis()); 404 } finally { 405 ConverterManager.getInstance().removeInstantConverter(MockZeroNullIntegerConverter.INSTANCE); 406 } 407 } 408 409 413 public void testConstructor_int_int_int() throws Throwable { 414 DateMidnight test = new DateMidnight(2002, 6, 9); 415 assertEquals(ISOChronology.getInstance(), test.getChronology()); 416 assertEquals(LONDON, test.getZone()); 417 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 418 assertEquals(2002, test.getYear()); 419 assertEquals(6, test.getMonthOfYear()); 420 assertEquals(9, test.getDayOfMonth()); 421 try { 422 new DateMidnight(Integer.MIN_VALUE, 6, 9); 423 fail(); 424 } catch (IllegalArgumentException ex) {} 425 try { 426 new DateMidnight(Integer.MAX_VALUE, 6, 9); 427 fail(); 428 } catch (IllegalArgumentException ex) {} 429 try { 430 new DateMidnight(2002, 0, 9); 431 fail(); 432 } catch (IllegalArgumentException ex) {} 433 try { 434 new DateMidnight(2002, 13, 9); 435 fail(); 436 } catch (IllegalArgumentException ex) {} 437 try { 438 new DateMidnight(2002, 6, 0); 439 fail(); 440 } catch (IllegalArgumentException ex) {} 441 try { 442 new DateMidnight(2002, 6, 31); 443 fail(); 444 } catch (IllegalArgumentException ex) {} 445 new DateMidnight(2002, 7, 31); 446 try { 447 new DateMidnight(2002, 7, 32); 448 fail(); 449 } catch (IllegalArgumentException ex) {} 450 } 451 452 455 public void testConstructor_int_int_int_DateTimeZone() throws Throwable { 456 DateMidnight test = new DateMidnight(2002, 6, 9, PARIS); 457 assertEquals(ISOChronology.getInstance(PARIS), test.getChronology()); 458 assertEquals(TEST_TIME_NOW_PARIS, test.getMillis()); 459 assertEquals(2002, test.getYear()); 460 assertEquals(6, test.getMonthOfYear()); 461 assertEquals(9, test.getDayOfMonth()); 462 try { 463 new DateMidnight(Integer.MIN_VALUE, 6, 9, PARIS); 464 fail(); 465 } catch (IllegalArgumentException ex) {} 466 try { 467 new DateMidnight(Integer.MAX_VALUE, 6, 9, PARIS); 468 fail(); 469 } catch (IllegalArgumentException ex) {} 470 try { 471 new DateMidnight(2002, 0, 9, PARIS); 472 fail(); 473 } catch (IllegalArgumentException ex) {} 474 try { 475 new DateMidnight(2002, 13, 9, PARIS); 476 fail(); 477 } catch (IllegalArgumentException ex) {} 478 try { 479 new DateMidnight(2002, 6, 0, PARIS); 480 fail(); 481 } catch (IllegalArgumentException ex) {} 482 try { 483 new DateMidnight(2002, 6, 31, PARIS); 484 fail(); 485 } catch (IllegalArgumentException ex) {} 486 new DateMidnight(2002, 7, 31, PARIS); 487 try { 488 new DateMidnight(2002, 7, 32, PARIS); 489 fail(); 490 } catch (IllegalArgumentException ex) {} 491 } 492 493 496 public void testConstructor_int_int_int_nullDateTimeZone() throws Throwable { 497 DateMidnight test = new DateMidnight(2002, 6, 9, (DateTimeZone) null); 498 assertEquals(ISOChronology.getInstance(), test.getChronology()); 499 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 500 assertEquals(2002, test.getYear()); 501 assertEquals(6, test.getMonthOfYear()); 502 assertEquals(9, test.getDayOfMonth()); 503 } 504 505 508 public void testConstructor_int_int_int_Chronology() throws Throwable { 509 DateMidnight test = new DateMidnight(2002, 6, 9, GregorianChronology.getInstance()); 510 assertEquals(GregorianChronology.getInstance(), test.getChronology()); 511 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 512 assertEquals(2002, test.getYear()); 513 assertEquals(6, test.getMonthOfYear()); 514 assertEquals(9, test.getDayOfMonth()); 515 try { 516 new DateMidnight(Integer.MIN_VALUE, 6, 9, GregorianChronology.getInstance()); 517 fail(); 518 } catch (IllegalArgumentException ex) {} 519 try { 520 new DateMidnight(Integer.MAX_VALUE, 6, 9, GregorianChronology.getInstance()); 521 fail(); 522 } catch (IllegalArgumentException ex) {} 523 try { 524 new DateMidnight(2002, 0, 9, GregorianChronology.getInstance()); 525 fail(); 526 } catch (IllegalArgumentException ex) {} 527 try { 528 new DateMidnight(2002, 13, 9, GregorianChronology.getInstance()); 529 fail(); 530 } catch (IllegalArgumentException ex) {} 531 try { 532 new DateMidnight(2002, 6, 0, GregorianChronology.getInstance()); 533 fail(); 534 } catch (IllegalArgumentException ex) {} 535 try { 536 new DateMidnight(2002, 6, 31, GregorianChronology.getInstance()); 537 fail(); 538 } catch (IllegalArgumentException ex) {} 539 new DateMidnight(2002, 7, 31, GregorianChronology.getInstance()); 540 try { 541 new DateMidnight(2002, 7, 32, GregorianChronology.getInstance()); 542 fail(); 543 } catch (IllegalArgumentException ex) {} 544 } 545 546 549 public void testConstructor_int_int_int_nullChronology() throws Throwable { 550 DateMidnight test = new DateMidnight(2002, 6, 9, (Chronology) null); 551 assertEquals(ISOChronology.getInstance(), test.getChronology()); 552 assertEquals(TEST_TIME_NOW_LONDON, test.getMillis()); 553 assertEquals(2002, test.getYear()); 554 assertEquals(6, test.getMonthOfYear()); 555 assertEquals(9, test.getDayOfMonth()); 556 } 557 558 } 559 | Popular Tags |