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.format.DateTimeFormat; 33 import org.joda.time.format.DateTimeFormatter; 34 35 40 public class TestLocalTime_Basics extends TestCase { 41 42 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 43 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 44 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 45 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS); 46 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON); 47 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO); 48 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 49 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON); 50 51 private long TEST_TIME_NOW = 52 10L * DateTimeConstants.MILLIS_PER_HOUR 53 + 20L * DateTimeConstants.MILLIS_PER_MINUTE 54 + 30L * DateTimeConstants.MILLIS_PER_SECOND 55 + 40L; 56 57 63 private long TEST_TIME2 = 64 1L * DateTimeConstants.MILLIS_PER_DAY 65 + 5L * DateTimeConstants.MILLIS_PER_HOUR 66 + 6L * DateTimeConstants.MILLIS_PER_MINUTE 67 + 7L * DateTimeConstants.MILLIS_PER_SECOND 68 + 8L; 69 70 private DateTimeZone zone = null; 71 72 public static void main(String [] args) { 73 junit.textui.TestRunner.run(suite()); 74 } 75 76 public static TestSuite suite() { 77 return new TestSuite(TestLocalTime_Basics.class); 78 } 79 80 public TestLocalTime_Basics(String name) { 81 super(name); 82 } 83 84 protected void setUp() throws Exception { 85 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 86 zone = DateTimeZone.getDefault(); 87 DateTimeZone.setDefault(LONDON); 88 } 89 90 protected void tearDown() throws Exception { 91 DateTimeUtils.setCurrentMillisSystem(); 92 DateTimeZone.setDefault(zone); 93 zone = null; 94 } 95 96 public void testGet_DateTimeFieldType() { 98 LocalTime test = new LocalTime(10, 20, 30, 40); 99 assertEquals(10, test.get(DateTimeFieldType.hourOfDay())); 100 assertEquals(20, test.get(DateTimeFieldType.minuteOfHour())); 101 assertEquals(30, test.get(DateTimeFieldType.secondOfMinute())); 102 assertEquals(40, test.get(DateTimeFieldType.millisOfSecond())); 103 assertEquals(TEST_TIME_NOW / 60000 , test.get(DateTimeFieldType.minuteOfDay())); 104 assertEquals(TEST_TIME_NOW / 1000 , test.get(DateTimeFieldType.secondOfDay())); 105 assertEquals(TEST_TIME_NOW , test.get(DateTimeFieldType.millisOfDay())); 106 assertEquals(10, test.get(DateTimeFieldType.hourOfHalfday())); 107 assertEquals(DateTimeConstants.AM, test.get(DateTimeFieldType.halfdayOfDay())); 108 test = new LocalTime(12, 30); 109 assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday())); 110 assertEquals(12, test.get(DateTimeFieldType.clockhourOfHalfday())); 111 assertEquals(12, test.get(DateTimeFieldType.clockhourOfDay())); 112 assertEquals(DateTimeConstants.PM, test.get(DateTimeFieldType.halfdayOfDay())); 113 test = new LocalTime(14, 30); 114 assertEquals(2, test.get(DateTimeFieldType.hourOfHalfday())); 115 assertEquals(2, test.get(DateTimeFieldType.clockhourOfHalfday())); 116 assertEquals(14, test.get(DateTimeFieldType.clockhourOfDay())); 117 assertEquals(DateTimeConstants.PM, test.get(DateTimeFieldType.halfdayOfDay())); 118 test = new LocalTime(0, 30); 119 assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday())); 120 assertEquals(12, test.get(DateTimeFieldType.clockhourOfHalfday())); 121 assertEquals(24, test.get(DateTimeFieldType.clockhourOfDay())); 122 assertEquals(DateTimeConstants.AM, test.get(DateTimeFieldType.halfdayOfDay())); 123 try { 124 test.get(null); 125 fail(); 126 } catch (IllegalArgumentException ex) {} 127 try { 128 test.get(DateTimeFieldType.dayOfMonth()); 129 fail(); 130 } catch (IllegalArgumentException ex) {} 131 } 132 133 public void testSize() { 134 LocalTime test = new LocalTime(10, 20, 30, 40); 135 assertEquals(4, test.size()); 136 } 137 138 public void testGetFieldType_int() { 139 LocalTime test = new LocalTime(10, 20, 30, 40); 140 assertSame(DateTimeFieldType.hourOfDay(), test.getFieldType(0)); 141 assertSame(DateTimeFieldType.minuteOfHour(), test.getFieldType(1)); 142 assertSame(DateTimeFieldType.secondOfMinute(), test.getFieldType(2)); 143 assertSame(DateTimeFieldType.millisOfSecond(), test.getFieldType(3)); 144 try { 145 test.getFieldType(-1); 146 } catch (IndexOutOfBoundsException ex) {} 147 try { 148 test.getFieldType(5); 149 } catch (IndexOutOfBoundsException ex) {} 150 } 151 152 public void testGetFieldTypes() { 153 LocalTime test = new LocalTime(10, 20, 30, 40); 154 DateTimeFieldType[] fields = test.getFieldTypes(); 155 assertSame(DateTimeFieldType.hourOfDay(), fields[0]); 156 assertSame(DateTimeFieldType.minuteOfHour(), fields[1]); 157 assertSame(DateTimeFieldType.secondOfMinute(), fields[2]); 158 assertSame(DateTimeFieldType.millisOfSecond(), fields[3]); 159 assertNotSame(test.getFieldTypes(), test.getFieldTypes()); 160 } 161 162 public void testGetField_int() { 163 LocalTime test = new LocalTime(10, 20, 30, 40, COPTIC_UTC); 164 assertSame(COPTIC_UTC.hourOfDay(), test.getField(0)); 165 assertSame(COPTIC_UTC.minuteOfHour(), test.getField(1)); 166 assertSame(COPTIC_UTC.secondOfMinute(), test.getField(2)); 167 assertSame(COPTIC_UTC.millisOfSecond(), test.getField(3)); 168 try { 169 test.getField(-1); 170 } catch (IndexOutOfBoundsException ex) {} 171 try { 172 test.getField(5); 173 } catch (IndexOutOfBoundsException ex) {} 174 } 175 176 public void testGetFields() { 177 LocalTime test = new LocalTime(10, 20, 30, 40, COPTIC_UTC); 178 DateTimeField[] fields = test.getFields(); 179 assertSame(COPTIC_UTC.hourOfDay(), fields[0]); 180 assertSame(COPTIC_UTC.minuteOfHour(), fields[1]); 181 assertSame(COPTIC_UTC.secondOfMinute(), fields[2]); 182 assertSame(COPTIC_UTC.millisOfSecond(), fields[3]); 183 assertNotSame(test.getFields(), test.getFields()); 184 } 185 186 public void testGetValue_int() { 187 LocalTime test = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); 188 assertEquals(10, test.getValue(0)); 189 assertEquals(20, test.getValue(1)); 190 assertEquals(30, test.getValue(2)); 191 assertEquals(40, test.getValue(3)); 192 try { 193 test.getValue(-1); 194 } catch (IndexOutOfBoundsException ex) {} 195 try { 196 test.getValue(5); 197 } catch (IndexOutOfBoundsException ex) {} 198 } 199 200 public void testGetValues() { 201 LocalTime test = new LocalTime(10, 20, 30, 40, COPTIC_UTC); 202 int[] values = test.getValues(); 203 assertEquals(10, values[0]); 204 assertEquals(20, values[1]); 205 assertEquals(30, values[2]); 206 assertEquals(40, values[3]); 207 assertNotSame(test.getValues(), test.getValues()); 208 } 209 210 public void testIsSupported_DateTimeFieldType() { 211 LocalTime test = new LocalTime(10, 20, 30, 40); 212 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfDay())); 213 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfHour())); 214 assertEquals(true, test.isSupported(DateTimeFieldType.secondOfMinute())); 215 assertEquals(true, test.isSupported(DateTimeFieldType.millisOfSecond())); 216 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfDay())); 217 assertEquals(true, test.isSupported(DateTimeFieldType.secondOfDay())); 218 assertEquals(true, test.isSupported(DateTimeFieldType.millisOfDay())); 219 220 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfHalfday())); 221 assertEquals(true, test.isSupported(DateTimeFieldType.halfdayOfDay())); 222 assertEquals(true, test.isSupported(DateTimeFieldType.clockhourOfHalfday())); 223 assertEquals(true, test.isSupported(DateTimeFieldType.clockhourOfDay())); 224 225 assertEquals(false, test.isSupported(DateTimeFieldType.dayOfMonth())); 226 assertEquals(false, test.isSupported((DateTimeFieldType) null)); 227 228 DateTimeFieldType d = new DateTimeFieldType("hours") { 229 public DurationFieldType getDurationType() { 230 return DurationFieldType.hours(); 231 } 232 public DurationFieldType getRangeDurationType() { 233 return null; 234 } 235 public DateTimeField getField(Chronology chronology) { 236 return chronology.hourOfDay(); 237 } 238 }; 239 assertEquals(false, test.isSupported(d)); 240 241 d = new DateTimeFieldType("hourOfYear") { 242 public DurationFieldType getDurationType() { 243 return DurationFieldType.hours(); 244 } 245 public DurationFieldType getRangeDurationType() { 246 return DurationFieldType.years(); 247 } 248 public DateTimeField getField(Chronology chronology) { 249 return chronology.hourOfDay(); 250 } 251 }; 252 assertEquals(false, test.isSupported(d)); 253 } 254 255 public void testIsSupported_DurationFieldType() { 256 LocalTime test = new LocalTime(10, 20, 30, 40); 257 assertEquals(true, test.isSupported(DurationFieldType.hours())); 258 assertEquals(true, test.isSupported(DurationFieldType.minutes())); 259 assertEquals(true, test.isSupported(DurationFieldType.seconds())); 260 assertEquals(true, test.isSupported(DurationFieldType.millis())); 261 assertEquals(true, test.isSupported(DurationFieldType.halfdays())); 262 263 assertEquals(false, test.isSupported(DurationFieldType.days())); 264 assertEquals(false, test.isSupported((DurationFieldType) null)); 265 } 266 267 public void testEqualsHashCode() { 268 LocalTime test1 = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); 269 LocalTime test2 = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); 270 assertEquals(true, test1.equals(test2)); 271 assertEquals(true, test2.equals(test1)); 272 assertEquals(true, test1.equals(test1)); 273 assertEquals(true, test2.equals(test2)); 274 assertEquals(true, test1.hashCode() == test2.hashCode()); 275 assertEquals(true, test1.hashCode() == test1.hashCode()); 276 assertEquals(true, test2.hashCode() == test2.hashCode()); 277 278 LocalTime test3 = new LocalTime(15, 20, 30, 40); 279 assertEquals(false, test1.equals(test3)); 280 assertEquals(false, test2.equals(test3)); 281 assertEquals(false, test3.equals(test1)); 282 assertEquals(false, test3.equals(test2)); 283 assertEquals(false, test1.hashCode() == test3.hashCode()); 284 assertEquals(false, test2.hashCode() == test3.hashCode()); 285 286 assertEquals(false, test1.equals("Hello")); 287 assertEquals(true, test1.equals(new TimeOfDay(10, 20, 30, 40, COPTIC_UTC))); 288 assertEquals(true, test1.equals(new MockInstant())); 289 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE)); 290 } 291 292 class MockInstant extends MockPartial { 293 public Chronology getChronology() { 294 return COPTIC_UTC; 295 } 296 public DateTimeField[] getFields() { 297 return new DateTimeField[] { 298 COPTIC_UTC.hourOfDay(), 299 COPTIC_UTC.minuteOfHour(), 300 COPTIC_UTC.secondOfMinute(), 301 COPTIC_UTC.millisOfSecond(), 302 }; 303 } 304 public int[] getValues() { 305 return new int[] {10, 20, 30, 40}; 306 } 307 } 308 309 public void testCompareTo() { 311 LocalTime test1 = new LocalTime(10, 20, 30, 40); 312 LocalTime test1a = new LocalTime(10, 20, 30, 40); 313 assertEquals(0, test1.compareTo(test1a)); 314 assertEquals(0, test1a.compareTo(test1)); 315 assertEquals(0, test1.compareTo(test1)); 316 assertEquals(0, test1a.compareTo(test1a)); 317 318 LocalTime test2 = new LocalTime(10, 20, 35, 40); 319 assertEquals(-1, test1.compareTo(test2)); 320 assertEquals(+1, test2.compareTo(test1)); 321 322 LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); 323 assertEquals(-1, test1.compareTo(test3)); 324 assertEquals(+1, test3.compareTo(test1)); 325 assertEquals(0, test3.compareTo(test2)); 326 327 DateTimeFieldType[] types = new DateTimeFieldType[] { 328 DateTimeFieldType.hourOfDay(), 329 DateTimeFieldType.minuteOfHour(), 330 DateTimeFieldType.secondOfMinute(), 331 DateTimeFieldType.millisOfSecond(), 332 }; 333 int[] values = new int[] {10, 20, 30, 40}; 334 Partial p = new Partial(types, values); 335 assertEquals(0, test1.compareTo(p)); 336 assertEquals(0, test1.compareTo(new TimeOfDay(10, 20, 30, 40))); 337 try { 338 test1.compareTo(null); 339 fail(); 340 } catch (NullPointerException ex) {} 341 try { 342 test1.compareTo(new Date ()); 343 fail(); 344 } catch (ClassCastException ex) {} 345 } 346 347 public void testIsEqual_LocalTime() { 349 LocalTime test1 = new LocalTime(10, 20, 30, 40); 350 LocalTime test1a = new LocalTime(10, 20, 30, 40); 351 assertEquals(true, test1.isEqual(test1a)); 352 assertEquals(true, test1a.isEqual(test1)); 353 assertEquals(true, test1.isEqual(test1)); 354 assertEquals(true, test1a.isEqual(test1a)); 355 356 LocalTime test2 = new LocalTime(10, 20, 35, 40); 357 assertEquals(false, test1.isEqual(test2)); 358 assertEquals(false, test2.isEqual(test1)); 359 360 LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); 361 assertEquals(false, test1.isEqual(test3)); 362 assertEquals(false, test3.isEqual(test1)); 363 assertEquals(true, test3.isEqual(test2)); 364 365 try { 366 new LocalTime(10, 20, 35, 40).isEqual(null); 367 fail(); 368 } catch (IllegalArgumentException ex) {} 369 } 370 371 public void testIsBefore_LocalTime() { 373 LocalTime test1 = new LocalTime(10, 20, 30, 40); 374 LocalTime test1a = new LocalTime(10, 20, 30, 40); 375 assertEquals(false, test1.isBefore(test1a)); 376 assertEquals(false, test1a.isBefore(test1)); 377 assertEquals(false, test1.isBefore(test1)); 378 assertEquals(false, test1a.isBefore(test1a)); 379 380 LocalTime test2 = new LocalTime(10, 20, 35, 40); 381 assertEquals(true, test1.isBefore(test2)); 382 assertEquals(false, test2.isBefore(test1)); 383 384 LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); 385 assertEquals(true, test1.isBefore(test3)); 386 assertEquals(false, test3.isBefore(test1)); 387 assertEquals(false, test3.isBefore(test2)); 388 389 try { 390 new LocalTime(10, 20, 35, 40).isBefore(null); 391 fail(); 392 } catch (IllegalArgumentException ex) {} 393 } 394 395 public void testIsAfter_LocalTime() { 397 LocalTime test1 = new LocalTime(10, 20, 30, 40); 398 LocalTime test1a = new LocalTime(10, 20, 30, 40); 399 assertEquals(false, test1.isAfter(test1a)); 400 assertEquals(false, test1a.isAfter(test1)); 401 assertEquals(false, test1.isAfter(test1)); 402 assertEquals(false, test1a.isAfter(test1a)); 403 404 LocalTime test2 = new LocalTime(10, 20, 35, 40); 405 assertEquals(false, test1.isAfter(test2)); 406 assertEquals(true, test2.isAfter(test1)); 407 408 LocalTime test3 = new LocalTime(10, 20, 35, 40, GregorianChronology.getInstanceUTC()); 409 assertEquals(false, test1.isAfter(test3)); 410 assertEquals(true, test3.isAfter(test1)); 411 assertEquals(false, test3.isAfter(test2)); 412 413 try { 414 new LocalTime(10, 20, 35, 40).isAfter(null); 415 fail(); 416 } catch (IllegalArgumentException ex) {} 417 } 418 419 public void testWithField_DateTimeFieldType_int_1() { 421 LocalTime test = new LocalTime(10, 20, 30, 40); 422 LocalTime result = test.withField(DateTimeFieldType.hourOfDay(), 15); 423 424 assertEquals(new LocalTime(10, 20, 30, 40), test); 425 assertEquals(new LocalTime(15, 20, 30, 40), result); 426 } 427 428 public void testWithField_DateTimeFieldType_int_2() { 429 LocalTime test = new LocalTime(10, 20, 30, 40); 430 try { 431 test.withField(null, 6); 432 fail(); 433 } catch (IllegalArgumentException ex) {} 434 } 435 436 public void testWithField_DateTimeFieldType_int_3() { 437 LocalTime test = new LocalTime(10, 20, 30, 40); 438 try { 439 test.withField(DateTimeFieldType.dayOfMonth(), 6); 440 fail(); 441 } catch (IllegalArgumentException ex) {} 442 } 443 444 public void testWithField_DateTimeFieldType_int_4() { 445 LocalTime test = new LocalTime(10, 20, 30, 40); 446 LocalTime result = test.withField(DateTimeFieldType.hourOfDay(), 10); 447 assertSame(test, result); 448 } 449 450 public void testWithFieldAdded_DurationFieldType_int_1() { 452 LocalTime test = new LocalTime(10, 20, 30, 40); 453 LocalTime result = test.withFieldAdded(DurationFieldType.hours(), 6); 454 455 assertEquals(new LocalTime(10, 20, 30, 40), test); 456 assertEquals(new LocalTime(16, 20, 30, 40), result); 457 } 458 459 public void testWithFieldAdded_DurationFieldType_int_2() { 460 LocalTime test = new LocalTime(10, 20, 30, 40); 461 try { 462 test.withFieldAdded(null, 0); 463 fail(); 464 } catch (IllegalArgumentException ex) {} 465 } 466 467 public void testWithFieldAdded_DurationFieldType_int_3() { 468 LocalTime test = new LocalTime(10, 20, 30, 40); 469 try { 470 test.withFieldAdded(null, 6); 471 fail(); 472 } catch (IllegalArgumentException ex) {} 473 } 474 475 public void testWithFieldAdded_DurationFieldType_int_4() { 476 LocalTime test = new LocalTime(10, 20, 30, 40); 477 LocalTime result = test.withFieldAdded(DurationFieldType.hours(), 0); 478 assertSame(test, result); 479 } 480 481 public void testWithFieldAdded_DurationFieldType_int_5() { 482 LocalTime test = new LocalTime(10, 20, 30, 40); 483 try { 484 test.withFieldAdded(DurationFieldType.days(), 6); 485 fail(); 486 } catch (IllegalArgumentException ex) {} 487 } 488 489 public void testWithFieldAdded_DurationFieldType_int_6() { 490 LocalTime test = new LocalTime(10, 20, 30, 40); 491 LocalTime result = test.withFieldAdded(DurationFieldType.hours(), 16); 492 493 assertEquals(new LocalTime(10, 20, 30, 40), test); 494 assertEquals(new LocalTime(2, 20, 30, 40), result); 495 } 496 497 public void testWithFieldAdded_DurationFieldType_int_7() { 498 LocalTime test = new LocalTime(23, 59, 59, 999); 499 LocalTime result = test.withFieldAdded(DurationFieldType.millis(), 1); 500 assertEquals(new LocalTime(0, 0, 0, 0), result); 501 502 test = new LocalTime(23, 59, 59, 999); 503 result = test.withFieldAdded(DurationFieldType.seconds(), 1); 504 assertEquals(new LocalTime(0, 0, 0, 999), result); 505 506 test = new LocalTime(23, 59, 59, 999); 507 result = test.withFieldAdded(DurationFieldType.minutes(), 1); 508 assertEquals(new LocalTime(0, 0, 59, 999), result); 509 510 test = new LocalTime(23, 59, 59, 999); 511 result = test.withFieldAdded(DurationFieldType.hours(), 1); 512 assertEquals(new LocalTime(0, 59, 59, 999), result); 513 } 514 515 public void testWithFieldAdded_DurationFieldType_int_8() { 516 LocalTime test = new LocalTime(0, 0, 0, 0); 517 LocalTime result = test.withFieldAdded(DurationFieldType.millis(), -1); 518 assertEquals(new LocalTime(23, 59, 59, 999), result); 519 520 test = new LocalTime(0, 0, 0, 0); 521 result = test.withFieldAdded(DurationFieldType.seconds(), -1); 522 assertEquals(new LocalTime(23, 59, 59, 0), result); 523 524 test = new LocalTime(0, 0, 0, 0); 525 result = test.withFieldAdded(DurationFieldType.minutes(), -1); 526 assertEquals(new LocalTime(23, 59, 0, 0), result); 527 528 test = new LocalTime(0, 0, 0, 0); 529 result = test.withFieldAdded(DurationFieldType.hours(), -1); 530 assertEquals(new LocalTime(23, 0, 0, 0), result); 531 } 532 533 public void testPlus_RP() { 535 LocalTime test = new LocalTime(10, 20, 30, 40, BUDDHIST_LONDON); 536 LocalTime result = test.plus(new Period(1, 2, 3, 4, 5, 6, 7, 8)); 537 LocalTime expected = new LocalTime(15, 26, 37, 48, BUDDHIST_LONDON); 538 assertEquals(expected, result); 539 540 result = test.plus((ReadablePeriod) null); 541 assertSame(test, result); 542 } 543 544 public void testPlusHours_int() { 545 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON); 546 LocalTime result = test.plusHours(1); 547 LocalTime expected = new LocalTime(2, 2, 3, 4, BUDDHIST_LONDON); 548 assertEquals(expected, result); 549 550 result = test.plusHours(0); 551 assertSame(test, result); 552 } 553 554 public void testPlusMinutes_int() { 555 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON); 556 LocalTime result = test.plusMinutes(1); 557 LocalTime expected = new LocalTime(1, 3, 3, 4, BUDDHIST_LONDON); 558 assertEquals(expected, result); 559 560 result = test.plusMinutes(0); 561 assertSame(test, result); 562 } 563 564 public void testPlusSeconds_int() { 565 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON); 566 LocalTime result = test.plusSeconds(1); 567 LocalTime expected = new LocalTime(1, 2, 4, 4, BUDDHIST_LONDON); 568 assertEquals(expected, result); 569 570 result = test.plusSeconds(0); 571 assertSame(test, result); 572 } 573 574 public void testPlusMillis_int() { 575 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON); 576 LocalTime result = test.plusMillis(1); 577 LocalTime expected = new LocalTime(1, 2, 3, 5, BUDDHIST_LONDON); 578 assertEquals(expected, result); 579 580 result = test.plusMillis(0); 581 assertSame(test, result); 582 } 583 584 public void testMinus_RP() { 586 LocalTime test = new LocalTime(10, 20, 30, 40, BUDDHIST_LONDON); 587 LocalTime result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1)); 588 LocalTime expected = new LocalTime(9, 19, 29, 39, BUDDHIST_LONDON); 589 assertEquals(expected, result); 590 591 result = test.minus((ReadablePeriod) null); 592 assertSame(test, result); 593 } 594 595 public void testMinusHours_int() { 596 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON); 597 LocalTime result = test.minusHours(1); 598 LocalTime expected = new LocalTime(0, 2, 3, 4, BUDDHIST_LONDON); 599 assertEquals(expected, result); 600 601 result = test.minusHours(0); 602 assertSame(test, result); 603 } 604 605 public void testMinusMinutes_int() { 606 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON); 607 LocalTime result = test.minusMinutes(1); 608 LocalTime expected = new LocalTime(1, 1, 3, 4, BUDDHIST_LONDON); 609 assertEquals(expected, result); 610 611 result = test.minusMinutes(0); 612 assertSame(test, result); 613 } 614 615 public void testMinusSeconds_int() { 616 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON); 617 LocalTime result = test.minusSeconds(1); 618 LocalTime expected = new LocalTime(1, 2, 2, 4, BUDDHIST_LONDON); 619 assertEquals(expected, result); 620 621 result = test.minusSeconds(0); 622 assertSame(test, result); 623 } 624 625 public void testMinusMillis_int() { 626 LocalTime test = new LocalTime(1, 2, 3, 4, BUDDHIST_LONDON); 627 LocalTime result = test.minusMillis(1); 628 LocalTime expected = new LocalTime(1, 2, 3, 3, BUDDHIST_LONDON); 629 assertEquals(expected, result); 630 631 result = test.minusMillis(0); 632 assertSame(test, result); 633 } 634 635 public void testGetters() { 637 LocalTime test = new LocalTime(10, 20, 30, 40); 638 assertEquals(10, test.getHourOfDay()); 639 assertEquals(20, test.getMinuteOfHour()); 640 assertEquals(30, test.getSecondOfMinute()); 641 assertEquals(40, test.getMillisOfSecond()); 642 assertEquals(TEST_TIME_NOW, test.getMillisOfDay()); 643 } 644 645 public void testWithers() { 647 LocalTime test = new LocalTime(10, 20, 30, 40); 648 check(test.withHourOfDay(6), 6, 20, 30, 40); 649 check(test.withMinuteOfHour(6), 10, 6, 30, 40); 650 check(test.withSecondOfMinute(6), 10, 20, 6, 40); 651 check(test.withMillisOfSecond(6), 10, 20, 30, 6); 652 check(test.withMillisOfDay(61234), 0, 1, 1, 234); 653 try { 654 test.withHourOfDay(-1); 655 fail(); 656 } catch (IllegalArgumentException ex) {} 657 try { 658 test.withHourOfDay(24); 659 fail(); 660 } catch (IllegalArgumentException ex) {} 661 } 662 663 public void testToDateTimeTodayDefaultZone() { 665 LocalTime base = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 667 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 668 669 DateTime test = base.toDateTimeToday(); 670 check(base, 10, 20, 30, 40); 671 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 672 expected = expected.hourOfDay().setCopy(10); 673 expected = expected.minuteOfHour().setCopy(20); 674 expected = expected.secondOfMinute().setCopy(30); 675 expected = expected.millisOfSecond().setCopy(40); 676 assertEquals(expected, test); 677 } 678 679 public void testToDateTimeToday_Zone() { 681 LocalTime base = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 683 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 684 685 DateTime test = base.toDateTimeToday(TOKYO); 686 check(base, 10, 20, 30, 40); 687 DateTime expected = new DateTime(dt.getMillis(), COPTIC_TOKYO); 688 expected = expected.hourOfDay().setCopy(10); 689 expected = expected.minuteOfHour().setCopy(20); 690 expected = expected.secondOfMinute().setCopy(30); 691 expected = expected.millisOfSecond().setCopy(40); 692 assertEquals(expected, test); 693 } 694 695 public void testToDateTimeToday_nullZone() { 696 LocalTime base = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); DateTime dt = new DateTime(2004, 6, 9, 6, 7, 8, 9); 698 DateTimeUtils.setCurrentMillisFixed(dt.getMillis()); 699 700 DateTime test = base.toDateTimeToday((DateTimeZone) null); 701 check(base, 10, 20, 30, 40); 702 DateTime expected = new DateTime(dt.getMillis(), COPTIC_LONDON); 703 expected = expected.hourOfDay().setCopy(10); 704 expected = expected.minuteOfHour().setCopy(20); 705 expected = expected.secondOfMinute().setCopy(30); 706 expected = expected.millisOfSecond().setCopy(40); 707 assertEquals(expected, test); 708 } 709 710 public void testToDateTime_RI() { 712 LocalTime base = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); 713 DateTime dt = new DateTime(0L); assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString()); 715 716 DateTime test = base.toDateTime(dt); 717 check(base, 10, 20, 30, 40); 718 assertEquals("1970-01-01T01:00:00.000+01:00", dt.toString()); 719 assertEquals("1970-01-01T10:20:30.040+01:00", test.toString()); 720 } 721 722 public void testToDateTime_nullRI() { 723 LocalTime base = new LocalTime(1, 2, 3, 4); 724 DateTimeUtils.setCurrentMillisFixed(TEST_TIME2); 725 726 DateTime test = base.toDateTime((ReadableInstant) null); 727 check(base, 1, 2, 3, 4); 728 assertEquals("1970-01-02T01:02:03.004+01:00", test.toString()); 729 } 730 731 public void testProperty() { 733 LocalTime test = new LocalTime(10, 20, 30, 40); 734 assertEquals(test.hourOfDay(), test.property(DateTimeFieldType.hourOfDay())); 735 assertEquals(test.minuteOfHour(), test.property(DateTimeFieldType.minuteOfHour())); 736 assertEquals(test.secondOfMinute(), test.property(DateTimeFieldType.secondOfMinute())); 737 assertEquals(test.millisOfSecond(), test.property(DateTimeFieldType.millisOfSecond())); 738 assertEquals(test.millisOfDay(), test.property(DateTimeFieldType.millisOfDay())); 739 740 assertEquals(test, test.property(DateTimeFieldType.minuteOfDay()).getLocalTime()); 741 assertEquals(test, test.property(DateTimeFieldType.secondOfDay()).getLocalTime()); 742 assertEquals(test, test.property(DateTimeFieldType.millisOfDay()).getLocalTime()); 743 assertEquals(test, test.property(DateTimeFieldType.hourOfHalfday()).getLocalTime()); 744 assertEquals(test, test.property(DateTimeFieldType.halfdayOfDay()).getLocalTime()); 745 assertEquals(test, test.property(DateTimeFieldType.clockhourOfHalfday()).getLocalTime()); 746 assertEquals(test, test.property(DateTimeFieldType.clockhourOfDay()).getLocalTime()); 747 748 try { 749 test.property(DateTimeFieldType.dayOfWeek()); 750 fail(); 751 } catch (IllegalArgumentException ex) {} 752 try { 753 test.property(null); 754 fail(); 755 } catch (IllegalArgumentException ex) {} 756 } 757 758 public void testSerialization() throws Exception { 760 LocalTime test = new LocalTime(10, 20, 30, 40, COPTIC_PARIS); 761 762 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 763 ObjectOutputStream oos = new ObjectOutputStream (baos); 764 oos.writeObject(test); 765 byte[] bytes = baos.toByteArray(); 766 oos.close(); 767 768 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 769 ObjectInputStream ois = new ObjectInputStream (bais); 770 LocalTime result = (LocalTime) ois.readObject(); 771 ois.close(); 772 773 assertEquals(test, result); 774 assertTrue(Arrays.equals(test.getValues(), result.getValues())); 775 assertTrue(Arrays.equals(test.getFields(), result.getFields())); 776 assertEquals(test.getChronology(), result.getChronology()); 777 } 778 779 public void testToString() { 781 LocalTime test = new LocalTime(10, 20, 30, 40); 782 assertEquals("10:20:30.040", test.toString()); 783 } 784 785 public void testToString_String() { 787 LocalTime test = new LocalTime(10, 20, 30, 40); 788 assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test.toString("yyyy HH")); 789 assertEquals("10:20:30.040", test.toString((String ) null)); 790 } 791 792 public void testToString_String_Locale() { 794 LocalTime test = new LocalTime(10, 20, 30, 40); 795 assertEquals("10 20", test.toString("H m", Locale.ENGLISH)); 796 assertEquals("10:20:30.040", test.toString(null, Locale.ENGLISH)); 797 assertEquals("10 20", test.toString("H m", null)); 798 assertEquals("10:20:30.040", test.toString(null, null)); 799 } 800 801 public void testToString_DTFormatter() { 803 LocalTime test = new LocalTime(10, 20, 30, 40); 804 assertEquals("\ufffd\ufffd\ufffd\ufffd 10", test.toString(DateTimeFormat.forPattern("yyyy HH"))); 805 assertEquals("10:20:30.040", test.toString((DateTimeFormatter) null)); 806 } 807 808 private void check(LocalTime test, int hour, int min, int sec, int milli) { 810 assertEquals(hour, test.getHourOfDay()); 811 assertEquals(min, test.getMinuteOfHour()); 812 assertEquals(sec, test.getSecondOfMinute()); 813 assertEquals(milli, test.getMillisOfSecond()); 814 } 815 } 816 | Popular Tags |