Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Calendar/FieldStateTest.java
47182 views
/*1* Copyright (c) 2003, 2016, 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.");147cal.setTime(new Date(2003-1990, OCTOBER, 31));148cal.set(DAY_OF_WEEK, SUNDAY);149cal.set(2003, OCTOBER, 31); // 2003/10/31 is Friday.150cal.set(ZONE_OFFSET, 0);151cal.set(DST_OFFSET, 0);152153// This call should change the day of week to FRIDAY since the154// selected field combination should be YEAR, MONTH and155// DAY_OF_MONTH. The other calendar fields must be normalized156// with the selected date.157cal.getTime();158cal.set(WEEK_OF_YEAR, 43);159if (!cal.checkDate(2003, OCTOBER, 24, FRIDAY)) {160errln(cal.getMessage());161}162}163164/*165* 4916815: REGRESSION: Problem with java.util.Calendar VM 1.4.2-b28166*/167public void Test4916815() {168logln("Set date to 2003/9/26 (Fri). Roll to Aug and back to Sep. "+169"Set dayofweek to Sunday which should be 2003/9/21.");170Koyomi cal = new Koyomi();171cal.clear();172// 2003/9/26 (Fri)173cal.set(2003, SEPTEMBER, 26);174// Go to August then back to September175cal.roll(MONTH, -1);176cal.roll(MONTH, +1);177Koyomi cal2 = (Koyomi) cal.clone();178cal2.getTime();179// Sunday of the week should be 2003/9/21.180cal2.set(DAY_OF_WEEK, SUNDAY);181if (!cal2.checkDate(2003, SEPTEMBER, 21, SUNDAY)) {182errln(cal2.getMessage());183}184}185186/*187* 4867075: GregorianCalendar get() calls complete() internally, should getTime() too?188*/189public void Test4867075() {190Koyomi cal = new Koyomi(Locale.US);191cal.clear();192cal.set(YEAR, 2004);193cal.set(WEEK_OF_YEAR, 1);194checkDate(cal, SUNDAY, 2003, DECEMBER, 28);195checkDate(cal, MONDAY, 2003, DECEMBER, 29);196checkDate(cal, TUESDAY, 2003, DECEMBER, 30);197checkDate(cal, WEDNESDAY, 2003, DECEMBER, 31);198checkDate(cal, THURSDAY, 2004, JANUARY, 1);199checkDate(cal, FRIDAY, 2004, JANUARY, 2);200checkDate(cal, SATURDAY, 2004, JANUARY, 3);201}202203private void checkDate(Koyomi cal, int dayOfWeek,204int expectedYear, int expectedMonth, int expectedDayOfMonth) {205cal.set(DAY_OF_WEEK, dayOfWeek);206cal.getTime();207if (!cal.checkInternalDate(expectedYear, expectedMonth, expectedDayOfMonth, dayOfWeek)) {208errln(cal.getMessage());209}210}211212static String toHexString(int x) {213return Integer.toHexString(x);214}215}216217218