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 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")
  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")
  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")
  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
  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")
  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")
  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")
 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")
 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
 120     public void test_WBY_isSupportedBy_ISO() {
 121         assertEquals(IsoFields.WEEK_BASED_YEAR.isSupportedBy(ThaiBuddhistDate.now()), false);
 122     }
 123 
 124     @Test
 125     public void test_Unit_isSupportedBy_ISO() {
 126         assertEquals(IsoFields.WEEK_BASED_YEARS.isSupportedBy(LocalDate.now()),true);
 127         assertEquals(IsoFields.WEEK_BASED_YEARS.isSupportedBy(ThaiBuddhistDate.now()),false);
 128         assertEquals(IsoFields.QUARTER_YEARS.isSupportedBy(LocalDate.now()),true);
 129         assertEquals(IsoFields.QUARTER_YEARS.isSupportedBy(ThaiBuddhistDate.now()),false);
 130     }
 131 
 132     @Test(dataProvider = "fields")
 133     public void test_WBY_range(TemporalField weekField, TemporalField yearField) {
 134         assertEquals(yearField.range(), ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE));
 135     }
 136 
 137     @Test(dataProvider = "fields")
 138     public void test_WBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField) {
 139         assertEquals(yearField.rangeRefinedBy(LocalDate.of(2012, 12, 31)), ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE));
 140     }
 141 
 142     //-----------------------------------------------------------------------
 143     @Test(dataProvider = "fields")
 144     public void test_getFrom(TemporalField weekField, TemporalField yearField) {
 145         // tests every day from 2011 to 2016 inclusive
 146         LocalDate date = LocalDate.of(2011, 1, 3);
 147         int wby = 2011;
 148         int week = 1;
 149         int dow = 1;
 150         for (int i = 1; i <= ((52 + 52 + 52 + 52 + 53 + 52) * 7); i++) {
 151             assertEquals(yearField.getFrom(date), wby);
 152             assertEquals(weekField.getFrom(date), week);
 153             assertEquals(DAY_OF_WEEK.getFrom(date), dow);
 154             if (dow == 7) {
 155                 dow = 1;
 156                 week++;
 157             } else {
 158                 dow++;
 159             }
 160             if (week > wbyLen(wby)) {
 161                 week = 1;
 162                 wby++;
 163             }
 164             date = date.plusDays(1);
 165         }
 166         assertEquals(yearField.getFrom(date), 2017);
 167         assertEquals(weekField.getFrom(date), 1);
 168         assertEquals(DAY_OF_WEEK.getFrom(date), 1);
 169     }
 170 
 171     @Test(dataProvider = "fields")
 172     public void test_adjustInto_dow(TemporalField weekField, TemporalField yearField) {
 173         // tests every day from 2012 to 2016 inclusive
 174         LocalDate date = LocalDate.of(2012, 1, 2);
 175         int wby = 2012;
 176         int week = 1;
 177         int dow = 1;
 178         for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
 179             for (int j = 1; j <= 7; j++) {
 180                 LocalDate adjusted = DAY_OF_WEEK.adjustInto(date, j);
 181                 assertEquals(adjusted.get(DAY_OF_WEEK), j);
 182                 assertEquals(adjusted.get(weekField), week);
 183                 assertEquals(adjusted.get(yearField), wby);
 184             }
 185             if (dow == 7) {
 186                 dow = 1;
 187                 week++;
 188             } else {
 189                 dow++;
 190             }
 191             if (week > wbyLen(wby)) {
 192                 week = 1;
 193                 wby++;
 194             }
 195             date = date.plusDays(1);
 196         }
 197     }
 198 
 199     @Test(dataProvider = "fields")
 200     public void test_adjustInto_week(TemporalField weekField, TemporalField yearField) {
 201         // tests every day from 2012 to 2016 inclusive
 202         LocalDate date = LocalDate.of(2012, 1, 2);
 203         int wby = 2012;
 204         int week = 1;
 205         int dow = 1;
 206         for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
 207             int weeksInYear = (wby == 2015 ? 53 : 52);
 208             for (int j = 1; j <= weeksInYear; j++) {
 209                 LocalDate adjusted = weekField.adjustInto(date, j);
 210                 assertEquals(adjusted.get(weekField), j);
 211                 assertEquals(adjusted.get(DAY_OF_WEEK), dow);
 212                 assertEquals(adjusted.get(yearField), wby);
 213             }
 214             if (dow == 7) {
 215                 dow = 1;
 216                 week++;
 217             } else {
 218                 dow++;
 219             }
 220             if (week > wbyLen(wby)) {
 221                 week = 1;
 222                 wby++;
 223             }
 224             date = date.plusDays(1);
 225         }
 226     }
 227 
 228     @Test(dataProvider = "fields")
 229     public void test_adjustInto_wby(TemporalField weekField, TemporalField yearField) {
 230         // tests every day from 2012 to 2016 inclusive
 231         LocalDate date = LocalDate.of(2012, 1, 2);
 232         int wby = 2012;
 233         int week = 1;
 234         int dow = 1;
 235         for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
 236             for (int j = 2004; j <= 2015; j++) {
 237                 LocalDate adjusted = yearField.adjustInto(date, j);
 238                 assertEquals(adjusted.get(yearField), j);
 239                 assertEquals(adjusted.get(DAY_OF_WEEK), dow);
 240                 assertEquals(adjusted.get(weekField), (week == 53 && wbyLen(j) == 52 ? 52 : week), "" + date + " " + adjusted);
 241             }
 242             if (dow == 7) {
 243                 dow = 1;
 244                 week++;
 245             } else {
 246                 dow++;
 247             }
 248             if (week > wbyLen(wby)) {
 249                 week = 1;
 250                 wby++;
 251             }
 252             date = date.plusDays(1);
 253         }
 254     }
 255 
 256     @Test(dataProvider = "fields")
 257     public void test_addTo_weekBasedYears(TemporalField weekField, TemporalField yearField) {
 258         // tests every day from 2012 to 2016 inclusive
 259         LocalDate date = LocalDate.of(2012, 1, 2);
 260         int wby = 2012;
 261         int week = 1;
 262         int dow = 1;
 263         for (int i = 1; i <= ((52 + 52 + 52 + 53 + 52) * 7); i++) {
 264             for (int j = -5; j <= 5; j++) {
 265                 LocalDate adjusted = IsoFields.WEEK_BASED_YEARS.addTo(date, j);
 266                 assertEquals(adjusted.get(yearField), wby + j);
 267                 assertEquals(adjusted.get(DAY_OF_WEEK), dow);
 268                 assertEquals(adjusted.get(weekField), (week == 53 && wbyLen(wby + j) == 52 ? 52 : week), "" + date + " " + adjusted);
 269             }
 270             if (dow == 7) {
 271                 dow = 1;
 272                 week++;
 273             } else {
 274                 dow++;
 275             }
 276             if (week > wbyLen(wby)) {
 277                 week = 1;
 278                 wby++;
 279             }
 280             date = date.plusDays(1);
 281         }
 282     }
 283 
 284     private int wbyLen(int wby) {
 285         return (wby == 2004 || wby == 2009 || wby == 2015 || wby == 2020 ? 53 : 52);
 286     }
 287 
 288 }