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