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 org.apache.harmony.luni.tests.java.util; 19 20 import java.util.BitSet; 21 import java.util.Calendar; 22 import java.util.Date; 23 import java.util.GregorianCalendar; 24 import java.util.Locale; 25 import java.util.SimpleTimeZone; 26 import java.util.TimeZone; 27 import java.util.Vector; 28 29 public class GregorianCalendarTest extends junit.framework.TestCase { 30 31 /** 32 * @tests java.util.GregorianCalendar#GregorianCalendar() 33 */ 34 public void test_Constructor() { 35 // Test for method java.util.GregorianCalendar() 36 assertTrue("Constructed incorrect calendar", (new GregorianCalendar() 37 .isLenient())); 38 } 39 40 /** 41 * @tests java.util.GregorianCalendar#GregorianCalendar(int, int, int) 42 */ 43 public void test_ConstructorIII() { 44 // Test for method java.util.GregorianCalendar(int, int, int) 45 GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 13); 46 assertEquals("Incorrect calendar constructed 1", 47 1972, gc.get(Calendar.YEAR)); 48 assertTrue("Incorrect calendar constructed 2", 49 gc.get(Calendar.MONTH) == Calendar.OCTOBER); 50 assertEquals("Incorrect calendar constructed 3", 13, gc 51 .get(Calendar.DAY_OF_MONTH)); 52 assertTrue("Incorrect calendar constructed 4", gc.getTimeZone().equals( 53 TimeZone.getDefault())); 54 } 55 56 /** 57 * @tests java.util.GregorianCalendar#GregorianCalendar(int, int, int, int, 58 * int) 59 */ 60 public void test_ConstructorIIIII() { 61 // Test for method java.util.GregorianCalendar(int, int, int, int, int) 62 GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 63 13, 19, 9); 64 assertEquals("Incorrect calendar constructed", 65 1972, gc.get(Calendar.YEAR)); 66 assertTrue("Incorrect calendar constructed", 67 gc.get(Calendar.MONTH) == Calendar.OCTOBER); 68 assertEquals("Incorrect calendar constructed", 13, gc 69 .get(Calendar.DAY_OF_MONTH)); 70 assertEquals("Incorrect calendar constructed", 7, gc.get(Calendar.HOUR)); 71 assertEquals("Incorrect calendar constructed", 72 1, gc.get(Calendar.AM_PM)); 73 assertEquals("Incorrect calendar constructed", 74 9, gc.get(Calendar.MINUTE)); 75 assertTrue("Incorrect calendar constructed", gc.getTimeZone().equals( 76 TimeZone.getDefault())); 77 78 //Regression for HARMONY-998 79 gc = new GregorianCalendar(1900, 0, 0, 0, Integer.MAX_VALUE); 80 assertEquals("Incorrect calendar constructed", 81 5983, gc.get(Calendar.YEAR)); 82 } 83 84 /** 85 * @tests java.util.GregorianCalendar#GregorianCalendar(int, int, int, int, 86 * int, int) 87 */ 88 public void test_ConstructorIIIIII() { 89 // Test for method java.util.GregorianCalendar(int, int, int, int, int, 90 // int) 91 GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 92 13, 19, 9, 59); 93 assertEquals("Incorrect calendar constructed", 94 1972, gc.get(Calendar.YEAR)); 95 assertTrue("Incorrect calendar constructed", 96 gc.get(Calendar.MONTH) == Calendar.OCTOBER); 97 assertEquals("Incorrect calendar constructed", 13, gc 98 .get(Calendar.DAY_OF_MONTH)); 99 assertEquals("Incorrect calendar constructed", 7, gc.get(Calendar.HOUR)); 100 assertEquals("Incorrect calendar constructed", 101 1, gc.get(Calendar.AM_PM)); 102 assertEquals("Incorrect calendar constructed", 103 9, gc.get(Calendar.MINUTE)); 104 assertEquals("Incorrect calendar constructed", 105 59, gc.get(Calendar.SECOND)); 106 assertTrue("Incorrect calendar constructed", gc.getTimeZone().equals( 107 TimeZone.getDefault())); 108 } 109 110 /** 111 * @tests java.util.GregorianCalendar#GregorianCalendar(java.util.Locale) 112 */ 113 public void test_ConstructorLjava_util_Locale() { 114 // Test for method java.util.GregorianCalendar(java.util.Locale) 115 Date date = new Date(); 116 GregorianCalendar gcJapan = new GregorianCalendar(Locale.JAPAN); 117 gcJapan.setTime(date); 118 GregorianCalendar gcJapan2 = new GregorianCalendar(Locale.JAPAN); 119 gcJapan2.setTime(date); 120 GregorianCalendar gcItaly = new GregorianCalendar(Locale.ITALY); 121 gcItaly.setTime(date); 122 assertTrue("Locales not created correctly", gcJapan.equals(gcJapan2) 123 && !gcJapan.equals(gcItaly)); 124 } 125 126 /** 127 * @tests java.util.GregorianCalendar#GregorianCalendar(java.util.TimeZone) 128 */ 129 public void test_ConstructorLjava_util_TimeZone() { 130 // Test for method java.util.GregorianCalendar(java.util.TimeZone) 131 Date date = new Date(2008,1,1); 132 TimeZone.getDefault(); 133 GregorianCalendar gc1 = new GregorianCalendar(TimeZone 134 .getTimeZone("EST")); 135 gc1.setTime(date); 136 GregorianCalendar gc2 = new GregorianCalendar(TimeZone 137 .getTimeZone("CST")); 138 gc2.setTime(date); 139 // CST is 1 hour before EST, add 1 to the CST time and convert to 0-12 140 // value 141 assertTrue("Incorrect calendar returned", 142 gc1.get(Calendar.HOUR) == ((gc2.get(Calendar.HOUR) + 1) % 12)); 143 144 // Regression test for HARMONY-2961 145 SimpleTimeZone timezone = new SimpleTimeZone(-3600 * 24 * 1000 * 2, 146 "GMT"); 147 GregorianCalendar gc = new GregorianCalendar(timezone); 148 149 // Regression test for HARMONY-5195 150 Calendar c1 = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 151 c1.set(Calendar.YEAR,1999); 152 c1.set(Calendar.MONTH,Calendar.JUNE); 153 c1.set(Calendar.DAY_OF_MONTH,2); 154 c1.set(Calendar.HOUR,15); 155 c1.set(Calendar.MINUTE,34); 156 c1.set(Calendar.SECOND,16); 157 assertEquals(34,c1.get(Calendar.MINUTE)); 158 c1.setTimeZone(new SimpleTimeZone(60000, "ONE MINUTE")); 159 assertEquals(35,c1.get(Calendar.MINUTE)); 160 161 } 162 163 /** 164 * @tests java.util.GregorianCalendar#GregorianCalendar(java.util.TimeZone, 165 * java.util.Locale) 166 */ 167 public void test_ConstructorLjava_util_TimeZoneLjava_util_Locale() { 168 // Test for method java.util.GregorianCalendar(java.util.TimeZone, 169 // java.util.Locale) 170 Date date = new Date(2008,1,1); 171 TimeZone.getDefault(); 172 GregorianCalendar gc1 = new GregorianCalendar(TimeZone 173 .getTimeZone("EST"), Locale.JAPAN); 174 gc1.setTime(date); 175 GregorianCalendar gc2 = new GregorianCalendar(TimeZone 176 .getTimeZone("EST"), Locale.JAPAN); 177 gc2.setTime(date); 178 GregorianCalendar gc3 = new GregorianCalendar(TimeZone 179 .getTimeZone("CST"), Locale.ITALY); 180 gc3.setTime(date); 181 // CST is 1 hour before EST, add 1 to the CST time and convert to 0-12 182 // value 183 assertTrue("Incorrect calendar returned", 184 gc1.get(Calendar.HOUR) == ((gc3.get(Calendar.HOUR) + 1) % 12)); 185 assertTrue("Locales not created correctly", gc1.equals(gc2) 186 && !gc1.equals(gc3)); 187 } 188 189 /** 190 * @tests java.util.GregorianCalendar#add(int, int) 191 */ 192 public void test_addII() { 193 // Test for method void java.util.GregorianCalendar.add(int, int) 194 GregorianCalendar gc1 = new GregorianCalendar(1998, 11, 6); 195 gc1.add(GregorianCalendar.YEAR, 1); 196 assertEquals("Add failed to Increment", 197 1999, gc1.get(GregorianCalendar.YEAR)); 198 199 gc1 = new GregorianCalendar(1999, Calendar.JULY, 31); 200 gc1.add(Calendar.MONTH, 7); 201 assertEquals("Wrong result year 1", 2000, gc1.get(Calendar.YEAR)); 202 assertTrue("Wrong result month 1", 203 gc1.get(Calendar.MONTH) == Calendar.FEBRUARY); 204 assertEquals("Wrong result date 1", 29, gc1.get(Calendar.DATE)); 205 206 gc1.add(Calendar.YEAR, -1); 207 assertEquals("Wrong result year 2", 1999, gc1.get(Calendar.YEAR)); 208 assertTrue("Wrong result month 2", 209 gc1.get(Calendar.MONTH) == Calendar.FEBRUARY); 210 assertEquals("Wrong result date 2", 28, gc1.get(Calendar.DATE)); 211 212 gc1 = new GregorianCalendar(TimeZone.getTimeZone("EST")); 213 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 214 gc1.add(Calendar.MILLISECOND, 24 * 60 * 60 * 1000); 215 assertEquals("Wrong time after MILLISECOND change", 16, gc1 216 .get(Calendar.HOUR_OF_DAY)); 217 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 218 gc1.add(Calendar.SECOND, 24 * 60 * 60); 219 assertEquals("Wrong time after SECOND change", 16, gc1 220 .get(Calendar.HOUR_OF_DAY)); 221 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 222 gc1.add(Calendar.MINUTE, 24 * 60); 223 assertEquals("Wrong time after MINUTE change", 16, gc1 224 .get(Calendar.HOUR_OF_DAY)); 225 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 226 gc1.add(Calendar.HOUR, 24); 227 assertEquals("Wrong time after HOUR change", 16, gc1 228 .get(Calendar.HOUR_OF_DAY)); 229 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 230 gc1.add(Calendar.HOUR_OF_DAY, 24); 231 assertEquals("Wrong time after HOUR_OF_DAY change", 16, gc1 232 .get(Calendar.HOUR_OF_DAY)); 233 234 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 235 gc1.add(Calendar.AM_PM, 2); 236 assertEquals("Wrong time after AM_PM change", 16, gc1 237 .get(Calendar.HOUR_OF_DAY)); 238 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 239 gc1.add(Calendar.DATE, 1); 240 assertEquals("Wrong time after DATE change", 16, gc1 241 .get(Calendar.HOUR_OF_DAY)); 242 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 243 gc1.add(Calendar.DAY_OF_YEAR, 1); 244 assertEquals("Wrong time after DAY_OF_YEAR change", 16, gc1 245 .get(Calendar.HOUR_OF_DAY)); 246 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 247 gc1.add(Calendar.DAY_OF_WEEK, 1); 248 assertEquals("Wrong time after DAY_OF_WEEK change", 16, gc1 249 .get(Calendar.HOUR_OF_DAY)); 250 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 251 gc1.add(Calendar.WEEK_OF_YEAR, 1); 252 assertEquals("Wrong time after WEEK_OF_YEAR change", 16, gc1 253 .get(Calendar.HOUR_OF_DAY)); 254 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 255 gc1.add(Calendar.WEEK_OF_MONTH, 1); 256 assertEquals("Wrong time after WEEK_OF_MONTH change", 16, gc1 257 .get(Calendar.HOUR_OF_DAY)); 258 gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change 259 gc1.add(Calendar.DAY_OF_WEEK_IN_MONTH, 1); 260 assertEquals("Wrong time after DAY_OF_WEEK_IN_MONTH change", 16, gc1 261 .get(Calendar.HOUR_OF_DAY)); 262 263 gc1.clear(); 264 gc1.set(2000, Calendar.APRIL, 1, 23, 0); 265 gc1.add(Calendar.DATE, 1); 266 assertTrue("Wrong time after DATE change near DST boundary", gc1 267 .get(Calendar.MONTH) == Calendar.APRIL 268 && gc1.get(Calendar.DATE) == 2 269 && gc1.get(Calendar.HOUR_OF_DAY) == 23); 270 } 271 272 /** 273 * @tests java.util.GregorianCalendar#equals(java.lang.Object) 274 */ 275 public void test_equalsLjava_lang_Object() { 276 // Test for method boolean 277 // java.util.GregorianCalendar.equals(java.lang.Object) 278 GregorianCalendar gc1 = new GregorianCalendar(1998, 11, 6); 279 GregorianCalendar gc2 = new GregorianCalendar(2000, 11, 6); 280 GregorianCalendar gc3 = new GregorianCalendar(1998, 11, 6); 281 assertTrue("Equality check failed", gc1.equals(gc3)); 282 assertTrue("Equality check failed", !gc1.equals(gc2)); 283 gc3.setGregorianChange(new Date()); 284 assertTrue("Different gregorian change", !gc1.equals(gc3)); 285 } 286 287 /** 288 * @tests java.util.GregorianCalendar#getActualMaximum(int) 289 */ 290 public void test_getActualMaximumI() { 291 // Test for method int java.util.GregorianCalendar.getActualMaximum(int) 292 GregorianCalendar gc1 = new GregorianCalendar(1900, 1, 1); 293 GregorianCalendar gc2 = new GregorianCalendar(1996, 1, 1); 294 GregorianCalendar gc3 = new GregorianCalendar(1997, 1, 1); 295 GregorianCalendar gc4 = new GregorianCalendar(2000, 1, 1); 296 GregorianCalendar gc5 = new GregorianCalendar(2000, 9, 9); 297 GregorianCalendar gc6 = new GregorianCalendar(2000, 3, 3); 298 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 1900", 299 28, gc1.getActualMaximum(Calendar.DAY_OF_MONTH)); 300 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 1996", 301 29, gc2.getActualMaximum(Calendar.DAY_OF_MONTH)); 302 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 1998", 303 28, gc3.getActualMaximum(Calendar.DAY_OF_MONTH)); 304 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 2000", 305 29, gc4.getActualMaximum(Calendar.DAY_OF_MONTH)); 306 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Oct 2000", 307 31, gc5.getActualMaximum(Calendar.DAY_OF_MONTH)); 308 assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Apr 2000", 309 30, gc6.getActualMaximum(Calendar.DAY_OF_MONTH)); 310 assertTrue("Wrong actual maximum value for MONTH", gc1 311 .getActualMaximum(Calendar.MONTH) == Calendar.DECEMBER); 312 assertEquals("Wrong actual maximum value for HOUR_OF_DAY", 23, gc1 313 .getActualMaximum(Calendar.HOUR_OF_DAY)); 314 assertEquals("Wrong actual maximum value for HOUR", 11, gc1 315 .getActualMaximum(Calendar.HOUR)); 316 assertEquals("Wrong actual maximum value for DAY_OF_WEEK_IN_MONTH", 4, gc6 317 .getActualMaximum(Calendar.DAY_OF_WEEK_IN_MONTH)); 318 319 320 // Regression test for harmony 2954 321 Date date = new Date(Date.parse("Jan 15 00:00:01 GMT 2000")); 322 GregorianCalendar gc = new GregorianCalendar(); 323 gc.setTimeInMillis(Date.parse("Dec 15 00:00:01 GMT 1582")); 324 assertEquals(355, gc.getActualMaximum(Calendar.DAY_OF_YEAR)); 325 gc.setGregorianChange(date); 326 gc.setTimeInMillis(Date.parse("Jan 16 00:00:01 GMT 2000")); 327 assertEquals(353, gc.getActualMaximum(Calendar.DAY_OF_YEAR)); 328 329 //Regression test for HARMONY-3004 330 gc = new GregorianCalendar(1900, 7, 1); 331 String[] ids = TimeZone.getAvailableIDs(); 332 for (int i = 0; i < ids.length; i++) { 333 TimeZone tz = TimeZone.getTimeZone(ids[i]); 334 gc.setTimeZone(tz); 335 for (int j = 1900; j < 2000; j++) { 336 gc.set(Calendar.YEAR, j); 337 assertEquals(7200000, gc.getActualMaximum(Calendar.DST_OFFSET)); 338 } 339 } 340 } 341 342 /** 343 * @tests java.util.GregorianCalendar#getActualMinimum(int) 344 */ 345 public void test_getActualMinimumI() { 346 // Test for method int java.util.GregorianCalendar.getActualMinimum(int) 347 GregorianCalendar gc1 = new GregorianCalendar(1900, 1, 1); 348 new GregorianCalendar(1996, 1, 1); 349 new GregorianCalendar(1997, 1, 1); 350 new GregorianCalendar(2000, 1, 1); 351 new GregorianCalendar(2000, 9, 9); 352 GregorianCalendar gc6 = new GregorianCalendar(2000, 3, 3); 353 assertEquals("Wrong actual minimum value for DAY_OF_MONTH for Feb 1900", 354 1, gc1.getActualMinimum(Calendar.DAY_OF_MONTH)); 355 assertTrue("Wrong actual minimum value for MONTH", gc1 356 .getActualMinimum(Calendar.MONTH) == Calendar.JANUARY); 357 assertEquals("Wrong actual minimum value for HOUR_OF_DAY", 0, gc1 358 .getActualMinimum(Calendar.HOUR_OF_DAY)); 359 assertEquals("Wrong actual minimum value for HOUR", 0, gc1 360 .getActualMinimum(Calendar.HOUR)); 361 assertEquals("Wrong actual minimum value for DAY_OF_WEEK_IN_MONTH", 1, gc6 362 .getActualMinimum(Calendar.DAY_OF_WEEK_IN_MONTH)); 363 } 364 365 /** 366 * @tests java.util.GregorianCalendar#getGreatestMinimum(int) 367 */ 368 public void test_getGreatestMinimumI() { 369 // Test for method int 370 // java.util.GregorianCalendar.getGreatestMinimum(int) 371 GregorianCalendar gc = new GregorianCalendar(); 372 assertEquals("Wrong greatest minimum value for DAY_OF_MONTH", 1, gc 373 .getGreatestMinimum(Calendar.DAY_OF_MONTH)); 374 assertTrue("Wrong greatest minimum value for MONTH", gc 375 .getGreatestMinimum(Calendar.MONTH) == Calendar.JANUARY); 376 assertEquals("Wrong greatest minimum value for HOUR_OF_DAY", 0, gc 377 .getGreatestMinimum(Calendar.HOUR_OF_DAY)); 378 assertEquals("Wrong greatest minimum value for HOUR", 0, gc 379 .getGreatestMinimum(Calendar.HOUR)); 380 381 BitSet result = new BitSet(); 382 int[] min = { 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, -46800000, 383 0 }; 384 for (int i = 0; i < min.length; i++) { 385 if (gc.getGreatestMinimum(i) != min[i]) 386 result.set(i); 387 } 388 assertTrue("Wrong greatest min for " + result, result.length() == 0); 389 } 390 391 /** 392 * @tests java.util.GregorianCalendar#getGregorianChange() 393 */ 394 public void test_getGregorianChange() { 395 // Test for method java.util.Date 396 // java.util.GregorianCalendar.getGregorianChange() 397 GregorianCalendar gc = new GregorianCalendar(); 398 GregorianCalendar returnedChange = new GregorianCalendar(TimeZone 399 .getTimeZone("EST")); 400 returnedChange.setTime(gc.getGregorianChange()); 401 assertEquals("Returned incorrect year", 402 1582, returnedChange.get(Calendar.YEAR)); 403 assertTrue("Returned incorrect month", returnedChange 404 .get(Calendar.MONTH) == Calendar.OCTOBER); 405 assertEquals("Returned incorrect day of month", 4, returnedChange 406 .get(Calendar.DAY_OF_MONTH)); 407 } 408 409 /** 410 * @tests java.util.GregorianCalendar#getLeastMaximum(int) 411 */ 412 public void test_getLeastMaximumI() { 413 // Test for method int java.util.GregorianCalendar.getLeastMaximum(int) 414 GregorianCalendar gc = new GregorianCalendar(); 415 assertEquals("Wrong least maximum value for DAY_OF_MONTH", 28, gc 416 .getLeastMaximum(Calendar.DAY_OF_MONTH)); 417 assertTrue("Wrong least maximum value for MONTH", gc 418 .getLeastMaximum(Calendar.MONTH) == Calendar.DECEMBER); 419 assertEquals("Wrong least maximum value for HOUR_OF_DAY", 23, gc 420 .getLeastMaximum(Calendar.HOUR_OF_DAY)); 421 assertEquals("Wrong least maximum value for HOUR", 11, gc 422 .getLeastMaximum(Calendar.HOUR)); 423 424 BitSet result = new BitSet(); 425 Vector values = new Vector(); 426 int[] max = { 1, 292269054, 11, 50, 3, 28, 355, 7, 3, 1, 11, 23, 59, 427 59, 999, 50400000, 1200000 }; 428 for (int i = 0; i < max.length; i++) { 429 if (gc.getLeastMaximum(i) != max[i]) { 430 result.set(i); 431 values.add(new Integer(gc.getLeastMaximum(i))); 432 } 433 } 434 assertTrue("Wrong least max for " + result + " = " + values, result 435 .length() == 0); 436 437 // Regression test for harmony-2947 438 Date date = new Date(Date.parse("Jan 1 00:00:01 GMT 2000")); 439 gc = new GregorianCalendar(); 440 gc.setGregorianChange(date); 441 gc.setTime(date); 442 assertEquals(gc.getActualMaximum(Calendar.WEEK_OF_YEAR), gc 443 .getLeastMaximum(Calendar.WEEK_OF_YEAR)); 444 } 445 446 /** 447 * @tests java.util.GregorianCalendar#getMaximum(int) 448 */ 449 public void test_getMaximumI() { 450 // Test for method int java.util.GregorianCalendar.getMaximum(int) 451 GregorianCalendar gc = new GregorianCalendar(); 452 assertEquals("Wrong maximum value for DAY_OF_MONTH", 31, gc 453 .getMaximum(Calendar.DAY_OF_MONTH)); 454 assertTrue("Wrong maximum value for MONTH", gc 455 .getMaximum(Calendar.MONTH) == Calendar.DECEMBER); 456 assertEquals("Wrong maximum value for HOUR_OF_DAY", 23, gc 457 .getMaximum(Calendar.HOUR_OF_DAY)); 458 assertEquals("Wrong maximum value for HOUR", 459 11, gc.getMaximum(Calendar.HOUR)); 460 461 BitSet result = new BitSet(); 462 Vector values = new Vector(); 463 int[] max = { 1, 292278994, 11, 53, 6, 31, 366, 7, 6, 1, 11, 23, 59, 464 59, 999, 50400000, 7200000 }; 465 for (int i = 0; i < max.length; i++) { 466 if (gc.getMaximum(i) != max[i]) { 467 result.set(i); 468 values.add(new Integer(gc.getMaximum(i))); 469 } 470 } 471 assertTrue("Wrong max for " + result + " = " + values, 472 result.length() == 0); 473 } 474 475 /** 476 * @tests java.util.GregorianCalendar#getMinimum(int) 477 */ 478 public void test_getMinimumI() { 479 // Test for method int java.util.GregorianCalendar.getMinimum(int) 480 GregorianCalendar gc = new GregorianCalendar(); 481 assertEquals("Wrong minimum value for DAY_OF_MONTH", 1, gc 482 .getMinimum(Calendar.DAY_OF_MONTH)); 483 assertTrue("Wrong minimum value for MONTH", gc 484 .getMinimum(Calendar.MONTH) == Calendar.JANUARY); 485 assertEquals("Wrong minimum value for HOUR_OF_DAY", 0, gc 486 .getMinimum(Calendar.HOUR_OF_DAY)); 487 assertEquals("Wrong minimum value for HOUR", 488 0, gc.getMinimum(Calendar.HOUR)); 489 490 BitSet result = new BitSet(); 491 int[] min = { 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, -46800000, 492 0 }; 493 for (int i = 0; i < min.length; i++) { 494 if (gc.getMinimum(i) != min[i]) 495 result.set(i); 496 } 497 assertTrue("Wrong min for " + result, result.length() == 0); 498 } 499 500 /** 501 * @tests java.util.GregorianCalendar#isLeapYear(int) 502 */ 503 public void test_isLeapYearI() { 504 // Test for method boolean java.util.GregorianCalendar.isLeapYear(int) 505 GregorianCalendar gc = new GregorianCalendar(1998, 11, 6); 506 assertTrue("Returned incorrect value for leap year", !gc 507 .isLeapYear(1998)); 508 assertTrue("Returned incorrect value for leap year", gc 509 .isLeapYear(2000)); 510 511 } 512 513 /** 514 * @tests java.util.GregorianCalendar#roll(int, int) 515 */ 516 public void test_rollII() { 517 // Test for method void java.util.GregorianCalendar.roll(int, int) 518 GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 8, 519 2, 5, 0); 520 gc.roll(Calendar.DAY_OF_MONTH, -1); 521 assertTrue("Failed to roll DAY_OF_MONTH down by 1", gc 522 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 7, 2, 5, 523 0))); 524 gc = new GregorianCalendar(1972, Calendar.OCTOBER, 8, 2, 5, 0); 525 gc.roll(Calendar.DAY_OF_MONTH, 25); 526 assertTrue("Failed to roll DAY_OF_MONTH up by 25", gc 527 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 2, 2, 5, 528 0))); 529 gc = new GregorianCalendar(1972, Calendar.OCTOBER, 8, 2, 5, 0); 530 gc.roll(Calendar.DAY_OF_MONTH, -10); 531 assertTrue("Failed to roll DAY_OF_MONTH down by 10", gc 532 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 29, 2, 5, 533 0))); 534 } 535 536 /** 537 * @tests java.util.GregorianCalendar#roll(int, boolean) 538 */ 539 public void test_rollIZ() { 540 // Test for method void java.util.GregorianCalendar.roll(int, boolean) 541 GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 542 13, 19, 9, 59); 543 gc.roll(Calendar.DAY_OF_MONTH, false); 544 assertTrue("Failed to roll day_of_month down", gc 545 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 12, 19, 546 9, 59))); 547 gc = new GregorianCalendar(1972, Calendar.OCTOBER, 13, 19, 9, 59); 548 gc.roll(Calendar.DAY_OF_MONTH, true); 549 assertTrue("Failed to roll day_of_month up", gc 550 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 14, 19, 551 9, 59))); 552 gc = new GregorianCalendar(1972, Calendar.OCTOBER, 31, 19, 9, 59); 553 gc.roll(Calendar.DAY_OF_MONTH, true); 554 assertTrue("Failed to roll day_of_month up", gc 555 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 1, 19, 9, 556 59))); 557 558 GregorianCalendar cal = new GregorianCalendar(); 559 int result; 560 try { 561 cal.roll(Calendar.ZONE_OFFSET, true); 562 result = 0; 563 } catch (IllegalArgumentException e) { 564 result = 1; 565 } 566 assertEquals("ZONE_OFFSET roll", 1, result); 567 try { 568 cal.roll(Calendar.DST_OFFSET, true); 569 result = 0; 570 } catch (IllegalArgumentException e) { 571 result = 1; 572 } 573 assertEquals("ZONE_OFFSET roll", 1, result); 574 575 cal.set(2004, Calendar.DECEMBER, 31, 5, 0, 0); 576 cal.roll(Calendar.WEEK_OF_YEAR, true); 577 assertEquals("Wrong year: " + cal.getTime(), 2004, cal 578 .get(Calendar.YEAR)); 579 assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal 580 .get(Calendar.MONTH)); 581 assertEquals("Wrong date: " + cal.getTime(), 9, cal.get(Calendar.DATE)); 582 583 // Regression for HARMONY-4372 584 cal.set(1994, 11, 30, 5, 0, 0); 585 cal.setMinimalDaysInFirstWeek(4); 586 cal.roll(Calendar.WEEK_OF_YEAR, true); 587 assertEquals("Wrong year: " + cal.getTime(), 1994, cal 588 .get(Calendar.YEAR)); 589 assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal 590 .get(Calendar.MONTH)); 591 assertEquals("Wrong date: " + cal.getTime(), 7, cal.get(Calendar.DATE)); 592 593 cal.roll(Calendar.WEEK_OF_YEAR, true); 594 assertEquals("Wrong year: " + cal.getTime(), 1994, cal 595 .get(Calendar.YEAR)); 596 assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal 597 .get(Calendar.MONTH)); 598 assertEquals("Wrong date: " + cal.getTime(), 14, cal.get(Calendar.DATE)); 599 600 cal.roll(Calendar.WEEK_OF_YEAR, false); 601 assertEquals("Wrong year: " + cal.getTime(), 1994, cal 602 .get(Calendar.YEAR)); 603 assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal 604 .get(Calendar.MONTH)); 605 assertEquals("Wrong date: " + cal.getTime(), 7, cal.get(Calendar.DATE)); 606 607 cal.roll(Calendar.WEEK_OF_YEAR, false); 608 assertEquals("Wrong year: " + cal.getTime(), 1994, cal 609 .get(Calendar.YEAR)); 610 assertEquals("Wrong month: " + cal.getTime(), Calendar.DECEMBER, cal 611 .get(Calendar.MONTH)); 612 assertEquals("Wrong date: " + cal.getTime(), 30, cal.get(Calendar.DATE)); 613 614 cal.roll(Calendar.WEEK_OF_YEAR, false); 615 assertEquals("Wrong year: " + cal.getTime(), 1994, cal 616 .get(Calendar.YEAR)); 617 assertEquals("Wrong month: " + cal.getTime(), Calendar.DECEMBER, cal 618 .get(Calendar.MONTH)); 619 assertEquals("Wrong date: " + cal.getTime(), 23, cal.get(Calendar.DATE)); 620 621 // Regression for HARMONY-4510 622 cal.set(1999, Calendar.DECEMBER, 31, 23, 59, 59); 623 cal.roll(GregorianCalendar.WEEK_OF_YEAR, true); 624 assertEquals("Wrong year: " + cal.getTime(), 1999, cal 625 .get(Calendar.YEAR)); 626 assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal 627 .get(Calendar.MONTH)); 628 assertEquals("Wrong date: " + cal.getTime(), 8, cal.get(Calendar.DATE)); 629 cal.roll(GregorianCalendar.WEEK_OF_YEAR, false); 630 assertEquals("Wrong year: " + cal.getTime(), 1999, cal 631 .get(Calendar.YEAR)); 632 assertEquals("Wrong month: " + cal.getTime(), Calendar.DECEMBER, cal 633 .get(Calendar.MONTH)); 634 assertEquals("Wrong date: " + cal.getTime(), 31, cal.get(Calendar.DATE)); 635 } 636 637 /** 638 * @tests java.util.GregorianCalendar#setGregorianChange(java.util.Date) 639 */ 640 public void test_setGregorianChangeLjava_util_Date() { 641 // Test for method void 642 // java.util.GregorianCalendar.setGregorianChange(java.util.Date) 643 GregorianCalendar gc1 = new GregorianCalendar(1582, Calendar.OCTOBER, 644 4, 0, 0); 645 GregorianCalendar gc2 = new GregorianCalendar(1972, Calendar.OCTOBER, 646 13, 0, 0); 647 gc1.setGregorianChange(gc2.getTime()); 648 assertTrue("Returned incorrect value", gc2.getTime().equals( 649 gc1.getGregorianChange())); 650 } 651 652 /** 653 * @tests java.util.GregorianCalendar#clone() 654 */ 655 public void test_clone() { 656 657 // Regression for HARMONY-498 658 GregorianCalendar gCalend = new GregorianCalendar(); 659 660 gCalend.set(Calendar.MILLISECOND, 0); 661 int dayOfMonth = gCalend.get(Calendar.DAY_OF_MONTH); 662 663 // create clone object and change date 664 GregorianCalendar gCalendClone = (GregorianCalendar) gCalend.clone(); 665 gCalendClone.add(Calendar.DATE, 1); 666 667 assertEquals("Before", dayOfMonth, gCalend.get(Calendar.DAY_OF_MONTH)); 668 gCalend.set(Calendar.MILLISECOND, 0);//changes nothing 669 assertEquals("After", dayOfMonth, gCalend.get(Calendar.DAY_OF_MONTH)); 670 } 671 672 /** 673 * @tests java.util.GregorianCalendar#getMinimalDaysInFirstWeek() 674 */ 675 public void test_getMinimalDaysInFirstWeek() { 676 // Regression for Harmony-1037 677 // Some non-bug differences below because of different CLDR data of Harmony 678 GregorianCalendar g = new GregorianCalendar(TimeZone 679 .getTimeZone("Europe/London"), new Locale("en", "GB")); 680 int minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek(); 681 assertEquals(4, minimalDaysInFirstWeek); 682 683 g = new GregorianCalendar(TimeZone.getTimeZone("Europe/London"), 684 new Locale("fr")); 685 minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek(); 686 assertEquals(4, minimalDaysInFirstWeek); 687 688 g = new GregorianCalendar(TimeZone.getTimeZone("Europe/London"), 689 new Locale("fr", "CA")); 690 minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek(); 691 assertEquals(4, minimalDaysInFirstWeek); 692 693 } 694 695 /** 696 * @tests java.util.GregorianCalendar#computeTime() 697 */ 698 public void test_computeTime() { 699 // Regression for Harmony-493 700 GregorianCalendar g = new GregorianCalendar( 701 TimeZone.getTimeZone("Europe/London"), 702 new Locale("en", "GB") 703 ); 704 g.clear(); 705 g.set(2006, 02, 26, 01, 50, 00); 706 assertEquals(1143337800000L, g.getTimeInMillis()); 707 708 GregorianCalendar g1 = new GregorianCalendar( 709 TimeZone.getTimeZone("Europe/Moscow") 710 ); 711 g1.clear(); 712 g1.set(2006, 02, 26, 02, 20, 00); // in the DST transition interval 713 assertEquals(1143328800000L, g1.getTimeInMillis()); 714 assertEquals(3, g1.get(Calendar.HOUR_OF_DAY)); 715 g1.clear(); 716 g1.set(2006, 9, 29, 02, 50, 00); // transition from DST 717 assertEquals(1162079400000L, g1.getTimeInMillis()); 718 assertEquals(2, g1.get(Calendar.HOUR_OF_DAY)); 719 // End of regression test 720 } 721 722 /** 723 * @tests java.util.GregorianCalendar#get(int) 724 */ 725 @SuppressWarnings("deprecation") 726 public void test_getI() { 727 // Regression test for HARMONY-2959 728 Date date = new Date(Date.parse("Jan 15 00:00:01 GMT 2000")); 729 GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 730 gc.setGregorianChange(date); 731 gc.setTimeInMillis(Date.parse("Dec 24 00:00:01 GMT 2000")); 732 assertEquals(346, gc.get(Calendar.DAY_OF_YEAR)); 733 734 // Regression test for HARMONY-3003 735 date = new Date(Date.parse("Feb 28 00:00:01 GMT 2000")); 736 gc = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 737 gc.setGregorianChange(date); 738 gc.setTimeInMillis(Date.parse("Dec 1 00:00:01 GMT 2000")); 739 assertEquals(1, gc.get(Calendar.DAY_OF_MONTH)); 740 assertEquals(11, gc.get(Calendar.MONTH)); 741 742 // Regression test for HARMONY-4513 743 gc = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 744 gc.set(1582, Calendar.OCTOBER, 15, 0, 0, 0); 745 // reset millisecond to zero in order to be the same time as cutover 746 gc.set(Calendar.MILLISECOND, 0); 747 assertEquals(0, gc.get(Calendar.MILLISECOND)); 748 assertEquals(1582, gc.get(Calendar.YEAR)); 749 assertEquals(Calendar.OCTOBER, gc.get(Calendar.MONTH)); 750 assertEquals(15, gc.get(Calendar.DAY_OF_MONTH)); 751 assertEquals(0, gc.get(Calendar.HOUR_OF_DAY)); 752 assertEquals(0, gc.get(Calendar.MINUTE)); 753 assertEquals(0, gc.get(Calendar.SECOND)); 754 gc.set(1582, Calendar.OCTOBER, 14, 0, 0, 0); 755 assertEquals(24, gc.get(Calendar.DAY_OF_MONTH)); 756 757 // Regression test for HARMONY-2422 758 gc = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 759 gc.set(GregorianCalendar.ZONE_OFFSET, -1); 760 assertEquals(-1, gc.get(GregorianCalendar.ZONE_OFFSET)); 761 gc.set(GregorianCalendar.ZONE_OFFSET, 1); 762 assertEquals(1, gc.get(GregorianCalendar.ZONE_OFFSET)); 763 gc.set(GregorianCalendar.ZONE_OFFSET, 0); 764 assertEquals(0, gc.get(GregorianCalendar.ZONE_OFFSET)); 765 } 766 767 /** 768 * Sets up the fixture, for example, open a network connection. This method 769 * is called before a test is executed. 770 */ 771 protected void setUp() { 772 } 773 774 /** 775 * Tears down the fixture, for example, close a network connection. This 776 * method is called after a test is executed. 777 */ 778 protected void tearDown() { 779 } 780 } 781