1 /*
2  * Copyright (c) 2010, 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 4267450
27  * @summary Unit test for week date support
28  */
29 
30 import java.text.*;
31 import java.util.*;
32 import static java.util.GregorianCalendar.*;
33 
34 public class WeekDateTest {
35 
36     // Week dates are in the ISO numbering for day-of-week.
37     static int[][][] data = {
38         // Calendar year-date, Week year-date
39         {{ 2005, 01, 01}, { 2004, 53, 6}},
40         {{ 2005, 01, 02}, { 2004, 53, 7}},
41         {{ 2005, 12, 31}, { 2005, 52, 6}},
42         {{ 2007, 01, 01}, { 2007, 01, 1}},
43         {{ 2007, 12, 30}, { 2007, 52, 7}},
44         {{ 2007, 12, 31}, { 2008, 01, 1}},
45         {{ 2008, 01, 01}, { 2008, 01, 2}},
46         {{ 2008, 12, 29}, { 2009, 01, 1}},
47         {{ 2008, 12, 31}, { 2009, 01, 3}},
48         {{ 2009, 01, 01}, { 2009, 01, 4}},
49         {{ 2009, 12, 31}, { 2009, 53, 4}},
50         {{ 2010, 01, 03}, { 2009, 53, 7}},
51         {{ 2009, 12, 31}, { 2009, 53, 4}},
52         {{ 2010, 01, 01}, { 2009, 53, 5}},
53         {{ 2010, 01, 02}, { 2009, 53, 6}},
54         {{ 2010, 01, 03}, { 2009, 53, 7}},
55         {{ 2008, 12, 28}, { 2008, 52, 7}},
56         {{ 2008, 12, 29}, { 2009, 01, 1}},
57         {{ 2008, 12, 30}, { 2009, 01, 2}},
58         {{ 2008, 12, 31}, { 2009, 01, 3}},
59         {{ 2009, 01, 01}, { 2009, 01, 4}}
60     };
61 
62     // Data for leniency test
63     static final int[][][] leniencyData = {
64         {{ 2008, 12, 28}, { 2009,  0, 7}},
65         {{ 2008, 12, 21}, { 2009, -1, 7}},
66         {{ 2009,  1,  4}, { 2008, 53, 7}},
67     };
68 
69     static final int[][] invalidData = {
70         { 2010, -1,  1},
71         { 2010, 00,  1},
72         { 2010, 55,  1},
73         { 2010, 03,  0},
74         { 2010, 04,  8},
75         { 2010, 04, 19},
76         { 2010, 05, -1},
77     };
78 
main(String[] args)79     public static void main(String[] args) {
80         GregorianCalendar cal = newCalendar();
81         for (int[][] dates : data) {
82             int[] expected = dates[0];
83             int[] weekDate = dates[1];
84             // Convert ISO 8601 day-of-week to Calendar.DAY_OF_WEEK.
85             int dayOfWeek = getCalendarDayOfWeek(weekDate[2]);
86 
87             cal.clear();
88             cal.setWeekDate(weekDate[0], weekDate[1], dayOfWeek);
89             if (cal.get(YEAR) != expected[0]
90                 || cal.get(MONTH)+1 != expected[1]
91                 || cal.get(DAY_OF_MONTH) != expected[2]) {
92                 String s = String.format("got=%4d-%02d-%02d, expected=%4d-%02d-%02d",
93                                cal.get(YEAR), cal.get(MONTH)+1, cal.get(DAY_OF_MONTH),
94                                expected[0], expected[1], expected[2]);
95                 throw new RuntimeException(s);
96             }
97             if (cal.getWeekYear() != weekDate[0]
98                 || cal.get(WEEK_OF_YEAR) != weekDate[1]
99                 || cal.get(DAY_OF_WEEK) != dayOfWeek) {
100                 String s = String.format(
101                     "got=%4d-W%02d-%d, expected=%4d-W%02d-%d (not ISO day-of-week)",
102                     cal.getWeekYear(), cal.get(WEEK_OF_YEAR), cal.get(DAY_OF_WEEK),
103                     weekDate[0], weekDate[1], dayOfWeek);
104                 throw new RuntimeException(s);
105             }
106         }
107 
108         // Test getWeeksInWeekYear().
109         // If we avoid the first week of January and the last week of
110         // December, getWeeksInWeekYear() and
111         // getActualMaximum(WEEK_OF_YEAR) values should be the same.
112         for (int year = 2000; year <= 2100; year++) {
113             cal.clear();
114             cal.set(year, JUNE, 1);
115             int n = cal.getWeeksInWeekYear();
116             if (n != cal.getActualMaximum(WEEK_OF_YEAR)) {
117                 String s = String.format("getWeeksInWeekYear() = %d, "
118                                          + "getActualMaximum(WEEK_OF_YEAR) = %d%n",
119                                          n, cal.getActualMaximum(WEEK_OF_YEAR));
120                 throw new RuntimeException(s);
121             }
122 
123             cal.setWeekDate(cal.getWeekYear(), 1, MONDAY);
124             if (cal.getWeeksInWeekYear() != n) {
125                 String s = String.format("first day: got %d, expected %d%n",
126                                          cal.getWeeksInWeekYear(), n);
127                 throw new RuntimeException(s);
128             }
129 
130             cal.setWeekDate(cal.getWeekYear(), n, SUNDAY);
131             if (cal.getWeeksInWeekYear() != n) {
132                 String s = String.format("last day: got %d, expected %d%n",
133                                          cal.getWeeksInWeekYear(), n);
134                 throw new RuntimeException(s);
135             }
136         }
137 
138         // Test lenient mode with out of range values.
139         for (int[][] dates : leniencyData) {
140             int[] expected = dates[0];
141             int[] weekDate = dates[1];
142             // Convert ISO 8601 day-of-week to Calendar.DAY_OF_WEEK.
143             int dayOfWeek = getCalendarDayOfWeek(weekDate[2]);
144 
145             cal.clear();
146             cal.setWeekDate(weekDate[0], weekDate[1], dayOfWeek);
147             if (cal.get(YEAR) != expected[0]
148                 || cal.get(MONTH)+1 != expected[1]
149                 || cal.get(DAY_OF_MONTH) != expected[2]) {
150                 String s = String.format("got=%4d-%02d-%02d, expected=%4d-%02d-%02d",
151                                cal.get(YEAR), cal.get(MONTH)+1, cal.get(DAY_OF_MONTH),
152                                expected[0], expected[1], expected[2]);
153                 throw new RuntimeException(s);
154             }
155         }
156 
157         // Test non-lenient mode
158         cal.setLenient(false);
159         for (int[] date : invalidData) {
160             cal.clear();
161             try {
162                 // Use the raw dayOfWeek value as invalid data
163                 cal.setWeekDate(date[0], date[1], date[2]);
164                 String s = String.format("didn't throw an IllegalArgumentException with"
165                                          + " %d, %d, %d",date[0], date[1], date[2]);
166                 throw new RuntimeException(s);
167             } catch (IllegalArgumentException e) {
168                 // OK
169             }
170         }
171     }
172 
newCalendar()173     private static GregorianCalendar newCalendar() {
174         // Use GMT to avoid any surprises related DST transitions.
175         GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
176         if (!cal.isWeekDateSupported()) {
177             throw new RuntimeException("Week dates not supported");
178         }
179         // Setup the ISO 8601 compatible parameters
180         cal.setFirstDayOfWeek(MONDAY);
181         cal.setMinimalDaysInFirstWeek(4);
182         return cal;
183     }
184 
getCalendarDayOfWeek(int isoDayOfWeek)185     private static int getCalendarDayOfWeek(int isoDayOfWeek) {
186         return (isoDayOfWeek == 7) ? SUNDAY : isoDayOfWeek + 1;
187     }
188 }
189