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.util.ArrayList;
  66 import java.util.Arrays;
  67 import java.util.List;
  68 import java.util.Locale;
  69 
  70 import java.time.DateTimeException;
  71 import java.time.LocalDate;
  72 import java.time.LocalTime;
  73 import java.time.Month;
  74 import java.time.format.TextStyle;
  75 import java.time.temporal.ChronoField;
  76 import java.time.temporal.ChronoUnit;
  77 import java.time.temporal.ISOChrono;
  78 import java.time.temporal.JulianFields;
  79 import java.time.temporal.Queries;
  80 import java.time.temporal.TemporalAccessor;
  81 import java.time.temporal.TemporalField;
  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     @Test
 171     public void test_query_chrono() {
 172         assertEquals(Month.JUNE.query(Queries.chrono()), ISOChrono.INSTANCE);
 173         assertEquals(Queries.chrono().queryFrom(Month.JUNE), ISOChrono.INSTANCE);
 174     }
 175 
 176     @Test
 177     public void test_query_zoneId() {
 178         assertEquals(Month.JUNE.query(Queries.zoneId()), null);
 179         assertEquals(Queries.zoneId().queryFrom(Month.JUNE), null);
 180     }
 181 
 182     @Test
 183     public void test_query_precision() {
 184         assertEquals(Month.JUNE.query(Queries.precision()), ChronoUnit.MONTHS);
 185         assertEquals(Queries.precision().queryFrom(Month.JUNE), ChronoUnit.MONTHS);
 186     }
 187 
 188     @Test
 189     public void test_query_offset() {
 190         assertEquals(Month.JUNE.query(Queries.offset()), null);
 191         assertEquals(Queries.offset().queryFrom(Month.JUNE), null);
 192     }
 193 
 194     @Test
 195     public void test_query_zone() {
 196         assertEquals(Month.JUNE.query(Queries.zone()), null);
 197         assertEquals(Queries.zone().queryFrom(Month.JUNE), null);
 198     }
 199 
 200     @Test(expectedExceptions=NullPointerException.class)
 201     public void test_query_null() {
 202         Month.JUNE.query(null);
 203     }
 204 
 205     //-----------------------------------------------------------------------
 206     // getText()
 207     //-----------------------------------------------------------------------
 208     @Test(groups={"tck"})
 209     public void test_getText() {
 210         assertEquals(Month.JANUARY.getText(TextStyle.SHORT, Locale.US), "Jan");
 211     }
 212 
 213     @Test(expectedExceptions = NullPointerException.class, groups={"tck"})
 214     public void test_getText_nullStyle() {
 215         Month.JANUARY.getText(null, Locale.US);
 216     }
 217 
 218     @Test(expectedExceptions = NullPointerException.class, groups={"tck"})
 219     public void test_getText_nullLocale() {
 220         Month.JANUARY.getText(TextStyle.FULL, null);
 221     }
 222 
 223     //-----------------------------------------------------------------------
 224     // plus(long), plus(long,unit)
 225     //-----------------------------------------------------------------------
 226     @DataProvider(name="plus")
 227     Object[][] data_plus() {
 228         return new Object[][] {
 229             {1, -13, 12},
 230             {1, -12, 1},
 231             {1, -11, 2},
 232             {1, -10, 3},
 233             {1, -9, 4},
 234             {1, -8, 5},
 235             {1, -7, 6},
 236             {1, -6, 7},
 237             {1, -5, 8},
 238             {1, -4, 9},
 239             {1, -3, 10},
 240             {1, -2, 11},
 241             {1, -1, 12},
 242             {1, 0, 1},
 243             {1, 1, 2},
 244             {1, 2, 3},
 245             {1, 3, 4},
 246             {1, 4, 5},
 247             {1, 5, 6},
 248             {1, 6, 7},
 249             {1, 7, 8},
 250             {1, 8, 9},
 251             {1, 9, 10},
 252             {1, 10, 11},
 253             {1, 11, 12},
 254             {1, 12, 1},
 255             {1, 13, 2},
 256 
 257             {1, 1, 2},
 258             {2, 1, 3},
 259             {3, 1, 4},
 260             {4, 1, 5},
 261             {5, 1, 6},
 262             {6, 1, 7},
 263             {7, 1, 8},
 264             {8, 1, 9},
 265             {9, 1, 10},
 266             {10, 1, 11},
 267             {11, 1, 12},
 268             {12, 1, 1},
 269 
 270             {1, -1, 12},
 271             {2, -1, 1},
 272             {3, -1, 2},
 273             {4, -1, 3},
 274             {5, -1, 4},
 275             {6, -1, 5},
 276             {7, -1, 6},
 277             {8, -1, 7},
 278             {9, -1, 8},
 279             {10, -1, 9},
 280             {11, -1, 10},
 281             {12, -1, 11},
 282         };
 283     }
 284 
 285     @Test(dataProvider="plus", groups={"tck"})
 286     public void test_plus_long(int base, long amount, int expected) {
 287         assertEquals(Month.of(base).plus(amount), Month.of(expected));
 288     }
 289 
 290     //-----------------------------------------------------------------------
 291     // minus(long), minus(long,unit)
 292     //-----------------------------------------------------------------------
 293     @DataProvider(name="minus")
 294     Object[][] data_minus() {
 295         return new Object[][] {
 296             {1, -13, 2},
 297             {1, -12, 1},
 298             {1, -11, 12},
 299             {1, -10, 11},
 300             {1, -9, 10},
 301             {1, -8, 9},
 302             {1, -7, 8},
 303             {1, -6, 7},
 304             {1, -5, 6},
 305             {1, -4, 5},
 306             {1, -3, 4},
 307             {1, -2, 3},
 308             {1, -1, 2},
 309             {1, 0, 1},
 310             {1, 1, 12},
 311             {1, 2, 11},
 312             {1, 3, 10},
 313             {1, 4, 9},
 314             {1, 5, 8},
 315             {1, 6, 7},
 316             {1, 7, 6},
 317             {1, 8, 5},
 318             {1, 9, 4},
 319             {1, 10, 3},
 320             {1, 11, 2},
 321             {1, 12, 1},
 322             {1, 13, 12},
 323         };
 324     }
 325 
 326     @Test(dataProvider="minus", groups={"tck"})
 327     public void test_minus_long(int base, long amount, int expected) {
 328         assertEquals(Month.of(base).minus(amount), Month.of(expected));
 329     }
 330 
 331     //-----------------------------------------------------------------------
 332     // length(boolean)
 333     //-----------------------------------------------------------------------
 334     @Test(groups={"tck"})
 335     public void test_length_boolean_notLeapYear() {
 336         assertEquals(Month.JANUARY.length(false), 31);
 337         assertEquals(Month.FEBRUARY.length(false), 28);
 338         assertEquals(Month.MARCH.length(false), 31);
 339         assertEquals(Month.APRIL.length(false), 30);
 340         assertEquals(Month.MAY.length(false), 31);
 341         assertEquals(Month.JUNE.length(false), 30);
 342         assertEquals(Month.JULY.length(false), 31);
 343         assertEquals(Month.AUGUST.length(false), 31);
 344         assertEquals(Month.SEPTEMBER.length(false), 30);
 345         assertEquals(Month.OCTOBER.length(false), 31);
 346         assertEquals(Month.NOVEMBER.length(false), 30);
 347         assertEquals(Month.DECEMBER.length(false), 31);
 348     }
 349 
 350     @Test(groups={"tck"})
 351     public void test_length_boolean_leapYear() {
 352         assertEquals(Month.JANUARY.length(true), 31);
 353         assertEquals(Month.FEBRUARY.length(true), 29);
 354         assertEquals(Month.MARCH.length(true), 31);
 355         assertEquals(Month.APRIL.length(true), 30);
 356         assertEquals(Month.MAY.length(true), 31);
 357         assertEquals(Month.JUNE.length(true), 30);
 358         assertEquals(Month.JULY.length(true), 31);
 359         assertEquals(Month.AUGUST.length(true), 31);
 360         assertEquals(Month.SEPTEMBER.length(true), 30);
 361         assertEquals(Month.OCTOBER.length(true), 31);
 362         assertEquals(Month.NOVEMBER.length(true), 30);
 363         assertEquals(Month.DECEMBER.length(true), 31);
 364     }
 365 
 366     //-----------------------------------------------------------------------
 367     // minLength()
 368     //-----------------------------------------------------------------------
 369     @Test(groups={"tck"})
 370     public void test_minLength() {
 371         assertEquals(Month.JANUARY.minLength(), 31);
 372         assertEquals(Month.FEBRUARY.minLength(), 28);
 373         assertEquals(Month.MARCH.minLength(), 31);
 374         assertEquals(Month.APRIL.minLength(), 30);
 375         assertEquals(Month.MAY.minLength(), 31);
 376         assertEquals(Month.JUNE.minLength(), 30);
 377         assertEquals(Month.JULY.minLength(), 31);
 378         assertEquals(Month.AUGUST.minLength(), 31);
 379         assertEquals(Month.SEPTEMBER.minLength(), 30);
 380         assertEquals(Month.OCTOBER.minLength(), 31);
 381         assertEquals(Month.NOVEMBER.minLength(), 30);
 382         assertEquals(Month.DECEMBER.minLength(), 31);
 383     }
 384 
 385     //-----------------------------------------------------------------------
 386     // maxLength()
 387     //-----------------------------------------------------------------------
 388     @Test(groups={"tck"})
 389     public void test_maxLength() {
 390         assertEquals(Month.JANUARY.maxLength(), 31);
 391         assertEquals(Month.FEBRUARY.maxLength(), 29);
 392         assertEquals(Month.MARCH.maxLength(), 31);
 393         assertEquals(Month.APRIL.maxLength(), 30);
 394         assertEquals(Month.MAY.maxLength(), 31);
 395         assertEquals(Month.JUNE.maxLength(), 30);
 396         assertEquals(Month.JULY.maxLength(), 31);
 397         assertEquals(Month.AUGUST.maxLength(), 31);
 398         assertEquals(Month.SEPTEMBER.maxLength(), 30);
 399         assertEquals(Month.OCTOBER.maxLength(), 31);
 400         assertEquals(Month.NOVEMBER.maxLength(), 30);
 401         assertEquals(Month.DECEMBER.maxLength(), 31);
 402     }
 403 
 404     //-----------------------------------------------------------------------
 405     // firstDayOfYear(boolean)
 406     //-----------------------------------------------------------------------
 407     @Test(groups={"tck"})
 408     public void test_firstDayOfYear_notLeapYear() {
 409         assertEquals(Month.JANUARY.firstDayOfYear(false), 1);
 410         assertEquals(Month.FEBRUARY.firstDayOfYear(false), 1 + 31);
 411         assertEquals(Month.MARCH.firstDayOfYear(false), 1 + 31 + 28);
 412         assertEquals(Month.APRIL.firstDayOfYear(false), 1 + 31 + 28 + 31);
 413         assertEquals(Month.MAY.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30);
 414         assertEquals(Month.JUNE.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31);
 415         assertEquals(Month.JULY.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30);
 416         assertEquals(Month.AUGUST.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31);
 417         assertEquals(Month.SEPTEMBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31);
 418         assertEquals(Month.OCTOBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
 419         assertEquals(Month.NOVEMBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
 420         assertEquals(Month.DECEMBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
 421     }
 422 
 423     @Test(groups={"tck"})
 424     public void test_firstDayOfYear_leapYear() {
 425         assertEquals(Month.JANUARY.firstDayOfYear(true), 1);
 426         assertEquals(Month.FEBRUARY.firstDayOfYear(true), 1 + 31);
 427         assertEquals(Month.MARCH.firstDayOfYear(true), 1 + 31 + 29);
 428         assertEquals(Month.APRIL.firstDayOfYear(true), 1 + 31 + 29 + 31);
 429         assertEquals(Month.MAY.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30);
 430         assertEquals(Month.JUNE.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31);
 431         assertEquals(Month.JULY.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30);
 432         assertEquals(Month.AUGUST.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31);
 433         assertEquals(Month.SEPTEMBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31);
 434         assertEquals(Month.OCTOBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
 435         assertEquals(Month.NOVEMBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
 436         assertEquals(Month.DECEMBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
 437     }
 438 
 439     //-----------------------------------------------------------------------
 440     // firstMonthOfQuarter()
 441     //-----------------------------------------------------------------------
 442     @Test(groups={"tck"})
 443     public void test_firstMonthOfQuarter() {
 444         assertEquals(Month.JANUARY.firstMonthOfQuarter(), Month.JANUARY);
 445         assertEquals(Month.FEBRUARY.firstMonthOfQuarter(), Month.JANUARY);
 446         assertEquals(Month.MARCH.firstMonthOfQuarter(), Month.JANUARY);
 447         assertEquals(Month.APRIL.firstMonthOfQuarter(), Month.APRIL);
 448         assertEquals(Month.MAY.firstMonthOfQuarter(), Month.APRIL);
 449         assertEquals(Month.JUNE.firstMonthOfQuarter(), Month.APRIL);
 450         assertEquals(Month.JULY.firstMonthOfQuarter(), Month.JULY);
 451         assertEquals(Month.AUGUST.firstMonthOfQuarter(), Month.JULY);
 452         assertEquals(Month.SEPTEMBER.firstMonthOfQuarter(), Month.JULY);
 453         assertEquals(Month.OCTOBER.firstMonthOfQuarter(), Month.OCTOBER);
 454         assertEquals(Month.NOVEMBER.firstMonthOfQuarter(), Month.OCTOBER);
 455         assertEquals(Month.DECEMBER.firstMonthOfQuarter(), Month.OCTOBER);
 456     }
 457 
 458     //-----------------------------------------------------------------------
 459     // toString()
 460     //-----------------------------------------------------------------------
 461     @Test(groups={"tck"})
 462     public void test_toString() {
 463         assertEquals(Month.JANUARY.toString(), "JANUARY");
 464         assertEquals(Month.FEBRUARY.toString(), "FEBRUARY");
 465         assertEquals(Month.MARCH.toString(), "MARCH");
 466         assertEquals(Month.APRIL.toString(), "APRIL");
 467         assertEquals(Month.MAY.toString(), "MAY");
 468         assertEquals(Month.JUNE.toString(), "JUNE");
 469         assertEquals(Month.JULY.toString(), "JULY");
 470         assertEquals(Month.AUGUST.toString(), "AUGUST");
 471         assertEquals(Month.SEPTEMBER.toString(), "SEPTEMBER");
 472         assertEquals(Month.OCTOBER.toString(), "OCTOBER");
 473         assertEquals(Month.NOVEMBER.toString(), "NOVEMBER");
 474         assertEquals(Month.DECEMBER.toString(), "DECEMBER");
 475     }
 476 
 477     //-----------------------------------------------------------------------
 478     // generated methods
 479     //-----------------------------------------------------------------------
 480     @Test(groups={"tck"})
 481     public void test_enum() {
 482         assertEquals(Month.valueOf("JANUARY"), Month.JANUARY);
 483         assertEquals(Month.values()[0], Month.JANUARY);
 484     }
 485 
 486 }