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