1 /*
2  * Copyright (c) 2014, 2020, 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package test.java.time.temporal;
26 
27 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
28 import static org.testng.Assert.assertEquals;
29 import static org.testng.Assert.assertTrue;
30 
31 import java.time.DayOfWeek;
32 import java.time.LocalDate;
33 import java.time.LocalTime;
34 import java.time.MonthDay;
35 import java.time.OffsetDateTime;
36 import java.time.Year;
37 import java.time.chrono.ThaiBuddhistDate;
38 import java.time.temporal.ChronoUnit;
39 import java.time.temporal.IsoFields;
40 import java.time.temporal.TemporalField;
41 import java.time.temporal.ValueRange;
42 import java.time.temporal.WeekFields;
43 
44 import org.testng.annotations.DataProvider;
45 import org.testng.annotations.Test;
46 
47 /**
48  * Test.
49  */
50 @Test
51 public class TestIsoWeekFields {
52 
53     @DataProvider(name = "fields")
data_Fields()54     Object[][] data_Fields() {
55         return new Object[][] {
56                 {IsoFields.WEEK_OF_WEEK_BASED_YEAR, IsoFields.WEEK_BASED_YEAR},
57                 {WeekFields.ISO.weekOfWeekBasedYear(), WeekFields.ISO.weekBasedYear()},
58         };
59     }
60 
61     //-----------------------------------------------------------------------
62     // WEEK_OF_WEEK_BASED_YEAR
63     //-----------------------------------------------------------------------
64     @Test(dataProvider = "fields")
test_WOWBY_basics(TemporalField weekField, TemporalField yearField)65     public void test_WOWBY_basics(TemporalField weekField, TemporalField yearField) {
66         assertEquals(weekField.isDateBased(), true);
67         assertEquals(weekField.isTimeBased(), false);
68         assertEquals(weekField.getBaseUnit(), ChronoUnit.WEEKS);
69         assertEquals(weekField.getRangeUnit(), IsoFields.WEEK_BASED_YEARS);
70     }
71 
72     @Test(dataProvider = "fields")
test_WOWBY_isSupportedBy(TemporalField weekField, TemporalField yearField)73     public void test_WOWBY_isSupportedBy(TemporalField weekField, TemporalField yearField) {
74         assertEquals(weekField.isSupportedBy(LocalTime.NOON), false);
75         assertEquals(weekField.isSupportedBy(MonthDay.of(2, 1)), false);
76         assertEquals(weekField.isSupportedBy(LocalDate.MIN), true);
77         assertEquals(weekField.isSupportedBy(OffsetDateTime.MAX), true);
78     }
79 
80     @Test
test_WOWBY_isSupportedBy_fieldsDiffer()81     public void test_WOWBY_isSupportedBy_fieldsDiffer() {
82         assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.isSupportedBy(ThaiBuddhistDate.now()), false);
83         assertEquals(WeekFields.ISO.weekOfWeekBasedYear().isSupportedBy(ThaiBuddhistDate.now()), true);
84     }
85 
86     @Test(dataProvider = "fields")
test_WOWBY_range(TemporalField weekField, TemporalField yearField)87     public void test_WOWBY_range(TemporalField weekField, TemporalField yearField) {
88         assertEquals(weekField.range(), ValueRange.of(1, 52, 53));
89     }
90 
91     @Test(dataProvider = "fields")
test_WOWBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField)92     public void test_WOWBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField) {
93         assertEquals(weekField.rangeRefinedBy(LocalDate.of(2012, 12, 31)), ValueRange.of(1, 52));
94         assertEquals(weekField.rangeRefinedBy(LocalDate.of(2013, 12, 29)), ValueRange.of(1, 52));
95         assertEquals(weekField.rangeRefinedBy(LocalDate.of(2013, 12, 30)), ValueRange.of(1, 52));
96         assertEquals(weekField.rangeRefinedBy(LocalDate.of(2014, 12, 28)), ValueRange.of(1, 52));
97         assertEquals(weekField.rangeRefinedBy(LocalDate.of(2014, 12, 29)), ValueRange.of(1, 53));
98         assertEquals(weekField.rangeRefinedBy(LocalDate.of(2016, 1, 3)), ValueRange.of(1, 53));
99         assertEquals(weekField.rangeRefinedBy(LocalDate.of(2016, 1, 4)), ValueRange.of(1, 52));
100     }
101 
102     //-----------------------------------------------------------------------
103     // WEEK_BASED_YEAR
104     //-----------------------------------------------------------------------
105     @Test(dataProvider = "fields")
test_WBY_basics(TemporalField weekField, TemporalField yearField)106     public void test_WBY_basics(TemporalField weekField, TemporalField yearField) {
107         assertEquals(yearField.isDateBased(), true);
108         assertEquals(yearField.isTimeBased(), false);
109         assertEquals(yearField.getBaseUnit(), IsoFields.WEEK_BASED_YEARS);
110         assertEquals(yearField.getRangeUnit(), ChronoUnit.FOREVER);
111     }
112 
113     @Test(dataProvider = "fields")
test_WBY_isSupportedBy(TemporalField weekField, TemporalField yearField)114     public void test_WBY_isSupportedBy(TemporalField weekField, TemporalField yearField) {
115         assertEquals(yearField.isSupportedBy(LocalTime.NOON), false);
116         assertEquals(yearField.isSupportedBy(MonthDay.of(2, 1)), false);
117         assertEquals(yearField.isSupportedBy(LocalDate.MIN), true);
118         assertEquals(yearField.isSupportedBy(OffsetDateTime.MAX), true);
119     }
120 
121     @Test
test_WBY_isSupportedBy_ISO()122     public void test_WBY_isSupportedBy_ISO() {
123         assertEquals(IsoFields.WEEK_BASED_YEAR.isSupportedBy(ThaiBuddhistDate.now()), false);
124     }
125 
126     @Test
test_Unit_isSupportedBy_ISO()127     public void test_Unit_isSupportedBy_ISO() {
128         assertEquals(IsoFields.WEEK_BASED_YEARS.isSupportedBy(LocalDate.now()),true);
129         assertEquals(IsoFields.WEEK_BASED_YEARS.isSupportedBy(ThaiBuddhistDate.now()),false);
130         assertEquals(IsoFields.QUARTER_YEARS.isSupportedBy(LocalDate.now()),true);
131         assertEquals(IsoFields.QUARTER_YEARS.isSupportedBy(ThaiBuddhistDate.now()),false);
132     }
133 
134     @Test(dataProvider = "fields")
test_WBY_range(TemporalField weekField, TemporalField yearField)135     public void test_WBY_range(TemporalField weekField, TemporalField yearField) {
136         assertEquals(yearField.range(), ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE));
137     }
138 
139     @Test(dataProvider = "fields")
test_WBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField)140     public void test_WBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField) {
141         assertEquals(yearField.rangeRefinedBy(LocalDate.of(2012, 12, 31)), ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE));
142     }
143 
144     //-----------------------------------------------------------------------
145     @Test(dataProvider = "fields")
test_getFrom(TemporalField weekField, TemporalField yearField)146     public void test_getFrom(TemporalField weekField, TemporalField yearField) {
147         // tests every day from 2011 to 2016 inclusive
148         LocalDate date = LocalDate.of(2011, 1, 3);
149         int wby = 2011;
150         int week = 1;
151         int dow = 1;
152         for (int i = 1; i <= ((52 + 52 + 52 + 52 + 53 + 52) * 7); i++) {
153             assertEquals(yearField.getFrom(date), wby);
154             assertEquals(weekField.getFrom(date), week);
155             assertEquals(DAY_OF_WEEK.getFrom(date), dow);
156             if (dow == 7) {
157                 dow = 1;
158                 week++;
159             } else {
160                 dow++;
161             }
162             if (week > wbyLen(wby)) {
163                 week = 1;
164                 wby++;
165             }
166             date = date.plusDays(1);
167         }
168         assertEquals(yearField.getFrom(date), 2017);
169         assertEquals(weekField.getFrom(date), 1);
170         assertEquals(DAY_OF_WEEK.getFrom(date), 1);
171     }
172 
173     @Test(dataProvider = "fields")
test_adjustInto_dow(TemporalField weekField, TemporalField yearField)174     public void test_adjustInto_dow(TemporalField weekField, TemporalField yearField) {
175         // tests every day from 2012 to 2016 inclusive
176         LocalDate date = LocalDate.of(2012, 1, 2);
177         int wby = 2012;
178         int week = 1;
179         int dow = 1;
180         for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
181             for (int j = 1; j <= 7; j++) {
182                 LocalDate adjusted = DAY_OF_WEEK.adjustInto(date, j);
183                 assertEquals(adjusted.get(DAY_OF_WEEK), j);
184                 assertEquals(adjusted.get(weekField), week);
185                 assertEquals(adjusted.get(yearField), wby);
186             }
187             if (dow == 7) {
188                 dow = 1;
189                 week++;
190             } else {
191                 dow++;
192             }
193             if (week > wbyLen(wby)) {
194                 week = 1;
195                 wby++;
196             }
197             date = date.plusDays(1);
198         }
199     }
200 
201     @Test(dataProvider = "fields")
test_adjustInto_week(TemporalField weekField, TemporalField yearField)202     public void test_adjustInto_week(TemporalField weekField, TemporalField yearField) {
203         // tests every day from 2012 to 2016 inclusive
204         LocalDate date = LocalDate.of(2012, 1, 2);
205         int wby = 2012;
206         int week = 1;
207         int dow = 1;
208         for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
209             int weeksInYear = (wby == 2015 ? 53 : 52);
210             for (int j = 1; j <= weeksInYear; j++) {
211                 LocalDate adjusted = weekField.adjustInto(date, j);
212                 assertEquals(adjusted.get(weekField), j);
213                 assertEquals(adjusted.get(DAY_OF_WEEK), dow);
214                 assertEquals(adjusted.get(yearField), wby);
215             }
216             if (dow == 7) {
217                 dow = 1;
218                 week++;
219             } else {
220                 dow++;
221             }
222             if (week > wbyLen(wby)) {
223                 week = 1;
224                 wby++;
225             }
226             date = date.plusDays(1);
227         }
228     }
229 
230     @Test(dataProvider = "fields")
test_adjustInto_wby(TemporalField weekField, TemporalField yearField)231     public void test_adjustInto_wby(TemporalField weekField, TemporalField yearField) {
232         // tests every day from 2012 to 2016 inclusive
233         LocalDate date = LocalDate.of(2012, 1, 2);
234         int wby = 2012;
235         int week = 1;
236         int dow = 1;
237         for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
238             for (int j = 2004; j <= 2015; j++) {
239                 LocalDate adjusted = yearField.adjustInto(date, j);
240                 assertEquals(adjusted.get(yearField), j);
241                 assertEquals(adjusted.get(DAY_OF_WEEK), dow);
242                 assertEquals(adjusted.get(weekField), (week == 53 && wbyLen(j) == 52 ? 52 : week), "" + date + " " + adjusted);
243             }
244             if (dow == 7) {
245                 dow = 1;
246                 week++;
247             } else {
248                 dow++;
249             }
250             if (week > wbyLen(wby)) {
251                 week = 1;
252                 wby++;
253             }
254             date = date.plusDays(1);
255         }
256     }
257 
258     @Test(dataProvider = "fields")
test_addTo_weekBasedYears(TemporalField weekField, TemporalField yearField)259     public void test_addTo_weekBasedYears(TemporalField weekField, TemporalField yearField) {
260         // tests every day from 2012 to 2016 inclusive
261         LocalDate date = LocalDate.of(2012, 1, 2);
262         int wby = 2012;
263         int week = 1;
264         int dow = 1;
265         for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
266             for (int j = -5; j <= 5; j++) {
267                 LocalDate adjusted = IsoFields.WEEK_BASED_YEARS.addTo(date, j);
268                 assertEquals(adjusted.get(yearField), wby + j);
269                 assertEquals(adjusted.get(DAY_OF_WEEK), dow);
270                 assertEquals(adjusted.get(weekField), (week == 53 && wbyLen(wby + j) == 52 ? 52 : week), "" + date + " " + adjusted);
271             }
272             if (dow == 7) {
273                 dow = 1;
274                 week++;
275             } else {
276                 dow++;
277             }
278             if (week > wbyLen(wby)) {
279                 week = 1;
280                 wby++;
281             }
282             date = date.plusDays(1);
283         }
284     }
285 
286     @Test
test_ISOSingleton()287     public void test_ISOSingleton() {
288         assertTrue(WeekFields.ISO == WeekFields.of(DayOfWeek.MONDAY, 4));
289     }
290 
wbyLen(int wby)291     private int wbyLen(int wby) {
292         return (wby == 2004 || wby == 2009 || wby == 2015 || wby == 2020 ? 53 : 52);
293     }
294 
295 }
296