Home | History | Annotate | Download | only in util
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.tests.java.util;
     19 
     20 import java.util.BitSet;
     21 import java.util.Calendar;
     22 import java.util.Date;
     23 import java.util.GregorianCalendar;
     24 import java.util.Locale;
     25 import java.util.SimpleTimeZone;
     26 import java.util.TimeZone;
     27 import java.util.Vector;
     28 
     29 public class GregorianCalendarTest extends junit.framework.TestCase {
     30 
     31     private static final TimeZone AMERICA_CHICAGO = TimeZone.getTimeZone("America/Chicago");
     32     private static final TimeZone AMERICA_NEW_YORK = TimeZone.getTimeZone("America/New_York");
     33 
     34     private Locale defaultLocale;
     35 
     36     @Override
     37     public void setUp() throws Exception {
     38         super.setUp();
     39         defaultLocale = Locale.getDefault();
     40         // Most tests are locale independent, but locale does affect start-of-week.
     41         Locale.setDefault(Locale.US);
     42     }
     43 
     44     @Override
     45     public void tearDown() throws Exception {
     46         Locale.setDefault(defaultLocale);
     47         super.tearDown();
     48     }
     49 
     50     /**
     51      * java.util.GregorianCalendar#GregorianCalendar()
     52      */
     53     public void test_Constructor() {
     54         // Test for method java.util.GregorianCalendar()
     55         assertTrue("Constructed incorrect calendar", (new GregorianCalendar()
     56                 .isLenient()));
     57     }
     58 
     59     /**
     60      * java.util.GregorianCalendar#GregorianCalendar(int, int, int)
     61      */
     62     public void test_ConstructorIII() {
     63         // Test for method java.util.GregorianCalendar(int, int, int)
     64         GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 13);
     65         assertEquals("Incorrect calendar constructed 1",
     66                 1972, gc.get(Calendar.YEAR));
     67         assertTrue("Incorrect calendar constructed 2",
     68                 gc.get(Calendar.MONTH) == Calendar.OCTOBER);
     69         assertEquals("Incorrect calendar constructed 3", 13, gc
     70                 .get(Calendar.DAY_OF_MONTH));
     71         assertTrue("Incorrect calendar constructed 4", gc.getTimeZone().equals(
     72                 TimeZone.getDefault()));
     73     }
     74 
     75     /**
     76      * java.util.GregorianCalendar#GregorianCalendar(int, int, int, int,
     77      *int)
     78      */
     79     public void test_ConstructorIIIII() {
     80         // Test for method java.util.GregorianCalendar(int, int, int, int, int)
     81         GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER,
     82                 13, 19, 9);
     83         assertEquals("Incorrect calendar constructed",
     84                 1972, gc.get(Calendar.YEAR));
     85         assertTrue("Incorrect calendar constructed",
     86                 gc.get(Calendar.MONTH) == Calendar.OCTOBER);
     87         assertEquals("Incorrect calendar constructed", 13, gc
     88                 .get(Calendar.DAY_OF_MONTH));
     89         assertEquals("Incorrect calendar constructed", 7, gc.get(Calendar.HOUR));
     90         assertEquals("Incorrect calendar constructed",
     91                 1, gc.get(Calendar.AM_PM));
     92         assertEquals("Incorrect calendar constructed",
     93                 9, gc.get(Calendar.MINUTE));
     94         assertTrue("Incorrect calendar constructed", gc.getTimeZone().equals(
     95                 TimeZone.getDefault()));
     96 
     97         //Regression for HARMONY-998
     98         gc = new GregorianCalendar(1900, 0, 0, 0, Integer.MAX_VALUE);
     99         assertEquals("Incorrect calendar constructed",
    100                 5983, gc.get(Calendar.YEAR));
    101     }
    102 
    103     /**
    104      * java.util.GregorianCalendar#GregorianCalendar(int, int, int, int,
    105      *int, int)
    106      */
    107     public void test_ConstructorIIIIII() {
    108         // Test for method java.util.GregorianCalendar(int, int, int, int, int,
    109         // int)
    110         GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER,
    111                 13, 19, 9, 59);
    112         assertEquals("Incorrect calendar constructed",
    113                 1972, gc.get(Calendar.YEAR));
    114         assertTrue("Incorrect calendar constructed",
    115                 gc.get(Calendar.MONTH) == Calendar.OCTOBER);
    116         assertEquals("Incorrect calendar constructed", 13, gc
    117                 .get(Calendar.DAY_OF_MONTH));
    118         assertEquals("Incorrect calendar constructed", 7, gc.get(Calendar.HOUR));
    119         assertEquals("Incorrect calendar constructed",
    120                 1, gc.get(Calendar.AM_PM));
    121         assertEquals("Incorrect calendar constructed",
    122                 9, gc.get(Calendar.MINUTE));
    123         assertEquals("Incorrect calendar constructed",
    124                 59, gc.get(Calendar.SECOND));
    125         assertTrue("Incorrect calendar constructed", gc.getTimeZone().equals(
    126                 TimeZone.getDefault()));
    127     }
    128 
    129     /**
    130      * java.util.GregorianCalendar#GregorianCalendar(java.util.Locale)
    131      */
    132     public void test_ConstructorLjava_util_Locale() {
    133         // Test for method java.util.GregorianCalendar(java.util.Locale)
    134         Date date = new Date();
    135         GregorianCalendar gcJapan = new GregorianCalendar(Locale.JAPAN);
    136         gcJapan.setTime(date);
    137         GregorianCalendar gcJapan2 = new GregorianCalendar(Locale.JAPAN);
    138         gcJapan2.setTime(date);
    139         GregorianCalendar gcItaly = new GregorianCalendar(Locale.ITALY);
    140         gcItaly.setTime(date);
    141         assertTrue("Locales not created correctly", gcJapan.equals(gcJapan2)
    142                 && !gcJapan.equals(gcItaly));
    143     }
    144 
    145     /**
    146      * java.util.GregorianCalendar#GregorianCalendar(java.util.TimeZone)
    147      */
    148     public void test_ConstructorLjava_util_TimeZone() {
    149         // Test for method java.util.GregorianCalendar(java.util.TimeZone)
    150         Date date = new Date(2008, 1, 1);
    151         TimeZone.getDefault();
    152         GregorianCalendar gc1 = new GregorianCalendar(AMERICA_NEW_YORK);
    153         gc1.setTime(date);
    154         GregorianCalendar gc2 = new GregorianCalendar(AMERICA_CHICAGO);
    155         gc2.setTime(date);
    156         // Chicago is 1 hour before New York, add 1 to the Chicago time and convert to 0-12 value
    157         assertEquals("Incorrect calendar returned",
    158                 gc1.get(Calendar.HOUR), ((gc2.get(Calendar.HOUR) + 1) % 12));
    159 
    160         // Regression test for HARMONY-2961
    161         SimpleTimeZone timezone = new SimpleTimeZone(-3600 * 24 * 1000 * 2,
    162                 "GMT");
    163         GregorianCalendar gc = new GregorianCalendar(timezone);
    164 
    165         // Regression test for HARMONY-5195
    166         Calendar c1 = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    167         c1.set(Calendar.YEAR, 1999);
    168         c1.set(Calendar.MONTH, Calendar.JUNE);
    169         c1.set(Calendar.DAY_OF_MONTH, 2);
    170         c1.set(Calendar.HOUR, 15);
    171         c1.set(Calendar.MINUTE, 34);
    172         c1.set(Calendar.SECOND, 16);
    173         assertEquals(34, c1.get(Calendar.MINUTE));
    174         c1.setTimeZone(new SimpleTimeZone(60000, "ONE MINUTE"));
    175         assertEquals(35, c1.get(Calendar.MINUTE));
    176     }
    177 
    178     /**
    179      * java.util.GregorianCalendar#GregorianCalendar(java.util.TimeZone,
    180      *java.util.Locale)
    181      */
    182     public void test_ConstructorLjava_util_TimeZoneLjava_util_Locale() {
    183         // Test for method java.util.GregorianCalendar(java.util.TimeZone,
    184         // java.util.Locale)
    185         Date date = new Date(2008, 1, 1);
    186         TimeZone.getDefault();
    187         GregorianCalendar gc1 = new GregorianCalendar(AMERICA_NEW_YORK, Locale.JAPAN);
    188         gc1.setTime(date);
    189         GregorianCalendar gc2 = new GregorianCalendar(AMERICA_NEW_YORK, Locale.JAPAN);
    190         gc2.setTime(date);
    191         GregorianCalendar gc3 = new GregorianCalendar(AMERICA_CHICAGO, Locale.ITALY);
    192         gc3.setTime(date);
    193         // Chicago is 1 hour before New York, add 1 to the Chicago time and convert to 0-12 value
    194         assertEquals("Incorrect calendar returned",
    195                 gc1.get(Calendar.HOUR), ((gc3.get(Calendar.HOUR) + 1) % 12));
    196         assertTrue("Locales not created correctly", gc1.equals(gc2)
    197                 && !gc1.equals(gc3));
    198     }
    199 
    200     /**
    201      * java.util.GregorianCalendar#add(int, int)
    202      */
    203     public void test_addII() {
    204         // Test for method void java.util.GregorianCalendar.add(int, int)
    205         GregorianCalendar gc1 = new GregorianCalendar(1998, 11, 6);
    206         gc1.add(GregorianCalendar.YEAR, 1);
    207         assertEquals("Add failed to Increment",
    208                 1999, gc1.get(GregorianCalendar.YEAR));
    209 
    210         gc1 = new GregorianCalendar(1999, Calendar.JULY, 31);
    211         gc1.add(Calendar.MONTH, 7);
    212         assertEquals("Wrong result year 1", 2000, gc1.get(Calendar.YEAR));
    213         assertTrue("Wrong result month 1",
    214                 gc1.get(Calendar.MONTH) == Calendar.FEBRUARY);
    215         assertEquals("Wrong result date 1", 29, gc1.get(Calendar.DATE));
    216 
    217         gc1.add(Calendar.YEAR, -1);
    218         assertEquals("Wrong result year 2", 1999, gc1.get(Calendar.YEAR));
    219         assertTrue("Wrong result month 2",
    220                 gc1.get(Calendar.MONTH) == Calendar.FEBRUARY);
    221         assertEquals("Wrong result date 2", 28, gc1.get(Calendar.DATE));
    222 
    223         gc1 = new GregorianCalendar(AMERICA_NEW_YORK);
    224         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    225         gc1.add(Calendar.MILLISECOND, 24 * 60 * 60 * 1000);
    226         assertEquals("Wrong time after MILLISECOND change", 17, gc1
    227                 .get(Calendar.HOUR_OF_DAY));
    228         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    229         gc1.add(Calendar.SECOND, 24 * 60 * 60);
    230         assertEquals("Wrong time after SECOND change", 17, gc1
    231                 .get(Calendar.HOUR_OF_DAY));
    232         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    233         gc1.add(Calendar.MINUTE, 24 * 60);
    234         assertEquals("Wrong time after MINUTE change", 17, gc1
    235                 .get(Calendar.HOUR_OF_DAY));
    236         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    237         gc1.add(Calendar.HOUR, 24);
    238         assertEquals("Wrong time after HOUR change", 17, gc1
    239                 .get(Calendar.HOUR_OF_DAY));
    240         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    241         gc1.add(Calendar.HOUR_OF_DAY, 24);
    242         assertEquals("Wrong time after HOUR_OF_DAY change", 17, gc1
    243                 .get(Calendar.HOUR_OF_DAY));
    244 
    245         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    246         gc1.add(Calendar.AM_PM, 2);
    247         assertEquals("Wrong time after AM_PM change", 16, gc1
    248                 .get(Calendar.HOUR_OF_DAY));
    249         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    250         gc1.add(Calendar.DATE, 1);
    251         assertEquals("Wrong time after DATE change", 16, gc1
    252                 .get(Calendar.HOUR_OF_DAY));
    253         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    254         gc1.add(Calendar.DAY_OF_YEAR, 1);
    255         assertEquals("Wrong time after DAY_OF_YEAR change", 16, gc1
    256                 .get(Calendar.HOUR_OF_DAY));
    257         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    258         gc1.add(Calendar.DAY_OF_WEEK, 1);
    259         assertEquals("Wrong time after DAY_OF_WEEK change", 16, gc1
    260                 .get(Calendar.HOUR_OF_DAY));
    261         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    262         gc1.add(Calendar.WEEK_OF_YEAR, 1);
    263         assertEquals("Wrong time after WEEK_OF_YEAR change", 16, gc1
    264                 .get(Calendar.HOUR_OF_DAY));
    265         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    266         gc1.add(Calendar.WEEK_OF_MONTH, 1);
    267         assertEquals("Wrong time after WEEK_OF_MONTH change", 16, gc1
    268                 .get(Calendar.HOUR_OF_DAY));
    269         gc1.set(1999, Calendar.APRIL, 3, 16, 0); // day before DST change
    270         gc1.add(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
    271         assertEquals("Wrong time after DAY_OF_WEEK_IN_MONTH change", 16, gc1
    272                 .get(Calendar.HOUR_OF_DAY));
    273 
    274         gc1.clear();
    275         gc1.set(2000, Calendar.APRIL, 1, 23, 0);
    276         gc1.add(Calendar.DATE, 1);
    277         assertTrue("Wrong time after DATE change near DST boundary", gc1
    278                 .get(Calendar.MONTH) == Calendar.APRIL
    279                 && gc1.get(Calendar.DATE) == 2
    280                 && gc1.get(Calendar.HOUR_OF_DAY) == 23);
    281     }
    282 
    283     /**
    284      * java.util.GregorianCalendar#equals(java.lang.Object)
    285      */
    286     public void test_equalsLjava_lang_Object() {
    287         // Test for method boolean
    288         // java.util.GregorianCalendar.equals(java.lang.Object)
    289         GregorianCalendar gc1 = new GregorianCalendar(1998, 11, 6);
    290         GregorianCalendar gc2 = new GregorianCalendar(2000, 11, 6);
    291         GregorianCalendar gc3 = new GregorianCalendar(1998, 11, 6);
    292         assertTrue("Equality check failed", gc1.equals(gc3));
    293         assertTrue("Equality check failed", !gc1.equals(gc2));
    294         gc3.setGregorianChange(new Date());
    295         assertTrue("Different gregorian change", !gc1.equals(gc3));
    296     }
    297 
    298     /**
    299      * java.util.GregorianCalendar#getActualMaximum(int)
    300      */
    301     public void test_getActualMaximumI() {
    302         // Test for method int java.util.GregorianCalendar.getActualMaximum(int)
    303         GregorianCalendar gc1 = new GregorianCalendar(1900, 1, 1);
    304         GregorianCalendar gc2 = new GregorianCalendar(1996, 1, 1);
    305         GregorianCalendar gc3 = new GregorianCalendar(1997, 1, 1);
    306         GregorianCalendar gc4 = new GregorianCalendar(2000, 1, 1);
    307         GregorianCalendar gc5 = new GregorianCalendar(2000, 9, 9);
    308         GregorianCalendar gc6 = new GregorianCalendar(2000, 3, 3);
    309         assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 1900",
    310                 28, gc1.getActualMaximum(Calendar.DAY_OF_MONTH));
    311         assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 1996",
    312                 29, gc2.getActualMaximum(Calendar.DAY_OF_MONTH));
    313         assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 1998",
    314                 28, gc3.getActualMaximum(Calendar.DAY_OF_MONTH));
    315         assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Feb 2000",
    316                 29, gc4.getActualMaximum(Calendar.DAY_OF_MONTH));
    317         assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Oct 2000",
    318                 31, gc5.getActualMaximum(Calendar.DAY_OF_MONTH));
    319         assertEquals("Wrong actual maximum value for DAY_OF_MONTH for Apr 2000",
    320                 30, gc6.getActualMaximum(Calendar.DAY_OF_MONTH));
    321         assertTrue("Wrong actual maximum value for MONTH", gc1
    322                 .getActualMaximum(Calendar.MONTH) == Calendar.DECEMBER);
    323         assertEquals("Wrong actual maximum value for HOUR_OF_DAY", 23, gc1
    324                 .getActualMaximum(Calendar.HOUR_OF_DAY));
    325         assertEquals("Wrong actual maximum value for HOUR", 11, gc1
    326                 .getActualMaximum(Calendar.HOUR));
    327         assertEquals("Wrong actual maximum value for DAY_OF_WEEK_IN_MONTH", 4, gc6
    328                 .getActualMaximum(Calendar.DAY_OF_WEEK_IN_MONTH));
    329 
    330 
    331         // Regression test for harmony 2954
    332         Date date = new Date(Date.parse("Jan 15 00:00:01 GMT 2000"));
    333         GregorianCalendar gc = new GregorianCalendar();
    334         gc.setTimeInMillis(Date.parse("Dec 15 00:00:01 GMT 1582"));
    335         assertEquals(355, gc.getActualMaximum(Calendar.DAY_OF_YEAR));
    336         gc.setGregorianChange(date);
    337         gc.setTimeInMillis(Date.parse("Jan 16 00:00:01 GMT 2000"));
    338         assertEquals(353, gc.getActualMaximum(Calendar.DAY_OF_YEAR));
    339 
    340         //Regression test for HARMONY-3004
    341         gc = new GregorianCalendar(1900, 7, 1);
    342         String[] ids = TimeZone.getAvailableIDs();
    343         for (int i = 0; i < ids.length; i++) {
    344             TimeZone tz = TimeZone.getTimeZone(ids[i]);
    345             gc.setTimeZone(tz);
    346             for (int j = 1900; j < 2000; j++) {
    347                 gc.set(Calendar.YEAR, j);
    348                 assertEquals(7200000, gc.getActualMaximum(Calendar.DST_OFFSET));
    349             }
    350         }
    351     }
    352 
    353     /**
    354      * java.util.GregorianCalendar#getActualMinimum(int)
    355      */
    356     public void test_getActualMinimumI() {
    357         // Test for method int java.util.GregorianCalendar.getActualMinimum(int)
    358         GregorianCalendar gc1 = new GregorianCalendar(1900, 1, 1);
    359         new GregorianCalendar(1996, 1, 1);
    360         new GregorianCalendar(1997, 1, 1);
    361         new GregorianCalendar(2000, 1, 1);
    362         new GregorianCalendar(2000, 9, 9);
    363         GregorianCalendar gc6 = new GregorianCalendar(2000, 3, 3);
    364         assertEquals("Wrong actual minimum value for DAY_OF_MONTH for Feb 1900",
    365                 1, gc1.getActualMinimum(Calendar.DAY_OF_MONTH));
    366         assertTrue("Wrong actual minimum value for MONTH", gc1
    367                 .getActualMinimum(Calendar.MONTH) == Calendar.JANUARY);
    368         assertEquals("Wrong actual minimum value for HOUR_OF_DAY", 0, gc1
    369                 .getActualMinimum(Calendar.HOUR_OF_DAY));
    370         assertEquals("Wrong actual minimum value for HOUR", 0, gc1
    371                 .getActualMinimum(Calendar.HOUR));
    372         assertEquals("Wrong actual minimum value for DAY_OF_WEEK_IN_MONTH", 1, gc6
    373                 .getActualMinimum(Calendar.DAY_OF_WEEK_IN_MONTH));
    374     }
    375 
    376     /**
    377      * java.util.GregorianCalendar#getGreatestMinimum(int)
    378      */
    379     public void test_getGreatestMinimumI() {
    380         // Test for method int
    381         // java.util.GregorianCalendar.getGreatestMinimum(int)
    382         GregorianCalendar gc = new GregorianCalendar();
    383         assertEquals("Wrong greatest minimum value for DAY_OF_MONTH", 1, gc
    384                 .getGreatestMinimum(Calendar.DAY_OF_MONTH));
    385         assertTrue("Wrong greatest minimum value for MONTH", gc
    386                 .getGreatestMinimum(Calendar.MONTH) == Calendar.JANUARY);
    387         assertEquals("Wrong greatest minimum value for HOUR_OF_DAY", 0, gc
    388                 .getGreatestMinimum(Calendar.HOUR_OF_DAY));
    389         assertEquals("Wrong greatest minimum value for HOUR", 0, gc
    390                 .getGreatestMinimum(Calendar.HOUR));
    391 
    392         BitSet result = new BitSet();
    393         int[] min = { 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, -46800000,
    394                 0 };
    395         for (int i = 0; i < min.length; i++) {
    396             if (gc.getGreatestMinimum(i) != min[i])
    397                 result.set(i);
    398         }
    399         assertTrue("Wrong greatest min for " + result, result.length() == 0);
    400     }
    401 
    402     /**
    403      * java.util.GregorianCalendar#getGregorianChange()
    404      */
    405     public void test_getGregorianChange() {
    406         // Test for method java.util.Date
    407         // java.util.GregorianCalendar.getGregorianChange()
    408         GregorianCalendar gc = new GregorianCalendar();
    409         GregorianCalendar returnedChange = new GregorianCalendar(AMERICA_NEW_YORK);
    410         returnedChange.setTime(gc.getGregorianChange());
    411         assertEquals("Returned incorrect year",
    412                 1582, returnedChange.get(Calendar.YEAR));
    413         assertTrue("Returned incorrect month", returnedChange
    414                 .get(Calendar.MONTH) == Calendar.OCTOBER);
    415         assertEquals("Returned incorrect day of month", 4, returnedChange
    416                 .get(Calendar.DAY_OF_MONTH));
    417     }
    418 
    419     /**
    420      * java.util.GregorianCalendar#getLeastMaximum(int)
    421      */
    422     public void test_getLeastMaximumI() {
    423         // Test for method int java.util.GregorianCalendar.getLeastMaximum(int)
    424         GregorianCalendar gc = new GregorianCalendar();
    425         assertEquals("Wrong least maximum value for DAY_OF_MONTH", 28, gc
    426                 .getLeastMaximum(Calendar.DAY_OF_MONTH));
    427         assertTrue("Wrong least maximum value for MONTH", gc
    428                 .getLeastMaximum(Calendar.MONTH) == Calendar.DECEMBER);
    429         assertEquals("Wrong least maximum value for HOUR_OF_DAY", 23, gc
    430                 .getLeastMaximum(Calendar.HOUR_OF_DAY));
    431         assertEquals("Wrong least maximum value for HOUR", 11, gc
    432                 .getLeastMaximum(Calendar.HOUR));
    433 
    434         BitSet result = new BitSet();
    435         Vector values = new Vector();
    436         int[] max = { 1, 292269054, 11, 50, 3, 28, 355, 7, 3, 1, 11, 23, 59,
    437                 59, 999, 50400000, 1200000 };
    438         for (int i = 0; i < max.length; i++) {
    439             if (gc.getLeastMaximum(i) != max[i]) {
    440                 result.set(i);
    441                 values.add(new Integer(gc.getLeastMaximum(i)));
    442             }
    443         }
    444         assertTrue("Wrong least max for " + result + " = " + values, result
    445                 .length() == 0);
    446     }
    447 
    448     /**
    449      * java.util.GregorianCalendar#getMaximum(int)
    450      */
    451     public void test_getMaximumI() {
    452         // Test for method int java.util.GregorianCalendar.getMaximum(int)
    453         GregorianCalendar gc = new GregorianCalendar();
    454         assertEquals("Wrong maximum value for DAY_OF_MONTH", 31, gc
    455                 .getMaximum(Calendar.DAY_OF_MONTH));
    456         assertTrue("Wrong maximum value for MONTH", gc
    457                 .getMaximum(Calendar.MONTH) == Calendar.DECEMBER);
    458         assertEquals("Wrong maximum value for HOUR_OF_DAY", 23, gc
    459                 .getMaximum(Calendar.HOUR_OF_DAY));
    460         assertEquals("Wrong maximum value for HOUR",
    461                 11, gc.getMaximum(Calendar.HOUR));
    462 
    463         BitSet result = new BitSet();
    464         Vector values = new Vector();
    465         int[] max = { 1, 292278994, 11, 53, 6, 31, 366, 7, 6, 1, 11, 23, 59,
    466                 59, 999, 50400000, 7200000 };
    467         for (int i = 0; i < max.length; i++) {
    468             if (gc.getMaximum(i) != max[i]) {
    469                 result.set(i);
    470                 values.add(new Integer(gc.getMaximum(i)));
    471             }
    472         }
    473         assertTrue("Wrong max for " + result + " = " + values,
    474                 result.length() == 0);
    475     }
    476 
    477     /**
    478      * java.util.GregorianCalendar#getMinimum(int)
    479      */
    480     public void test_getMinimumI() {
    481         // Test for method int java.util.GregorianCalendar.getMinimum(int)
    482         GregorianCalendar gc = new GregorianCalendar();
    483         assertEquals("Wrong minimum value for DAY_OF_MONTH", 1, gc
    484                 .getMinimum(Calendar.DAY_OF_MONTH));
    485         assertTrue("Wrong minimum value for MONTH", gc
    486                 .getMinimum(Calendar.MONTH) == Calendar.JANUARY);
    487         assertEquals("Wrong minimum value for HOUR_OF_DAY", 0, gc
    488                 .getMinimum(Calendar.HOUR_OF_DAY));
    489         assertEquals("Wrong minimum value for HOUR",
    490                 0, gc.getMinimum(Calendar.HOUR));
    491 
    492         BitSet result = new BitSet();
    493         int[] min = { 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, -46800000,
    494                 0 };
    495         for (int i = 0; i < min.length; i++) {
    496             if (gc.getMinimum(i) != min[i])
    497                 result.set(i);
    498         }
    499         assertTrue("Wrong min for " + result, result.length() == 0);
    500     }
    501 
    502     /**
    503      * java.util.GregorianCalendar#isLeapYear(int)
    504      */
    505     public void test_isLeapYearI() {
    506         // Test for method boolean java.util.GregorianCalendar.isLeapYear(int)
    507         GregorianCalendar gc = new GregorianCalendar(1998, 11, 6);
    508         assertTrue("Returned incorrect value for leap year", !gc
    509                 .isLeapYear(1998));
    510         assertTrue("Returned incorrect value for leap year", gc
    511                 .isLeapYear(2000));
    512 
    513     }
    514 
    515     /**
    516      * java.util.GregorianCalendar#roll(int, int)
    517      */
    518     public void test_rollII() {
    519         // Test for method void java.util.GregorianCalendar.roll(int, int)
    520         GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER, 8,
    521                 2, 5, 0);
    522         gc.roll(Calendar.DAY_OF_MONTH, -1);
    523         assertTrue("Failed to roll DAY_OF_MONTH down by 1", gc
    524                 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 7, 2, 5,
    525                         0)));
    526         gc = new GregorianCalendar(1972, Calendar.OCTOBER, 8, 2, 5, 0);
    527         gc.roll(Calendar.DAY_OF_MONTH, 25);
    528         assertTrue("Failed to roll DAY_OF_MONTH up by 25", gc
    529                 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 2, 2, 5,
    530                         0)));
    531         gc = new GregorianCalendar(1972, Calendar.OCTOBER, 8, 2, 5, 0);
    532         gc.roll(Calendar.DAY_OF_MONTH, -10);
    533         assertTrue("Failed to roll DAY_OF_MONTH down by 10", gc
    534                 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 29, 2, 5,
    535                         0)));
    536     }
    537 
    538     /**
    539      * java.util.GregorianCalendar#roll(int, boolean)
    540      */
    541     public void test_rollIZ() {
    542         // Test for method void java.util.GregorianCalendar.roll(int, boolean)
    543         GregorianCalendar gc = new GregorianCalendar(1972, Calendar.OCTOBER,
    544                 13, 19, 9, 59);
    545         gc.roll(Calendar.DAY_OF_MONTH, false);
    546         assertTrue("Failed to roll day_of_month down", gc
    547                 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 12, 19,
    548                         9, 59)));
    549         gc = new GregorianCalendar(1972, Calendar.OCTOBER, 13, 19, 9, 59);
    550         gc.roll(Calendar.DAY_OF_MONTH, true);
    551         assertTrue("Failed to roll day_of_month up", gc
    552                 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 14, 19,
    553                         9, 59)));
    554         gc = new GregorianCalendar(1972, Calendar.OCTOBER, 31, 19, 9, 59);
    555         gc.roll(Calendar.DAY_OF_MONTH, true);
    556         assertTrue("Failed to roll day_of_month up", gc
    557                 .equals(new GregorianCalendar(1972, Calendar.OCTOBER, 1, 19, 9,
    558                         59)));
    559 
    560         GregorianCalendar cal = new GregorianCalendar();
    561         int result;
    562         try {
    563             cal.roll(Calendar.ZONE_OFFSET, true);
    564             result = 0;
    565         } catch (IllegalArgumentException e) {
    566             result = 1;
    567         }
    568         assertEquals("ZONE_OFFSET roll", 1, result);
    569         try {
    570             cal.roll(Calendar.DST_OFFSET, true);
    571             result = 0;
    572         } catch (IllegalArgumentException e) {
    573             result = 1;
    574         }
    575         assertEquals("ZONE_OFFSET roll", 1, result);
    576 
    577         cal.set(2004, Calendar.DECEMBER, 31, 5, 0, 0);
    578         cal.roll(Calendar.WEEK_OF_YEAR, true);
    579         assertEquals("Wrong year: " + cal.getTime(), 2004, cal
    580                 .get(Calendar.YEAR));
    581         assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal
    582                 .get(Calendar.MONTH));
    583         assertEquals("Wrong date: " + cal.getTime(), 9, cal.get(Calendar.DATE));
    584 
    585         // Regression for HARMONY-4372
    586         cal.set(1994, 11, 30, 5, 0, 0);
    587         cal.setMinimalDaysInFirstWeek(4);
    588         cal.roll(Calendar.WEEK_OF_YEAR, true);
    589         assertEquals("Wrong year: " + cal.getTime(), 1994, cal
    590                 .get(Calendar.YEAR));
    591         assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal
    592                 .get(Calendar.MONTH));
    593         assertEquals("Wrong date: " + cal.getTime(), 7, cal.get(Calendar.DATE));
    594 
    595         cal.roll(Calendar.WEEK_OF_YEAR, true);
    596         assertEquals("Wrong year: " + cal.getTime(), 1994, cal
    597                 .get(Calendar.YEAR));
    598         assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal
    599                 .get(Calendar.MONTH));
    600         assertEquals("Wrong date: " + cal.getTime(), 14, cal.get(Calendar.DATE));
    601 
    602         cal.roll(Calendar.WEEK_OF_YEAR, false);
    603         assertEquals("Wrong year: " + cal.getTime(), 1994, cal
    604                 .get(Calendar.YEAR));
    605         assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal
    606                 .get(Calendar.MONTH));
    607         assertEquals("Wrong date: " + cal.getTime(), 7, cal.get(Calendar.DATE));
    608 
    609         cal.roll(Calendar.WEEK_OF_YEAR, false);
    610         assertEquals("Wrong year: " + cal.getTime(), 1994, cal
    611                 .get(Calendar.YEAR));
    612         assertEquals("Wrong month: " + cal.getTime(), Calendar.DECEMBER, cal
    613                 .get(Calendar.MONTH));
    614         assertEquals("Wrong date: " + cal.getTime(), 30, cal.get(Calendar.DATE));
    615 
    616         cal.roll(Calendar.WEEK_OF_YEAR, false);
    617         assertEquals("Wrong year: " + cal.getTime(), 1994, cal
    618                 .get(Calendar.YEAR));
    619         assertEquals("Wrong month: " + cal.getTime(), Calendar.DECEMBER, cal
    620                 .get(Calendar.MONTH));
    621         assertEquals("Wrong date: " + cal.getTime(), 23, cal.get(Calendar.DATE));
    622 
    623         // Regression for HARMONY-4510
    624         cal.set(1999, Calendar.DECEMBER, 31, 23, 59, 59);
    625         cal.roll(GregorianCalendar.WEEK_OF_YEAR, true);
    626         assertEquals("Wrong year: " + cal.getTime(), 1999, cal
    627                 .get(Calendar.YEAR));
    628         assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal
    629                 .get(Calendar.MONTH));
    630         assertEquals("Wrong date: " + cal.getTime(), 8, cal.get(Calendar.DATE));
    631         cal.roll(GregorianCalendar.WEEK_OF_YEAR, false);
    632         assertEquals("Wrong year: " + cal.getTime(), 1999, cal
    633                 .get(Calendar.YEAR));
    634         assertEquals("Wrong month: " + cal.getTime(), Calendar.DECEMBER, cal
    635                 .get(Calendar.MONTH));
    636         assertEquals("Wrong date: " + cal.getTime(), 31, cal.get(Calendar.DATE));
    637     }
    638 
    639     /**
    640      * java.util.GregorianCalendar#setGregorianChange(java.util.Date)
    641      */
    642     public void test_setGregorianChangeLjava_util_Date() {
    643         // Test for method void
    644         // java.util.GregorianCalendar.setGregorianChange(java.util.Date)
    645         GregorianCalendar gc1 = new GregorianCalendar(1582, Calendar.OCTOBER,
    646                 4, 0, 0);
    647         GregorianCalendar gc2 = new GregorianCalendar(1972, Calendar.OCTOBER,
    648                 13, 0, 0);
    649         gc1.setGregorianChange(gc2.getTime());
    650         assertTrue("Returned incorrect value", gc2.getTime().equals(
    651                 gc1.getGregorianChange()));
    652     }
    653 
    654     /**
    655      * java.util.GregorianCalendar#clone()
    656      */
    657     public void test_clone() {
    658 
    659         // Regression for HARMONY-498
    660         GregorianCalendar gCalend = new GregorianCalendar();
    661 
    662         gCalend.set(Calendar.MILLISECOND, 0);
    663         int dayOfMonth = gCalend.get(Calendar.DAY_OF_MONTH);
    664 
    665         // create clone object and change date
    666         GregorianCalendar gCalendClone = (GregorianCalendar) gCalend.clone();
    667         gCalendClone.add(Calendar.DATE, 1);
    668 
    669         assertEquals("Before", dayOfMonth, gCalend.get(Calendar.DAY_OF_MONTH));
    670         gCalend.set(Calendar.MILLISECOND, 0);//changes nothing
    671         assertEquals("After", dayOfMonth, gCalend.get(Calendar.DAY_OF_MONTH));
    672     }
    673 
    674     /**
    675      * java.util.GregorianCalendar#getMinimalDaysInFirstWeek()
    676      */
    677     public void test_getMinimalDaysInFirstWeek() {
    678         // Regression for Harmony-1037
    679         // Some non-bug differences below because of different CLDR data of Harmony
    680         GregorianCalendar g = new GregorianCalendar(TimeZone
    681                 .getTimeZone("Europe/London"), new Locale("en", "GB"));
    682         int minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek();
    683         assertEquals(4, minimalDaysInFirstWeek);
    684 
    685         g = new GregorianCalendar(TimeZone.getTimeZone("Europe/London"),
    686                 new Locale("fr"));
    687         minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek();
    688         assertEquals(4, minimalDaysInFirstWeek);
    689 
    690         g = new GregorianCalendar(TimeZone.getTimeZone("Europe/London"),
    691                 new Locale("fr", "CA"));
    692         minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek();
    693         assertEquals(1, minimalDaysInFirstWeek);
    694 
    695     }
    696 
    697     /**
    698      * java.util.GregorianCalendar#computeTime()
    699      */
    700     public void test_computeTime() {
    701         // Regression for Harmony-493
    702         GregorianCalendar g = new GregorianCalendar(
    703                 TimeZone.getTimeZone("Europe/London"),
    704                 new Locale("en", "GB")
    705         );
    706         g.clear();
    707         g.set(2006, Calendar.MARCH, 26, 01, 50, 00);
    708         assertEquals(1143337800000L, g.getTimeInMillis());
    709 
    710         GregorianCalendar g1 = new GregorianCalendar(
    711                 TimeZone.getTimeZone("Europe/Moscow"));
    712         g1.clear();
    713         g1.set(2006, Calendar.MARCH, 26, 02, 20, 00);
    714         assertEquals(1143328800000L, g1.getTimeInMillis());
    715         assertEquals(3, g1.get(Calendar.HOUR_OF_DAY));
    716         assertEquals(20, g1.get(Calendar.MINUTE));
    717 
    718         g1.clear();
    719         g1.set(2006, Calendar.OCTOBER, 29, 02, 50, 00);
    720         assertEquals(1162079400000L, g1.getTimeInMillis());
    721         assertEquals(2, g1.get(Calendar.HOUR_OF_DAY));
    722         assertEquals(50, g1.get(Calendar.MINUTE));
    723         // End of regression test
    724     }
    725 
    726     /**
    727      * java.util.GregorianCalendar#get(int)
    728      */
    729     @SuppressWarnings("deprecation")
    730     public void test_getI() {
    731         // Regression test for HARMONY-2959
    732         Date date = new Date(Date.parse("Jan 15 00:00:01 GMT 2000"));
    733         GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    734         gc.setGregorianChange(date);
    735         gc.setTimeInMillis(Date.parse("Dec 24 00:00:01 GMT 2000"));
    736         assertEquals(346, gc.get(Calendar.DAY_OF_YEAR));
    737 
    738         // Regression test for HARMONY-3003
    739         date = new Date(Date.parse("Feb 28 00:00:01 GMT 2000"));
    740         gc = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    741         gc.setGregorianChange(date);
    742         gc.setTimeInMillis(Date.parse("Dec 1 00:00:01 GMT 2000"));
    743         assertEquals(1, gc.get(Calendar.DAY_OF_MONTH));
    744         assertEquals(11, gc.get(Calendar.MONTH));
    745 
    746         // Regression test for HARMONY-4513
    747         gc = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    748         gc.set(1582, Calendar.OCTOBER, 15, 0, 0, 0);
    749         // reset millisecond to zero in order to be the same time as cutover
    750         gc.set(Calendar.MILLISECOND, 0);
    751         assertEquals(0, gc.get(Calendar.MILLISECOND));
    752         assertEquals(1582, gc.get(Calendar.YEAR));
    753         assertEquals(Calendar.OCTOBER, gc.get(Calendar.MONTH));
    754         assertEquals(15, gc.get(Calendar.DAY_OF_MONTH));
    755         assertEquals(0, gc.get(Calendar.HOUR_OF_DAY));
    756         assertEquals(0, gc.get(Calendar.MINUTE));
    757         assertEquals(0, gc.get(Calendar.SECOND));
    758         gc.set(1582, Calendar.OCTOBER, 14, 0, 0, 0);
    759         assertEquals(24, gc.get(Calendar.DAY_OF_MONTH));
    760     }
    761 }
    762