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 java.text.DateFormatSymbols; 21 import java.util.ArrayList; 22 import java.util.Calendar; 23 import java.util.Date; 24 import java.util.GregorianCalendar; 25 import java.util.List; 26 import java.util.Locale; 27 import java.util.Map; 28 import java.util.TimeZone; 29 30 import org.apache.harmony.testframework.serialization.SerializationTest; 31 32 public class CalendarTest extends junit.framework.TestCase { 33 34 Locale defaultLocale; 35 36 /** 37 * java.util.Calendar#set(int, int) 38 */ 39 public void test_setII() { 40 // Test for correct result defined by the last set field 41 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST")); 42 43 cal.clear(); 44 cal.set(Calendar.YEAR, 2002); 45 assertTrue("Incorrect result 0: " + cal.getTime().getTime(), cal 46 .getTime().getTime() == 1009861200000L); 47 48 cal.clear(); 49 cal.set(Calendar.YEAR, 2002); 50 cal.set(Calendar.MONTH, Calendar.MARCH); 51 assertTrue("Incorrect result 0a: " + cal.getTime(), cal.getTime() 52 .getTime() == 1014958800000L); 53 54 cal.clear(); 55 cal.set(Calendar.YEAR, 2002); 56 cal.set(Calendar.DATE, 24); 57 assertTrue("Incorrect result 0b: " + cal.getTime(), cal.getTime() 58 .getTime() == 1011848400000L); 59 60 cal.set(Calendar.MONTH, Calendar.OCTOBER); 61 cal.set(Calendar.DATE, 31); 62 cal.set(Calendar.MONTH, Calendar.NOVEMBER); 63 cal.set(Calendar.DATE, 26); 64 assertTrue("Incorrect month: " + cal.get(Calendar.MONTH), cal 65 .get(Calendar.MONTH) == Calendar.NOVEMBER); 66 67 int dow = cal.get(Calendar.DAY_OF_WEEK); 68 cal.set(Calendar.DATE, 27); 69 assertTrue("Incorrect DAY_OF_WEEK: " + cal.get(Calendar.DAY_OF_WEEK) 70 + " expected: " + dow, cal.get(Calendar.DAY_OF_WEEK) != dow); 71 72 cal.clear(); 73 cal.set(Calendar.YEAR, 2002); 74 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 75 assertTrue("Incorrect result 0c1: " + cal.getTime().getTime(), cal 76 .getTime().getTime() == 1010379600000L); 77 78 cal.clear(); 79 cal.set(Calendar.YEAR, 2002); 80 cal.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY); 81 assertTrue("Incorrect result 0c2: " + cal.getTime().getTime(), cal 82 .getTime().getTime() == 1009861200000L); 83 84 cal.clear(); 85 cal.set(Calendar.YEAR, 2002); 86 cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY); 87 assertTrue("Incorrect result 0c3: " + cal.getTime(), cal.getTime() 88 .getTime() == 1010034000000L); 89 90 cal.clear(); 91 cal.set(Calendar.YEAR, 2002); 92 cal.set(Calendar.WEEK_OF_MONTH, 2); 93 assertTrue("Incorrect result 0d: " + cal.getTime(), cal.getTime() 94 .getTime() == 1010293200000L); 95 96 cal.clear(); 97 cal.set(Calendar.YEAR, 2002); 98 cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 2); 99 assertTrue("Incorrect result 0e: " + cal.getTime(), cal.getTime() 100 .getTime() == 1010898000000L); 101 102 cal.clear(); 103 cal.set(Calendar.YEAR, 2002); 104 cal.set(Calendar.WEEK_OF_YEAR, 11); 105 assertTrue("Incorrect result 0f: " + cal.getTime(), cal.getTime() 106 .getTime() == 1015736400000L); 107 108 cal.clear(); 109 cal.set(Calendar.YEAR, 2002); 110 cal.set(Calendar.DATE, 24); 111 cal.set(Calendar.WEEK_OF_YEAR, 11); 112 assertTrue("Incorrect result 0g: " + cal.getTime(), cal.getTime() 113 .getTime() == 1011848400000L); 114 115 cal.clear(); 116 cal.set(Calendar.YEAR, 2002); 117 cal.get(Calendar.WEEK_OF_YEAR); // Force fields to compute 118 cal.set(Calendar.WEEK_OF_YEAR, 11); 119 assertTrue("Incorrect result 0h: " + cal.getTime(), cal.getTime() 120 .getTime() == 1015909200000L); 121 122 // WEEK_OF_YEAR has priority over MONTH/DATE 123 cal.clear(); 124 cal.set(Calendar.YEAR, 2002); 125 cal.set(Calendar.DAY_OF_YEAR, 170); 126 cal.set(Calendar.WEEK_OF_YEAR, 11); 127 cal.set(Calendar.MONTH, Calendar.JANUARY); 128 cal.set(Calendar.DATE, 5); 129 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 130 assertTrue("Incorrect result 1: " + cal.getTime(), cal.getTime() 131 .getTime() == 1015822800000L); 132 133 // WEEK_OF_YEAR has priority over MONTH/DATE 134 cal.clear(); 135 cal.set(Calendar.YEAR, 2002); 136 cal.set(Calendar.WEEK_OF_YEAR, 11); 137 cal.set(Calendar.MONTH, Calendar.JANUARY); 138 cal.set(Calendar.DATE, 5); 139 cal.set(Calendar.DAY_OF_YEAR, 170); 140 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 141 assertTrue("Incorrect result 1a: " + cal.getTime(), cal.getTime() 142 .getTime() == 1015822800000L); 143 144 // DAY_OF_WEEK has no effect when other fields not set 145 cal.clear(); 146 cal.set(Calendar.YEAR, 2002); 147 cal.set(Calendar.MONTH, Calendar.MARCH); 148 cal.set(Calendar.DATE, 11); 149 cal.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY); 150 assertTrue("Incorrect result 1b: " + cal.getTime(), cal.getTime() 151 .getTime() == 1015822800000L); 152 // Regression for HARMONY-4384 153 // Set DAY_OF_WEEK without DATE 154 cal.clear(); 155 cal.set(Calendar.YEAR, 2002); 156 cal.set(Calendar.MONTH, Calendar.MARCH); 157 cal.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY); 158 assertEquals("Incorrect result 1b: " + cal.getTime(), 1015304400000L, cal.getTime() 159 .getTime()); 160 161 162 // WEEK_OF_MONTH has priority 163 cal.clear(); 164 cal.set(Calendar.YEAR, 2002); 165 cal.set(Calendar.WEEK_OF_YEAR, 12); 166 cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); 167 cal.set(Calendar.WEEK_OF_MONTH, 3); 168 cal.set(Calendar.MONTH, Calendar.MARCH); 169 cal.set(Calendar.DATE, 5); 170 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 171 assertTrue("Incorrect result 2: " + cal.getTime(), cal.getTime() 172 .getTime() == 1015822800000L); 173 174 // DAY_OF_WEEK_IN_MONTH has priority over WEEK_OF_YEAR 175 cal.clear(); 176 cal.set(Calendar.YEAR, 2002); 177 cal.set(Calendar.WEEK_OF_YEAR, 12); 178 cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 2); 179 cal.set(Calendar.MONTH, Calendar.MARCH); 180 cal.set(Calendar.DATE, 5); 181 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 182 assertTrue("Incorrect result 3: " + cal.getTime(), cal.getTime() 183 .getTime() == 1015822800000L); 184 185 // WEEK_OF_MONTH has priority, MONTH not set 186 cal.clear(); 187 cal.set(Calendar.YEAR, 2002); 188 cal.set(Calendar.WEEK_OF_YEAR, 12); 189 cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); 190 cal.set(Calendar.WEEK_OF_MONTH, 3); 191 cal.set(Calendar.DATE, 25); 192 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 193 assertTrue("Incorrect result 4: " + cal.getTime(), cal.getTime() 194 .getTime() == 1010984400000L); 195 196 // WEEK_OF_YEAR has priority when MONTH set last and DAY_OF_WEEK set 197 cal.clear(); 198 cal.set(Calendar.YEAR, 2002); 199 cal.set(Calendar.WEEK_OF_YEAR, 11); 200 cal.set(Calendar.DATE, 25); 201 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 202 cal.set(Calendar.MONTH, Calendar.JANUARY); 203 assertTrue("Incorrect result 5: " + cal.getTime(), cal.getTime() 204 .getTime() == 1015822800000L); 205 206 // Use MONTH/DATE when WEEK_OF_YEAR set but not DAY_OF_WEEK 207 cal.clear(); 208 cal.set(Calendar.YEAR, 2002); 209 cal.set(Calendar.WEEK_OF_YEAR, 12); 210 cal.set(Calendar.DATE, 11); 211 cal.set(Calendar.MONTH, Calendar.MARCH); 212 assertTrue("Incorrect result 5a: " + cal.getTime(), cal.getTime() 213 .getTime() == 1015822800000L); 214 215 // Use MONTH/DATE when DAY_OF_WEEK is not set 216 cal.clear(); 217 cal.set(Calendar.YEAR, 2002); 218 cal.set(Calendar.WEEK_OF_YEAR, 12); 219 cal.set(Calendar.DATE, 11); 220 cal.set(Calendar.WEEK_OF_MONTH, 1); 221 cal.set(Calendar.MONTH, Calendar.MARCH); 222 assertTrue("Incorrect result 5b: " + cal.getTime(), cal.getTime() 223 .getTime() == 1015822800000L); 224 225 // WEEK_OF_MONTH has priority 226 cal.clear(); 227 cal.set(Calendar.YEAR, 2002); 228 cal.set(Calendar.WEEK_OF_YEAR, 12); 229 cal.set(Calendar.DATE, 5); 230 cal.set(Calendar.WEEK_OF_MONTH, 3); 231 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 232 cal.set(Calendar.MONTH, Calendar.MARCH); 233 assertTrue("Incorrect result 5c: " + cal.getTime(), cal.getTime() 234 .getTime() == 1015822800000L); 235 236 // DATE has priority when set last 237 cal.clear(); 238 cal.set(Calendar.YEAR, 2002); 239 cal.set(Calendar.WEEK_OF_YEAR, 12); 240 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 241 cal.set(Calendar.MONTH, Calendar.MARCH); 242 cal.set(Calendar.DATE, 11); 243 assertTrue("Incorrect result 6: " + cal.getTime(), cal.getTime() 244 .getTime() == 1015822800000L); 245 246 // DATE has priority when set last, MONTH not set 247 cal.clear(); 248 cal.set(Calendar.YEAR, 2002); 249 cal.set(Calendar.WEEK_OF_YEAR, 12); 250 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 251 cal.set(Calendar.DATE, 14); 252 assertTrue("Incorrect result 7: " + cal.getTime(), cal.getTime() 253 .getTime() == 1010984400000L); 254 255 // DAY_OF_YEAR has priority when MONTH set last and DATE not set 256 cal.clear(); 257 cal.set(Calendar.YEAR, 2002); 258 cal.set(Calendar.DAY_OF_YEAR, 70); 259 cal.set(Calendar.MONTH, Calendar.JANUARY); 260 assertTrue("Incorrect result 8: " + cal.getTime(), cal.getTime() 261 .getTime() == 1015822800000L); 262 263 // DAY/MONTH has priority when DATE set after DAY_OF_YEAR 264 cal.clear(); 265 cal.set(Calendar.YEAR, 2002); 266 cal.set(Calendar.DAY_OF_YEAR, 170); 267 cal.set(Calendar.DATE, 11); 268 cal.set(Calendar.MONTH, Calendar.MARCH); 269 assertTrue("Incorrect result 8a: " + cal.getTime(), cal.getTime() 270 .getTime() == 1015822800000L); 271 272 // DAY_OF_YEAR has priority when set after DATE 273 cal.clear(); 274 cal.set(Calendar.YEAR, 2002); 275 cal.set(Calendar.DATE, 15); 276 cal.set(Calendar.DAY_OF_YEAR, 70); 277 cal.set(Calendar.MONTH, Calendar.JANUARY); 278 assertTrue("Incorrect result 8b: " + cal.getTime(), cal.getTime() 279 .getTime() == 1015822800000L); 280 281 // DATE has priority when set last 282 cal.clear(); 283 cal.set(Calendar.YEAR, 2002); 284 cal.set(Calendar.DAY_OF_YEAR, 70); 285 cal.set(Calendar.DATE, 14); 286 assertTrue("Incorrect result 9: " + cal.getTime(), cal.getTime() 287 .getTime() == 1010984400000L); 288 289 // DATE has priority when set last 290 cal.clear(); 291 cal.set(Calendar.YEAR, 2002); 292 cal.set(Calendar.WEEK_OF_YEAR, 15); 293 cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY); 294 cal.set(Calendar.DATE, 14); 295 assertTrue("Incorrect result 9a: " + cal.getTime(), cal.getTime() 296 .getTime() == 1010984400000L); 297 298 cal.clear(); 299 cal.set(Calendar.YEAR, 2002); 300 cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 301 cal.set(Calendar.DATE, 14); 302 cal.set(Calendar.WEEK_OF_YEAR, 11); 303 assertTrue("Incorrect result 9b: " + cal.getTime(), cal.getTime() 304 .getTime() == 1015822800000L); 305 306 cal.clear(); 307 cal.set(Calendar.YEAR, 2002); 308 cal.set(Calendar.DATE, 14); 309 cal.set(Calendar.WEEK_OF_YEAR, 11); 310 assertTrue("Incorrect result 9c: " + cal.getTime(), cal.getTime() 311 .getTime() == 1010984400000L); 312 313 cal.clear(); 314 cal.set(Calendar.YEAR, 2002); 315 cal.set(Calendar.WEEK_OF_MONTH, 1); 316 cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY); 317 cal.set(Calendar.MONTH, Calendar.MARCH); 318 cal.set(Calendar.DATE, 11); 319 assertTrue("Incorrect result 9d: " + cal.getTime(), cal.getTime() 320 .getTime() == 1015822800000L); 321 322 // DAY_OF_YEAR has priority when DAY_OF_MONTH set last and other fields 323 // not set 324 cal.clear(); 325 cal.set(Calendar.YEAR, 2002); 326 cal.set(Calendar.DAY_OF_YEAR, 70); 327 cal.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY); 328 assertTrue("Incorrect result 10: " + cal.getTime(), cal.getTime() 329 .getTime() == 1015822800000L); 330 331 // MONTH/DATE has priority when DAY_OF_WEEK_IN_MONTH set last but 332 // DAY_OF_WEEK not set 333 cal.clear(); 334 cal.set(Calendar.YEAR, 2002); 335 cal.set(Calendar.DATE, 11); 336 cal.set(Calendar.MONTH, Calendar.MARCH); 337 cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); 338 assertTrue("Incorrect result 11: " + cal.getTime(), cal.getTime() 339 .getTime() == 1015822800000L); 340 341 // MONTH/DATE has priority when WEEK_OF_YEAR set last but DAY_OF_WEEK 342 // not set 343 cal.clear(); 344 cal.set(Calendar.YEAR, 2002); 345 cal.set(Calendar.DATE, 11); 346 cal.set(Calendar.MONTH, Calendar.MARCH); 347 cal.set(Calendar.WEEK_OF_YEAR, 15); 348 assertTrue("Incorrect result 12: " + cal.getTime(), cal.getTime() 349 .getTime() == 1015822800000L); 350 351 // MONTH/DATE has priority when WEEK_OF_MONTH set last but DAY_OF_WEEK 352 // not set 353 cal.clear(); 354 cal.set(Calendar.YEAR, 2002); 355 cal.set(Calendar.DATE, 11); 356 cal.set(Calendar.MONTH, Calendar.MARCH); 357 cal.set(Calendar.WEEK_OF_MONTH, 1); 358 assertTrue("Incorrect result 13: " + cal.getTime(), cal.getTime() 359 .getTime() == 1015822800000L); 360 361 // Ensure last date field set is reset after computing 362 cal.clear(); 363 cal.set(Calendar.YEAR, 2002); 364 cal.set(Calendar.DAY_OF_YEAR, 111); 365 cal.get(Calendar.YEAR); 366 cal.set(Calendar.MONTH, Calendar.MARCH); 367 cal.set(Calendar.AM_PM, Calendar.AM); 368 assertTrue("Incorrect result 14: " + cal.getTime(), cal.getTime() 369 .getTime() == 1016686800000L); 370 371 int hour = cal.get(Calendar.HOUR); 372 cal.set(Calendar.HOUR, hour); 373 cal.set(Calendar.AM_PM, Calendar.PM); 374 assertEquals("AM_PM not changed", Calendar.PM, cal.get(Calendar.AM_PM)); 375 // setting AM_PM without HOUR should not have any affect 376 cal.set(Calendar.AM_PM, Calendar.AM); 377 assertEquals("AM_PM was changed 1", 378 Calendar.AM, cal.get(Calendar.AM_PM)); 379 int hourOfDay = cal.get(Calendar.HOUR_OF_DAY); 380 hour = cal.get(Calendar.HOUR); 381 cal.set(Calendar.AM_PM, Calendar.PM); 382 assertEquals("AM_PM was changed 2", 383 Calendar.PM, cal.get(Calendar.AM_PM)); 384 assertEquals(hour, cal.get(Calendar.HOUR)); 385 assertEquals(hourOfDay + 12, cal.get(Calendar.HOUR_OF_DAY)); 386 387 // regression test for Harmony-2122 388 cal = Calendar.getInstance(); 389 int oldValue = cal.get(Calendar.AM_PM); 390 int newValue = (oldValue == Calendar.AM) ? Calendar.PM : Calendar.AM; 391 cal.set(Calendar.AM_PM, newValue); 392 newValue = cal.get(Calendar.AM_PM); 393 assertTrue(newValue != oldValue); 394 } 395 396 /** 397 * java.util.Calendar#setTime(java.util.Date) 398 */ 399 public void test_setTimeLjava_util_Date() { 400 Calendar cal = Calendar.getInstance(); 401 // Use millisecond time for testing in Core 402 cal.setTime(new Date(884581200000L)); // (98, Calendar.JANUARY, 12) 403 assertEquals("incorrect millis", 884581200000L, cal.getTime().getTime()); 404 cal.setTimeZone(TimeZone.getTimeZone("EST")); 405 cal.setTime(new Date(943506000000L)); // (99, Calendar.NOVEMBER, 25) 406 assertTrue("incorrect fields", cal.get(Calendar.YEAR) == 1999 407 && cal.get(Calendar.MONTH) == Calendar.NOVEMBER 408 && cal.get(Calendar.DATE) == 25); 409 } 410 411 /** 412 * java.util.Calendar#compareTo(Calendar) 413 */ 414 public void test_compareToLjava_util_Calendar_null() { 415 Calendar cal = Calendar.getInstance(); 416 try { 417 cal.compareTo(null); 418 fail("should throw NullPointerException"); 419 } catch (NullPointerException e) { 420 // expected 421 } 422 } 423 424 /** 425 * java.util.Calendar#compareTo(Calendar) 426 */ 427 public void test_compareToLjava_util_Calendar() { 428 Calendar cal = Calendar.getInstance(); 429 cal.clear(); 430 cal.set(1997, 12, 13, 23, 57); 431 432 Calendar anotherCal = Calendar.getInstance(); 433 anotherCal.clear(); 434 anotherCal.set(1997, 12, 13, 23, 57); 435 assertEquals(0, cal.compareTo(anotherCal)); 436 437 anotherCal = Calendar.getInstance(); 438 anotherCal.clear(); 439 anotherCal.set(1997, 11, 13, 24, 57); 440 assertEquals(1, cal.compareTo(anotherCal)); 441 442 anotherCal = Calendar.getInstance(); 443 anotherCal.clear(); 444 anotherCal.set(1997, 12, 13, 23, 58); 445 assertEquals(-1, cal.compareTo(anotherCal)); 446 } 447 448 /** 449 * java.util.Calendar#clone() 450 */ 451 public void test_clone() { 452 // Regression for HARMONY-475 453 Calendar cal = Calendar.getInstance(); 454 cal.set(2006, 5, 6, 11, 35); 455 Calendar anotherCal = (Calendar) cal.clone(); 456 // should be deep clone 457 assertNotSame("getTimeZone", cal.getTimeZone(), anotherCal 458 .getTimeZone()); 459 } 460 461 /** 462 * java.util.Calendar#getTimeInMillis() 463 */ 464 public void test_getTimeInMillis() { 465 Calendar cal = Calendar.getInstance(); 466 467 int year = Integer.MIN_VALUE + 71; 468 cal.setTimeZone(TimeZone.getTimeZone("GMT")); 469 cal.set(Calendar.YEAR, year + 1900); 470 cal.set(Calendar.MONTH, Calendar.JANUARY); 471 cal.set(Calendar.DATE, 1); 472 cal.set(Calendar.HOUR_OF_DAY, 0); 473 cal.set(Calendar.MINUTE, 0); 474 cal.set(Calendar.SECOND, 0); 475 cal.set(Calendar.MILLISECOND, 0); 476 477 assertEquals(6017546357372606464L, cal.getTimeInMillis()); 478 } 479 480 private static final Locale[] locales = new Locale[] { Locale.getDefault(), 481 Locale.US, Locale.UK, Locale.TAIWAN, Locale.PRC, Locale.KOREA, 482 Locale.JAPAN, Locale.ITALIAN, Locale.GERMAN, Locale.ENGLISH, 483 Locale.CHINA, Locale.CANADA, Locale.FRANCE }; 484 485 /** 486 * java.util.Calendar#before(Object) 487 * java.util.Calendar#after(Object) 488 */ 489 public void test_before_after() { 490 Calendar early = Calendar.getInstance(); 491 Calendar late = Calendar.getInstance(); 492 // test by second 493 early.set(2008, 3, 20, 17, 28, 12); 494 late.set(2008, 3, 20, 17, 28, 22); 495 // test before() 496 assertTrue(early.before(late)); 497 assertFalse(early.before(early)); 498 assertFalse(late.before(early)); 499 // test after(); 500 assertTrue(late.after(early)); 501 assertFalse(late.after(late)); 502 assertFalse(early.after(late)); 503 504 // test by minute 505 early.set(2008, 3, 20, 17, 18, 12); 506 late.set(2008, 3, 20, 17, 28, 12); 507 // test before() 508 assertTrue(early.before(late)); 509 assertFalse(early.before(early)); 510 assertFalse(late.before(early)); 511 // test after(); 512 assertTrue(late.after(early)); 513 assertFalse(late.after(late)); 514 assertFalse(early.after(late)); 515 516 // test by hour 517 early.set(2008, 3, 20, 17, 28, 12); 518 late.set(2008, 3, 20, 27, 28, 12); 519 // test before() 520 assertTrue(early.before(late)); 521 assertFalse(early.before(early)); 522 assertFalse(late.before(early)); 523 // test after(); 524 assertTrue(late.after(early)); 525 assertFalse(late.after(late)); 526 assertFalse(early.after(late)); 527 528 // test by day 529 early.set(2008, 3, 10, 17, 28, 12); 530 late.set(2008, 3, 20, 17, 28, 12); 531 // test before() 532 assertTrue(early.before(late)); 533 assertFalse(early.before(early)); 534 assertFalse(late.before(early)); 535 // test after(); 536 assertTrue(late.after(early)); 537 assertFalse(late.after(late)); 538 assertFalse(early.after(late)); 539 540 // test by month 541 early.set(2008, 2, 20, 17, 28, 12); 542 late.set(2008, 3, 20, 17, 28, 12); 543 // test before() 544 assertTrue(early.before(late)); 545 assertFalse(early.before(early)); 546 assertFalse(late.before(early)); 547 // test after(); 548 assertTrue(late.after(early)); 549 assertFalse(late.after(late)); 550 assertFalse(early.after(late)); 551 552 // test by year 553 early.set(2007, 3, 20, 17, 28, 12); 554 late.set(2008, 3, 20, 17, 28, 12); 555 // test before() 556 assertTrue(early.before(late)); 557 assertFalse(early.before(early)); 558 assertFalse(late.before(early)); 559 // test after(); 560 assertTrue(late.after(early)); 561 assertFalse(late.after(late)); 562 assertFalse(early.after(late)); 563 } 564 565 /** 566 * java.util.Calendar#clear() 567 * java.util.Calendar#clear(int) 568 */ 569 public void test_clear() { 570 Calendar calendar = Calendar.getInstance(); 571 572 int count = 6; 573 int[] fields = new int[count]; 574 int[] defaults = new int[count]; 575 576 fields[0] = Calendar.YEAR; 577 fields[1] = Calendar.MONTH; 578 fields[2] = Calendar.DATE; 579 fields[3] = Calendar.HOUR_OF_DAY; 580 fields[4] = Calendar.MINUTE; 581 fields[5] = Calendar.SECOND; 582 583 defaults[0] = 1970; 584 defaults[1] = 0; 585 defaults[2] = 1; 586 defaults[3] = 0; 587 defaults[4] = 0; 588 defaults[5] = 0; 589 590 calendar.set(2008, 3, 20, 17, 28, 12); 591 592 // test clear(int) 593 for (int i = 0; i < fields.length; i++) { 594 int index = fields[i]; 595 calendar.clear(index); 596 if (5 == index) { 597 // RI also doesn't change the value of DATE 598 assertEquals("Field " + index + " Should equal to 20.", 20, 599 calendar.get(index)); 600 } else if (11 == index) { 601 // RI also doesn't change the value of HOUR 602 assertEquals("Field " + index + " Should equal to 17.", 17, 603 calendar.get(index)); 604 } else { 605 // Other have been set to default values 606 assertEquals("Field " + index + " Should equal to " 607 + defaults[i] + ".", defaults[i], calendar.get(index)); 608 } 609 } 610 611 // test clear() 612 calendar.set(2008, 3, 20, 17, 28, 12); 613 614 calendar.clear(); 615 616 for (int i = 0; i < fields.length; i++) { 617 int index = fields[i]; 618 assertEquals("Field " + index + " Should equal to " 619 + defaults[i] + ".", defaults[i], calendar.get(index)); 620 } 621 } 622 623 /** 624 * java.util.Calendar#isSet(int) 625 */ 626 public void test_isSet() { 627 Calendar calendar = Calendar.getInstance(); 628 calendar.clear(); 629 for (int i = 0; i < Calendar.FIELD_COUNT; i++) { 630 assertFalse(calendar.isSet(i)); 631 } 632 } 633 634 /** 635 * java.util.Calendar#getAvailableLocales() 636 */ 637 public void test_getAvailableLocales() { 638 Locale[] locales = Calendar.getAvailableLocales(); 639 boolean exist = false; 640 for (int i = 0; i < locales.length; i++) { 641 Locale l = locales[i]; 642 if (Locale.US.equals(l)) { 643 exist = true; 644 break; 645 } 646 } 647 assertTrue(exist); 648 } 649 650 /** 651 * java.util.Calendar#getInstance(Locale) 652 * java.util.Calendar#getInstance(TimeZone, Locale) 653 */ 654 public void test_getInstance() { 655 // test getInstance(Locale) 656 Calendar us_calendar = Calendar.getInstance(Locale.US); 657 Calendar de_calendar = Calendar.getInstance(Locale.GERMAN); 658 assertEquals(Calendar.SUNDAY, us_calendar 659 .getFirstDayOfWeek()); 660 assertEquals(Calendar.MONDAY, de_calendar 661 .getFirstDayOfWeek()); 662 663 // test getInstance(Locale, TimeZone) 664 Calendar gmt_calendar = Calendar.getInstance(TimeZone 665 .getTimeZone("GMT"), Locale.US); 666 assertEquals(TimeZone.getTimeZone("GMT"), 667 gmt_calendar.getTimeZone()); 668 Calendar est_calendar = Calendar.getInstance(TimeZone 669 .getTimeZone("EST"), Locale.US); 670 assertEquals(TimeZone.getTimeZone("EST") 671 .getID(), est_calendar.getTimeZone().getID()); 672 } 673 674 /** 675 * java.util.Calendar#internalGet(int) 676 */ 677 public void test_internalGet() { 678 MockGregorianCalendar c = new MockGregorianCalendar(); 679 c.clear(Calendar.YEAR); 680 assertEquals(0, c.internal_get(Calendar.YEAR)); 681 } 682 683 /** 684 * java.util.Calendar#hashCode() 685 */ 686 public void test_hashcode() { 687 Calendar calendar = Calendar.getInstance(Locale.JAPAN); 688 assertTrue(calendar.hashCode() == calendar.hashCode()); 689 } 690 691 /** 692 * java.util.Calendar#roll(int, int) 693 */ 694 public void test_roll() { 695 Calendar calendar = Calendar.getInstance(); 696 calendar.set(2008, 3, 20, 17, 28, 12); 697 698 // roll up 699 calendar.roll(Calendar.DATE, 5); 700 assertEquals(25, calendar.get(Calendar.DATE)); 701 702 // roll down 703 calendar.roll(Calendar.DATE, -5); 704 assertEquals(20, calendar.get(Calendar.DATE)); 705 706 // roll 0 707 calendar.roll(Calendar.DATE, 0); 708 assertEquals(20, calendar.get(Calendar.DATE)); 709 710 // roll overweight 711 calendar.set(2008, 1, 31, 17, 28, 12); 712 calendar.roll(Calendar.MONTH, 1); 713 assertEquals(2, calendar.get(Calendar.DATE)); 714 715 } 716 717 /** 718 * java.util.Calendar#toString() 719 */ 720 public void test_toString() { 721 Calendar calendar = Calendar.getInstance(); 722 //Should be the current time with no interrogation in the string. 723 assertTrue(calendar.toString() instanceof String); 724 assertEquals(-1, calendar.toString().indexOf("?")); 725 calendar.clear(); 726 assertTrue(calendar.toString() instanceof String); 727 assertTrue(0 <= calendar.toString().indexOf("?")); 728 } 729 730 /** 731 * serialization/deserialization. 732 */ 733 public void testSerializationSelf() throws Exception { 734 Calendar calendar = Calendar.getInstance(); 735 calendar.set(2008, 3, 20, 17, 28, 12); 736 737 SerializationTest.verifySelf(calendar); 738 } 739 740 741 private class MockGregorianCalendar extends GregorianCalendar { 742 public int internal_get(int field) { 743 return super.internalGet(field); 744 } 745 } 746 747 private class MockCalendar extends Calendar { 748 749 public MockCalendar() { 750 super(); 751 } 752 753 @Override 754 public void add(int field, int value) { 755 } 756 757 @Override 758 protected void computeFields() { 759 } 760 761 @Override 762 protected void computeTime() { 763 } 764 765 @Override 766 public int getGreatestMinimum(int field) { 767 return 0; 768 } 769 770 @Override 771 public int getLeastMaximum(int field) { 772 return 0; 773 } 774 775 @Override 776 public int getMaximum(int field) { 777 return 0; 778 } 779 780 @Override 781 public int getMinimum(int field) { 782 return 0; 783 } 784 785 @Override 786 public void roll(int field, boolean increment) { 787 } 788 } 789 790 /** 791 * {@link java.util.Calendar#getDisplayName(int, int, Locale)} 792 * @since 1.6 793 */ 794 public void test_getDisplayNameIILjava_util_Locale() { 795 Calendar cal = Calendar.getInstance(); 796 for (int field = 0; field < Calendar.FIELD_COUNT; field++) { 797 for (Locale locale : locales) { 798 DateFormatSymbols symbols = new DateFormatSymbols(locale); 799 String value = null; 800 switch (field) { 801 case Calendar.AM_PM: 802 cal.set(Calendar.AM_PM, Calendar.AM); 803 value = symbols.getAmPmStrings()[0]; 804 assertEquals(cal.getDisplayName(field, Calendar.SHORT, 805 locale), value); 806 assertEquals(cal.getDisplayName(field, Calendar.LONG, 807 locale), value); 808 cal.set(Calendar.AM_PM, Calendar.PM); 809 value = symbols.getAmPmStrings()[1]; 810 assertEquals(cal.getDisplayName(field, Calendar.SHORT, 811 locale), value); 812 assertEquals(cal.getDisplayName(field, Calendar.LONG, 813 locale), value); 814 break; 815 case Calendar.ERA: 816 cal.set(Calendar.ERA, GregorianCalendar.BC); 817 value = symbols.getEras()[0]; 818 assertEquals(cal.getDisplayName(field, Calendar.SHORT, 819 locale), value); 820 assertEquals(cal.getDisplayName(field, Calendar.LONG, 821 locale), value); 822 cal.set(Calendar.ERA, GregorianCalendar.AD); 823 value = symbols.getEras()[1]; 824 assertEquals(cal.getDisplayName(field, Calendar.SHORT, 825 locale), value); 826 assertEquals(cal.getDisplayName(field, Calendar.LONG, 827 locale), value); 828 break; 829 case Calendar.MONTH: 830 cal.set(Calendar.DAY_OF_MONTH, 1); 831 for (int month = 0; month <= 11; month++) { 832 cal.set(Calendar.MONTH, month); 833 value = symbols.getShortMonths()[month]; 834 assertEquals(cal.getDisplayName(field, Calendar.SHORT, 835 locale), value); 836 value = symbols.getMonths()[month]; 837 assertEquals(cal.getDisplayName(field, Calendar.LONG, 838 locale), value); 839 } 840 break; 841 case Calendar.DAY_OF_WEEK: 842 for (int day = 1; day <= 7; day++) { 843 cal.set(Calendar.DAY_OF_WEEK, day); 844 value = symbols.getShortWeekdays()[day]; 845 assertEquals(cal.getDisplayName(field, Calendar.SHORT, 846 locale), value); 847 value = symbols.getWeekdays()[day]; 848 assertEquals(cal.getDisplayName(field, Calendar.LONG, 849 locale), value); 850 } 851 break; 852 default: 853 assertNull(cal 854 .getDisplayName(field, Calendar.SHORT, locale)); 855 assertNull(cal.getDisplayName(field, Calendar.LONG, locale)); 856 } 857 } 858 } 859 860 cal.setLenient(true); 861 862 try { 863 cal.getDisplayName(-1, Calendar.SHORT, Locale.US); 864 fail("Should throw IllegalArgumentException"); 865 } catch (IllegalArgumentException e) { 866 // expected 867 } 868 try { 869 cal.getDisplayName(Calendar.FIELD_COUNT, Calendar.LONG, Locale.US); 870 fail("Should throw IllegalArgumentException"); 871 } catch (IllegalArgumentException e) { 872 // expected 873 } 874 try { 875 cal.getDisplayName(Calendar.MONTH, -1, Locale.US); 876 fail("Should throw IllegalArgumentException"); 877 } catch (IllegalArgumentException e) { 878 // expected 879 } 880 try { 881 cal.getDisplayName(Calendar.MONTH, 3, Locale.US); 882 fail("Should throw IllegalArgumentException"); 883 } catch (IllegalArgumentException e) { 884 // expected 885 } 886 try { 887 cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, null); 888 fail("Should throw NullPointerException"); 889 } catch (NullPointerException e) { 890 // expected 891 } 892 try { 893 cal.getDisplayName(-1, Calendar.SHORT, null); 894 fail("Should throw IllegalArgumentException"); 895 } catch (IllegalArgumentException e) { 896 // expected 897 } 898 try { 899 cal.getDisplayName(Calendar.MONTH, -1, null); 900 fail("Should throw IllegalArgumentException"); 901 } catch (IllegalArgumentException e) { 902 // expected 903 } 904 // in lenient mode, following cases pass 905 cal.set(Calendar.SECOND, 999); 906 cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US); 907 // test for ALL_STYLES, it is equal to use SHORT 908 for (int field = 0; field < Calendar.FIELD_COUNT; field++) { 909 for (Locale locale : locales) { 910 String result = cal.getDisplayName(field, Calendar.ALL_STYLES, 911 locale); 912 if (field == Calendar.AM_PM || field == Calendar.ERA 913 || field == Calendar.MONTH 914 || field == Calendar.DAY_OF_WEEK) { 915 assertEquals(result, cal.getDisplayName(field, 916 Calendar.SHORT, locale)); 917 } else { 918 assertNull(result); 919 } 920 } 921 } 922 923 // invalid value for an un-related field when the calendar is not 924 // lenient 925 cal.setLenient(false); 926 assertNotNull(cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, 927 Locale.US)); 928 cal.set(Calendar.SECOND, 999); 929 try { 930 cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US); 931 fail("Should throw IllegalArgumentException"); 932 } catch (IllegalArgumentException e) { 933 // expected 934 } 935 try { 936 cal.getDisplayName(Calendar.MONTH, Calendar.ALL_STYLES, Locale.US); 937 fail("Should throw IllegalArgumentException"); 938 } catch (IllegalArgumentException e) { 939 // expected 940 } 941 } 942 943 /** 944 * {@link java.util.Calendar#getDisplayNames(int, int, Locale)} 945 * @since 1.6 946 */ 947 public void test_getDisplayNamesIILjava_util_Locale() { 948 assertEquals(0, Calendar.ALL_STYLES); 949 assertEquals(1, Calendar.SHORT); 950 assertEquals(2, Calendar.LONG); 951 952 Calendar cal = Calendar.getInstance(Locale.US); 953 954 for (int field = 0; field < Calendar.FIELD_COUNT; field++) { 955 for (Locale locale : locales) { 956 Map<String, Integer> shortResult = cal.getDisplayNames(field, 957 Calendar.SHORT, locale); 958 Map<String, Integer> longResult = cal.getDisplayNames(field, 959 Calendar.LONG, locale); 960 Map<String, Integer> allResult = cal.getDisplayNames(field, 961 Calendar.ALL_STYLES, locale); 962 DateFormatSymbols symbols = new DateFormatSymbols(locale); 963 String[] values = null; 964 switch (field) { 965 case Calendar.AM_PM: 966 case Calendar.ERA: 967 values = (field == Calendar.AM_PM) ? symbols 968 .getAmPmStrings() : symbols.getEras(); 969 assertDisplayNameMap(values, shortResult, 0); 970 assertDisplayNameMap(values, longResult, 0); 971 assertDisplayNameMap(values, allResult, 0); 972 break; 973 case Calendar.MONTH: 974 values = symbols.getShortMonths(); 975 assertDisplayNameMap(values, shortResult, 0); 976 values = symbols.getMonths(); 977 assertDisplayNameMap(values, longResult, 0); 978 assertTrue(allResult.size() >= shortResult.size()); 979 assertTrue(allResult.size() >= longResult.size()); 980 assertTrue(allResult.size() <= shortResult.size() 981 + longResult.size()); 982 break; 983 case Calendar.DAY_OF_WEEK: 984 values = symbols.getShortWeekdays(); 985 assertDisplayNameMap(values, shortResult, 1); 986 values = symbols.getWeekdays(); 987 assertDisplayNameMap(values, longResult, 1); 988 assertTrue(allResult.size() >= shortResult.size()); 989 assertTrue(allResult.size() >= longResult.size()); 990 assertTrue(allResult.size() <= shortResult.size() 991 + longResult.size()); 992 break; 993 default: 994 assertNull(shortResult); 995 assertNull(longResult); 996 assertNull(allResult); 997 } 998 } 999 } 1000 1001 cal.setLenient(true); 1002 1003 try { 1004 cal.getDisplayNames(-1, Calendar.SHORT, Locale.US); 1005 fail("Should throw IllegalArgumentException"); 1006 } catch (IllegalArgumentException e) { 1007 // expected 1008 } 1009 try { 1010 cal.getDisplayNames(Calendar.FIELD_COUNT, Calendar.LONG, Locale.US); 1011 fail("Should throw IllegalArgumentException"); 1012 } catch (IllegalArgumentException e) { 1013 // expected 1014 } 1015 try { 1016 cal.getDisplayNames(Calendar.MONTH, -1, Locale.US); 1017 fail("Should throw IllegalArgumentException"); 1018 } catch (IllegalArgumentException e) { 1019 // expected 1020 } 1021 try { 1022 cal.getDisplayNames(Calendar.MONTH, 3, Locale.US); 1023 fail("Should throw IllegalArgumentException"); 1024 } catch (IllegalArgumentException e) { 1025 // expected 1026 } 1027 try { 1028 cal.getDisplayNames(Calendar.MONTH, Calendar.SHORT, null); 1029 fail("Should throw NullPointerException"); 1030 } catch (NullPointerException e) { 1031 // expected 1032 } 1033 try { 1034 cal.getDisplayNames(-1, Calendar.SHORT, null); 1035 fail("Should throw IllegalArgumentException"); 1036 } catch (IllegalArgumentException e) { 1037 // expected 1038 } 1039 try { 1040 cal.getDisplayNames(Calendar.MONTH, -1, null); 1041 fail("Should throw IllegalArgumentException"); 1042 } catch (IllegalArgumentException e) { 1043 // expected 1044 } 1045 cal.set(Calendar.SECOND, 999); 1046 cal.getDisplayNames(Calendar.MONTH, Calendar.SHORT, Locale.US); 1047 1048 // RI fails here 1049 // invalid value for an un-related field when the calendar is not 1050 // lenient 1051 cal.setLenient(false); 1052 cal.set(Calendar.SECOND, 999); 1053 try { 1054 cal.getDisplayNames(Calendar.MONTH, Calendar.SHORT, Locale.US); 1055 fail("Should throw IllegalArgumentException"); 1056 } catch (IllegalArgumentException e) { 1057 // expected 1058 } 1059 } 1060 1061 private void assertDisplayNameMap(String[] values, 1062 Map<String, Integer> result, int shift) { 1063 List<String> trimValue = new ArrayList<String>(); 1064 for (String value : values) { 1065 if (value.trim().length() > 0) { 1066 trimValue.add(value); 1067 } 1068 } 1069 assertEquals(trimValue.size(), result.size()); 1070 for (int i = 0; i < trimValue.size(); i++) { 1071 assertEquals(i + shift, result.get(trimValue.get(i)).intValue()); 1072 } 1073 } 1074 1075 /** 1076 * {@link java.util.Calendar#getActualMaximum(int)} 1077 */ 1078 public void test_getActualMaximum_I() { 1079 Calendar c = new MockCalendar(); 1080 assertEquals("should be equal to 0", 0, c.getActualMaximum(0)); 1081 } 1082 1083 /** 1084 * {@link java.util.Calendar#getActualMinimum(int)} 1085 */ 1086 public void test_getActualMinimum_I() { 1087 Calendar c = new MockCalendar(); 1088 assertEquals("should be equal to 0", 0, c.getActualMinimum(0)); 1089 } 1090 1091 protected void setUp() { 1092 defaultLocale = Locale.getDefault(); 1093 Locale.setDefault(Locale.US); 1094 } 1095 1096 protected void tearDown() { 1097 Locale.setDefault(defaultLocale); 1098 } 1099 } 1100