Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Calendar/NonLenientTest.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 4147269 4266783 472603026* @summary Make sure that validation is adequate in non-lenient mode.27* @library /java/text/testlib28*/2930import java.util.*;3132import static java.util.Calendar.*;3334public class NonLenientTest extends IntlTest {3536public static void main(String[] args) throws Exception {37Locale reservedLocale = Locale.getDefault();38TimeZone reservedTimeZone = TimeZone.getDefault();39try {40Locale.setDefault(Locale.US);41TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));42new NonLenientTest().run(args);43} finally {44// restore the reserved locale and time zone45Locale.setDefault(reservedLocale);46TimeZone.setDefault(reservedTimeZone);47}48}4950public void TestValidationInNonLenient() {51Koyomi cal = getNonLenient();5253// 2003 isn't a leap year.54cal.set(2003, FEBRUARY, 29);55validate(cal, "2003/2/29");5657// October has only 31 days.58cal.set(2003, OCTOBER, 32);59validate(cal, "2003/10/32");6061// 2003/10/31 is Friday.62cal.set(2003, OCTOBER, 31);63cal.set(DAY_OF_WEEK, SUNDAY);64validate(cal, "2003/10/31 SUNDAY");6566// 2003/10/31 is the 304th day of the year.67cal.clear();68cal.set(DAY_OF_YEAR, 1);69cal.set(2003, OCTOBER, 31);70validate(cal, "2003/10/31 DAY_OF_YEAR=1");7172// 2003/10 isn't the 1st week of the year.73cal.clear();74cal.set(YEAR, 2003);75cal.set(WEEK_OF_YEAR, 1);76cal.set(MONTH, OCTOBER);77validate(cal, "2003/10 WEEK_OF_YEAR=1");7879// The 1st week of 2003 doesn't have Monday.80cal.clear();81cal.set(YEAR, 2003);82cal.set(WEEK_OF_YEAR, 1);83cal.set(DAY_OF_WEEK, MONDAY);84validate(cal, "2003 WEEK_OF_YEAR=1 MONDAY.");8586// 2003 has 52 weeks.87cal.clear();88cal.set(YEAR, 2003);89cal.set(WEEK_OF_YEAR, 53);90cal.set(DAY_OF_WEEK, WEDNESDAY);91validate(cal, "2003 WEEK_OF_YEAR=53");9293/*94* These test cases assume incompatible behavior in Tiger as95* the result of the validation bug fixes. However, it looks96* like we have to allow applications to set ZONE_OFFSET and97* DST_OFFSET values to modify the time zone offsets given by98* a TimeZone. The definition of non-leniency for time zone99* offsets is somewhat vague. (See 6231602)100*101* The following test cases are now disabled.102103// America/Los_Angeles is GMT-08:00104cal.clear();105cal.set(2003, OCTOBER, 31);106cal.set(ZONE_OFFSET, 0);107validate(cal, "ZONE_OFFSET=0:00 in America/Los_Angeles");108109// 2003/10/31 shouldn't be in DST.110cal.clear();111cal.set(2003, OCTOBER, 31);112cal.set(DST_OFFSET, 60*60*1000);113validate(cal, "2003/10/31 DST_OFFSET=1:00 in America/Los_Angeles");114115*/116}117118/**119* 4266783: java.util.GregorianCalendar: incorrect validation in non-lenient120*/121public void Test4266783() {122Koyomi cal = getNonLenient();123// 2003/1 has up to 5 weeks.124cal.set(YEAR, 2003);125cal.set(MONTH, JANUARY);126cal.set(WEEK_OF_MONTH, 6);127cal.set(DAY_OF_WEEK, SUNDAY);128validate(cal, "6th Sunday in Jan 2003");129}130131132/**133* 4726030: GregorianCalendar doesn't check invalid dates in non-lenient134*/135public void Test4726030() {136Koyomi cal = getNonLenient();137// Default year is 1970 in GregorianCalendar which isn't a leap year.138cal.set(MONTH, FEBRUARY);139cal.set(DAY_OF_MONTH, 29);140validate(cal, "2/29 in the default year 1970");141}142143/**144* 4147269: java.util.GregorianCalendar.computeTime() works wrong when lenient is false145*/146public void Test4147269() {147Koyomi calendar = getNonLenient();148Date date = (new GregorianCalendar(1996,0,3)).getTime();149150for (int field = 0; field < Calendar.FIELD_COUNT; field++) {151calendar.setTime(date);152int max = calendar.getActualMaximum(field);153int value = max+1;154calendar.set(field, value);155try {156calendar.computeTime(); // call method under test157errln("Test failed with field " + calendar.getFieldName(field)158+ "\n\tdate before: " + date159+ "\n\tdate after: " + calendar.getTime()160+ "\n\tvalue: " + value + " (max = " + max +")");161} catch (IllegalArgumentException e) {162}163}164165for (int field = 0; field < Calendar.FIELD_COUNT; field++) {166calendar.setTime(date);167int min = calendar.getActualMinimum(field);168int value = min-1;169calendar.set(field, value);170try {171calendar.computeTime(); // call method under test172errln("Test failed with field " + calendar.getFieldName(field)173+ "\n\tdate before: " + date174+ "\n\tdate after: " + calendar.getTime()175+ "\n\tvalue: " + value + " (min = " + min +")");176} catch (IllegalArgumentException e) {177}178}179}180181void validate(Koyomi cal, String desc) {182int[] originalFields = cal.getFields();183int setFields = cal.getSetStateFields();184185try {186cal.complete();187errln(desc + " should throw IllegalArgumentException in non-lenient.");188} catch (IllegalArgumentException e) {189}190191// The code below will be executed with the -nothrow option192193// In non-lenient, calendar field values that have beeb set by194// user shouldn't be modified.195int[] afterFields = cal.getFields();196for (int i = 0; i < Calendar.FIELD_COUNT; i++) {197if (cal.isSet(i) && originalFields[i] != afterFields[i]) {198errln(" complete() modified fields[" + cal.getFieldName(i) + "] got "199+ afterFields[i] + ", expected " + originalFields[i]);200}201}202// In non-lenient, set state of fields shouldn't be modified.203int afterSetFields = cal.getSetStateFields();204if (setFields != afterSetFields) {205errln(" complate() modified set states: before 0x" + toHex(setFields)206+ ", after 0x"+ toHex(afterSetFields));207}208}209210static Koyomi getNonLenient() {211Koyomi cal = new Koyomi();212cal.clear();213cal.setLenient(false);214return cal;215}216217static String toHex(int x) {218return Integer.toHexString(x);219}220}221222223