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 4359204 4928615 4743587 4956232 6459836 6549953
 27  * @run junit/othervm GregorianCutoverTest
 28  * @summary Unit tests related to the Gregorian cutover support.
 29  */
 30 
 31 import java.util.Date;
 32 import java.util.Locale;
 33 import java.util.TimeZone;
 34 
 35 import static java.util.GregorianCalendar.*;
 36 
 37 import org.junit.jupiter.api.Test;
 38 import org.junit.jupiter.api.BeforeAll;
 39 
 40 import static org.junit.jupiter.api.Assertions.fail;
 41 
 42 public class GregorianCutoverTest {
 43 
 44     // Change JVM default Locale and TimeZone
 45     @BeforeAll
 46     static void initAll() {
 47         Locale.setDefault(Locale.US);
 48         TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
 49     }
 50 
 51 
 52     /**
 53      * 4359204: GregorianCalendar.get(cal.DAY_OF_YEAR) is inconsistent for year 1582
 54      */
 55     @Test
 56     public void Test4359204() {
 57         Koyomi cal = new Koyomi();
 58 
 59         cal.set(1582, JANUARY, 1);
 60         checkContinuity(cal, DAY_OF_YEAR);
 61         checkContinuity(cal, WEEK_OF_YEAR);
 62         cal.set(1582, OCTOBER, 1);
 63         checkContinuity(cal, WEEK_OF_MONTH);
 64 
 65         // JCK tests the cutover date 1970-1-1 (Epoch)
 66         cal.setGregorianChange(new Date(0));
 67         cal.set(1969, JANUARY, 1);
 68         checkContinuity(cal, DAY_OF_YEAR);
 69         checkContinuity(cal, WEEK_OF_YEAR);
 70         cal.set(1969, DECEMBER, 1);
 71         checkContinuity(cal, WEEK_OF_MONTH);
 72         cal.set(1970, JANUARY, 1);
 73         checkContinuity(cal, DAY_OF_YEAR);
 74         checkContinuity(cal, WEEK_OF_YEAR);
 75 
 76         // Use large date (year >= 50000)
 77         @SuppressWarnings("deprecation")
 78         Date d = new Date(50000 - 1900, JANUARY, 20);
 79         cal.setGregorianChange(d);
 80         cal.set(49998, JANUARY, 1);
 81         checkContinuity(cal, DAY_OF_YEAR);
 82         checkContinuity(cal, WEEK_OF_YEAR);
 83         cal.set(49999, JANUARY, 1);
 84         checkContinuity(cal, DAY_OF_YEAR);
 85         checkContinuity(cal, WEEK_OF_YEAR);
 86         cal.set(50000, JANUARY, 20);
 87         checkContinuity(cal, DAY_OF_YEAR);
 88         checkContinuity(cal, WEEK_OF_YEAR);
 89 
 90         // Handling of "overlapping" dates may still be incorrect as
 91         // of 1.5. Also, there's no way to disambiguate "overlapping"
 92         // dates.
 93         // millis=-112033929600000: date=-1581-10-15T00:00:00.000Z
 94         cal.setGregorianChange(new Date(-112033929600000L));
 95         cal.set(ERA, AD);
 96         cal.set(-1581, JANUARY, 1);
 97         // The year should have 379 days.
 98         checkContinuity(cal, DAY_OF_YEAR);
 99         checkContinuity(cal, WEEK_OF_YEAR);
100 
101         System.out.println("Default cutover");
102         cal = new Koyomi();
103         cal.set(1582, OCTOBER, 1);
104         System.out.println("  roll --DAY_OF_MONTH from 1582/10/01");
105         cal.roll(DAY_OF_MONTH, -1);
106         if (!cal.checkDate(1582, OCTOBER, 31)) {
107             fail(cal.getMessage());
108         }
109         System.out.println("  roll DAY_OF_MONTH+10 from 1582/10/31");
110         cal.roll(DAY_OF_MONTH, +10);
111         if (!cal.checkDate(1582, OCTOBER, 20)) {
112             fail(cal.getMessage());
113         }
114         System.out.println("  roll DAY_OF_MONTH-10 from 1582/10/20");
115         cal.roll(DAY_OF_MONTH, -10);
116         if (!cal.checkDate(1582, OCTOBER, 31)) {
117             fail(cal.getMessage());
118         }
119         System.out.println("  roll back one day further");
120         cal.roll(DAY_OF_MONTH, +1);
121         if (!cal.checkDate(1582, OCTOBER, 1)) {
122             fail(cal.getMessage());
123         }
124 
125         // should handle the gap between 1969/12/22 (Julian) to 1970/1/5 (Gregorian)
126         System.out.println("Cutover date is 1970/1/5");
127         @SuppressWarnings("deprecation")
128         Date d1 = new Date(1970 - 1900, JANUARY, 5);
129         cal.setGregorianChange(d1);
130         cal.set(ERA, AD);
131         cal.set(YEAR, 1970);
132         System.out.println("  Set DAY_OF_YEAR to the 28th day of 1970");
133         cal.set(DAY_OF_YEAR, 28);
134         if (!cal.checkDate(1970, FEBRUARY, 1)) {
135             fail(cal.getMessage());
136         }
137         if (!cal.checkFieldValue(WEEK_OF_YEAR, 5)) {
138             fail(cal.getMessage());
139         }
140         System.out.println("  1969/12/22 should be the 356th day of the year.");
141         cal.set(1969, DECEMBER, 22);
142         if (!cal.checkFieldValue(DAY_OF_YEAR, 356)) {
143             fail(cal.getMessage());
144         }
145         System.out.println("  Set DAY_OF_YEAR to autual maximum.");
146         int actualMaxDayOfYear = cal.getActualMaximum(DAY_OF_YEAR);
147         if (actualMaxDayOfYear != 356) {
148             fail("actual maximum of DAY_OF_YEAR: got " + actualMaxDayOfYear + ", expected 356");
149         }
150         cal.set(DAY_OF_YEAR, actualMaxDayOfYear);
151         if (!cal.checkDate(1969, DECEMBER, 22)) {
152             fail(cal.getMessage());
153         }
154         cal.set(1969, DECEMBER, 22);
155         cal.roll(DAY_OF_YEAR, +1);
156         System.out.println("  Set to 1969/12/22 and roll DAY_OF_YEAR++");
157         if (!cal.checkDate(1969, JANUARY, 1)) {
158             fail(cal.getMessage());
159         }
160         System.out.println("  1970/1/5 should be the first day of the year.");
161         cal.set(1970, JANUARY, 5);
162         if (!cal.checkFieldValue(DAY_OF_YEAR, 1)) {
163             fail(cal.getMessage());
164         }
165         System.out.println("  roll --DAY_OF_MONTH from 1970/1/5");
166         cal.roll(DAY_OF_MONTH, -1);
167         if (!cal.checkDate(1970, JANUARY, 31)) {
168             fail(cal.getMessage());
169         }
170         System.out.println("  roll back one day of month");
171         cal.roll(DAY_OF_MONTH, +1);
172         if (!cal.checkDate(1970, JANUARY, 5)) {
173             fail(cal.getMessage());
174         }
175 
176         // Test "missing" dates in non-lenient.
177         cal = new Koyomi(); // new instance for the default cutover
178         cal.setLenient(false);
179         try {
180             // the next day of 1582/10/4 (Julian) is 1582/10/15 (Gregorian)
181             System.out.println("1582/10/10 doesn't exit with the default cutover.");
182             cal.set(1582, OCTOBER, 10);
183             cal.getTime();
184             fail("    Didn't throw IllegalArgumentException in non-lenient.");
185         } catch (IllegalArgumentException e) {
186         }
187     }
188 
189     private void checkContinuity(Koyomi cal, int field) {
190         cal.getTime();
191         System.out.println(Koyomi.getFieldName(field) + " starting on " + cal.toDateString());
192         int max = cal.getActualMaximum(field);
193         for (int i = 1; i <= max; i++) {
194             System.out.println(i + "    " + cal.toDateString());
195             if (!cal.checkFieldValue(field, i)) {
196                 fail("    " + cal.toDateString() + ":\t" + cal.getMessage());
197             }
198             cal.add(field, +1);
199         }
200     }
201 
202     /**
203      * 4928615: GregorianCalendar returns wrong dates after setGregorianChange
204      */
205     @Test
206     public void Test4928615() {
207         Koyomi cal = new Koyomi();
208         System.out.println("Today is 2003/10/1 Gregorian.");
209         @SuppressWarnings("deprecation")
210         Date x = new Date(2003 - 1900, 10 - 1, 1);
211         cal.setTime(x);
212 
213         System.out.println("  Changing the cutover date to yesterday...");
214         cal.setGregorianChange(new Date(x.getTime() - (24 * 3600 * 1000)));
215         if (!cal.checkDate(2003, OCTOBER, 1)) {
216             fail("    " + cal.getMessage());
217         }
218         System.out.println("  Changing the cutover date to tomorrow...");
219         cal.setGregorianChange(new Date(x.getTime() + (24 * 3600 * 1000)));
220         if (!cal.checkDate(2003, SEPTEMBER, 18)) {
221             fail("    " + cal.getMessage());
222         }
223     }
224 
225     /**
226      * 4743587: GregorianCalendar.getLeastMaximum() returns wrong values
227      */
228     @Test
229     public void Test4743587() {
230         Koyomi cal = new Koyomi();
231         Koyomi cal2 = (Koyomi) cal.clone();
232         System.out.println("getLeastMaximum should handle cutover year.\n"
233                 + "  default cutover date");
234         if (!cal.checkLeastMaximum(DAY_OF_YEAR, 365 - 10)) {
235             fail("    " + cal.getMessage());
236         }
237         if (!cal.checkLeastMaximum(WEEK_OF_YEAR, 52 - ((10 + 6) / 7))) {
238             fail("    " + cal.getMessage());
239         }
240         // Corrected for 4956232
241         if (!cal.checkLeastMaximum(DAY_OF_MONTH, 28)) {
242             fail("    " + cal.getMessage());
243         }
244         if (!cal.checkLeastMaximum(WEEK_OF_MONTH, 3)) {
245             fail("    " + cal.getMessage());
246         }
247         if (!cal.checkLeastMaximum(DAY_OF_WEEK_IN_MONTH, 3)) {
248             fail("    " + cal.getMessage());
249         }
250         // make sure that getLeastMaximum calls didn't affect the date
251         if (!cal.equals(cal2)) {
252             fail("    getLeastMaximum calls modified the object.");
253         }
254         if (!cal.checkGreatestMinimum(DAY_OF_MONTH, 1)) {
255             fail("    " + cal.getMessage());
256         }
257 
258         System.out.println("  changing the date to 1582/10/20 for actual min/max tests");
259         cal.set(1582, OCTOBER, 20);
260         if (!cal.checkActualMinimum(DAY_OF_MONTH, 1)) {
261             fail("    " + cal.getMessage());
262         }
263         if (!cal.checkActualMaximum(DAY_OF_MONTH, 31)) {
264             fail("    " + cal.getMessage());
265         }
266 
267         cal = new Koyomi();
268         System.out.println("Change the cutover date to 1970/1/5.");
269         @SuppressWarnings("deprecation")
270         Date d = new Date(1970 - 1900, 0, 5);
271         cal.setGregorianChange(d);
272         if (!cal.checkLeastMaximum(DAY_OF_YEAR, 356)) {
273             fail("    " + cal.getMessage());
274         }
275         if (!cal.checkLeastMaximum(DAY_OF_MONTH, 22)) {
276             fail("    " + cal.getMessage());
277         }
278         if (!cal.checkGreatestMinimum(DAY_OF_MONTH, 5)) {
279             fail("    " + cal.getMessage());
280         }
281         cal.set(1970, JANUARY, 10);
282         if (!cal.checkActualMinimum(DAY_OF_MONTH, 5)) {
283             fail("    " + cal.getMessage());
284         }
285         if (!cal.checkActualMaximum(DAY_OF_MONTH, 31)) {
286             fail("    " + cal.getMessage());
287         }
288     }
289 
290     /**
291      * 6459836: (cal) GregorianCalendar set method provides wrong result
292      */
293     @Test
294     public void Test6459836() {
295         int hour = 13865672;
296         Koyomi gc1 = new Koyomi();
297         gc1.clear();
298         gc1.set(1, JANUARY, 1, 0, 0, 0);
299         gc1.set(HOUR_OF_DAY, hour);
300         if (!gc1.checkDate(1582, OCTOBER, 4)) {
301             fail("test case 1: " + gc1.getMessage());
302         }
303         gc1.clear();
304         gc1.set(1, JANUARY, 1, 0, 0, 0);
305         gc1.set(HOUR_OF_DAY, hour + 24);
306         if (!gc1.checkDate(1582, OCTOBER, 15)) {
307             fail("test case 2: " + gc1.getMessage());
308         }
309     }
310 
311     /**
312      * 6549953 (cal) WEEK_OF_YEAR and DAY_OF_YEAR calculation problems around Gregorian cutover
313      */
314     @Test
315     public void Test6549953() {
316         Koyomi cal = new Koyomi();
317 
318         cal.set(YEAR, 1582);
319         cal.set(WEEK_OF_YEAR, 42);
320         cal.set(DAY_OF_WEEK, FRIDAY);
321         cal.checkFieldValue(WEEK_OF_YEAR, 42);
322         cal.checkFieldValue(DAY_OF_WEEK, FRIDAY);
323         if (!cal.checkDate(1582, OCTOBER, 29)) {
324             fail(cal.getMessage());
325         }
326         cal.clear();
327         cal.set(1582, OCTOBER, 1);
328         cal.set(DAY_OF_YEAR, 292);
329         if (!cal.checkDate(1582, OCTOBER, 29)) {
330             fail(cal.getMessage());
331         }
332     }
333 }