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);
 74         validate(cal, "2003/10/31 DAY_OF_YEAR=1");
 75 
 76         // 2003/10 isn't the 1st week of the year.
 77         cal.clear();
 78         cal.set(YEAR, 2003);
 79         cal.set(WEEK_OF_YEAR, 1);
 80         cal.set(MONTH, OCTOBER);
 81         validate(cal, "2003/10 WEEK_OF_YEAR=1");
 82 
 83         // The 1st week of 2003 doesn't have Monday.
 84         cal.clear();
 85         cal.set(YEAR, 2003);
 86         cal.set(WEEK_OF_YEAR, 1);
 87         cal.set(DAY_OF_WEEK, MONDAY);
 88         validate(cal, "2003 WEEK_OF_YEAR=1 MONDAY.");
 89 
 90         // 2003 has 52 weeks.
 91         cal.clear();
 92         cal.set(YEAR, 2003);
 93         cal.set(WEEK_OF_YEAR, 53);
 94         cal.set(DAY_OF_WEEK, WEDNESDAY);
 95         validate(cal, "2003 WEEK_OF_YEAR=53");
 96 
 97         /*
 98          * These test cases assume incompatible behavior in Tiger as
 99          * the result of the validation bug fixes. However, it looks
100          * like we have to allow applications to set ZONE_OFFSET and
101          * DST_OFFSET values to modify the time zone offsets given by
102          * a TimeZone. The definition of non-leniency for time zone
103          * offsets is somewhat vague.  (See 6231602)
104          *
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 }