1 /* 2 * Copyright (c) 2012, 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. 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 /* 27 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos 28 * 29 * All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions are met: 33 * 34 * * Redistributions of source code must retain the above copyright notice, 35 * this list of conditions and the following disclaimer. 36 * 37 * * Redistributions in binary form must reproduce the above copyright notice, 38 * this list of conditions and the following disclaimer in the documentation 39 * and/or other materials provided with the distribution. 40 * 41 * * Neither the name of JSR-310 nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 49 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 package tck.java.time.temporal; 58 59 import static java.time.DayOfWeek.FRIDAY; 60 import static java.time.DayOfWeek.MONDAY; 61 import static java.time.DayOfWeek.SATURDAY; 62 import static java.time.DayOfWeek.SUNDAY; 63 import static java.time.DayOfWeek.THURSDAY; 64 import static java.time.DayOfWeek.TUESDAY; 65 import static java.time.DayOfWeek.WEDNESDAY; 66 import static java.time.temporal.ChronoField.DAY_OF_WEEK; 67 import static java.time.temporal.ChronoField.YEAR; 68 import static org.testng.Assert.assertEquals; 69 import static org.testng.Assert.fail; 70 71 import java.time.DayOfWeek; 72 import java.time.LocalDate; 73 import java.time.LocalDateTime; 74 import java.time.chrono.ThaiBuddhistDate; 75 import java.time.format.DateTimeFormatter; 76 import java.time.format.DateTimeFormatterBuilder; 77 import java.time.format.DateTimeParseException; 78 import java.time.format.ResolverStyle; 79 import java.time.temporal.IsoFields; 80 import java.time.temporal.Temporal; 81 import java.time.temporal.TemporalField; 82 import java.time.temporal.UnsupportedTemporalTypeException; 83 import java.time.temporal.ValueRange; 84 85 import org.testng.annotations.DataProvider; 86 import org.testng.annotations.Test; 87 88 /** 89 * Test. 90 */ 91 @Test 92 public class TCKIsoFields { 93 94 @DataProvider(name="quarter") data_quarter()95 Object[][] data_quarter() { 96 return new Object[][] { 97 {LocalDate.of(1969, 12, 29), 90, 4}, 98 {LocalDate.of(1969, 12, 30), 91, 4}, 99 {LocalDate.of(1969, 12, 31), 92, 4}, 100 101 {LocalDate.of(1970, 1, 1), 1, 1}, 102 {LocalDate.of(1970, 1, 2), 2, 1}, 103 {LocalDate.of(1970, 2, 28), 59, 1}, 104 {LocalDate.of(1970, 3, 1), 60, 1}, 105 {LocalDate.of(1970, 3, 31), 90, 1}, 106 107 {LocalDate.of(1970, 4, 1), 1, 2}, 108 {LocalDate.of(1970, 6, 30), 91, 2}, 109 110 {LocalDate.of(1970, 7, 1), 1, 3}, 111 {LocalDate.of(1970, 9, 30), 92, 3}, 112 113 {LocalDate.of(1970, 10, 1), 1, 4}, 114 {LocalDate.of(1970, 12, 31), 92, 4}, 115 116 {LocalDate.of(1972, 2, 28), 59, 1}, 117 {LocalDate.of(1972, 2, 29), 60, 1}, 118 {LocalDate.of(1972, 3, 1), 61, 1}, 119 {LocalDate.of(1972, 3, 31), 91, 1}, 120 }; 121 } 122 123 //----------------------------------------------------------------------- 124 // DAY_OF_QUARTER 125 //----------------------------------------------------------------------- 126 @Test(dataProvider = "quarter") test_DOQ(LocalDate date, int doq, int qoy)127 public void test_DOQ(LocalDate date, int doq, int qoy) { 128 assertEquals(IsoFields.DAY_OF_QUARTER.getFrom(date), doq); 129 assertEquals(date.get(IsoFields.DAY_OF_QUARTER), doq); 130 } 131 test_DOQ_basics()132 public void test_DOQ_basics() { 133 assertEquals(IsoFields.DAY_OF_QUARTER.isDateBased(), true); 134 assertEquals(IsoFields.DAY_OF_QUARTER.isTimeBased(), false); 135 } 136 137 //----------------------------------------------------------------------- 138 // QUARTER_OF_YEAR 139 //----------------------------------------------------------------------- 140 @Test(dataProvider = "quarter") test_QOY(LocalDate date, int doq, int qoy)141 public void test_QOY(LocalDate date, int doq, int qoy) { 142 assertEquals(IsoFields.QUARTER_OF_YEAR.getFrom(date), qoy); 143 assertEquals(date.get(IsoFields.QUARTER_OF_YEAR), qoy); 144 } 145 test_QOY_basics()146 public void test_QOY_basics() { 147 assertEquals(IsoFields.QUARTER_OF_YEAR.isDateBased(), true); 148 assertEquals(IsoFields.QUARTER_OF_YEAR.isTimeBased(), false); 149 } 150 151 //----------------------------------------------------------------------- 152 // parse quarters 153 //----------------------------------------------------------------------- 154 @Test(dataProvider = "quarter") test_parse_quarters(LocalDate date, int doq, int qoy)155 public void test_parse_quarters(LocalDate date, int doq, int qoy) { 156 DateTimeFormatter f = new DateTimeFormatterBuilder() 157 .appendValue(YEAR).appendLiteral('-') 158 .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral('-') 159 .appendValue(IsoFields.DAY_OF_QUARTER) 160 .toFormatter().withResolverStyle(ResolverStyle.STRICT); 161 LocalDate parsed = LocalDate.parse(date.getYear() + "-" + qoy + "-" + doq, f); 162 assertEquals(parsed, date); 163 } 164 165 @Test(dataProvider = "quarter") test_parse_quarters_SMART(LocalDate date, int doq, int qoy)166 public void test_parse_quarters_SMART(LocalDate date, int doq, int qoy) { 167 DateTimeFormatter f = new DateTimeFormatterBuilder() 168 .appendValue(YEAR).appendLiteral('-') 169 .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral('-') 170 .appendValue(IsoFields.DAY_OF_QUARTER) 171 .toFormatter().withResolverStyle(ResolverStyle.SMART); 172 LocalDate parsed = LocalDate.parse(date.getYear() + "-" + qoy + "-" + doq, f); 173 assertEquals(parsed, date); 174 } 175 176 @Test(dataProvider = "quarter") test_parse_quarters_LENIENT(LocalDate date, int doq, int qoy)177 public void test_parse_quarters_LENIENT(LocalDate date, int doq, int qoy) { 178 DateTimeFormatter f = new DateTimeFormatterBuilder() 179 .appendValue(YEAR).appendLiteral('-') 180 .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral('-') 181 .appendValue(IsoFields.DAY_OF_QUARTER) 182 .toFormatter().withResolverStyle(ResolverStyle.LENIENT); 183 LocalDate parsed = LocalDate.parse(date.getYear() + "-" + qoy + "-" + doq, f); 184 assertEquals(parsed, date); 185 } 186 187 //----------------------------------------------------------------------- 188 @DataProvider(name="parseLenientQuarter") data_parseLenientQuarter()189 Object[][] data_parseLenientQuarter() { 190 return new Object[][] { 191 {"2012:0:1", LocalDate.of(2011, 10, 1), false}, 192 {"2012:5:1", LocalDate.of(2013, 1, 1), false}, 193 194 {"2012:1:-1", LocalDate.of(2011, 12, 30), false}, 195 {"2012:1:0", LocalDate.of(2011, 12, 31), false}, 196 {"2012:0:0", LocalDate.of(2011, 9, 30), false}, 197 198 {"2012:1:92", LocalDate.of(2012, 4, 1), true}, 199 {"2012:2:92", LocalDate.of(2012, 7, 1), true}, 200 {"2012:2:93", LocalDate.of(2012, 7, 2), false}, 201 {"2012:3:93", LocalDate.of(2012, 10, 1), false}, 202 {"2012:4:93", LocalDate.of(2013, 1, 1), false}, 203 {"2012:4:182", LocalDate.of(2013, 3, 31), false}, 204 {"2012:4:183", LocalDate.of(2013, 4, 1), false}, 205 206 {"2011:1:91", LocalDate.of(2011, 4, 1), true}, 207 {"2011:1:92", LocalDate.of(2011, 4, 2), true}, 208 }; 209 } 210 211 @Test(dataProvider = "parseLenientQuarter", expectedExceptions = DateTimeParseException.class) test_parse_parseLenientQuarter_STRICT(String str, LocalDate expected, boolean smart)212 public void test_parse_parseLenientQuarter_STRICT(String str, LocalDate expected, boolean smart) { 213 DateTimeFormatter f = new DateTimeFormatterBuilder() 214 .appendValue(YEAR).appendLiteral(':') 215 .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral(':') 216 .appendValue(IsoFields.DAY_OF_QUARTER) 217 .toFormatter().withResolverStyle(ResolverStyle.STRICT); 218 LocalDate.parse(str, f); 219 } 220 221 @Test(dataProvider = "parseLenientQuarter") test_parse_parseLenientQuarter_SMART(String str, LocalDate expected, boolean smart)222 public void test_parse_parseLenientQuarter_SMART(String str, LocalDate expected, boolean smart) { 223 DateTimeFormatter f = new DateTimeFormatterBuilder() 224 .appendValue(YEAR).appendLiteral(':') 225 .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral(':') 226 .appendValue(IsoFields.DAY_OF_QUARTER) 227 .toFormatter().withResolverStyle(ResolverStyle.SMART); 228 if (smart) { 229 LocalDate parsed = LocalDate.parse(str, f); 230 assertEquals(parsed, expected); 231 } else { 232 try { 233 LocalDate.parse(str, f); 234 fail("Should have failed"); 235 } catch (DateTimeParseException ex) { 236 // expected 237 } 238 } 239 } 240 241 @Test(dataProvider = "parseLenientQuarter") test_parse_parseLenientQuarter_LENIENT(String str, LocalDate expected, boolean smart)242 public void test_parse_parseLenientQuarter_LENIENT(String str, LocalDate expected, boolean smart) { 243 DateTimeFormatter f = new DateTimeFormatterBuilder() 244 .appendValue(YEAR).appendLiteral(':') 245 .appendValue(IsoFields.QUARTER_OF_YEAR).appendLiteral(':') 246 .appendValue(IsoFields.DAY_OF_QUARTER) 247 .toFormatter().withResolverStyle(ResolverStyle.LENIENT); 248 LocalDate parsed = LocalDate.parse(str, f); 249 assertEquals(parsed, expected); 250 } 251 252 //----------------------------------------------------------------------- 253 // quarters between 254 //----------------------------------------------------------------------- 255 @DataProvider(name="quartersBetween") data_quartersBetween()256 Object[][] data_quartersBetween() { 257 return new Object[][] { 258 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 1), 0}, 259 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 2), 0}, 260 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 2, 1), 0}, 261 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 3, 1), 0}, 262 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 3, 31), 0}, 263 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 4, 1), 1}, 264 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 4, 2), 1}, 265 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 6, 30), 1}, 266 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 7, 1), 2}, 267 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 10, 1), 3}, 268 {LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 31), 3}, 269 {LocalDate.of(2000, 1, 1), LocalDate.of(2001, 1, 1), 4}, 270 {LocalDate.of(2000, 1, 1), LocalDate.of(2002, 1, 1), 8}, 271 272 {LocalDate.of(2000, 1, 1), LocalDate.of(1999, 12, 31), 0}, 273 {LocalDate.of(2000, 1, 1), LocalDate.of(1999, 10, 2), 0}, 274 {LocalDate.of(2000, 1, 1), LocalDate.of(1999, 10, 1), -1}, 275 {LocalDate.of(2000, 1, 1), LocalDate.of(1999, 7, 2), -1}, 276 {LocalDate.of(2000, 1, 1), LocalDate.of(1999, 7, 1), -2}, 277 {LocalDate.of(2000, 1, 1), LocalDate.of(1999, 4, 2), -2}, 278 {LocalDate.of(2000, 1, 1), LocalDate.of(1999, 4, 1), -3}, 279 {LocalDate.of(2000, 1, 1), LocalDate.of(1999, 1, 2), -3}, 280 {LocalDate.of(2000, 1, 1), LocalDate.of(1999, 1, 1), -4}, 281 {LocalDate.of(2000, 1, 1), LocalDate.of(1998, 12, 31), -4}, 282 {LocalDate.of(2000, 1, 1), LocalDate.of(1998, 10, 2), -4}, 283 {LocalDate.of(2000, 1, 1), LocalDate.of(1998, 10, 1), -5}, 284 285 {LocalDate.of(2000, 1, 1), LocalDateTime.of(2001, 4, 5, 0, 0), 5}, 286 }; 287 } 288 289 @Test(dataProvider="quartersBetween") test_quarters_between(LocalDate start, Temporal end, long expected)290 public void test_quarters_between(LocalDate start, Temporal end, long expected) { 291 assertEquals(IsoFields.QUARTER_YEARS.between(start, end), expected); 292 } 293 294 @Test(dataProvider="quartersBetween") test_quarters_between_until(LocalDate start, Temporal end, long expected)295 public void test_quarters_between_until(LocalDate start, Temporal end, long expected) { 296 assertEquals(start.until(end, IsoFields.QUARTER_YEARS), expected); 297 } 298 299 //----------------------------------------------------------------------- 300 //----------------------------------------------------------------------- 301 //----------------------------------------------------------------------- 302 @DataProvider(name="week") data_week()303 Object[][] data_week() { 304 return new Object[][] { 305 {LocalDate.of(1969, 12, 29), MONDAY, 1, 1970}, 306 {LocalDate.of(2012, 12, 23), SUNDAY, 51, 2012}, 307 {LocalDate.of(2012, 12, 24), MONDAY, 52, 2012}, 308 {LocalDate.of(2012, 12, 27), THURSDAY, 52, 2012}, 309 {LocalDate.of(2012, 12, 28), FRIDAY, 52, 2012}, 310 {LocalDate.of(2012, 12, 29), SATURDAY, 52, 2012}, 311 {LocalDate.of(2012, 12, 30), SUNDAY, 52, 2012}, 312 {LocalDate.of(2012, 12, 31), MONDAY, 1, 2013}, 313 {LocalDate.of(2013, 1, 1), TUESDAY, 1, 2013}, 314 {LocalDate.of(2013, 1, 2), WEDNESDAY, 1, 2013}, 315 {LocalDate.of(2013, 1, 6), SUNDAY, 1, 2013}, 316 {LocalDate.of(2013, 1, 7), MONDAY, 2, 2013}, 317 }; 318 } 319 320 //----------------------------------------------------------------------- 321 // WEEK_OF_WEEK_BASED_YEAR 322 //----------------------------------------------------------------------- 323 @Test(dataProvider="week") test_WOWBY(LocalDate date, DayOfWeek dow, int week, int wby)324 public void test_WOWBY(LocalDate date, DayOfWeek dow, int week, int wby) { 325 assertEquals(date.getDayOfWeek(), dow); 326 assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week); 327 assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week); 328 } 329 test_WOWBY_basics()330 public void test_WOWBY_basics() { 331 assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.isDateBased(), true); 332 assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.isTimeBased(), false); 333 } 334 335 //----------------------------------------------------------------------- 336 // WEEK_BASED_YEAR 337 //----------------------------------------------------------------------- 338 @Test(dataProvider="week") test_WBY(LocalDate date, DayOfWeek dow, int week, int wby)339 public void test_WBY(LocalDate date, DayOfWeek dow, int week, int wby) { 340 assertEquals(date.getDayOfWeek(), dow); 341 assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby); 342 assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby); 343 } 344 test_WBY_basics()345 public void test_WBY_basics() { 346 assertEquals(IsoFields.WEEK_BASED_YEAR.isDateBased(), true); 347 assertEquals(IsoFields.WEEK_BASED_YEAR.isTimeBased(), false); 348 } 349 350 //----------------------------------------------------------------------- 351 // parse weeks 352 //----------------------------------------------------------------------- 353 @Test(dataProvider="week") test_parse_weeks_STRICT(LocalDate date, DayOfWeek dow, int week, int wby)354 public void test_parse_weeks_STRICT(LocalDate date, DayOfWeek dow, int week, int wby) { 355 DateTimeFormatter f = new DateTimeFormatterBuilder() 356 .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral('-') 357 .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral('-') 358 .appendValue(DAY_OF_WEEK) 359 .toFormatter().withResolverStyle(ResolverStyle.STRICT); 360 LocalDate parsed = LocalDate.parse(wby + "-" + week + "-" + dow.getValue(), f); 361 assertEquals(parsed, date); 362 } 363 364 @Test(dataProvider="week") test_parse_weeks_SMART(LocalDate date, DayOfWeek dow, int week, int wby)365 public void test_parse_weeks_SMART(LocalDate date, DayOfWeek dow, int week, int wby) { 366 DateTimeFormatter f = new DateTimeFormatterBuilder() 367 .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral('-') 368 .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral('-') 369 .appendValue(DAY_OF_WEEK) 370 .toFormatter().withResolverStyle(ResolverStyle.SMART); 371 LocalDate parsed = LocalDate.parse(wby + "-" + week + "-" + dow.getValue(), f); 372 assertEquals(parsed, date); 373 } 374 375 @Test(dataProvider="week") test_parse_weeks_LENIENT(LocalDate date, DayOfWeek dow, int week, int wby)376 public void test_parse_weeks_LENIENT(LocalDate date, DayOfWeek dow, int week, int wby) { 377 DateTimeFormatter f = new DateTimeFormatterBuilder() 378 .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral('-') 379 .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral('-') 380 .appendValue(DAY_OF_WEEK) 381 .toFormatter().withResolverStyle(ResolverStyle.LENIENT); 382 LocalDate parsed = LocalDate.parse(wby + "-" + week + "-" + dow.getValue(), f); 383 assertEquals(parsed, date); 384 } 385 386 //----------------------------------------------------------------------- 387 @DataProvider(name="parseLenientWeek") data_parseLenientWeek()388 Object[][] data_parseLenientWeek() { 389 return new Object[][] { 390 {"2012:52:-1", LocalDate.of(2012, 12, 22), false}, 391 {"2012:52:0", LocalDate.of(2012, 12, 23), false}, 392 {"2012:52:8", LocalDate.of(2012, 12, 31), false}, 393 {"2012:52:9", LocalDate.of(2013, 1, 1), false}, 394 395 {"2012:53:1", LocalDate.of(2012, 12, 31), true}, 396 {"2012:54:1", LocalDate.of(2013, 1, 7), false}, 397 398 {"2013:0:1", LocalDate.of(2012, 12, 24), false}, 399 {"2013:0:0", LocalDate.of(2012, 12, 23), false}, 400 }; 401 } 402 403 @Test(dataProvider = "parseLenientWeek", expectedExceptions = DateTimeParseException.class) test_parse_parseLenientWeek_STRICT(String str, LocalDate expected, boolean smart)404 public void test_parse_parseLenientWeek_STRICT(String str, LocalDate expected, boolean smart) { 405 DateTimeFormatter f = new DateTimeFormatterBuilder() 406 .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral(':') 407 .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral(':') 408 .appendValue(DAY_OF_WEEK) 409 .toFormatter().withResolverStyle(ResolverStyle.STRICT); 410 LocalDate.parse(str, f); 411 } 412 413 @Test(dataProvider = "parseLenientWeek") test_parse_parseLenientWeek_SMART(String str, LocalDate expected, boolean smart)414 public void test_parse_parseLenientWeek_SMART(String str, LocalDate expected, boolean smart) { 415 DateTimeFormatter f = new DateTimeFormatterBuilder() 416 .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral(':') 417 .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral(':') 418 .appendValue(DAY_OF_WEEK) 419 .toFormatter().withResolverStyle(ResolverStyle.SMART); 420 if (smart) { 421 LocalDate parsed = LocalDate.parse(str, f); 422 assertEquals(parsed, expected); 423 } else { 424 try { 425 LocalDate.parse(str, f); 426 fail("Should have failed"); 427 } catch (DateTimeParseException ex) { 428 // expected 429 } 430 } 431 } 432 433 @Test(dataProvider = "parseLenientWeek") test_parse_parseLenientWeek_LENIENT(String str, LocalDate expected, boolean smart)434 public void test_parse_parseLenientWeek_LENIENT(String str, LocalDate expected, boolean smart) { 435 DateTimeFormatter f = new DateTimeFormatterBuilder() 436 .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral(':') 437 .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral(':') 438 .appendValue(DAY_OF_WEEK) 439 .toFormatter().withResolverStyle(ResolverStyle.LENIENT); 440 LocalDate parsed = LocalDate.parse(str, f); 441 assertEquals(parsed, expected); 442 } 443 444 //----------------------------------------------------------------------- 445 // rangeRefinedBy 446 //----------------------------------------------------------------------- 447 @DataProvider(name="isofields") data_isofields()448 Object[][] data_isofields() { 449 return new Object[][] { 450 {IsoFields.DAY_OF_QUARTER, 49, ValueRange.of(1, 91)}, 451 {IsoFields.QUARTER_OF_YEAR, 2, ValueRange.of(1, 4)}, 452 {IsoFields.WEEK_OF_WEEK_BASED_YEAR, 20, ValueRange.of(1, 52)}, 453 {IsoFields.WEEK_BASED_YEAR, 2016, ValueRange.of(LocalDate.MIN.getYear(), 454 LocalDate.MAX.getYear())}, 455 }; 456 } 457 458 @Test(dataProvider = "isofields") test_isofields_rangerefinedby(TemporalField field, int value, ValueRange valueRange)459 public void test_isofields_rangerefinedby(TemporalField field, int value, ValueRange valueRange) { 460 LocalDate date = LocalDate.of(2016, 5, 19); 461 assertEquals(field.rangeRefinedBy(date), valueRange); 462 } 463 464 @Test(dataProvider = "isofields", expectedExceptions = UnsupportedTemporalTypeException.class) test_nonisofields_rangerefinedby(TemporalField field, int value, ValueRange valueRange)465 public void test_nonisofields_rangerefinedby(TemporalField field, int value, ValueRange valueRange) { 466 field.rangeRefinedBy(ThaiBuddhistDate.now()); 467 } 468 469 //----------------------------------------------------------------------- 470 // getFrom 471 //----------------------------------------------------------------------- 472 @Test(dataProvider = "isofields") test_isofields_getFrom(TemporalField field, int value, ValueRange valueRange)473 public void test_isofields_getFrom(TemporalField field, int value, ValueRange valueRange) { 474 LocalDate date = LocalDate.of(2016, 5, 19); 475 assertEquals(field.getFrom(date), value); 476 } 477 478 @Test(dataProvider = "isofields", expectedExceptions = UnsupportedTemporalTypeException.class) test_nonisofields_getFrom(TemporalField field, int value, ValueRange valueRange)479 public void test_nonisofields_getFrom(TemporalField field, int value, ValueRange valueRange) { 480 field.getFrom(ThaiBuddhistDate.now()); 481 } 482 483 //----------------------------------------------------------------------- test_loop()484 public void test_loop() { 485 // loop round at least one 400 year cycle, including before 1970 486 LocalDate date = LocalDate.of(1960, 1, 5); // Tuseday of week 1 1960 487 int year = 1960; 488 int wby = 1960; 489 int weekLen = 52; 490 int week = 1; 491 while (date.getYear() < 2400) { 492 DayOfWeek loopDow = date.getDayOfWeek(); 493 if (date.getYear() != year) { 494 year = date.getYear(); 495 } 496 if (loopDow == MONDAY) { 497 week++; 498 if ((week == 53 && weekLen == 52) || week == 54) { 499 week = 1; 500 LocalDate firstDayOfWeekBasedYear = date.plusDays(14).withDayOfYear(1); 501 DayOfWeek firstDay = firstDayOfWeekBasedYear.getDayOfWeek(); 502 weekLen = (firstDay == THURSDAY || (firstDay == WEDNESDAY && firstDayOfWeekBasedYear.isLeapYear()) ? 53 : 52); 503 wby++; 504 } 505 } 506 assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date), ValueRange.of(1, weekLen), "Failed on " + date + " " + date.getDayOfWeek()); 507 assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week, "Failed on " + date + " " + date.getDayOfWeek()); 508 assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week, "Failed on " + date + " " + date.getDayOfWeek()); 509 assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby, "Failed on " + date + " " + date.getDayOfWeek()); 510 assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby, "Failed on " + date + " " + date.getDayOfWeek()); 511 date = date.plusDays(1); 512 } 513 } 514 515 // TODO: more tests 516 } 517