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.temporal;
  61 
  62 import static java.time.temporal.ChronoField.ERA;
  63 import static java.time.temporal.ChronoField.YEAR;
  64 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
  65 import static java.time.temporal.ISOChrono.ERA_BCE;
  66 import static java.time.temporal.ISOChrono.ERA_CE;
  67 import static org.testng.Assert.assertEquals;
  68 import static org.testng.Assert.assertFalse;
  69 import static org.testng.Assert.assertNotNull;
  70 import static org.testng.Assert.assertTrue;
  71 
  72 import java.time.DateTimeException;
  73 import java.time.LocalDate;
  74 import java.time.LocalDateTime;
  75 import java.time.Month;
  76 import java.time.temporal.Chrono;
  77 import java.time.temporal.ChronoField;
  78 import java.time.temporal.ChronoLocalDate;
  79 import java.time.temporal.Adjusters;
  80 import java.time.calendar.HijrahChrono;
  81 import java.time.temporal.Era;
  82 import java.time.temporal.ISOChrono;
  83 
  84 import org.testng.Assert;
  85 import org.testng.annotations.DataProvider;
  86 import org.testng.annotations.Test;
  87 
  88 /**
  89  * Test.
  90  */
  91 @Test
  92 public class TestISOChrono {
  93 
  94     //-----------------------------------------------------------------------
  95     // Chrono.ofName("ISO")  Lookup by name
  96     //-----------------------------------------------------------------------
  97     @Test(groups={"tck"})
  98     public void test_chrono_byName() {
  99         Chrono<ISOChrono> c = ISOChrono.INSTANCE;
 100         Chrono<?> test = Chrono.of("ISO");
 101         Assert.assertNotNull(test, "The ISO calendar could not be found byName");
 102         Assert.assertEquals(test.getId(), "ISO", "ID mismatch");
 103         Assert.assertEquals(test.getCalendarType(), "iso8601", "Type mismatch");
 104         Assert.assertEquals(test, c);
 105     }
 106 
 107     //-----------------------------------------------------------------------
 108     // Lookup by Singleton
 109     //-----------------------------------------------------------------------
 110     @Test(groups="tck")
 111     public void instanceNotNull() {
 112         assertNotNull(ISOChrono.INSTANCE);
 113     }
 114 
 115     //-----------------------------------------------------------------------
 116     // Era creation
 117     //-----------------------------------------------------------------------
 118     @Test(groups="tck")
 119     public void test_eraOf() {
 120         assertEquals(ISOChrono.INSTANCE.eraOf(0), ERA_BCE);
 121         assertEquals(ISOChrono.INSTANCE.eraOf(1), ERA_CE);
 122     }
 123 
 124     //-----------------------------------------------------------------------
 125     // creation, toLocalDate()
 126     //-----------------------------------------------------------------------
 127     @DataProvider(name="samples")
 128     Object[][] data_samples() {
 129         return new Object[][] {
 130             {ISOChrono.INSTANCE.date(1, 7, 8), LocalDate.of(1, 7, 8)},
 131             {ISOChrono.INSTANCE.date(1, 7, 20), LocalDate.of(1, 7, 20)},
 132             {ISOChrono.INSTANCE.date(1, 7, 21), LocalDate.of(1, 7, 21)},
 133 
 134             {ISOChrono.INSTANCE.date(2, 7, 8), LocalDate.of(2, 7, 8)},
 135             {ISOChrono.INSTANCE.date(3, 6, 27), LocalDate.of(3, 6, 27)},
 136             {ISOChrono.INSTANCE.date(3, 5, 23), LocalDate.of(3, 5, 23)},
 137             {ISOChrono.INSTANCE.date(4, 6, 16), LocalDate.of(4, 6, 16)},
 138             {ISOChrono.INSTANCE.date(4, 7, 3), LocalDate.of(4, 7, 3)},
 139             {ISOChrono.INSTANCE.date(4, 7, 4), LocalDate.of(4, 7, 4)},
 140             {ISOChrono.INSTANCE.date(5, 1, 1), LocalDate.of(5, 1, 1)},
 141             {ISOChrono.INSTANCE.date(1727, 3, 3), LocalDate.of(1727, 3, 3)},
 142             {ISOChrono.INSTANCE.date(1728, 10, 28), LocalDate.of(1728, 10, 28)},
 143             {ISOChrono.INSTANCE.date(2012, 10, 29), LocalDate.of(2012, 10, 29)},
 144         };
 145     }
 146 
 147     @Test(dataProvider="samples", groups={"tck"})
 148     public void test_toLocalDate(ChronoLocalDate<ISOChrono> isoDate, LocalDate iso) {
 149         assertEquals(LocalDate.from(isoDate), iso);
 150     }
 151 
 152     @Test(dataProvider="samples", groups={"tck"})
 153     public void test_fromCalendrical(ChronoLocalDate<ISOChrono> isoDate, LocalDate iso) {
 154         assertEquals(ISOChrono.INSTANCE.date(iso), isoDate);
 155     }
 156 
 157     @DataProvider(name="badDates")
 158     Object[][] data_badDates() {
 159         return new Object[][] {
 160             {2012, 0, 0},
 161 
 162             {2012, -1, 1},
 163             {2012, 0, 1},
 164             {2012, 14, 1},
 165             {2012, 15, 1},
 166 
 167             {2012, 1, -1},
 168             {2012, 1, 0},
 169             {2012, 1, 32},
 170 
 171             {2012, 12, -1},
 172             {2012, 12, 0},
 173             {2012, 12, 32},
 174         };
 175     }
 176 
 177     @Test(dataProvider="badDates", groups={"tck"}, expectedExceptions=DateTimeException.class)
 178     public void test_badDates(int year, int month, int dom) {
 179         ISOChrono.INSTANCE.date(year, month, dom);
 180     }
 181 
 182     @Test(groups="tck")
 183     public void test_date_withEra() {
 184         int year = 5;
 185         int month = 5;
 186         int dayOfMonth = 5;
 187         ChronoLocalDate<ISOChrono> test = ISOChrono.INSTANCE.date(ERA_BCE, year, month, dayOfMonth);
 188         assertEquals(test.getEra(), ERA_BCE);
 189         assertEquals(test.get(ChronoField.YEAR_OF_ERA), year);
 190         assertEquals(test.get(ChronoField.MONTH_OF_YEAR), month);
 191         assertEquals(test.get(ChronoField.DAY_OF_MONTH), dayOfMonth);
 192 
 193         assertEquals(test.get(YEAR), 1 + (-1 * year));
 194         assertEquals(test.get(ERA), 0);
 195         assertEquals(test.get(YEAR_OF_ERA), year);
 196     }
 197 
 198     @SuppressWarnings({ "unchecked", "rawtypes" })
 199     @Test(expectedExceptions=DateTimeException.class, groups="tck")
 200     public void test_date_withEra_withWrongEra() {
 201         ISOChrono.INSTANCE.date((Era) HijrahChrono.ERA_AH, 1, 1, 1);
 202     }
 203 
 204     //-----------------------------------------------------------------------
 205     // with(DateTimeAdjuster)
 206     //-----------------------------------------------------------------------
 207     @Test(groups={"tck"})
 208     public void test_adjust1() {
 209         ChronoLocalDate<ISOChrono> base = ISOChrono.INSTANCE.date(1728, 10, 28);
 210         ChronoLocalDate<ISOChrono> test = base.with(Adjusters.lastDayOfMonth());
 211         assertEquals(test, ISOChrono.INSTANCE.date(1728, 10, 31));
 212     }
 213 
 214     @Test(groups={"tck"})
 215     public void test_adjust2() {
 216         ChronoLocalDate<ISOChrono> base = ISOChrono.INSTANCE.date(1728, 12, 2);
 217         ChronoLocalDate<ISOChrono> test = base.with(Adjusters.lastDayOfMonth());
 218         assertEquals(test, ISOChrono.INSTANCE.date(1728, 12, 31));
 219     }
 220 
 221     //-----------------------------------------------------------------------
 222     // ISODate.with(Local*)
 223     //-----------------------------------------------------------------------
 224     @Test(groups={"tck"})
 225     public void test_adjust_toLocalDate() {
 226         ChronoLocalDate<ISOChrono> isoDate = ISOChrono.INSTANCE.date(1726, 1, 4);
 227         ChronoLocalDate<ISOChrono> test = isoDate.with(LocalDate.of(2012, 7, 6));
 228         assertEquals(test, ISOChrono.INSTANCE.date(2012, 7, 6));
 229     }
 230 
 231     @Test(groups={"tck"})
 232     public void test_adjust_toMonth() {
 233         ChronoLocalDate<ISOChrono> isoDate = ISOChrono.INSTANCE.date(1726, 1, 4);
 234         assertEquals(ISOChrono.INSTANCE.date(1726, 4, 4), isoDate.with(Month.APRIL));
 235     }
 236 
 237     //-----------------------------------------------------------------------
 238     // LocalDate.with(ISODate)
 239     //-----------------------------------------------------------------------
 240     @Test(groups={"tck"})
 241     public void test_LocalDate_adjustToISODate() {
 242         ChronoLocalDate<ISOChrono> isoDate = ISOChrono.INSTANCE.date(1728, 10, 29);
 243         LocalDate test = LocalDate.MIN.with(isoDate);
 244         assertEquals(test, LocalDate.of(1728, 10, 29));
 245     }
 246 
 247     @Test(groups={"tck"})
 248     public void test_LocalDateTime_adjustToISODate() {
 249         ChronoLocalDate<ISOChrono> isoDate = ISOChrono.INSTANCE.date(1728, 10, 29);
 250         LocalDateTime test = LocalDateTime.MIN.with(isoDate);
 251         assertEquals(test, LocalDateTime.of(1728, 10, 29, 0, 0));
 252     }
 253 
 254     //-----------------------------------------------------------------------
 255     // isLeapYear()
 256     //-----------------------------------------------------------------------
 257     @DataProvider(name="leapYears")
 258     Object[][] leapYearInformation() {
 259         return new Object[][] {
 260                 {2000, true},
 261                 {1996, true},
 262                 {1600, true},
 263 
 264                 {1900, false},
 265                 {2100, false},
 266         };
 267     }
 268 
 269     @Test(dataProvider="leapYears", groups="tck")
 270     public void test_isLeapYear(int year, boolean isLeapYear) {
 271         assertEquals(ISOChrono.INSTANCE.isLeapYear(year), isLeapYear);
 272     }
 273 
 274     //-----------------------------------------------------------------------
 275     // toString()
 276     //-----------------------------------------------------------------------
 277     @Test(groups="tck")
 278     public void test_now() {
 279         assertEquals(LocalDate.from(ISOChrono.INSTANCE.dateNow()), LocalDate.now());
 280     }
 281 
 282     //-----------------------------------------------------------------------
 283     // toString()
 284     //-----------------------------------------------------------------------
 285     @DataProvider(name="toString")
 286     Object[][] data_toString() {
 287         return new Object[][] {
 288             {ISOChrono.INSTANCE.date(1, 1, 1), "0001-01-01"},
 289             {ISOChrono.INSTANCE.date(1728, 10, 28), "1728-10-28"},
 290             {ISOChrono.INSTANCE.date(1728, 10, 29), "1728-10-29"},
 291             {ISOChrono.INSTANCE.date(1727, 12, 5), "1727-12-05"},
 292             {ISOChrono.INSTANCE.date(1727, 12, 6), "1727-12-06"},
 293         };
 294     }
 295 
 296     @Test(dataProvider="toString", groups={"tck"})
 297     public void test_toString(ChronoLocalDate<ISOChrono> isoDate, String expected) {
 298         assertEquals(isoDate.toString(), expected);
 299     }
 300 
 301     //-----------------------------------------------------------------------
 302     // equals()
 303     //-----------------------------------------------------------------------
 304     @Test(groups="tck")
 305     public void test_equals_true() {
 306         assertTrue(ISOChrono.INSTANCE.equals(ISOChrono.INSTANCE));
 307     }
 308 
 309     @Test(groups="tck")
 310     public void test_equals_false() {
 311         assertFalse(ISOChrono.INSTANCE.equals(HijrahChrono.INSTANCE));
 312     }
 313 
 314 }