1 /*
 2  * Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4401223
27  * @summary Make sure that GregorianCalendar doesn't cause
28  *          IllegalArgumentException at some special situations which are
29  *          related to the Leap Year.
30  * @run junit bug4401223
31  */
32 
33 import java.util.Date;
34 import java.util.GregorianCalendar;
35 
36 import static java.util.GregorianCalendar.DATE;
37 import static java.util.GregorianCalendar.DAY_OF_YEAR;
38 import static java.util.GregorianCalendar.DECEMBER;
39 import static java.util.GregorianCalendar.FEBRUARY;
40 import static java.util.GregorianCalendar.MONTH;
41 import static java.util.GregorianCalendar.YEAR;
42 
43 import org.junit.jupiter.api.Test;
44 
45 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
46 import static org.junit.jupiter.api.Assertions.assertEquals;
47 
48 public class bug4401223 {
49 
50     // Ensure IAE not thrown for date: 12-29-00
51     @SuppressWarnings("deprecation")
52     @Test
53     public void checkExceptionTest() {
54         Date date = new Date(2000 - 1900, FEBRUARY, 29);
55         GregorianCalendar gc = new GregorianCalendar();
56         assertDoesNotThrow(() -> {
57             gc.setTime(date);
58             gc.setLenient(false);
59             gc.set(YEAR, 2001);
60         }, "Exception occurred for 2/29/00 & set(YEAR,2001)");
61     }
62 
63     // Ensure IAE not thrown for date: 12-31-00. Validate expected values.
64     @SuppressWarnings("deprecation")
65     @Test
66     public void checkExceptionAndValuesTest() {
67         Date date = new Date(2000 - 1900, DECEMBER, 31);
68         GregorianCalendar gc = new GregorianCalendar();
69         assertDoesNotThrow(() -> {
70             gc.setTime(date);
71             gc.setLenient(false);
72             gc.set(YEAR, 2001);
73         }, "Exception occurred for 12/31/00 & set(YEAR,2001)");
74 
75         String errMsg = "Wrong date,  got: " + gc.getTime();
76 
77         assertEquals(2001, gc.get(YEAR), errMsg);
78         assertEquals(DECEMBER, gc.get(MONTH), errMsg);
79         assertEquals(31, gc.get(DATE), errMsg);
80         assertEquals(365, gc.get(DAY_OF_YEAR), errMsg);
81     }
82 }