1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package tests.api.java.util; 19 20 import dalvik.annotation.AndroidOnly; 21 import dalvik.annotation.TestTargetNew; 22 import dalvik.annotation.TestLevel; 23 import dalvik.annotation.TestTargetClass; 24 import dalvik.annotation.KnownFailure; 25 import tests.support.Support_Locale; 26 27 import java.util.BitSet; 28 import java.util.Calendar; 29 import java.util.Date; 30 import java.util.GregorianCalendar; 31 import java.util.Locale; 32 import java.util.SimpleTimeZone; 33 import java.util.TimeZone; 34 import java.util.Vector; 35 36 37 @TestTargetClass(GregorianCalendar.class) 38 public class GregorianCalendarTest extends junit.framework.TestCase { 39 40 /** 41 * @tests java.util.GregorianCalendar#GregorianCalendar() 42 */ 43 @TestTargetNew( 44 level = TestLevel.COMPLETE, 45 notes = "", 46 method = "GregorianCalendar", 47 args = {} 48 ) 49 public void test_Constructor() { 50 // Test for method java.util.GregorianCalendar() 51 assertTrue("Constructed incorrect calendar", (new GregorianCalendar() 52 .isLenient())); 53 } 54 55 /** 56 * @tests java.util.GregorianCalendar#GregorianCalendar(int, int, int) 57 */ 58 @TestTargetNew( 59 level = TestLevel.COMPLETE, 60 notes = "", 61 method = "GregorianCalendar", 62 args = {int.class, int.class, int.class} 63 ) 64 public void test_ConstructorIII() { 65 // Test for method java.util.GregorianCalendar(int, int, int) 66 GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 13); 67 assertEquals("Incorrect calendar constructed 1", 68 1972, gc.get(Calendar.YEAR)); 69 assertTrue("Incorrect calendar constructed 2", 70 gc.get(Calendar.MONTH) == Calendar.OCTOBER); 71 assertEquals("Incorrect calendar constructed 3", 13, gc 72 .get(Calendar.DAY_OF_MONTH)); 73 assertTrue("Incorrect calendar constructed 4", gc.getTimeZone().equals( 74 TimeZone.getDefault())); 75 } 76 77 /** 78 * @tests java.util.GregorianCalendar#GregorianCalendar(int, int, int, int, 79 * int) 80 */ 81 @TestTargetNew( 82 level = TestLevel.COMPLETE, 83 notes = "", 84 method = "GregorianCalendar", 85 args = {int.class, int.class, int.class, int.class, int.class} 86 ) 87 public void test_ConstructorIIIII() { 88 // Test for method java.util.GregorianCalendar(int, int, int, int, int) 89 GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 90 13, 19, 9); 91 assertEquals("Incorrect calendar constructed", 92 1972, gc.get(Calendar.YEAR)); 93 assertTrue("Incorrect calendar constructed", 94 gc.get(Calendar.MONTH) == Calendar.OCTOBER); 95 assertEquals("Incorrect calendar constructed", 13, gc 96 .get(Calendar.DAY_OF_MONTH)); 97 assertEquals("Incorrect calendar constructed", 7, gc.get(Calendar.HOUR)); 98 assertEquals("Incorrect calendar constructed", 99 1, gc.get(Calendar.AM_PM)); 100 assertEquals("Incorrect calendar constructed", 101 9, gc.get(Calendar.MINUTE)); 102 assertTrue("Incorrect calendar constructed", gc.getTimeZone().equals( 103 TimeZone.getDefault())); 104 105 //Regression for HARMONY-998 106 gc = new GregorianCalendar(1900, 0, 0, 0, Integer.MAX_VALUE); 107 assertEquals("Incorrect calendar constructed", 108 5983, gc.get(Calendar.YEAR)); 109 } 110 111 /** 112 * @tests java.util.GregorianCalendar#GregorianCalendar(int, int, int, int, 113 * int, int) 114 */ 115 @TestTargetNew( 116 level = TestLevel.COMPLETE, 117 notes = "", 118 method = "GregorianCalendar", 119 args = {int.class, int.class, int.class, int.class, int.class, int.class} 120 ) 121 public void test_ConstructorIIIIII() { 122 // Test for method java.util.GregorianCalendar(int, int, int, int, int, 123 // int) 124 GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 125 13, 19, 9, 59); 126 assertEquals("Incorrect calendar constructed", 127 1972, gc.get(Calendar.YEAR)); 128 assertTrue("Incorrect calendar constructed", 129 gc.get(Calendar.MONTH) == Calendar.OCTOBER); 130 assertEquals("Incorrect calendar constructed", 13, gc 131 .get(Calendar.DAY_OF_MONTH)); 132 assertEquals("Incorrect calendar constructed", 7, gc.get(Calendar.HOUR)); 133 assertEquals("Incorrect calendar constructed", 134 1, gc.get(Calendar.AM_PM)); 135 assertEquals("Incorrect calendar constructed", 136 9, gc.get(Calendar.MINUTE)); 137 assertEquals("Incorrect calendar constructed", 138 59, gc.get(Calendar.SECOND)); 139 assertTrue("Incorrect calendar constructed", gc.getTimeZone().equals( 140 TimeZone.getDefault())); 141 } 142 143 /** 144 * @tests java.util.GregorianCalendar#GregorianCalendar(java.util.Locale) 145 */ 146 @TestTargetNew( 147 level = TestLevel.COMPLETE, 148 notes = "", 149 method = "GregorianCalendar", 150 args = {java.util.Locale.class} 151 ) 152 public void test_ConstructorLjava_util_Locale() { 153 Locale[] requiredLocales = {Locale.US, Locale.FRANCE}; 154 if (!Support_Locale.areLocalesAvailable(requiredLocales)) { 155 // locale dependent test, bug 1943269 156 return; 157 } 158 // Test for method java.util.GregorianCalendar(java.util.Locale) 159 Date date = new Date(); 160 GregorianCalendar gcUS = new GregorianCalendar(Locale.US); 161 gcUS.setTime(date); 162 GregorianCalendar gcUS2 = new GregorianCalendar(Locale.US); 163 gcUS2.setTime(date); 164 GregorianCalendar gcFrance = new GregorianCalendar(Locale.FRANCE); 165 gcFrance.setTime(date); 166 assertTrue("Locales not created correctly", gcUS.equals(gcUS2)); 167 assertFalse("Locales not created correctly", gcUS.equals(gcFrance)); 168 } 169 170 /** 171 * @tests java.util.GregorianCalendar#GregorianCalendar(java.util.TimeZone) 172 */ 173 @TestTargetNew( 174 level = TestLevel.COMPLETE, 175 notes = "", 176 method = "GregorianCalendar", 177 args = {java.util.TimeZone.class} 178 ) 179 public void test_ConstructorLjava_util_TimeZone() { 180 // Test for method java.util.GregorianCalendar(java.util.TimeZone) 181 Date date = new Date(); 182 TimeZone.getDefault(); 183 GregorianCalendar gc1 = new GregorianCalendar(TimeZone 184 .getTimeZone("EST")); 185 gc1.setTime(date); 186 GregorianCalendar gc2 = new GregorianCalendar(TimeZone 187 .getTimeZone("CST")); 188 gc2.setTime(date); 189 190 assertFalse(gc1.equals(gc2)); 191 192 gc1 = new GregorianCalendar(TimeZone 193 .getTimeZone("GMT+2")); 194 gc1.setTime(date); 195 gc2 = new GregorianCalendar(TimeZone 196 .getTimeZone("GMT+1")); 197 gc2.setTime(date); 198 assertTrue("Incorrect calendar returned", 199 gc1.get(Calendar.HOUR) == ((gc2.get(Calendar.HOUR) + 1) % 12)); 200 201 // Regression test for HARMONY-2961 202 SimpleTimeZone timezone = new SimpleTimeZone(-3600 * 24 * 1000 * 2, 203 "GMT"); 204 GregorianCalendar gc = new GregorianCalendar(timezone); 205 } 206 207 /** 208 * @tests java.util.GregorianCalendar#GregorianCalendar(java.util.TimeZone, 209 * java.util.Locale) 210 */ 211 @TestTargetNew( 212 level = TestLevel.COMPLETE, 213 notes = "", 214 method = "GregorianCalendar", 215 args = {java.util.TimeZone.class, java.util.Locale.class} 216 ) 217 public void test_ConstructorLjava_util_TimeZoneLjava_util_Locale() { 218 // Test for method java.util.GregorianCalendar(java.util.TimeZone, 219 // java.util.Locale) 220 Date date = new Date(); 221 TimeZone.getDefault(); 222 GregorianCalendar gc1 = new GregorianCalendar(TimeZone 223 .getTimeZone("EST"), Locale.US); 224 gc1.setTime(date); 225 GregorianCalendar gc2 = new GregorianCalendar(TimeZone 226 .getTimeZone("EST"), Locale.US); 227 gc2.setTime(date); 228 GregorianCalendar gc3 = new GregorianCalendar(TimeZone 229 .getTimeZone("CST"), Locale.FRANCE); 230 gc3.setTime(date); 231 assertTrue(gc1.equals(gc2)); 232 assertFalse(gc2.equals(gc3)); 233 assertFalse(gc3.equals(gc1)); 234 235 gc1 = new GregorianCalendar(TimeZone 236 .getTimeZone("GMT+2"), Locale.US); 237 gc1.setTime(date); 238 gc3 = new GregorianCalendar(TimeZone 239 .getTimeZone("GMT+1"), Locale.FRANCE); 240 gc3.setTime(date); 241 // CST is 1 hour before EST, add 1 to the CST time and convert to 0-12 242 // value 243 assertTrue("Incorrect calendar returned", 244 gc1.get(Calendar.HOUR) == ((gc3.get(Calendar.HOUR) + 1) % 12)); 245 } 246 247 /** 248 * @tests java.util.GregorianCalendar#add(int, int) 249 */ 250 @TestTargetNew( 251 level = TestLevel.COMPLETE, 252 notes = "", 253 method = "add", 254 args = {int.class, int.class} 255 ) 256 @AndroidOnly("This test fails on the RI with version 1.5 but succeeds" 257 + "on the RI with version 1.6") 258 public void test_addII() { 259 // Test for method void java.util.GregorianCalendar.add(int, int) 260 GregorianCalendar gc1 = new GregorianCalendar(1998, 11, 6); 261 gc1.add(GregorianCalendar.YEAR, 1); 262 assertEquals("Add failed to Increment", 263 1999, gc1.get(GregorianCalendar.YEAR)); 264 265 gc1 = new GregorianCalendar(1999, Calendar.JULY, 31); 266 gc1.add(Calendar.MONTH, 7); 267 assertEquals("Wrong result year 1", 2000, gc1.get(Calendar.YEAR)); 268 assertTrue("Wrong result month 1", 269 gc1.get(Calendar.MONTH) == Calendar.FEBRUARY); 270 assertEquals("Wrong result date 1", 29, gc1.get(Calendar.DATE)); 271 272 gc1.add(Calendar.YEAR, -1); 273 assertEquals("Wrong result year 2", 1999, gc1.get(Calendar.YEAR)); 274 assertTrue("Wrong result month 2", 275 gc1.get(Calendar.MONTH) == Calendar.FEBRUARY); 276 assertEquals("Wrong result date 2", 28, gc1.get(Calendar.DATE)); 277 278 gc1 = new GregorianCalendar(TimeZone.getTimeZone("EST")); 279 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 280 gc1.add(Calendar.MILLISECOND, 24 * 60 * 60 * 1000); 281 282 assertEquals("Wrong time after MILLISECOND change", 16, gc1 283 .get(Calendar.HOUR_OF_DAY)); 284 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 285 gc1.add(Calendar.SECOND, 24 * 60 * 60); 286 assertEquals("Wrong time after SECOND change", 16, gc1 287 .get(Calendar.HOUR_OF_DAY)); 288 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 289 gc1.add(Calendar.MINUTE, 24 * 60); 290 assertEquals("Wrong time after MINUTE change", 16, gc1 291 .get(Calendar.HOUR_OF_DAY)); 292 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 293 gc1.add(Calendar.HOUR, 24); 294 assertEquals("Wrong time after HOUR change", 16, gc1 295 .get(Calendar.HOUR_OF_DAY)); 296 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 297 gc1.add(Calendar.HOUR_OF_DAY, 24); 298 assertEquals("Wrong time after HOUR_OF_DAY change", 16, gc1 299 .get(Calendar.HOUR_OF_DAY)); 300 301 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 302 gc1.add(Calendar.AM_PM, 2); 303 assertEquals("Wrong time after AM_PM change", 16, gc1 304 .get(Calendar.HOUR_OF_DAY)); 305 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 306 gc1.add(Calendar.DATE, 1); 307 assertEquals("Wrong time after DATE change", 16, gc1 308 .get(Calendar.HOUR_OF_DAY)); 309 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 310 gc1.add(Calendar.DAY_OF_YEAR, 1); 311 assertEquals("Wrong time after DAY_OF_YEAR change", 16, gc1 312 .get(Calendar.HOUR_OF_DAY)); 313 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 314 gc1.add(Calendar.DAY_OF_WEEK, 1); 315 assertEquals("Wrong time after DAY_OF_WEEK change", 16, gc1 316 .get(Calendar.HOUR_OF_DAY)); 317 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 318 gc1.add(Calendar.WEEK_OF_YEAR, 1); 319 assertEquals("Wrong time after WEEK_OF_YEAR change", 16, gc1 320 .get(Calendar.HOUR_OF_DAY)); 321 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 322 gc1.add(Calendar.WEEK_OF_MONTH, 1); 323 assertEquals("Wrong time after WEEK_OF_MONTH change", 16, gc1 324 .get(Calendar.HOUR_OF_DAY)); 325 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 326 gc1.add(Calendar.DAY_OF_WEEK_IN_MONTH, 1); 327 assertEquals("Wrong time after DAY_OF_WEEK_IN_MONTH change", 16, gc1 328 .get(Calendar.HOUR_OF_DAY)); 329 330 gc1.clear(); 331 gc1.set(2000, Calendar.APRIL, 1, 23, 0); 332 gc1.add(Calendar.DATE, 1); 333 assertTrue("Wrong time after DATE change near DST boundary", gc1 334 .get(Calendar.MONTH) == Calendar.APRIL 335 && gc1.get(Calendar.DATE) == 2 336 && gc1.get(Calendar.HOUR_OF_DAY) == 23); 337 } 338 339 /** 340 * @tests java.util.GregorianCalendar#equals(java.lang.Object) 341 */ 342 @TestTargetNew( 343 level = TestLevel.COMPLETE, 344 notes = "", 345 method = "equals", 346 args = {java.lang.Object.class} 347 ) 348 public void test_equalsLjava_lang_Object() { 349 // Test for method boolean 350 // java.util.GregorianCalendar.equals(java.lang.Object) 351 GregorianCalendar gc1 = new GregorianCalendar(1998, 11, 6); 352 GregorianCalendar gc2 = new GregorianCalendar(2000, 11, 6); 353 GregorianCalendar gc3 = new GregorianCalendar(1998, 11, 6); 354 assertTrue("Equality check failed", gc1.equals(gc3)); 355 assertTrue("Equality check failed", !gc1.equals(gc2)); 356 gc3.setGregorianChange(new Date()); 357 assertTrue("Different gregorian change", !gc1.equals(gc3)); 358 } 359 360 /** 361 * @tests java.util.GregorianCalendar#getActualMaximum(int) 362 */ 363 @TestTargetNew( 364 level = TestLevel.COMPLETE, 365 notes = "", 366 method = "getActualMaximum", 367 args = {int.class} 368 ) 369 public void test_getActualMaximumI() { 370 // Test for method int java.util.GregorianCalendar.getActualMaximum(int) 371 GregorianCalendar gc1 = new GregorianCalendar(1900, 1, 1); 372 GregorianCalendar gc2 = new GregorianCalendar(1996, 1, 1); 373 GregorianCalendar gc3 = new GregorianCalendar(1997, 1, 1); 374 GregorianCalendar gc4 = new GregorianCalendar(2000, 1, 1); 375 GregorianCalendar gc5 = new GregorianCalendar(2000, 9, 9); 376 GregorianCalendar gc6 = new GregorianCalendar(2000, 3, 3); 377 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 1900", 378 28, gc1.getActualMaximum(Calendar.DAY_OF_MONTH)); 379 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 1996", 380 29, gc2.getActualMaximum(Calendar.DAY_OF_MONTH)); 381 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 1998", 382 28, gc3.getActualMaximum(Calendar.DAY_OF_MONTH)); 383 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 2000", 384 29, gc4.getActualMaximum(Calendar.DAY_OF_MONTH)); 385 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Oct 2000", 386 31, gc5.getActualMaximum(Calendar.DAY_OF_MONTH)); 387 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Apr 2000", 388 30, gc6.getActualMaximum(Calendar.DAY_OF_MONTH)); 389 assertTrue("Wrong actual maximum value for MONTH", gc1 390 .getActualMaximum(Calendar.MONTH) == Calendar.DECEMBER); 391 assertEquals("Wrong actual maximum value for HOUR_OF_DAY", 23, gc1 392 .getActualMaximum(Calendar.HOUR_OF_DAY)); 393 assertEquals("Wrong actual maximum value for HOUR", 11, gc1 394 .getActualMaximum(Calendar.HOUR)); 395 assertEquals("Wrong actual maximum value for DAY_OF_WEEK_IN_MONTH", 4, gc6 396 .getActualMaximum(Calendar.DAY_OF_WEEK_IN_MONTH)); 397 398 399 // Regression test for harmony 2954 400 Date date = new Date(Date.parse("Jan 15 00:00:01 GMT 2000")); 401 GregorianCalendar gc = new GregorianCalendar(); 402 gc.setTimeInMillis(Date.parse("Dec 15 00:00:01 GMT 1582")); 403 assertEquals(355, gc.getActualMaximum(Calendar.DAY_OF_YEAR)); 404 gc.setGregorianChange(date); 405 gc.setTimeInMillis(Date.parse("Jan 16 00:00:01 GMT 2000")); 406 assertEquals(353, gc.getActualMaximum(Calendar.DAY_OF_YEAR)); 407 408 //Regression test for HARMONY-3004 409 gc = new GregorianCalendar(1900, 7, 1); 410 String[] ids = TimeZone.getAvailableIDs(); 411 for (int i = 0; i < ids.length; i++) { 412 TimeZone tz = TimeZone.getTimeZone(ids[i]); 413 gc.setTimeZone(tz); 414 for (int j = 1900; j < 2000; j++) { 415 gc.set(Calendar.YEAR, j); 416 assertEquals(7200000, gc.getActualMaximum(Calendar.DST_OFFSET)); 417 } 418 } 419 } 420 421 /** 422 * @tests java.util.GregorianCalendar#getActualMinimum(int) 423 */ 424 @TestTargetNew( 425 level = TestLevel.COMPLETE, 426 notes = "", 427 method = "getActualMinimum", 428 args = {int.class} 429 ) 430 public void test_getActualMinimumI() { 431 // Test for method int java.util.GregorianCalendar.getActualMinimum(int) 432 GregorianCalendar gc1 = new GregorianCalendar(1900, 1, 1); 433 new GregorianCalendar(1996, 1, 1); 434 new GregorianCalendar(1997, 1, 1); 435 new GregorianCalendar(2000, 1, 1); 436 new GregorianCalendar(2000, 9, 9); 437 GregorianCalendar gc6 = new GregorianCalendar(2000, 3, 3); 438 assertEquals("Wrong actual minimum value for DAY_OF_MONTH for Feb 1900", 439 1, gc1.getActualMinimum(Calendar.DAY_OF_MONTH)); 440 assertTrue("Wrong actual minimum value for MONTH", gc1 441 .getActualMinimum(Calendar.MONTH) == Calendar.JANUARY); 442 assertEquals("Wrong actual minimum value for HOUR_OF_DAY", 0, gc1 443 .getActualMinimum(Calendar.HOUR_OF_DAY)); 444 assertEquals("Wrong actual minimum value for HOUR", 0, gc1 445 .getActualMinimum(Calendar.HOUR)); 446 assertEquals("Wrong actual minimum value for DAY_OF_WEEK_IN_MONTH", 1, gc6 447 .getActualMinimum(Calendar.DAY_OF_WEEK_IN_MONTH)); 448 } 449 450 /** 451 * @tests java.util.GregorianCalendar#getGreatestMinimum(int) 452 */ 453 @TestTargetNew( 454 level = TestLevel.COMPLETE, 455 notes = "", 456 method = "getGreatestMinimum", 457 args = {int.class} 458 ) 459 public void test_getGreatestMinimumI() { 460 // Test for method int 461 // java.util.GregorianCalendar.getGreatestMinimum(int) 462 GregorianCalendar gc = new GregorianCalendar(); 463 assertEquals("Wrong greatest minimum value for DAY_OF_MONTH", 1, gc 464 .getGreatestMinimum(Calendar.DAY_OF_MONTH)); 465 assertTrue("Wrong greatest minimum value for MONTH", gc 466 .getGreatestMinimum(Calendar.MONTH) == Calendar.JANUARY); 467 assertEquals("Wrong greatest minimum value for HOUR_OF_DAY", 0, gc 468 .getGreatestMinimum(Calendar.HOUR_OF_DAY)); 469 assertEquals("Wrong greatest minimum value for HOUR", 0, gc 470 .getGreatestMinimum(Calendar.HOUR)); 471 472 BitSet result = new BitSet(); 473 int[] min = { 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, -46800000, 474 0 }; 475 for (int i = 0; i < min.length; i++) { 476 if (gc.getGreatestMinimum(i) != min[i]) 477 result.set(i); 478 } 479 assertTrue("Wrong greatest min for " + result, result.length() == 0); 480 } 481 482 /** 483 * @tests java.util.GregorianCalendar#getGregorianChange() 484 */ 485 @TestTargetNew( 486 level = TestLevel.COMPLETE, 487 notes = "", 488 method = "getGregorianChange", 489 args = {} 490 ) 491 public void test_getGregorianChange() { 492 // Test for method java.util.Date 493 // java.util.GregorianCalendar.getGregorianChange() 494 GregorianCalendar gc = new GregorianCalendar(); 495 GregorianCalendar returnedChange = new GregorianCalendar(TimeZone 496 .getTimeZone("EST")); 497 returnedChange.setTime(gc.getGregorianChange()); 498 assertEquals("Returned incorrect year", 499 1582, returnedChange.get(Calendar.YEAR)); 500 assertTrue("Returned incorrect month", returnedChange 501 .get(Calendar.MONTH) == Calendar.OCTOBER); 502 assertEquals("Returned incorrect day of month", 4, returnedChange 503 .get(Calendar.DAY_OF_MONTH)); 504 } 505 506 /** 507 * @tests java.util.GregorianCalendar#getLeastMaximum(int) 508 */ 509 @TestTargetNew( 510 level = TestLevel.COMPLETE, 511 notes = "", 512 method = "getLeastMaximum", 513 args = {int.class} 514 ) 515 public void test_getLeastMaximumI() { 516 // Test for method int java.util.GregorianCalendar.getLeastMaximum(int) 517 GregorianCalendar gc = new GregorianCalendar(); 518 assertEquals("Wrong least maximum value for DAY_OF_MONTH", 28, gc 519 .getLeastMaximum(Calendar.DAY_OF_MONTH)); 520 assertTrue("Wrong least maximum value for MONTH", gc 521 .getLeastMaximum(Calendar.MONTH) == Calendar.DECEMBER); 522 assertEquals("Wrong least maximum value for HOUR_OF_DAY", 23, gc 523 .getLeastMaximum(Calendar.HOUR_OF_DAY)); 524 assertEquals("Wrong least maximum value for HOUR", 11, gc 525 .getLeastMaximum(Calendar.HOUR)); 526 527 BitSet result = new BitSet(); 528 Vector values = new Vector(); 529 int[] max = { 1, 292269054, 11, 50, 3, 28, 355, 7, 3, 1, 11, 23, 59, 530 59, 999, 50400000, 1200000 }; 531 for (int i = 0; i < max.length; i++) { 532 if (gc.getLeastMaximum(i) != max[i]) { 533 result.set(i); 534 values.add(new Integer(gc.getLeastMaximum(i))); 535 } 536 } 537 assertTrue("Wrong least max for " + result + " = " + values, result 538 .length() == 0); 539 540 // Regression test for harmony-2947 541 Date date = new Date(Date.parse("Jan 1 00:00:01 GMT 2000")); 542 gc = new GregorianCalendar(); 543 gc.setGregorianChange(date); 544 gc.setTime(date); 545 assertEquals(gc.getActualMaximum(Calendar.WEEK_OF_YEAR), gc 546 .getLeastMaximum(Calendar.WEEK_OF_YEAR)); 547 } 548 549 /** 550 * @tests java.util.GregorianCalendar#getMaximum(int) 551 */ 552 @TestTargetNew( 553 level = TestLevel.COMPLETE, 554 notes = "", 555 method = "getMaximum", 556 args = {int.class} 557 ) 558 public void test_getMaximumI() { 559 // Test for method int java.util.GregorianCalendar.getMaximum(int) 560 GregorianCalendar gc = new GregorianCalendar(); 561 assertEquals("Wrong maximum value for DAY_OF_MONTH", 31, gc 562 .getMaximum(Calendar.DAY_OF_MONTH)); 563 assertTrue("Wrong maximum value for MONTH", gc 564 .getMaximum(Calendar.MONTH) == Calendar.DECEMBER); 565 assertEquals("Wrong maximum value for HOUR_OF_DAY", 23, gc 566 .getMaximum(Calendar.HOUR_OF_DAY)); 567 assertEquals("Wrong maximum value for HOUR", 568 11, gc.getMaximum(Calendar.HOUR)); 569 570 BitSet result = new BitSet(); 571 Vector values = new Vector(); 572 int[] max = { 1, 292278994, 11, 53, 6, 31, 366, 7, 6, 1, 11, 23, 59, 573 59, 999, 50400000, 7200000 }; 574 for (int i = 0; i < max.length; i++) { 575 if (gc.getMaximum(i) != max[i]) { 576 result.set(i); 577 values.add(new Integer(gc.getMaximum(i))); 578 } 579 } 580 assertTrue("Wrong max for " + result + " = " + values, 581 result.length() == 0); 582 } 583 584 /** 585 * @tests java.util.GregorianCalendar#getMinimum(int) 586 */ 587 @TestTargetNew( 588 level = TestLevel.COMPLETE, 589 notes = "", 590 method = "getMinimum", 591 args = {int.class} 592 ) 593 public void test_getMinimumI() { 594 // Test for method int java.util.GregorianCalendar.getMinimum(int) 595 GregorianCalendar gc = new GregorianCalendar(); 596 assertEquals("Wrong minimum value for DAY_OF_MONTH", 1, gc 597 .getMinimum(Calendar.DAY_OF_MONTH)); 598 assertTrue("Wrong minimum value for MONTH", gc 599 .getMinimum(Calendar.MONTH) == Calendar.JANUARY); 600 assertEquals("Wrong minimum value for HOUR_OF_DAY", 0, gc 601 .getMinimum(Calendar.HOUR_OF_DAY)); 602 assertEquals("Wrong minimum value for HOUR", 603 0, gc.getMinimum(Calendar.HOUR)); 604 605 BitSet result = new BitSet(); 606 int[] min = { 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, -46800000, 607 0 }; 608 for (int i = 0; i < min.length; i++) { 609 if (gc.getMinimum(i) != min[i]) 610 result.set(i); 611 } 612 assertTrue("Wrong min for " + result, result.length() == 0); 613 } 614 615 /** 616 * @tests java.util.GregorianCalendar#isLeapYear(int) 617 */ 618 @TestTargetNew( 619 level = TestLevel.COMPLETE, 620 notes = "", 621 method = "isLeapYear", 622 args = {int.class} 623 ) 624 public void test_isLeapYearI() { 625 // Test for method boolean java.util.GregorianCalendar.isLeapYear(int) 626 GregorianCalendar gc = new GregorianCalendar(1998, 11, 6); 627 assertTrue("Returned incorrect value for leap year", !gc 628 .isLeapYear(1998)); 629 assertTrue("Returned incorrect value for leap year", gc 630 .isLeapYear(2000)); 631 632 } 633 634 /** 635 * @tests java.util.GregorianCalendar#roll(int, int) 636 */ 637 @TestTargetNew( 638 level = TestLevel.COMPLETE, 639 notes = "", 640 method = "roll", 641 args = {int.class, int.class} 642 ) 643 public void test_rollII() { 644 // Test for method void java.util.GregorianCalendar.roll(int, int) 645 GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 8, 646 2, 5, 0); 647 gc.roll(Calendar.DAY_OF_MONTH, -1); 648 assertTrue("Failed to roll DAY_OF_MONTH down by 1", gc 649 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 7, 2, 5, 650 0))); 651 gc = new GregorianCalendar(1972, Calendar.OCTOBER, 8, 2, 5, 0); 652 gc.roll(Calendar.DAY_OF_MONTH, 25); 653 assertTrue("Failed to roll DAY_OF_MONTH up by 25", gc 654 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 2, 2, 5, 655 0))); 656 gc = new GregorianCalendar(1972, Calendar.OCTOBER, 8, 2, 5, 0); 657 gc.roll(Calendar.DAY_OF_MONTH, -10); 658 assertTrue("Failed to roll DAY_OF_MONTH down by 10", gc 659 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 29, 2, 5, 660 0))); 661 } 662 663 /** 664 * @tests java.util.GregorianCalendar#roll(int, boolean) 665 */ 666 @TestTargetNew( 667 level = TestLevel.COMPLETE, 668 notes = "", 669 method = "roll", 670 args = {int.class, boolean.class} 671 ) 672 public void test_rollIZ() { 673 // Test for method void java.util.GregorianCalendar.roll(int, boolean) 674 GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 675 13, 19, 9, 59); 676 gc.roll(Calendar.DAY_OF_MONTH, false); 677 assertTrue("Failed to roll day_of_month down", gc 678 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 12, 19, 679 9, 59))); 680 gc = new GregorianCalendar(1972, Calendar.OCTOBER, 13, 19, 9, 59); 681 gc.roll(Calendar.DAY_OF_MONTH, true); 682 assertTrue("Failed to roll day_of_month up", gc 683 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 14, 19, 684 9, 59))); 685 gc = new GregorianCalendar(1972, Calendar.OCTOBER, 31, 19, 9, 59); 686 gc.roll(Calendar.DAY_OF_MONTH, true); 687 assertTrue("Failed to roll day_of_month up", gc 688 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 1, 19, 9, 689 59))); 690 691 GregorianCalendar cal = new GregorianCalendar(); 692 int result; 693 try { 694 cal.roll(Calendar.ZONE_OFFSET, true); 695 result = 0; 696 } catch (IllegalArgumentException e) { 697 result = 1; 698 } 699 assertEquals("ZONE_OFFSET roll", 1, result); 700 try { 701 cal.roll(Calendar.DST_OFFSET, true); 702 result = 0; 703 } catch (IllegalArgumentException e) { 704 result = 1; 705 } 706 assertEquals("ZONE_OFFSET roll", 1, result); 707 708 cal.set(2004, Calendar.DECEMBER, 31, 5, 0, 0); 709 cal.roll(Calendar.WEEK_OF_YEAR, true); 710 assertTrue("Wrong year: " + cal.getTime(), 711 cal.get(Calendar.YEAR) == 2004); 712 assertTrue("Wrong month: " + cal.getTime(), 713 cal.get(Calendar.MONTH) == Calendar.JANUARY); 714 assertTrue("Wrong date: " + cal.getTime(), cal.get(Calendar.DATE) == 9); 715 } 716 717 /** 718 * @tests java.util.GregorianCalendar#setGregorianChange(java.util.Date) 719 */ 720 @TestTargetNew( 721 level = TestLevel.COMPLETE, 722 notes = "", 723 method = "setGregorianChange", 724 args = {java.util.Date.class} 725 ) 726 public void test_setGregorianChangeLjava_util_Date() { 727 // Test for method void 728 // java.util.GregorianCalendar.setGregorianChange(java.util.Date) 729 GregorianCalendar gc1 = new GregorianCalendar(1582, Calendar.OCTOBER, 730 4, 0, 0); 731 GregorianCalendar gc2 = new GregorianCalendar(1972, Calendar.OCTOBER, 732 13, 0, 0); 733 gc1.setGregorianChange(gc2.getTime()); 734 assertTrue("Returned incorrect value", gc2.getTime().equals( 735 gc1.getGregorianChange())); 736 } 737 738 /** 739 * @tests java.util.GregorianCalendar#clone() 740 */ 741 @TestTargetNew( 742 level = TestLevel.COMPLETE, 743 notes = "", 744 method = "clone", 745 args = {} 746 ) 747 public void test_clone() { 748 749 // Regression for HARMONY-498 750 GregorianCalendar gCalend = new GregorianCalendar(); 751 752 gCalend.set(Calendar.MILLISECOND, 0); 753 int dayOfMonth = gCalend.get(Calendar.DAY_OF_MONTH); 754 755 // create clone object and change date 756 GregorianCalendar gCalendClone = (GregorianCalendar) gCalend.clone(); 757 gCalendClone.add(Calendar.DATE, 1); 758 759 assertEquals("Before", dayOfMonth, gCalend.get(Calendar.DAY_OF_MONTH)); 760 gCalend.set(Calendar.MILLISECOND, 0);//changes nothing 761 assertEquals("After", dayOfMonth, gCalend.get(Calendar.DAY_OF_MONTH)); 762 } 763 764 /** 765 * @tests java.util.GregorianCalendar#getMinimalDaysInFirstWeek() 766 */ 767 @TestTargetNew( 768 level = TestLevel.COMPLETE, 769 notes = "", 770 method = "getMinimalDaysInFirstWeek", 771 args = {} 772 ) 773 @KnownFailure("Some difference in timezones and/or locales data" 774 + "Who is right, the CLDR or the RI?") 775 public void test_getMinimalDaysInFirstWeek() { 776 // Regression for Harmony-1037 777 GregorianCalendar g = new GregorianCalendar(TimeZone 778 .getTimeZone("Paris/France"), new Locale("en", "GB")); 779 int minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek(); 780 assertEquals(4, minimalDaysInFirstWeek); 781 782 g = new GregorianCalendar(TimeZone.getTimeZone("Paris/France"), 783 new Locale("fr", "FR")); 784 minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek(); 785 assertEquals(4, minimalDaysInFirstWeek); 786 787 g = new GregorianCalendar(TimeZone.getTimeZone("Europe/London"), 788 new Locale("fr", "CA")); 789 minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek(); 790 assertEquals(1, minimalDaysInFirstWeek); 791 } 792 793 /** 794 * @tests java.util.GregorianCalendar#computeTime() 795 */ 796 @TestTargetNew( 797 level = TestLevel.COMPLETE, 798 notes = "Checks computeTime indirectly.", 799 method = "computeTime", 800 args = {} 801 ) 802 public void test_computeTime() { 803 804 // Regression for Harmony-493 805 GregorianCalendar g = new GregorianCalendar( 806 TimeZone.getTimeZone("Europe/London"), 807 new Locale("en", "GB") 808 ); 809 g.clear(); 810 g.set(2006, 02, 26, 01, 50, 00); 811 assertEquals(1143337800000L, g.getTimeInMillis()); 812 813 GregorianCalendar g1 = new GregorianCalendar( 814 TimeZone.getTimeZone("Europe/Moscow") 815 ); 816 g1.clear(); 817 g1.set(2006, 02, 26, 02, 20, 00); // in the DST transition interval 818 assertEquals(1143328800000L, g1.getTimeInMillis()); 819 assertEquals(3, g1.get(Calendar.HOUR_OF_DAY)); 820 g1.clear(); 821 g1.set(2006, 9, 29, 02, 50, 00); // transition from DST 822 assertEquals(1162079400000L, g1.getTimeInMillis()); 823 assertEquals(2, g1.get(Calendar.HOUR_OF_DAY)); 824 // End of regression test 825 826 g1.set(2006, -9, 29, 02, 50, 00); // transition from DST 827 g1.setLenient(false); 828 829 try { 830 g1.getTimeInMillis(); 831 fail("IllegalArgumentException expected"); 832 } catch (IllegalArgumentException e) { 833 //expected 834 } 835 } 836 837 @TestTargetNew( 838 level = TestLevel.COMPLETE, 839 notes = "Checks computeFields indirectly.", 840 method = "computeFields", 841 args = {} 842 ) 843 public void test_computeFields() { 844 GregorianCalendar g = new GregorianCalendar( 845 TimeZone.getTimeZone("Europe/London"), 846 new Locale("en", "GB") 847 ); 848 g.clear(); 849 g.setTimeInMillis(1222185600225L); 850 assertEquals(1, g.get(Calendar.ERA)); 851 assertEquals(2008, g.get(Calendar.YEAR)); 852 assertEquals(Calendar.SEPTEMBER, g.get(Calendar.MONTH)); 853 assertEquals(23, g.get(Calendar.DAY_OF_MONTH)); 854 assertEquals(17, g.get(Calendar.HOUR_OF_DAY)); 855 assertEquals(0, g.get(Calendar.MINUTE)); 856 } 857 858 /** 859 * @tests java.util.GregorianCalendar#get(int) 860 */ 861 @TestTargetNew( 862 level = TestLevel.PARTIAL_COMPLETE, 863 notes = "Doesn't verify ArrayIndexOutOfBoundsException.", 864 method = "get", 865 args = {int.class} 866 ) 867 @SuppressWarnings("deprecation") 868 public void test_getI() { 869 // Regression test for HARMONY-2959 870 Date date = new Date(Date.parse("Jan 15 00:00:01 GMT 2000")); 871 GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 872 gc.setGregorianChange(date); 873 gc.setTimeInMillis(Date.parse("Dec 24 00:00:01 GMT 2000")); 874 assertEquals(346, gc.get(Calendar.DAY_OF_YEAR)); 875 876 // Regression test for HARMONY-3003 877 date = new Date(Date.parse("Feb 28 00:00:01 GMT 2000")); 878 gc = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 879 gc.setGregorianChange(date); 880 gc.setTimeInMillis(Date.parse("Dec 1 00:00:01 GMT 2000")); 881 assertEquals(1, gc.get(Calendar.DAY_OF_MONTH)); 882 assertEquals(11, gc.get(Calendar.MONTH)); 883 } 884 885 @TestTargetNew( 886 level = TestLevel.COMPLETE, 887 notes = "", 888 method = "hashCode", 889 args = {} 890 ) 891 public void test_hashCode() { 892 GregorianCalendar g = new GregorianCalendar( 893 TimeZone.getTimeZone("Europe/London"), 894 new Locale("en", "GB") 895 ); 896 g.clear(); 897 g.setTimeInMillis(1222185600225L); 898 899 GregorianCalendar g1 = new GregorianCalendar( 900 TimeZone.getTimeZone("Europe/Moscow")); 901 g1.clear(); 902 g1.set(2008, Calendar.SEPTEMBER, 23, 18, 0, 0); 903 assertNotSame(g.hashCode(), g1.hashCode()); 904 } 905 906 @TestTargetNew( 907 level = TestLevel.COMPLETE, 908 notes = "", 909 method = "setFirstDayOfWeek", 910 args = {int.class} 911 ) 912 public void test_setFirstDayOfWeekI() { 913 GregorianCalendar g = new GregorianCalendar( 914 TimeZone.getTimeZone("Europe/London"), 915 new Locale("en", "GB")); 916 917 for (int i = 0; i < 10; i++) { 918 g.setFirstDayOfWeek(i); 919 assertEquals(i, g.getFirstDayOfWeek()); 920 } 921 g.setLenient(false); 922 g.setFirstDayOfWeek(10); 923 g.setFirstDayOfWeek(-10); 924 } 925 926 @TestTargetNew( 927 level = TestLevel.COMPLETE, 928 notes = "", 929 method = "setMinimalDaysInFirstWeek", 930 args = {int.class} 931 ) 932 public void test_setMinimalDaysInFirstWeekI() { 933 GregorianCalendar g = new GregorianCalendar( 934 TimeZone.getTimeZone("Europe/London"), 935 new Locale("en", "GB")); 936 937 for (int i = 0; i < 10; i++) { 938 g.setMinimalDaysInFirstWeek(i); 939 assertEquals(i, g.getMinimalDaysInFirstWeek()); 940 } 941 g.setLenient(false); 942 g.setMinimalDaysInFirstWeek(10); 943 g.setMinimalDaysInFirstWeek(-10); 944 } 945 946 /** 947 * Sets up the fixture, for example, open a network connection. This method 948 * is called before a test is executed. 949 */ 950 protected void setUp() { 951 Locale.setDefault(Locale.US); 952 } 953 954 /** 955 * Tears down the fixture, for example, close a network connection. This 956 * method is called after a test is executed. 957 */ 958 protected void tearDown() { 959 } 960 } 961