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.util.Locale; 019 020 import junit.framework.TestCase; 021 import junit.framework.TestSuite; 022 023 import org.joda.time.chrono.CopticChronology; 024 025 /** 026 * This class is a Junit unit test for LocalDateTime. 027 * 028 * @author Stephen Colebourne 029 */ 030 public class TestLocalDateTime_Properties extends TestCase { 031 032 private static final CopticChronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 033 034 private int MILLIS_OF_DAY = 035 (int) (10L * DateTimeConstants.MILLIS_PER_HOUR 036 + 20L * DateTimeConstants.MILLIS_PER_MINUTE 037 + 30L * DateTimeConstants.MILLIS_PER_SECOND 038 + 40L); 039 private long TEST_TIME_NOW = 040 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY 041 + MILLIS_OF_DAY; 042 private long TEST_TIME1 = 043 (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 044 + 1L * DateTimeConstants.MILLIS_PER_HOUR 045 + 2L * DateTimeConstants.MILLIS_PER_MINUTE 046 + 3L * DateTimeConstants.MILLIS_PER_SECOND 047 + 4L; 048 private long TEST_TIME2 = 049 (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 050 + 4L * DateTimeConstants.MILLIS_PER_HOUR 051 + 5L * DateTimeConstants.MILLIS_PER_MINUTE 052 + 6L * DateTimeConstants.MILLIS_PER_SECOND 053 + 7L; 054 055 private DateTimeZone zone = null; 056 057 private Locale systemDefaultLocale = null; 058 059 public static void main(String[] args) { 060 junit.textui.TestRunner.run(suite()); 061 } 062 063 public static TestSuite suite() { 064 return new TestSuite(TestLocalDateTime_Properties.class); 065 } 066 067 public TestLocalDateTime_Properties(String name) { 068 super(name); 069 } 070 071 protected void setUp() throws Exception { 072 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 073 zone = DateTimeZone.getDefault(); 074 DateTimeZone.setDefault(DateTimeZone.UTC); 075 systemDefaultLocale = Locale.getDefault(); 076 Locale.setDefault(Locale.ENGLISH); 077 } 078 079 protected void tearDown() throws Exception { 080 DateTimeUtils.setCurrentMillisSystem(); 081 DateTimeZone.setDefault(zone); 082 zone = null; 083 Locale.setDefault(systemDefaultLocale); 084 systemDefaultLocale = null; 085 } 086 087 //----------------------------------------------------------------------- 088 public void testPropertyGetYear() { 089 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 090 assertSame(test.getChronology().year(), test.year().getField()); 091 assertEquals("year", test.year().getName()); 092 assertEquals("Property[year]", test.year().toString()); 093 assertSame(test, test.year().getLocalDateTime()); 094 assertEquals(1972, test.year().get()); 095 assertEquals("1972", test.year().getAsString()); 096 assertEquals("1972", test.year().getAsText()); 097 assertEquals("1972", test.year().getAsText(Locale.FRENCH)); 098 assertEquals("1972", test.year().getAsShortText()); 099 assertEquals("1972", test.year().getAsShortText(Locale.FRENCH)); 100 assertEquals(test.getChronology().years(), test.year().getDurationField()); 101 assertEquals(null, test.year().getRangeDurationField()); 102 assertEquals(9, test.year().getMaximumTextLength(null)); 103 assertEquals(9, test.year().getMaximumShortTextLength(null)); 104 } 105 106 public void testPropertyGetMaxMinValuesYear() { 107 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 108 assertEquals(-292275054, test.year().getMinimumValue()); 109 assertEquals(-292275054, test.year().getMinimumValueOverall()); 110 assertEquals(292278993, test.year().getMaximumValue()); 111 assertEquals(292278993, test.year().getMaximumValueOverall()); 112 } 113 114 public void testPropertyAddToCopyYear() { 115 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 116 LocalDateTime copy = test.year().addToCopy(9); 117 check(test, 1972, 6, 9, 10, 20, 30, 40); 118 check(copy, 1981, 6, 9, 10, 20, 30, 40); 119 120 copy = test.year().addToCopy(0); 121 check(copy, 1972, 6, 9, 10, 20, 30, 40); 122 123 copy = test.year().addToCopy(292278993 - 1972); 124 check(copy, 292278993, 6, 9, 10, 20, 30, 40); 125 126 try { 127 test.year().addToCopy(292278993 - 1972 + 1); 128 fail(); 129 } catch (IllegalArgumentException ex) {} 130 check(test, 1972, 6, 9, 10, 20, 30, 40); 131 132 copy = test.year().addToCopy(-1972); 133 check(copy, 0, 6, 9, 10, 20, 30, 40); 134 135 copy = test.year().addToCopy(-1973); 136 check(copy, -1, 6, 9, 10, 20, 30, 40); 137 138 try { 139 test.year().addToCopy(-292275054 - 1972 - 1); 140 fail(); 141 } catch (IllegalArgumentException ex) {} 142 check(test, 1972, 6, 9, 10, 20, 30, 40); 143 } 144 145 public void testPropertyAddWrapFieldToCopyYear() { 146 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 147 LocalDateTime copy = test.year().addWrapFieldToCopy(9); 148 check(test, 1972, 6, 9, 10, 20, 30, 40); 149 check(copy, 1981, 6, 9, 10, 20, 30, 40); 150 151 copy = test.year().addWrapFieldToCopy(0); 152 check(copy, 1972, 6, 9, 10, 20, 30, 40); 153 154 copy = test.year().addWrapFieldToCopy(292278993 - 1972 + 1); 155 check(copy, -292275054, 6, 9, 10, 20, 30, 40); 156 157 copy = test.year().addWrapFieldToCopy(-292275054 - 1972 - 1); 158 check(copy, 292278993, 6, 9, 10, 20, 30, 40); 159 } 160 161 public void testPropertySetCopyYear() { 162 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 163 LocalDateTime copy = test.year().setCopy(12); 164 check(test, 1972, 6, 9, 10, 20, 30, 40); 165 check(copy, 12, 6, 9, 10, 20, 30, 40); 166 } 167 168 public void testPropertySetCopyTextYear() { 169 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 170 LocalDateTime copy = test.year().setCopy("12"); 171 check(test, 1972, 6, 9, 10, 20, 30, 40); 172 check(copy, 12, 6, 9, 10, 20, 30, 40); 173 } 174 175 public void testPropertyCompareToYear() { 176 LocalDateTime test1 = new LocalDateTime(TEST_TIME1); 177 LocalDateTime test2 = new LocalDateTime(TEST_TIME2); 178 assertEquals(true, test1.year().compareTo(test2) < 0); 179 assertEquals(true, test2.year().compareTo(test1) > 0); 180 assertEquals(true, test1.year().compareTo(test1) == 0); 181 try { 182 test1.year().compareTo((ReadablePartial) null); 183 fail(); 184 } catch (IllegalArgumentException ex) {} 185 186 DateTime dt1 = new DateTime(TEST_TIME1); 187 DateTime dt2 = new DateTime(TEST_TIME2); 188 assertEquals(true, test1.year().compareTo(dt2) < 0); 189 assertEquals(true, test2.year().compareTo(dt1) > 0); 190 assertEquals(true, test1.year().compareTo(dt1) == 0); 191 try { 192 test1.year().compareTo((ReadableInstant) null); 193 fail(); 194 } catch (IllegalArgumentException ex) {} 195 } 196 197 //----------------------------------------------------------------------- 198 public void testPropertyGetMonth() { 199 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 200 assertSame(test.getChronology().monthOfYear(), test.monthOfYear().getField()); 201 assertEquals("monthOfYear", test.monthOfYear().getName()); 202 assertEquals("Property[monthOfYear]", test.monthOfYear().toString()); 203 assertSame(test, test.monthOfYear().getLocalDateTime()); 204 assertEquals(6, test.monthOfYear().get()); 205 assertEquals("6", test.monthOfYear().getAsString()); 206 assertEquals("June", test.monthOfYear().getAsText()); 207 assertEquals("juin", test.monthOfYear().getAsText(Locale.FRENCH)); 208 assertEquals("Jun", test.monthOfYear().getAsShortText()); 209 assertEquals("juin", test.monthOfYear().getAsShortText(Locale.FRENCH)); 210 assertEquals(test.getChronology().months(), test.monthOfYear().getDurationField()); 211 assertEquals(test.getChronology().years(), test.monthOfYear().getRangeDurationField()); 212 assertEquals(9, test.monthOfYear().getMaximumTextLength(null)); 213 assertEquals(3, test.monthOfYear().getMaximumShortTextLength(null)); 214 test = new LocalDateTime(1972, 7, 9, 10, 20, 30, 40); 215 assertEquals("juillet", test.monthOfYear().getAsText(Locale.FRENCH)); 216 assertEquals("juil.", test.monthOfYear().getAsShortText(Locale.FRENCH)); 217 } 218 219 public void testPropertyGetMaxMinValuesMonth() { 220 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 221 assertEquals(1, test.monthOfYear().getMinimumValue()); 222 assertEquals(1, test.monthOfYear().getMinimumValueOverall()); 223 assertEquals(12, test.monthOfYear().getMaximumValue()); 224 assertEquals(12, test.monthOfYear().getMaximumValueOverall()); 225 } 226 227 public void testPropertyAddToCopyMonth() { 228 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 229 LocalDateTime copy = test.monthOfYear().addToCopy(6); 230 check(test, 1972, 6, 9, 10, 20, 30, 40); 231 check(copy, 1972, 12, 9, 10, 20, 30, 40); 232 233 copy = test.monthOfYear().addToCopy(7); 234 check(copy, 1973, 1, 9, 10, 20, 30, 40); 235 236 copy = test.monthOfYear().addToCopy(-5); 237 check(copy, 1972, 1, 9, 10, 20, 30, 40); 238 239 copy = test.monthOfYear().addToCopy(-6); 240 check(copy, 1971, 12, 9, 10, 20, 30, 40); 241 242 test = new LocalDateTime(1972, 1, 31, 10, 20, 30, 40); 243 copy = test.monthOfYear().addToCopy(1); 244 check(copy, 1972, 2, 29, 10, 20, 30, 40); 245 246 copy = test.monthOfYear().addToCopy(2); 247 check(copy, 1972, 3, 31, 10, 20, 30, 40); 248 249 copy = test.monthOfYear().addToCopy(3); 250 check(copy, 1972, 4, 30, 10, 20, 30, 40); 251 252 test = new LocalDateTime(1971, 1, 31, 10, 20, 30, 40); 253 copy = test.monthOfYear().addToCopy(1); 254 check(copy, 1971, 2, 28, 10, 20, 30, 40); 255 } 256 257 public void testPropertyAddWrapFieldToCopyMonth() { 258 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 259 LocalDateTime copy = test.monthOfYear().addWrapFieldToCopy(4); 260 check(test, 1972, 6, 9, 10, 20, 30, 40); 261 check(copy, 1972, 10, 9, 10, 20, 30, 40); 262 263 copy = test.monthOfYear().addWrapFieldToCopy(8); 264 check(copy, 1972, 2, 9, 10, 20, 30, 40); 265 266 copy = test.monthOfYear().addWrapFieldToCopy(-8); 267 check(copy, 1972, 10, 9, 10, 20, 30, 40); 268 269 test = new LocalDateTime(1972, 1, 31, 10, 20, 30, 40); 270 copy = test.monthOfYear().addWrapFieldToCopy(1); 271 check(copy, 1972, 2, 29, 10, 20, 30, 40); 272 273 copy = test.monthOfYear().addWrapFieldToCopy(2); 274 check(copy, 1972, 3, 31, 10, 20, 30, 40); 275 276 copy = test.monthOfYear().addWrapFieldToCopy(3); 277 check(copy, 1972, 4, 30, 10, 20, 30, 40); 278 279 test = new LocalDateTime(1971, 1, 31, 10, 20, 30, 40); 280 copy = test.monthOfYear().addWrapFieldToCopy(1); 281 check(copy, 1971, 2, 28, 10, 20, 30, 40); 282 } 283 284 public void testPropertySetCopyMonth() { 285 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 286 LocalDateTime copy = test.monthOfYear().setCopy(12); 287 check(test, 1972, 6, 9, 10, 20, 30, 40); 288 check(copy, 1972, 12, 9, 10, 20, 30, 40); 289 290 test = new LocalDateTime(1972, 1, 31, 10, 20, 30, 40); 291 copy = test.monthOfYear().setCopy(2); 292 check(copy, 1972, 2, 29, 10, 20, 30, 40); 293 294 try { 295 test.monthOfYear().setCopy(13); 296 fail(); 297 } catch (IllegalArgumentException ex) {} 298 try { 299 test.monthOfYear().setCopy(0); 300 fail(); 301 } catch (IllegalArgumentException ex) {} 302 } 303 304 public void testPropertySetCopyTextMonth() { 305 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 306 LocalDateTime copy = test.monthOfYear().setCopy("12"); 307 check(test, 1972, 6, 9, 10, 20, 30, 40); 308 check(copy, 1972, 12, 9, 10, 20, 30, 40); 309 310 copy = test.monthOfYear().setCopy("December"); 311 check(test, 1972, 6, 9, 10, 20, 30, 40); 312 check(copy, 1972, 12, 9, 10, 20, 30, 40); 313 314 copy = test.monthOfYear().setCopy("Dec"); 315 check(test, 1972, 6, 9, 10, 20, 30, 40); 316 check(copy, 1972, 12, 9, 10, 20, 30, 40); 317 } 318 319 public void testPropertyCompareToMonth() { 320 LocalDateTime test1 = new LocalDateTime(TEST_TIME1); 321 LocalDateTime test2 = new LocalDateTime(TEST_TIME2); 322 assertEquals(true, test1.monthOfYear().compareTo(test2) < 0); 323 assertEquals(true, test2.monthOfYear().compareTo(test1) > 0); 324 assertEquals(true, test1.monthOfYear().compareTo(test1) == 0); 325 try { 326 test1.monthOfYear().compareTo((ReadablePartial) null); 327 fail(); 328 } catch (IllegalArgumentException ex) {} 329 330 DateTime dt1 = new DateTime(TEST_TIME1); 331 DateTime dt2 = new DateTime(TEST_TIME2); 332 assertEquals(true, test1.monthOfYear().compareTo(dt2) < 0); 333 assertEquals(true, test2.monthOfYear().compareTo(dt1) > 0); 334 assertEquals(true, test1.monthOfYear().compareTo(dt1) == 0); 335 try { 336 test1.monthOfYear().compareTo((ReadableInstant) null); 337 fail(); 338 } catch (IllegalArgumentException ex) {} 339 } 340 341 //----------------------------------------------------------------------- 342 public void testPropertyGetDay() { 343 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 344 assertSame(test.getChronology().dayOfMonth(), test.dayOfMonth().getField()); 345 assertEquals("dayOfMonth", test.dayOfMonth().getName()); 346 assertEquals("Property[dayOfMonth]", test.dayOfMonth().toString()); 347 assertSame(test, test.dayOfMonth().getLocalDateTime()); 348 assertEquals(9, test.dayOfMonth().get()); 349 assertEquals("9", test.dayOfMonth().getAsString()); 350 assertEquals("9", test.dayOfMonth().getAsText()); 351 assertEquals("9", test.dayOfMonth().getAsText(Locale.FRENCH)); 352 assertEquals("9", test.dayOfMonth().getAsShortText()); 353 assertEquals("9", test.dayOfMonth().getAsShortText(Locale.FRENCH)); 354 assertEquals(test.getChronology().days(), test.dayOfMonth().getDurationField()); 355 assertEquals(test.getChronology().months(), test.dayOfMonth().getRangeDurationField()); 356 assertEquals(2, test.dayOfMonth().getMaximumTextLength(null)); 357 assertEquals(2, test.dayOfMonth().getMaximumShortTextLength(null)); 358 } 359 360 public void testPropertyGetMaxMinValuesDay() { 361 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 362 assertEquals(1, test.dayOfMonth().getMinimumValue()); 363 assertEquals(1, test.dayOfMonth().getMinimumValueOverall()); 364 assertEquals(30, test.dayOfMonth().getMaximumValue()); 365 assertEquals(31, test.dayOfMonth().getMaximumValueOverall()); 366 test = new LocalDateTime(1972, 7, 9, 10, 20, 30, 40); 367 assertEquals(31, test.dayOfMonth().getMaximumValue()); 368 test = new LocalDateTime(1972, 2, 9, 10, 20, 30, 40); 369 assertEquals(29, test.dayOfMonth().getMaximumValue()); 370 test = new LocalDateTime(1971, 2, 9, 10, 20, 30, 40); 371 assertEquals(28, test.dayOfMonth().getMaximumValue()); 372 } 373 374 public void testPropertyAddToCopyDay() { 375 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 376 LocalDateTime copy = test.dayOfMonth().addToCopy(9); 377 check(test, 1972, 6, 9, 10, 20, 30, 40); 378 check(copy, 1972, 6, 18, 10, 20, 30, 40); 379 380 copy = test.dayOfMonth().addToCopy(21); 381 check(copy, 1972, 6, 30, 10, 20, 30, 40); 382 383 copy = test.dayOfMonth().addToCopy(22); 384 check(copy, 1972, 7, 1, 10, 20, 30, 40); 385 386 copy = test.dayOfMonth().addToCopy(22 + 30); 387 check(copy, 1972, 7, 31, 10, 20, 30, 40); 388 389 copy = test.dayOfMonth().addToCopy(22 + 31); 390 check(copy, 1972, 8, 1, 10, 20, 30, 40); 391 392 copy = test.dayOfMonth().addToCopy(21 + 31 + 31 + 30 + 31 + 30 + 31); 393 check(copy, 1972, 12, 31, 10, 20, 30, 40); 394 395 copy = test.dayOfMonth().addToCopy(22 + 31 + 31 + 30 + 31 + 30 + 31); 396 check(copy, 1973, 1, 1, 10, 20, 30, 40); 397 398 copy = test.dayOfMonth().addToCopy(-8); 399 check(copy, 1972, 6, 1, 10, 20, 30, 40); 400 401 copy = test.dayOfMonth().addToCopy(-9); 402 check(copy, 1972, 5, 31, 10, 20, 30, 40); 403 404 copy = test.dayOfMonth().addToCopy(-8 - 31 - 30 - 31 - 29 - 31); 405 check(copy, 1972, 1, 1, 10, 20, 30, 40); 406 407 copy = test.dayOfMonth().addToCopy(-9 - 31 - 30 - 31 - 29 - 31); 408 check(copy, 1971, 12, 31, 10, 20, 30, 40); 409 } 410 411 public void testPropertyAddWrapFieldToCopyDay() { 412 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 413 LocalDateTime copy = test.dayOfMonth().addWrapFieldToCopy(21); 414 check(test, 1972, 6, 9, 10, 20, 30, 40); 415 check(copy, 1972, 6, 30, 10, 20, 30, 40); 416 417 copy = test.dayOfMonth().addWrapFieldToCopy(22); 418 check(copy, 1972, 6, 1, 10, 20, 30, 40); 419 420 copy = test.dayOfMonth().addWrapFieldToCopy(-12); 421 check(copy, 1972, 6, 27, 10, 20, 30, 40); 422 423 test = new LocalDateTime(1972, 7, 9, 10, 20, 30, 40); 424 copy = test.dayOfMonth().addWrapFieldToCopy(21); 425 check(copy, 1972, 7, 30, 10, 20, 30, 40); 426 427 copy = test.dayOfMonth().addWrapFieldToCopy(22); 428 check(copy, 1972, 7, 31, 10, 20, 30, 40); 429 430 copy = test.dayOfMonth().addWrapFieldToCopy(23); 431 check(copy, 1972, 7, 1, 10, 20, 30, 40); 432 433 copy = test.dayOfMonth().addWrapFieldToCopy(-12); 434 check(copy, 1972, 7, 28, 10, 20, 30, 40); 435 } 436 437 public void testPropertySetCopyDay() { 438 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 439 LocalDateTime copy = test.dayOfMonth().setCopy(12); 440 check(test, 1972, 6, 9, 10, 20, 30, 40); 441 check(copy, 1972, 6, 12, 10, 20, 30, 40); 442 443 try { 444 test.dayOfMonth().setCopy(31); 445 fail(); 446 } catch (IllegalArgumentException ex) {} 447 try { 448 test.dayOfMonth().setCopy(0); 449 fail(); 450 } catch (IllegalArgumentException ex) {} 451 } 452 453 public void testPropertySetCopyTextDay() { 454 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 455 LocalDateTime copy = test.dayOfMonth().setCopy("12"); 456 check(test, 1972, 6, 9, 10, 20, 30, 40); 457 check(copy, 1972, 6, 12, 10, 20, 30, 40); 458 } 459 460 public void testPropertyWithMaximumValueDayOfMonth() { 461 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 462 LocalDateTime copy = test.dayOfMonth().withMaximumValue(); 463 check(test, 1972, 6, 9, 10, 20, 30, 40); 464 check(copy, 1972, 6, 30, 10, 20, 30, 40); 465 } 466 467 public void testPropertyWithMinimumValueDayOfMonth() { 468 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40); 469 LocalDateTime copy = test.dayOfMonth().withMinimumValue(); 470 check(test, 1972, 6, 9, 10, 20, 30, 40); 471 check(copy, 1972, 6, 1, 10, 20, 30, 40); 472 } 473 474 public void testPropertyCompareToDay() { 475 LocalDateTime test1 = new LocalDateTime(TEST_TIME1); 476 LocalDateTime test2 = new LocalDateTime(TEST_TIME2); 477 assertEquals(true, test1.dayOfMonth().compareTo(test2) < 0); 478 assertEquals(true, test2.dayOfMonth().compareTo(test1) > 0); 479 assertEquals(true, test1.dayOfMonth().compareTo(test1) == 0); 480 try { 481 test1.dayOfMonth().compareTo((ReadablePartial) null); 482 fail(); 483 } catch (IllegalArgumentException ex) {} 484 485 DateTime dt1 = new DateTime(TEST_TIME1); 486 DateTime dt2 = new DateTime(TEST_TIME2); 487 assertEquals(true, test1.dayOfMonth().compareTo(dt2) < 0); 488 assertEquals(true, test2.dayOfMonth().compareTo(dt1) > 0); 489 assertEquals(true, test1.dayOfMonth().compareTo(dt1) == 0); 490 try { 491 test1.dayOfMonth().compareTo((ReadableInstant) null); 492 fail(); 493 } catch (IllegalArgumentException ex) {} 494 } 495 496 public void testPropertyEquals() { 497 LocalDateTime test1 = new LocalDateTime(2005, 11, 8, 10, 20, 30, 40); 498 LocalDateTime test2 = new LocalDateTime(2005, 11, 9, 10, 20, 30, 40); 499 LocalDateTime test3 = new LocalDateTime(2005, 11, 8, 10, 20, 30, 40, COPTIC_UTC); 500 assertEquals(false, test1.dayOfMonth().equals(test1.year())); 501 assertEquals(false, test1.dayOfMonth().equals(test1.monthOfYear())); 502 assertEquals(true, test1.dayOfMonth().equals(test1.dayOfMonth())); 503 assertEquals(false, test1.dayOfMonth().equals(test2.year())); 504 assertEquals(false, test1.dayOfMonth().equals(test2.monthOfYear())); 505 assertEquals(false, test1.dayOfMonth().equals(test2.dayOfMonth())); 506 507 assertEquals(false, test1.monthOfYear().equals(test1.year())); 508 assertEquals(true, test1.monthOfYear().equals(test1.monthOfYear())); 509 assertEquals(false, test1.monthOfYear().equals(test1.dayOfMonth())); 510 assertEquals(false, test1.monthOfYear().equals(test2.year())); 511 assertEquals(true, test1.monthOfYear().equals(test2.monthOfYear())); 512 assertEquals(false, test1.monthOfYear().equals(test2.dayOfMonth())); 513 514 assertEquals(false, test1.dayOfMonth().equals(null)); 515 assertEquals(false, test1.dayOfMonth().equals("any")); 516 517 // chrono 518 assertEquals(false, test1.dayOfMonth().equals(test3.dayOfMonth())); 519 } 520 521 public void testPropertyHashCode() { 522 LocalDateTime test1 = new LocalDateTime(2005, 11, 8, 10, 20, 30, 40); 523 LocalDateTime test2 = new LocalDateTime(2005, 11, 9, 10, 20, 30, 40); 524 assertEquals(true, test1.dayOfMonth().hashCode() == test1.dayOfMonth().hashCode()); 525 assertEquals(false, test1.dayOfMonth().hashCode() == test2.dayOfMonth().hashCode()); 526 assertEquals(true, test1.monthOfYear().hashCode() == test1.monthOfYear().hashCode()); 527 assertEquals(true, test1.monthOfYear().hashCode() == test2.monthOfYear().hashCode()); 528 } 529 530 //----------------------------------------------------------------------- 531 public void testPropertyGetHour() { 532 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 533 assertSame(test.getChronology().hourOfDay(), test.hourOfDay().getField()); 534 assertEquals("hourOfDay", test.hourOfDay().getName()); 535 assertEquals("Property[hourOfDay]", test.hourOfDay().toString()); 536 assertSame(test, test.hourOfDay().getLocalDateTime()); 537 assertEquals(10, test.hourOfDay().get()); 538 assertEquals("10", test.hourOfDay().getAsString()); 539 assertEquals("10", test.hourOfDay().getAsText()); 540 assertEquals("10", test.hourOfDay().getAsText(Locale.FRENCH)); 541 assertEquals("10", test.hourOfDay().getAsShortText()); 542 assertEquals("10", test.hourOfDay().getAsShortText(Locale.FRENCH)); 543 assertEquals(test.getChronology().hours(), test.hourOfDay().getDurationField()); 544 assertEquals(test.getChronology().days(), test.hourOfDay().getRangeDurationField()); 545 assertEquals(2, test.hourOfDay().getMaximumTextLength(null)); 546 assertEquals(2, test.hourOfDay().getMaximumShortTextLength(null)); 547 } 548 549 public void testPropertyRoundHour() { 550 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20); 551 check(test.hourOfDay().roundCeilingCopy(), 2005, 6, 9, 11, 0, 0, 0); 552 check(test.hourOfDay().roundFloorCopy(), 2005, 6, 9, 10, 0, 0, 0); 553 check(test.hourOfDay().roundHalfCeilingCopy(), 2005, 6, 9, 10, 0, 0, 0); 554 check(test.hourOfDay().roundHalfFloorCopy(), 2005, 6, 9, 10, 0, 0, 0); 555 check(test.hourOfDay().roundHalfEvenCopy(), 2005, 6, 9, 10, 0, 0, 0); 556 557 test = new LocalDateTime(2005, 6, 9, 10, 40); 558 check(test.hourOfDay().roundCeilingCopy(), 2005, 6, 9, 11, 0, 0, 0); 559 check(test.hourOfDay().roundFloorCopy(), 2005, 6, 9, 10, 0, 0, 0); 560 check(test.hourOfDay().roundHalfCeilingCopy(), 2005, 6, 9, 11, 0, 0, 0); 561 check(test.hourOfDay().roundHalfFloorCopy(), 2005, 6, 9, 11, 0, 0, 0); 562 check(test.hourOfDay().roundHalfEvenCopy(), 2005, 6, 9, 11, 0, 0, 0); 563 564 test = new LocalDateTime(2005, 6, 9, 10, 30); 565 check(test.hourOfDay().roundCeilingCopy(), 2005, 6, 9, 11, 0, 0, 0); 566 check(test.hourOfDay().roundFloorCopy(), 2005, 6, 9, 10, 0, 0, 0); 567 check(test.hourOfDay().roundHalfCeilingCopy(), 2005, 6, 9, 11, 0, 0, 0); 568 check(test.hourOfDay().roundHalfFloorCopy(), 2005, 6, 9, 10, 0, 0, 0); 569 check(test.hourOfDay().roundHalfEvenCopy(), 2005, 6, 9, 10, 0, 0, 0); 570 571 test = new LocalDateTime(2005, 6, 9, 11, 30); 572 check(test.hourOfDay().roundCeilingCopy(), 2005, 6, 9, 12, 0, 0, 0); 573 check(test.hourOfDay().roundFloorCopy(), 2005, 6, 9, 11, 0, 0, 0); 574 check(test.hourOfDay().roundHalfCeilingCopy(), 2005, 6, 9, 12, 0, 0, 0); 575 check(test.hourOfDay().roundHalfFloorCopy(), 2005, 6, 9, 11, 0, 0, 0); 576 check(test.hourOfDay().roundHalfEvenCopy(), 2005, 6, 9, 12, 0, 0, 0); 577 } 578 579 public void testPropertyGetMaxMinValuesHour() { 580 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 581 assertEquals(0, test.hourOfDay().getMinimumValue()); 582 assertEquals(0, test.hourOfDay().getMinimumValueOverall()); 583 assertEquals(23, test.hourOfDay().getMaximumValue()); 584 assertEquals(23, test.hourOfDay().getMaximumValueOverall()); 585 } 586 587 public void testPropertyWithMaxMinValueHour() { 588 LocalDateTime test = new LocalDateTime(2005, 6, 9, 0, 20, 30, 40); 589 check(test.hourOfDay().withMaximumValue(), 2005, 6, 9, 23, 20, 30, 40); 590 check(test.hourOfDay().withMinimumValue(), 2005, 6, 9, 0, 20, 30, 40); 591 } 592 593 public void testPropertyAddToCopyHour() { 594 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 595 LocalDateTime copy = test.hourOfDay().addToCopy(9); 596 check(test, 2005, 6, 9, 10, 20, 30, 40); 597 check(copy, 2005, 6, 9, 19, 20, 30, 40); 598 599 copy = test.hourOfDay().addToCopy(0); 600 check(copy, 2005, 6, 9, 10, 20, 30, 40); 601 602 copy = test.hourOfDay().addToCopy(13); 603 check(copy, 2005, 6, 9, 23, 20, 30, 40); 604 605 copy = test.hourOfDay().addToCopy(14); 606 check(copy, 2005, 6, 10, 0, 20, 30, 40); 607 608 copy = test.hourOfDay().addToCopy(-10); 609 check(copy, 2005, 6, 9, 0, 20, 30, 40); 610 611 copy = test.hourOfDay().addToCopy(-11); 612 check(copy, 2005, 6, 8, 23, 20, 30, 40); 613 } 614 615 public void testPropertyAddWrapFieldToCopyHour() { 616 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 617 LocalDateTime copy = test.hourOfDay().addWrapFieldToCopy(9); 618 check(test, 2005, 6, 9, 10, 20, 30, 40); 619 check(copy, 2005, 6, 9, 19, 20, 30, 40); 620 621 copy = test.hourOfDay().addWrapFieldToCopy(0); 622 check(copy, 2005, 6, 9, 10, 20, 30, 40); 623 624 copy = test.hourOfDay().addWrapFieldToCopy(18); 625 check(copy, 2005, 6, 9, 4, 20, 30, 40); 626 627 copy = test.hourOfDay().addWrapFieldToCopy(-15); 628 check(copy, 2005, 6, 9, 19, 20, 30, 40); 629 } 630 631 public void testPropertySetHour() { 632 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 633 LocalDateTime copy = test.hourOfDay().setCopy(12); 634 check(test, 2005, 6, 9, 10, 20, 30, 40); 635 check(copy, 2005, 6, 9, 12, 20, 30, 40); 636 637 try { 638 test.hourOfDay().setCopy(24); 639 fail(); 640 } catch (IllegalArgumentException ex) {} 641 try { 642 test.hourOfDay().setCopy(-1); 643 fail(); 644 } catch (IllegalArgumentException ex) {} 645 } 646 647 public void testPropertySetTextHour() { 648 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 649 LocalDateTime copy = test.hourOfDay().setCopy("12"); 650 check(test, 2005, 6, 9, 10, 20, 30, 40); 651 check(copy, 2005, 6, 9, 12, 20, 30, 40); 652 } 653 654 public void testPropertyWithMaximumValueHour() { 655 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 656 LocalDateTime copy = test.hourOfDay().withMaximumValue(); 657 check(test, 2005, 6, 9, 10, 20, 30, 40); 658 check(copy, 2005, 6, 9, 23, 20, 30, 40); 659 } 660 661 public void testPropertyWithMinimumValueHour() { 662 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 663 LocalDateTime copy = test.hourOfDay().withMinimumValue(); 664 check(test, 2005, 6, 9, 10, 20, 30, 40); 665 check(copy, 2005, 6, 9, 0, 20, 30, 40); 666 } 667 668 public void testPropertyCompareToHour() { 669 LocalDateTime test1 = new LocalDateTime(TEST_TIME1); 670 LocalDateTime test2 = new LocalDateTime(TEST_TIME2); 671 assertEquals(true, test1.hourOfDay().compareTo(test2) < 0); 672 assertEquals(true, test2.hourOfDay().compareTo(test1) > 0); 673 assertEquals(true, test1.hourOfDay().compareTo(test1) == 0); 674 try { 675 test1.hourOfDay().compareTo((ReadablePartial) null); 676 fail(); 677 } catch (IllegalArgumentException ex) {} 678 679 DateTime dt1 = new DateTime(TEST_TIME1); 680 DateTime dt2 = new DateTime(TEST_TIME2); 681 assertEquals(true, test1.hourOfDay().compareTo(dt2) < 0); 682 assertEquals(true, test2.hourOfDay().compareTo(dt1) > 0); 683 assertEquals(true, test1.hourOfDay().compareTo(dt1) == 0); 684 try { 685 test1.hourOfDay().compareTo((ReadableInstant) null); 686 fail(); 687 } catch (IllegalArgumentException ex) {} 688 } 689 690 //----------------------------------------------------------------------- 691 public void testPropertyGetMinute() { 692 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 693 assertSame(test.getChronology().minuteOfHour(), test.minuteOfHour().getField()); 694 assertEquals("minuteOfHour", test.minuteOfHour().getName()); 695 assertEquals("Property[minuteOfHour]", test.minuteOfHour().toString()); 696 assertSame(test, test.minuteOfHour().getLocalDateTime()); 697 assertEquals(20, test.minuteOfHour().get()); 698 assertEquals("20", test.minuteOfHour().getAsString()); 699 assertEquals("20", test.minuteOfHour().getAsText()); 700 assertEquals("20", test.minuteOfHour().getAsText(Locale.FRENCH)); 701 assertEquals("20", test.minuteOfHour().getAsShortText()); 702 assertEquals("20", test.minuteOfHour().getAsShortText(Locale.FRENCH)); 703 assertEquals(test.getChronology().minutes(), test.minuteOfHour().getDurationField()); 704 assertEquals(test.getChronology().hours(), test.minuteOfHour().getRangeDurationField()); 705 assertEquals(2, test.minuteOfHour().getMaximumTextLength(null)); 706 assertEquals(2, test.minuteOfHour().getMaximumShortTextLength(null)); 707 } 708 709 public void testPropertyGetMaxMinValuesMinute() { 710 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 711 assertEquals(0, test.minuteOfHour().getMinimumValue()); 712 assertEquals(0, test.minuteOfHour().getMinimumValueOverall()); 713 assertEquals(59, test.minuteOfHour().getMaximumValue()); 714 assertEquals(59, test.minuteOfHour().getMaximumValueOverall()); 715 } 716 717 public void testPropertyWithMaxMinValueMinute() { 718 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 719 check(test.minuteOfHour().withMaximumValue(), 2005, 6, 9, 10, 59, 30, 40); 720 check(test.minuteOfHour().withMinimumValue(), 2005, 6, 9, 10, 0, 30, 40); 721 } 722 723 public void testPropertyAddToCopyMinute() { 724 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 725 LocalDateTime copy = test.minuteOfHour().addToCopy(9); 726 check(test, 2005, 6, 9, 10, 20, 30, 40); 727 check(copy, 2005, 6, 9, 10, 29, 30, 40); 728 729 copy = test.minuteOfHour().addToCopy(39); 730 check(copy, 2005, 6, 9, 10, 59, 30, 40); 731 732 copy = test.minuteOfHour().addToCopy(40); 733 check(copy, 2005, 6, 9, 11, 0, 30, 40); 734 735 copy = test.minuteOfHour().addToCopy(1 * 60 + 45); 736 check(copy, 2005, 6, 9, 12, 5, 30, 40); 737 738 copy = test.minuteOfHour().addToCopy(13 * 60 + 39); 739 check(copy, 2005, 6, 9, 23, 59, 30, 40); 740 741 copy = test.minuteOfHour().addToCopy(13 * 60 + 40); 742 check(copy, 2005, 6, 10, 0, 0, 30, 40); 743 744 copy = test.minuteOfHour().addToCopy(-9); 745 check(copy, 2005, 6, 9, 10, 11, 30, 40); 746 747 copy = test.minuteOfHour().addToCopy(-19); 748 check(copy, 2005, 6, 9, 10, 1, 30, 40); 749 750 copy = test.minuteOfHour().addToCopy(-20); 751 check(copy, 2005, 6, 9, 10, 0, 30, 40); 752 753 copy = test.minuteOfHour().addToCopy(-21); 754 check(copy, 2005, 6, 9, 9, 59, 30, 40); 755 756 copy = test.minuteOfHour().addToCopy(-(10 * 60 + 20)); 757 check(copy, 2005, 6, 9, 0, 0, 30, 40); 758 759 copy = test.minuteOfHour().addToCopy(-(10 * 60 + 21)); 760 check(copy, 2005, 6, 8, 23, 59, 30, 40); 761 } 762 763 public void testPropertyAddWrapFieldToCopyMinute() { 764 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 765 LocalDateTime copy = test.minuteOfHour().addWrapFieldToCopy(9); 766 check(test, 2005, 6, 9, 10, 20, 30, 40); 767 check(copy, 2005, 6, 9, 10, 29, 30, 40); 768 769 copy = test.minuteOfHour().addWrapFieldToCopy(49); 770 check(copy, 2005, 6, 9, 10, 9, 30, 40); 771 772 copy = test.minuteOfHour().addWrapFieldToCopy(-47); 773 check(copy, 2005, 6, 9, 10, 33, 30, 40); 774 } 775 776 public void testPropertySetMinute() { 777 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 778 LocalDateTime copy = test.minuteOfHour().setCopy(12); 779 check(test, 2005, 6, 9, 10, 20, 30, 40); 780 check(copy, 2005, 6, 9, 10, 12, 30, 40); 781 782 try { 783 test.minuteOfHour().setCopy(60); 784 fail(); 785 } catch (IllegalArgumentException ex) {} 786 try { 787 test.minuteOfHour().setCopy(-1); 788 fail(); 789 } catch (IllegalArgumentException ex) {} 790 } 791 792 public void testPropertySetTextMinute() { 793 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 794 LocalDateTime copy = test.minuteOfHour().setCopy("12"); 795 check(test, 2005, 6, 9, 10, 20, 30, 40); 796 check(copy, 2005, 6, 9, 10, 12, 30, 40); 797 } 798 799 public void testPropertyCompareToMinute() { 800 LocalDateTime test1 = new LocalDateTime(TEST_TIME1); 801 LocalDateTime test2 = new LocalDateTime(TEST_TIME2); 802 assertEquals(true, test1.minuteOfHour().compareTo(test2) < 0); 803 assertEquals(true, test2.minuteOfHour().compareTo(test1) > 0); 804 assertEquals(true, test1.minuteOfHour().compareTo(test1) == 0); 805 try { 806 test1.minuteOfHour().compareTo((ReadablePartial) null); 807 fail(); 808 } catch (IllegalArgumentException ex) {} 809 810 DateTime dt1 = new DateTime(TEST_TIME1); 811 DateTime dt2 = new DateTime(TEST_TIME2); 812 assertEquals(true, test1.minuteOfHour().compareTo(dt2) < 0); 813 assertEquals(true, test2.minuteOfHour().compareTo(dt1) > 0); 814 assertEquals(true, test1.minuteOfHour().compareTo(dt1) == 0); 815 try { 816 test1.minuteOfHour().compareTo((ReadableInstant) null); 817 fail(); 818 } catch (IllegalArgumentException ex) {} 819 } 820 821 //----------------------------------------------------------------------- 822 public void testPropertyGetSecond() { 823 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 824 assertSame(test.getChronology().secondOfMinute(), test.secondOfMinute().getField()); 825 assertEquals("secondOfMinute", test.secondOfMinute().getName()); 826 assertEquals("Property[secondOfMinute]", test.secondOfMinute().toString()); 827 assertSame(test, test.secondOfMinute().getLocalDateTime()); 828 assertEquals(30, test.secondOfMinute().get()); 829 assertEquals("30", test.secondOfMinute().getAsString()); 830 assertEquals("30", test.secondOfMinute().getAsText()); 831 assertEquals("30", test.secondOfMinute().getAsText(Locale.FRENCH)); 832 assertEquals("30", test.secondOfMinute().getAsShortText()); 833 assertEquals("30", test.secondOfMinute().getAsShortText(Locale.FRENCH)); 834 assertEquals(test.getChronology().seconds(), test.secondOfMinute().getDurationField()); 835 assertEquals(test.getChronology().minutes(), test.secondOfMinute().getRangeDurationField()); 836 assertEquals(2, test.secondOfMinute().getMaximumTextLength(null)); 837 assertEquals(2, test.secondOfMinute().getMaximumShortTextLength(null)); 838 } 839 840 public void testPropertyGetMaxMinValuesSecond() { 841 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 842 assertEquals(0, test.secondOfMinute().getMinimumValue()); 843 assertEquals(0, test.secondOfMinute().getMinimumValueOverall()); 844 assertEquals(59, test.secondOfMinute().getMaximumValue()); 845 assertEquals(59, test.secondOfMinute().getMaximumValueOverall()); 846 } 847 848 public void testPropertyWithMaxMinValueSecond() { 849 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 850 check(test.secondOfMinute().withMaximumValue(), 2005, 6, 9, 10, 20, 59, 40); 851 check(test.secondOfMinute().withMinimumValue(), 2005, 6, 9, 10, 20, 0, 40); 852 } 853 854 public void testPropertyAddToCopySecond() { 855 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 856 LocalDateTime copy = test.secondOfMinute().addToCopy(9); 857 check(test, 2005, 6, 9, 10, 20, 30, 40); 858 check(copy, 2005, 6, 9, 10, 20, 39, 40); 859 860 copy = test.secondOfMinute().addToCopy(29); 861 check(copy, 2005, 6, 9, 10, 20, 59, 40); 862 863 copy = test.secondOfMinute().addToCopy(30); 864 check(copy, 2005, 6, 9, 10, 21, 0, 40); 865 866 copy = test.secondOfMinute().addToCopy(39 * 60 + 29); 867 check(copy, 2005, 6, 9, 10, 59, 59, 40); 868 869 copy = test.secondOfMinute().addToCopy(39 * 60 + 30); 870 check(copy, 2005, 6, 9, 11, 0, 0, 40); 871 872 copy = test.secondOfMinute().addToCopy(13 * 60 * 60 + 39 * 60 + 30); 873 check(copy, 2005, 6, 10, 0, 0, 0, 40); 874 875 copy = test.secondOfMinute().addToCopy(-9); 876 check(copy, 2005, 6, 9, 10, 20, 21, 40); 877 878 copy = test.secondOfMinute().addToCopy(-30); 879 check(copy, 2005, 6, 9, 10, 20, 0, 40); 880 881 copy = test.secondOfMinute().addToCopy(-31); 882 check(copy, 2005, 6, 9, 10, 19, 59, 40); 883 884 copy = test.secondOfMinute().addToCopy(-(10 * 60 * 60 + 20 * 60 + 30)); 885 check(copy, 2005, 6, 9, 0, 0, 0, 40); 886 887 copy = test.secondOfMinute().addToCopy(-(10 * 60 * 60 + 20 * 60 + 31)); 888 check(copy, 2005, 6, 8, 23, 59, 59, 40); 889 } 890 891 public void testPropertyAddWrapFieldToCopySecond() { 892 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 893 LocalDateTime copy = test.secondOfMinute().addWrapFieldToCopy(9); 894 check(test, 2005, 6, 9, 10, 20, 30, 40); 895 check(copy, 2005, 6, 9, 10, 20, 39, 40); 896 897 copy = test.secondOfMinute().addWrapFieldToCopy(49); 898 check(copy, 2005, 6, 9, 10, 20, 19, 40); 899 900 copy = test.secondOfMinute().addWrapFieldToCopy(-47); 901 check(copy, 2005, 6, 9, 10, 20, 43, 40); 902 } 903 904 public void testPropertySetSecond() { 905 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 906 LocalDateTime copy = test.secondOfMinute().setCopy(12); 907 check(test, 2005, 6, 9, 10, 20, 30, 40); 908 check(copy, 2005, 6, 9, 10, 20, 12, 40); 909 910 try { 911 test.secondOfMinute().setCopy(60); 912 fail(); 913 } catch (IllegalArgumentException ex) {} 914 try { 915 test.secondOfMinute().setCopy(-1); 916 fail(); 917 } catch (IllegalArgumentException ex) {} 918 } 919 920 public void testPropertySetTextSecond() { 921 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 922 LocalDateTime copy = test.secondOfMinute().setCopy("12"); 923 check(test, 2005, 6, 9, 10, 20, 30, 40); 924 check(copy, 2005, 6, 9, 10, 20, 12, 40); 925 } 926 927 public void testPropertyCompareToSecond() { 928 LocalDateTime test1 = new LocalDateTime(TEST_TIME1); 929 LocalDateTime test2 = new LocalDateTime(TEST_TIME2); 930 assertEquals(true, test1.secondOfMinute().compareTo(test2) < 0); 931 assertEquals(true, test2.secondOfMinute().compareTo(test1) > 0); 932 assertEquals(true, test1.secondOfMinute().compareTo(test1) == 0); 933 try { 934 test1.secondOfMinute().compareTo((ReadablePartial) null); 935 fail(); 936 } catch (IllegalArgumentException ex) {} 937 938 DateTime dt1 = new DateTime(TEST_TIME1); 939 DateTime dt2 = new DateTime(TEST_TIME2); 940 assertEquals(true, test1.secondOfMinute().compareTo(dt2) < 0); 941 assertEquals(true, test2.secondOfMinute().compareTo(dt1) > 0); 942 assertEquals(true, test1.secondOfMinute().compareTo(dt1) == 0); 943 try { 944 test1.secondOfMinute().compareTo((ReadableInstant) null); 945 fail(); 946 } catch (IllegalArgumentException ex) {} 947 } 948 949 //----------------------------------------------------------------------- 950 public void testPropertyGetMilli() { 951 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 952 assertSame(test.getChronology().millisOfSecond(), test.millisOfSecond().getField()); 953 assertEquals("millisOfSecond", test.millisOfSecond().getName()); 954 assertEquals("Property[millisOfSecond]", test.millisOfSecond().toString()); 955 assertSame(test, test.millisOfSecond().getLocalDateTime()); 956 assertEquals(40, test.millisOfSecond().get()); 957 assertEquals("40", test.millisOfSecond().getAsString()); 958 assertEquals("40", test.millisOfSecond().getAsText()); 959 assertEquals("40", test.millisOfSecond().getAsText(Locale.FRENCH)); 960 assertEquals("40", test.millisOfSecond().getAsShortText()); 961 assertEquals("40", test.millisOfSecond().getAsShortText(Locale.FRENCH)); 962 assertEquals(test.getChronology().millis(), test.millisOfSecond().getDurationField()); 963 assertEquals(test.getChronology().seconds(), test.millisOfSecond().getRangeDurationField()); 964 assertEquals(3, test.millisOfSecond().getMaximumTextLength(null)); 965 assertEquals(3, test.millisOfSecond().getMaximumShortTextLength(null)); 966 } 967 968 public void testPropertyGetMaxMinValuesMilli() { 969 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 970 assertEquals(0, test.millisOfSecond().getMinimumValue()); 971 assertEquals(0, test.millisOfSecond().getMinimumValueOverall()); 972 assertEquals(999, test.millisOfSecond().getMaximumValue()); 973 assertEquals(999, test.millisOfSecond().getMaximumValueOverall()); 974 } 975 976 public void testPropertyWithMaxMinValueMilli() { 977 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 978 check(test.millisOfSecond().withMaximumValue(), 2005, 6, 9, 10, 20, 30, 999); 979 check(test.millisOfSecond().withMinimumValue(), 2005, 6, 9, 10, 20, 30, 0); 980 } 981 982 public void testPropertyAddToCopyMilli() { 983 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 984 LocalDateTime copy = test.millisOfSecond().addToCopy(9); 985 check(test, 2005, 6, 9, 10, 20, 30, 40); 986 check(copy, 2005, 6, 9, 10, 20, 30, 49); 987 988 copy = test.millisOfSecond().addToCopy(959); 989 check(copy, 2005, 6, 9, 10, 20, 30, 999); 990 991 copy = test.millisOfSecond().addToCopy(960); 992 check(copy, 2005, 6, 9, 10, 20, 31, 0); 993 994 copy = test.millisOfSecond().addToCopy(13 * 60 * 60 * 1000 + 39 * 60 * 1000 + 29 * 1000 + 959); 995 check(copy, 2005, 6, 9, 23, 59, 59, 999); 996 997 copy = test.millisOfSecond().addToCopy(13 * 60 * 60 * 1000 + 39 * 60 * 1000 + 29 * 1000 + 960); 998 check(copy, 2005, 6, 10, 0, 0, 0, 0); 999 1000 copy = test.millisOfSecond().addToCopy(-9); 1001 check(copy, 2005, 6, 9, 10, 20, 30, 31); 1002 1003 copy = test.millisOfSecond().addToCopy(-40); 1004 check(copy, 2005, 6, 9, 10, 20, 30, 0); 1005 1006 copy = test.millisOfSecond().addToCopy(-41); 1007 check(copy, 2005, 6, 9, 10, 20, 29, 999); 1008 1009 copy = test.millisOfSecond().addToCopy(-(10 * 60 * 60 * 1000 + 20 * 60 * 1000 + 30 * 1000 + 40)); 1010 check(copy, 2005, 6, 9, 0, 0, 0, 0); 1011 1012 copy = test.millisOfSecond().addToCopy(-(10 * 60 * 60 * 1000 + 20 * 60 * 1000 + 30 * 1000 + 41)); 1013 check(copy, 2005, 6, 8, 23, 59, 59, 999); 1014 } 1015 1016 public void testPropertyAddWrapFieldToCopyMilli() { 1017 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 1018 LocalDateTime copy = test.millisOfSecond().addWrapFieldToCopy(9); 1019 check(test, 2005, 6, 9, 10, 20, 30, 40); 1020 check(copy, 2005, 6, 9, 10, 20, 30, 49); 1021 1022 copy = test.millisOfSecond().addWrapFieldToCopy(995); 1023 check(copy, 2005, 6, 9, 10, 20, 30, 35); 1024 1025 copy = test.millisOfSecond().addWrapFieldToCopy(-47); 1026 check(copy, 2005, 6, 9, 10, 20, 30, 993); 1027 } 1028 1029 public void testPropertySetMilli() { 1030 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 1031 LocalDateTime copy = test.millisOfSecond().setCopy(12); 1032 check(test, 2005, 6, 9, 10, 20, 30, 40); 1033 check(copy, 2005, 6, 9, 10, 20, 30, 12); 1034 1035 try { 1036 test.millisOfSecond().setCopy(1000); 1037 fail(); 1038 } catch (IllegalArgumentException ex) {} 1039 try { 1040 test.millisOfSecond().setCopy(-1); 1041 fail(); 1042 } catch (IllegalArgumentException ex) {} 1043 } 1044 1045 public void testPropertySetTextMilli() { 1046 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40); 1047 LocalDateTime copy = test.millisOfSecond().setCopy("12"); 1048 check(test, 2005, 6, 9, 10, 20, 30, 40); 1049 check(copy, 2005, 6, 9, 10, 20, 30, 12); 1050 } 1051 1052 public void testPropertyCompareToMilli() { 1053 LocalDateTime test1 = new LocalDateTime(TEST_TIME1); 1054 LocalDateTime test2 = new LocalDateTime(TEST_TIME2); 1055 assertEquals(true, test1.millisOfSecond().compareTo(test2) < 0); 1056 assertEquals(true, test2.millisOfSecond().compareTo(test1) > 0); 1057 assertEquals(true, test1.millisOfSecond().compareTo(test1) == 0); 1058 try { 1059 test1.millisOfSecond().compareTo((ReadablePartial) null); 1060 fail(); 1061 } catch (IllegalArgumentException ex) {} 1062 1063 DateTime dt1 = new DateTime(TEST_TIME1); 1064 DateTime dt2 = new DateTime(TEST_TIME2); 1065 assertEquals(true, test1.millisOfSecond().compareTo(dt2) < 0); 1066 assertEquals(true, test2.millisOfSecond().compareTo(dt1) > 0); 1067 assertEquals(true, test1.millisOfSecond().compareTo(dt1) == 0); 1068 try { 1069 test1.millisOfSecond().compareTo((ReadableInstant) null); 1070 fail(); 1071 } catch (IllegalArgumentException ex) {} 1072 } 1073 1074 //----------------------------------------------------------------------- 1075 private void check(LocalDateTime test, int year, int month, int day, int hour, int min, int sec, int mil) { 1076 assertEquals(year, test.getYear()); 1077 assertEquals(month, test.getMonthOfYear()); 1078 assertEquals(day, test.getDayOfMonth()); 1079 assertEquals(hour, test.getHourOfDay()); 1080 assertEquals(min, test.getMinuteOfHour()); 1081 assertEquals(sec, test.getSecondOfMinute()); 1082 assertEquals(mil, test.getMillisOfSecond()); 1083 } 1084 }