Search in sources :

Example 46 with WeekFields

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

the class TCKWeekFieldsSerialization method test_invalid_serialform.

@Test
public void test_invalid_serialform() throws Exception {
    WeekFields wf = WeekFields.of(DayOfWeek.MONDAY, 7);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(wf);
    byte[] template = baos.toByteArray();
    // (minimalDays = 5) {
    byte[] good1 = { 0, 0, 0, 5 };
    byte[] val = Arrays.copyOf(template, template.length);
    System.arraycopy(good1, 0, val, 105, good1.length);
    try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(val))) {
        Object o = in.readObject();
        assertEquals(o, WeekFields.of(DayOfWeek.MONDAY, 5), "Should be MONDAY, min = 5");
    } catch (Exception ioe) {
        fail("Unexpected exception " + ioe);
    }
    // (minimalDays < 1) {
    byte[] bad1 = { 0, 0, 0, 0 };
    val = Arrays.copyOf(template, template.length);
    System.arraycopy(bad1, 0, val, 105, bad1.length);
    try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(val))) {
        in.readObject();
        fail("Invalid minimalDays < 1 " + WeekFields.class.getName());
    } catch (Exception ioe) {
    // Expected exception
    }
    // (minimalDays > 7) {
    byte[] bad2 = { 0, 0, 0, 8 };
    val = Arrays.copyOf(template, template.length);
    System.arraycopy(bad2, 0, val, 105, bad2.length);
    try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(val))) {
        in.readObject();
        fail("Invalid minimalDays > 7 " + WeekFields.class.getName());
    } catch (Exception ioe) {
    // Expected exception
    }
    // (StartDay = null) {
    byte[] bad3 = { 0x70 };
    val = Arrays.copyOf(template, 110);
    System.arraycopy(bad3, 0, val, 105 + 4, bad3.length);
    try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(val))) {
        in.readObject();
        fail("Invalid startDay == null " + WeekFields.class.getName());
    } catch (Exception ioe) {
    // Expected exception
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WeekFields(java.time.temporal.WeekFields) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 47 with WeekFields

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

the class TCKWeekFields method test_parse_resolve_localizedWom_lenient.

@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWom_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField womField = week.weekOfMonth();
    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral(':').appendValue(MONTH_OF_YEAR).appendLiteral(':').appendValue(womField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(LENIENT);
        int wom = date.get(womField);
        int dow = date.get(DAY_OF_WEEK);
        for (int j = wom - 10; j < wom + 10; j++) {
            String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j);
        }
        date = date.plusDays(1);
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 48 with WeekFields

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

the class TCKWeekFields method test_parse_resolve_localizedWoWBY_strict.

@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWoWBY_strict(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(yowbyField).appendLiteral(':').appendValue(wowbyField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(STRICT);
    String str = "2012:0:1";
    try {
        LocalDate date = LocalDate.parse(str, f);
        assertEquals(date.get(yowbyField), 2012);
        assertEquals(date.get(wowbyField), 0);
        assertEquals(date.get(DAY_OF_WEEK), 1);
    } catch (DateTimeException ex) {
    // expected
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) WeekFields(java.time.temporal.WeekFields) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 49 with WeekFields

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

the class TCKWeekFields method test_parse_resolve_localizedWoWBY_lenient.

@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWoWBY_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();
    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(yowbyField).appendLiteral(':').appendValue(wowbyField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(LENIENT);
        int wowby = date.get(wowbyField);
        int dow = date.get(DAY_OF_WEEK);
        for (int j = wowby - 60; j < wowby + 60; j++) {
            String str = date.get(yowbyField) + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - wowby), " ::" + str + ": :" + i + "::" + j);
        }
        date = date.plusDays(1);
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 50 with WeekFields

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

the class TCKWeekFields method test_weekOfYearField.

@Test(dataProvider = "weekFields")
public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) {
    // Known to be ISO Monday
    LocalDate day = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField woyField = week.weekOfYear();
    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOY = day.get(woyField);
        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = day.withDayOfYear(1);
        int offset = -(day1.get(dowField) - 1);
        int week1 = day1.get(woyField);
        if (week1 == 0) {
            // week of the 1st is partial; start with first full week
            offset += 7;
        }
        offset += actualDOW - 1;
        offset += (actualWOY - 1) * 7;
        LocalDate result = day1.plusDays(offset);
        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result));
        day = day.plusDays(1);
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Aggregations

WeekFields (java.time.temporal.WeekFields)73 AbstractTCKTest (tck.java.time.AbstractTCKTest)58 LocalDate (java.time.LocalDate)57 TemporalField (java.time.temporal.TemporalField)51 DateTimeFormatter (java.time.format.DateTimeFormatter)36 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)34 Test (org.junit.Test)33 Test (org.testng.annotations.Test)33 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)31 DayOfWeek (java.time.DayOfWeek)7 DateTimeException (java.time.DateTimeException)6 ValueRange (java.time.temporal.ValueRange)6 ParsePosition (java.text.ParsePosition)4 TemporalAccessor (java.time.temporal.TemporalAccessor)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 GregorianCalendar (java.util.GregorianCalendar)2