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.Arrays; 019 020 import junit.framework.TestCase; 021 import junit.framework.TestSuite; 022 023 import org.joda.time.chrono.GregorianChronology; 024 import org.joda.time.chrono.ISOChronology; 025 026 /** 027 * This class is a Junit unit test for Partial. 028 * 029 * @author Stephen Colebourne 030 */ 031 public class TestPartial_Constructors extends TestCase { 032 033 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 034 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 035 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 036 private static final Chronology GREGORIAN_PARIS = GregorianChronology.getInstance(PARIS); 037 private static final Chronology GREGORIAN_UTC = GregorianChronology.getInstanceUTC(); 038 private static final int OFFSET = 1; 039 040 private long TEST_TIME_NOW = 041 10L * DateTimeConstants.MILLIS_PER_HOUR 042 + 20L * DateTimeConstants.MILLIS_PER_MINUTE 043 + 30L * DateTimeConstants.MILLIS_PER_SECOND 044 + 40L; 045 046 private long TEST_TIME1 = 047 1L * DateTimeConstants.MILLIS_PER_HOUR 048 + 2L * DateTimeConstants.MILLIS_PER_MINUTE 049 + 3L * DateTimeConstants.MILLIS_PER_SECOND 050 + 4L; 051 052 private long TEST_TIME2 = 053 1L * DateTimeConstants.MILLIS_PER_DAY 054 + 5L * DateTimeConstants.MILLIS_PER_HOUR 055 + 6L * DateTimeConstants.MILLIS_PER_MINUTE 056 + 7L * DateTimeConstants.MILLIS_PER_SECOND 057 + 8L; 058 059 private DateTimeZone zone = null; 060 061 public static void main(String[] args) { 062 junit.textui.TestRunner.run(suite()); 063 } 064 065 public static TestSuite suite() { 066 return new TestSuite(TestPartial_Constructors.class); 067 } 068 069 public TestPartial_Constructors(String name) { 070 super(name); 071 } 072 073 protected void setUp() throws Exception { 074 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 075 zone = DateTimeZone.getDefault(); 076 DateTimeZone.setDefault(LONDON); 077 } 078 079 protected void tearDown() throws Exception { 080 DateTimeUtils.setCurrentMillisSystem(); 081 DateTimeZone.setDefault(zone); 082 zone = null; 083 } 084 085 //----------------------------------------------------------------------- 086 /** 087 * Test constructor 088 */ 089 public void testConstructor() throws Throwable { 090 Partial test = new Partial(); 091 assertEquals(ISO_UTC, test.getChronology()); 092 assertEquals(0, test.size()); 093 } 094 095 //----------------------------------------------------------------------- 096 /** 097 * Test constructor 098 */ 099 public void testConstructor_Chrono() throws Throwable { 100 Partial test = new Partial((Chronology) null); 101 assertEquals(ISO_UTC, test.getChronology()); 102 assertEquals(0, test.size()); 103 104 test = new Partial(GREGORIAN_PARIS); 105 assertEquals(GREGORIAN_UTC, test.getChronology()); 106 assertEquals(0, test.size()); 107 } 108 109 //----------------------------------------------------------------------- 110 /** 111 * Test constructor 112 */ 113 public void testConstructor_Type_int() throws Throwable { 114 Partial test = new Partial(DateTimeFieldType.dayOfYear(), 4); 115 assertEquals(ISO_UTC, test.getChronology()); 116 assertEquals(1, test.size()); 117 assertEquals(4, test.getValue(0)); 118 assertEquals(4, test.get(DateTimeFieldType.dayOfYear())); 119 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear())); 120 } 121 122 /** 123 * Test constructor 124 */ 125 public void testConstructorEx1_Type_int() throws Throwable { 126 try { 127 new Partial(null, 4); 128 fail(); 129 } catch (IllegalArgumentException ex) { 130 assertMessageContains(ex, "must not be null"); 131 } 132 } 133 134 /** 135 * Test constructor 136 */ 137 public void testConstructorEx2_Type_int() throws Throwable { 138 try { 139 new Partial(DateTimeFieldType.dayOfYear(), 0); 140 fail(); 141 } catch (IllegalArgumentException ex) { 142 // expected 143 } 144 } 145 146 //----------------------------------------------------------------------- 147 /** 148 * Test constructor 149 */ 150 public void testConstructor_Type_int_Chrono() throws Throwable { 151 Partial test = new Partial(DateTimeFieldType.dayOfYear(), 4, GREGORIAN_PARIS); 152 assertEquals(GREGORIAN_UTC, test.getChronology()); 153 assertEquals(1, test.size()); 154 assertEquals(4, test.getValue(0)); 155 assertEquals(4, test.get(DateTimeFieldType.dayOfYear())); 156 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear())); 157 } 158 159 /** 160 * Test constructor 161 */ 162 public void testConstructorEx_Type_int_Chrono() throws Throwable { 163 try { 164 new Partial(null, 4, ISO_UTC); 165 fail(); 166 } catch (IllegalArgumentException ex) { 167 assertMessageContains(ex, "must not be null"); 168 } 169 } 170 171 /** 172 * Test constructor 173 */ 174 public void testConstructorEx2_Type_int_Chrono() throws Throwable { 175 try { 176 new Partial(DateTimeFieldType.dayOfYear(), 0, ISO_UTC); 177 fail(); 178 } catch (IllegalArgumentException ex) { 179 // expected 180 } 181 } 182 183 //----------------------------------------------------------------------- 184 /** 185 * Test constructor 186 */ 187 public void testConstructor_TypeArray_intArray() throws Throwable { 188 DateTimeFieldType[] types = new DateTimeFieldType[] { 189 DateTimeFieldType.year(), 190 DateTimeFieldType.dayOfYear() 191 }; 192 int[] values = new int[] {2005, 33}; 193 Partial test = new Partial(types, values); 194 assertEquals(ISO_UTC, test.getChronology()); 195 assertEquals(2, test.size()); 196 assertEquals(2005, test.getValue(0)); 197 assertEquals(2005, test.get(DateTimeFieldType.year())); 198 assertEquals(true, test.isSupported(DateTimeFieldType.year())); 199 assertEquals(33, test.getValue(1)); 200 assertEquals(33, test.get(DateTimeFieldType.dayOfYear())); 201 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear())); 202 assertEquals(true, Arrays.equals(test.getFieldTypes(), types)); 203 assertEquals(true, Arrays.equals(test.getValues(), values)); 204 } 205 206 /** 207 * Test constructor 208 */ 209 public void testConstructor2_TypeArray_intArray() throws Throwable { 210 DateTimeFieldType[] types = new DateTimeFieldType[0]; 211 int[] values = new int[0]; 212 Partial test = new Partial(types, values); 213 assertEquals(ISO_UTC, test.getChronology()); 214 assertEquals(0, test.size()); 215 } 216 217 /** 218 * Test constructor 219 */ 220 public void testConstructorEx1_TypeArray_intArray() throws Throwable { 221 try { 222 new Partial((DateTimeFieldType[]) null, new int[] {1}); 223 fail(); 224 } catch (IllegalArgumentException ex) { 225 assertMessageContains(ex, "must not be null"); 226 } 227 } 228 229 /** 230 * Test constructor 231 */ 232 public void testConstructorEx3_TypeArray_intArray() throws Throwable { 233 try { 234 new Partial(new DateTimeFieldType[] {DateTimeFieldType.dayOfYear()}, null); 235 fail(); 236 } catch (IllegalArgumentException ex) { 237 assertMessageContains(ex, "must not be null"); 238 } 239 } 240 241 /** 242 * Test constructor 243 */ 244 public void testConstructorEx5_TypeArray_intArray() throws Throwable { 245 try { 246 new Partial(new DateTimeFieldType[] {DateTimeFieldType.dayOfYear()}, new int[2]); 247 fail(); 248 } catch (IllegalArgumentException ex) { 249 assertMessageContains(ex, "same length"); 250 } 251 } 252 253 /** 254 * Test constructor 255 */ 256 public void testConstructorEx6_TypeArray_intArray() throws Throwable { 257 try { 258 new Partial(new DateTimeFieldType[] {null, DateTimeFieldType.dayOfYear()}, new int[2]); 259 fail(); 260 } catch (IllegalArgumentException ex) { 261 assertMessageContains(ex, "contain null"); 262 } 263 try { 264 new Partial(new DateTimeFieldType[] {DateTimeFieldType.dayOfYear(), null}, new int[2]); 265 fail(); 266 } catch (IllegalArgumentException ex) { 267 assertMessageContains(ex, "contain null"); 268 } 269 } 270 271 /** 272 * Test constructor 273 */ 274 public void testConstructorEx7_TypeArray_intArray() throws Throwable { 275 int[] values = new int[] {1, 1, 1}; 276 DateTimeFieldType[] types = new DateTimeFieldType[] { 277 DateTimeFieldType.dayOfMonth(), DateTimeFieldType.year(), DateTimeFieldType.monthOfYear() }; 278 try { 279 new Partial(types, values); 280 fail(); 281 } catch (IllegalArgumentException ex) { 282 assertMessageContains(ex, "must be in order", "largest-smallest"); 283 } 284 285 types = new DateTimeFieldType[] { 286 DateTimeFieldType.year(), DateTimeFieldType.dayOfMonth(), DateTimeFieldType.monthOfYear() }; 287 try { 288 new Partial(types, values); 289 fail(); 290 } catch (IllegalArgumentException ex) { 291 assertMessageContains(ex, "must be in order", "largest-smallest"); 292 } 293 294 types = new DateTimeFieldType[] { 295 DateTimeFieldType.year(), DateTimeFieldType.era(), DateTimeFieldType.monthOfYear() }; 296 try { 297 new Partial(types, values); 298 fail(); 299 } catch (IllegalArgumentException ex) { 300 assertMessageContains(ex, "must be in order", "largest-smallest"); 301 } 302 303 types = new DateTimeFieldType[] { 304 DateTimeFieldType.year(), DateTimeFieldType.dayOfMonth(), DateTimeFieldType.era() }; 305 try { 306 new Partial(types, values); 307 fail(); 308 } catch (IllegalArgumentException ex) { 309 assertMessageContains(ex, "must be in order", "largest-smallest"); 310 } 311 312 types = new DateTimeFieldType[] { 313 DateTimeFieldType.year(), DateTimeFieldType.dayOfMonth(), DateTimeFieldType.dayOfYear() }; 314 try { 315 new Partial(types, values); 316 fail(); 317 } catch (IllegalArgumentException ex) { 318 assertMessageContains(ex, "must be in order", "largest-smallest"); 319 } 320 321 types = new DateTimeFieldType[] { 322 DateTimeFieldType.yearOfEra(), DateTimeFieldType.year(), DateTimeFieldType.dayOfYear() }; 323 try { 324 new Partial(types, values); 325 fail(); 326 } catch (IllegalArgumentException ex) { 327 assertMessageContains(ex, "must be in order", "largest-smallest"); 328 } 329 } 330 331 /** 332 * Test constructor 333 */ 334 public void testConstructorEx8_TypeArray_intArray() throws Throwable { 335 int[] values = new int[] {1, 1, 1}; 336 DateTimeFieldType[] types = new DateTimeFieldType[] { 337 DateTimeFieldType.era(), DateTimeFieldType.year(), DateTimeFieldType.year() }; 338 try { 339 new Partial(types, values); 340 fail(); 341 } catch (IllegalArgumentException ex) { 342 assertMessageContains(ex, "must not", "duplicate"); 343 } 344 345 types = new DateTimeFieldType[] { 346 DateTimeFieldType.era(), DateTimeFieldType.era(), DateTimeFieldType.monthOfYear() }; 347 try { 348 new Partial(types, values); 349 fail(); 350 } catch (IllegalArgumentException ex) { 351 assertMessageContains(ex, "must not", "duplicate"); 352 } 353 354 types = new DateTimeFieldType[] { 355 DateTimeFieldType.dayOfYear(), DateTimeFieldType.dayOfMonth(), DateTimeFieldType.dayOfMonth() }; 356 try { 357 new Partial(types, values); 358 fail(); 359 } catch (IllegalArgumentException ex) { 360 assertMessageContains(ex, "must not", "duplicate"); 361 } 362 } 363 364 /** 365 * Test constructor 366 */ 367 public void testConstructorEx9_TypeArray_intArray() throws Throwable { 368 int[] values = new int[] {3, 0}; 369 DateTimeFieldType[] types = new DateTimeFieldType[] { 370 DateTimeFieldType.dayOfMonth(), DateTimeFieldType.dayOfWeek()}; 371 try { 372 new Partial(types, values); 373 fail(); 374 } catch (IllegalArgumentException ex) { 375 // expected 376 } 377 } 378 379 //----------------------------------------------------------------------- 380 /** 381 * Test constructor 382 */ 383 public void testConstructor_TypeArray_intArray_Chrono() throws Throwable { 384 DateTimeFieldType[] types = new DateTimeFieldType[] { 385 DateTimeFieldType.year(), 386 DateTimeFieldType.dayOfYear() 387 }; 388 int[] values = new int[] {2005, 33}; 389 Partial test = new Partial(types, values, GREGORIAN_PARIS); 390 assertEquals(GREGORIAN_UTC, test.getChronology()); 391 assertEquals(2, test.size()); 392 assertEquals(2005, test.getValue(0)); 393 assertEquals(2005, test.get(DateTimeFieldType.year())); 394 assertEquals(true, test.isSupported(DateTimeFieldType.year())); 395 assertEquals(33, test.getValue(1)); 396 assertEquals(33, test.get(DateTimeFieldType.dayOfYear())); 397 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear())); 398 assertEquals(true, Arrays.equals(test.getFieldTypes(), types)); 399 assertEquals(true, Arrays.equals(test.getValues(), values)); 400 } 401 402 //----------------------------------------------------------------------- 403 /** 404 * Test constructor 405 */ 406 public void testConstructor_Partial() throws Throwable { 407 YearMonthDay ymd = new YearMonthDay(2005, 6, 25, GREGORIAN_PARIS); 408 Partial test = new Partial(ymd); 409 assertEquals(GREGORIAN_UTC, test.getChronology()); 410 assertEquals(3, test.size()); 411 assertEquals(2005, test.getValue(0)); 412 assertEquals(2005, test.get(DateTimeFieldType.year())); 413 assertEquals(true, test.isSupported(DateTimeFieldType.year())); 414 assertEquals(6, test.getValue(1)); 415 assertEquals(6, test.get(DateTimeFieldType.monthOfYear())); 416 assertEquals(true, test.isSupported(DateTimeFieldType.monthOfYear())); 417 assertEquals(25, test.getValue(2)); 418 assertEquals(25, test.get(DateTimeFieldType.dayOfMonth())); 419 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfMonth())); 420 } 421 422 /** 423 * Test constructor 424 */ 425 public void testConstructorEx_Partial() throws Throwable { 426 try { 427 new Partial((ReadablePartial) null); 428 fail(); 429 } catch (IllegalArgumentException ex) { 430 assertMessageContains(ex, "must not be null"); 431 } 432 } 433 434 //----------------------------------------------------------------------- 435 /** 436 * Checks if the exception message is valid. 437 * 438 * @param ex the exception to check 439 * @param str the string to check 440 */ 441 private void assertMessageContains(Exception ex, String str) { 442 assertEquals(ex.getMessage() + ": " + str, true, ex.getMessage().indexOf(str) >= 0); 443 } 444 445 /** 446 * Checks if the exception message is valid. 447 * 448 * @param ex the exception to check 449 * @param str1 the string to check 450 * @param str2 the string to check 451 */ 452 private void assertMessageContains(Exception ex, String str1, String str2) { 453 assertEquals(ex.getMessage() + ": " + str1 + "/" + str2, true, 454 ex.getMessage().indexOf(str1) >= 0 && 455 ex.getMessage().indexOf(str2) >= 0 && 456 ex.getMessage().indexOf(str1) < ex.getMessage().indexOf(str2)); 457 } 458 459 }