• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp.chrono;
33 
34 import static org.testng.Assert.assertEquals;
35 import static org.testng.Assert.assertFalse;
36 import static org.testng.Assert.assertTrue;
37 import static org.testng.Assert.fail;
38 
39 import java.util.List;
40 
41 import org.testng.Assert;
42 import org.testng.annotations.DataProvider;
43 import org.testng.annotations.Test;
44 import org.threeten.bp.DateTimeException;
45 import org.threeten.bp.LocalDate;
46 import org.threeten.bp.LocalDateTime;
47 import org.threeten.bp.Month;
48 import org.threeten.bp.temporal.TemporalAdjusters;
49 
50 /**
51  * Test.
52  */
53 @Test
54 public class TestJapaneseChronology {
55 
56     //-----------------------------------------------------------------------
57     // Chrono.ofName("Japanese")  Lookup by name
58     //-----------------------------------------------------------------------
59     @Test
test_chrono_byName()60     public void test_chrono_byName() {
61         Chronology c = JapaneseChronology.INSTANCE;
62         Chronology test = Chronology.of("Japanese");
63         Assert.assertNotNull(test, "The Japanese calendar could not be found byName");
64         Assert.assertEquals(test.getId(), "Japanese", "ID mismatch");
65         Assert.assertEquals(test.getCalendarType(), "japanese", "Type mismatch");
66         Assert.assertEquals(test, c);
67     }
68 
69     //-----------------------------------------------------------------------
70     // creation, toLocalDate()
71     //-----------------------------------------------------------------------
72     @DataProvider(name="samples")
data_samples()73     Object[][] data_samples() {
74         return new Object[][] {
75             {JapaneseChronology.INSTANCE.date(1890, 3, 3), LocalDate.of(1890, 3, 3)},
76             {JapaneseChronology.INSTANCE.date(1890, 10, 28), LocalDate.of(1890, 10, 28)},
77             {JapaneseChronology.INSTANCE.date(1890, 10, 29), LocalDate.of(1890, 10, 29)},
78         };
79     }
80 
81     @Test(dataProvider="samples")
test_toLocalDate(ChronoLocalDate jdate, LocalDate iso)82     public void test_toLocalDate(ChronoLocalDate jdate, LocalDate iso) {
83         assertEquals(LocalDate.from(jdate), iso);
84     }
85 
86     @Test(dataProvider="samples")
test_fromCalendrical(ChronoLocalDate jdate, LocalDate iso)87     public void test_fromCalendrical(ChronoLocalDate jdate, LocalDate iso) {
88         assertEquals(JapaneseChronology.INSTANCE.date(iso), jdate);
89     }
90 
91     @DataProvider(name="badDates")
data_badDates()92     Object[][] data_badDates() {
93         return new Object[][] {
94             {1728, 0, 0},
95             {1890, 0, 0},
96 
97             {1890, -1, 1},
98             {1890, 0, 1},
99             {1890, 14, 1},
100             {1890, 15, 1},
101 
102             {1890, 1, -1},
103             {1890, 1, 0},
104             {1890, 1, 32},
105 
106             {1890, 12, -1},
107             {1890, 12, 0},
108             {1890, 12, 32},
109         };
110     }
111 
112     @Test(dataProvider="badDates", expectedExceptions=DateTimeException.class)
test_badDates(int year, int month, int dom)113     public void test_badDates(int year, int month, int dom) {
114         JapaneseChronology.INSTANCE.date(year, month, dom);
115     }
116 
117     //-----------------------------------------------------------------------
118     // with(WithAdjuster)
119     //-----------------------------------------------------------------------
120     @Test
test_adjust1()121     public void test_adjust1() {
122         ChronoLocalDate base = JapaneseChronology.INSTANCE.date(1890, 10, 29);
123         ChronoLocalDate test = base.with(TemporalAdjusters.lastDayOfMonth());
124         assertEquals(test, JapaneseChronology.INSTANCE.date(1890, 10, 31));
125     }
126 
127     @Test
test_adjust2()128     public void test_adjust2() {
129         ChronoLocalDate base = JapaneseChronology.INSTANCE.date(1890, 12, 2);
130         ChronoLocalDate test = base.with(TemporalAdjusters.lastDayOfMonth());
131         assertEquals(test, JapaneseChronology.INSTANCE.date(1890, 12, 31));
132     }
133 
134     //-----------------------------------------------------------------------
135     // JapaneseDate.with(Local*)
136     //-----------------------------------------------------------------------
137     @Test
test_adjust_toLocalDate()138     public void test_adjust_toLocalDate() {
139         ChronoLocalDate jdate = JapaneseChronology.INSTANCE.date(1890, 1, 4);
140         ChronoLocalDate test = jdate.with(LocalDate.of(2012, 7, 6));
141         assertEquals(test, JapaneseChronology.INSTANCE.date(2012, 7, 6));
142     }
143 
144     @Test(expectedExceptions=DateTimeException.class)
test_adjust_toMonth()145     public void test_adjust_toMonth() {
146         ChronoLocalDate jdate = JapaneseChronology.INSTANCE.date(1890, 1, 4);
147         jdate.with(Month.APRIL);
148     }
149 
150     //-----------------------------------------------------------------------
151     // LocalDate.with(JapaneseDate)
152     //-----------------------------------------------------------------------
153     @Test
test_LocalDate_adjustToJapaneseDate()154     public void test_LocalDate_adjustToJapaneseDate() {
155         ChronoLocalDate jdate = JapaneseChronology.INSTANCE.date(1890, 10, 29);
156         LocalDate test = LocalDate.MIN.with(jdate);
157         assertEquals(test, LocalDate.of(1890, 10, 29));
158     }
159 
160     @Test
test_LocalDateTime_adjustToJapaneseDate()161     public void test_LocalDateTime_adjustToJapaneseDate() {
162         ChronoLocalDate jdate = JapaneseChronology.INSTANCE.date(1890, 10, 29);
163         LocalDateTime test = LocalDateTime.MIN.with(jdate);
164         assertEquals(test, LocalDateTime.of(1890, 10, 29, 0, 0));
165     }
166 
167     //-----------------------------------------------------------------------
168     // Check Japanese Eras
169     //-----------------------------------------------------------------------
170     @DataProvider(name="japaneseEras")
data_japaneseEras()171     Object[][] data_japaneseEras() {
172         return new Object[][] {
173             { JapaneseEra.MEIJI, -1, "Meiji"},
174             { JapaneseEra.TAISHO, 0, "Taisho"},
175             { JapaneseEra.SHOWA, 1, "Showa"},
176             { JapaneseEra.HEISEI, 2, "Heisei"},
177             { JapaneseEra.REIWA, 3, "Reiwa"},
178         };
179     }
180 
181     @Test(dataProvider="japaneseEras")
test_Japanese_Eras(Era era, int eraValue, String name)182     public void test_Japanese_Eras(Era era, int eraValue, String name) {
183         assertEquals(era.getValue(), eraValue, "EraValue");
184         assertEquals(era.toString(), name, "Era Name");
185         assertEquals(era, JapaneseChronology.INSTANCE.eraOf(eraValue), "JapaneseChrono.eraOf()");
186         assertEquals(JapaneseEra.valueOf(name), era);
187         List<Era> eras = JapaneseChronology.INSTANCE.eras();
188         assertTrue(eras.contains(era), "Era is not present in JapaneseChronology.INSTANCE.eras()");
189     }
190 
191     @Test
test_Japanese_badEras()192     public void test_Japanese_badEras() {
193         int badEras[] = {-1000, -998, -997, -2, 4, 1000};
194         for (int badEra : badEras) {
195             try {
196                 Era era = JapaneseChronology.INSTANCE.eraOf(badEra);
197                 fail("JapaneseChronology.eraOf returned " + era + " + for invalid eraValue " + badEra);
198             } catch (DateTimeException ex) {
199                 // ignore expected exception
200             }
201         }
202         try {
203             Era era = JapaneseEra.valueOf("Rubbish");
204             fail("JapaneseEra.valueOf returned " + era + " + for invalid era name Rubbish");
205         } catch (IllegalArgumentException ex) {
206             // ignore expected exception
207         }
208     }
209 
210     @Test
test_Japanese_registerEra()211     public void test_Japanese_registerEra() {
212         try {
213             JapaneseEra.registerEra(JapaneseEra.SHOWA.endDate(), "TestAdditional");
214             fail("JapaneseEra.registerEra should have failed");
215         } catch (DateTimeException ex) {
216             // ignore expected exception
217         }
218         JapaneseEra additional = JapaneseEra.registerEra(LocalDate.of(2100, 1, 1), "TestAdditional");
219         assertEquals(JapaneseEra.of(4), additional);
220         assertEquals(JapaneseEra.valueOf("TestAdditional"), additional);
221         assertEquals(JapaneseEra.values()[5], additional);
222         try {
223             JapaneseEra.registerEra(LocalDate.of(2200, 1, 1), "TestAdditional2");
224             fail("JapaneseEra.registerEra should have failed");
225         } catch (DateTimeException ex) {
226             // ignore expected exception
227         }
228     }
229 
230     //-----------------------------------------------------------------------
231     // toString()
232     //-----------------------------------------------------------------------
233     @DataProvider(name="toString")
data_toString()234     Object[][] data_toString() {
235         return new Object[][] {
236             {JapaneseChronology.INSTANCE.date(1873,  9,  8), "Japanese Meiji 6-09-08"},
237             {JapaneseChronology.INSTANCE.date(1912,  7, 29), "Japanese Meiji 45-07-29"},
238             {JapaneseChronology.INSTANCE.date(1912,  7, 30), "Japanese Taisho 1-07-30"},
239             {JapaneseChronology.INSTANCE.date(1926, 12, 24), "Japanese Taisho 15-12-24"},
240             {JapaneseChronology.INSTANCE.date(1926, 12, 25), "Japanese Showa 1-12-25"},
241             {JapaneseChronology.INSTANCE.date(1989,  1,  7), "Japanese Showa 64-01-07"},
242             {JapaneseChronology.INSTANCE.date(1989,  1,  8), "Japanese Heisei 1-01-08"},
243             {JapaneseChronology.INSTANCE.date(2019, 4,  30), "Japanese Heisei 31-04-30"},
244             {JapaneseChronology.INSTANCE.date(2019, 5,  1), "Japanese Reiwa 1-05-01"},
245             {JapaneseChronology.INSTANCE.date(2020, 12,  24), "Japanese Reiwa 2-12-24"},
246         };
247     }
248 
249     @Test(dataProvider="toString")
test_toString(ChronoLocalDate jdate, String expected)250     public void test_toString(ChronoLocalDate jdate, String expected) {
251         assertEquals(jdate.toString(), expected);
252     }
253 
254     //-----------------------------------------------------------------------
255     // equals()
256     //-----------------------------------------------------------------------
257     @Test
test_equals_true()258     public void test_equals_true() {
259         assertTrue(JapaneseChronology.INSTANCE.equals(JapaneseChronology.INSTANCE));
260     }
261 
262     @Test
test_equals_false()263     public void test_equals_false() {
264         assertFalse(JapaneseChronology.INSTANCE.equals(IsoChronology.INSTANCE));
265     }
266 
267 }
268