Search in sources :

Example 11 with DateTimeException

use of java.time.DateTimeException in project jdk8u_jdk by JetBrains.

the class TCKLocalTime method test_plus_longTemporalUnit_invalidUnit.

@Test
public void test_plus_longTemporalUnit_invalidUnit() {
    for (TemporalUnit unit : INVALID_UNITS) {
        try {
            TEST_12_30_40_987654321.plus(1, unit);
            fail("Unit should not be allowed " + unit);
        } catch (DateTimeException ex) {
        // expected
        }
    }
}
Also used : DateTimeException(java.time.DateTimeException) TemporalUnit(java.time.temporal.TemporalUnit) Test(org.testng.annotations.Test)

Example 12 with DateTimeException

use of java.time.DateTimeException in project jdk8u_jdk by JetBrains.

the class TCKZoneId method factory_from_TemporalAccessor_zoneId.

//-----------------------------------------------------------------------
// from(TemporalAccessor)
//-----------------------------------------------------------------------
@Test
public void factory_from_TemporalAccessor_zoneId() {
    TemporalAccessor mock = new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            return false;
        }

        @Override
        public long getLong(TemporalField field) {
            throw new DateTimeException("Mock");
        }

        @SuppressWarnings("unchecked")
        @Override
        public <R> R query(TemporalQuery<R> query) {
            if (query == TemporalQueries.zoneId()) {
                return (R) ZoneId.of("Europe/Paris");
            }
            return TemporalAccessor.super.query(query);
        }
    };
    assertEquals(ZoneId.from(mock), ZoneId.of("Europe/Paris"));
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) TemporalQuery(java.time.temporal.TemporalQuery) Test(org.testng.annotations.Test)

Example 13 with DateTimeException

use of java.time.DateTimeException in project jdk8u_jdk by JetBrains.

the class TestDateTimeBuilderCombinations method test_derive.

@Test(dataProvider = "combine")
public void test_derive(final TemporalField field1, final Number value1, final TemporalField field2, final Number value2, final TemporalField field3, final Number value3, final TemporalField field4, final Number value4, Class<?> query, Object expectedVal) {
    // mock for testing that does not fully comply with TemporalAccessor contract
    TemporalAccessor test = new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            return field == field1 || field == field2 || field == field3 || field == field4;
        }

        @Override
        public long getLong(TemporalField field) {
            if (field == field1) {
                return value1.longValue();
            }
            if (field == field2) {
                return value2.longValue();
            }
            if (field == field3) {
                return value3.longValue();
            }
            if (field == field4) {
                return value4.longValue();
            }
            throw new DateTimeException("Unsupported");
        }
    };
    String str = "";
    DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
    dtfb.appendValue(field1).appendLiteral('-');
    str += value1 + "-";
    if (field2 != null) {
        dtfb.appendValue(field2).appendLiteral('-');
        str += value2 + "-";
    }
    if (field3 != null) {
        dtfb.appendValue(field3).appendLiteral('-');
        str += value3 + "-";
    }
    if (field4 != null) {
        dtfb.appendValue(field4).appendLiteral('-');
        str += value4 + "-";
    }
    TemporalAccessor parsed = dtfb.toFormatter().parse(str);
    if (query == LocalDate.class) {
        if (expectedVal != null) {
            assertEquals(parsed.query(LocalDate::from), expectedVal);
        } else {
            try {
                parsed.query(LocalDate::from);
                fail();
            } catch (DateTimeException ex) {
            // expected
            }
        }
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 14 with DateTimeException

use of java.time.DateTimeException in project jdk8u_jdk by JetBrains.

the class TCKInstantPrinterParser method test_parse_digitsNine.

@Test(dataProvider = "parseDigits")
public void test_parse_digitsNine(long instantSecs, int nano, String input) {
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendInstant(9).toFormatter();
    if (input.charAt(input.length() - 11) == '.') {
        Instant expected = Instant.ofEpochSecond(instantSecs, nano);
        assertEquals(f.parse(input, Instant::from), expected);
        assertEquals(f.parse(input).query(DateTimeFormatter.parsedExcessDays()), Period.ZERO);
        assertEquals(f.parse(input).query(DateTimeFormatter.parsedLeapSecond()), Boolean.FALSE);
    } else {
        try {
            f.parse(input, Instant::from);
            fail();
        } catch (DateTimeException ex) {
        // expected
        }
    }
}
Also used : DateTimeException(java.time.DateTimeException) Instant(java.time.Instant) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 15 with DateTimeException

use of java.time.DateTimeException in project jdk8u_jdk by JetBrains.

the class TCKHijrahChronology method test_resolve_ymd_smart.

@Test(dataProvider = "resolve_ymd")
public void test_resolve_ymd_smart(int y, int m, int d, HijrahDate expected, Object smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m);
    fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d);
    if (Boolean.TRUE.equals(smart)) {
        HijrahDate date = HijrahChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else if (smart instanceof HijrahDate) {
        HijrahDate date = HijrahChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
        assertEquals(date, smart);
    } else {
        try {
            HijrahDate date = HijrahChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART);
            fail("Should have failed, returned: " + date);
        } catch (DateTimeException ex) {
        // expected
        }
    }
}
Also used : HijrahDate(java.time.chrono.HijrahDate) TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Aggregations

DateTimeException (java.time.DateTimeException)75 Test (org.testng.annotations.Test)58 TemporalField (java.time.temporal.TemporalField)48 HashMap (java.util.HashMap)34 LocalDate (java.time.LocalDate)18 TemporalAccessor (java.time.temporal.TemporalAccessor)13 DateTimeFormatter (java.time.format.DateTimeFormatter)11 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)10 MinguoDate (java.time.chrono.MinguoDate)7 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)7 MockFieldValue (test.java.time.temporal.MockFieldValue)7 HijrahDate (java.time.chrono.HijrahDate)6 ChronoLocalDate (java.time.chrono.ChronoLocalDate)5 JapaneseDate (java.time.chrono.JapaneseDate)5 Chronology (java.time.chrono.Chronology)4 IOException (java.io.IOException)3 WeekFields (java.time.temporal.WeekFields)3 AbstractTCKTest (tck.java.time.AbstractTCKTest)3 ResolverStyle (java.time.format.ResolverStyle)2 TemporalQuery (java.time.temporal.TemporalQuery)2