001 /* 002 * Copyright 2001-2010 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.Arrays; 023 import java.util.Calendar; 024 import java.util.Date; 025 import java.util.GregorianCalendar; 026 import java.util.Locale; 027 import java.util.SimpleTimeZone; 028 import java.util.TimeZone; 029 030 import junit.framework.TestCase; 031 import junit.framework.TestSuite; 032 033 import org.joda.time.chrono.BuddhistChronology; 034 import org.joda.time.chrono.CopticChronology; 035 import org.joda.time.chrono.GJChronology; 036 import org.joda.time.chrono.GregorianChronology; 037 import org.joda.time.chrono.ISOChronology; 038 import org.joda.time.chrono.LenientChronology; 039 import org.joda.time.chrono.StrictChronology; 040 import org.joda.time.format.DateTimeFormat; 041 import org.joda.time.format.DateTimeFormatter; 042 043 /** 044 * This class is a Junit unit test for LocalDate. 045 * 046 * @author Stephen Colebourne 047 */ 048 public class TestLocalDate_Basics extends TestCase { 049 050 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 051 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 052 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 053 // private static final int OFFSET = 1; 054 private static final GJChronology GJ_UTC = GJChronology.getInstanceUTC(); 055 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS); 056 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON); 057 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO); 058 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 059 // private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 060 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON); 061 // private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO); 062 // private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 063 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS); 064 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON); 065 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO); 066 // private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC(); 067 068 /** Mock zone simulating Asia/Gaza cutover at midnight 2007-04-01 */ 069 private static long CUTOVER_GAZA = 1175378400000L; 070 private static int OFFSET_GAZA = 7200000; // +02:00 071 private static final DateTimeZone MOCK_GAZA = new MockZone(CUTOVER_GAZA, OFFSET_GAZA, 3600); 072 073 private long TEST_TIME_NOW = 074 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 075 076 // private long TEST_TIME1 = 077 // (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 078 // + 12L * DateTimeConstants.MILLIS_PER_HOUR 079 // + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 080 // 081 // private long TEST_TIME2 = 082 // (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 083 // + 14L * DateTimeConstants.MILLIS_PER_HOUR 084 // + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 085 086 private DateTimeZone zone = null; 087 088 private Locale systemDefaultLocale = null; 089 090 public static void main(String[] args) { 091 junit.textui.TestRunner.run(suite()); 092 } 093 094 public static TestSuite suite() { 095 return new TestSuite(TestLocalDate_Basics.class); 096 } 097 098 public TestLocalDate_Basics(String name) { 099 super(name); 100 } 101 102 protected void setUp() throws Exception { 103 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 104 zone = DateTimeZone.getDefault(); 105 DateTimeZone.setDefault(LONDON); 106 systemDefaultLocale = Locale.getDefault(); 107 Locale.setDefault(Locale.ENGLISH); 108 } 109 110 protected void tearDown() throws Exception { 111 DateTimeUtils.setCurrentMillisSystem(); 112 DateTimeZone.setDefault(zone); 113 zone = null; 114 Locale.setDefault(systemDefaultLocale); 115 systemDefaultLocale = null; 116 } 117 118 //----------------------------------------------------------------------- 119 public void testGet_DateTimeFieldType() { 120 LocalDate test = new LocalDate(); 121 assertEquals(1970, test.get(DateTimeFieldType.year())); 122 assertEquals(6, test.get(DateTimeFieldType.monthOfYear())); 123 assertEquals(9, test.get(DateTimeFieldType.dayOfMonth())); 124 assertEquals(2, test.get(DateTimeFieldType.dayOfWeek())); 125 assertEquals(160, test.get(DateTimeFieldType.dayOfYear())); 126 assertEquals(24, test.get(DateTimeFieldType.weekOfWeekyear())); 127 assertEquals(1970, test.get(DateTimeFieldType.weekyear())); 128 try { 129 test.get(null); 130 fail(); 131 } catch (IllegalArgumentException ex) {} 132 try { 133 test.get(DateTimeFieldType.hourOfDay()); 134 fail(); 135 } catch (IllegalArgumentException ex) {} 136 } 137 138 public void testSize() { 139 LocalDate test = new LocalDate(); 140 assertEquals(3, test.size()); 141 } 142 143 public void testGetFieldType_int() { 144 LocalDate test = new LocalDate(COPTIC_PARIS); 145 assertSame(DateTimeFieldType.year(), test.getFieldType(0)); 146 assertSame(DateTimeFieldType.monthOfYear(), test.getFieldType(1)); 147 assertSame(DateTimeFieldType.dayOfMonth(), test.getFieldType(2)); 148 try { 149 test.getFieldType(-1); 150 } catch (IndexOutOfBoundsException ex) {} 151 try { 152 test.getFieldType(3); 153 } catch (IndexOutOfBoundsException ex) {} 154 } 155 156 public void testGetFieldTypes() { 157 LocalDate test = new LocalDate(COPTIC_PARIS); 158 DateTimeFieldType[] fields = test.getFieldTypes(); 159 assertSame(DateTimeFieldType.year(), fields[0]); 160 assertSame(DateTimeFieldType.monthOfYear(), fields[1]); 161 assertSame(DateTimeFieldType.dayOfMonth(), fields[2]); 162 assertNotSame(test.getFieldTypes(), test.getFieldTypes()); 163 } 164 165 public void testGetField_int() { 166 LocalDate test = new LocalDate(COPTIC_PARIS); 167 assertSame(COPTIC_UTC.year(), test.getField(0)); 168 assertSame(COPTIC_UTC.monthOfYear(), test.getField(1)); 169 assertSame(COPTIC_UTC.dayOfMonth(), test.getField(2)); 170 try { 171 test.getField(-1); 172 } catch (IndexOutOfBoundsException ex) {} 173 try { 174 test.getField(3); 175 } catch (IndexOutOfBoundsException ex) {} 176 } 177 178 public void testGetFields() { 179 LocalDate test = new LocalDate(COPTIC_PARIS); 180 DateTimeField[] fields = test.getFields(); 181 assertSame(COPTIC_UTC.year(), fields[0]); 182 assertSame(COPTIC_UTC.monthOfYear(), fields[1]); 183 assertSame(COPTIC_UTC.dayOfMonth(), fields[2]); 184 assertNotSame(test.getFields(), test.getFields()); 185 } 186 187 public void testGetValue_int() { 188 LocalDate test = new LocalDate(); 189 assertEquals(1970, test.getValue(0)); 190 assertEquals(6, test.getValue(1)); 191 assertEquals(9, test.getValue(2)); 192 try { 193 test.getValue(-1); 194 } catch (IndexOutOfBoundsException ex) {} 195 try { 196 test.getValue(3); 197 } catch (IndexOutOfBoundsException ex) {} 198 } 199 200 public void testGetValues() { 201 LocalDate test = new LocalDate(); 202 int[] values = test.getValues(); 203 assertEquals(1970, values[0]); 204 assertEquals(6, values[1]); 205 assertEquals(9, values[2]); 206 assertNotSame(test.getValues(), test.getValues()); 207 } 208 209 public void testIsSupported_DateTimeFieldType() { 210 LocalDate test = new LocalDate(COPTIC_PARIS); 211 assertEquals(true, test.isSupported(DateTimeFieldType.year())); 212 assertEquals(true, test.isSupported(DateTimeFieldType.monthOfYear())); 213 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfMonth())); 214 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfWeek())); 215 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear())); 216 assertEquals(true, test.isSupported(DateTimeFieldType.weekOfWeekyear())); 217 assertEquals(true, test.isSupported(DateTimeFieldType.weekyear())); 218 assertEquals(true, test.isSupported(DateTimeFieldType.yearOfCentury())); 219 assertEquals(true, test.isSupported(DateTimeFieldType.yearOfEra())); 220 assertEquals(true, test.isSupported(DateTimeFieldType.centuryOfEra())); 221 assertEquals(true, test.isSupported(DateTimeFieldType.weekyearOfCentury())); 222 assertEquals(true, test.isSupported(DateTimeFieldType.era())); 223 assertEquals(false, test.isSupported(DateTimeFieldType.hourOfDay())); 224 assertEquals(false, test.isSupported((DateTimeFieldType) null)); 225 } 226 227 public void testIsSupported_DurationFieldType() { 228 LocalDate test = new LocalDate(1970, 6, 9); 229 assertEquals(false, test.isSupported(DurationFieldType.eras())); 230 assertEquals(true, test.isSupported(DurationFieldType.centuries())); 231 assertEquals(true, test.isSupported(DurationFieldType.years())); 232 assertEquals(true, test.isSupported(DurationFieldType.months())); 233 assertEquals(true, test.isSupported(DurationFieldType.weekyears())); 234 assertEquals(true, test.isSupported(DurationFieldType.weeks())); 235 assertEquals(true, test.isSupported(DurationFieldType.days())); 236 237 assertEquals(false, test.isSupported(DurationFieldType.hours())); 238 assertEquals(false, test.isSupported((DurationFieldType) null)); 239 } 240 241 public void testEqualsHashCode() { 242 LocalDate test1 = new LocalDate(1970, 6, 9, COPTIC_PARIS); 243 LocalDate test2 = new LocalDate(1970, 6, 9, COPTIC_PARIS); 244 assertEquals(true, test1.equals(test2)); 245 assertEquals(true, test2.equals(test1)); 246 assertEquals(true, test1.equals(test1)); 247 assertEquals(true, test2.equals(test2)); 248 assertEquals(true, test1.hashCode() == test2.hashCode()); 249 assertEquals(true, test1.hashCode() == test1.hashCode()); 250 assertEquals(true, test2.hashCode() == test2.hashCode()); 251 252 LocalDate test3 = new LocalDate(1971, 6, 9); 253 assertEquals(false, test1.equals(test3)); 254 assertEquals(false, test2.equals(test3)); 255 assertEquals(false, test3.equals(test1)); 256 assertEquals(false, test3.equals(test2)); 257 assertEquals(false, test1.hashCode() == test3.hashCode()); 258 assertEquals(false, test2.hashCode() == test3.hashCode()); 259 260 assertEquals(false, test1.equals("Hello")); 261 assertEquals(true, test1.equals(new MockInstant())); 262 assertEquals(true, test1.equals(new YearMonthDay(1970, 6, 9, COPTIC_PARIS))); 263 assertEquals(true, test1.hashCode() == new YearMonthDay(1970, 6, 9, COPTIC_PARIS).hashCode()); 264 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE)); 265 } 266 267 class MockInstant extends MockPartial { 268 public Chronology getChronology() { 269 return COPTIC_UTC; 270 } 271 public DateTimeField[] getFields() { 272 return new DateTimeField[] { 273 COPTIC_UTC.year(), 274 COPTIC_UTC.monthOfYear(), 275 COPTIC_UTC.dayOfMonth(), 276 }; 277 } 278 public int[] getValues() { 279 return new int[] {1970, 6, 9}; 280 } 281 } 282 283 public void testEqualsHashCodeLenient() { 284 LocalDate test1 = new LocalDate(1970, 6, 9, LenientChronology.getInstance(COPTIC_PARIS)); 285 LocalDate test2 = new LocalDate(1970, 6, 9, LenientChronology.getInstance(COPTIC_PARIS)); 286 assertEquals(true, test1.equals(test2)); 287 assertEquals(true, test2.equals(test1)); 288 assertEquals(true, test1.equals(test1)); 289 assertEquals(true, test2.equals(test2)); 290 assertEquals(true, test1.hashCode() == test2.hashCode()); 291 assertEquals(true, test1.hashCode() == test1.hashCode()); 292 assertEquals(true, test2.hashCode() == test2.hashCode()); 293 } 294 295 public void testEqualsHashCodeStrict() { 296 LocalDate test1 = new LocalDate(1970, 6, 9, StrictChronology.getInstance(COPTIC_PARIS)); 297 LocalDate test2 = new LocalDate(1970, 6, 9, StrictChronology.getInstance(COPTIC_PARIS)); 298 assertEquals(true, test1.equals(test2)); 299 assertEquals(true, test2.equals(test1)); 300 assertEquals(true, test1.equals(test1)); 301 assertEquals(true, test2.equals(test2)); 302 assertEquals(true, test1.hashCode() == test2.hashCode()); 303 assertEquals(true, test1.hashCode() == test1.hashCode()); 304 assertEquals(true, test2.hashCode() == test2.hashCode()); 305 } 306 307 public void testEqualsHashCodeAPI() { 308 LocalDate test = new LocalDate(1970, 6, 9, COPTIC_PARIS); 309 int expected = 157; 310 expected = 23 * expected + 1970; 311 expected = 23 * expected + COPTIC_UTC.year().getType().hashCode(); 312 expected = 23 * expected + 6; 313 expected = 23 * expected + COPTIC_UTC.monthOfYear().getType().hashCode(); 314 expected = 23 * expected + 9; 315 expected = 23 * expected + COPTIC_UTC.dayOfMonth().getType().hashCode(); 316 expected += COPTIC_UTC.hashCode(); 317 assertEquals(expected, test.hashCode()); 318 } 319 320 //----------------------------------------------------------------------- 321 public void testCompareTo() { 322 LocalDate test1 = new LocalDate(2005, 6, 2); 323 LocalDate test1a = new LocalDate(2005, 6, 2); 324 assertEquals(0, test1.compareTo(test1a)); 325 assertEquals(0, test1a.compareTo(test1)); 326 assertEquals(0, test1.compareTo(test1)); 327 assertEquals(0, test1a.compareTo(test1a)); 328 329 LocalDate test2 = new LocalDate(2005, 7, 2); 330 assertEquals(-1, test1.compareTo(test2)); 331 assertEquals(+1, test2.compareTo(test1)); 332 333 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC()); 334 assertEquals(-1, test1.compareTo(test3)); 335 assertEquals(+1, test3.compareTo(test1)); 336 assertEquals(0, test3.compareTo(test2)); 337 338 DateTimeFieldType[] types = new DateTimeFieldType[] { 339 DateTimeFieldType.year(), 340 DateTimeFieldType.monthOfYear(), 341 DateTimeFieldType.dayOfMonth(), 342 }; 343 int[] values = new int[] {2005, 6, 2}; 344 Partial p = new Partial(types, values); 345 assertEquals(0, test1.compareTo(p)); 346 assertEquals(0, test1.compareTo(new YearMonthDay(2005, 6, 2))); 347 try { 348 test1.compareTo(null); 349 fail(); 350 } catch (NullPointerException ex) {} 351 // try { 352 // test1.compareTo(new Date()); 353 // fail(); 354 // } catch (ClassCastException ex) {} 355 try { 356 test1.compareTo(new TimeOfDay()); 357 fail(); 358 } catch (ClassCastException ex) {} 359 Partial partial = new Partial() 360 .with(DateTimeFieldType.centuryOfEra(), 1) 361 .with(DateTimeFieldType.halfdayOfDay(), 0) 362 .with(DateTimeFieldType.dayOfMonth(), 9); 363 try { 364 new LocalDate(1970, 6, 9).compareTo(partial); 365 fail(); 366 } catch (ClassCastException ex) {} 367 } 368 369 //----------------------------------------------------------------------- 370 public void testIsEqual_LocalDate() { 371 LocalDate test1 = new LocalDate(2005, 6, 2); 372 LocalDate test1a = new LocalDate(2005, 6, 2); 373 assertEquals(true, test1.isEqual(test1a)); 374 assertEquals(true, test1a.isEqual(test1)); 375 assertEquals(true, test1.isEqual(test1)); 376 assertEquals(true, test1a.isEqual(test1a)); 377 378 LocalDate test2 = new LocalDate(2005, 7, 2); 379 assertEquals(false, test1.isEqual(test2)); 380 assertEquals(false, test2.isEqual(test1)); 381 382 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC()); 383 assertEquals(false, test1.isEqual(test3)); 384 assertEquals(false, test3.isEqual(test1)); 385 assertEquals(true, test3.isEqual(test2)); 386 387 try { 388 new LocalDate(2005, 7, 2).isEqual(null); 389 fail(); 390 } catch (IllegalArgumentException ex) {} 391 } 392 393 //----------------------------------------------------------------------- 394 public void testIsBefore_LocalDate() { 395 LocalDate test1 = new LocalDate(2005, 6, 2); 396 LocalDate test1a = new LocalDate(2005, 6, 2); 397 assertEquals(false, test1.isBefore(test1a)); 398 assertEquals(false, test1a.isBefore(test1)); 399 assertEquals(false, test1.isBefore(test1)); 400 assertEquals(false, test1a.isBefore(test1a)); 401 402 LocalDate test2 = new LocalDate(2005, 7, 2); 403 assertEquals(true, test1.isBefore(test2)); 404 assertEquals(false, test2.isBefore(test1)); 405 406 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC()); 407 assertEquals(true, test1.isBefore(test3)); 408 assertEquals(false, test3.isBefore(test1)); 409 assertEquals(false, test3.isBefore(test2)); 410 411 try { 412 new LocalDate(2005, 7, 2).isBefore(null); 413 fail(); 414 } catch (IllegalArgumentException ex) {} 415 } 416 417 //----------------------------------------------------------------------- 418 public void testIsAfter_LocalDate() { 419 LocalDate test1 = new LocalDate(2005, 6, 2); 420 LocalDate test1a = new LocalDate(2005, 6, 2); 421 assertEquals(false, test1.isAfter(test1a)); 422 assertEquals(false, test1a.isAfter(test1)); 423 assertEquals(false, test1.isAfter(test1)); 424 assertEquals(false, test1a.isAfter(test1a)); 425 426 LocalDate test2 = new LocalDate(2005, 7, 2); 427 assertEquals(false, test1.isAfter(test2)); 428 assertEquals(true, test2.isAfter(test1)); 429 430 LocalDate test3 = new LocalDate(2005, 7, 2, GregorianChronology.getInstanceUTC()); 431 assertEquals(false, test1.isAfter(test3)); 432 assertEquals(true, test3.isAfter(test1)); 433 assertEquals(false, test3.isAfter(test2)); 434 435 try { 436 new LocalDate(2005, 7, 2).isAfter(null); 437 fail(); 438 } catch (IllegalArgumentException ex) {} 439 } 440 441 //----------------------------------------------------------------------- 442 public void testWithField_DateTimeFieldType_int_1() { 443 LocalDate test = new LocalDate(2004, 6, 9); 444 LocalDate result = test.withField(DateTimeFieldType.year(), 2006); 445 446 assertEquals(new LocalDate(2004, 6, 9), test); 447 assertEquals(new LocalDate(2006, 6, 9), result); 448 } 449 450 public void testWithField_DateTimeFieldType_int_2() { 451 LocalDate test = new LocalDate(2004, 6, 9); 452 try { 453 test.withField(null, 6); 454 fail(); 455 } catch (IllegalArgumentException ex) {} 456 } 457 458 public void testWithField_DateTimeFieldType_int_3() { 459 LocalDate test = new LocalDate(2004, 6, 9); 460 try { 461 test.withField(DateTimeFieldType.hourOfDay(), 6); 462 fail(); 463 } catch (IllegalArgumentException ex) {} 464 } 465 466 public void testWithField_DateTimeFieldType_int_4() { 467 LocalDate test = new LocalDate(2004, 6, 9); 468 LocalDate result = test.withField(DateTimeFieldType.year(), 2004); 469 assertEquals(new LocalDate(2004, 6, 9), test); 470 assertSame(test, result); 471 } 472 473 //----------------------------------------------------------------------- 474 public void testWithFieldAdded_DurationFieldType_int_1() { 475 LocalDate test = new LocalDate(2004, 6, 9); 476 LocalDate result = test.withFieldAdded(DurationFieldType.years(), 6); 477 478 assertEquals(new LocalDate(2004, 6, 9), test); 479 assertEquals(new LocalDate(2010, 6, 9), result); 480 } 481 482 public void testWithFieldAdded_DurationFieldType_int_2() { 483 LocalDate test = new LocalDate(2004, 6, 9); 484 try { 485 test.withFieldAdded(null, 0); 486 fail(); 487 } catch (IllegalArgumentException ex) {} 488 } 489 490 public void testWithFieldAdded_DurationFieldType_int_3() { 491 LocalDate test = new LocalDate(2004, 6, 9); 492 try { 493 test.withFieldAdded(null, 6); 494 fail(); 495 } catch (IllegalArgumentException ex) {} 496 } 497 498 public void testWithFieldAdded_DurationFieldType_int_4() { 499 LocalDate test = new LocalDate(2004, 6, 9); 500 LocalDate result = test.withFieldAdded(DurationFieldType.years(), 0); 501 assertSame(test, result); 502 } 503 504 public void testWithFieldAdded_DurationFieldType_int_5() { 505 LocalDate test = new LocalDate(2004, 6, 9); 506 try { 507 test.withFieldAdded(DurationFieldType.hours(), 6); 508 fail(); 509 } catch (IllegalArgumentException ex) {} 510 } 511 512 //----------------------------------------------------------------------- 513 public void testPlus_RP() { 514 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 515 LocalDate result = test.plus(new Period(1, 2, 3, 4, 29, 6, 7, 8)); 516 LocalDate expected = new LocalDate(2003, 7, 28, BUDDHIST_LONDON); 517 assertEquals(expected, result); 518 519 result = test.plus((ReadablePeriod) null); 520 assertSame(test, result); 521 } 522 523 public void testPlusYears_int() { 524 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 525 LocalDate result = test.plusYears(1); 526 LocalDate expected = new LocalDate(2003, 5, 3, BUDDHIST_LONDON); 527 assertEquals(expected, result); 528 529 result = test.plusYears(0); 530 assertSame(test, result); 531 } 532 533 public void testPlusMonths_int() { 534 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 535 LocalDate result = test.plusMonths(1); 536 LocalDate expected = new LocalDate(2002, 6, 3, BUDDHIST_LONDON); 537 assertEquals(expected, result); 538 539 result = test.plusMonths(0); 540 assertSame(test, result); 541 } 542 543 public void testPlusWeeks_int() { 544 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 545 LocalDate result = test.plusWeeks(1); 546 LocalDate expected = new LocalDate(2002, 5, 10, BUDDHIST_LONDON); 547 assertEquals(expected, result); 548 549 result = test.plusWeeks(0); 550 assertSame(test, result); 551 } 552 553 public void testPlusDays_int() { 554 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 555 LocalDate result = test.plusDays(1); 556 LocalDate expected = new LocalDate(2002, 5, 4, BUDDHIST_LONDON); 557 assertEquals(expected, result); 558 559 result = test.plusDays(0); 560 assertSame(test, result); 561 } 562 563 //----------------------------------------------------------------------- 564 public void testMinus_RP() { 565 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 566 LocalDate result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1)); 567 568 // TODO breaks because it subtracts millis now, and thus goes 569 // into the previous day 570 571 LocalDate expected = new LocalDate(2001, 3, 26, BUDDHIST_LONDON); 572 assertEquals(expected, result); 573 574 result = test.minus((ReadablePeriod) null); 575 assertSame(test, result); 576 } 577 578 public void testMinusYears_int() { 579 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 580 LocalDate result = test.minusYears(1); 581 LocalDate expected = new LocalDate(2001, 5, 3, BUDDHIST_LONDON); 582 assertEquals(expected, result); 583 584 result = test.minusYears(0); 585 assertSame(test, result); 586 } 587 588 public void testMinusMonths_int() { 589 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 590 LocalDate result = test.minusMonths(1); 591 LocalDate expected = new LocalDate(2002, 4, 3, BUDDHIST_LONDON); 592 assertEquals(expected, result); 593 594 result = test.minusMonths(0); 595 assertSame(test, result); 596 } 597 598 public void testMinusWeeks_int() { 599 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 600 LocalDate result = test.minusWeeks(1); 601 LocalDate expected = new LocalDate(2002, 4, 26, BUDDHIST_LONDON); 602 assertEquals(expected, result); 603 604 result = test.minusWeeks(0); 605 assertSame(test, result); 606 } 607 608 public void testMinusDays_int() { 609 LocalDate test = new LocalDate(2002, 5, 3, BUDDHIST_LONDON); 610 LocalDate result = test.minusDays(1); 611 LocalDate expected = new LocalDate(2002, 5, 2, BUDDHIST_LONDON); 612 assertEquals(expected, result); 613 614 result = test.minusDays(0); 615 assertSame(test, result); 616 } 617 618 //----------------------------------------------------------------------- 619 public void testGetters() { 620 LocalDate test = new LocalDate(1970, 6, 9, GJ_UTC); 621 assertEquals(1970, test.getYear()); 622 assertEquals(6, test.getMonthOfYear()); 623 assertEquals(9, test.getDayOfMonth()); 624 assertEquals(160, test.getDayOfYear()); 625 assertEquals(2, test.getDayOfWeek()); 626 assertEquals(24, test.getWeekOfWeekyear()); 627 assertEquals(1970, test.getWeekyear()); 628 assertEquals(70, test.getYearOfCentury()); 629 assertEquals(20, test.getCenturyOfEra()); 630 assertEquals(1970, test.getYearOfEra()); 631 assertEquals(DateTimeConstants.AD, test.getEra()); 632 } 633 634 //----------------------------------------------------------------------- 635 public void testWithers() { 636 LocalDate test = new LocalDate(1970, 6, 9, GJ_UTC); 637 check(test.withYear(2000), 2000, 6, 9); 638 check(test.withMonthOfYear(2), 1970, 2, 9); 639 check(test.withDayOfMonth(2), 1970, 6, 2); 640 check(test.withDayOfYear(6), 1970, 1, 6); 641 check(test.withDayOfWeek(6), 1970, 6, 13); 642 check(test.withWeekOfWeekyear(6), 1970, 2, 3); 643 check(test.withWeekyear(1971), 1971, 6, 15); 644 check(test.withYearOfCentury(60), 1960, 6, 9); 645 check(test.withCenturyOfEra(21), 2070, 6, 9); 646 check(test.withYearOfEra(1066), 1066, 6, 9); 647 check(test.withEra(DateTimeConstants.BC), -1970, 6, 9); 648 try { 649 test.withMonthOfYear(0); 650 fail(); 651 } catch (IllegalArgumentException ex) {} 652 try { 653 test.withMonthOfYear(13); 654 fail(); 655 } catch (IllegalArgumentException ex) {} 656 } 657 658 //----------------------------------------------------------------------- 659 public void testToDateTimeAtStartOfDay() { 660 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 661 662 DateTime test = base.toDateTimeAtStartOfDay(); 663 check(base, 2005, 6, 9); 664 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test); 665 } 666 667 public void testToDateTimeAtStartOfDay_avoidDST() { 668 LocalDate base = new LocalDate(2007, 4, 1); 669 670 DateTimeZone.setDefault(MOCK_GAZA); 671 DateTime test = base.toDateTimeAtStartOfDay(); 672 check(base, 2007, 4, 1); 673 assertEquals(new DateTime(2007, 4, 1, 1, 0, 0, 0, MOCK_GAZA), test); 674 } 675 676 //----------------------------------------------------------------------- 677 public void testToDateTimeAtStartOfDay_Zone() { 678 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 679 680 DateTime test = base.toDateTimeAtStartOfDay(TOKYO); 681 check(base, 2005, 6, 9); 682 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_TOKYO), test); 683 } 684 685 public void testToDateTimeAtStartOfDay_Zone_avoidDST() { 686 LocalDate base = new LocalDate(2007, 4, 1); 687 688 DateTime test = base.toDateTimeAtStartOfDay(MOCK_GAZA); 689 check(base, 2007, 4, 1); 690 assertEquals(new DateTime(2007, 4, 1, 1, 0, 0, 0, MOCK_GAZA), test); 691 } 692 693 public void testToDateTimeAtStartOfDay_nullZone() { 694 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 695 696 DateTime test = base.toDateTimeAtStartOfDay((DateTimeZone) null); 697 check(base, 2005, 6, 9); 698 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test); 699 } 700 701 //----------------------------------------------------------------------- 702 @SuppressWarnings("deprecation") 703 public void testToDateTimeAtMidnight() { 704 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 705 706 DateTime test = base.toDateTimeAtMidnight(); 707 check(base, 2005, 6, 9); 708 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test); 709 } 710 711 //----------------------------------------------------------------------- 712 @SuppressWarnings("deprecation") 713 public void testToDateTimeAtMidnight_Zone() { 714 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 715 716 DateTime test = base.toDateTimeAtMidnight(TOKYO); 717 check(base, 2005, 6, 9); 718 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_TOKYO), test); 719 } 720 721 @SuppressWarnings("deprecation") 722 public void testToDateTimeAtMidnight_nullZone() { 723 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 724 725 DateTime test = base.toDateTimeAtMidnight((DateTimeZone) null); 726 check(base, 2005, 6, 9); 727 assertEquals(new DateTime(2005, 6, 9, 0, 0, 0, 0, COPTIC_LONDON), test); 728 } 729 730 //----------------------------------------------------------------------- 731 public void testToDateTimeAtCurrentTime() { 732 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 733 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 734 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 735 736 DateTime test = base.toDateTimeAtCurrentTime(); 737 check(base, 2005, 6, 9); 738 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 739 expected = expected.year().setCopy(2005); 740 expected = expected.monthOfYear().setCopy(6); 741 expected = expected.dayOfMonth().setCopy(9); 742 assertEquals(expected, test); 743 } 744 745 //----------------------------------------------------------------------- 746 public void testToDateTimeAtCurrentTime_Zone() { 747 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 748 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 749 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 750 751 DateTime test = base.toDateTimeAtCurrentTime(TOKYO); 752 check(base, 2005, 6, 9); 753 DateTime expected = new DateTime(dt.getMillis(), COPTIC_TOKYO); 754 expected = expected.year().setCopy(2005); 755 expected = expected.monthOfYear().setCopy(6); 756 expected = expected.dayOfMonth().setCopy(9); 757 assertEquals(expected, test); 758 } 759 760 public void testToDateTimeAtCurrentTime_nullZone() { 761 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 762 DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 763 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 764 765 DateTime test = base.toDateTimeAtCurrentTime((DateTimeZone) null); 766 check(base, 2005, 6, 9); 767 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 768 expected = expected.year().setCopy(2005); 769 expected = expected.monthOfYear().setCopy(6); 770 expected = expected.dayOfMonth().setCopy(9); 771 assertEquals(expected, test); 772 } 773 774 //----------------------------------------------------------------------- 775 public void testToLocalDateTime_LocalTime() { 776 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 777 LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO); 778 779 LocalDateTime test = base.toLocalDateTime(tod); 780 check(base, 2005, 6, 9); 781 LocalDateTime expected = new LocalDateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_UTC); 782 assertEquals(expected, test); 783 } 784 785 public void testToLocalDateTime_nullLocalTime() { 786 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 787 788 try { 789 base.toLocalDateTime((LocalTime) null); 790 fail(); 791 } catch (IllegalArgumentException ex) { 792 // expected 793 } 794 } 795 796 public void testToLocalDateTime_wrongChronologyLocalTime() { 797 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 798 LocalTime tod = new LocalTime(12, 13, 14, 15, BUDDHIST_PARIS); // PARIS irrelevant 799 800 try { 801 base.toLocalDateTime(tod); 802 fail(); 803 } catch (IllegalArgumentException ex) { 804 // expected 805 } 806 } 807 808 //----------------------------------------------------------------------- 809 public void testToDateTime_LocalTime() { 810 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 811 LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO); 812 813 DateTime test = base.toDateTime(tod); 814 check(base, 2005, 6, 9); 815 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 816 assertEquals(expected, test); 817 } 818 819 public void testToDateTime_nullLocalTime() { 820 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 821 long now = new DateTime(2004, 5, 8, 12, 13, 14, 15, COPTIC_LONDON).getMillis(); 822 DateTimeUtils.setCurrentMillisFixed(now); 823 824 DateTime test = base.toDateTime((LocalTime) null); 825 check(base, 2005, 6, 9); 826 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 827 assertEquals(expected, test); 828 } 829 830 //----------------------------------------------------------------------- 831 public void testToDateTime_LocalTime_Zone() { 832 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 833 LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO); 834 835 DateTime test = base.toDateTime(tod, TOKYO); 836 check(base, 2005, 6, 9); 837 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_TOKYO); 838 assertEquals(expected, test); 839 } 840 841 public void testToDateTime_LocalTime_nullZone() { 842 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 843 LocalTime tod = new LocalTime(12, 13, 14, 15, COPTIC_TOKYO); 844 845 DateTime test = base.toDateTime(tod, null); 846 check(base, 2005, 6, 9); 847 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_LONDON); 848 assertEquals(expected, test); 849 } 850 851 public void testToDateTime_nullLocalTime_Zone() { 852 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 853 long now = new DateTime(2004, 5, 8, 12, 13, 14, 15, COPTIC_TOKYO).getMillis(); 854 DateTimeUtils.setCurrentMillisFixed(now); 855 856 DateTime test = base.toDateTime((LocalTime) null, TOKYO); 857 check(base, 2005, 6, 9); 858 DateTime expected = new DateTime(2005, 6, 9, 12, 13, 14, 15, COPTIC_TOKYO); 859 assertEquals(expected, test); 860 } 861 862 public void testToDateTime_wrongChronoLocalTime_Zone() { 863 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 864 LocalTime tod = new LocalTime(12, 13, 14, 15, BUDDHIST_TOKYO); 865 866 try { 867 base.toDateTime(tod, LONDON); 868 fail(); 869 } catch (IllegalArgumentException ex) {} 870 } 871 872 //----------------------------------------------------------------------- 873 public void testToDateMidnight() { 874 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 875 876 DateMidnight test = base.toDateMidnight(); 877 check(base, 2005, 6, 9); 878 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test); 879 } 880 881 //----------------------------------------------------------------------- 882 public void testToDateMidnight_Zone() { 883 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 884 885 DateMidnight test = base.toDateMidnight(TOKYO); 886 check(base, 2005, 6, 9); 887 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_TOKYO), test); 888 } 889 890 public void testToDateMidnight_nullZone() { 891 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 892 893 DateMidnight test = base.toDateMidnight((DateTimeZone) null); 894 check(base, 2005, 6, 9); 895 assertEquals(new DateMidnight(2005, 6, 9, COPTIC_LONDON), test); 896 } 897 898 //----------------------------------------------------------------------- 899 public void testToDateTime_RI() { 900 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); 901 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7); 902 903 DateTime test = base.toDateTime(dt); 904 check(base, 2005, 6, 9); 905 DateTime expected = dt; 906 expected = expected.year().setCopy(2005); 907 expected = expected.monthOfYear().setCopy(6); 908 expected = expected.dayOfMonth().setCopy(9); 909 assertEquals(expected, test); 910 } 911 912 public void testToDateTime_nullRI() { 913 LocalDate base = new LocalDate(2005, 6, 9); 914 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7); 915 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 916 917 DateTime test = base.toDateTime((ReadableInstant) null); 918 check(base, 2005, 6, 9); 919 DateTime expected = dt; 920 expected = expected.year().setCopy(2005); 921 expected = expected.monthOfYear().setCopy(6); 922 expected = expected.dayOfMonth().setCopy(9); 923 assertEquals(expected, test); 924 } 925 926 //----------------------------------------------------------------------- 927 public void testToInterval() { 928 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 929 Interval test = base.toInterval(); 930 check(base, 2005, 6, 9); 931 DateTime start = base.toDateTimeAtStartOfDay(); 932 DateTime end = start.plus(Period.days(1)); 933 Interval expected = new Interval(start, end); 934 assertEquals(expected, test); 935 } 936 937 //----------------------------------------------------------------------- 938 public void testToInterval_Zone() { 939 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 940 Interval test = base.toInterval(TOKYO); 941 check(base, 2005, 6, 9); 942 DateTime start = base.toDateTimeAtStartOfDay(TOKYO); 943 DateTime end = start.plus(Period.days(1)); 944 Interval expected = new Interval(start, end); 945 assertEquals(expected, test); 946 } 947 948 public void testToInterval_Zone_noMidnight() { 949 LocalDate base = new LocalDate(2006, 4, 1, ISO_LONDON); // LONDON irrelevant 950 DateTimeZone gaza = DateTimeZone.forID("Asia/Gaza"); 951 Interval test = base.toInterval(gaza); 952 check(base, 2006, 4, 1); 953 DateTime start = new DateTime(2006, 4, 1, 1, 0, 0, 0, gaza); 954 DateTime end = new DateTime(2006, 4, 2, 0, 0, 0, 0, gaza); 955 Interval expected = new Interval(start, end); 956 assertEquals(expected, test); 957 } 958 959 public void testToInterval_nullZone() { 960 LocalDate base = new LocalDate(2005, 6, 9, COPTIC_PARIS); // PARIS irrelevant 961 Interval test = base.toInterval(null); 962 check(base, 2005, 6, 9); 963 DateTime start = base.toDateTimeAtStartOfDay(LONDON); 964 DateTime end = start.plus(Period.days(1)); 965 Interval expected = new Interval(start, end); 966 assertEquals(expected, test); 967 } 968 969 //----------------------------------------------------------------------- 970 public void testToDate_summer() { 971 LocalDate base = new LocalDate(2005, 7, 9, COPTIC_PARIS); 972 973 Date test = base.toDate(); 974 check(base, 2005, 7, 9); 975 976 GregorianCalendar gcal = new GregorianCalendar(); 977 gcal.clear(); 978 gcal.set(Calendar.YEAR, 2005); 979 gcal.set(Calendar.MONTH, Calendar.JULY); 980 gcal.set(Calendar.DAY_OF_MONTH, 9); 981 assertEquals(gcal.getTime(), test); 982 } 983 984 public void testToDate_winter() { 985 LocalDate base = new LocalDate(2005, 1, 9, COPTIC_PARIS); 986 987 Date test = base.toDate(); 988 check(base, 2005, 1, 9); 989 990 GregorianCalendar gcal = new GregorianCalendar(); 991 gcal.clear(); 992 gcal.set(Calendar.YEAR, 2005); 993 gcal.set(Calendar.MONTH, Calendar.JANUARY); 994 gcal.set(Calendar.DAY_OF_MONTH, 9); 995 assertEquals(gcal.getTime(), test); 996 } 997 998 public void testToDate_springDST() { 999 LocalDate base = new LocalDate(2007, 4, 2); 1000 1001 SimpleTimeZone testZone = new SimpleTimeZone(3600000, "NoMidnight", 1002 Calendar.APRIL, 2, 0, 0, Calendar.OCTOBER, 2, 0, 3600000); 1003 TimeZone currentZone = TimeZone.getDefault(); 1004 try { 1005 TimeZone.setDefault(testZone); 1006 Date test = base.toDate(); 1007 check(base, 2007, 4, 2); 1008 assertEquals("Mon Apr 02 01:00:00 GMT+02:00 2007", test.toString()); 1009 } finally { 1010 TimeZone.setDefault(currentZone); 1011 } 1012 } 1013 1014 public void testToDate_springDST_2Hour40Savings() { 1015 LocalDate base = new LocalDate(2007, 4, 2); 1016 1017 SimpleTimeZone testZone = new SimpleTimeZone(3600000, "NoMidnight", 1018 Calendar.APRIL, 2, 0, 0, Calendar.OCTOBER, 2, 0, 3600000, (3600000 / 6) * 16); 1019 TimeZone currentZone = TimeZone.getDefault(); 1020 try { 1021 TimeZone.setDefault(testZone); 1022 Date test = base.toDate(); 1023 check(base, 2007, 4, 2); 1024 assertEquals("Mon Apr 02 02:40:00 GMT+03:40 2007", test.toString()); 1025 } finally { 1026 TimeZone.setDefault(currentZone); 1027 } 1028 } 1029 1030 public void testToDate_autumnDST() { 1031 LocalDate base = new LocalDate(2007, 10, 2); 1032 1033 SimpleTimeZone testZone = new SimpleTimeZone(3600000, "NoMidnight", 1034 Calendar.APRIL, 2, 0, 0, Calendar.OCTOBER, 2, 0, 3600000); 1035 TimeZone currentZone = TimeZone.getDefault(); 1036 try { 1037 TimeZone.setDefault(testZone); 1038 Date test = base.toDate(); 1039 check(base, 2007, 10, 2); 1040 assertEquals("Tue Oct 02 00:00:00 GMT+02:00 2007", test.toString()); 1041 } finally { 1042 TimeZone.setDefault(currentZone); 1043 } 1044 } 1045 1046 //----------------------------------------------------------------------- 1047 public void testProperty() { 1048 LocalDate test = new LocalDate(2005, 6, 9, GJ_UTC); 1049 assertEquals(test.year(), test.property(DateTimeFieldType.year())); 1050 assertEquals(test.monthOfYear(), test.property(DateTimeFieldType.monthOfYear())); 1051 assertEquals(test.dayOfMonth(), test.property(DateTimeFieldType.dayOfMonth())); 1052 assertEquals(test.dayOfWeek(), test.property(DateTimeFieldType.dayOfWeek())); 1053 assertEquals(test.dayOfYear(), test.property(DateTimeFieldType.dayOfYear())); 1054 assertEquals(test.weekOfWeekyear(), test.property(DateTimeFieldType.weekOfWeekyear())); 1055 assertEquals(test.weekyear(), test.property(DateTimeFieldType.weekyear())); 1056 assertEquals(test.yearOfCentury(), test.property(DateTimeFieldType.yearOfCentury())); 1057 assertEquals(test.yearOfEra(), test.property(DateTimeFieldType.yearOfEra())); 1058 assertEquals(test.centuryOfEra(), test.property(DateTimeFieldType.centuryOfEra())); 1059 assertEquals(test.era(), test.property(DateTimeFieldType.era())); 1060 try { 1061 test.property(DateTimeFieldType.millisOfDay()); 1062 fail(); 1063 } catch (IllegalArgumentException ex) {} 1064 try { 1065 test.property(null); 1066 fail(); 1067 } catch (IllegalArgumentException ex) {} 1068 } 1069 1070 //----------------------------------------------------------------------- 1071 public void testSerialization() throws Exception { 1072 LocalDate test = new LocalDate(1972, 6, 9, COPTIC_PARIS); 1073 1074 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1075 ObjectOutputStream oos = new ObjectOutputStream(baos); 1076 oos.writeObject(test); 1077 byte[] bytes = baos.toByteArray(); 1078 oos.close(); 1079 1080 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 1081 ObjectInputStream ois = new ObjectInputStream(bais); 1082 LocalDate result = (LocalDate) ois.readObject(); 1083 ois.close(); 1084 1085 assertEquals(test, result); 1086 assertTrue(Arrays.equals(test.getValues(), result.getValues())); 1087 assertTrue(Arrays.equals(test.getFields(), result.getFields())); 1088 assertEquals(test.getChronology(), result.getChronology()); 1089 } 1090 1091 //----------------------------------------------------------------------- 1092 public void testToString() { 1093 LocalDate test = new LocalDate(2002, 6, 9); 1094 assertEquals("2002-06-09", test.toString()); 1095 } 1096 1097 //----------------------------------------------------------------------- 1098 public void testToString_String() { 1099 LocalDate test = new LocalDate(2002, 6, 9); 1100 assertEquals("2002 \ufffd\ufffd", test.toString("yyyy HH")); 1101 assertEquals("2002-06-09", test.toString((String) null)); 1102 } 1103 1104 //----------------------------------------------------------------------- 1105 public void testToString_String_Locale() { 1106 LocalDate test = new LocalDate(1970, 6, 9); 1107 assertEquals("Tue 9/6", test.toString("EEE d/M", Locale.ENGLISH)); 1108 assertEquals("mar. 9/6", test.toString("EEE d/M", Locale.FRENCH)); 1109 assertEquals("1970-06-09", test.toString(null, Locale.ENGLISH)); 1110 assertEquals("Tue 9/6", test.toString("EEE d/M", null)); 1111 assertEquals("1970-06-09", test.toString(null, null)); 1112 } 1113 1114 //----------------------------------------------------------------------- 1115 public void testToString_DTFormatter() { 1116 LocalDate test = new LocalDate(2002, 6, 9); 1117 assertEquals("2002 \ufffd\ufffd", test.toString(DateTimeFormat.forPattern("yyyy HH"))); 1118 assertEquals("2002-06-09", test.toString((DateTimeFormatter) null)); 1119 } 1120 1121 //----------------------------------------------------------------------- 1122 private void check(LocalDate test, int hour, int min, int sec) { 1123 assertEquals(hour, test.getYear()); 1124 assertEquals(min, test.getMonthOfYear()); 1125 assertEquals(sec, test.getDayOfMonth()); 1126 } 1127 }