1 16 package org.joda.time; 17 18 import java.util.Locale ; 19 20 import junit.framework.TestCase; 21 import junit.framework.TestSuite; 22 23 28 public class TestPartial_Properties extends TestCase { 29 30 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 31 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 32 33 private DateTimeZone zone = null; 34 private static final DateTimeFieldType[] TYPES = new DateTimeFieldType[] { 35 DateTimeFieldType.hourOfDay(), 36 DateTimeFieldType.minuteOfHour(), 37 DateTimeFieldType.secondOfMinute(), 38 DateTimeFieldType.millisOfSecond() 39 }; 40 private static final int[] VALUES = new int[] {10, 20, 30, 40}; 41 private static final int[] VALUES1 = new int[] {1, 2, 3, 4}; 42 private static final int[] VALUES2 = new int[] {5, 6, 7, 8}; 43 44 private long TEST_TIME1 = 51 1L * DateTimeConstants.MILLIS_PER_HOUR 52 + 2L * DateTimeConstants.MILLIS_PER_MINUTE 53 + 3L * DateTimeConstants.MILLIS_PER_SECOND 54 + 4L; 55 private long TEST_TIME2 = 56 1L * DateTimeConstants.MILLIS_PER_DAY 57 + 5L * DateTimeConstants.MILLIS_PER_HOUR 58 + 6L * DateTimeConstants.MILLIS_PER_MINUTE 59 + 7L * DateTimeConstants.MILLIS_PER_SECOND 60 + 8L; 61 62 public static void main(String [] args) { 63 junit.textui.TestRunner.run(suite()); 64 } 65 66 public static TestSuite suite() { 67 return new TestSuite(TestPartial_Properties.class); 68 } 69 70 public TestPartial_Properties(String name) { 71 super(name); 72 } 73 74 protected void setUp() throws Exception { 75 zone = DateTimeZone.getDefault(); 76 DateTimeZone.setDefault(DateTimeZone.UTC); 77 } 78 79 protected void tearDown() throws Exception { 80 DateTimeZone.setDefault(zone); 81 zone = null; 82 } 83 84 public void testPropertyGetHour() { 86 Partial test = new Partial(TYPES, VALUES); 87 assertSame(test.getChronology().hourOfDay(), test.property(DateTimeFieldType.hourOfDay()).getField()); 88 assertEquals("hourOfDay", test.property(DateTimeFieldType.hourOfDay()).getName()); 89 assertEquals("Property[hourOfDay]", test.property(DateTimeFieldType.hourOfDay()).toString()); 90 assertSame(test, test.property(DateTimeFieldType.hourOfDay()).getReadablePartial()); 91 assertSame(test, test.property(DateTimeFieldType.hourOfDay()).getPartial()); 92 assertEquals(10, test.property(DateTimeFieldType.hourOfDay()).get()); 93 assertEquals("10", test.property(DateTimeFieldType.hourOfDay()).getAsString()); 94 assertEquals("10", test.property(DateTimeFieldType.hourOfDay()).getAsText()); 95 assertEquals("10", test.property(DateTimeFieldType.hourOfDay()).getAsText(Locale.FRENCH)); 96 assertEquals("10", test.property(DateTimeFieldType.hourOfDay()).getAsShortText()); 97 assertEquals("10", test.property(DateTimeFieldType.hourOfDay()).getAsShortText(Locale.FRENCH)); 98 assertEquals(test.getChronology().hours(), test.property(DateTimeFieldType.hourOfDay()).getDurationField()); 99 assertEquals(test.getChronology().days(), test.property(DateTimeFieldType.hourOfDay()).getRangeDurationField()); 100 assertEquals(2, test.property(DateTimeFieldType.hourOfDay()).getMaximumTextLength(null)); 101 assertEquals(2, test.property(DateTimeFieldType.hourOfDay()).getMaximumShortTextLength(null)); 102 } 103 104 public void testPropertyGetMaxMinValuesHour() { 105 Partial test = new Partial(TYPES, VALUES); 106 assertEquals(0, test.property(DateTimeFieldType.hourOfDay()).getMinimumValue()); 107 assertEquals(0, test.property(DateTimeFieldType.hourOfDay()).getMinimumValueOverall()); 108 assertEquals(23, test.property(DateTimeFieldType.hourOfDay()).getMaximumValue()); 109 assertEquals(23, test.property(DateTimeFieldType.hourOfDay()).getMaximumValueOverall()); 110 } 111 112 public void testPropertyAddHour() { 135 Partial test = new Partial(TYPES, VALUES); 136 Partial copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(9); 137 check(test, 10, 20, 30, 40); 138 check(copy, 19, 20, 30, 40); 139 140 copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(0); 141 check(copy, 10, 20, 30, 40); 142 143 copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(13); 144 check(copy, 23, 20, 30, 40); 145 146 try { 147 test.property(DateTimeFieldType.hourOfDay()).addToCopy(14); 148 fail(); 149 } catch (IllegalArgumentException ex) {} 150 check(test, 10, 20, 30, 40); 151 152 copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(-10); 153 check(copy, 0, 20, 30, 40); 154 155 try { 156 test.property(DateTimeFieldType.hourOfDay()).addToCopy(-11); 157 fail(); 158 } catch (IllegalArgumentException ex) {} 159 check(test, 10, 20, 30, 40); 160 } 161 162 public void testPropertyAddWrapFieldHour() { 163 Partial test = new Partial(TYPES, VALUES); 164 Partial copy = test.property(DateTimeFieldType.hourOfDay()).addWrapFieldToCopy(9); 165 check(test, 10, 20, 30, 40); 166 check(copy, 19, 20, 30, 40); 167 168 copy = test.property(DateTimeFieldType.hourOfDay()).addWrapFieldToCopy(0); 169 check(copy, 10, 20, 30, 40); 170 171 copy = test.property(DateTimeFieldType.hourOfDay()).addWrapFieldToCopy(18); 172 check(copy, 4, 20, 30, 40); 173 174 copy = test.property(DateTimeFieldType.hourOfDay()).addWrapFieldToCopy(-15); 175 check(copy, 19, 20, 30, 40); 176 } 177 178 public void testPropertySetHour() { 179 Partial test = new Partial(TYPES, VALUES); 180 Partial copy = test.property(DateTimeFieldType.hourOfDay()).setCopy(12); 181 check(test, 10, 20, 30, 40); 182 check(copy, 12, 20, 30, 40); 183 184 try { 185 test.property(DateTimeFieldType.hourOfDay()).setCopy(24); 186 fail(); 187 } catch (IllegalArgumentException ex) {} 188 try { 189 test.property(DateTimeFieldType.hourOfDay()).setCopy(-1); 190 fail(); 191 } catch (IllegalArgumentException ex) {} 192 } 193 194 public void testPropertySetTextHour() { 195 Partial test = new Partial(TYPES, VALUES); 196 Partial copy = test.property(DateTimeFieldType.hourOfDay()).setCopy("12"); 197 check(test, 10, 20, 30, 40); 198 check(copy, 12, 20, 30, 40); 199 } 200 201 public void testPropertyWithMaximumValueHour() { 202 Partial test = new Partial(TYPES, VALUES); 203 Partial copy = test.property(DateTimeFieldType.hourOfDay()).withMaximumValue(); 204 check(test, 10, 20, 30, 40); 205 check(copy, 23, 20, 30, 40); 206 } 207 208 public void testPropertyWithMinimumValueHour() { 209 Partial test = new Partial(TYPES, VALUES); 210 Partial copy = test.property(DateTimeFieldType.hourOfDay()).withMinimumValue(); 211 check(test, 10, 20, 30, 40); 212 check(copy, 0, 20, 30, 40); 213 } 214 215 public void testPropertyCompareToHour() { 216 Partial test1 = new Partial(TYPES, VALUES1); 217 Partial test2 = new Partial(TYPES, VALUES2); 218 assertEquals(true, test1.property(DateTimeFieldType.hourOfDay()).compareTo(test2) < 0); 219 assertEquals(true, test2.property(DateTimeFieldType.hourOfDay()).compareTo(test1) > 0); 220 assertEquals(true, test1.property(DateTimeFieldType.hourOfDay()).compareTo(test1) == 0); 221 try { 222 test1.property(DateTimeFieldType.hourOfDay()).compareTo((ReadablePartial) null); 223 fail(); 224 } catch (IllegalArgumentException ex) {} 225 226 DateTime dt1 = new DateTime(TEST_TIME1); 227 DateTime dt2 = new DateTime(TEST_TIME2); 228 assertEquals(true, test1.property(DateTimeFieldType.hourOfDay()).compareTo(dt2) < 0); 229 assertEquals(true, test2.property(DateTimeFieldType.hourOfDay()).compareTo(dt1) > 0); 230 assertEquals(true, test1.property(DateTimeFieldType.hourOfDay()).compareTo(dt1) == 0); 231 try { 232 test1.property(DateTimeFieldType.hourOfDay()).compareTo((ReadableInstant) null); 233 fail(); 234 } catch (IllegalArgumentException ex) {} 235 } 236 237 public void testPropertyGetMinute() { 239 Partial test = new Partial(TYPES, VALUES); 240 assertSame(test.getChronology().minuteOfHour(), test.property(DateTimeFieldType.minuteOfHour()).getField()); 241 assertEquals("minuteOfHour", test.property(DateTimeFieldType.minuteOfHour()).getName()); 242 assertEquals("Property[minuteOfHour]", test.property(DateTimeFieldType.minuteOfHour()).toString()); 243 assertSame(test, test.property(DateTimeFieldType.minuteOfHour()).getReadablePartial()); 244 assertSame(test, test.property(DateTimeFieldType.minuteOfHour()).getPartial()); 245 assertEquals(20, test.property(DateTimeFieldType.minuteOfHour()).get()); 246 assertEquals("20", test.property(DateTimeFieldType.minuteOfHour()).getAsString()); 247 assertEquals("20", test.property(DateTimeFieldType.minuteOfHour()).getAsText()); 248 assertEquals("20", test.property(DateTimeFieldType.minuteOfHour()).getAsText(Locale.FRENCH)); 249 assertEquals("20", test.property(DateTimeFieldType.minuteOfHour()).getAsShortText()); 250 assertEquals("20", test.property(DateTimeFieldType.minuteOfHour()).getAsShortText(Locale.FRENCH)); 251 assertEquals(test.getChronology().minutes(), test.property(DateTimeFieldType.minuteOfHour()).getDurationField()); 252 assertEquals(test.getChronology().hours(), test.property(DateTimeFieldType.minuteOfHour()).getRangeDurationField()); 253 assertEquals(2, test.property(DateTimeFieldType.minuteOfHour()).getMaximumTextLength(null)); 254 assertEquals(2, test.property(DateTimeFieldType.minuteOfHour()).getMaximumShortTextLength(null)); 255 } 256 257 public void testPropertyGetMaxMinValuesMinute() { 258 Partial test = new Partial(TYPES, VALUES); 259 assertEquals(0, test.property(DateTimeFieldType.minuteOfHour()).getMinimumValue()); 260 assertEquals(0, test.property(DateTimeFieldType.minuteOfHour()).getMinimumValueOverall()); 261 assertEquals(59, test.property(DateTimeFieldType.minuteOfHour()).getMaximumValue()); 262 assertEquals(59, test.property(DateTimeFieldType.minuteOfHour()).getMaximumValueOverall()); 263 } 264 265 305 public void testPropertyAddMinute() { 306 Partial test = new Partial(TYPES, VALUES); 307 Partial copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(9); 308 check(test, 10, 20, 30, 40); 309 check(copy, 10, 29, 30, 40); 310 311 copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(39); 312 check(copy, 10, 59, 30, 40); 313 314 copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(40); 315 check(copy, 11, 0, 30, 40); 316 317 copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(1 * 60 + 45); 318 check(copy, 12, 5, 30, 40); 319 320 copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(13 * 60 + 39); 321 check(copy, 23, 59, 30, 40); 322 323 try { 324 test.property(DateTimeFieldType.minuteOfHour()).addToCopy(13 * 60 + 40); 325 fail(); 326 } catch (IllegalArgumentException ex) {} 327 check(test, 10, 20, 30, 40); 328 329 copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-9); 330 check(copy, 10, 11, 30, 40); 331 332 copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-19); 333 check(copy, 10, 1, 30, 40); 334 335 copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-20); 336 check(copy, 10, 0, 30, 40); 337 338 copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-21); 339 check(copy, 9, 59, 30, 40); 340 341 copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-(10 * 60 + 20)); 342 check(copy, 0, 0, 30, 40); 343 344 try { 345 test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-(10 * 60 + 21)); 346 fail(); 347 } catch (IllegalArgumentException ex) {} 348 check(test, 10, 20, 30, 40); 349 } 350 351 public void testPropertyAddWrapFieldMinute() { 352 Partial test = new Partial(TYPES, VALUES); 353 Partial copy = test.property(DateTimeFieldType.minuteOfHour()).addWrapFieldToCopy(9); 354 check(test, 10, 20, 30, 40); 355 check(copy, 10, 29, 30, 40); 356 357 copy = test.property(DateTimeFieldType.minuteOfHour()).addWrapFieldToCopy(49); 358 check(copy, 10, 9, 30, 40); 359 360 copy = test.property(DateTimeFieldType.minuteOfHour()).addWrapFieldToCopy(-47); 361 check(copy, 10, 33, 30, 40); 362 } 363 364 public void testPropertySetMinute() { 365 Partial test = new Partial(TYPES, VALUES); 366 Partial copy = test.property(DateTimeFieldType.minuteOfHour()).setCopy(12); 367 check(test, 10, 20, 30, 40); 368 check(copy, 10, 12, 30, 40); 369 370 try { 371 test.property(DateTimeFieldType.minuteOfHour()).setCopy(60); 372 fail(); 373 } catch (IllegalArgumentException ex) {} 374 try { 375 test.property(DateTimeFieldType.minuteOfHour()).setCopy(-1); 376 fail(); 377 } catch (IllegalArgumentException ex) {} 378 } 379 380 public void testPropertySetTextMinute() { 381 Partial test = new Partial(TYPES, VALUES); 382 Partial copy = test.property(DateTimeFieldType.minuteOfHour()).setCopy("12"); 383 check(test, 10, 20, 30, 40); 384 check(copy, 10, 12, 30, 40); 385 } 386 387 public void testPropertyCompareToMinute() { 388 Partial test1 = new Partial(TYPES, VALUES1); 389 Partial test2 = new Partial(TYPES, VALUES2); 390 assertEquals(true, test1.property(DateTimeFieldType.minuteOfHour()).compareTo(test2) < 0); 391 assertEquals(true, test2.property(DateTimeFieldType.minuteOfHour()).compareTo(test1) > 0); 392 assertEquals(true, test1.property(DateTimeFieldType.minuteOfHour()).compareTo(test1) == 0); 393 try { 394 test1.property(DateTimeFieldType.minuteOfHour()).compareTo((ReadablePartial) null); 395 fail(); 396 } catch (IllegalArgumentException ex) {} 397 398 DateTime dt1 = new DateTime(TEST_TIME1); 399 DateTime dt2 = new DateTime(TEST_TIME2); 400 assertEquals(true, test1.property(DateTimeFieldType.minuteOfHour()).compareTo(dt2) < 0); 401 assertEquals(true, test2.property(DateTimeFieldType.minuteOfHour()).compareTo(dt1) > 0); 402 assertEquals(true, test1.property(DateTimeFieldType.minuteOfHour()).compareTo(dt1) == 0); 403 try { 404 test1.property(DateTimeFieldType.minuteOfHour()).compareTo((ReadableInstant) null); 405 fail(); 406 } catch (IllegalArgumentException ex) {} 407 } 408 409 private void check(Partial test, int hour, int min, int sec, int milli) { 411 assertEquals(hour, test.get(DateTimeFieldType.hourOfDay())); 412 assertEquals(min, test.get(DateTimeFieldType.minuteOfHour())); 413 assertEquals(sec, test.get(DateTimeFieldType.secondOfMinute())); 414 assertEquals(milli, test.get(DateTimeFieldType.millisOfSecond())); 415 } 416 } 417 | Popular Tags |