Path: blob/master/test/jdk/java/util/Calendar/NonLenientTest.java
41149 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.Date;31import java.util.GregorianCalendar;32import java.util.Locale;33import java.util.TimeZone;3435import static java.util.Calendar.*;3637public class NonLenientTest extends IntlTest {3839public static void main(String[] args) throws Exception {40Locale reservedLocale = Locale.getDefault();41TimeZone reservedTimeZone = TimeZone.getDefault();42try {43Locale.setDefault(Locale.US);44TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));45new NonLenientTest().run(args);46} finally {47// restore the reserved locale and time zone48Locale.setDefault(reservedLocale);49TimeZone.setDefault(reservedTimeZone);50}51}5253public void TestValidationInNonLenient() {54Koyomi cal = getNonLenient();5556// 2003 isn't a leap year.57cal.set(2003, FEBRUARY, 29);58validate(cal, "2003/2/29");5960// October has only 31 days.61cal.set(2003, OCTOBER, 32);62validate(cal, "2003/10/32");6364// 2003/10/31 is Friday.65cal.set(2003, OCTOBER, 31);66cal.set(DAY_OF_WEEK, SUNDAY);67validate(cal, "2003/10/31 SUNDAY");6869// 2003/10/31 is the 304th day of the year.70cal.clear();71cal.set(DAY_OF_YEAR, 1);72cal.set(2003, OCTOBER, 31);73validate(cal, "2003/10/31 DAY_OF_YEAR=1");7475// 2003/10 isn't the 1st week of the year.76cal.clear();77cal.set(YEAR, 2003);78cal.set(WEEK_OF_YEAR, 1);79cal.set(MONTH, OCTOBER);80validate(cal, "2003/10 WEEK_OF_YEAR=1");8182// The 1st week of 2003 doesn't have Monday.83cal.clear();84cal.set(YEAR, 2003);85cal.set(WEEK_OF_YEAR, 1);86cal.set(DAY_OF_WEEK, MONDAY);87validate(cal, "2003 WEEK_OF_YEAR=1 MONDAY.");8889// 2003 has 52 weeks.90cal.clear();91cal.set(YEAR, 2003);92cal.set(WEEK_OF_YEAR, 53);93cal.set(DAY_OF_WEEK, WEDNESDAY);94validate(cal, "2003 WEEK_OF_YEAR=53");9596/*97* These test cases assume incompatible behavior in Tiger as98* the result of the validation bug fixes. However, it looks99* like we have to allow applications to set ZONE_OFFSET and100* DST_OFFSET values to modify the time zone offsets given by101* a TimeZone. The definition of non-leniency for time zone102* offsets is somewhat vague. (See 6231602)103*104* The following test cases are now disabled.105106// America/Los_Angeles is GMT-08:00107cal.clear();108cal.set(2003, OCTOBER, 31);109cal.set(ZONE_OFFSET, 0);110validate(cal, "ZONE_OFFSET=0:00 in America/Los_Angeles");111112// 2003/10/31 shouldn't be in DST.113cal.clear();114cal.set(2003, OCTOBER, 31);115cal.set(DST_OFFSET, 60*60*1000);116validate(cal, "2003/10/31 DST_OFFSET=1:00 in America/Los_Angeles");117118*/119}120121/**122* 4266783: java.util.GregorianCalendar: incorrect validation in non-lenient123*/124public void Test4266783() {125Koyomi cal = getNonLenient();126// 2003/1 has up to 5 weeks.127cal.set(YEAR, 2003);128cal.set(MONTH, JANUARY);129cal.set(WEEK_OF_MONTH, 6);130cal.set(DAY_OF_WEEK, SUNDAY);131validate(cal, "6th Sunday in Jan 2003");132}133134/**135* 4726030: GregorianCalendar doesn't check invalid dates in non-lenient136*/137public void Test4726030() {138Koyomi cal = getNonLenient();139// Default year is 1970 in GregorianCalendar which isn't a leap year.140cal.set(MONTH, FEBRUARY);141cal.set(DAY_OF_MONTH, 29);142validate(cal, "2/29 in the default year 1970");143}144145/**146* 4147269: java.util.GregorianCalendar.computeTime() works wrong when lenient is false147*/148public void Test4147269() {149Koyomi calendar = getNonLenient();150Date date = (new GregorianCalendar(1996, 0, 3)).getTime();151152for (int field = 0; field < FIELD_COUNT; field++) {153calendar.setTime(date);154int max = calendar.getActualMaximum(field);155int value = max + 1;156calendar.set(field, value);157try {158calendar.computeTime(); // call method under test159errln("Test failed with field " + Koyomi.getFieldName(field)160+ "\n\tdate before: " + date161+ "\n\tdate after: " + calendar.getTime()162+ "\n\tvalue: " + value + " (max = " + max + ")");163} catch (IllegalArgumentException e) {164}165}166167for (int field = 0; field < FIELD_COUNT; field++) {168calendar.setTime(date);169int min = calendar.getActualMinimum(field);170int value = min - 1;171calendar.set(field, value);172try {173calendar.computeTime(); // call method under test174errln("Test failed with field " + Koyomi.getFieldName(field)175+ "\n\tdate before: " + date176+ "\n\tdate after: " + calendar.getTime()177+ "\n\tvalue: " + value + " (min = " + min + ")");178} catch (IllegalArgumentException e) {179}180}181}182183void validate(Koyomi cal, String desc) {184int[] originalFields = cal.getFields();185int setFields = cal.getSetStateFields();186187try {188cal.complete();189errln(desc + " should throw IllegalArgumentException in non-lenient.");190} catch (IllegalArgumentException e) {191}192193// The code below will be executed with the -nothrow option194195// In non-lenient, calendar field values that have beeb set by196// user shouldn't be modified.197int[] afterFields = cal.getFields();198for (int i = 0; i < FIELD_COUNT; i++) {199if (cal.isSet(i) && originalFields[i] != afterFields[i]) {200errln(" complete() modified fields[" + Koyomi.getFieldName(i) + "] got "201+ afterFields[i] + ", expected " + originalFields[i]);202}203}204// In non-lenient, set state of fields shouldn't be modified.205int afterSetFields = cal.getSetStateFields();206if (setFields != afterSetFields) {207errln(" complate() modified set states: before 0x" + toHex(setFields)208+ ", after 0x" + toHex(afterSetFields));209}210}211212static Koyomi getNonLenient() {213Koyomi cal = new Koyomi();214cal.clear();215cal.setLenient(false);216return cal;217}218219static String toHex(int x) {220return Integer.toHexString(x);221}222}223224225