< prev index next >

test/jdk/java/util/Calendar/NonLenientTest.java

Print this page

  1 /*
  2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 4147269 4266783 4726030
 27  * @summary Make sure that validation is adequate in non-lenient mode.
 28  * @library /java/text/testlib
 29  */
 30 
 31 import java.util.Date;
 32 import java.util.GregorianCalendar;
 33 import java.util.Locale;
 34 import java.util.TimeZone;
 35 
 36 import static java.util.Calendar.*;
 37 
 38 public class NonLenientTest extends IntlTest {

 39 
 40     public static void main(String[] args) throws Exception {
 41         Locale reservedLocale = Locale.getDefault();
 42         TimeZone reservedTimeZone = TimeZone.getDefault();
 43         try {
 44             Locale.setDefault(Locale.US);
 45             TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
 46             new NonLenientTest().run(args);
 47         } finally {
 48             // restore the reserved locale and time zone
 49             Locale.setDefault(reservedLocale);
 50             TimeZone.setDefault(reservedTimeZone);
 51         }
 52     }
 53 


 54     public void TestValidationInNonLenient() {
 55         Koyomi cal = getNonLenient();
 56 
 57         // 2003 isn't a leap year.
 58         cal.set(2003, FEBRUARY, 29);
 59         validate(cal, "2003/2/29");
 60 
 61         // October has only 31 days.
 62         cal.set(2003, OCTOBER, 32);
 63         validate(cal, "2003/10/32");
 64 
 65         // 2003/10/31 is Friday.
 66         cal.set(2003, OCTOBER, 31);
 67         cal.set(DAY_OF_WEEK, SUNDAY);
 68         validate(cal, "2003/10/31 SUNDAY");
 69 
 70         // 2003/10/31 is the 304th day of the year.
 71         cal.clear();
 72         cal.set(DAY_OF_YEAR, 1);
 73         cal.set(2003, OCTOBER, 31);

105          * The following test cases are now disabled.
106 
107         // America/Los_Angeles is GMT-08:00
108         cal.clear();
109         cal.set(2003, OCTOBER, 31);
110         cal.set(ZONE_OFFSET, 0);
111         validate(cal, "ZONE_OFFSET=0:00 in America/Los_Angeles");
112 
113         // 2003/10/31 shouldn't be in DST.
114         cal.clear();
115         cal.set(2003, OCTOBER, 31);
116         cal.set(DST_OFFSET, 60*60*1000);
117         validate(cal, "2003/10/31 DST_OFFSET=1:00 in America/Los_Angeles");
118 
119         */
120     }
121 
122     /**
123      * 4266783: java.util.GregorianCalendar: incorrect validation in non-lenient
124      */

125     public void Test4266783() {
126         Koyomi cal = getNonLenient();
127         // 2003/1 has up to 5 weeks.
128         cal.set(YEAR, 2003);
129         cal.set(MONTH, JANUARY);
130         cal.set(WEEK_OF_MONTH, 6);
131         cal.set(DAY_OF_WEEK, SUNDAY);
132         validate(cal, "6th Sunday in Jan 2003");
133     }
134 
135     /**
136      * 4726030: GregorianCalendar doesn't check invalid dates in non-lenient
137      */

138     public void Test4726030() {
139         Koyomi cal = getNonLenient();
140         // Default year is 1970 in GregorianCalendar which isn't a leap year.
141         cal.set(MONTH, FEBRUARY);
142         cal.set(DAY_OF_MONTH, 29);
143         validate(cal, "2/29 in the default year 1970");
144     }
145 
146     /**
147      * 4147269: java.util.GregorianCalendar.computeTime() works wrong when lenient is false
148      */

149     public void Test4147269() {
150         Koyomi calendar = getNonLenient();
151         Date date = (new GregorianCalendar(1996, 0, 3)).getTime();
152 
153         for (int field = 0; field < FIELD_COUNT; field++) {
154             calendar.setTime(date);
155             int max = calendar.getActualMaximum(field);
156             int value = max + 1;
157             calendar.set(field, value);
158             try {
159                 calendar.computeTime(); // call method under test
160                 errln("Test failed with field " + Koyomi.getFieldName(field)
161                         + "\n\tdate before:  " + date
162                         + "\n\tdate after:   " + calendar.getTime()
163                         + "\n\tvalue: " + value + "  (max = " + max + ")");
164             } catch (IllegalArgumentException e) {
165             }
166         }
167 
168         for (int field = 0; field < FIELD_COUNT; field++) {
169             calendar.setTime(date);
170             int min = calendar.getActualMinimum(field);
171             int value = min - 1;
172             calendar.set(field, value);
173             try {
174                 calendar.computeTime(); // call method under test
175                 errln("Test failed with field " + Koyomi.getFieldName(field)
176                         + "\n\tdate before:  " + date
177                         + "\n\tdate after:   " + calendar.getTime()
178                         + "\n\tvalue: " + value + "  (min = " + min + ")");
179             } catch (IllegalArgumentException e) {
180             }
181         }
182     }
183 
184     void validate(Koyomi cal, String desc) {
185         int[] originalFields = cal.getFields();
186         int setFields = cal.getSetStateFields();
187 
188         try {
189             cal.complete();
190             errln(desc + " should throw IllegalArgumentException in non-lenient.");
191         } catch (IllegalArgumentException e) {
192         }
193 
194         // The code below will be executed with the -nothrow option
195 
196         // In non-lenient, calendar field values that have beeb set by
197         // user shouldn't be modified.
198         int[] afterFields = cal.getFields();
199         for (int i = 0; i < FIELD_COUNT; i++) {
200             if (cal.isSet(i) && originalFields[i] != afterFields[i]) {
201                 errln("    complete() modified fields[" + Koyomi.getFieldName(i) + "] got "
202                         + afterFields[i] + ", expected " + originalFields[i]);
203             }
204         }
205         // In non-lenient, set state of fields shouldn't be modified.
206         int afterSetFields = cal.getSetStateFields();
207         if (setFields != afterSetFields) {
208             errln("    complate() modified set states: before 0x" + toHex(setFields)
209                     + ", after 0x" + toHex(afterSetFields));
210         }
211     }
212 
213     static Koyomi getNonLenient() {
214         Koyomi cal = new Koyomi();
215         cal.clear();
216         cal.setLenient(false);
217         return cal;
218     }
219 
220     static String toHex(int x) {
221         return Integer.toHexString(x);
222     }
223 }

  1 /*
  2  * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 4147269 4266783 4726030
 27  * @summary Make sure that validation is adequate in non-lenient mode.
 28  * @run junit/othervm NonLenientTest
 29  */
 30 
 31 import java.util.Date;
 32 import java.util.GregorianCalendar;
 33 import java.util.Locale;
 34 import java.util.TimeZone;
 35 
 36 import static java.util.Calendar.*;
 37 
 38 import org.junit.jupiter.api.Test;
 39 import org.junit.jupiter.api.BeforeAll;
 40 
 41 import static org.junit.jupiter.api.Assertions.fail;
 42 
 43 public class NonLenientTest {
 44 
 45     // Change JVM default Locale and TimeZone
 46     @BeforeAll
 47     static void initAll() {
 48         Locale.setDefault(Locale.US);
 49         TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));



 50     }
 51 
 52 
 53     @Test
 54     public void TestValidationInNonLenient() {
 55         Koyomi cal = getNonLenient();
 56 
 57         // 2003 isn't a leap year.
 58         cal.set(2003, FEBRUARY, 29);
 59         validate(cal, "2003/2/29");
 60 
 61         // October has only 31 days.
 62         cal.set(2003, OCTOBER, 32);
 63         validate(cal, "2003/10/32");
 64 
 65         // 2003/10/31 is Friday.
 66         cal.set(2003, OCTOBER, 31);
 67         cal.set(DAY_OF_WEEK, SUNDAY);
 68         validate(cal, "2003/10/31 SUNDAY");
 69 
 70         // 2003/10/31 is the 304th day of the year.
 71         cal.clear();
 72         cal.set(DAY_OF_YEAR, 1);
 73         cal.set(2003, OCTOBER, 31);

105          * The following test cases are now disabled.
106 
107         // America/Los_Angeles is GMT-08:00
108         cal.clear();
109         cal.set(2003, OCTOBER, 31);
110         cal.set(ZONE_OFFSET, 0);
111         validate(cal, "ZONE_OFFSET=0:00 in America/Los_Angeles");
112 
113         // 2003/10/31 shouldn't be in DST.
114         cal.clear();
115         cal.set(2003, OCTOBER, 31);
116         cal.set(DST_OFFSET, 60*60*1000);
117         validate(cal, "2003/10/31 DST_OFFSET=1:00 in America/Los_Angeles");
118 
119         */
120     }
121 
122     /**
123      * 4266783: java.util.GregorianCalendar: incorrect validation in non-lenient
124      */
125     @Test
126     public void Test4266783() {
127         Koyomi cal = getNonLenient();
128         // 2003/1 has up to 5 weeks.
129         cal.set(YEAR, 2003);
130         cal.set(MONTH, JANUARY);
131         cal.set(WEEK_OF_MONTH, 6);
132         cal.set(DAY_OF_WEEK, SUNDAY);
133         validate(cal, "6th Sunday in Jan 2003");
134     }
135 
136     /**
137      * 4726030: GregorianCalendar doesn't check invalid dates in non-lenient
138      */
139     @Test
140     public void Test4726030() {
141         Koyomi cal = getNonLenient();
142         // Default year is 1970 in GregorianCalendar which isn't a leap year.
143         cal.set(MONTH, FEBRUARY);
144         cal.set(DAY_OF_MONTH, 29);
145         validate(cal, "2/29 in the default year 1970");
146     }
147 
148     /**
149      * 4147269: java.util.GregorianCalendar.computeTime() works wrong when lenient is false
150      */
151     @Test
152     public void Test4147269() {
153         Koyomi calendar = getNonLenient();
154         Date date = (new GregorianCalendar(1996, 0, 3)).getTime();
155 
156         for (int field = 0; field < FIELD_COUNT; field++) {
157             calendar.setTime(date);
158             int max = calendar.getActualMaximum(field);
159             int value = max + 1;
160             calendar.set(field, value);
161             try {
162                 calendar.computeTime(); // call method under test
163                 fail("Test failed with field " + Koyomi.getFieldName(field)
164                         + "\n\tdate before:  " + date
165                         + "\n\tdate after:   " + calendar.getTime()
166                         + "\n\tvalue: " + value + "  (max = " + max + ")");
167             } catch (IllegalArgumentException e) {
168             }
169         }
170 
171         for (int field = 0; field < FIELD_COUNT; field++) {
172             calendar.setTime(date);
173             int min = calendar.getActualMinimum(field);
174             int value = min - 1;
175             calendar.set(field, value);
176             try {
177                 calendar.computeTime(); // call method under test
178                 fail("Test failed with field " + Koyomi.getFieldName(field)
179                         + "\n\tdate before:  " + date
180                         + "\n\tdate after:   " + calendar.getTime()
181                         + "\n\tvalue: " + value + "  (min = " + min + ")");
182             } catch (IllegalArgumentException e) {
183             }
184         }
185     }
186 
187     void validate(Koyomi cal, String desc) {
188         int[] originalFields = cal.getFields();
189         int setFields = cal.getSetStateFields();
190 
191         try {
192             cal.complete();
193             fail(desc + " should throw IllegalArgumentException in non-lenient.");
194         } catch (IllegalArgumentException e) {
195         }
196 
197         // The code below will be executed with the -nothrow option
198 
199         // In non-lenient, calendar field values that have beeb set by
200         // user shouldn't be modified.
201         int[] afterFields = cal.getFields();
202         for (int i = 0; i < FIELD_COUNT; i++) {
203             if (cal.isSet(i) && originalFields[i] != afterFields[i]) {
204                 fail("    complete() modified fields[" + Koyomi.getFieldName(i) + "] got "
205                         + afterFields[i] + ", expected " + originalFields[i]);
206             }
207         }
208         // In non-lenient, set state of fields shouldn't be modified.
209         int afterSetFields = cal.getSetStateFields();
210         if (setFields != afterSetFields) {
211             fail("    complate() modified set states: before 0x" + toHex(setFields)
212                     + ", after 0x" + toHex(afterSetFields));
213         }
214     }
215 
216     static Koyomi getNonLenient() {
217         Koyomi cal = new Koyomi();
218         cal.clear();
219         cal.setLenient(false);
220         return cal;
221     }
222 
223     static String toHex(int x) {
224         return Integer.toHexString(x);
225     }
226 }
< prev index next >