1 /*
  2  * Copyright (c) 2002, 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 4514831
 27  * @summary Confirm that GregorianCalendar.roll() works properly during
 28  *          transition from Daylight Saving Time to Standard Time.
 29  * @run junit bug4514831
 30  */
 31 
 32 import java.util.GregorianCalendar;
 33 import java.util.Locale;
 34 import java.util.TimeZone;
 35 
 36 import static java.util.GregorianCalendar.DAY_OF_MONTH;
 37 import static java.util.GregorianCalendar.DAY_OF_WEEK;
 38 import static java.util.GregorianCalendar.DAY_OF_WEEK_IN_MONTH;
 39 import static java.util.GregorianCalendar.DAY_OF_YEAR;
 40 import static java.util.GregorianCalendar.OCTOBER;
 41 import static java.util.GregorianCalendar.THURSDAY;
 42 
 43 import org.junit.jupiter.api.AfterAll;
 44 import org.junit.jupiter.api.BeforeAll;
 45 import org.junit.jupiter.api.Test;
 46 
 47 import static org.junit.jupiter.api.Assertions.assertEquals;
 48 
 49 public class bug4514831 {
 50     // Data of 7 rolls in the form of a string for the respective field
 51     private static final String expectedDayOfYearData = "27-28 28-29 29-30 30-31 31-1 1-2 2-3 ";
 52     private static final String expectedDayOfWeekData = "27-28 28-29 29-30 30-31 31-25 25-26 26-27 ";
 53     private static final String expectedDayOfWeekInMonthData = "1-8 8-15 15-22 22-29 29-1 1-8 8-15 ";
 54     private static final TimeZone savedTz = TimeZone.getDefault();
 55     private static final Locale savedLocale = Locale.getDefault();
 56 
 57     // Save JVM default Locale and TimeZone
 58     @BeforeAll
 59     void initAll() {
 60         Locale.setDefault(Locale.US);
 61         TimeZone.setDefault(TimeZone.getTimeZone("US/Pacific"));
 62     }
 63 
 64     // Restore JVM default Locale and TimeZone
 65     @AfterAll
 66     void tearDownAll() {
 67         Locale.setDefault(savedLocale);
 68         TimeZone.setDefault(savedTz);
 69     }
 70 
 71     /*
 72      * Test some roll values during transition (DAY_OF_YEAR field). Uses
 73      * the boolean roll method. Roll multiple times and attach the returned
 74      * values to a long string which is then compared to the expected data.
 75      */
 76     public void rollDayOfYearTest() {
 77         StringBuilder actualRollData = new StringBuilder();
 78         GregorianCalendar cal = new GregorianCalendar(2001, OCTOBER, 27);
 79         for (int i = 0; i < 7; i++) {
 80             actualRollData.append(cal.get(DAY_OF_MONTH)).append("-");
 81             cal.roll(DAY_OF_YEAR, true);
 82             actualRollData.append(cal.get(DAY_OF_MONTH)).append(" ");
 83         }
 84         assertEquals(expectedDayOfYearData, actualRollData.toString(),
 85                 "Wrong roll(DAY_OF_YEAR) transition");
 86     }
 87 
 88     /*
 89      * Test some roll values during transition (DAY_OF_WEEK field). Uses
 90      * the boolean roll method. Roll multiple times and attach the returned
 91      * values to a long string which is then compared to the expected data.
 92      */
 93     public void rollDayOfWeekTest() {
 94         StringBuilder actualRollData = new StringBuilder();
 95         GregorianCalendar cal = new GregorianCalendar(2001, OCTOBER, 27);
 96         cal.setFirstDayOfWeek(THURSDAY);
 97         for (int i = 0; i < 7; i++) {
 98             actualRollData.append(cal.get(DAY_OF_MONTH)).append("-");
 99             cal.roll(DAY_OF_WEEK, true);
100             actualRollData.append(cal.get(DAY_OF_MONTH)).append(" ");
101         }
102         assertEquals(expectedDayOfWeekData, actualRollData.toString(),
103                 "Wrong roll(DAY_OF_WEEK) transition");
104     }
105 
106     /*
107      * Test some roll values during transition (DAY_OF_WEEK_IN_MONTH field). Uses
108      * the boolean roll method. Roll multiple times and attach the returned
109      * values to a long string which is then compared to the expected data.
110      */
111     public void rollDayOfWeekInMonthTest() {
112         StringBuilder actualRollData = new StringBuilder();
113         GregorianCalendar cal = new GregorianCalendar(2001, OCTOBER, 1);
114         for (int i = 0; i < 7; i++) {
115             actualRollData.append(cal.get(DAY_OF_MONTH)).append("-");
116             cal.roll(DAY_OF_WEEK_IN_MONTH, true);
117             actualRollData.append(cal.get(DAY_OF_MONTH)).append(" ");
118         }
119         assertEquals(expectedDayOfWeekInMonthData, actualRollData.toString(),
120                 "Wrong roll(DAY_OF_WEEK_IN_MONTH) transition");
121     }
122 }