1 /*
   2  * Copyright (c) 2012, 2013, 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 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) {
 190                         // week of the 1st is partial; start with first full week
 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 }