1 16 package org.joda.time; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.util.Arrays ; 23 import java.util.Date ; 24 import java.util.Locale ; 25 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import org.joda.time.chrono.BuddhistChronology; 30 import org.joda.time.chrono.CopticChronology; 31 import org.joda.time.chrono.GregorianChronology; 32 import org.joda.time.chrono.ISOChronology; 33 import org.joda.time.format.DateTimeFormat; 34 import org.joda.time.format.DateTimeFormatter; 35 36 41 public class TestTimeOfDay_Basics extends TestCase { 42 43 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 44 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 45 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 46 private static final int OFFSET = 1; 47 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS); 48 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON); 49 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO); 50 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 51 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 52 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON); 53 private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO); 54 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 55 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS); 56 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON); 57 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO); 58 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC(); 59 60 private long TEST_TIME_NOW = 61 10L * DateTimeConstants.MILLIS_PER_HOUR 62 + 20L * DateTimeConstants.MILLIS_PER_MINUTE 63 + 30L * DateTimeConstants.MILLIS_PER_SECOND 64 + 40L; 65 66 private long TEST_TIME1 = 67 1L * DateTimeConstants.MILLIS_PER_HOUR 68 + 2L * DateTimeConstants.MILLIS_PER_MINUTE 69 + 3L * DateTimeConstants.MILLIS_PER_SECOND 70 + 4L; 71 72 private long TEST_TIME2 = 73 1L * DateTimeConstants.MILLIS_PER_DAY 74 + 5L * DateTimeConstants.MILLIS_PER_HOUR 75 + 6L * DateTimeConstants.MILLIS_PER_MINUTE 76 + 7L * DateTimeConstants.MILLIS_PER_SECOND 77 + 8L; 78 79 private DateTimeZone zone = null; 80 81 public static void main(String [] args) { 82 junit.textui.TestRunner.run(suite()); 83 } 84 85 public static TestSuite suite() { 86 return new TestSuite(TestTimeOfDay_Basics.class); 87 } 88 89 public TestTimeOfDay_Basics(String name) { 90 super(name); 91 } 92 93 protected void setUp() throws Exception { 94 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 95 zone = DateTimeZone.getDefault(); 96 DateTimeZone.setDefault(LONDON); 97 } 98 99 protected void tearDown() throws Exception { 100 DateTimeUtils.setCurrentMillisSystem(); 101 DateTimeZone.setDefault(zone); 102 zone = null; 103 } 104 105 public void testGet() { 107 TimeOfDay test = new TimeOfDay(); 108 assertEquals(10 + OFFSET, test.get(DateTimeFieldType.hourOfDay())); 109 assertEquals(20, test.get(DateTimeFieldType.minuteOfHour())); 110 assertEquals(30, test.get(DateTimeFieldType.secondOfMinute())); 111 assertEquals(40, test.get(DateTimeFieldType.millisOfSecond())); 112 try { 113 test.get(null); 114 fail(); 115 } catch (IllegalArgumentException ex) {} 116 try { 117 test.get(DateTimeFieldType.dayOfMonth()); 118 fail(); 119 } catch (IllegalArgumentException ex) {} 120 } 121 122 public void testSize() { 123 TimeOfDay test = new TimeOfDay(); 124 assertEquals(4, test.size()); 125 } 126 127 public void testGetFieldType() { 128 TimeOfDay test = new TimeOfDay(COPTIC_PARIS); 129 assertSame(DateTimeFieldType.hourOfDay(), test.getFieldType(0)); 130 assertSame(DateTimeFieldType.minuteOfHour(), test.getFieldType(1)); 131 assertSame(DateTimeFieldType.secondOfMinute(), test.getFieldType(2)); 132 assertSame(DateTimeFieldType.millisOfSecond(), test.getFieldType(3)); 133 try { 134 test.getFieldType(-1); 135 } catch (IndexOutOfBoundsException ex) {} 136 try { 137 test.getFieldType(5); 138 } catch (IndexOutOfBoundsException ex) {} 139 } 140 141 public void testGetFieldTypes() { 142 TimeOfDay test = new TimeOfDay(COPTIC_PARIS); 143 DateTimeFieldType[] fields = test.getFieldTypes(); 144 assertSame(DateTimeFieldType.hourOfDay(), fields[0]); 145 assertSame(DateTimeFieldType.minuteOfHour(), fields[1]); 146 assertSame(DateTimeFieldType.secondOfMinute(), fields[2]); 147 assertSame(DateTimeFieldType.millisOfSecond(), fields[3]); 148 assertNotSame(test.getFieldTypes(), test.getFieldTypes()); 149 } 150 151 public void testGetField() { 152 TimeOfDay test = new TimeOfDay(COPTIC_PARIS); 153 assertSame(CopticChronology.getInstanceUTC().hourOfDay(), test.getField(0)); 154 assertSame(CopticChronology.getInstanceUTC().minuteOfHour(), test.getField(1)); 155 assertSame(CopticChronology.getInstanceUTC().secondOfMinute(), test.getField(2)); 156 assertSame(CopticChronology.getInstanceUTC().millisOfSecond(), test.getField(3)); 157 try { 158 test.getField(-1); 159 } catch (IndexOutOfBoundsException ex) {} 160 try { 161 test.getField(5); 162 } catch (IndexOutOfBoundsException ex) {} 163 } 164 165 public void testGetFields() { 166 TimeOfDay test = new TimeOfDay(COPTIC_PARIS); 167 DateTimeField[] fields = test.getFields(); 168 assertSame(CopticChronology.getInstanceUTC().hourOfDay(), fields[0]); 169 assertSame(CopticChronology.getInstanceUTC().minuteOfHour(), fields[1]); 170 assertSame(CopticChronology.getInstanceUTC().secondOfMinute(), fields[2]); 171 assertSame(CopticChronology.getInstanceUTC().millisOfSecond(), fields[3]); 172 assertNotSame(test.getFields(), test.getFields()); 173 } 174 175 public void testGetValue() { 176 TimeOfDay test = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); 177 assertEquals(10, test.getValue(0)); 178 assertEquals(20, test.getValue(1)); 179 assertEquals(30, test.getValue(2)); 180 assertEquals(40, test.getValue(3)); 181 try { 182 test.getValue(-1); 183 } catch (IndexOutOfBoundsException ex) {} 184 try { 185 test.getValue(5); 186 } catch (IndexOutOfBoundsException ex) {} 187 } 188 189 public void testGetValues() { 190 TimeOfDay test = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); 191 int[] values = test.getValues(); 192 assertEquals(10, values[0]); 193 assertEquals(20, values[1]); 194 assertEquals(30, values[2]); 195 assertEquals(40, values[3]); 196 assertNotSame(test.getValues(), test.getValues()); 197 } 198 199 public void testIsSupported() { 200 TimeOfDay test = new TimeOfDay(COPTIC_PARIS); 201 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfDay())); 202 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfHour())); 203 assertEquals(true, test.isSupported(DateTimeFieldType.secondOfMinute())); 204 assertEquals(true, test.isSupported(DateTimeFieldType.millisOfSecond())); 205 assertEquals(false, test.isSupported(DateTimeFieldType.dayOfMonth())); 206 } 207 208 public void testEqualsHashCode() { 209 TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); 210 TimeOfDay test2 = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); 211 assertEquals(true, test1.equals(test2)); 212 assertEquals(true, test2.equals(test1)); 213 assertEquals(true, test1.equals(test1)); 214 assertEquals(true, test2.equals(test2)); 215 assertEquals(true, test1.hashCode() == test2.hashCode()); 216 assertEquals(true, test1.hashCode() == test1.hashCode()); 217 assertEquals(true, test2.hashCode() == test2.hashCode()); 218 219 TimeOfDay test3 = new TimeOfDay(15, 20, 30, 40); 220 assertEquals(false, test1.equals(test3)); 221 assertEquals(false, test2.equals(test3)); 222 assertEquals(false, test3.equals(test1)); 223 assertEquals(false, test3.equals(test2)); 224 assertEquals(false, test1.hashCode() == test3.hashCode()); 225 assertEquals(false, test2.hashCode() == test3.hashCode()); 226 227 assertEquals(false, test1.equals("Hello")); 228 assertEquals(true, test1.equals(new MockInstant())); 229 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE)); 230 } 231 232 class MockInstant extends MockPartial { 233 public Chronology getChronology() { 234 return CopticChronology.getInstanceUTC(); 235 } 236 public DateTimeField[] getFields() { 237 return new DateTimeField[] { 238 CopticChronology.getInstanceUTC().hourOfDay(), 239 CopticChronology.getInstanceUTC().minuteOfHour(), 240 CopticChronology.getInstanceUTC().secondOfMinute(), 241 CopticChronology.getInstanceUTC().millisOfSecond(), 242 }; 243 } 244 public int[] getValues() { 245 return new int[] {10, 20, 30, 40}; 246 } 247 } 248 249 public void testCompareTo() { 251 TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40); 252 TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40); 253 assertEquals(0, test1.compareTo(test1a)); 254 assertEquals(0, test1a.compareTo(test1)); 255 assertEquals(0, test1.compareTo(test1)); 256 assertEquals(0, test1a.compareTo(test1a)); 257 258 TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40); 259 assertEquals(-1, test1.compareTo(test2)); 260 assertEquals(+1, test2.compareTo(test1)); 261 262 TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); 263 assertEquals(-1, test1.compareTo(test3)); 264 assertEquals(+1, test3.compareTo(test1)); 265 assertEquals(0, test3.compareTo(test2)); 266 267 DateTimeFieldType[] types = new DateTimeFieldType[] { 268 DateTimeFieldType.hourOfDay(), 269 DateTimeFieldType.minuteOfHour(), 270 DateTimeFieldType.secondOfMinute(), 271 DateTimeFieldType.millisOfSecond(), 272 }; 273 int[] values = new int[] {10, 20, 30, 40}; 274 Partial p = new Partial(types, values); 275 assertEquals(0, test1.compareTo(p)); 276 try { 277 test1.compareTo(null); 278 fail(); 279 } catch (NullPointerException ex) {} 280 try { 281 test1.compareTo(new Date ()); 282 fail(); 283 } catch (ClassCastException ex) {} 284 } 285 286 public void testIsEqual_TOD() { 288 TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40); 289 TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40); 290 assertEquals(true, test1.isEqual(test1a)); 291 assertEquals(true, test1a.isEqual(test1)); 292 assertEquals(true, test1.isEqual(test1)); 293 assertEquals(true, test1a.isEqual(test1a)); 294 295 TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40); 296 assertEquals(false, test1.isEqual(test2)); 297 assertEquals(false, test2.isEqual(test1)); 298 299 TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); 300 assertEquals(false, test1.isEqual(test3)); 301 assertEquals(false, test3.isEqual(test1)); 302 assertEquals(true, test3.isEqual(test2)); 303 304 try { 305 new TimeOfDay(10, 20, 35, 40).isEqual(null); 306 fail(); 307 } catch (IllegalArgumentException ex) {} 308 } 309 310 public void testIsBefore_TOD() { 312 TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40); 313 TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40); 314 assertEquals(false, test1.isBefore(test1a)); 315 assertEquals(false, test1a.isBefore(test1)); 316 assertEquals(false, test1.isBefore(test1)); 317 assertEquals(false, test1a.isBefore(test1a)); 318 319 TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40); 320 assertEquals(true, test1.isBefore(test2)); 321 assertEquals(false, test2.isBefore(test1)); 322 323 TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); 324 assertEquals(true, test1.isBefore(test3)); 325 assertEquals(false, test3.isBefore(test1)); 326 assertEquals(false, test3.isBefore(test2)); 327 328 try { 329 new TimeOfDay(10, 20, 35, 40).isBefore(null); 330 fail(); 331 } catch (IllegalArgumentException ex) {} 332 } 333 334 public void testIsAfter_TOD() { 336 TimeOfDay test1 = new TimeOfDay(10, 20, 30, 40); 337 TimeOfDay test1a = new TimeOfDay(10, 20, 30, 40); 338 assertEquals(false, test1.isAfter(test1a)); 339 assertEquals(false, test1a.isAfter(test1)); 340 assertEquals(false, test1.isAfter(test1)); 341 assertEquals(false, test1a.isAfter(test1a)); 342 343 TimeOfDay test2 = new TimeOfDay(10, 20, 35, 40); 344 assertEquals(false, test1.isAfter(test2)); 345 assertEquals(true, test2.isAfter(test1)); 346 347 TimeOfDay test3 = new TimeOfDay(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); 348 assertEquals(false, test1.isAfter(test3)); 349 assertEquals(true, test3.isAfter(test1)); 350 assertEquals(false, test3.isAfter(test2)); 351 352 try { 353 new TimeOfDay(10, 20, 35, 40).isAfter(null); 354 fail(); 355 } catch (IllegalArgumentException ex) {} 356 } 357 358 public void testWithChronologyRetainFields_Chrono() { 360 TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); 361 TimeOfDay test = base.withChronologyRetainFields(BUDDHIST_TOKYO); 362 check(base, 10, 20, 30, 40); 363 assertEquals(COPTIC_UTC, base.getChronology()); 364 check(test, 10, 20, 30, 40); 365 assertEquals(BUDDHIST_UTC, test.getChronology()); 366 } 367 368 public void testWithChronologyRetainFields_sameChrono() { 369 TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); 370 TimeOfDay test = base.withChronologyRetainFields(COPTIC_TOKYO); 371 assertSame(base, test); 372 } 373 374 public void testWithChronologyRetainFields_nullChrono() { 375 TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); 376 TimeOfDay test = base.withChronologyRetainFields(null); 377 check(base, 10, 20, 30, 40); 378 assertEquals(COPTIC_UTC, base.getChronology()); 379 check(test, 10, 20, 30, 40); 380 assertEquals(ISO_UTC, test.getChronology()); 381 } 382 383 public void testWithField1() { 385 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 386 TimeOfDay result = test.withField(DateTimeFieldType.hourOfDay(), 15); 387 388 assertEquals(new TimeOfDay(10, 20, 30, 40), test); 389 assertEquals(new TimeOfDay(15, 20, 30, 40), result); 390 } 391 392 public void testWithField2() { 393 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 394 try { 395 test.withField(null, 6); 396 fail(); 397 } catch (IllegalArgumentException ex) {} 398 } 399 400 public void testWithField3() { 401 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 402 try { 403 test.withField(DateTimeFieldType.dayOfMonth(), 6); 404 fail(); 405 } catch (IllegalArgumentException ex) {} 406 } 407 408 public void testWithField4() { 409 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 410 TimeOfDay result = test.withField(DateTimeFieldType.hourOfDay(), 10); 411 assertSame(test, result); 412 } 413 414 public void testWithFieldAdded1() { 416 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 417 TimeOfDay result = test.withFieldAdded(DurationFieldType.hours(), 6); 418 419 assertEquals(new TimeOfDay(10, 20, 30, 40), test); 420 assertEquals(new TimeOfDay(16, 20, 30, 40), result); 421 } 422 423 public void testWithFieldAdded2() { 424 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 425 try { 426 test.withFieldAdded(null, 0); 427 fail(); 428 } catch (IllegalArgumentException ex) {} 429 } 430 431 public void testWithFieldAdded3() { 432 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 433 try { 434 test.withFieldAdded(null, 6); 435 fail(); 436 } catch (IllegalArgumentException ex) {} 437 } 438 439 public void testWithFieldAdded4() { 440 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 441 TimeOfDay result = test.withFieldAdded(DurationFieldType.hours(), 0); 442 assertSame(test, result); 443 } 444 445 public void testWithFieldAdded5() { 446 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 447 try { 448 test.withFieldAdded(DurationFieldType.days(), 6); 449 fail(); 450 } catch (IllegalArgumentException ex) {} 451 } 452 453 public void testWithFieldAdded6() { 454 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 455 TimeOfDay result = test.withFieldAdded(DurationFieldType.hours(), 16); 456 457 assertEquals(new TimeOfDay(10, 20, 30, 40), test); 458 assertEquals(new TimeOfDay(2, 20, 30, 40), result); 459 } 460 461 public void testWithFieldAdded7() { 462 TimeOfDay test = new TimeOfDay(23, 59, 59, 999); 463 TimeOfDay result = test.withFieldAdded(DurationFieldType.millis(), 1); 464 assertEquals(new TimeOfDay(0, 0, 0, 0), result); 465 466 test = new TimeOfDay(23, 59, 59, 999); 467 result = test.withFieldAdded(DurationFieldType.seconds(), 1); 468 assertEquals(new TimeOfDay(0, 0, 0, 999), result); 469 470 test = new TimeOfDay(23, 59, 59, 999); 471 result = test.withFieldAdded(DurationFieldType.minutes(), 1); 472 assertEquals(new TimeOfDay(0, 0, 59, 999), result); 473 474 test = new TimeOfDay(23, 59, 59, 999); 475 result = test.withFieldAdded(DurationFieldType.hours(), 1); 476 assertEquals(new TimeOfDay(0, 59, 59, 999), result); 477 } 478 479 public void testWithFieldAdded8() { 480 TimeOfDay test = new TimeOfDay(0, 0, 0, 0); 481 TimeOfDay result = test.withFieldAdded(DurationFieldType.millis(), -1); 482 assertEquals(new TimeOfDay(23, 59, 59, 999), result); 483 484 test = new TimeOfDay(0, 0, 0, 0); 485 result = test.withFieldAdded(DurationFieldType.seconds(), -1); 486 assertEquals(new TimeOfDay(23, 59, 59, 0), result); 487 488 test = new TimeOfDay(0, 0, 0, 0); 489 result = test.withFieldAdded(DurationFieldType.minutes(), -1); 490 assertEquals(new TimeOfDay(23, 59, 0, 0), result); 491 492 test = new TimeOfDay(0, 0, 0, 0); 493 result = test.withFieldAdded(DurationFieldType.hours(), -1); 494 assertEquals(new TimeOfDay(23, 0, 0, 0), result); 495 } 496 497 public void testPlus_RP() { 499 TimeOfDay test = new TimeOfDay(10, 20, 30, 40, BuddhistChronology.getInstance()); 500 TimeOfDay result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7, 8)); 501 TimeOfDay expected = new TimeOfDay(15, 26, 37, 48, BuddhistChronology.getInstance()); 502 assertEquals(expected, result); 503 504 result = test.plus((ReadablePeriod) null); 505 assertSame(test, result); 506 } 507 508 public void testPlusHours_int() { 509 TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology.getInstance()); 510 TimeOfDay result = test.plusHours(1); 511 TimeOfDay expected = new TimeOfDay(2, 2, 3, 4, BuddhistChronology.getInstance()); 512 assertEquals(expected, result); 513 514 result = test.plusHours(0); 515 assertSame(test, result); 516 } 517 518 public void testPlusMinutes_int() { 519 TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology.getInstance()); 520 TimeOfDay result = test.plusMinutes(1); 521 TimeOfDay expected = new TimeOfDay(1, 3, 3, 4, BuddhistChronology.getInstance()); 522 assertEquals(expected, result); 523 524 result = test.plusMinutes(0); 525 assertSame(test, result); 526 } 527 528 public void testPlusSeconds_int() { 529 TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology.getInstance()); 530 TimeOfDay result = test.plusSeconds(1); 531 TimeOfDay expected = new TimeOfDay(1, 2, 4, 4, BuddhistChronology.getInstance()); 532 assertEquals(expected, result); 533 534 result = test.plusSeconds(0); 535 assertSame(test, result); 536 } 537 538 public void testPlusMillis_int() { 539 TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology.getInstance()); 540 TimeOfDay result = test.plusMillis(1); 541 TimeOfDay expected = new TimeOfDay(1, 2, 3, 5, BuddhistChronology.getInstance()); 542 assertEquals(expected, result); 543 544 result = test.plusMillis(0); 545 assertSame(test, result); 546 } 547 548 public void testMinus_RP() { 550 TimeOfDay test = new TimeOfDay(10, 20, 30, 40, BuddhistChronology.getInstance()); 551 TimeOfDay result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1)); 552 TimeOfDay expected = new TimeOfDay(9, 19, 29, 39, BuddhistChronology.getInstance()); 553 assertEquals(expected, result); 554 555 result = test.minus((ReadablePeriod) null); 556 assertSame(test, result); 557 } 558 559 public void testMinusHours_int() { 560 TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology.getInstance()); 561 TimeOfDay result = test.minusHours(1); 562 TimeOfDay expected = new TimeOfDay(0, 2, 3, 4, BuddhistChronology.getInstance()); 563 assertEquals(expected, result); 564 565 result = test.minusHours(0); 566 assertSame(test, result); 567 } 568 569 public void testMinusMinutes_int() { 570 TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology.getInstance()); 571 TimeOfDay result = test.minusMinutes(1); 572 TimeOfDay expected = new TimeOfDay(1, 1, 3, 4, BuddhistChronology.getInstance()); 573 assertEquals(expected, result); 574 575 result = test.minusMinutes(0); 576 assertSame(test, result); 577 } 578 579 public void testMinusSeconds_int() { 580 TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology.getInstance()); 581 TimeOfDay result = test.minusSeconds(1); 582 TimeOfDay expected = new TimeOfDay(1, 2, 2, 4, BuddhistChronology.getInstance()); 583 assertEquals(expected, result); 584 585 result = test.minusSeconds(0); 586 assertSame(test, result); 587 } 588 589 public void testMinusMillis_int() { 590 TimeOfDay test = new TimeOfDay(1, 2, 3, 4, BuddhistChronology.getInstance()); 591 TimeOfDay result = test.minusMillis(1); 592 TimeOfDay expected = new TimeOfDay(1, 2, 3, 3, BuddhistChronology.getInstance()); 593 assertEquals(expected, result); 594 595 result = test.minusMillis(0); 596 assertSame(test, result); 597 } 598 599 public void testToLocalTime() { 601 TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_UTC); 602 LocalTime test = base.toLocalTime(); 603 assertEquals(new LocalTime(10, 20, 30, 40, COPTIC_UTC), test); 604 } 605 606 public void testToDateTimeToday() { 608 TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 610 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 611 612 DateTime test = base.toDateTimeToday(); 613 check(base, 10, 20, 30, 40); 614 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 615 expected = expected.hourOfDay().setCopy(10); 616 expected = expected.minuteOfHour().setCopy(20); 617 expected = expected.secondOfMinute().setCopy(30); 618 expected = expected.millisOfSecond().setCopy(40); 619 assertEquals(expected, test); 620 } 621 622 public void testToDateTimeToday_Zone() { 624 TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 626 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 627 628 DateTime test = base.toDateTimeToday(TOKYO); 629 check(base, 10, 20, 30, 40); 630 DateTime expected = new DateTime(dt.getMillis(), COPTIC_TOKYO); 631 expected = expected.hourOfDay().setCopy(10); 632 expected = expected.minuteOfHour().setCopy(20); 633 expected = expected.secondOfMinute().setCopy(30); 634 expected = expected.millisOfSecond().setCopy(40); 635 assertEquals(expected, test); 636 } 637 638 public void testToDateTimeToday_nullZone() { 639 TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 641 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 642 643 DateTime test = base.toDateTimeToday((DateTimeZone) null); 644 check(base, 10, 20, 30, 40); 645 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 646 expected = expected.hourOfDay().setCopy(10); 647 expected = expected.minuteOfHour().setCopy(20); 648 expected = expected.secondOfMinute().setCopy(30); 649 expected = expected.millisOfSecond().setCopy(40); 650 assertEquals(expected, test); 651 } 652 653 805 public void testToDateTime_RI() { 807 TimeOfDay base = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); 808 DateTime dt = new DateTime(0L); assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString()); 810 811 DateTime test = base.toDateTime(dt); 812 check(base, 10, 20, 30, 40); 813 assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString()); 814 assertEquals("1970-01-01T10:20:30.040+01:00", test.toString()); 815 } 816 817 public void testToDateTime_nullRI() { 818 TimeOfDay base = new TimeOfDay(1, 2, 3, 4); 819 DateTimeUtils.setCurrentMillisFixed(TEST_TIME2); 820 821 DateTime test = base.toDateTime((ReadableInstant) null); 822 check(base, 1, 2, 3, 4); 823 assertEquals("1970-01-02T01:02:03.004+01:00", test.toString()); 824 } 825 826 public void testWithers() { 828 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 829 check(test.withHourOfDay(6), 6, 20, 30, 40); 830 check(test.withMinuteOfHour(6), 10, 6, 30, 40); 831 check(test.withSecondOfMinute(6), 10, 20, 6, 40); 832 check(test.withMillisOfSecond(6), 10, 20, 30, 6); 833 try { 834 test.withHourOfDay(-1); 835 fail(); 836 } catch (IllegalArgumentException ex) {} 837 try { 838 test.withHourOfDay(24); 839 fail(); 840 } catch (IllegalArgumentException ex) {} 841 } 842 843 public void testProperty() { 845 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 846 assertEquals(test.hourOfDay(), test.property(DateTimeFieldType.hourOfDay())); 847 assertEquals(test.minuteOfHour(), test.property(DateTimeFieldType.minuteOfHour())); 848 assertEquals(test.secondOfMinute(), test.property(DateTimeFieldType.secondOfMinute())); 849 assertEquals(test.millisOfSecond(), test.property(DateTimeFieldType.millisOfSecond())); 850 try { 851 test.property(DateTimeFieldType.millisOfDay()); 852 fail(); 853 } catch (IllegalArgumentException ex) {} 854 try { 855 test.property(null); 856 fail(); 857 } catch (IllegalArgumentException ex) {} 858 } 859 860 public void testSerialization() throws Exception { 862 TimeOfDay test = new TimeOfDay(10, 20, 30, 40, COPTIC_PARIS); 863 864 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 865 ObjectOutputStream oos = new ObjectOutputStream (baos); 866 oos.writeObject(test); 867 byte[] bytes = baos.toByteArray(); 868 oos.close(); 869 870 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 871 ObjectInputStream ois = new ObjectInputStream (bais); 872 TimeOfDay result = (TimeOfDay) ois.readObject(); 873 ois.close(); 874 875 assertEquals(test, result); 876 assertTrue(Arrays.equals(test.getValues(), result.getValues())); 877 assertTrue(Arrays.equals(test.getFields(), result.getFields())); 878 assertEquals(test.getChronology(), result.getChronology()); 879 } 880 881 public void testToString() { 883 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 884 assertEquals("T10:20:30.040", test.toString()); 885 } 886 887 public void testToString_String() { 889 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 890 assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test.toString("yyyy HH")); 891 assertEquals("T10:20:30.040", test.toString((String ) null)); 892 } 893 894 public void testToString_String_Locale() { 896 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 897 assertEquals("10 20", test.toString("H m", Locale.ENGLISH)); 898 assertEquals("T10:20:30.040", test.toString(null, Locale.ENGLISH)); 899 assertEquals("10 20", test.toString("H m", null)); 900 assertEquals("T10:20:30.040", test.toString(null, null)); 901 } 902 903 public void testToString_DTFormatter() { 905 TimeOfDay test = new TimeOfDay(10, 20, 30, 40); 906 assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test.toString(DateTimeFormat.forPattern("yyyy HH"))); 907 assertEquals("T10:20:30.040", test.toString((DateTimeFormatter) null)); 908 } 909 910 private void check(TimeOfDay test, int hour, int min, int sec, int milli) { 912 assertEquals(hour, test.getHourOfDay()); 913 assertEquals(min, test.getMinuteOfHour()); 914 assertEquals(sec, test.getSecondOfMinute()); 915 assertEquals(milli, test.getMillisOfSecond()); 916 } 917 } 918 | Popular Tags |