< prev index next >

test/jdk/java/util/Calendar/bug4514831.java

Print this page

  1 /*
  2  * Copyright (c) 2002, 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 4514831
 27  * @summary Confirm that GregorianCalendar.roll() works properly during transition from Daylight Saving Time to Standard Time.


 28  */
 29 
 30 import java.util.GregorianCalendar;
 31 import java.util.Locale;
 32 import java.util.TimeZone;
 33 
 34 import static java.util.GregorianCalendar.*;





 35 



 36 
 37 public class bug4514831 {
 38 
 39     public static void main(String[] args) {
 40         Locale savedLocale = Locale.getDefault();
 41         TimeZone savedTimeZone = TimeZone.getDefault();
 42         boolean err = false;
 43 
 44         String golden_data1 = "27-28 28-29 29-30 30-31 31-1 1-2 2-3 ";
 45         String golden_data2 = "27-28 28-29 29-30 30-31 31-25 25-26 26-27 ";
 46         String golden_data3 = "1-8 8-15 15-22 22-29 29-1 1-8 8-15 ";




 47 
 48         try {
 49             Locale.setDefault(Locale.US);
 50             TimeZone.setDefault(TimeZone.getTimeZone("US/Pacific"));



 51 
 52             String test_roll = "";
 53             GregorianCalendar c_roll = new GregorianCalendar(2001, OCTOBER, 27);
 54             for (int i = 0; i < 7; i++) {
 55                 test_roll += c_roll.get(DAY_OF_MONTH) + "-";
 56                 c_roll.roll(DAY_OF_YEAR, true);
 57                 test_roll += c_roll.get(DAY_OF_MONTH) + " ";
 58             }
 59             if (!test_roll.equals(golden_data1)) {
 60                 err = true;
 61                 System.err.println("Wrong roll(DAY_OF_YEAR) transition: got "
 62                         + test_roll + "expected " + golden_data1);
 63             }
 64 
 65             test_roll = "";
 66             c_roll = new GregorianCalendar(2001, OCTOBER, 27);
 67             c_roll.setFirstDayOfWeek(THURSDAY);
 68             for (int i = 0; i < 7; i++) {
 69                 test_roll += c_roll.get(DAY_OF_MONTH) + "-";
 70                 c_roll.roll(DAY_OF_WEEK, true);
 71                 test_roll += c_roll.get(DAY_OF_MONTH) + " ";
 72             }
 73             if (!test_roll.equals(golden_data2)) {
 74                 err = true;
 75                 System.err.println("Wrong roll(DAY_OF_WEEK) transition: got "
 76                         + test_roll + "expected " + golden_data2);
 77             }



 78 
 79             test_roll = "";
 80             c_roll = new GregorianCalendar(2001, OCTOBER, 1);
 81             for (int i = 0; i < 7; i++) {
 82                 test_roll += c_roll.get(DAY_OF_MONTH) + "-";
 83                 c_roll.roll(DAY_OF_WEEK_IN_MONTH, true);
 84                 test_roll += c_roll.get(DAY_OF_MONTH) + " ";
 85             }
 86             if (!test_roll.equals(golden_data3)) {
 87                 err = true;
 88                 System.err.println("Wrong roll(DAY_OF_WEEK_IN_MONTH) transition: got "
 89                         + test_roll + "expected " + golden_data3);
 90             }
 91         } finally {
 92             Locale.setDefault(savedLocale);
 93             TimeZone.setDefault(savedTimeZone);
 94         }



 95 
 96         if (err) {
 97             throw new RuntimeException("Wrong roll() transition");










 98         }


 99     }
100 }

  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 }
< prev index next >