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.temporal.ChronoField.MONTH_OF_YEAR;
  63 import static org.testng.Assert.assertEquals;
  64 
  65 import java.time.DateTimeException;
  66 import java.time.LocalDate;
  67 import java.time.LocalTime;
  68 import java.time.Month;
  69 import java.time.chrono.IsoChronology;
  70 import java.time.format.TextStyle;
  71 import java.time.temporal.ChronoField;
  72 import java.time.temporal.ChronoUnit;
  73 import java.time.temporal.JulianFields;
  74 import java.time.temporal.Queries;
  75 import java.time.temporal.TemporalAccessor;
  76 import java.time.temporal.TemporalField;
  77 import java.time.temporal.TemporalQuery;
  78 import java.util.ArrayList;
  79 import java.util.Arrays;
  80 import java.util.List;
  81 import java.util.Locale;
  82 
  83 import org.testng.annotations.DataProvider;
  84 import org.testng.annotations.Test;
  85 
  86 /**
  87  * Test Month.
  88  */
  89 @Test
  90 public class TCKMonth extends AbstractDateTimeTest {
  91 
  92     private static final int MAX_LENGTH = 12;
  93 
  94     //-----------------------------------------------------------------------
  95     @Override
  96     protected List<TemporalAccessor> samples() {
  97         TemporalAccessor[] array = {Month.JANUARY, Month.JUNE, Month.DECEMBER, };
  98         return Arrays.asList(array);
  99     }
 100 
 101     @Override
 102     protected List<TemporalField> validFields() {
 103         TemporalField[] array = {
 104             MONTH_OF_YEAR,
 105         };
 106         return Arrays.asList(array);
 107     }
 108 
 109     @Override
 110     protected List<TemporalField> invalidFields() {
 111         List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
 112         list.removeAll(validFields());
 113         list.add(JulianFields.JULIAN_DAY);
 114         list.add(JulianFields.MODIFIED_JULIAN_DAY);
 115         list.add(JulianFields.RATA_DIE);
 116         return list;
 117     }
 118 
 119     //-----------------------------------------------------------------------
 120     @Test(groups={"tck"})
 121     public void test_factory_int_singleton() {
 122         for (int i = 1; i <= MAX_LENGTH; i++) {
 123             Month test = Month.of(i);
 124             assertEquals(test.getValue(), i);
 125         }
 126     }
 127 
 128     @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
 129     public void test_factory_int_tooLow() {
 130         Month.of(0);
 131     }
 132 
 133     @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
 134     public void test_factory_int_tooHigh() {
 135         Month.of(13);
 136     }
 137 
 138     //-----------------------------------------------------------------------
 139     @Test(groups={"tck"})
 140     public void test_factory_CalendricalObject() {
 141         assertEquals(Month.from(LocalDate.of(2011, 6, 6)), Month.JUNE);
 142     }
 143 
 144     @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
 145     public void test_factory_CalendricalObject_invalid_noDerive() {
 146         Month.from(LocalTime.of(12, 30));
 147     }
 148 
 149     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 150     public void test_factory_CalendricalObject_null() {
 151         Month.from((TemporalAccessor) null);
 152     }
 153 
 154     //-----------------------------------------------------------------------
 155     // get(TemporalField)
 156     //-----------------------------------------------------------------------
 157     @Test
 158     public void test_get_TemporalField() {
 159         assertEquals(Month.JULY.get(ChronoField.MONTH_OF_YEAR), 7);
 160     }
 161 
 162     @Test
 163     public void test_getLong_TemporalField() {
 164         assertEquals(Month.JULY.getLong(ChronoField.MONTH_OF_YEAR), 7);
 165     }
 166 
 167     //-----------------------------------------------------------------------
 168     // query(TemporalQuery)
 169     //-----------------------------------------------------------------------
 170     @DataProvider(name="query")
 171     Object[][] data_query() {
 172         return new Object[][] {
 173                 {Month.JUNE, Queries.chronology(), IsoChronology.INSTANCE},
 174                 {Month.JUNE, Queries.zoneId(), null},
 175                 {Month.JUNE, Queries.precision(), ChronoUnit.MONTHS},
 176                 {Month.JUNE, Queries.zone(), null},
 177                 {Month.JUNE, Queries.offset(), null},
 178                 {Month.JUNE, Queries.localDate(), null},
 179                 {Month.JUNE, Queries.localTime(), null},
 180         };
 181     }
 182 
 183     @Test(dataProvider="query")
 184     public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
 185         assertEquals(temporal.query(query), expected);
 186     }
 187 
 188     @Test(dataProvider="query")
 189     public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
 190         assertEquals(query.queryFrom(temporal), expected);
 191     }
 192 
 193     @Test(expectedExceptions=NullPointerException.class)
 194     public void test_query_null() {
 195         Month.JUNE.query(null);
 196     }
 197 
 198     //-----------------------------------------------------------------------
 199     // getText()
 200     //-----------------------------------------------------------------------
 201     @Test(groups={"tck"})
 202     public void test_getText() {
 203         assertEquals(Month.JANUARY.getDisplayName(TextStyle.SHORT, Locale.US), "Jan");
 204     }
 205 
 206     @Test(expectedExceptions = NullPointerException.class, groups={"tck"})
 207     public void test_getText_nullStyle() {
 208         Month.JANUARY.getDisplayName(null, Locale.US);
 209     }
 210 
 211     @Test(expectedExceptions = NullPointerException.class, groups={"tck"})
 212     public void test_getText_nullLocale() {
 213         Month.JANUARY.getDisplayName(TextStyle.FULL, null);
 214     }
 215 
 216     //-----------------------------------------------------------------------
 217     // plus(long), plus(long,unit)
 218     //-----------------------------------------------------------------------
 219     @DataProvider(name="plus")
 220     Object[][] data_plus() {
 221         return new Object[][] {
 222             {1, -13, 12},
 223             {1, -12, 1},
 224             {1, -11, 2},
 225             {1, -10, 3},
 226             {1, -9, 4},
 227             {1, -8, 5},
 228             {1, -7, 6},
 229             {1, -6, 7},
 230             {1, -5, 8},
 231             {1, -4, 9},
 232             {1, -3, 10},
 233             {1, -2, 11},
 234             {1, -1, 12},
 235             {1, 0, 1},
 236             {1, 1, 2},
 237             {1, 2, 3},
 238             {1, 3, 4},
 239             {1, 4, 5},
 240             {1, 5, 6},
 241             {1, 6, 7},
 242             {1, 7, 8},
 243             {1, 8, 9},
 244             {1, 9, 10},
 245             {1, 10, 11},
 246             {1, 11, 12},
 247             {1, 12, 1},
 248             {1, 13, 2},
 249 
 250             {1, 1, 2},
 251             {2, 1, 3},
 252             {3, 1, 4},
 253             {4, 1, 5},
 254             {5, 1, 6},
 255             {6, 1, 7},
 256             {7, 1, 8},
 257             {8, 1, 9},
 258             {9, 1, 10},
 259             {10, 1, 11},
 260             {11, 1, 12},
 261             {12, 1, 1},
 262 
 263             {1, -1, 12},
 264             {2, -1, 1},
 265             {3, -1, 2},
 266             {4, -1, 3},
 267             {5, -1, 4},
 268             {6, -1, 5},
 269             {7, -1, 6},
 270             {8, -1, 7},
 271             {9, -1, 8},
 272             {10, -1, 9},
 273             {11, -1, 10},
 274             {12, -1, 11},
 275         };
 276     }
 277 
 278     @Test(dataProvider="plus", groups={"tck"})
 279     public void test_plus_long(int base, long amount, int expected) {
 280         assertEquals(Month.of(base).plus(amount), Month.of(expected));
 281     }
 282 
 283     //-----------------------------------------------------------------------
 284     // minus(long), minus(long,unit)
 285     //-----------------------------------------------------------------------
 286     @DataProvider(name="minus")
 287     Object[][] data_minus() {
 288         return new Object[][] {
 289             {1, -13, 2},
 290             {1, -12, 1},
 291             {1, -11, 12},
 292             {1, -10, 11},
 293             {1, -9, 10},
 294             {1, -8, 9},
 295             {1, -7, 8},
 296             {1, -6, 7},
 297             {1, -5, 6},
 298             {1, -4, 5},
 299             {1, -3, 4},
 300             {1, -2, 3},
 301             {1, -1, 2},
 302             {1, 0, 1},
 303             {1, 1, 12},
 304             {1, 2, 11},
 305             {1, 3, 10},
 306             {1, 4, 9},
 307             {1, 5, 8},
 308             {1, 6, 7},
 309             {1, 7, 6},
 310             {1, 8, 5},
 311             {1, 9, 4},
 312             {1, 10, 3},
 313             {1, 11, 2},
 314             {1, 12, 1},
 315             {1, 13, 12},
 316         };
 317     }
 318 
 319     @Test(dataProvider="minus", groups={"tck"})
 320     public void test_minus_long(int base, long amount, int expected) {
 321         assertEquals(Month.of(base).minus(amount), Month.of(expected));
 322     }
 323 
 324     //-----------------------------------------------------------------------
 325     // length(boolean)
 326     //-----------------------------------------------------------------------
 327     @Test(groups={"tck"})
 328     public void test_length_boolean_notLeapYear() {
 329         assertEquals(Month.JANUARY.length(false), 31);
 330         assertEquals(Month.FEBRUARY.length(false), 28);
 331         assertEquals(Month.MARCH.length(false), 31);
 332         assertEquals(Month.APRIL.length(false), 30);
 333         assertEquals(Month.MAY.length(false), 31);
 334         assertEquals(Month.JUNE.length(false), 30);
 335         assertEquals(Month.JULY.length(false), 31);
 336         assertEquals(Month.AUGUST.length(false), 31);
 337         assertEquals(Month.SEPTEMBER.length(false), 30);
 338         assertEquals(Month.OCTOBER.length(false), 31);
 339         assertEquals(Month.NOVEMBER.length(false), 30);
 340         assertEquals(Month.DECEMBER.length(false), 31);
 341     }
 342 
 343     @Test(groups={"tck"})
 344     public void test_length_boolean_leapYear() {
 345         assertEquals(Month.JANUARY.length(true), 31);
 346         assertEquals(Month.FEBRUARY.length(true), 29);
 347         assertEquals(Month.MARCH.length(true), 31);
 348         assertEquals(Month.APRIL.length(true), 30);
 349         assertEquals(Month.MAY.length(true), 31);
 350         assertEquals(Month.JUNE.length(true), 30);
 351         assertEquals(Month.JULY.length(true), 31);
 352         assertEquals(Month.AUGUST.length(true), 31);
 353         assertEquals(Month.SEPTEMBER.length(true), 30);
 354         assertEquals(Month.OCTOBER.length(true), 31);
 355         assertEquals(Month.NOVEMBER.length(true), 30);
 356         assertEquals(Month.DECEMBER.length(true), 31);
 357     }
 358 
 359     //-----------------------------------------------------------------------
 360     // minLength()
 361     //-----------------------------------------------------------------------
 362     @Test(groups={"tck"})
 363     public void test_minLength() {
 364         assertEquals(Month.JANUARY.minLength(), 31);
 365         assertEquals(Month.FEBRUARY.minLength(), 28);
 366         assertEquals(Month.MARCH.minLength(), 31);
 367         assertEquals(Month.APRIL.minLength(), 30);
 368         assertEquals(Month.MAY.minLength(), 31);
 369         assertEquals(Month.JUNE.minLength(), 30);
 370         assertEquals(Month.JULY.minLength(), 31);
 371         assertEquals(Month.AUGUST.minLength(), 31);
 372         assertEquals(Month.SEPTEMBER.minLength(), 30);
 373         assertEquals(Month.OCTOBER.minLength(), 31);
 374         assertEquals(Month.NOVEMBER.minLength(), 30);
 375         assertEquals(Month.DECEMBER.minLength(), 31);
 376     }
 377 
 378     //-----------------------------------------------------------------------
 379     // maxLength()
 380     //-----------------------------------------------------------------------
 381     @Test(groups={"tck"})
 382     public void test_maxLength() {
 383         assertEquals(Month.JANUARY.maxLength(), 31);
 384         assertEquals(Month.FEBRUARY.maxLength(), 29);
 385         assertEquals(Month.MARCH.maxLength(), 31);
 386         assertEquals(Month.APRIL.maxLength(), 30);
 387         assertEquals(Month.MAY.maxLength(), 31);
 388         assertEquals(Month.JUNE.maxLength(), 30);
 389         assertEquals(Month.JULY.maxLength(), 31);
 390         assertEquals(Month.AUGUST.maxLength(), 31);
 391         assertEquals(Month.SEPTEMBER.maxLength(), 30);
 392         assertEquals(Month.OCTOBER.maxLength(), 31);
 393         assertEquals(Month.NOVEMBER.maxLength(), 30);
 394         assertEquals(Month.DECEMBER.maxLength(), 31);
 395     }
 396 
 397     //-----------------------------------------------------------------------
 398     // firstDayOfYear(boolean)
 399     //-----------------------------------------------------------------------
 400     @Test(groups={"tck"})
 401     public void test_firstDayOfYear_notLeapYear() {
 402         assertEquals(Month.JANUARY.firstDayOfYear(false), 1);
 403         assertEquals(Month.FEBRUARY.firstDayOfYear(false), 1 + 31);
 404         assertEquals(Month.MARCH.firstDayOfYear(false), 1 + 31 + 28);
 405         assertEquals(Month.APRIL.firstDayOfYear(false), 1 + 31 + 28 + 31);
 406         assertEquals(Month.MAY.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30);
 407         assertEquals(Month.JUNE.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31);
 408         assertEquals(Month.JULY.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30);
 409         assertEquals(Month.AUGUST.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31);
 410         assertEquals(Month.SEPTEMBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31);
 411         assertEquals(Month.OCTOBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
 412         assertEquals(Month.NOVEMBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
 413         assertEquals(Month.DECEMBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
 414     }
 415 
 416     @Test(groups={"tck"})
 417     public void test_firstDayOfYear_leapYear() {
 418         assertEquals(Month.JANUARY.firstDayOfYear(true), 1);
 419         assertEquals(Month.FEBRUARY.firstDayOfYear(true), 1 + 31);
 420         assertEquals(Month.MARCH.firstDayOfYear(true), 1 + 31 + 29);
 421         assertEquals(Month.APRIL.firstDayOfYear(true), 1 + 31 + 29 + 31);
 422         assertEquals(Month.MAY.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30);
 423         assertEquals(Month.JUNE.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31);
 424         assertEquals(Month.JULY.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30);
 425         assertEquals(Month.AUGUST.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31);
 426         assertEquals(Month.SEPTEMBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31);
 427         assertEquals(Month.OCTOBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
 428         assertEquals(Month.NOVEMBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
 429         assertEquals(Month.DECEMBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
 430     }
 431 
 432     //-----------------------------------------------------------------------
 433     // firstMonthOfQuarter()
 434     //-----------------------------------------------------------------------
 435     @Test(groups={"tck"})
 436     public void test_firstMonthOfQuarter() {
 437         assertEquals(Month.JANUARY.firstMonthOfQuarter(), Month.JANUARY);
 438         assertEquals(Month.FEBRUARY.firstMonthOfQuarter(), Month.JANUARY);
 439         assertEquals(Month.MARCH.firstMonthOfQuarter(), Month.JANUARY);
 440         assertEquals(Month.APRIL.firstMonthOfQuarter(), Month.APRIL);
 441         assertEquals(Month.MAY.firstMonthOfQuarter(), Month.APRIL);
 442         assertEquals(Month.JUNE.firstMonthOfQuarter(), Month.APRIL);
 443         assertEquals(Month.JULY.firstMonthOfQuarter(), Month.JULY);
 444         assertEquals(Month.AUGUST.firstMonthOfQuarter(), Month.JULY);
 445         assertEquals(Month.SEPTEMBER.firstMonthOfQuarter(), Month.JULY);
 446         assertEquals(Month.OCTOBER.firstMonthOfQuarter(), Month.OCTOBER);
 447         assertEquals(Month.NOVEMBER.firstMonthOfQuarter(), Month.OCTOBER);
 448         assertEquals(Month.DECEMBER.firstMonthOfQuarter(), Month.OCTOBER);
 449     }
 450 
 451     //-----------------------------------------------------------------------
 452     // toString()
 453     //-----------------------------------------------------------------------
 454     @Test(groups={"tck"})
 455     public void test_toString() {
 456         assertEquals(Month.JANUARY.toString(), "JANUARY");
 457         assertEquals(Month.FEBRUARY.toString(), "FEBRUARY");
 458         assertEquals(Month.MARCH.toString(), "MARCH");
 459         assertEquals(Month.APRIL.toString(), "APRIL");
 460         assertEquals(Month.MAY.toString(), "MAY");
 461         assertEquals(Month.JUNE.toString(), "JUNE");
 462         assertEquals(Month.JULY.toString(), "JULY");
 463         assertEquals(Month.AUGUST.toString(), "AUGUST");
 464         assertEquals(Month.SEPTEMBER.toString(), "SEPTEMBER");
 465         assertEquals(Month.OCTOBER.toString(), "OCTOBER");
 466         assertEquals(Month.NOVEMBER.toString(), "NOVEMBER");
 467         assertEquals(Month.DECEMBER.toString(), "DECEMBER");
 468     }
 469 
 470     //-----------------------------------------------------------------------
 471     // generated methods
 472     //-----------------------------------------------------------------------
 473     @Test(groups={"tck"})
 474     public void test_enum() {
 475         assertEquals(Month.valueOf("JANUARY"), Month.JANUARY);
 476         assertEquals(Month.values()[0], Month.JANUARY);
 477     }
 478 
 479 }