/* * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * * Neither the name of JSR-310 nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package javax.time.chrono; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.time.Duration; import java.time.LocalDate; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.calendrical.ChronoUnit; import java.time.calendrical.DateTime; import java.time.calendrical.DateTimeAccessor; import java.time.calendrical.DateTimeBuilder; import java.time.calendrical.DateTimeField; import java.time.calendrical.DateTimeValueRange; import java.time.calendrical.PeriodUnit; import java.time.chrono.Chrono; import java.time.chrono.ChronoZonedDateTime; import java.time.chrono.ISOChrono; import java.time.chrono.global.HijrahChrono; import java.time.chrono.global.JapaneseChrono; import java.time.chrono.global.MinguoChrono; import java.time.chrono.global.ThaiBuddhistChrono; import java.util.ArrayList; import java.util.List; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /** * Test assertions that must be true for all built-in chronologies. */ @Test public class TestChronoZonedDateTime { // ----------------------------------------------------------------------- // regular data factory for names and descriptions of available calendars // ----------------------------------------------------------------------- @DataProvider(name = "calendars") Chrono[][] data_of_calendars() { return new Chrono[][] { { HijrahChrono.INSTANCE }, { ISOChrono.INSTANCE }, { JapaneseChrono.INSTANCE }, { MinguoChrono.INSTANCE }, { ThaiBuddhistChrono.INSTANCE }, }; } @Test(groups = { "tck" }, dataProvider = "calendars") public void test_badWithAdjusterChrono(Chrono<?> chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoZonedDateTime czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); DateTime.WithAdjuster adjuster = new FixedAdjuster(czdt2); if (chrono != chrono2) { try { czdt.with(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + czdt + ", supplied: " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { ChronoZonedDateTime<?> result = czdt.with(adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date"); } } } @Test(groups = { "tck" }, dataProvider = "calendars") public void test_badPlusAdjusterChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoZonedDateTime czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); DateTime.PlusAdjuster adjuster = new FixedAdjuster(czdt2); if (chrono != chrono2) { try { ChronoZonedDateTime<?> notreached = czdt.plus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + czdt + ", supplied: " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.plus(adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date time"); } } } @Test(groups = { "tck" }, dataProvider = "calendars") public void test_badMinusAdjusterChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoZonedDateTime czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); DateTime.MinusAdjuster adjuster = new FixedAdjuster(czdt2); if (chrono != chrono2) { try { ChronoZonedDateTime<?> notreached = czdt.minus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + czdt + ", supplied: " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.minus(adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date"); } } } @Test(groups = { "tck" }, dataProvider = "calendars") public void test_badPlusPeriodUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoZonedDateTime czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); PeriodUnit adjuster = new FixedPeriodUnit(czdt2); if (chrono != chrono2) { try { ChronoZonedDateTime<?> notreached = czdt.plus(1, adjuster); Assert.fail("PeriodUnit.doPlus plus should have thrown a ClassCastException, " + czdt + " can not be cast to " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.plus(1, adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date"); } } } @Test(groups = { "tck" }, dataProvider = "calendars") public void test_badMinusPeriodUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoZonedDateTime czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); PeriodUnit adjuster = new FixedPeriodUnit(czdt2); if (chrono != chrono2) { try { ChronoZonedDateTime<?> notreached = czdt.minus(1, adjuster); Assert.fail("PeriodUnit.doPlus minus should have thrown a ClassCastException, " + czdt.getClass() + " can not be cast to " + czdt2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.minus(1, adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date"); } } } @Test(groups = { "tck" }, dataProvider = "calendars") public void test_badDateTimeFieldChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoZonedDateTime czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); DateTimeField adjuster = new FixedDateTimeField(czdt2); if (chrono != chrono2) { try { ChronoZonedDateTime<?> notreached = czdt.with(adjuster, 1); Assert.fail("DateTimeField doWith() should have thrown a ClassCastException, " + czdt.getClass() + " can not be cast to " + czdt2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.with(adjuster, 1); assertEquals(result, czdt2, "DateTimeField doWith() failed to replace date"); } } } // ----------------------------------------------------------------------- // isBefore, isAfter, isEqual, INSTANT_COMPARATOR test a Chrono against the other Chronos // ----------------------------------------------------------------------- @Test(groups = { "tck" }, dataProvider = "calendars") public void test_zonedDateTime_comparisons(Chrono chrono) { List<ChronoZonedDateTime<?>> dates = new ArrayList<>(); ChronoZonedDateTime<?> date = chrono.date(LocalDate.of(1900, 1, 1)).atTime(LocalTime.MIN_TIME) .atZone(ZoneOffset.UTC); // Insert dates in order, no duplicates dates.add(date.minus(100, ChronoUnit.YEARS)); dates.add(date.minus(1, ChronoUnit.YEARS)); dates.add(date.minus(1, ChronoUnit.MONTHS)); dates.add(date.minus(1, ChronoUnit.WEEKS)); dates.add(date.minus(1, ChronoUnit.DAYS)); dates.add(date.minus(1, ChronoUnit.HOURS)); dates.add(date.minus(1, ChronoUnit.MINUTES)); dates.add(date.minus(1, ChronoUnit.SECONDS)); dates.add(date.minus(1, ChronoUnit.NANOS)); dates.add(date); dates.add(date.plus(1, ChronoUnit.NANOS)); dates.add(date.plus(1, ChronoUnit.SECONDS)); dates.add(date.plus(1, ChronoUnit.MINUTES)); dates.add(date.plus(1, ChronoUnit.HOURS)); dates.add(date.plus(1, ChronoUnit.DAYS)); dates.add(date.plus(1, ChronoUnit.WEEKS)); dates.add(date.plus(1, ChronoUnit.MONTHS)); dates.add(date.plus(1, ChronoUnit.YEARS)); dates.add(date.plus(100, ChronoUnit.YEARS)); // Check these dates against the corresponding dates for every calendar for (Chrono[] clist : data_of_calendars()) { List<ChronoZonedDateTime<?>> otherDates = new ArrayList<>(); Chrono chrono2 = ISOChrono.INSTANCE; // clist[0]; for (ChronoZonedDateTime<?> d : dates) { otherDates.add(chrono2.date(d).atTime(d.getTime()).atZone(d.getZone())); } // Now compare the sequence of original dates with the sequence of converted dates for (int i = 0; i < dates.size(); i++) { ChronoZonedDateTime<?> a = dates.get(i); for (int j = 0; j < otherDates.size(); j++) { ChronoZonedDateTime<?> b = otherDates.get(j); int cmp = ChronoZonedDateTime.INSTANT_COMPARATOR.compare(a, b); if (i < j) { assertTrue(cmp < 0, a + " compare " + b); assertEquals(a.isBefore(b), true, a + " isBefore " + b); assertEquals(a.isAfter(b), false, a + " ifAfter " + b); assertEquals(a.isEqual(b), false, a + " isEqual " + b); } else if (i > j) { assertTrue(cmp > 0, a + " compare " + b); assertEquals(a.isBefore(b), false, a + " isBefore " + b); assertEquals(a.isAfter(b), true, a + " ifAfter " + b); assertEquals(a.isEqual(b), false, a + " isEqual " + b); } else { assertTrue(cmp == 0, a + " compare " + b); assertEquals(a.isBefore(b), false, a + " isBefore " + b); assertEquals(a.isAfter(b), false, a + " ifAfter " + b); assertEquals(a.isEqual(b), true, a + " isEqual " + b); } } } } } // ----------------------------------------------------------------------- // Test Serialization of ISO via chrono API // ----------------------------------------------------------------------- @Test(groups = { "tck" }, dataProvider = "calendars") public <C extends Chrono<C>> void test_ChronoZonedDateTimeSerialization(C chrono) throws Exception { ZonedDateTime ref = LocalDate.of(2000, 1, 5).atTime(12, 1, 2, 3).atZone(ZoneId.of("GMT+01:23")); ChronoZonedDateTime<C> orginal = chrono.date(ref).atTime(ref.getTime()).atZone(ref.getZone()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(orginal); out.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream in = new ObjectInputStream(bais); ChronoZonedDateTime<C> ser = (ChronoZonedDateTime<C>) in.readObject(); assertEquals(ser, orginal, "deserialized date is wrong"); } /** * FixedAdjusted returns a fixed DateTime in all adjustments. Construct an adjuster with the DateTime that * should be returned from doWithAdjustment. */ static class FixedAdjuster implements DateTime.WithAdjuster, DateTime.PlusAdjuster, DateTime.MinusAdjuster { private DateTime datetime; FixedAdjuster(DateTime datetime) { this.datetime = datetime; } @Override public DateTime doWithAdjustment(DateTime ignore) { return this.datetime; } @Override public DateTime doPlusAdjustment(DateTime ignore) { return this.datetime; } @Override public DateTime doMinusAdjustment(DateTime ignore) { return this.datetime; } } /** * FixedPeriodUnit returns a fixed DateTime in all adjustments. Construct an FixedPeriodUnit with the * DateTime that should be returned from doPlus. */ static class FixedPeriodUnit implements PeriodUnit { private DateTime dateTime; FixedPeriodUnit(DateTime dateTime) { this.dateTime = dateTime; } @Override public String getName() { return "FixedPeriodUnit"; } @Override public Duration getDuration() { throw new UnsupportedOperationException("Not supported yet."); } @Override public boolean isDurationEstimated() { throw new UnsupportedOperationException("Not supported yet."); } @Override public boolean isSupported(DateTime dateTime) { throw new UnsupportedOperationException("Not supported yet."); } @Override public <R extends DateTime> R doPlus(R dateTime, long periodToAdd) { return (R) this.dateTime; } @Override public <R extends DateTime> PeriodBetween between(R dateTime1, R dateTime2) { throw new UnsupportedOperationException("Not supported yet."); } } /** * FixedDateTimeField returns a fixed DateTime in all adjustments. Construct an FixedDateTimeField with the * DateTime that should be returned from doWith. */ static class FixedDateTimeField implements DateTimeField { private DateTime dateTime; FixedDateTimeField(DateTime dateTime) { this.dateTime = dateTime; } @Override public String getName() { return "FixedDateTimeField"; } @Override public PeriodUnit getBaseUnit() { throw new UnsupportedOperationException("Not supported yet."); } @Override public PeriodUnit getRangeUnit() { throw new UnsupportedOperationException("Not supported yet."); } @Override public int compare(DateTimeAccessor dateTime1, DateTimeAccessor dateTime2) { throw new UnsupportedOperationException("Not supported yet."); } @Override public DateTimeValueRange range() { throw new UnsupportedOperationException("Not supported yet."); } @Override public boolean doIsSupported(DateTimeAccessor dateTime) { throw new UnsupportedOperationException("Not supported yet."); } @Override public DateTimeValueRange doRange(DateTimeAccessor dateTime) { throw new UnsupportedOperationException("Not supported yet."); } @Override public long doGet(DateTimeAccessor dateTime) { throw new UnsupportedOperationException("Not supported yet."); } @SuppressWarnings("unchecked") @Override public <R extends DateTime> R doWith(R dateTime, long newValue) { return (R) this.dateTime; } @Override public boolean resolve(DateTimeBuilder builder, long value) { throw new UnsupportedOperationException("Not supported yet."); } } }