1 /*
   2  * Copyright (c) 2004, 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 5078053
  27  * @summary Make sure that Calendar.complete() normalizes stamp[] to
  28  * COMPUTED. This can be observed through add() and roll().
  29  */
  30 
  31 import java.util.TimeZone;
  32 import static java.util.Calendar.*;
  33 
  34 public class Bug5078053 {
  35     static int errorCount = 0;
  36 
  37     public static void main(String[] args) {
  38         TimeZone defaultTz = TimeZone.getDefault();
  39 
  40         try {
  41             TimeZone tz = TimeZone.getTimeZone("Australia/Adelaide");
  42             TimeZone.setDefault(tz);
  43             Koyomi cal = new Koyomi();
  44             cal.setFirstDayOfWeek(2);
  45             cal.setMinimalDaysInFirstWeek(4);
  46 
  47             // test roll()
  48             cal.clear();
  49             // 2002-01-01T00:00:00 in Australia/Adelaide
  50             cal.setTimeInMillis(1009805400000L);
  51             System.out.println(cal.getTime());
  52             // The following set calls shouldn't affect roll() and add()
  53             cal.set(DAY_OF_WEEK, cal.get(DAY_OF_WEEK));
  54             cal.set(WEEK_OF_YEAR, cal.get(WEEK_OF_YEAR));
  55             cal.getTime();
  56             cal.roll(MONTH, +1);
  57             System.out.println("roll: " + cal.getTime());
  58             if (!cal.checkDate(2002, FEBRUARY, 1)) {
  59                 error("roll(MONTH, +1): " + cal.getMessage());
  60             }
  61             cal.roll(MONTH, -1);
  62             if (!cal.checkDate(2002, JANUARY, 1)) {
  63                 error("roll(MONTH, -1): " + cal.getMessage());
  64             }
  65 
  66             // test add()
  67             cal.clear();
  68             // 2002-01-01T00:00:00+0930 in Australia/Adelaide
  69             cal.setTimeInMillis(1009805400000L);
  70             cal.set(DAY_OF_WEEK, cal.get(DAY_OF_WEEK));
  71             cal.set(WEEK_OF_YEAR, cal.get(WEEK_OF_YEAR));
  72             cal.getTime();
  73             cal.add(MONTH, +1);
  74             System.out.println(" add: " + cal.getTime());
  75             if (!cal.checkDate(2002, FEBRUARY, 1)) {
  76                 error("add(MONTH, +1): " + cal.getMessage());
  77             }
  78             cal.add(MONTH, -1);
  79             if (!cal.checkDate(2002, JANUARY, 1)) {
  80                 error("add(MONTH, -1): " + cal.getMessage());
  81             }
  82         }
  83         finally {
  84             TimeZone.setDefault(defaultTz);
  85         }
  86 
  87         checkErrors();
  88     }
  89 
  90     static void error(String s) {
  91         System.out.println(s);
  92         errorCount++;
  93     }
  94 
  95     static void checkErrors() {
  96         if (errorCount > 0) {
  97             throw new RuntimeException("Failed: " + errorCount + " error(s)");
  98         }
  99     }
 100 }