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