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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * This file is available under and governed by the GNU General Public
  26  * License version 2 only, as published by the Free Software Foundation.
  27  * However, the following notice accompanied the original version of this
  28  * file:
  29  *
  30  * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
  31  *
  32  * All rights reserved.
  33  *
  34  * Redistribution and use in source and binary forms, with or without
  35  * modification, are permitted provided that the following conditions are met:
  36  *
  37  *  * Redistributions of source code must retain the above copyright notice,
  38  *    this list of conditions and the following disclaimer.
  39  *
  40  *  * Redistributions in binary form must reproduce the above copyright notice,
  41  *    this list of conditions and the following disclaimer in the documentation
  42  *    and/or other materials provided with the distribution.
  43  *
  44  *  * Neither the name of JSR-310 nor the names of its contributors
  45  *    may be used to endorse or promote products derived from this software
  46  *    without specific prior written permission.
  47  *
  48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59  */
  60 package tck.java.time;
  61 
  62 import static java.time.DayOfWeek.MONDAY;
  63 import static java.time.DayOfWeek.SUNDAY;
  64 import static java.time.DayOfWeek.WEDNESDAY;
  65 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
  66 import static org.testng.Assert.assertEquals;
  67 import static org.testng.Assert.assertSame;
  68 
  69 import java.time.DateTimeException;
  70 import java.time.DayOfWeek;
  71 import java.time.LocalDate;
  72 import java.time.LocalTime;
  73 import java.time.chrono.IsoChronology;
  74 import java.time.format.TextStyle;
  75 import java.time.temporal.ChronoField;
  76 import java.time.temporal.ChronoUnit;
  77 import java.time.temporal.JulianFields;
  78 import java.time.temporal.Queries;
  79 import java.time.temporal.Temporal;
  80 import java.time.temporal.TemporalAccessor;
  81 import java.time.temporal.TemporalField;
  82 import java.time.temporal.TemporalQuery;
  83 import java.util.ArrayList;
  84 import java.util.Arrays;
  85 import java.util.List;
  86 import java.util.Locale;
  87 
  88 import org.testng.annotations.BeforeMethod;
  89 import org.testng.annotations.DataProvider;
  90 import org.testng.annotations.Test;
  91 
  92 /**
  93  * Test DayOfWeek.
  94  */
  95 @Test
  96 public class TCKDayOfWeek extends AbstractDateTimeTest {
  97 
  98     @BeforeMethod
  99     public void setUp() {
 100     }
 101 
 102     //-----------------------------------------------------------------------
 103     @Override
 104     protected List<TemporalAccessor> samples() {
 105         TemporalAccessor[] array = {MONDAY, WEDNESDAY, SUNDAY, };
 106         return Arrays.asList(array);
 107     }
 108 
 109     @Override
 110     protected List<TemporalField> validFields() {
 111         TemporalField[] array = {
 112             DAY_OF_WEEK,
 113         };
 114         return Arrays.asList(array);
 115     }
 116 
 117     @Override
 118     protected List<TemporalField> invalidFields() {
 119         List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
 120         list.removeAll(validFields());
 121         list.add(JulianFields.JULIAN_DAY);
 122         list.add(JulianFields.MODIFIED_JULIAN_DAY);
 123         list.add(JulianFields.RATA_DIE);
 124         return list;
 125     }
 126 
 127     //-----------------------------------------------------------------------
 128     @Test(groups={"tck"})
 129     public void test_factory_int_singleton() {
 130         for (int i = 1; i <= 7; i++) {
 131             DayOfWeek test = DayOfWeek.of(i);
 132             assertEquals(test.getValue(), i);
 133             assertSame(DayOfWeek.of(i), test);
 134         }
 135     }
 136 
 137     @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
 138     public void test_factory_int_valueTooLow() {
 139         DayOfWeek.of(0);
 140     }
 141 
 142     @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
 143     public void test_factory_int_valueTooHigh() {
 144         DayOfWeek.of(8);
 145     }
 146 
 147     //-----------------------------------------------------------------------
 148     @Test(groups={"tck"})
 149     public void test_factory_CalendricalObject() {
 150         assertEquals(DayOfWeek.from(LocalDate.of(2011, 6, 6)), DayOfWeek.MONDAY);
 151     }
 152 
 153     @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
 154     public void test_factory_CalendricalObject_invalid_noDerive() {
 155         DayOfWeek.from(LocalTime.of(12, 30));
 156     }
 157 
 158     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 159     public void test_factory_CalendricalObject_null() {
 160         DayOfWeek.from((TemporalAccessor) null);
 161     }
 162 
 163     //-----------------------------------------------------------------------
 164     // get(TemporalField)
 165     //-----------------------------------------------------------------------
 166     @Test
 167     public void test_get_TemporalField() {
 168         assertEquals(DayOfWeek.WEDNESDAY.getLong(ChronoField.DAY_OF_WEEK), 3);
 169     }
 170 
 171     @Test
 172     public void test_getLong_TemporalField() {
 173         assertEquals(DayOfWeek.WEDNESDAY.getLong(ChronoField.DAY_OF_WEEK), 3);
 174     }
 175 
 176     //-----------------------------------------------------------------------
 177     // query(TemporalQuery)
 178     //-----------------------------------------------------------------------
 179     @DataProvider(name="query")
 180     Object[][] data_query() {
 181         return new Object[][] {
 182                 {DayOfWeek.FRIDAY, Queries.chronology(), null},
 183                 {DayOfWeek.FRIDAY, Queries.zoneId(), null},
 184                 {DayOfWeek.FRIDAY, Queries.precision(), ChronoUnit.DAYS},
 185                 {DayOfWeek.FRIDAY, Queries.zone(), null},
 186                 {DayOfWeek.FRIDAY, Queries.offset(), null},
 187                 {DayOfWeek.FRIDAY, Queries.localDate(), null},
 188                 {DayOfWeek.FRIDAY, Queries.localTime(), null},
 189         };
 190     }
 191 
 192     @Test(dataProvider="query")
 193     public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
 194         assertEquals(temporal.query(query), expected);
 195     }
 196 
 197     @Test(dataProvider="query")
 198     public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
 199         assertEquals(query.queryFrom(temporal), expected);
 200     }
 201 
 202     @Test(expectedExceptions=NullPointerException.class)
 203     public void test_query_null() {
 204         DayOfWeek.FRIDAY.query(null);
 205     }
 206 
 207     //-----------------------------------------------------------------------
 208     // getText()
 209     //-----------------------------------------------------------------------
 210     @Test(groups={"tck"})
 211     public void test_getText() {
 212         assertEquals(DayOfWeek.MONDAY.getDisplayName(TextStyle.SHORT, Locale.US), "Mon");
 213     }
 214 
 215     @Test(expectedExceptions = NullPointerException.class, groups={"tck"})
 216     public void test_getText_nullStyle() {
 217         DayOfWeek.MONDAY.getDisplayName(null, Locale.US);
 218     }
 219 
 220     @Test(expectedExceptions = NullPointerException.class, groups={"tck"})
 221     public void test_getText_nullLocale() {
 222         DayOfWeek.MONDAY.getDisplayName(TextStyle.FULL, null);
 223     }
 224 
 225     //-----------------------------------------------------------------------
 226     // plus(long), plus(long,unit)
 227     //-----------------------------------------------------------------------
 228     @DataProvider(name="plus")
 229     Object[][] data_plus() {
 230         return new Object[][] {
 231             {1, -8, 7},
 232             {1, -7, 1},
 233             {1, -6, 2},
 234             {1, -5, 3},
 235             {1, -4, 4},
 236             {1, -3, 5},
 237             {1, -2, 6},
 238             {1, -1, 7},
 239             {1, 0, 1},
 240             {1, 1, 2},
 241             {1, 2, 3},
 242             {1, 3, 4},
 243             {1, 4, 5},
 244             {1, 5, 6},
 245             {1, 6, 7},
 246             {1, 7, 1},
 247             {1, 8, 2},
 248 
 249             {1, 1, 2},
 250             {2, 1, 3},
 251             {3, 1, 4},
 252             {4, 1, 5},
 253             {5, 1, 6},
 254             {6, 1, 7},
 255             {7, 1, 1},
 256 
 257             {1, -1, 7},
 258             {2, -1, 1},
 259             {3, -1, 2},
 260             {4, -1, 3},
 261             {5, -1, 4},
 262             {6, -1, 5},
 263             {7, -1, 6},
 264         };
 265     }
 266 
 267     @Test(dataProvider="plus", groups={"tck"})
 268     public void test_plus_long(int base, long amount, int expected) {
 269         assertEquals(DayOfWeek.of(base).plus(amount), DayOfWeek.of(expected));
 270     }
 271 
 272     //-----------------------------------------------------------------------
 273     // minus(long), minus(long,unit)
 274     //-----------------------------------------------------------------------
 275     @DataProvider(name="minus")
 276     Object[][] data_minus() {
 277         return new Object[][] {
 278             {1, -8, 2},
 279             {1, -7, 1},
 280             {1, -6, 7},
 281             {1, -5, 6},
 282             {1, -4, 5},
 283             {1, -3, 4},
 284             {1, -2, 3},
 285             {1, -1, 2},
 286             {1, 0, 1},
 287             {1, 1, 7},
 288             {1, 2, 6},
 289             {1, 3, 5},
 290             {1, 4, 4},
 291             {1, 5, 3},
 292             {1, 6, 2},
 293             {1, 7, 1},
 294             {1, 8, 7},
 295         };
 296     }
 297 
 298     @Test(dataProvider="minus", groups={"tck"})
 299     public void test_minus_long(int base, long amount, int expected) {
 300         assertEquals(DayOfWeek.of(base).minus(amount), DayOfWeek.of(expected));
 301     }
 302 
 303     //-----------------------------------------------------------------------
 304     // adjustInto()
 305     //-----------------------------------------------------------------------
 306     @Test(groups={"tck"})
 307     public void test_adjustInto() {
 308         assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 2)), LocalDate.of(2012, 8, 27));
 309         assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 3)), LocalDate.of(2012, 9, 3));
 310         assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 4)), LocalDate.of(2012, 9, 3));
 311         assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 10)), LocalDate.of(2012, 9, 10));
 312         assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 11)), LocalDate.of(2012, 9, 10));
 313     }
 314 
 315     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 316     public void test_adjustInto_null() {
 317         DayOfWeek.MONDAY.adjustInto((Temporal) null);
 318     }
 319 
 320     //-----------------------------------------------------------------------
 321     // toString()
 322     //-----------------------------------------------------------------------
 323     @Test(groups={"tck"})
 324     public void test_toString() {
 325         assertEquals(DayOfWeek.MONDAY.toString(), "MONDAY");
 326         assertEquals(DayOfWeek.TUESDAY.toString(), "TUESDAY");
 327         assertEquals(DayOfWeek.WEDNESDAY.toString(), "WEDNESDAY");
 328         assertEquals(DayOfWeek.THURSDAY.toString(), "THURSDAY");
 329         assertEquals(DayOfWeek.FRIDAY.toString(), "FRIDAY");
 330         assertEquals(DayOfWeek.SATURDAY.toString(), "SATURDAY");
 331         assertEquals(DayOfWeek.SUNDAY.toString(), "SUNDAY");
 332     }
 333 
 334     //-----------------------------------------------------------------------
 335     // generated methods
 336     //-----------------------------------------------------------------------
 337     @Test(groups={"tck"})
 338     public void test_enum() {
 339         assertEquals(DayOfWeek.valueOf("MONDAY"), DayOfWeek.MONDAY);
 340         assertEquals(DayOfWeek.values()[0], DayOfWeek.MONDAY);
 341     }
 342 
 343 }