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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos 28 * 29 * All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions are met: 33 * 34 * * Redistributions of source code must retain the above copyright notice, 35 * this list of conditions and the following disclaimer. 36 * 37 * * Redistributions in binary form must reproduce the above copyright notice, 38 * this list of conditions and the following disclaimer in the documentation 39 * and/or other materials provided with the distribution. 40 * 41 * * Neither the name of JSR-310 nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 49 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 package tck.java.time.calendar; 58 59 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 60 import static java.time.temporal.ChronoField.DAY_OF_YEAR; 61 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 62 import static java.time.temporal.ChronoField.YEAR; 63 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 64 import static org.testng.Assert.assertEquals; 65 import static org.testng.Assert.assertFalse; 66 import static org.testng.Assert.assertTrue; 67 68 import java.time.DateTimeException; 69 import java.time.LocalDate; 70 import java.time.LocalDateTime; 71 import java.time.Month; 72 import java.time.calendar.ThaiBuddhistChrono; 73 import java.time.temporal.Chrono; 74 import java.time.temporal.ChronoField; 75 import java.time.temporal.ChronoLocalDate; 76 import java.time.temporal.Adjusters; 77 import java.time.temporal.ValueRange; 78 import java.time.temporal.ISOChrono; 79 80 import org.testng.Assert; 81 import org.testng.annotations.DataProvider; 82 import org.testng.annotations.Test; 83 84 /** 85 * Test. 86 */ 87 @Test 88 public class TestThaiBuddhistChrono { 89 90 private static final int YDIFF = 543; 91 92 //----------------------------------------------------------------------- 93 // Chrono.ofName("ThaiBuddhist") Lookup by name 94 //----------------------------------------------------------------------- 95 @Test(groups={"tck"}) 96 public void test_chrono_byName() { 97 Chrono<ThaiBuddhistChrono> c = ThaiBuddhistChrono.INSTANCE; 98 Chrono<?> test = Chrono.of("ThaiBuddhist"); 99 Assert.assertNotNull(test, "The ThaiBuddhist calendar could not be found byName"); 100 Assert.assertEquals(test.getId(), "ThaiBuddhist", "ID mismatch"); 101 Assert.assertEquals(test.getCalendarType(), "buddhist", "Type mismatch"); 102 Assert.assertEquals(test, c); 103 } 104 105 //----------------------------------------------------------------------- 106 // creation, toLocalDate() 107 //----------------------------------------------------------------------- 108 @DataProvider(name="samples") 109 Object[][] data_samples() { 110 return new Object[][] { 111 {ThaiBuddhistChrono.INSTANCE.date(1 + YDIFF, 1, 1), LocalDate.of(1, 1, 1)}, 112 {ThaiBuddhistChrono.INSTANCE.date(1 + YDIFF, 1, 2), LocalDate.of(1, 1, 2)}, 113 {ThaiBuddhistChrono.INSTANCE.date(1 + YDIFF, 1, 3), LocalDate.of(1, 1, 3)}, 114 115 {ThaiBuddhistChrono.INSTANCE.date(2 + YDIFF, 1, 1), LocalDate.of(2, 1, 1)}, 116 {ThaiBuddhistChrono.INSTANCE.date(3 + YDIFF, 1, 1), LocalDate.of(3, 1, 1)}, 117 {ThaiBuddhistChrono.INSTANCE.date(3 + YDIFF, 12, 6), LocalDate.of(3, 12, 6)}, 118 {ThaiBuddhistChrono.INSTANCE.date(4 + YDIFF, 1, 1), LocalDate.of(4, 1, 1)}, 119 {ThaiBuddhistChrono.INSTANCE.date(4 + YDIFF, 7, 3), LocalDate.of(4, 7, 3)}, 120 {ThaiBuddhistChrono.INSTANCE.date(4 + YDIFF, 7, 4), LocalDate.of(4, 7, 4)}, 121 {ThaiBuddhistChrono.INSTANCE.date(5 + YDIFF, 1, 1), LocalDate.of(5, 1, 1)}, 122 {ThaiBuddhistChrono.INSTANCE.date(1662 + YDIFF, 3, 3), LocalDate.of(1662, 3, 3)}, 123 {ThaiBuddhistChrono.INSTANCE.date(1728 + YDIFF, 10, 28), LocalDate.of(1728, 10, 28)}, 124 {ThaiBuddhistChrono.INSTANCE.date(1728 + YDIFF, 10, 29), LocalDate.of(1728, 10, 29)}, 125 {ThaiBuddhistChrono.INSTANCE.date(2555, 8, 29), LocalDate.of(2012, 8, 29)}, 126 }; 127 } 128 129 @Test(dataProvider="samples", groups={"tck"}) 130 public void test_toLocalDate(ChronoLocalDate<ThaiBuddhistChrono> jdate, LocalDate iso) { 131 assertEquals(LocalDate.from(jdate), iso); 132 } 133 134 @Test(dataProvider="samples", groups={"tck"}) 135 public void test_fromCalendrical(ChronoLocalDate<ThaiBuddhistChrono> jdate, LocalDate iso) { 136 assertEquals(ThaiBuddhistChrono.INSTANCE.date(iso), jdate); 137 } 138 139 @DataProvider(name="badDates") 140 Object[][] data_badDates() { 141 return new Object[][] { 142 {1728, 0, 0}, 143 144 {1728, -1, 1}, 145 {1728, 0, 1}, 146 {1728, 14, 1}, 147 {1728, 15, 1}, 148 149 {1728, 1, -1}, 150 {1728, 1, 0}, 151 {1728, 1, 32}, 152 153 {1728, 12, -1}, 154 {1728, 12, 0}, 155 {1728, 12, 32}, 156 }; 157 } 158 159 @Test(dataProvider="badDates", groups={"tck"}, expectedExceptions=DateTimeException.class) 160 public void test_badDates(int year, int month, int dom) { 161 ThaiBuddhistChrono.INSTANCE.date(year, month, dom); 162 } 163 164 //----------------------------------------------------------------------- 165 // with(WithAdjuster) 166 //----------------------------------------------------------------------- 167 @Test(groups={"tck"}) 168 public void test_adjust1() { 169 ChronoLocalDate<ThaiBuddhistChrono> base = ThaiBuddhistChrono.INSTANCE.date(1728, 10, 29); 170 ChronoLocalDate<ThaiBuddhistChrono> test = base.with(Adjusters.lastDayOfMonth()); 171 assertEquals(test, ThaiBuddhistChrono.INSTANCE.date(1728, 10, 31)); 172 } 173 174 @Test(groups={"tck"}) 175 public void test_adjust2() { 176 ChronoLocalDate<ThaiBuddhistChrono> base = ThaiBuddhistChrono.INSTANCE.date(1728, 12, 2); 177 ChronoLocalDate<ThaiBuddhistChrono> test = base.with(Adjusters.lastDayOfMonth()); 178 assertEquals(test, ThaiBuddhistChrono.INSTANCE.date(1728, 12, 31)); 179 } 180 181 //----------------------------------------------------------------------- 182 // withYear() 183 //----------------------------------------------------------------------- 184 @Test(groups={"tck"}) 185 public void test_withYear_BE() { 186 ChronoLocalDate<ThaiBuddhistChrono> base = ThaiBuddhistChrono.INSTANCE.date(2555, 8, 29); 187 ChronoLocalDate<ThaiBuddhistChrono> test = base.with(YEAR, 2554); 188 assertEquals(test, ThaiBuddhistChrono.INSTANCE.date(2554, 8, 29)); 189 } 190 191 @Test(groups={"tck"}) 192 public void test_withYear_BBE() { 193 ChronoLocalDate<ThaiBuddhistChrono> base = ThaiBuddhistChrono.INSTANCE.date(-2554, 8, 29); 194 ChronoLocalDate<ThaiBuddhistChrono> test = base.with(YEAR_OF_ERA, 2554); 195 assertEquals(test, ThaiBuddhistChrono.INSTANCE.date(-2553, 8, 29)); 196 } 197 198 //----------------------------------------------------------------------- 199 // withEra() 200 //----------------------------------------------------------------------- 201 @Test(groups={"tck"}) 202 public void test_withEra_BE() { 203 ChronoLocalDate<ThaiBuddhistChrono> base = ThaiBuddhistChrono.INSTANCE.date(2555, 8, 29); 204 ChronoLocalDate<ThaiBuddhistChrono> test = base.with(ChronoField.ERA, ThaiBuddhistChrono.ERA_BE.getValue()); 205 assertEquals(test, ThaiBuddhistChrono.INSTANCE.date(2555, 8, 29)); 206 } 207 208 @Test(groups={"tck"}) 209 public void test_withEra_BBE() { 210 ChronoLocalDate<ThaiBuddhistChrono> base = ThaiBuddhistChrono.INSTANCE.date(-2554, 8, 29); 211 ChronoLocalDate<ThaiBuddhistChrono> test = base.with(ChronoField.ERA, ThaiBuddhistChrono.ERA_BEFORE_BE.getValue()); 212 assertEquals(test, ThaiBuddhistChrono.INSTANCE.date(-2554, 8, 29)); 213 } 214 215 @Test(groups={"tck"}) 216 public void test_withEra_swap() { 217 ChronoLocalDate<ThaiBuddhistChrono> base = ThaiBuddhistChrono.INSTANCE.date(-2554, 8, 29); 218 ChronoLocalDate<ThaiBuddhistChrono> test = base.with(ChronoField.ERA, ThaiBuddhistChrono.ERA_BE.getValue()); 219 assertEquals(test, ThaiBuddhistChrono.INSTANCE.date(2555, 8, 29)); 220 } 221 222 //----------------------------------------------------------------------- 223 // BuddhistDate.with(Local*) 224 //----------------------------------------------------------------------- 225 @Test(groups={"tck"}) 226 public void test_adjust_toLocalDate() { 227 ChronoLocalDate<ThaiBuddhistChrono> jdate = ThaiBuddhistChrono.INSTANCE.date(1726, 1, 4); 228 ChronoLocalDate<ThaiBuddhistChrono> test = jdate.with(LocalDate.of(2012, 7, 6)); 229 assertEquals(test, ThaiBuddhistChrono.INSTANCE.date(2555, 7, 6)); 230 } 231 232 @Test(groups={"tck"}, expectedExceptions=DateTimeException.class) 233 public void test_adjust_toMonth() { 234 ChronoLocalDate<ThaiBuddhistChrono> jdate = ThaiBuddhistChrono.INSTANCE.date(1726, 1, 4); 235 jdate.with(Month.APRIL); 236 } 237 238 //----------------------------------------------------------------------- 239 // LocalDate.with(BuddhistDate) 240 //----------------------------------------------------------------------- 241 @Test(groups={"tck"}) 242 public void test_LocalDate_adjustToBuddhistDate() { 243 ChronoLocalDate<ThaiBuddhistChrono> jdate = ThaiBuddhistChrono.INSTANCE.date(2555, 10, 29); 244 LocalDate test = LocalDate.MIN.with(jdate); 245 assertEquals(test, LocalDate.of(2012, 10, 29)); 246 } 247 248 @Test(groups={"tck"}) 249 public void test_LocalDateTime_adjustToBuddhistDate() { 250 ChronoLocalDate<ThaiBuddhistChrono> jdate = ThaiBuddhistChrono.INSTANCE.date(2555, 10, 29); 251 LocalDateTime test = LocalDateTime.MIN.with(jdate); 252 assertEquals(test, LocalDateTime.of(2012, 10, 29, 0, 0)); 253 } 254 255 //----------------------------------------------------------------------- 256 // toString() 257 //----------------------------------------------------------------------- 258 @DataProvider(name="toString") 259 Object[][] data_toString() { 260 return new Object[][] { 261 {ThaiBuddhistChrono.INSTANCE.date(544, 1, 1), "ThaiBuddhist BE 544-01-01"}, 262 {ThaiBuddhistChrono.INSTANCE.date(2271, 10, 28), "ThaiBuddhist BE 2271-10-28"}, 263 {ThaiBuddhistChrono.INSTANCE.date(2271, 10, 29), "ThaiBuddhist BE 2271-10-29"}, 264 {ThaiBuddhistChrono.INSTANCE.date(2270, 12, 5), "ThaiBuddhist BE 2270-12-05"}, 265 {ThaiBuddhistChrono.INSTANCE.date(2270, 12, 6), "ThaiBuddhist BE 2270-12-06"}, 266 }; 267 } 268 269 @Test(dataProvider="toString", groups={"tck"}) 270 public void test_toString(ChronoLocalDate<ThaiBuddhistChrono> jdate, String expected) { 271 assertEquals(jdate.toString(), expected); 272 } 273 274 //----------------------------------------------------------------------- 275 // chronology range(ChronoField) 276 //----------------------------------------------------------------------- 277 @Test(groups={"tck"}) 278 public void test_Chrono_range() { 279 long minYear = LocalDate.MIN.getYear() + YDIFF; 280 long maxYear = LocalDate.MAX.getYear() + YDIFF; 281 assertEquals(ThaiBuddhistChrono.INSTANCE.range(YEAR), ValueRange.of(minYear, maxYear)); 282 assertEquals(ThaiBuddhistChrono.INSTANCE.range(YEAR_OF_ERA), ValueRange.of(1, -minYear + 1, maxYear)); 283 284 assertEquals(ThaiBuddhistChrono.INSTANCE.range(DAY_OF_MONTH), DAY_OF_MONTH.range()); 285 assertEquals(ThaiBuddhistChrono.INSTANCE.range(DAY_OF_YEAR), DAY_OF_YEAR.range()); 286 assertEquals(ThaiBuddhistChrono.INSTANCE.range(MONTH_OF_YEAR), MONTH_OF_YEAR.range()); 287 } 288 289 //----------------------------------------------------------------------- 290 // equals() 291 //----------------------------------------------------------------------- 292 @Test(groups="tck") 293 public void test_equals_true() { 294 assertTrue(ThaiBuddhistChrono.INSTANCE.equals(ThaiBuddhistChrono.INSTANCE)); 295 } 296 297 @Test(groups="tck") 298 public void test_equals_false() { 299 assertFalse(ThaiBuddhistChrono.INSTANCE.equals(ISOChrono.INSTANCE)); 300 } 301 302 }