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);
 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     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 }