test/java/time/tck/java/time/temporal/TCKWeekFields.java

Print this page




  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 org.testng.Assert.assertEquals;
  60 import static org.testng.Assert.assertSame;
  61 import static org.testng.Assert.fail;
  62 
  63 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
  64 import static java.time.temporal.ChronoField.DAY_OF_MONTH;

  65 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
  66 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  67 import static java.time.temporal.ChronoField.YEAR;


  68 
  69 import java.io.ByteArrayInputStream;
  70 import java.io.ByteArrayOutputStream;
  71 import java.io.IOException;
  72 import java.io.ObjectInputStream;
  73 import java.io.ObjectOutputStream;
  74 
  75 import java.time.DayOfWeek;
  76 import java.time.LocalDate;


  77 import java.time.temporal.TemporalField;
  78 import java.time.format.DateTimeBuilder;
  79 import java.time.temporal.ValueRange;
  80 import java.time.temporal.WeekFields;
  81 

  82 import org.testng.annotations.Test;

  83 
  84 /**
  85  * Test WeekFields.
  86  */
  87 @Test
  88 public class TCKWeekFields {
  89 
  90     @Test(groups={"tck"})
  91     public void test_WeekFieldsOf() {
  92         for (DayOfWeek dow : DayOfWeek.values()) {
  93             for (int minDays = 1; minDays <= 7; minDays++) {
  94                 WeekFields week = WeekFields.of(dow, minDays);
  95                 assertEquals(week.getFirstDayOfWeek(), dow, "Incorrect firstDayOfWeek");
  96                 assertEquals(week.getMinimalDaysInFirstWeek(), minDays, "Incorrect MinimalDaysInFirstWeek");

  97             }
  98         }


























  99     }
 100 
 101     @Test(groups={"tck"})
 102     public void test_DayOfWeek() {
 103         LocalDate day = LocalDate.of(2000, 1, 10);  // Known to be ISO Monday
 104         for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
 105             for (int minDays = 1; minDays <= 7; minDays++) {
 106                 WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 107                 TemporalField f = week.dayOfWeek();
 108                 //System.out.printf("  Week: %s; field: %s%n", week, f);
 109 
 110                 for (int i = 1; i <= 7; i++) {
 111                     //System.out.printf("  ISO Dow: %s, WeekDOW ordinal: %s%n", day.getDayOfWeek(), day.get(f));
 112                     assertEquals(day.get(f), (7 + day.getDayOfWeek().getValue() - firstDayOfWeek.getValue()) % 7 + 1);
 113                     day = day.plusDays(1);
 114                 }
 115             }
 116         }
 117     }
 118 
 119     @Test(groups={"tck"})
 120     public void test_WeekOfMonth() {
 121         for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
 122             for (int minDays = 1; minDays <= 7; minDays++) {
 123                 LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
 124                 WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 125                 TemporalField dowField = week.dayOfWeek();
 126                 TemporalField womField = week.weekOfMonth();
 127                 //System.err.printf("%n  Week: %s; dowField: %s, domField: %s%n", week, dowField, womField);
 128 
 129                 DayOfWeek isoDOW = day.getDayOfWeek();
 130                 int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;
 131 
 132                 for (int i = 1; i <= 15; i++) {
 133                     int actualDOW = day.get(dowField);
 134                     int actualWOM = day.get(womField);
 135 
 136                     // Verify that the combination of day of week and week of month can be used
 137                     // to reconstruct the same date.
 138                     LocalDate day1 = day.withDayOfMonth(1);
 139                     int offset = - (day1.get(dowField) - 1);
 140                     //System.err.printf("   refDay: %s%n", day1.plusDays(offset));
 141                     int week1 = day1.get(womField);
 142                     if (week1 == 0) {
 143                         // week of the 1st is partial; start with first full week
 144                         offset += 7;
 145                     }
 146                     //System.err.printf("   refDay2: %s, offset: %d, week1: %d%n", day1.plusDays(offset), offset, week1);
 147                     offset += actualDOW - 1;
 148                     //System.err.printf("   refDay3: %s%n", day1.plusDays(offset));
 149                     offset += (actualWOM - 1) * 7;
 150                     //System.err.printf("   refDay4: %s%n", day1.plusDays(offset));
 151                     LocalDate result = day1.plusDays(offset);
 152 
 153                     if (!day.equals(result)) {
 154                         System.err.printf("FAIL ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
 155                                 day.getDayOfWeek(), offset, actualDOW, actualWOM, day, result);
 156                     }
 157                     assertEquals(result, day, "Incorrect dayOfWeek or weekOfMonth: "
 158                             + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
 159                             week, day.getDayOfWeek(), offset, actualDOW, actualWOM, day, result));
 160                     day = day.plusDays(1);
 161                 }
 162             }
 163         }
 164     }
 165 
 166     @Test(groups={"tck"})
 167     public void test_WeekOfYear() {
 168         for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
 169             for (int minDays = 1; minDays <= 7; minDays++) {
 170                 LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
 171                 WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 172                 TemporalField dowField = week.dayOfWeek();
 173                 TemporalField woyField = week.weekOfYear();
 174                 //System.err.printf("%n  Year: %s; dowField: %s, woyField: %s%n", week, dowField, woyField);
 175 
 176                 DayOfWeek isoDOW = day.getDayOfWeek();
 177                 int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;
 178 
 179                 for (int i = 1; i <= 15; i++) {
 180                     int actualDOW = day.get(dowField);
 181                     int actualWOY = day.get(woyField);
 182 
 183                     // Verify that the combination of day of week and week of month can be used
 184                     // to reconstruct the same date.
 185                     LocalDate day1 = day.withDayOfYear(1);
 186                     int offset = - (day1.get(dowField) - 1);
 187                     //System.err.printf("   refDay: %s%n", day1.plusDays(offset));
 188                     int week1 = day1.get(woyField);
 189                     if (week1 == 0) {


 191                         offset += 7;
 192                     }
 193                     //System.err.printf("   refDay2: %s, offset: %d, week1: %d%n", day1.plusDays(offset), offset, week1);
 194                     offset += actualDOW - 1;
 195                     //System.err.printf("   refDay3: %s%n", day1.plusDays(offset));
 196                     offset += (actualWOY - 1) * 7;
 197                     //System.err.printf("   refDay4: %s%n", day1.plusDays(offset));
 198                     LocalDate result = day1.plusDays(offset);
 199 
 200 
 201                     if (!day.equals(result)) {
 202                         System.err.printf("FAIL  ISO Dow: %s, offset: %s, actualDOW: %s, actualWOY: %s, expected: %s, result: %s%n",
 203                                 day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result);
 204                     }
 205                     assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
 206                             + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
 207                             week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result));
 208                     day = day.plusDays(1);
 209                 }
 210             }
 211         }
 212     }
 213 
 214     @Test(groups={"tck"})
 215     public void test_fieldRanges() {
 216         for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
 217             for (int minDays = 1; minDays <= 7; minDays++) {
 218                 WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
 219                 TemporalField dowField = weekDef.dayOfWeek();
 220                 TemporalField womField = weekDef.weekOfMonth();
 221                 TemporalField woyField = weekDef.weekOfYear();
 222 
 223                 LocalDate day = LocalDate.of(2012, 11, 30);
 224                 LocalDate endDay = LocalDate.of(2013, 1, 2);
 225                 while (day.isBefore(endDay)) {
 226                     LocalDate last = day.with(DAY_OF_MONTH, day.lengthOfMonth());
 227                     int lastWOM = last.get(womField);
 228                     LocalDate first = day.with(DAY_OF_MONTH, 1);
 229                     int firstWOM = first.get(womField);
 230                     ValueRange rangeWOM = day.range(womField);
 231                     assertEquals(rangeWOM.getMinimum(), firstWOM,
 232                             "Range min should be same as WeekOfMonth for first day of month: "
 233                             + first + ", " + weekDef);
 234                     assertEquals(rangeWOM.getMaximum(), lastWOM,
 235                             "Range max should be same as WeekOfMonth for last day of month: "
 236                             + last + ", " + weekDef);
 237 
 238                     last = day.with(DAY_OF_YEAR, day.lengthOfYear());
 239                     int lastWOY = last.get(woyField);
 240                     first = day.with(DAY_OF_YEAR, 1);
 241                     int firstWOY = first.get(woyField);
 242                     ValueRange rangeWOY = day.range(woyField);
 243                     assertEquals(rangeWOY.getMinimum(), firstWOY,
 244                             "Range min should be same as WeekOfYear for first day of Year: "
 245                             + day + ", " + weekDef);
 246                     assertEquals(rangeWOY.getMaximum(), lastWOY,
 247                             "Range max should be same as WeekOfYear for last day of Year: "
 248                             + day + ", " + weekDef);
 249 
 250                     day = day.plusDays(1);
 251                 }
 252             }
 253         }
 254     }
 255 
 256     //-----------------------------------------------------------------------
 257     // withDayOfWeek()
 258     //-----------------------------------------------------------------------
 259     @Test(groups = {"tck"})
 260     public void test_withDayOfWeek() {
 261         for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
 262             for (int minDays = 1; minDays <= 7; minDays++) {
 263                 LocalDate day = LocalDate.of(2012, 12, 15);  // Safely in the middle of a month
 264                 WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 265 
 266                 TemporalField dowField = week.dayOfWeek();
 267                 TemporalField womField = week.weekOfMonth();
 268                 TemporalField woyField = week.weekOfYear();

 269                 int wom = day.get(womField);
 270                 int woy = day.get(woyField);
 271                 for (int dow = 1; dow <= 7; dow++) {
 272                     LocalDate result = day.with(dowField, dow);
 273                     if (result.get(dowField) != dow) {
 274                         System.err.printf(" DOW actual: %d, expected: %d, week:%s%n",
 275                                 result.get(dowField), dow, week);
 276                     }
 277                     if (result.get(womField) != wom) {
 278                         System.err.printf(" WOM actual: %d, expected: %d, week:%s%n",
 279                                 result.get(womField), wom, week);
 280                     }
 281                     if (result.get(woyField) != woy) {
 282                         System.err.printf(" WOY actual: %d, expected: %d, week:%s%n",
 283                                 result.get(woyField), woy, week);
 284                     }
 285                     assertEquals(result.get(dowField), dow, String.format("Incorrect new Day of week: %s", result));
 286                     assertEquals(result.get(womField), wom, "Week of Month should not change");
 287                     assertEquals(result.get(woyField), woy, "Week of Year should not change");
 288                 }
 289             }





















 290         }
 291     }
 292 
 293     @Test()
 294     public void test_computedFieldResolver() {
 295         // For all starting days of week, for all minDays, for two weeks in Dec 2012
 296         // Test that when supplied with partial values, that the resolver
 297         // fills in the month
 298         for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
 299             for (int minDays = 1; minDays <= 7; minDays++) {
 300                 LocalDate day = LocalDate.of(2012, 12, 15);  // Safely in the middle of a month
 301                 WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 302 
 303                 TemporalField dowField = week.dayOfWeek();
 304                 TemporalField womField = week.weekOfMonth();





















 305                 TemporalField woyField = week.weekOfYear();
 306                 int wom = day.get(womField);
 307                 int woy = day.get(woyField);
 308                 for (int dow = 1; dow <= 15; dow++) {
 309                     // Test that with dayOfWeek and Week of month it computes the day of month
 310                     DateTimeBuilder builder = new DateTimeBuilder();
 311                     builder.addFieldValue(YEAR, day.get(YEAR));
 312                     builder.addFieldValue(MONTH_OF_YEAR, day.get(MONTH_OF_YEAR));
 313                     builder.addFieldValue(DAY_OF_WEEK, day.get(DAY_OF_WEEK));
 314                     builder.addFieldValue(dowField, day.get(dowField));
 315                     builder.addFieldValue(womField, day.get(womField));
 316 
 317                     boolean res1 = dowField.resolve(builder, day.get(dowField));
 318                     boolean res2 = womField.resolve(builder, day.get(womField));
 319                     assertEquals(day.get(DAY_OF_MONTH), day.get(DAY_OF_MONTH));
 320                     assertEquals(day.get(DAY_OF_YEAR), day.get(DAY_OF_YEAR));
 321 
 322                     day = day.plusDays(1);













 323                 }
 324                 day = LocalDate.of(2012, 12, 15);  // Safely in the middle of a month
 325                 for (int dow = 1; dow <= 15; dow++) {
 326                     // Test that with dayOfWeek and Week of year it computes the day of year
 327                     DateTimeBuilder builder = new DateTimeBuilder();
 328                     builder.addFieldValue(YEAR, day.get(YEAR));
 329                     builder.addFieldValue(DAY_OF_WEEK, day.get(DAY_OF_WEEK));
 330                     builder.addFieldValue(dowField, day.get(dowField));
 331                     builder.addFieldValue(woyField, day.get(woyField));
 332 
 333                     boolean res1 = dowField.resolve(builder, day.get(dowField));
 334                     boolean res2 = woyField.resolve(builder, day.get(woyField));




 335 
 336                     assertEquals(day.get(DAY_OF_MONTH), day.get(DAY_OF_MONTH));
 337                     assertEquals(day.get(DAY_OF_YEAR), day.get(DAY_OF_YEAR));









 338 
 339                     day = day.plusDays(1);
 340                 }
 341             }
 342         }






 343     }
 344 
 345     @Test(groups = {"tck"})
 346     public void test_WeekFieldsSingleton() throws IOException, ClassNotFoundException {



 347         for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
 348             for (int minDays = 1; minDays <= 7; minDays++) {
 349                 WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
 350                 WeekFields weekDef2 = WeekFields.of(firstDayOfWeek, minDays);
 351                 assertSame(weekDef2, weekDef, "WeekFields same parameters should be same instance");
 352                 try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
 353                      ObjectOutputStream oos = new ObjectOutputStream(baos)) {
 354                     oos.writeObject(weekDef);
 355 
 356                     ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
 357                         baos.toByteArray()));
 358                     WeekFields result = (WeekFields)ois.readObject();
 359                     assertSame(result, weekDef, "Deserialized object same as serialized.");
 360                 }
 361                 // Exceptions will be handled as failures by TestNG
 362             }
 363         }

 364     }

 365 }


  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.temporal.ChronoField.DAY_OF_MONTH;
  60 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
  61 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
  62 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  63 import static java.time.temporal.ChronoField.YEAR;
  64 import static org.testng.Assert.assertEquals;
  65 import static org.testng.Assert.assertSame;
  66 


  67 import java.io.IOException;



  68 import java.time.DayOfWeek;
  69 import java.time.LocalDate;
  70 import java.time.format.DateTimeFormatter;
  71 import java.time.format.DateTimeFormatterBuilder;
  72 import java.time.temporal.TemporalField;

  73 import java.time.temporal.ValueRange;
  74 import java.time.temporal.WeekFields;
  75 
  76 import org.testng.annotations.DataProvider;
  77 import org.testng.annotations.Test;
  78 import tck.java.time.AbstractTCKTest;
  79 
  80 /**
  81  * Test WeekFields.
  82  */
  83 @Test
  84 public class TCKWeekFields extends AbstractTCKTest {
  85 
  86     @Test(dataProvider="weekFields")
  87     public void test_of_DayOfWeek_int_singleton(DayOfWeek firstDayOfWeek, int minDays) {
  88         WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
  89         assertEquals(week.getFirstDayOfWeek(), firstDayOfWeek, "Incorrect firstDayOfWeek");


  90         assertEquals(week.getMinimalDaysInFirstWeek(), minDays, "Incorrect MinimalDaysInFirstWeek");
  91         assertSame(WeekFields.of(firstDayOfWeek, minDays), week);
  92     }
  93 
  94     //-----------------------------------------------------------------------
  95     @Test
  96     public void test_dayOfWeekField_simpleGet() {
  97         LocalDate date = LocalDate.of(2000, 1, 10);  // Known to be ISO Monday
  98         assertEquals(date.get(WeekFields.ISO.dayOfWeek()), 1);
  99         assertEquals(date.get(WeekFields.of(DayOfWeek.MONDAY, 1).dayOfWeek()), 1);
 100         assertEquals(date.get(WeekFields.of(DayOfWeek.MONDAY, 7).dayOfWeek()), 1);
 101         assertEquals(date.get(WeekFields.SUNDAY_START.dayOfWeek()), 2);
 102         assertEquals(date.get(WeekFields.of(DayOfWeek.SUNDAY, 1).dayOfWeek()), 2);
 103         assertEquals(date.get(WeekFields.of(DayOfWeek.SUNDAY, 7).dayOfWeek()), 2);
 104         assertEquals(date.get(WeekFields.of(DayOfWeek.SATURDAY, 1).dayOfWeek()), 3);
 105         assertEquals(date.get(WeekFields.of(DayOfWeek.FRIDAY, 1).dayOfWeek()), 4);
 106         assertEquals(date.get(WeekFields.of(DayOfWeek.TUESDAY, 1).dayOfWeek()), 7);
 107     }
 108 
 109     @Test
 110     public void test_dayOfWeekField_simpleSet() {
 111         LocalDate date = LocalDate.of(2000, 1, 10);  // Known to be ISO Monday
 112         assertEquals(date.with(WeekFields.ISO.dayOfWeek(), 2), LocalDate.of(2000, 1, 11));
 113         assertEquals(date.with(WeekFields.ISO.dayOfWeek(), 7), LocalDate.of(2000, 1, 16));
 114 
 115         assertEquals(date.with(WeekFields.SUNDAY_START.dayOfWeek(), 3), LocalDate.of(2000, 1, 11));
 116         assertEquals(date.with(WeekFields.SUNDAY_START.dayOfWeek(), 7), LocalDate.of(2000, 1, 15));
 117 
 118         assertEquals(date.with(WeekFields.of(DayOfWeek.SATURDAY, 1).dayOfWeek(), 4), LocalDate.of(2000, 1, 11));
 119         assertEquals(date.with(WeekFields.of(DayOfWeek.TUESDAY, 1).dayOfWeek(), 1), LocalDate.of(2000, 1, 4));
 120     }
 121 
 122     @Test(dataProvider="weekFields")
 123     public void test_dayOfWeekField(DayOfWeek firstDayOfWeek, int minDays) {
 124         LocalDate day = LocalDate.of(2000, 1, 10);  // Known to be ISO Monday


 125         WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 126         TemporalField f = week.dayOfWeek();
 127         //System.out.printf("  Week: %s; field: %s%n", week, f);
 128 
 129         for (int i = 1; i <= 7; i++) {
 130             //System.out.printf("  ISO Dow: %s, WeekDOW ordinal: %s%n", day.getDayOfWeek(), day.get(f));
 131             assertEquals(day.get(f), (7 + day.getDayOfWeek().getValue() - firstDayOfWeek.getValue()) % 7 + 1);
 132             day = day.plusDays(1);
 133         }
 134     }


 135 
 136     @Test(dataProvider="weekFields")
 137     public void test_weekOfMonthField(DayOfWeek firstDayOfWeek, int minDays) {


 138         LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
 139         WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 140         TemporalField dowField = week.dayOfWeek();
 141         TemporalField womField = week.weekOfMonth();
 142         //System.err.printf("%n  Week: %s; dowField: %s, domField: %s%n", week, dowField, womField);
 143 
 144         DayOfWeek isoDOW = day.getDayOfWeek();
 145         int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;
 146 
 147         for (int i = 1; i <= 15; i++) {
 148             int actualDOW = day.get(dowField);
 149             int actualWOM = day.get(womField);
 150 
 151             // Verify that the combination of day of week and week of month can be used
 152             // to reconstruct the same date.
 153             LocalDate day1 = day.withDayOfMonth(1);
 154             int offset = - (day1.get(dowField) - 1);
 155             //System.err.printf("   refDay: %s%n", day1.plusDays(offset));
 156             int week1 = day1.get(womField);
 157             if (week1 == 0) {
 158                 // week of the 1st is partial; start with first full week
 159                 offset += 7;
 160             }
 161             //System.err.printf("   refDay2: %s, offset: %d, week1: %d%n", day1.plusDays(offset), offset, week1);
 162             offset += actualDOW - 1;
 163             //System.err.printf("   refDay3: %s%n", day1.plusDays(offset));
 164             offset += (actualWOM - 1) * 7;
 165             //System.err.printf("   refDay4: %s%n", day1.plusDays(offset));
 166             LocalDate result = day1.plusDays(offset);
 167 
 168             if (!day.equals(result)) {
 169                 System.err.printf("FAIL ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
 170                         day.getDayOfWeek(), offset, actualDOW, actualWOM, day, result);
 171             }
 172             assertEquals(result, day, "Incorrect dayOfWeek or weekOfMonth: "
 173                     + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
 174                     week, day.getDayOfWeek(), offset, actualDOW, actualWOM, day, result));
 175             day = day.plusDays(1);
 176         }
 177     }


 178 
 179     @Test(dataProvider="weekFields")
 180     public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) {


 181         LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
 182         WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 183         TemporalField dowField = week.dayOfWeek();
 184         TemporalField woyField = week.weekOfYear();
 185         //System.err.printf("%n  Year: %s; dowField: %s, woyField: %s%n", week, dowField, woyField);
 186 
 187         DayOfWeek isoDOW = day.getDayOfWeek();
 188         int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;
 189 
 190         for (int i = 1; i <= 15; i++) {
 191             int actualDOW = day.get(dowField);
 192             int actualWOY = day.get(woyField);
 193 
 194             // Verify that the combination of day of week and week of month can be used
 195             // to reconstruct the same date.
 196             LocalDate day1 = day.withDayOfYear(1);
 197             int offset = - (day1.get(dowField) - 1);
 198             //System.err.printf("   refDay: %s%n", day1.plusDays(offset));
 199             int week1 = day1.get(woyField);
 200             if (week1 == 0) {


 202                 offset += 7;
 203             }
 204             //System.err.printf("   refDay2: %s, offset: %d, week1: %d%n", day1.plusDays(offset), offset, week1);
 205             offset += actualDOW - 1;
 206             //System.err.printf("   refDay3: %s%n", day1.plusDays(offset));
 207             offset += (actualWOY - 1) * 7;
 208             //System.err.printf("   refDay4: %s%n", day1.plusDays(offset));
 209             LocalDate result = day1.plusDays(offset);
 210 
 211 
 212             if (!day.equals(result)) {
 213                 System.err.printf("FAIL  ISO Dow: %s, offset: %s, actualDOW: %s, actualWOY: %s, expected: %s, result: %s%n",
 214                         day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result);
 215             }
 216             assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
 217                     + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
 218                     week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result));
 219             day = day.plusDays(1);
 220         }
 221     }


 222 
 223     @Test(dataProvider="weekFields")
 224     public void test_fieldRanges(DayOfWeek firstDayOfWeek, int minDays) {


 225         WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);

 226         TemporalField womField = weekDef.weekOfMonth();
 227         TemporalField woyField = weekDef.weekOfYear();
 228 
 229         LocalDate day = LocalDate.of(2012, 11, 30);
 230         LocalDate endDay = LocalDate.of(2013, 1, 2);
 231         while (day.isBefore(endDay)) {
 232             LocalDate last = day.with(DAY_OF_MONTH, day.lengthOfMonth());
 233             int lastWOM = last.get(womField);
 234             LocalDate first = day.with(DAY_OF_MONTH, 1);
 235             int firstWOM = first.get(womField);
 236             ValueRange rangeWOM = day.range(womField);
 237             assertEquals(rangeWOM.getMinimum(), firstWOM,
 238                     "Range min should be same as WeekOfMonth for first day of month: "
 239                     + first + ", " + weekDef);
 240             assertEquals(rangeWOM.getMaximum(), lastWOM,
 241                     "Range max should be same as WeekOfMonth for last day of month: "
 242                     + last + ", " + weekDef);
 243 
 244             last = day.with(DAY_OF_YEAR, day.lengthOfYear());
 245             int lastWOY = last.get(woyField);
 246             first = day.with(DAY_OF_YEAR, 1);
 247             int firstWOY = first.get(woyField);
 248             ValueRange rangeWOY = day.range(woyField);
 249             assertEquals(rangeWOY.getMinimum(), firstWOY,
 250                     "Range min should be same as WeekOfYear for first day of Year: "
 251                     + day + ", " + weekDef);
 252             assertEquals(rangeWOY.getMaximum(), lastWOY,
 253                     "Range max should be same as WeekOfYear for last day of Year: "
 254                     + day + ", " + weekDef);
 255 
 256             day = day.plusDays(1);
 257         }
 258     }


 259 
 260     //-----------------------------------------------------------------------
 261     // withDayOfWeek()
 262     //-----------------------------------------------------------------------
 263     @Test(dataProvider="weekFields")
 264     public void test_withDayOfWeek(DayOfWeek firstDayOfWeek, int minDays) {


 265         LocalDate day = LocalDate.of(2012, 12, 15);  // Safely in the middle of a month
 266         WeekFields week = WeekFields.of(firstDayOfWeek, minDays);

 267         TemporalField dowField = week.dayOfWeek();
 268         TemporalField womField = week.weekOfMonth();
 269         TemporalField woyField = week.weekOfYear();
 270 
 271         int wom = day.get(womField);
 272         int woy = day.get(woyField);
 273         for (int dow = 1; dow <= 7; dow++) {
 274             LocalDate result = day.with(dowField, dow);
 275             if (result.get(dowField) != dow) {
 276                 System.err.printf(" DOW actual: %d, expected: %d, week:%s%n",
 277                         result.get(dowField), dow, week);
 278             }
 279             if (result.get(womField) != wom) {
 280                 System.err.printf(" WOM actual: %d, expected: %d, week:%s%n",
 281                         result.get(womField), wom, week);
 282             }
 283             if (result.get(woyField) != woy) {
 284                 System.err.printf(" WOY actual: %d, expected: %d, week:%s%n",
 285                         result.get(woyField), woy, week);
 286             }
 287             assertEquals(result.get(dowField), dow, String.format("Incorrect new Day of week: %s", result));
 288             assertEquals(result.get(womField), wom, "Week of Month should not change");
 289             assertEquals(result.get(woyField), woy, "Week of Year should not change");
 290         }
 291     }
 292 
 293     //-----------------------------------------------------------------------
 294     @Test(dataProvider="weekFields")
 295     public void test_parse_resolve_localizedWom(DayOfWeek firstDayOfWeek, int minDays) {
 296         LocalDate date = LocalDate.of(2012, 12, 15);
 297         WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 298         TemporalField womField = week.weekOfMonth();
 299 
 300         for (int i = 1; i <= 60; i++) {
 301             // Test that with dayOfWeek and Week of month it computes the date
 302             DateTimeFormatter f = new DateTimeFormatterBuilder()
 303                     .appendValue(YEAR).appendLiteral('-')
 304                     .appendValue(MONTH_OF_YEAR).appendLiteral('-')
 305                     .appendValue(womField).appendLiteral('-')
 306                     .appendValue(DAY_OF_WEEK).toFormatter();
 307             String str = date.getYear() + "-" + date.getMonthValue() + "-" +
 308                     date.get(womField) + "-" + date.get(DAY_OF_WEEK);
 309             LocalDate parsed = LocalDate.parse(str, f);
 310             assertEquals(parsed, date, " ::" + str + "::" + i);
 311 
 312             date = date.plusDays(1);
 313         }
 314     }
 315 
 316     @Test(dataProvider="weekFields")
 317     public void test_parse_resolve_localizedWomDow(DayOfWeek firstDayOfWeek, int minDays) {
 318         LocalDate date = LocalDate.of(2012, 12, 15);





 319         WeekFields week = WeekFields.of(firstDayOfWeek, minDays);

 320         TemporalField dowField = week.dayOfWeek();
 321         TemporalField womField = week.weekOfMonth();
 322 
 323         for (int i = 1; i <= 15; i++) {
 324             // Test that with dayOfWeek and Week of month it computes the date
 325             DateTimeFormatter f = new DateTimeFormatterBuilder()
 326                     .appendValue(YEAR).appendLiteral('-')
 327                     .appendValue(MONTH_OF_YEAR).appendLiteral('-')
 328                     .appendValue(womField).appendLiteral('-')
 329                     .appendValue(dowField).toFormatter();
 330             String str = date.getYear() + "-" + date.getMonthValue() + "-" +
 331                     date.get(womField) + "-" + date.get(dowField);
 332             LocalDate parsed = LocalDate.parse(str, f);
 333             assertEquals(parsed, date, " :: " + str + " " + i);
 334 
 335             date = date.plusDays(1);
 336         }
 337     }
 338 
 339     @Test(dataProvider="weekFields")
 340     public void test_parse_resolve_localizedWoy(DayOfWeek firstDayOfWeek, int minDays) {
 341         LocalDate date = LocalDate.of(2012, 12, 15);
 342         WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 343         TemporalField woyField = week.weekOfYear();















 344 
 345         for (int i = 1; i <= 60; i++) {
 346             // Test that with dayOfWeek and Week of month it computes the date
 347             DateTimeFormatter f = new DateTimeFormatterBuilder()
 348                     .appendValue(YEAR).appendLiteral('-')
 349                     .appendValue(MONTH_OF_YEAR).appendLiteral('-')
 350                     .appendValue(woyField).appendLiteral('-')
 351                     .appendValue(DAY_OF_WEEK).toFormatter();
 352             String str = date.getYear() + "-" + date.getMonthValue() + "-" +
 353                     date.get(woyField) + "-" + date.get(DAY_OF_WEEK);
 354             LocalDate parsed = LocalDate.parse(str, f);
 355             assertEquals(parsed, date, " :: " + str + " " + i);
 356 
 357             date = date.plusDays(1);
 358         }
 359     }








 360 
 361     @Test(dataProvider="weekFields")
 362     public void test_parse_resolve_localizedWoyDow(DayOfWeek firstDayOfWeek, int minDays) {
 363         LocalDate date = LocalDate.of(2012, 12, 15);
 364         WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
 365         TemporalField dowField = week.dayOfWeek();
 366         TemporalField woyField = week.weekOfYear();
 367 
 368         for (int i = 1; i <= 60; i++) {
 369             // Test that with dayOfWeek and Week of month it computes the date
 370             DateTimeFormatter f = new DateTimeFormatterBuilder()
 371                     .appendValue(YEAR).appendLiteral('-')
 372                     .appendValue(MONTH_OF_YEAR).appendLiteral('-')
 373                     .appendValue(woyField).appendLiteral('-')
 374                     .appendValue(dowField).toFormatter();
 375             String str = date.getYear() + "-" + date.getMonthValue() + "-" +
 376                     date.get(woyField) + "-" + date.get(dowField);
 377             LocalDate parsed = LocalDate.parse(str, f);
 378             assertEquals(parsed, date, " :: " + str + " " + i);
 379 
 380             date = date.plusDays(1);

 381         }
 382     }
 383 
 384     //-----------------------------------------------------------------------
 385     @Test(dataProvider="weekFields")
 386     public void test_serializable_singleton(DayOfWeek firstDayOfWeek, int minDays) throws IOException, ClassNotFoundException {
 387         WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
 388         assertSerializableSame(weekDef);  // spec state singleton
 389     }
 390 
 391     //-----------------------------------------------------------------------
 392     @DataProvider(name="weekFields")
 393     Object[][] data_weekFields() {
 394         Object[][] objects = new Object[49][];
 395         int i = 0;
 396         for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
 397             for (int minDays = 1; minDays <= 7; minDays++) {
 398                 objects[i++] = new Object[] {firstDayOfWeek, minDays};












 399             }
 400         }
 401         return objects;
 402     }
 403 
 404 }