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 java.time.format.DateTimeBuilder;
  60 import java.time.temporal.*;
  61 
  62 import static java.time.DayOfWeek.FRIDAY;
  63 import static java.time.DayOfWeek.MONDAY;
  64 import static java.time.DayOfWeek.SATURDAY;
  65 import static java.time.DayOfWeek.SUNDAY;
  66 import static java.time.DayOfWeek.THURSDAY;
  67 import static java.time.DayOfWeek.TUESDAY;
  68 import static java.time.DayOfWeek.WEDNESDAY;
  69 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
  70 import static java.time.temporal.ChronoField.YEAR;
  71 import static org.testng.Assert.assertEquals;
  72 import static org.testng.Assert.assertTrue;
  73 
  74 import java.time.DayOfWeek;
  75 import java.time.LocalDate;
  76 
  77 import org.testng.annotations.DataProvider;
  78 import org.testng.annotations.Test;
  79 
  80 /**
  81  * Test.
  82  */
  83 @Test(groups={"tck"})
  84 public class TCKISOFields {
  85 
  86     @DataProvider(name="quarter")
  87     Object[][] data_quarter() {
  88         return new Object[][] {
  89                 {LocalDate.of(1969, 12, 29), 90, 4},
  90                 {LocalDate.of(1969, 12, 30), 91, 4},
  91                 {LocalDate.of(1969, 12, 31), 92, 4},
  92 
  93                 {LocalDate.of(1970, 1, 1), 1, 1},
  94                 {LocalDate.of(1970, 1, 2), 2, 1},
  95                 {LocalDate.of(1970, 2, 28), 59, 1},
  96                 {LocalDate.of(1970, 3, 1), 60, 1},
  97                 {LocalDate.of(1970, 3, 31), 90, 1},
  98 
  99                 {LocalDate.of(1970, 4, 1), 1, 2},
 100                 {LocalDate.of(1970, 6, 30), 91, 2},
 101 
 102                 {LocalDate.of(1970, 7, 1), 1, 3},
 103                 {LocalDate.of(1970, 9, 30), 92, 3},
 104 
 105                 {LocalDate.of(1970, 10, 1), 1, 4},
 106                 {LocalDate.of(1970, 12, 31), 92, 4},
 107 
 108                 {LocalDate.of(1972, 2, 28), 59, 1},
 109                 {LocalDate.of(1972, 2, 29), 60, 1},
 110                 {LocalDate.of(1972, 3, 1), 61, 1},
 111                 {LocalDate.of(1972, 3, 31), 91, 1},
 112         };
 113     }
 114 
 115     //-----------------------------------------------------------------------
 116     // DAY_OF_QUARTER
 117     //-----------------------------------------------------------------------
 118     @Test(dataProvider="quarter")
 119     public void test_DOQ(LocalDate date, int doq, int qoy) {
 120         assertEquals(ISOFields.DAY_OF_QUARTER.doGet(date), doq);
 121         assertEquals(date.get(ISOFields.DAY_OF_QUARTER), doq);
 122     }
 123 
 124     //-----------------------------------------------------------------------
 125     // QUARTER_OF_YEAR
 126     //-----------------------------------------------------------------------
 127     @Test(dataProvider="quarter")
 128     public void test_QOY(LocalDate date, int doq, int qoy) {
 129         assertEquals(ISOFields.QUARTER_OF_YEAR.doGet(date), qoy);
 130         assertEquals(date.get(ISOFields.QUARTER_OF_YEAR), qoy);
 131     }
 132 
 133     //-----------------------------------------------------------------------
 134     // builder
 135     //-----------------------------------------------------------------------
 136     @Test(dataProvider="quarter")
 137     public void test_builder_quarters(LocalDate date, int doq, int qoy) {
 138         DateTimeBuilder builder = new DateTimeBuilder();
 139         builder.addFieldValue(ISOFields.DAY_OF_QUARTER, doq);
 140         builder.addFieldValue(ISOFields.QUARTER_OF_YEAR, qoy);
 141         builder.addFieldValue(YEAR, date.getYear());
 142         builder.resolve();
 143         assertEquals(builder.query(LocalDate::from), date);
 144     }
 145 
 146     //-----------------------------------------------------------------------
 147     //-----------------------------------------------------------------------
 148     //-----------------------------------------------------------------------
 149     @DataProvider(name="week")
 150     Object[][] data_week() {
 151         return new Object[][] {
 152                 {LocalDate.of(1969, 12, 29), MONDAY, 1, 1970},
 153                 {LocalDate.of(2012, 12, 23), SUNDAY, 51, 2012},
 154                 {LocalDate.of(2012, 12, 24), MONDAY, 52, 2012},
 155                 {LocalDate.of(2012, 12, 27), THURSDAY, 52, 2012},
 156                 {LocalDate.of(2012, 12, 28), FRIDAY, 52, 2012},
 157                 {LocalDate.of(2012, 12, 29), SATURDAY, 52, 2012},
 158                 {LocalDate.of(2012, 12, 30), SUNDAY, 52, 2012},
 159                 {LocalDate.of(2012, 12, 31), MONDAY, 1, 2013},
 160                 {LocalDate.of(2013, 1, 1), TUESDAY, 1, 2013},
 161                 {LocalDate.of(2013, 1, 2), WEDNESDAY, 1, 2013},
 162                 {LocalDate.of(2013, 1, 6), SUNDAY, 1, 2013},
 163                 {LocalDate.of(2013, 1, 7), MONDAY, 2, 2013},
 164         };
 165     }
 166 
 167     //-----------------------------------------------------------------------
 168     // WEEK_OF_WEEK_BASED_YEAR
 169     //-----------------------------------------------------------------------
 170     @Test(dataProvider="week")
 171     public void test_WOWBY(LocalDate date, DayOfWeek dow, int week, int wby) {
 172         assertEquals(date.getDayOfWeek(), dow);
 173         assertEquals(ISOFields.WEEK_OF_WEEK_BASED_YEAR.doGet(date), week);
 174         assertEquals(date.get(ISOFields.WEEK_OF_WEEK_BASED_YEAR), week);
 175     }
 176 
 177     //-----------------------------------------------------------------------
 178     // WEEK_BASED_YEAR
 179     //-----------------------------------------------------------------------
 180     @Test(dataProvider="week")
 181     public void test_WBY(LocalDate date, DayOfWeek dow, int week, int wby) {
 182         assertEquals(date.getDayOfWeek(), dow);
 183         assertEquals(ISOFields.WEEK_BASED_YEAR.doGet(date), wby);
 184         assertEquals(date.get(ISOFields.WEEK_BASED_YEAR), wby);
 185     }
 186 
 187     //-----------------------------------------------------------------------
 188     // builder
 189     //-----------------------------------------------------------------------
 190     @Test(dataProvider="week")
 191     public void test_builder_weeks(LocalDate date, DayOfWeek dow, int week, int wby) {
 192         DateTimeBuilder builder = new DateTimeBuilder();
 193         builder.addFieldValue(ISOFields.WEEK_BASED_YEAR, wby);
 194         builder.addFieldValue(ISOFields.WEEK_OF_WEEK_BASED_YEAR, week);
 195         builder.addFieldValue(DAY_OF_WEEK, dow.getValue());
 196         builder.resolve();
 197         assertEquals(builder.query(LocalDate::from), date);
 198     }
 199 
 200     //-----------------------------------------------------------------------
 201     public void test_loop() {
 202         // loop round at least one 400 year cycle, including before 1970
 203         LocalDate date = LocalDate.of(1960, 1, 5);  // Tuseday of week 1 1960
 204         int year = 1960;
 205         int wby = 1960;
 206         int weekLen = 52;
 207         int week = 1;
 208         while (date.getYear() < 2400) {
 209             DayOfWeek loopDow = date.getDayOfWeek();
 210             if (date.getYear() != year) {
 211                 year = date.getYear();
 212             }
 213             if (loopDow == MONDAY) {
 214                 week++;
 215                 if ((week == 53 && weekLen == 52) || week == 54) {
 216                     week = 1;
 217                     LocalDate firstDayOfWeekBasedYear = date.plusDays(14).withDayOfYear(1);
 218                     DayOfWeek firstDay = firstDayOfWeekBasedYear.getDayOfWeek();
 219                     weekLen = (firstDay == THURSDAY || (firstDay == WEDNESDAY && firstDayOfWeekBasedYear.isLeapYear()) ? 53 : 52);
 220                     wby++;
 221                 }
 222             }
 223             assertEquals(ISOFields.WEEK_OF_WEEK_BASED_YEAR.doRange(date), ValueRange.of(1, weekLen), "Failed on " + date + " " + date.getDayOfWeek());
 224             assertEquals(ISOFields.WEEK_OF_WEEK_BASED_YEAR.doGet(date), week, "Failed on " + date + " " + date.getDayOfWeek());
 225             assertEquals(date.get(ISOFields.WEEK_OF_WEEK_BASED_YEAR), week, "Failed on " + date + " " + date.getDayOfWeek());
 226             assertEquals(ISOFields.WEEK_BASED_YEAR.doGet(date), wby, "Failed on " + date + " " + date.getDayOfWeek());
 227             assertEquals(date.get(ISOFields.WEEK_BASED_YEAR), wby, "Failed on " + date + " " + date.getDayOfWeek());
 228             date = date.plusDays(1);
 229         }
 230     }
 231 
 232     // TODO: more tests
 233 }