• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
setUp()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
tearDown()45     public void tearDown() throws Exception {
46         Locale.setDefault(defaultLocale);
47         super.tearDown();
48     }
49 
50     /**
51      * java.util.GregorianCalendar#GregorianCalendar()
52      */
test_Constructor()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      */
test_ConstructorIII()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      */
test_ConstructorIIIII()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      */
test_ConstructorIIIIII()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      */
test_ConstructorLjava_util_Locale()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      */
test_ConstructorLjava_util_TimeZone()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      */
test_ConstructorLjava_util_TimeZoneLjava_util_Locale()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      */
test_addII()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      */
test_equalsLjava_lang_Object()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      */
test_getActualMaximumI()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      */
test_getActualMinimumI()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      */
test_getGreatestMinimumI()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      */
test_getGregorianChange()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      */
test_getLeastMaximumI()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      */
test_getMaximumI()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      */
test_getMinimumI()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      */
test_isLeapYearI()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      */
test_rollII()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      */
test_rollIZ()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         // Android-changed: Bugfix for https://bugs.openjdk.java.net/browse/JDK-6902861. This
584         // returned 9 before Android O.
585         assertEquals("Wrong date: " + cal.getTime(), 2, cal.get(Calendar.DATE));
586 
587         // Regression for HARMONY-4372
588         cal.set(1994, 11, 30, 5, 0, 0);
589         cal.setMinimalDaysInFirstWeek(4);
590         cal.roll(Calendar.WEEK_OF_YEAR, true);
591         assertEquals("Wrong year: " + cal.getTime(), 1994, cal
592                 .get(Calendar.YEAR));
593         assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal
594                 .get(Calendar.MONTH));
595         assertEquals("Wrong date: " + cal.getTime(), 7, cal.get(Calendar.DATE));
596 
597         cal.roll(Calendar.WEEK_OF_YEAR, true);
598         assertEquals("Wrong year: " + cal.getTime(), 1994, cal
599                 .get(Calendar.YEAR));
600         assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal
601                 .get(Calendar.MONTH));
602         assertEquals("Wrong date: " + cal.getTime(), 14, cal.get(Calendar.DATE));
603 
604         cal.roll(Calendar.WEEK_OF_YEAR, false);
605         assertEquals("Wrong year: " + cal.getTime(), 1994, cal
606                 .get(Calendar.YEAR));
607         assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal
608                 .get(Calendar.MONTH));
609         assertEquals("Wrong date: " + cal.getTime(), 7, cal.get(Calendar.DATE));
610 
611         cal.roll(Calendar.WEEK_OF_YEAR, false);
612         assertEquals("Wrong year: " + cal.getTime(), 1994, cal
613                 .get(Calendar.YEAR));
614         assertEquals("Wrong month: " + cal.getTime(), Calendar.DECEMBER, cal
615                 .get(Calendar.MONTH));
616         assertEquals("Wrong date: " + cal.getTime(), 30, cal.get(Calendar.DATE));
617 
618         cal.roll(Calendar.WEEK_OF_YEAR, false);
619         assertEquals("Wrong year: " + cal.getTime(), 1994, cal
620                 .get(Calendar.YEAR));
621         assertEquals("Wrong month: " + cal.getTime(), Calendar.DECEMBER, cal
622                 .get(Calendar.MONTH));
623         assertEquals("Wrong date: " + cal.getTime(), 23, cal.get(Calendar.DATE));
624 
625         // Regression for HARMONY-4510
626         cal.set(1999, Calendar.DECEMBER, 31, 23, 59, 59);
627         cal.roll(GregorianCalendar.WEEK_OF_YEAR, true);
628         assertEquals("Wrong year: " + cal.getTime(), 1999, cal
629                 .get(Calendar.YEAR));
630         assertEquals("Wrong month: " + cal.getTime(), Calendar.JANUARY, cal
631                 .get(Calendar.MONTH));
632         assertEquals("Wrong date: " + cal.getTime(), 8, cal.get(Calendar.DATE));
633         cal.roll(GregorianCalendar.WEEK_OF_YEAR, false);
634         assertEquals("Wrong year: " + cal.getTime(), 1999, cal
635                 .get(Calendar.YEAR));
636         assertEquals("Wrong month: " + cal.getTime(), Calendar.DECEMBER, cal
637                 .get(Calendar.MONTH));
638         assertEquals("Wrong date: " + cal.getTime(), 31, cal.get(Calendar.DATE));
639     }
640 
641     /**
642      * java.util.GregorianCalendar#setGregorianChange(java.util.Date)
643      */
test_setGregorianChangeLjava_util_Date()644     public void test_setGregorianChangeLjava_util_Date() {
645         // Test for method void
646         // java.util.GregorianCalendar.setGregorianChange(java.util.Date)
647         GregorianCalendar gc1 = new GregorianCalendar(1582, Calendar.OCTOBER,
648                 4, 0, 0);
649         GregorianCalendar gc2 = new GregorianCalendar(1972, Calendar.OCTOBER,
650                 13, 0, 0);
651         gc1.setGregorianChange(gc2.getTime());
652         assertTrue("Returned incorrect value", gc2.getTime().equals(
653                 gc1.getGregorianChange()));
654     }
655 
656     /**
657      * java.util.GregorianCalendar#clone()
658      */
test_clone()659     public void test_clone() {
660 
661         // Regression for HARMONY-498
662         GregorianCalendar gCalend = new GregorianCalendar();
663 
664         gCalend.set(Calendar.MILLISECOND, 0);
665         int dayOfMonth = gCalend.get(Calendar.DAY_OF_MONTH);
666 
667         // create clone object and change date
668         GregorianCalendar gCalendClone = (GregorianCalendar) gCalend.clone();
669         gCalendClone.add(Calendar.DATE, 1);
670 
671         assertEquals("Before", dayOfMonth, gCalend.get(Calendar.DAY_OF_MONTH));
672         gCalend.set(Calendar.MILLISECOND, 0);//changes nothing
673         assertEquals("After", dayOfMonth, gCalend.get(Calendar.DAY_OF_MONTH));
674     }
675 
676     /**
677      * java.util.GregorianCalendar#getMinimalDaysInFirstWeek()
678      */
test_getMinimalDaysInFirstWeek()679     public void test_getMinimalDaysInFirstWeek() {
680         // Regression for Harmony-1037
681         // Some non-bug differences below because of different CLDR data of Harmony
682         GregorianCalendar g = new GregorianCalendar(TimeZone
683                 .getTimeZone("Europe/London"), new Locale("en", "GB"));
684         int minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek();
685         assertEquals(4, minimalDaysInFirstWeek);
686 
687         g = new GregorianCalendar(TimeZone.getTimeZone("Europe/London"),
688                 new Locale("fr"));
689         minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek();
690         assertEquals(4, minimalDaysInFirstWeek);
691 
692         g = new GregorianCalendar(TimeZone.getTimeZone("Europe/London"),
693                 new Locale("fr", "CA"));
694         minimalDaysInFirstWeek = g.getMinimalDaysInFirstWeek();
695         assertEquals(1, minimalDaysInFirstWeek);
696 
697     }
698 
699     /**
700      * java.util.GregorianCalendar#computeTime()
701      */
test_computeTime()702     public void test_computeTime() {
703         // Regression for Harmony-493
704         GregorianCalendar g = new GregorianCalendar(
705                 TimeZone.getTimeZone("Europe/London"),
706                 new Locale("en", "GB")
707         );
708         g.clear();
709         g.set(2006, Calendar.MARCH, 26, 01, 50, 00);
710         assertEquals(1143337800000L, g.getTimeInMillis());
711 
712         GregorianCalendar g1 = new GregorianCalendar(
713                 TimeZone.getTimeZone("Europe/Moscow"));
714         g1.clear();
715         g1.set(2006, Calendar.MARCH, 26, 02, 20, 00);
716         assertEquals(1143328800000L, g1.getTimeInMillis());
717         assertEquals(3, g1.get(Calendar.HOUR_OF_DAY));
718         assertEquals(20, g1.get(Calendar.MINUTE));
719 
720         g1.clear();
721         g1.set(2006, Calendar.OCTOBER, 29, 02, 50, 00);
722         assertEquals(1162079400000L, g1.getTimeInMillis());
723         assertEquals(2, g1.get(Calendar.HOUR_OF_DAY));
724         assertEquals(50, g1.get(Calendar.MINUTE));
725         // End of regression test
726     }
727 
728     /**
729      * java.util.GregorianCalendar#get(int)
730      */
731     @SuppressWarnings("deprecation")
test_getI()732     public void test_getI() {
733         // Regression test for HARMONY-2959
734         Date date = new Date(Date.parse("Jan 15 00:00:01 GMT 2000"));
735         GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
736         gc.setGregorianChange(date);
737         gc.setTimeInMillis(Date.parse("Dec 24 00:00:01 GMT 2000"));
738         assertEquals(346, gc.get(Calendar.DAY_OF_YEAR));
739 
740         // Regression test for HARMONY-3003
741         date = new Date(Date.parse("Feb 28 00:00:01 GMT 2000"));
742         gc = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
743         gc.setGregorianChange(date);
744         gc.setTimeInMillis(Date.parse("Dec 1 00:00:01 GMT 2000"));
745         assertEquals(1, gc.get(Calendar.DAY_OF_MONTH));
746         assertEquals(11, gc.get(Calendar.MONTH));
747 
748         // Regression test for HARMONY-4513
749         gc = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
750         gc.set(1582, Calendar.OCTOBER, 15, 0, 0, 0);
751         // reset millisecond to zero in order to be the same time as cutover
752         gc.set(Calendar.MILLISECOND, 0);
753         assertEquals(0, gc.get(Calendar.MILLISECOND));
754         assertEquals(1582, gc.get(Calendar.YEAR));
755         assertEquals(Calendar.OCTOBER, gc.get(Calendar.MONTH));
756         assertEquals(15, gc.get(Calendar.DAY_OF_MONTH));
757         assertEquals(0, gc.get(Calendar.HOUR_OF_DAY));
758         assertEquals(0, gc.get(Calendar.MINUTE));
759         assertEquals(0, gc.get(Calendar.SECOND));
760         gc.set(1582, Calendar.OCTOBER, 14, 0, 0, 0);
761         assertEquals(24, gc.get(Calendar.DAY_OF_MONTH));
762     }
763 }
764