001 /* 002 * Copyright 2001-2005 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 /** 024 * This class is a Junit unit test for Partial. 025 * 026 * @author Stephen Colebourne 027 */ 028 public class TestPartial_Properties extends TestCase { 029 030 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 031 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 032 033 private DateTimeZone zone = null; 034 private static final DateTimeFieldType[] TYPES = new DateTimeFieldType[] { 035 DateTimeFieldType.hourOfDay(), 036 DateTimeFieldType.minuteOfHour(), 037 DateTimeFieldType.secondOfMinute(), 038 DateTimeFieldType.millisOfSecond() 039 }; 040 private static final int[] VALUES = new int[] {10, 20, 30, 40}; 041 private static final int[] VALUES1 = new int[] {1, 2, 3, 4}; 042 private static final int[] VALUES2 = new int[] {5, 6, 7, 8}; 043 044 // private long TEST_TIME_NOW = 045 // 10L * DateTimeConstants.MILLIS_PER_HOUR 046 // + 20L * DateTimeConstants.MILLIS_PER_MINUTE 047 // + 30L * DateTimeConstants.MILLIS_PER_SECOND 048 // + 40L; 049 // 050 private long TEST_TIME1 = 051 1L * DateTimeConstants.MILLIS_PER_HOUR 052 + 2L * DateTimeConstants.MILLIS_PER_MINUTE 053 + 3L * DateTimeConstants.MILLIS_PER_SECOND 054 + 4L; 055 private long TEST_TIME2 = 056 1L * DateTimeConstants.MILLIS_PER_DAY 057 + 5L * DateTimeConstants.MILLIS_PER_HOUR 058 + 6L * DateTimeConstants.MILLIS_PER_MINUTE 059 + 7L * DateTimeConstants.MILLIS_PER_SECOND 060 + 8L; 061 062 public static void main(String[] args) { 063 junit.textui.TestRunner.run(suite()); 064 } 065 066 public static TestSuite suite() { 067 return new TestSuite(TestPartial_Properties.class); 068 } 069 070 public TestPartial_Properties(String name) { 071 super(name); 072 } 073 074 protected void setUp() throws Exception { 075 zone = DateTimeZone.getDefault(); 076 DateTimeZone.setDefault(DateTimeZone.UTC); 077 } 078 079 protected void tearDown() throws Exception { 080 DateTimeZone.setDefault(zone); 081 zone = null; 082 } 083 084 //----------------------------------------------------------------------- 085 public void testPropertyGetHour() { 086 Partial test = new Partial(TYPES, VALUES); 087 assertSame(test.getChronology().hourOfDay(), test.property(DateTimeFieldType.hourOfDay()).getField()); 088 assertEquals("hourOfDay", test.property(DateTimeFieldType.hourOfDay()).getName()); 089 assertEquals("Property[hourOfDay]", test.property(DateTimeFieldType.hourOfDay()).toString()); 090 assertSame(test, test.property(DateTimeFieldType.hourOfDay()).getReadablePartial()); 091 assertSame(test, test.property(DateTimeFieldType.hourOfDay()).getPartial()); 092 assertEquals(10, test.property(DateTimeFieldType.hourOfDay()).get()); 093 assertEquals("10", test.property(DateTimeFieldType.hourOfDay()).getAsString()); 094 assertEquals("10", test.property(DateTimeFieldType.hourOfDay()).getAsText()); 095 assertEquals("10", test.property(DateTimeFieldType.hourOfDay()).getAsText(Locale.FRENCH)); 096 assertEquals("10", test.property(DateTimeFieldType.hourOfDay()).getAsShortText()); 097 assertEquals("10", test.property(DateTimeFieldType.hourOfDay()).getAsShortText(Locale.FRENCH)); 098 assertEquals(test.getChronology().hours(), test.property(DateTimeFieldType.hourOfDay()).getDurationField()); 099 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() { 113 // Partial test = new Partial(TYPES, VALUES); 114 // Partial copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(9); 115 // check(test, 10, 20, 30, 40); 116 // check(copy, 19, 20, 30, 40); 117 // 118 // copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(0); 119 // check(copy, 10, 20, 30, 40); 120 // 121 // copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(13); 122 // check(copy, 23, 20, 30, 40); 123 // 124 // copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(14); 125 // check(copy, 0, 20, 30, 40); 126 // 127 // copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(-10); 128 // check(copy, 0, 20, 30, 40); 129 // 130 // copy = test.property(DateTimeFieldType.hourOfDay()).addToCopy(-11); 131 // check(copy, 23, 20, 30, 40); 132 // } 133 // 134 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 //----------------------------------------------------------------------- 238 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 // public void testPropertyAddMinute() { 266 // Partial test = new Partial(TYPES, VALUES); 267 // Partial copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(9); 268 // check(test, 10, 20, 30, 40); 269 // check(copy, 10, 29, 30, 40); 270 // 271 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(39); 272 // check(copy, 10, 59, 30, 40); 273 // 274 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(40); 275 // check(copy, 11, 0, 30, 40); 276 // 277 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(1 * 60 + 45); 278 // check(copy, 12, 5, 30, 40); 279 // 280 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(13 * 60 + 39); 281 // check(copy, 23, 59, 30, 40); 282 // 283 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(13 * 60 + 40); 284 // check(copy, 0, 0, 30, 40); 285 // 286 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-9); 287 // check(copy, 10, 11, 30, 40); 288 // 289 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-19); 290 // check(copy, 10, 1, 30, 40); 291 // 292 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-20); 293 // check(copy, 10, 0, 30, 40); 294 // 295 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-21); 296 // check(copy, 9, 59, 30, 40); 297 // 298 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-(10 * 60 + 20)); 299 // check(copy, 0, 0, 30, 40); 300 // 301 // copy = test.property(DateTimeFieldType.minuteOfHour()).addToCopy(-(10 * 60 + 21)); 302 // check(copy, 23, 59, 30, 40); 303 // } 304 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 //----------------------------------------------------------------------- 410 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 }