• 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.threeten.bp.temporal.ChronoField.DAY_OF_MONTH;
38 import static org.threeten.bp.temporal.ChronoField.DAY_OF_YEAR;
39 import static org.threeten.bp.temporal.ChronoField.MONTH_OF_YEAR;
40 import static org.threeten.bp.temporal.ChronoField.YEAR;
41 import static org.threeten.bp.temporal.ChronoField.YEAR_OF_ERA;
42 
43 import org.testng.Assert;
44 import org.testng.annotations.DataProvider;
45 import org.testng.annotations.Test;
46 import org.threeten.bp.DateTimeException;
47 import org.threeten.bp.LocalDate;
48 import org.threeten.bp.LocalDateTime;
49 import org.threeten.bp.Month;
50 import org.threeten.bp.temporal.ChronoField;
51 import org.threeten.bp.temporal.TemporalAdjusters;
52 import org.threeten.bp.temporal.ValueRange;
53 
54 /**
55  * Test.
56  */
57 @Test
58 public class TestThaiBuddhistChronology {
59 
60     private static final int YDIFF = 543;
61 
62     //-----------------------------------------------------------------------
63     // Chrono.ofName("ThaiBuddhist")  Lookup by name
64     //-----------------------------------------------------------------------
65     @Test
test_chrono_byName()66     public void test_chrono_byName() {
67         Chronology c = ThaiBuddhistChronology.INSTANCE;
68         Chronology test = Chronology.of("ThaiBuddhist");
69         Assert.assertNotNull(test, "The ThaiBuddhist calendar could not be found byName");
70         Assert.assertEquals(test.getId(), "ThaiBuddhist", "ID mismatch");
71         Assert.assertEquals(test.getCalendarType(), "buddhist", "Type mismatch");
72         Assert.assertEquals(test, c);
73     }
74 
75     //-----------------------------------------------------------------------
76     // creation, toLocalDate()
77     //-----------------------------------------------------------------------
78     @DataProvider(name="samples")
data_samples()79     Object[][] data_samples() {
80         return new Object[][] {
81             {ThaiBuddhistChronology.INSTANCE.date(1 + YDIFF, 1, 1), LocalDate.of(1, 1, 1)},
82             {ThaiBuddhistChronology.INSTANCE.date(1 + YDIFF, 1, 2), LocalDate.of(1, 1, 2)},
83             {ThaiBuddhistChronology.INSTANCE.date(1 + YDIFF, 1, 3), LocalDate.of(1, 1, 3)},
84 
85             {ThaiBuddhistChronology.INSTANCE.date(2 + YDIFF, 1, 1), LocalDate.of(2, 1, 1)},
86             {ThaiBuddhistChronology.INSTANCE.date(3 + YDIFF, 1, 1), LocalDate.of(3, 1, 1)},
87             {ThaiBuddhistChronology.INSTANCE.date(3 + YDIFF, 12, 6), LocalDate.of(3, 12, 6)},
88             {ThaiBuddhistChronology.INSTANCE.date(4 + YDIFF, 1, 1), LocalDate.of(4, 1, 1)},
89             {ThaiBuddhistChronology.INSTANCE.date(4 + YDIFF, 7, 3), LocalDate.of(4, 7, 3)},
90             {ThaiBuddhistChronology.INSTANCE.date(4 + YDIFF, 7, 4), LocalDate.of(4, 7, 4)},
91             {ThaiBuddhistChronology.INSTANCE.date(5 + YDIFF, 1, 1), LocalDate.of(5, 1, 1)},
92             {ThaiBuddhistChronology.INSTANCE.date(1662 + YDIFF, 3, 3), LocalDate.of(1662, 3, 3)},
93             {ThaiBuddhistChronology.INSTANCE.date(1728 + YDIFF, 10, 28), LocalDate.of(1728, 10, 28)},
94             {ThaiBuddhistChronology.INSTANCE.date(1728 + YDIFF, 10, 29), LocalDate.of(1728, 10, 29)},
95             {ThaiBuddhistChronology.INSTANCE.date(2555, 8, 29), LocalDate.of(2012, 8, 29)},
96         };
97     }
98 
99     @Test(dataProvider="samples")
test_toLocalDate(ChronoLocalDate jdate, LocalDate iso)100     public void test_toLocalDate(ChronoLocalDate jdate, LocalDate iso) {
101         assertEquals(LocalDate.from(jdate), iso);
102     }
103 
104     @Test(dataProvider="samples")
test_fromCalendrical(ChronoLocalDate jdate, LocalDate iso)105     public void test_fromCalendrical(ChronoLocalDate jdate, LocalDate iso) {
106         assertEquals(ThaiBuddhistChronology.INSTANCE.date(iso), jdate);
107     }
108 
109     @DataProvider(name="badDates")
data_badDates()110     Object[][] data_badDates() {
111         return new Object[][] {
112             {1728, 0, 0},
113 
114             {1728, -1, 1},
115             {1728, 0, 1},
116             {1728, 14, 1},
117             {1728, 15, 1},
118 
119             {1728, 1, -1},
120             {1728, 1, 0},
121             {1728, 1, 32},
122 
123             {1728, 12, -1},
124             {1728, 12, 0},
125             {1728, 12, 32},
126         };
127     }
128 
129     @Test(dataProvider="badDates", expectedExceptions=DateTimeException.class)
test_badDates(int year, int month, int dom)130     public void test_badDates(int year, int month, int dom) {
131         ThaiBuddhistChronology.INSTANCE.date(year, month, dom);
132     }
133 
134     //-----------------------------------------------------------------------
135     // with(WithAdjuster)
136     //-----------------------------------------------------------------------
137     @Test
test_adjust1()138     public void test_adjust1() {
139         ChronoLocalDate base = ThaiBuddhistChronology.INSTANCE.date(1728, 10, 29);
140         ChronoLocalDate test = base.with(TemporalAdjusters.lastDayOfMonth());
141         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(1728, 10, 31));
142     }
143 
144     @Test
test_adjust2()145     public void test_adjust2() {
146         ChronoLocalDate base = ThaiBuddhistChronology.INSTANCE.date(1728, 12, 2);
147         ChronoLocalDate test = base.with(TemporalAdjusters.lastDayOfMonth());
148         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(1728, 12, 31));
149     }
150 
151     //-----------------------------------------------------------------------
152     // withYear()
153     //-----------------------------------------------------------------------
154     @Test
test_withYear_BE()155     public void test_withYear_BE() {
156         ChronoLocalDate base = ThaiBuddhistChronology.INSTANCE.date(2555, 8, 29);
157         ChronoLocalDate test = base.with(YEAR, 2554);
158         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(2554, 8, 29));
159     }
160 
161     @Test
test_withYear_BBE()162     public void test_withYear_BBE() {
163         ChronoLocalDate base = ThaiBuddhistChronology.INSTANCE.date(-2554, 8, 29);
164         ChronoLocalDate test = base.with(YEAR_OF_ERA, 2554);
165         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(-2553, 8, 29));
166     }
167 
168     //-----------------------------------------------------------------------
169     // withEra()
170     //-----------------------------------------------------------------------
171     @Test
test_withEra_BE()172     public void test_withEra_BE() {
173         ChronoLocalDate base = ThaiBuddhistChronology.INSTANCE.date(2555, 8, 29);
174         ChronoLocalDate test = base.with(ChronoField.ERA, ThaiBuddhistEra.BE.getValue());
175         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(2555, 8, 29));
176     }
177 
178     @Test
test_withEra_BBE()179     public void test_withEra_BBE() {
180         ChronoLocalDate base = ThaiBuddhistChronology.INSTANCE.date(-2554, 8, 29);
181         ChronoLocalDate test = base.with(ChronoField.ERA, ThaiBuddhistEra.BEFORE_BE.getValue());
182         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(-2554, 8, 29));
183     }
184 
185     @Test
test_withEra_swap()186     public void test_withEra_swap() {
187         ChronoLocalDate base = ThaiBuddhistChronology.INSTANCE.date(-2554, 8, 29);
188         ChronoLocalDate test = base.with(ChronoField.ERA, ThaiBuddhistEra.BE.getValue());
189         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(2555, 8, 29));
190     }
191 
192     //-----------------------------------------------------------------------
193     // BuddhistDate.with(Local*)
194     //-----------------------------------------------------------------------
195     @Test
test_adjust_toLocalDate()196     public void test_adjust_toLocalDate() {
197         ChronoLocalDate jdate = ThaiBuddhistChronology.INSTANCE.date(1726, 1, 4);
198         ChronoLocalDate test = jdate.with(LocalDate.of(2012, 7, 6));
199         assertEquals(test, ThaiBuddhistChronology.INSTANCE.date(2555, 7, 6));
200     }
201 
202     @Test(expectedExceptions=DateTimeException.class)
test_adjust_toMonth()203     public void test_adjust_toMonth() {
204         ChronoLocalDate jdate = ThaiBuddhistChronology.INSTANCE.date(1726, 1, 4);
205         jdate.with(Month.APRIL);
206     }
207 
208     //-----------------------------------------------------------------------
209     // LocalDate.with(BuddhistDate)
210     //-----------------------------------------------------------------------
211     @Test
test_LocalDate_adjustToBuddhistDate()212     public void test_LocalDate_adjustToBuddhistDate() {
213         ChronoLocalDate jdate = ThaiBuddhistChronology.INSTANCE.date(2555, 10, 29);
214         LocalDate test = LocalDate.MIN.with(jdate);
215         assertEquals(test, LocalDate.of(2012, 10, 29));
216     }
217 
218     @Test
test_LocalDateTime_adjustToBuddhistDate()219     public void test_LocalDateTime_adjustToBuddhistDate() {
220         ChronoLocalDate jdate = ThaiBuddhistChronology.INSTANCE.date(2555, 10, 29);
221         LocalDateTime test = LocalDateTime.MIN.with(jdate);
222         assertEquals(test, LocalDateTime.of(2012, 10, 29, 0, 0));
223     }
224 
225     //-----------------------------------------------------------------------
226     // toString()
227     //-----------------------------------------------------------------------
228     @DataProvider(name="toString")
data_toString()229     Object[][] data_toString() {
230         return new Object[][] {
231             {ThaiBuddhistChronology.INSTANCE.date(544, 1, 1), "ThaiBuddhist BE 544-01-01"},
232             {ThaiBuddhistChronology.INSTANCE.date(2271, 10, 28), "ThaiBuddhist BE 2271-10-28"},
233             {ThaiBuddhistChronology.INSTANCE.date(2271, 10, 29), "ThaiBuddhist BE 2271-10-29"},
234             {ThaiBuddhistChronology.INSTANCE.date(2270, 12, 5), "ThaiBuddhist BE 2270-12-05"},
235             {ThaiBuddhistChronology.INSTANCE.date(2270, 12, 6), "ThaiBuddhist BE 2270-12-06"},
236         };
237     }
238 
239     @Test(dataProvider="toString")
test_toString(ChronoLocalDate jdate, String expected)240     public void test_toString(ChronoLocalDate jdate, String expected) {
241         assertEquals(jdate.toString(), expected);
242     }
243 
244     //-----------------------------------------------------------------------
245     // chronology range(ChronoField)
246     //-----------------------------------------------------------------------
247     @Test
test_Chrono_range()248     public void test_Chrono_range() {
249         long minYear = LocalDate.MIN.getYear() + YDIFF;
250         long maxYear = LocalDate.MAX.getYear() + YDIFF;
251         assertEquals(ThaiBuddhistChronology.INSTANCE.range(YEAR), ValueRange.of(minYear, maxYear));
252         assertEquals(ThaiBuddhistChronology.INSTANCE.range(YEAR_OF_ERA), ValueRange.of(1, -minYear + 1, maxYear));
253 
254         assertEquals(ThaiBuddhistChronology.INSTANCE.range(DAY_OF_MONTH), DAY_OF_MONTH.range());
255         assertEquals(ThaiBuddhistChronology.INSTANCE.range(DAY_OF_YEAR), DAY_OF_YEAR.range());
256         assertEquals(ThaiBuddhistChronology.INSTANCE.range(MONTH_OF_YEAR), MONTH_OF_YEAR.range());
257     }
258 
259     //-----------------------------------------------------------------------
260     // equals()
261     //-----------------------------------------------------------------------
262     @Test
test_equals_true()263     public void test_equals_true() {
264         assertTrue(ThaiBuddhistChronology.INSTANCE.equals(ThaiBuddhistChronology.INSTANCE));
265     }
266 
267     @Test
test_equals_false()268     public void test_equals_false() {
269         assertFalse(ThaiBuddhistChronology.INSTANCE.equals(IsoChronology.INSTANCE));
270     }
271 
272 }
273