1 /* 2 * Copyright (c) 2013, 2014, 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) 2013, 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.chrono; 58 59 import static java.time.temporal.ChronoUnit.DAYS; 60 import static java.time.temporal.ChronoUnit.HOURS; 61 import static java.time.temporal.ChronoUnit.MONTHS; 62 import static java.time.temporal.ChronoUnit.YEARS; 63 import static org.testng.Assert.assertEquals; 64 65 import java.io.ByteArrayInputStream; 66 import java.io.ByteArrayOutputStream; 67 import java.io.ObjectInputStream; 68 import java.io.ObjectOutputStream; 69 import java.time.DateTimeException; 70 import java.time.LocalDate; 71 import java.time.Period; 72 import java.time.chrono.ChronoLocalDate; 73 import java.time.chrono.ChronoPeriod; 74 import java.time.chrono.Chronology; 75 import java.time.chrono.HijrahChronology; 76 import java.time.chrono.IsoChronology; 77 import java.time.chrono.JapaneseChronology; 78 import java.time.chrono.MinguoChronology; 79 import java.time.chrono.ThaiBuddhistChronology; 80 import java.time.temporal.Temporal; 81 import java.time.temporal.UnsupportedTemporalTypeException; 82 83 import org.testng.annotations.DataProvider; 84 import org.testng.annotations.Test; 85 86 @Test 87 public class TCKChronoPeriod { 88 89 //----------------------------------------------------------------------- 90 // regular data factory for names and descriptions of available calendars 91 //----------------------------------------------------------------------- 92 @DataProvider(name = "calendars") data_of_calendars()93 Chronology[][] data_of_calendars() { 94 return new Chronology[][]{ 95 {HijrahChronology.INSTANCE}, 96 {IsoChronology.INSTANCE}, 97 {JapaneseChronology.INSTANCE}, 98 {MinguoChronology.INSTANCE}, 99 {ThaiBuddhistChronology.INSTANCE}}; 100 } 101 102 //----------------------------------------------------------------------- 103 // Test Serialization of Calendars 104 //----------------------------------------------------------------------- 105 @Test(dataProvider="calendars") test_serialization(Chronology chrono)106 public void test_serialization(Chronology chrono) throws Exception { 107 ChronoPeriod period = chrono.period(1, 2, 3); 108 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 109 ObjectOutputStream out = new ObjectOutputStream(baos); 110 out.writeObject(period); 111 out.close(); 112 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 113 114 ObjectInputStream in = new ObjectInputStream(bais); 115 ChronoPeriod ser = (ChronoPeriod) in.readObject(); 116 assertEquals(ser, period, "deserialized ChronoPeriod is wrong"); 117 } 118 119 @Test(dataProvider="calendars") test_get(Chronology chrono)120 public void test_get(Chronology chrono) { 121 ChronoPeriod period = chrono.period(1, 2, 3); 122 assertEquals(period.get(YEARS), 1); 123 assertEquals(period.get(MONTHS), 2); 124 assertEquals(period.get(DAYS), 3); 125 } 126 127 @Test(dataProvider="calendars", expectedExceptions=UnsupportedTemporalTypeException.class) test_get_unsupported(Chronology chrono)128 public void test_get_unsupported(Chronology chrono) { 129 ChronoPeriod period = chrono.period(1, 2, 3); 130 period.get(HOURS); 131 } 132 133 @Test(dataProvider="calendars") test_getUnits(Chronology chrono)134 public void test_getUnits(Chronology chrono) { 135 ChronoPeriod period = chrono.period(1, 2, 3); 136 assertEquals(period.getUnits().size(), 3); 137 assertEquals(period.getUnits().get(0), YEARS); 138 assertEquals(period.getUnits().get(1), MONTHS); 139 assertEquals(period.getUnits().get(2), DAYS); 140 } 141 142 @Test(dataProvider="calendars") test_getChronology(Chronology chrono)143 public void test_getChronology(Chronology chrono) { 144 ChronoPeriod period = chrono.period(1, 2, 3); 145 assertEquals(period.getChronology(), chrono); 146 } 147 148 @Test(dataProvider="calendars") test_isZero_isNegative(Chronology chrono)149 public void test_isZero_isNegative(Chronology chrono) { 150 ChronoPeriod periodPositive = chrono.period(1, 2, 3); 151 assertEquals(periodPositive.isZero(), false); 152 assertEquals(periodPositive.isNegative(), false); 153 154 ChronoPeriod periodZero = chrono.period(0, 0, 0); 155 assertEquals(periodZero.isZero(), true); 156 assertEquals(periodZero.isNegative(), false); 157 158 ChronoPeriod periodNegative = chrono.period(-1, 0, 0); 159 assertEquals(periodNegative.isZero(), false); 160 assertEquals(periodNegative.isNegative(), true); 161 } 162 163 //----------------------------------------------------------------------- 164 @Test(dataProvider="calendars") test_plus(Chronology chrono)165 public void test_plus(Chronology chrono) { 166 ChronoPeriod period = chrono.period(1, 2, 3); 167 ChronoPeriod period2 = chrono.period(2, 3, 4); 168 ChronoPeriod result = period.plus(period2); 169 assertEquals(result, chrono.period(3, 5, 7)); 170 } 171 172 @Test(dataProvider="calendars", expectedExceptions=DateTimeException.class) test_plus_wrongChrono(Chronology chrono)173 public void test_plus_wrongChrono(Chronology chrono) { 174 ChronoPeriod period = chrono.period(1, 2, 3); 175 ChronoPeriod isoPeriod = Period.of(2, 3, 4); 176 ChronoPeriod thaiPeriod = ThaiBuddhistChronology.INSTANCE.period(2, 3, 4); 177 // one of these two will fail 178 period.plus(isoPeriod); 179 period.plus(thaiPeriod); 180 } 181 182 @Test(dataProvider="calendars") test_minus(Chronology chrono)183 public void test_minus(Chronology chrono) { 184 ChronoPeriod period = chrono.period(1, 2, 3); 185 ChronoPeriod period2 = chrono.period(2, 3, 4); 186 ChronoPeriod result = period.minus(period2); 187 assertEquals(result, chrono.period(-1, -1, -1)); 188 } 189 190 @Test(dataProvider="calendars", expectedExceptions=DateTimeException.class) test_minus_wrongChrono(Chronology chrono)191 public void test_minus_wrongChrono(Chronology chrono) { 192 ChronoPeriod period = chrono.period(1, 2, 3); 193 ChronoPeriod isoPeriod = Period.of(2, 3, 4); 194 ChronoPeriod thaiPeriod = ThaiBuddhistChronology.INSTANCE.period(2, 3, 4); 195 // one of these two will fail 196 period.minus(isoPeriod); 197 period.minus(thaiPeriod); 198 } 199 200 //----------------------------------------------------------------------- 201 @Test(dataProvider="calendars") test_addTo(Chronology chrono)202 public void test_addTo(Chronology chrono) { 203 ChronoPeriod period = chrono.period(1, 2, 3); 204 ChronoLocalDate date = chrono.dateNow(); 205 Temporal result = period.addTo(date); 206 assertEquals(result, date.plus(14, MONTHS).plus(3, DAYS)); 207 } 208 209 @Test(dataProvider="calendars", expectedExceptions=DateTimeException.class) test_addTo_wrongChrono(Chronology chrono)210 public void test_addTo_wrongChrono(Chronology chrono) { 211 ChronoPeriod period = chrono.period(1, 2, 3); 212 ChronoLocalDate isoDate = LocalDate.of(2000, 1, 1); 213 ChronoLocalDate thaiDate = ThaiBuddhistChronology.INSTANCE.date(2000, 1, 1); 214 // one of these two will fail 215 period.addTo(isoDate); 216 period.addTo(thaiDate); 217 } 218 219 @Test(dataProvider="calendars") test_subtractFrom(Chronology chrono)220 public void test_subtractFrom(Chronology chrono) { 221 ChronoPeriod period = chrono.period(1, 2, 3); 222 ChronoLocalDate date = chrono.dateNow(); 223 Temporal result = period.subtractFrom(date); 224 assertEquals(result, date.minus(14, MONTHS).minus(3, DAYS)); 225 } 226 227 @Test(dataProvider="calendars", expectedExceptions=DateTimeException.class) test_subtractFrom_wrongChrono(Chronology chrono)228 public void test_subtractFrom_wrongChrono(Chronology chrono) { 229 ChronoPeriod period = chrono.period(1, 2, 3); 230 ChronoLocalDate isoDate = LocalDate.of(2000, 1, 1); 231 ChronoLocalDate thaiDate = ThaiBuddhistChronology.INSTANCE.date(2000, 1, 1); 232 // one of these two will fail 233 period.subtractFrom(isoDate); 234 period.subtractFrom(thaiDate); 235 } 236 237 //----------------------------------------------------------------------- 238 @Test(dataProvider="calendars") test_negated(Chronology chrono)239 public void test_negated(Chronology chrono) { 240 ChronoPeriod period = chrono.period(1, 2, 3); 241 assertEquals(period.negated(), chrono.period(-1, -2, -3)); 242 } 243 244 @Test(dataProvider="calendars") test_multipliedBy(Chronology chrono)245 public void test_multipliedBy(Chronology chrono) { 246 ChronoPeriod period = chrono.period(1, 2, 3); 247 assertEquals(period.multipliedBy(3), chrono.period(3, 6, 9)); 248 } 249 250 //----------------------------------------------------------------------- 251 @Test(dataProvider="calendars") test_equals_equal(Chronology chrono)252 public void test_equals_equal(Chronology chrono) { 253 ChronoPeriod a1 = chrono.period(1, 2, 3); 254 ChronoPeriod a2 = chrono.period(1, 2, 3); 255 assertEquals(a1, a1); 256 assertEquals(a1, a2); 257 assertEquals(a2, a1); 258 assertEquals(a2, a2); 259 assertEquals(a1.hashCode(), a2.hashCode()); 260 } 261 262 @Test(dataProvider="calendars") test_equals_notEqual(Chronology chrono)263 public void test_equals_notEqual(Chronology chrono) { 264 ChronoPeriod a = chrono.period(1, 2, 3); 265 ChronoPeriod b = chrono.period(2, 2, 3); 266 assertEquals(a.equals(b), false); 267 assertEquals(b.equals(a), false); 268 assertEquals(a.equals(""), false); 269 assertEquals(a.equals(null), false); 270 } 271 272 @Test(dataProvider="calendars") test_toString(Chronology chrono)273 public void test_toString(Chronology chrono) { 274 ChronoPeriod period = chrono.period(1, 2, 3); 275 if (period instanceof Period == false) { 276 assertEquals(period.toString(), chrono.getId() + " P1Y2M3D"); 277 } 278 } 279 280 } 281