Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Calendar/GregorianCutoverTest.java
46985 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 4359204 4928615 4743587 4956232 6459836 654995326* @library /java/text/testlib27* @build Koyomi28* @run main GregorianCutoverTest29* @summary Unit tests related to the Gregorian cutover support.30*/3132import java.util.Date;33import java.util.Locale;34import java.util.TimeZone;3536import static java.util.GregorianCalendar.*;3738public class GregorianCutoverTest extends IntlTest {3940public static void main(String[] args) throws Exception {41TimeZone tz = TimeZone.getDefault();42Locale lc = Locale.getDefault();43try {44TimeZone.setDefault(TimeZone.getTimeZone("GMT"));45Locale.setDefault(Locale.US);4647new GregorianCutoverTest().run(args);48} finally {49TimeZone.setDefault(tz);50Locale.setDefault(lc);51}52}5354/**55* 4359204: GregorianCalendar.get(cal.DAY_OF_YEAR) is inconsistent for year 158256*/57public void Test4359204() {58Koyomi cal = new Koyomi();5960cal.set(1582, JANUARY, 1);61checkContinuity(cal, DAY_OF_YEAR);62checkContinuity(cal, WEEK_OF_YEAR);63cal.set(1582, OCTOBER, 1);64checkContinuity(cal, WEEK_OF_MONTH);6566// JCK tests the cutover date 1970-1-1 (Epoch)67cal.setGregorianChange(new Date(0));68cal.set(1969, JANUARY, 1);69checkContinuity(cal, DAY_OF_YEAR);70checkContinuity(cal, WEEK_OF_YEAR);71cal.set(1969, DECEMBER, 1);72checkContinuity(cal, WEEK_OF_MONTH);73cal.set(1970, JANUARY, 1);74checkContinuity(cal, DAY_OF_YEAR);75checkContinuity(cal, WEEK_OF_YEAR);7677// Use large date (year >= 50000)78cal.setGregorianChange(new Date(50000-1900, JANUARY, 20));79cal.set(49998, JANUARY, 1);80checkContinuity(cal, DAY_OF_YEAR);81checkContinuity(cal, WEEK_OF_YEAR);82cal.set(49999, JANUARY, 1);83checkContinuity(cal, DAY_OF_YEAR);84checkContinuity(cal, WEEK_OF_YEAR);85cal.set(50000, JANUARY, 20);86checkContinuity(cal, DAY_OF_YEAR);87checkContinuity(cal, WEEK_OF_YEAR);8889// Handling of "overlapping" dates may still be incorrect as90// of 1.5. Also, there's no way to disambiguate "overlapping"91// dates.92// millis=-112033929600000: date=-1581-10-15T00:00:00.000Z93cal.setGregorianChange(new Date(-112033929600000L));94cal.set(ERA, AD);95cal.set(-1581, JANUARY, 1);96// The year should have 379 days.97checkContinuity(cal, DAY_OF_YEAR);98checkContinuity(cal, WEEK_OF_YEAR);99100logln("Default cutover");101cal = new Koyomi();102cal.set(1582, OCTOBER, 1);103logln(" roll --DAY_OF_MONTH from 1582/10/01");104cal.roll(DAY_OF_MONTH, -1);105if (!cal.checkDate(1582, OCTOBER, 31)) {106errln(cal.getMessage());107}108logln(" roll DAY_OF_MONTH+10 from 1582/10/31");109cal.roll(DAY_OF_MONTH, +10);110if (!cal.checkDate(1582, OCTOBER, 20)) {111errln(cal.getMessage());112}113logln(" roll DAY_OF_MONTH-10 from 1582/10/20");114cal.roll(DAY_OF_MONTH, -10);115if (!cal.checkDate(1582, OCTOBER, 31)) {116errln(cal.getMessage());117}118logln(" roll back one day further");119cal.roll(DAY_OF_MONTH, +1);120if (!cal.checkDate(1582, OCTOBER, 1)) {121errln(cal.getMessage());122}123124// should handle the gap between 1969/12/22 (Julian) to 1970/1/5 (Gregorian)125logln("Cutover date is 1970/1/5");126cal.setGregorianChange(new Date(1970-1900, JANUARY, 5));127cal.set(ERA, AD);128cal.set(YEAR, 1970);129logln(" Set DAY_OF_YEAR to the 28th day of 1970");130cal.set(DAY_OF_YEAR, 28);131if (!cal.checkDate(1970, FEBRUARY, 1)) {132errln(cal.getMessage());133}134if (!cal.checkFieldValue(WEEK_OF_YEAR, 5)) {135errln(cal.getMessage());136}137logln(" 1969/12/22 should be the 356th day of the year.");138cal.set(1969, DECEMBER, 22);139if (!cal.checkFieldValue(DAY_OF_YEAR, 356)) {140errln(cal.getMessage());141}142logln(" Set DAY_OF_YEAR to autual maximum.");143int actualMaxDayOfYear = cal.getActualMaximum(DAY_OF_YEAR);144if (actualMaxDayOfYear != 356) {145errln("actual maximum of DAY_OF_YEAR: got " + actualMaxDayOfYear + ", expected 356");146}147cal.set(DAY_OF_YEAR, actualMaxDayOfYear);148if (!cal.checkDate(1969, DECEMBER, 22)) {149errln(cal.getMessage());150}151cal.set(1969, DECEMBER, 22);152cal.roll(DAY_OF_YEAR, +1);153logln(" Set to 1969/12/22 and roll DAY_OF_YEAR++");154if (!cal.checkDate(1969, JANUARY, 1)) {155errln(cal.getMessage());156}157logln(" 1970/1/5 should be the first day of the year.");158cal.set(1970, JANUARY, 5);159if (!cal.checkFieldValue(DAY_OF_YEAR, 1)) {160errln(cal.getMessage());161}162logln(" roll --DAY_OF_MONTH from 1970/1/5");163cal.roll(DAY_OF_MONTH, -1);164if (!cal.checkDate(1970, JANUARY, 31)) {165errln(cal.getMessage());166}167logln(" roll back one day of month");168cal.roll(DAY_OF_MONTH, +1);169if (!cal.checkDate(1970, JANUARY, 5)) {170errln(cal.getMessage());171}172173// Test "missing" dates in non-lenient.174cal = new Koyomi(); // new instance for the default cutover175cal.setLenient(false);176try {177// the next day of 1582/10/4 (Julian) is 1582/10/15 (Gregorian)178logln("1582/10/10 doesn't exit with the default cutover.");179cal.set(1582, OCTOBER, 10);180cal.getTime();181errln(" Didn't throw IllegalArgumentException in non-lenient.");182} catch (IllegalArgumentException e) {183}184}185186private void checkContinuity(Koyomi cal, int field) {187cal.getTime();188logln(Koyomi.getFieldName(field) + " starting on " + cal.toDateString());189int max = cal.getActualMaximum(field);190for (int i = 1; i <= max; i++) {191logln(i + " " + cal.toDateString());192if (!cal.checkFieldValue(field, i)) {193errln(" " + cal.toDateString() + ":\t" + cal.getMessage());194}195cal.add(field, +1);196}197}198199/**200* 4928615: GregorianCalendar returns wrong dates after setGregorianChange201*/202public void Test4928615() {203Koyomi cal = new Koyomi();204logln("Today is 2003/10/1 Gregorian.");205Date x = new Date(2003-1900, 10-1, 1);206cal.setTime(x);207208logln(" Changing the cutover date to yesterday...");209cal.setGregorianChange(new Date(x.getTime() - (24*3600*1000)));210if (!cal.checkDate(2003, OCTOBER, 1)) {211errln(" " + cal.getMessage());212}213logln(" Changing the cutover date to tomorrow...");214cal.setGregorianChange(new Date(x.getTime() + (24*3600*1000)));215if (!cal.checkDate(2003, SEPTEMBER, 18)) {216errln(" " + cal.getMessage());217}218}219220/**221* 4743587: GregorianCalendar.getLeastMaximum() returns wrong values222*/223public void Test4743587() {224Koyomi cal = new Koyomi();225Koyomi cal2 = (Koyomi) cal.clone();226logln("getLeastMaximum should handle cutover year.\n"227+" default cutover date");228if (!cal.checkLeastMaximum(DAY_OF_YEAR, 365-10)) {229errln(" " + cal.getMessage());230}231if (!cal.checkLeastMaximum(WEEK_OF_YEAR, 52-((10+6)/7))) {232errln(" " + cal.getMessage());233}234// Corrected for 4956232235if (!cal.checkLeastMaximum(DAY_OF_MONTH, 28)) {236errln(" " + cal.getMessage());237}238if (!cal.checkLeastMaximum(WEEK_OF_MONTH, 3)) {239errln(" " + cal.getMessage());240}241if (!cal.checkLeastMaximum(DAY_OF_WEEK_IN_MONTH, 3)) {242errln(" " + cal.getMessage());243}244// make sure that getLeastMaximum calls didn't affect the date245if (!cal.equals(cal2)) {246errln(" getLeastMaximum calls modified the object.");247}248if (!cal.checkGreatestMinimum(DAY_OF_MONTH, 1)) {249errln(" " + cal.getMessage());250}251252logln(" changing the date to 1582/10/20 for actual min/max tests");253cal.set(1582, OCTOBER, 20);254if (!cal.checkActualMinimum(DAY_OF_MONTH, 1)) {255errln(" " + cal.getMessage());256}257if (!cal.checkActualMaximum(DAY_OF_MONTH, 31)) {258errln(" " + cal.getMessage());259}260261cal = new Koyomi();262logln("Change the cutover date to 1970/1/5.");263cal.setGregorianChange(new Date(1970-1900, 0, 5));264if (!cal.checkLeastMaximum(DAY_OF_YEAR, 356)) {265errln(" " + cal.getMessage());266}267if (!cal.checkLeastMaximum(DAY_OF_MONTH, 22)) {268errln(" " + cal.getMessage());269}270if (!cal.checkGreatestMinimum(DAY_OF_MONTH, 5)) {271errln(" " + cal.getMessage());272}273cal.set(1970, JANUARY, 10);274if (!cal.checkActualMinimum(DAY_OF_MONTH, 5)) {275errln(" " + cal.getMessage());276}277if (!cal.checkActualMaximum(DAY_OF_MONTH, 31)) {278errln(" " + cal.getMessage());279}280}281282/**283* 6459836: (cal) GregorianCalendar set method provides wrong result284*/285public void Test6459836() {286int hour = 13865672;287Koyomi gc1 = new Koyomi();288gc1.clear();289gc1.set(1, gc1.JANUARY, 1, 0, 0, 0);290gc1.set(gc1.HOUR_OF_DAY, hour);291if (!gc1.checkDate(1582, gc1.OCTOBER, 4)) {292errln("test case 1: " + gc1.getMessage());293}294gc1.clear();295gc1.set(1, gc1.JANUARY, 1, 0, 0, 0);296gc1.set(gc1.HOUR_OF_DAY, hour + 24);297if (!gc1.checkDate(1582, gc1.OCTOBER, 15)) {298errln("test case 2: " + gc1.getMessage());299}300}301302/**303* 6549953 (cal) WEEK_OF_YEAR and DAY_OF_YEAR calculation problems around Gregorian cutover304*/305public void Test6549953() {306Koyomi cal = new Koyomi();307308cal.set(YEAR, 1582);309cal.set(WEEK_OF_YEAR, 42);310cal.set(DAY_OF_WEEK, FRIDAY);311cal.checkFieldValue(WEEK_OF_YEAR, 42);312cal.checkFieldValue(DAY_OF_WEEK, FRIDAY);313if (!cal.checkDate(1582, OCTOBER, 29)) {314errln(cal.getMessage());315}316cal.clear();317cal.set(1582, OCTOBER, 1);318cal.set(DAY_OF_YEAR, 292);319if (!cal.checkDate(1582, OCTOBER, 29)) {320errln(cal.getMessage());321}322}323}324325326