Path: blob/master/test/jdk/java/util/Calendar/FieldStateTest.java
41149 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4860664 4916815 486707526* @library /java/text/testlib27* @build Koyomi28* @run main FieldStateTest29* @summary Unit tests for internal fields states.30*/3132import java.util.Date;33import java.util.Locale;34import java.util.TimeZone;3536import static java.util.Calendar.*;3738public class FieldStateTest extends IntlTest {3940public static void main(String[] args) throws Exception {41Locale reservedLocale = Locale.getDefault();42TimeZone reservedTimeZone = TimeZone.getDefault();43try {44TimeZone.setDefault(TimeZone.getTimeZone("GMT"));45Locale.setDefault(Locale.US);4647new FieldStateTest().run(args);48} finally {49// restore the reserved locale and time zone50Locale.setDefault(reservedLocale);51TimeZone.setDefault(reservedTimeZone);52}53}5455public void TestFieldState() {56Koyomi cal = new Koyomi();57logln("Right after instantialtion:");58if (!cal.checkAllSet()) {59errln(cal.getMessage());60}6162logln("Set date to 2003/10/31 after the instantiation:");63cal.set(2003, OCTOBER, 31);64// let cal calculate the time65cal.getTime();66// At this point, all fields have to be recalculated and67// happen to have the set-state from the instantiation. The68// three fields should have "externally set" and the rest of69// the fields have "computed". But we can't distinguish them70// outside the package.71if (!cal.checkAllSet()) {72errln(cal.getMessage());73}74// Make sure that the correct date was produced.75if (!cal.checkInternalDate(2003, OCTOBER, 31, FRIDAY)) {76errln(cal.getMessage());77}7879logln("Change to Monday of the week, which is 2003/10/27:");80cal.set(DAY_OF_WEEK, MONDAY);81cal.getTime();82if (!cal.checkDate(2003, OCTOBER, 27)) {83errln(cal.getMessage());84}8586// The same operation didn't work after calling clear() before87// 1.5 because the set-state was just depends on its previous88// operations. After the instantiation, all the fields are set89// to "computed". But after calling clear(), the state becomes90// "unset".91logln("Set to 2003/10/31 after clear():");92cal.clear();93cal.set(2003, OCTOBER, 31);94cal.getTime();95cal.set(DAY_OF_WEEK, MONDAY);96if (!cal.checkDate(2003, OCTOBER, 27, MONDAY)) {97errln(cal.getMessage());98}99100logln("Set to 2003/10/31 after clear(), then to the 51st week of year (12/19):");101cal.clear();102cal.set(2003, OCTOBER, 31);103cal.getTime();104cal.set(WEEK_OF_YEAR, 51);105if (!cal.checkFieldValue(WEEK_OF_YEAR, 51)) {106errln(cal.getMessage());107}108if (!cal.checkDate(2003, DECEMBER, 19, FRIDAY)) {109errln(cal.getMessage());110}111112logln("Set to 2003/10 Mon of 4th week (10/20: 43rd week of year, 293rd day):");113cal.clear();114cal.set(YEAR, 2003);115cal.set(MONTH, OCTOBER);116cal.set(DAY_OF_WEEK, MONDAY);117cal.set(WEEK_OF_MONTH, 4);118cal.getTime();119if (!cal.checkFieldValue(DAY_OF_MONTH, 20)) {120errln(cal.getMessage());121}122if (!cal.checkFieldValue(DAY_OF_YEAR, 293)) {123errln(cal.getMessage());124}125if (!cal.checkFieldValue(WEEK_OF_YEAR, 43)) {126errln(cal.getMessage());127}128129logln("Set to 2003/10 Mon of 43rd week of year (10/20: 4th week of month, 293rd day):");130cal.clear();131cal.set(YEAR, 2003);132cal.set(DAY_OF_WEEK, MONDAY);133cal.set(WEEK_OF_YEAR, 43);134cal.getTime();135if (!cal.checkDate(2003, OCTOBER, 20, MONDAY)) {136errln(cal.getMessage());137}138if (!cal.checkFieldValue(WEEK_OF_MONTH, 4)) {139errln(cal.getMessage());140}141if (!cal.checkFieldValue(DAY_OF_YEAR, 293)) {142errln(cal.getMessage());143}144145logln("Set day of week to SUNDAY and date to 2003/10/31. "146+ "Then, getTime and set week of year to 43.");147148@SuppressWarnings("deprecation")149Date d = new Date(2003 - 1900, OCTOBER, 31);150cal.setTime(d);151cal.set(DAY_OF_WEEK, SUNDAY);152cal.set(2003, OCTOBER, 31); // 2003/10/31 is Friday.153cal.set(ZONE_OFFSET, 0);154cal.set(DST_OFFSET, 0);155156// This call should change the day of week to FRIDAY since the157// selected field combination should be YEAR, MONTH and158// DAY_OF_MONTH. The other calendar fields must be normalized159// with the selected date.160cal.getTime();161cal.set(WEEK_OF_YEAR, 43);162if (!cal.checkDate(2003, OCTOBER, 24, FRIDAY)) {163errln(cal.getMessage());164}165}166167/*168* 4916815: REGRESSION: Problem with java.util.Calendar VM 1.4.2-b28169*/170public void Test4916815() {171logln("Set date to 2003/9/26 (Fri). Roll to Aug and back to Sep. "172+ "Set dayofweek to Sunday which should be 2003/9/21.");173Koyomi cal = new Koyomi();174cal.clear();175// 2003/9/26 (Fri)176cal.set(2003, SEPTEMBER, 26);177// Go to August then back to September178cal.roll(MONTH, -1);179cal.roll(MONTH, +1);180Koyomi cal2 = (Koyomi) cal.clone();181cal2.getTime();182// Sunday of the week should be 2003/9/21.183cal2.set(DAY_OF_WEEK, SUNDAY);184if (!cal2.checkDate(2003, SEPTEMBER, 21, SUNDAY)) {185errln(cal2.getMessage());186}187}188189/*190* 4867075: GregorianCalendar get() calls complete() internally, should getTime() too?191*/192public void Test4867075() {193Koyomi cal = new Koyomi(Locale.US);194cal.clear();195cal.set(YEAR, 2004);196cal.set(WEEK_OF_YEAR, 1);197checkDate(cal, SUNDAY, 2003, DECEMBER, 28);198checkDate(cal, MONDAY, 2003, DECEMBER, 29);199checkDate(cal, TUESDAY, 2003, DECEMBER, 30);200checkDate(cal, WEDNESDAY, 2003, DECEMBER, 31);201checkDate(cal, THURSDAY, 2004, JANUARY, 1);202checkDate(cal, FRIDAY, 2004, JANUARY, 2);203checkDate(cal, SATURDAY, 2004, JANUARY, 3);204}205206private void checkDate(Koyomi cal, int dayOfWeek,207int expectedYear, int expectedMonth, int expectedDayOfMonth) {208cal.set(DAY_OF_WEEK, dayOfWeek);209cal.getTime();210if (!cal.checkInternalDate(expectedYear, expectedMonth, expectedDayOfMonth, dayOfWeek)) {211errln(cal.getMessage());212}213}214215static String toHexString(int x) {216return Integer.toHexString(x);217}218}219220221