Search in sources :

Example 16 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKThaiBuddhistChronology method test_resolve_yd_lenient.

@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_lenient(int y, int d, ThaiBuddhistDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d);
    ThaiBuddhistDate date = ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT);
    assertEquals(date, expected);
    assertEquals(fieldValues.size(), 0);
}
Also used : ThaiBuddhistDate(java.time.chrono.ThaiBuddhistDate) TemporalField(java.time.temporal.TemporalField) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 17 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKDateTimeFormatters method assertParseMatch.

private void assertParseMatch(TemporalAccessor parsed, Expected expected) {
    for (TemporalField field : expected.fieldValues.keySet()) {
        assertEquals(parsed.isSupported(field), true);
        parsed.getLong(field);
    }
    assertEquals(parsed.query(TemporalQueries.chronology()), expected.chrono);
    assertEquals(parsed.query(TemporalQueries.zoneId()), expected.zone);
}
Also used : TemporalField(java.time.temporal.TemporalField)

Example 18 with TemporalField

use of java.time.temporal.TemporalField in project jdk8u_jdk by JetBrains.

the class TCKLocalTime method test_with_longTemporalField_notChronoField.

// If the field is not a {@code ChronoField}, then the result of this method
// is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
// passing {@code this} as the argument.
@Test
public void test_with_longTemporalField_notChronoField() {
    final LocalTime result = LocalTime.of(12, 30);
    final LocalTime base = LocalTime.of(15, 45);
    TemporalField field = new TemporalField() {

        public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
            throw new UnsupportedOperationException();
        }

        public ValueRange range() {
            return null;
        }

        public boolean isTimeBased() {
            throw new UnsupportedOperationException();
        }

        public boolean isSupportedBy(TemporalAccessor temporal) {
            throw new UnsupportedOperationException();
        }

        public boolean isDateBased() {
            throw new UnsupportedOperationException();
        }

        public TemporalUnit getRangeUnit() {
            throw new UnsupportedOperationException();
        }

        public long getFrom(TemporalAccessor temporal) {
            throw new UnsupportedOperationException();
        }

        public TemporalUnit getBaseUnit() {
            throw new UnsupportedOperationException();
        }

        public <R extends Temporal> R adjustInto(R temporal, long newValue) {
            assertEquals(temporal, base);
            assertEquals(newValue, 12L);
            @SuppressWarnings("unchecked") R r = (R) result;
            return r;
        }
    };
    LocalTime test = base.with(field, 12L);
    assertSame(test, result);
}
Also used : MINUTE_OF_HOUR(java.time.temporal.ChronoField.MINUTE_OF_HOUR) FOREVER(java.time.temporal.ChronoUnit.FOREVER) TemporalField(java.time.temporal.TemporalField) TemporalAccessor(java.time.temporal.TemporalAccessor) LocalTime(java.time.LocalTime) Temporal(java.time.temporal.Temporal) Test(org.testng.annotations.Test)

Example 19 with TemporalField

use of java.time.temporal.TemporalField 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 20 with TemporalField

use of java.time.temporal.TemporalField 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)

Aggregations

TemporalField (java.time.temporal.TemporalField)104 Test (org.testng.annotations.Test)95 HashMap (java.util.HashMap)56 DateTimeException (java.time.DateTimeException)48 LocalDate (java.time.LocalDate)38 WeekFields (java.time.temporal.WeekFields)25 AbstractTCKTest (tck.java.time.AbstractTCKTest)25 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)20 DateTimeFormatter (java.time.format.DateTimeFormatter)19 TemporalAccessor (java.time.temporal.TemporalAccessor)13 JapaneseDate (java.time.chrono.JapaneseDate)12 HijrahDate (java.time.chrono.HijrahDate)11 MinguoDate (java.time.chrono.MinguoDate)10 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)10 Chronology (java.time.chrono.Chronology)4 IsoChronology (java.time.chrono.IsoChronology)4 ChronoLocalDate (java.time.chrono.ChronoLocalDate)3 HijrahChronology (java.time.chrono.HijrahChronology)3 JapaneseChronology (java.time.chrono.JapaneseChronology)3 MinguoChronology (java.time.chrono.MinguoChronology)3