test/java/time/tck/java/time/chrono/TestChronoLocalDateTime.java

Print this page

        

*** 52,95 **** * 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 tck.java.time.calendar; 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.util.ArrayList; - import java.util.List; - import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; ! import java.time.calendar.HijrahChrono; ! import java.time.calendar.JapaneseChrono; ! import java.time.calendar.MinguoChrono; ! import java.time.calendar.ThaiBuddhistChrono; ! import java.time.temporal.Chrono; ! import java.time.temporal.ChronoLocalDateTime; import java.time.temporal.ChronoUnit; - import java.time.temporal.SimplePeriod; import java.time.temporal.Temporal; import java.time.temporal.TemporalAccessor; - import java.time.format.DateTimeBuilder; - import java.time.temporal.TemporalAdder; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalField; - import java.time.temporal.TemporalSubtractor; - import java.time.temporal.ValueRange; - import java.time.temporal.ISOChrono; import java.time.temporal.TemporalUnit; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; --- 52,92 ---- * 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 tck.java.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.LocalDateTime; import java.time.LocalTime; ! import java.time.chrono.HijrahChronology; ! import java.time.chrono.JapaneseChronology; ! import java.time.chrono.MinguoChronology; ! import java.time.chrono.ThaiBuddhistChronology; ! import java.time.chrono.ChronoLocalDate; ! import java.time.chrono.ChronoLocalDateTime; ! import java.time.chrono.Chronology; ! import java.time.chrono.IsoChronology; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAdjuster; + import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalField; import java.time.temporal.TemporalUnit; + import java.time.temporal.ValueRange; + import java.util.ArrayList; + import java.util.List; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test;
*** 100,124 **** public class TestChronoLocalDateTime { //----------------------------------------------------------------------- // 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); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chrono[] clist : data_of_calendars()) { ! Chrono chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalAdjuster adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.with(adjuster); --- 97,121 ---- public class TestChronoLocalDateTime { //----------------------------------------------------------------------- // regular data factory for names and descriptions of available calendars //----------------------------------------------------------------------- @DataProvider(name = "calendars") ! Chronology[][] data_of_calendars() { ! return new Chronology[][]{ ! {HijrahChronology.INSTANCE}, ! {IsoChronology.INSTANCE}, ! {JapaneseChronology.INSTANCE}, ! {MinguoChronology.INSTANCE}, ! {ThaiBuddhistChronology.INSTANCE}}; } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badWithAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chronology[] clist : data_of_calendars()) { ! Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalAdjuster adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.with(adjuster);
*** 134,150 **** } } } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badPlusAdjusterChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chrono[] clist : data_of_calendars()) { ! Chrono chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); ! TemporalAdder adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.plus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + cdt + ", supplied: " + cdt2); --- 131,147 ---- } } } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badPlusAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chronology[] clist : data_of_calendars()) { ! Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); ! TemporalAmount adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.plus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + cdt + ", supplied: " + cdt2);
*** 158,174 **** } } } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badMinusAdjusterChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chrono[] clist : data_of_calendars()) { ! Chrono chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); ! TemporalSubtractor adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.minus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + cdt + ", supplied: " + cdt2); --- 155,171 ---- } } } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badMinusAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chronology[] clist : data_of_calendars()) { ! Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); ! TemporalAmount adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.minus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + cdt + ", supplied: " + cdt2);
*** 182,196 **** } } } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badPlusTemporalUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chrono[] clist : data_of_calendars()) { ! Chrono chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalUnit adjuster = new FixedTemporalUnit(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.plus(1, adjuster); --- 179,193 ---- } } } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badPlusTemporalUnitChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chronology[] clist : data_of_calendars()) { ! Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalUnit adjuster = new FixedTemporalUnit(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.plus(1, adjuster);
*** 206,220 **** } } } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badMinusTemporalUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chrono[] clist : data_of_calendars()) { ! Chrono chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalUnit adjuster = new FixedTemporalUnit(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.minus(1, adjuster); --- 203,217 ---- } } } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badMinusTemporalUnitChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chronology[] clist : data_of_calendars()) { ! Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalUnit adjuster = new FixedTemporalUnit(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.minus(1, adjuster);
*** 230,244 **** } } } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badTemporalFieldChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chrono[] clist : data_of_calendars()) { ! Chrono chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalField adjuster = new FixedTemporalField(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.with(adjuster, 1); --- 227,241 ---- } } } @Test(groups={"tck"}, dataProvider="calendars") ! public void test_badTemporalFieldChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); ! for (Chronology[] clist : data_of_calendars()) { ! Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalField adjuster = new FixedTemporalField(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.with(adjuster, 1);
*** 257,267 **** //----------------------------------------------------------------------- // isBefore, isAfter, isEqual //----------------------------------------------------------------------- @Test(groups={"tck"}, dataProvider="calendars") ! public void test_datetime_comparisons(Chrono chrono) { List<ChronoLocalDateTime<?>> dates = new ArrayList<>(); ChronoLocalDateTime<?> date = chrono.date(LocalDate.of(1900, 1, 1)).atTime(LocalTime.MIN); // Insert dates in order, no duplicates --- 254,264 ---- //----------------------------------------------------------------------- // isBefore, isAfter, isEqual //----------------------------------------------------------------------- @Test(groups={"tck"}, dataProvider="calendars") ! public void test_datetime_comparisons(Chronology chrono) { List<ChronoLocalDateTime<?>> dates = new ArrayList<>(); ChronoLocalDateTime<?> date = chrono.date(LocalDate.of(1900, 1, 1)).atTime(LocalTime.MIN); // Insert dates in order, no duplicates
*** 284,298 **** 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<ChronoLocalDateTime<?>> otherDates = new ArrayList<>(); ! Chrono chrono2 = clist[0]; for (ChronoLocalDateTime<?> d : dates) { ! otherDates.add(chrono2.date(d).atTime(d.getTime())); } // Now compare the sequence of original dates with the sequence of converted dates for (int i = 0; i < dates.size(); i++) { ChronoLocalDateTime<?> a = dates.get(i); --- 281,295 ---- 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 (Chronology[] clist : data_of_calendars()) { List<ChronoLocalDateTime<?>> otherDates = new ArrayList<>(); ! Chronology chrono2 = clist[0]; for (ChronoLocalDateTime<?> d : dates) { ! otherDates.add(chrono2.date(d).atTime(d.toLocalTime())); } // Now compare the sequence of original dates with the sequence of converted dates for (int i = 0; i < dates.size(); i++) { ChronoLocalDateTime<?> a = dates.get(i);
*** 322,350 **** //----------------------------------------------------------------------- // Test Serialization of ISO via chrono API //----------------------------------------------------------------------- @Test( groups={"tck"}, dataProvider="calendars") ! public <C extends Chrono<C>> void test_ChronoLocalDateTimeSerialization(C chrono) throws Exception { LocalDateTime ref = LocalDate.of(2000, 1, 5).atTime(12, 1, 2, 3); ! ChronoLocalDateTime<C> orginal = chrono.date(ref).atTime(ref.getTime()); 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); ! ChronoLocalDateTime<C> ser = (ChronoLocalDateTime<C>) in.readObject(); assertEquals(ser, orginal, "deserialized date is wrong"); } /** * FixedAdjusted returns a fixed Temporal in all adjustments. * Construct an adjuster with the Temporal that should be returned from adjust. */ ! static class FixedAdjuster implements TemporalAdjuster, TemporalAdder, TemporalSubtractor { private Temporal datetime; FixedAdjuster(Temporal datetime) { this.datetime = datetime; } --- 319,347 ---- //----------------------------------------------------------------------- // Test Serialization of ISO via chrono API //----------------------------------------------------------------------- @Test( groups={"tck"}, dataProvider="calendars") ! public void test_ChronoLocalDateTimeSerialization(Chronology chrono) throws Exception { LocalDateTime ref = LocalDate.of(2000, 1, 5).atTime(12, 1, 2, 3); ! ChronoLocalDateTime<?> orginal = chrono.date(ref).atTime(ref.toLocalTime()); 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); ! ChronoLocalDateTime<?> ser = (ChronoLocalDateTime<?>) in.readObject(); assertEquals(ser, orginal, "deserialized date is wrong"); } /** * FixedAdjusted returns a fixed Temporal in all adjustments. * Construct an adjuster with the Temporal that should be returned from adjust. */ ! static class FixedAdjuster implements TemporalAdjuster, TemporalAmount { private Temporal datetime; FixedAdjuster(Temporal datetime) { this.datetime = datetime; }
*** 362,371 **** --- 359,378 ---- @Override public Temporal subtractFrom(Temporal ignore) { return datetime; } + @Override + public long get(TemporalUnit unit) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public List<TemporalUnit> getUnits() { + throw new UnsupportedOperationException("Not supported yet."); + } + } /** * FixedTemporalUnit returns a fixed Temporal in all adjustments. * Construct an FixedTemporalUnit with the Temporal that should be returned from doAdd.
*** 391,412 **** public boolean isDurationEstimated() { throw new UnsupportedOperationException("Not supported yet."); } @Override ! public boolean isSupported(Temporal temporal) { throw new UnsupportedOperationException("Not supported yet."); } @SuppressWarnings("unchecked") @Override ! public <R extends Temporal> R doPlus(R dateTime, long periodToAdd) { return (R) this.temporal; } @Override ! public <R extends Temporal> SimplePeriod between(R dateTime1, R dateTime2) { throw new UnsupportedOperationException("Not supported yet."); } } /** --- 398,419 ---- public boolean isDurationEstimated() { throw new UnsupportedOperationException("Not supported yet."); } @Override ! public boolean isSupportedBy(Temporal temporal) { throw new UnsupportedOperationException("Not supported yet."); } @SuppressWarnings("unchecked") @Override ! public <R extends Temporal> R addTo(R temporal, long amount) { return (R) this.temporal; } @Override ! public long between(Temporal temporal1, Temporal temporal2) { throw new UnsupportedOperationException("Not supported yet."); } } /**
*** 438,469 **** public ValueRange range() { throw new UnsupportedOperationException("Not supported yet."); } @Override ! public boolean doIsSupported(TemporalAccessor temporal) { throw new UnsupportedOperationException("Not supported yet."); } @Override ! public ValueRange doRange(TemporalAccessor temporal) { throw new UnsupportedOperationException("Not supported yet."); } @Override ! public long doGet(TemporalAccessor temporal) { throw new UnsupportedOperationException("Not supported yet."); } @SuppressWarnings("unchecked") @Override ! public <R extends Temporal> R doWith(R temporal, long newValue) { return (R) this.temporal; } - - @Override - public boolean resolve(DateTimeBuilder builder, long value) { - throw new UnsupportedOperationException("Not supported yet."); - } - } } --- 445,470 ---- public ValueRange range() { throw new UnsupportedOperationException("Not supported yet."); } @Override ! public boolean isSupportedBy(TemporalAccessor temporal) { throw new UnsupportedOperationException("Not supported yet."); } @Override ! public ValueRange rangeRefinedBy(TemporalAccessor temporal) { throw new UnsupportedOperationException("Not supported yet."); } @Override ! public long getFrom(TemporalAccessor temporal) { throw new UnsupportedOperationException("Not supported yet."); } @SuppressWarnings("unchecked") @Override ! public <R extends Temporal> R adjustInto(R temporal, long newValue) { return (R) this.temporal; } } }