1 /*
  2  * Copyright (c) 2000, 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 4372743
 27  * @summary test that checks transitions of ERA and YEAR which are caused by add(MONTH).
 28  * @library /java/text/testlib
 29  */
 30 
 31 import java.util.GregorianCalendar;
 32 import java.util.TimeZone;
 33 
 34 import static java.util.GregorianCalendar.*;
 35 
 36 public class bug4372743 extends IntlTest {
 37 
 38     public static void main(String[] args) throws Exception {
 39         new bug4372743().run(args);
 40     }
 41 
 42     private int[][] data = {
 43         {AD, 2, MARCH},
 44         {AD, 2, FEBRUARY},
 45         {AD, 2, JANUARY},
 46         {AD, 1, DECEMBER},
 47         {AD, 1, NOVEMBER},
 48         {AD, 1, OCTOBER},
 49         {AD, 1, SEPTEMBER},
 50         {AD, 1, AUGUST},
 51         {AD, 1, JULY},
 52         {AD, 1, JUNE},
 53         {AD, 1, MAY},
 54         {AD, 1, APRIL},
 55         {AD, 1, MARCH},
 56         {AD, 1, FEBRUARY},
 57         {AD, 1, JANUARY},
 58         {BC, 1, DECEMBER},
 59         {BC, 1, NOVEMBER},
 60         {BC, 1, OCTOBER},
 61         {BC, 1, SEPTEMBER},
 62         {BC, 1, AUGUST},
 63         {BC, 1, JULY},
 64         {BC, 1, JUNE},
 65         {BC, 1, MAY},
 66         {BC, 1, APRIL},
 67         {BC, 1, MARCH},
 68         {BC, 1, FEBRUARY},
 69         {BC, 1, JANUARY},
 70         {BC, 2, DECEMBER},
 71         {BC, 2, NOVEMBER},
 72         {BC, 2, OCTOBER}};
 73     private int tablesize = data.length;
 74 
 75     private void check(GregorianCalendar gc, int index) {
 76         if (gc.get(ERA) != data[index][ERA]) {
 77             errln("Invalid era :" + gc.get(ERA)
 78                     + ", expected :" + data[index][ERA]);
 79         }
 80         if (gc.get(YEAR) != data[index][YEAR]) {
 81             errln("Invalid year :" + gc.get(YEAR)
 82                     + ", expected :" + data[index][YEAR]);
 83         }
 84         if (gc.get(MONTH) != data[index][MONTH]) {
 85             errln("Invalid month :" + gc.get(MONTH)
 86                     + ", expected :" + data[index][MONTH]);
 87         }
 88     }
 89 
 90     public void Test4372743() {
 91         GregorianCalendar gc;
 92         TimeZone saveZone = TimeZone.getDefault();
 93 
 94         try {
 95             TimeZone.setDefault(TimeZone.getTimeZone("PST"));
 96 
 97             /* Set March 3, A.D. 2 */
 98             gc = new GregorianCalendar(2, MARCH, 3);
 99             for (int i = 0; i < tablesize; i++) {
100                 check(gc, i);
101                 gc.add(MONTH, -1);
102             }
103 
104             /* Again, Set March 3, A.D. 2 */
105             gc = new GregorianCalendar(2, MARCH, 3);
106             for (int i = 0; i < tablesize; i += 7) {
107                 check(gc, i);
108                 gc.add(MONTH, -7);
109             }
110 
111             /* Set March 10, 2 B.C. */
112             gc = new GregorianCalendar(2, OCTOBER, 10);
113             gc.add(YEAR, -3);
114             for (int i = tablesize - 1; i >= 0; i--) {
115                 check(gc, i);
116                 gc.add(MONTH, 1);
117             }
118 
119             /* Again, Set March 10, 2 B.C. */
120             gc = new GregorianCalendar(2, OCTOBER, 10);
121             gc.add(YEAR, -3);
122             for (int i = tablesize - 1; i >= 0; i -= 8) {
123                 check(gc, i);
124                 gc.add(MONTH, 8);
125             }
126         } finally {
127             TimeZone.setDefault(saveZone);
128         }
129     }
130 }