1 /* 2 * Copyright (c) 2014, 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 30 import java.time.LocalDate; 31 import java.time.LocalTime; 32 import java.time.MonthDay; 33 import java.time.OffsetDateTime; 34 import java.time.Year; 35 import java.time.chrono.ThaiBuddhistDate; 36 import java.time.temporal.ChronoUnit; 37 import java.time.temporal.IsoFields; 38 import java.time.temporal.TemporalField; 39 import java.time.temporal.ValueRange; 40 import java.time.temporal.WeekFields; 41 42 import org.testng.annotations.DataProvider; 43 import org.testng.annotations.Test; 44 45 /** 46 * Test. 47 */ 48 @Test 49 public class TestIsoWeekFields { 50 51 @DataProvider(name = "fields") data_Fields()52 Object[][] data_Fields() { 53 return new Object[][] { 54 {IsoFields.WEEK_OF_WEEK_BASED_YEAR, IsoFields.WEEK_BASED_YEAR}, 55 {WeekFields.ISO.weekOfWeekBasedYear(), WeekFields.ISO.weekBasedYear()}, 56 }; 57 } 58 59 //----------------------------------------------------------------------- 60 // WEEK_OF_WEEK_BASED_YEAR 61 //----------------------------------------------------------------------- 62 @Test(dataProvider = "fields") test_WOWBY_basics(TemporalField weekField, TemporalField yearField)63 public void test_WOWBY_basics(TemporalField weekField, TemporalField yearField) { 64 assertEquals(weekField.isDateBased(), true); 65 assertEquals(weekField.isTimeBased(), false); 66 assertEquals(weekField.getBaseUnit(), ChronoUnit.WEEKS); 67 assertEquals(weekField.getRangeUnit(), IsoFields.WEEK_BASED_YEARS); 68 } 69 70 @Test(dataProvider = "fields") test_WOWBY_isSupportedBy(TemporalField weekField, TemporalField yearField)71 public void test_WOWBY_isSupportedBy(TemporalField weekField, TemporalField yearField) { 72 assertEquals(weekField.isSupportedBy(LocalTime.NOON), false); 73 assertEquals(weekField.isSupportedBy(MonthDay.of(2, 1)), false); 74 assertEquals(weekField.isSupportedBy(LocalDate.MIN), true); 75 assertEquals(weekField.isSupportedBy(OffsetDateTime.MAX), true); 76 } 77 78 @Test test_WOWBY_isSupportedBy_fieldsDiffer()79 public void test_WOWBY_isSupportedBy_fieldsDiffer() { 80 assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.isSupportedBy(ThaiBuddhistDate.now()), false); 81 assertEquals(WeekFields.ISO.weekOfWeekBasedYear().isSupportedBy(ThaiBuddhistDate.now()), true); 82 } 83 84 @Test(dataProvider = "fields") test_WOWBY_range(TemporalField weekField, TemporalField yearField)85 public void test_WOWBY_range(TemporalField weekField, TemporalField yearField) { 86 assertEquals(weekField.range(), ValueRange.of(1, 52, 53)); 87 } 88 89 @Test(dataProvider = "fields") test_WOWBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField)90 public void test_WOWBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField) { 91 assertEquals(weekField.rangeRefinedBy(LocalDate.of(2012, 12, 31)), ValueRange.of(1, 52)); 92 assertEquals(weekField.rangeRefinedBy(LocalDate.of(2013, 12, 29)), ValueRange.of(1, 52)); 93 assertEquals(weekField.rangeRefinedBy(LocalDate.of(2013, 12, 30)), ValueRange.of(1, 52)); 94 assertEquals(weekField.rangeRefinedBy(LocalDate.of(2014, 12, 28)), ValueRange.of(1, 52)); 95 assertEquals(weekField.rangeRefinedBy(LocalDate.of(2014, 12, 29)), ValueRange.of(1, 53)); 96 assertEquals(weekField.rangeRefinedBy(LocalDate.of(2016, 1, 3)), ValueRange.of(1, 53)); 97 assertEquals(weekField.rangeRefinedBy(LocalDate.of(2016, 1, 4)), ValueRange.of(1, 52)); 98 } 99 100 //----------------------------------------------------------------------- 101 // WEEK_BASED_YEAR 102 //----------------------------------------------------------------------- 103 @Test(dataProvider = "fields") test_WBY_basics(TemporalField weekField, TemporalField yearField)104 public void test_WBY_basics(TemporalField weekField, TemporalField yearField) { 105 assertEquals(yearField.isDateBased(), true); 106 assertEquals(yearField.isTimeBased(), false); 107 assertEquals(yearField.getBaseUnit(), IsoFields.WEEK_BASED_YEARS); 108 assertEquals(yearField.getRangeUnit(), ChronoUnit.FOREVER); 109 } 110 111 @Test(dataProvider = "fields") test_WBY_isSupportedBy(TemporalField weekField, TemporalField yearField)112 public void test_WBY_isSupportedBy(TemporalField weekField, TemporalField yearField) { 113 assertEquals(yearField.isSupportedBy(LocalTime.NOON), false); 114 assertEquals(yearField.isSupportedBy(MonthDay.of(2, 1)), false); 115 assertEquals(yearField.isSupportedBy(LocalDate.MIN), true); 116 assertEquals(yearField.isSupportedBy(OffsetDateTime.MAX), true); 117 } 118 119 @Test test_WBY_isSupportedBy_ISO()120 public void test_WBY_isSupportedBy_ISO() { 121 assertEquals(IsoFields.WEEK_BASED_YEAR.isSupportedBy(ThaiBuddhistDate.now()), false); 122 } 123 124 @Test(dataProvider = "fields") test_WBY_range(TemporalField weekField, TemporalField yearField)125 public void test_WBY_range(TemporalField weekField, TemporalField yearField) { 126 assertEquals(yearField.range(), ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE)); 127 } 128 129 @Test(dataProvider = "fields") test_WBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField)130 public void test_WBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField) { 131 assertEquals(yearField.rangeRefinedBy(LocalDate.of(2012, 12, 31)), ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE)); 132 } 133 134 //----------------------------------------------------------------------- 135 @Test(dataProvider = "fields") test_getFrom(TemporalField weekField, TemporalField yearField)136 public void test_getFrom(TemporalField weekField, TemporalField yearField) { 137 // tests every day from 2011 to 2016 inclusive 138 LocalDate date = LocalDate.of(2011, 1, 3); 139 int wby = 2011; 140 int week = 1; 141 int dow = 1; 142 for (int i = 1; i <= ((52 + 52 + 52 + 52 + 53 + 52) * 7); i++) { 143 assertEquals(yearField.getFrom(date), wby); 144 assertEquals(weekField.getFrom(date), week); 145 assertEquals(DAY_OF_WEEK.getFrom(date), dow); 146 if (dow == 7) { 147 dow = 1; 148 week++; 149 } else { 150 dow++; 151 } 152 if (week > wbyLen(wby)) { 153 week = 1; 154 wby++; 155 } 156 date = date.plusDays(1); 157 } 158 assertEquals(yearField.getFrom(date), 2017); 159 assertEquals(weekField.getFrom(date), 1); 160 assertEquals(DAY_OF_WEEK.getFrom(date), 1); 161 } 162 163 @Test(dataProvider = "fields") test_adjustInto_dow(TemporalField weekField, TemporalField yearField)164 public void test_adjustInto_dow(TemporalField weekField, TemporalField yearField) { 165 // tests every day from 2012 to 2016 inclusive 166 LocalDate date = LocalDate.of(2012, 1, 2); 167 int wby = 2012; 168 int week = 1; 169 int dow = 1; 170 for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) { 171 for (int j = 1; j <= 7; j++) { 172 LocalDate adjusted = DAY_OF_WEEK.adjustInto(date, j); 173 assertEquals(adjusted.get(DAY_OF_WEEK), j); 174 assertEquals(adjusted.get(weekField), week); 175 assertEquals(adjusted.get(yearField), wby); 176 } 177 if (dow == 7) { 178 dow = 1; 179 week++; 180 } else { 181 dow++; 182 } 183 if (week > wbyLen(wby)) { 184 week = 1; 185 wby++; 186 } 187 date = date.plusDays(1); 188 } 189 } 190 191 @Test(dataProvider = "fields") test_adjustInto_week(TemporalField weekField, TemporalField yearField)192 public void test_adjustInto_week(TemporalField weekField, TemporalField yearField) { 193 // tests every day from 2012 to 2016 inclusive 194 LocalDate date = LocalDate.of(2012, 1, 2); 195 int wby = 2012; 196 int week = 1; 197 int dow = 1; 198 for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) { 199 int weeksInYear = (wby == 2015 ? 53 : 52); 200 for (int j = 1; j <= weeksInYear; j++) { 201 LocalDate adjusted = weekField.adjustInto(date, j); 202 assertEquals(adjusted.get(weekField), j); 203 assertEquals(adjusted.get(DAY_OF_WEEK), dow); 204 assertEquals(adjusted.get(yearField), wby); 205 } 206 if (dow == 7) { 207 dow = 1; 208 week++; 209 } else { 210 dow++; 211 } 212 if (week > wbyLen(wby)) { 213 week = 1; 214 wby++; 215 } 216 date = date.plusDays(1); 217 } 218 } 219 220 @Test(dataProvider = "fields") test_adjustInto_wby(TemporalField weekField, TemporalField yearField)221 public void test_adjustInto_wby(TemporalField weekField, TemporalField yearField) { 222 // tests every day from 2012 to 2016 inclusive 223 LocalDate date = LocalDate.of(2012, 1, 2); 224 int wby = 2012; 225 int week = 1; 226 int dow = 1; 227 for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) { 228 for (int j = 2004; j <= 2015; j++) { 229 LocalDate adjusted = yearField.adjustInto(date, j); 230 assertEquals(adjusted.get(yearField), j); 231 assertEquals(adjusted.get(DAY_OF_WEEK), dow); 232 assertEquals(adjusted.get(weekField), (week == 53 && wbyLen(j) == 52 ? 52 : week), "" + date + " " + adjusted); 233 } 234 if (dow == 7) { 235 dow = 1; 236 week++; 237 } else { 238 dow++; 239 } 240 if (week > wbyLen(wby)) { 241 week = 1; 242 wby++; 243 } 244 date = date.plusDays(1); 245 } 246 } 247 248 @Test(dataProvider = "fields") test_addTo_weekBasedYears(TemporalField weekField, TemporalField yearField)249 public void test_addTo_weekBasedYears(TemporalField weekField, TemporalField yearField) { 250 // tests every day from 2012 to 2016 inclusive 251 LocalDate date = LocalDate.of(2012, 1, 2); 252 int wby = 2012; 253 int week = 1; 254 int dow = 1; 255 for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) { 256 for (int j = -5; j <= 5; j++) { 257 LocalDate adjusted = IsoFields.WEEK_BASED_YEARS.addTo(date, j); 258 assertEquals(adjusted.get(yearField), wby + j); 259 assertEquals(adjusted.get(DAY_OF_WEEK), dow); 260 assertEquals(adjusted.get(weekField), (week == 53 && wbyLen(wby + j) == 52 ? 52 : week), "" + date + " " + adjusted); 261 } 262 if (dow == 7) { 263 dow = 1; 264 week++; 265 } else { 266 dow++; 267 } 268 if (week > wbyLen(wby)) { 269 week = 1; 270 wby++; 271 } 272 date = date.plusDays(1); 273 } 274 } 275 wbyLen(int wby)276 private int wbyLen(int wby) { 277 return (wby == 2004 || wby == 2009 || wby == 2015 || wby == 2020 ? 53 : 52); 278 } 279 280 } 281