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