< prev index next >

test/jdk/java/util/Calendar/bug4401223.java

Print this page

 1 /*
 2  * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4401223
27  * @summary Make sure that GregorianCalendar doesn't cause IllegalArgumentException at some special situations which are related to the Leap Year.
28  * @library /java/text/testlib


29  */
30 
31 import java.util.Date;
32 import java.util.GregorianCalendar;
33 
34 import static java.util.GregorianCalendar.*;





35 
36 public class bug4401223 extends IntlTest {
37 
38     public void Test4401223a() {
39         int status = 0;
40         String s = null;
41 
42         try {
43             @SuppressWarnings("deprecation")
44             Date date = new Date(2000 - 1900, FEBRUARY, 29);
45             GregorianCalendar gc = new GregorianCalendar();





46             gc.setTime(date);
47             gc.setLenient(false);
48             gc.set(YEAR, 2001);
49             s = "02/29/00 & set(YEAR,2001) = " + gc.getTime().toString();
50         } catch (Exception ex) {
51             status++;
52             s = "Exception occurred for 2/29/00 & set(YEAR,2001): " + ex;
53         }
54         if (status > 0) {
55             errln(s);
56         } else {
57             logln(s);
58         }
59     }
60 
61     public void Test4401223b() {
62         int status = 0;
63         String s = null;
64 
65         try {
66             @SuppressWarnings("deprecation")
67             Date date = new Date(2000 - 1900, DECEMBER, 31);
68             GregorianCalendar gc = new GregorianCalendar();
69             gc.setTime(date);
70             gc.setLenient(false);
71             gc.set(YEAR, 2001);

72 
73             if (gc.get(YEAR) != 2001
74                     || gc.get(MONTH) != DECEMBER
75                     || gc.get(DATE) != 31
76                     || gc.get(DAY_OF_YEAR) != 365) {
77                 status++;
78                 s = "Wrong Date : 12/31/00 & set(YEAR,2001) ---> " + gc.getTime().toString();
79             } else {
80                 s = "12/31/00 & set(YEAR,2001) = " + gc.getTime().toString();
81             }
82         } catch (Exception ex) {
83             status++;
84             s = "Exception occurred for 12/31/00 & set(YEAR,2001) : " + ex;
85         }
86         if (status > 0) {
87             errln(s);
88         } else {
89             logln(s);
90         }
91     }
92 
93     public static void main(String[] args) throws Exception {
94         new bug4401223().run(args);


95     }
96 }

 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 }
< prev index next >