diff --git a/src/main/java/com/cedarsoftware/util/convert/SqlDateConversions.java b/src/main/java/com/cedarsoftware/util/convert/SqlDateConversions.java index e56c60be..379804da 100644 --- a/src/main/java/com/cedarsoftware/util/convert/SqlDateConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/SqlDateConversions.java @@ -11,6 +11,7 @@ import java.time.OffsetDateTime; import java.time.Year; import java.time.YearMonth; +import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Calendar; import java.util.Date; @@ -72,12 +73,25 @@ static BigInteger toBigInteger(Object from, Converter converter) { } static BigDecimal toBigDecimal(Object from, Converter converter) { + // Cast to the expected type. (Consider changing the parameter type if possible.) java.sql.Date sqlDate = (java.sql.Date) from; - return new BigDecimal(sqlDate.toLocalDate() - .atStartOfDay(converter.getOptions().getZoneId()) - .toInstant() - .toEpochMilli()) - .divide(BigDecimal.valueOf(1000), 9, RoundingMode.DOWN); + + // Get the ZoneId from the converter options. + ZoneId zone = converter.getOptions().getZoneId(); + + // Convert the sqlDate to an Instant (at the start of day in the given zone). + Instant instant = sqlDate.toLocalDate().atStartOfDay(zone).toInstant(); + + // Convert the epoch millis into seconds. + // (We use a division with 9 digits of scale so that if there are fractional parts + // they are preserved, then we remove trailing zeros.) + BigDecimal seconds = BigDecimal.valueOf(instant.toEpochMilli()) + .divide(BigDecimal.valueOf(1000), 9, RoundingMode.DOWN) + .stripTrailingZeros(); + + // Rebuild the BigDecimal from its plain string representation. + // This ensures that when you later call toString() it will not use exponential notation. + return new BigDecimal(seconds.toPlainString()); } static Instant toInstant(Object from, Converter converter) { diff --git a/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java b/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java index 24c7b517..306991bb 100644 --- a/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java +++ b/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java @@ -65,7 +65,6 @@ import org.junit.jupiter.params.provider.MethodSource; import static com.cedarsoftware.util.MapUtilities.mapOf; -import static com.cedarsoftware.util.convert.Converter.VALUE; import static com.cedarsoftware.util.convert.MapConversions.CALENDAR; import static com.cedarsoftware.util.convert.MapConversions.CAUSE; import static com.cedarsoftware.util.convert.MapConversions.CAUSE_MESSAGE; @@ -91,6 +90,7 @@ import static com.cedarsoftware.util.convert.MapConversions.URI_KEY; import static com.cedarsoftware.util.convert.MapConversions.URL_KEY; import static com.cedarsoftware.util.convert.MapConversions.V; +import static com.cedarsoftware.util.convert.MapConversions.VALUE; import static com.cedarsoftware.util.convert.MapConversions.YEAR_MONTH; import static com.cedarsoftware.util.convert.MapConversions.ZONE; import static com.cedarsoftware.util.convert.MapConversions.ZONED_DATE_TIME; @@ -221,6 +221,8 @@ public ZoneId getZoneId() { loadUuidTests(); loadEnumTests(); loadThrowableTests(); + loadCurrencyTests(); + loadPatternTests(); } /** @@ -234,6 +236,39 @@ static Map.Entry, Class> pair(Class source, Class target) { return new AbstractMap.SimpleImmutableEntry<>(source, target); } + /** + * Currency + */ + private static void loadPatternTests() { + TEST_DB.put(pair(Void.class, Pattern.class), new Object[][]{ + {null, null}, + }); + } + + /** + * Currency + */ + private static void loadCurrencyTests() { + TEST_DB.put(pair(Void.class, Currency.class), new Object[][]{ + { null, null}, + }); + TEST_DB.put(pair(Currency.class, Currency.class), new Object[][]{ + { Currency.getInstance("USD"), Currency.getInstance("USD") }, + { Currency.getInstance("JPY"), Currency.getInstance("JPY") }, + }); + TEST_DB.put(pair(Map.class, Currency.class), new Object[][] { + // Bidirectional tests (true) - major currencies + {mapOf(VALUE, "USD"), Currency.getInstance("USD"), true}, + {mapOf(VALUE, "EUR"), Currency.getInstance("EUR"), true}, + {mapOf(VALUE, "JPY"), Currency.getInstance("JPY"), true}, + {mapOf(VALUE, "GBP"), Currency.getInstance("GBP"), true}, + + // One-way tests (false) - with whitespace that should be trimmed + {mapOf(V, " USD "), Currency.getInstance("USD"), false}, + {mapOf(VALUE, " EUR "), Currency.getInstance("EUR"), false}, + {mapOf(VALUE, "\tJPY\n"), Currency.getInstance("JPY"), false} + }); } + /** * Enum */ @@ -571,7 +606,7 @@ private static void loadClassTests() { {"NoWayJose", new IllegalArgumentException("not found")}, }); TEST_DB.put(pair(Map.class, Class.class), new Object[][]{ - { mapOf(VALUE, Long.class), Long.class, true}, + { mapOf(V, Long.class), Long.class, true}, { mapOf(VALUE, "not a class"), new IllegalArgumentException("Cannot convert String 'not a class' to class. Class not found")}, }); } @@ -583,6 +618,9 @@ private static void loadMapTests() { TEST_DB.put(pair(Void.class, Map.class), new Object[][]{ {null, null} }); + TEST_DB.put(pair(Pattern.class, Map.class), new Object[][]{ + {Pattern.compile("(foo|bar)"), mapOf(VALUE, "(foo|bar)")}, + }); TEST_DB.put(pair(Map.class, Map.class), new Object[][]{ { new HashMap<>(), new IllegalArgumentException("Unsupported conversion") } }); @@ -1526,6 +1564,100 @@ private static void loadYearTests() { TEST_DB.put(pair(Year.class, Year.class), new Object[][]{ {Year.of(1970), Year.of(1970), true}, }); + TEST_DB.put(pair(Calendar.class, Year.class), new Object[][] { + {createCalendar(1888, 1, 2, 0, 0, 0), Year.of(1888), false}, + {createCalendar(1969, 12, 31, 0, 0, 0), Year.of(1969), false}, + {createCalendar(1970, 1, 1, 0, 0, 0), Year.of(1970), false}, + {createCalendar(2023, 6, 15, 0, 0, 0), Year.of(2023), false}, + {createCalendar(2023, 6, 15, 12, 30, 45), Year.of(2023), false}, + {createCalendar(2023, 12, 31, 23, 59, 59), Year.of(2023), false}, + {createCalendar(2023, 1, 1, 1, 0, 1), Year.of(2023), false} + }); + TEST_DB.put(pair(Date.class, Year.class), new Object[][] { + {date("1888-01-01T15:00:00Z"), Year.of(1888), false}, // 1888-01-02 00:00 Tokyo + {date("1969-12-30T15:00:00Z"), Year.of(1969), false}, // 1969-12-31 00:00 Tokyo + {date("1969-12-31T15:00:00Z"), Year.of(1970), false}, // 1970-01-01 00:00 Tokyo + {date("2023-06-14T15:00:00Z"), Year.of(2023), false}, // 2023-06-15 00:00 Tokyo + {date("2023-06-15T12:30:45Z"), Year.of(2023), false}, // 2023-06-15 21:30:45 Tokyo + {date("2023-06-15T14:59:59Z"), Year.of(2023), false}, // 2023-06-15 23:59:59 Tokyo + {date("2023-06-15T00:00:01Z"), Year.of(2023), false} // 2023-06-15 09:00:01 Tokyo + }); + TEST_DB.put(pair(java.sql.Date.class, Year.class), new Object[][] { + {java.sql.Date.valueOf("1888-01-02"), Year.of(1888), false}, + {java.sql.Date.valueOf("1969-12-31"), Year.of(1969), false}, + {java.sql.Date.valueOf("1970-01-01"), Year.of(1970), false}, + {java.sql.Date.valueOf("2023-06-15"), Year.of(2023), false}, + {java.sql.Date.valueOf("2023-01-01"), Year.of(2023), false}, + {java.sql.Date.valueOf("2023-12-31"), Year.of(2023), false} + }); + TEST_DB.put(pair(LocalDate.class, Year.class), new Object[][] { + {LocalDate.of(1888, 1, 2), Year.of(1888), false}, + {LocalDate.of(1969, 12, 31), Year.of(1969), false}, + {LocalDate.of(1970, 1, 1), Year.of(1970), false}, + {LocalDate.of(2023, 6, 15), Year.of(2023), false}, + {LocalDate.of(2023, 1, 1), Year.of(2023), false}, + {LocalDate.of(2023, 12, 31), Year.of(2023), false} + }); + TEST_DB.put(pair(LocalDateTime.class, Year.class), new Object[][] { + {LocalDateTime.of(1888, 1, 2, 0, 0), Year.of(1888), false}, + {LocalDateTime.of(1969, 12, 31, 0, 0), Year.of(1969), false}, + {LocalDateTime.of(1970, 1, 1, 0, 0), Year.of(1970), false}, + {LocalDateTime.of(2023, 6, 15, 0, 0), Year.of(2023), false}, + + // One-way tests (false) - various times on same date + {LocalDateTime.of(2023, 6, 15, 12, 30, 45), Year.of(2023), false}, + {LocalDateTime.of(2023, 6, 15, 23, 59, 59, 999_999_999), Year.of(2023), false}, + {LocalDateTime.of(2023, 6, 15, 0, 0, 0, 1), Year.of(2023), false}, + + // One-way tests (false) - different dates in same year + {LocalDateTime.of(2023, 1, 1, 12, 0), Year.of(2023), false}, + {LocalDateTime.of(2023, 12, 31, 12, 0), Year.of(2023), false} + }); + TEST_DB.put(pair(OffsetDateTime.class, Year.class), new Object[][] { + {odt("1888-01-01T15:00:00Z"), Year.of(1888), false}, // 1888-01-02 00:00 Tokyo + {odt("1969-12-30T15:00:00Z"), Year.of(1969), false}, // 1969-12-31 00:00 Tokyo + {odt("1969-12-31T15:00:00Z"), Year.of(1970), false}, // 1970-01-01 00:00 Tokyo + {odt("2023-06-14T15:00:00Z"), Year.of(2023), false}, // 2023-06-15 00:00 Tokyo + + // One-way tests (false) - various times before Tokyo midnight + {odt("2023-06-15T12:30:45Z"), Year.of(2023), false}, // 21:30:45 Tokyo + {odt("2023-06-15T14:59:59.999Z"), Year.of(2023), false}, // 23:59:59.999 Tokyo + {odt("2023-06-15T00:00:01Z"), Year.of(2023), false}, // 09:00:01 Tokyo + + // One-way tests (false) - same date in different offset + {odt("2023-06-15T00:00:00+09:00"), Year.of(2023), false}, // Tokyo local time + {odt("2023-06-15T00:00:00-05:00"), Year.of(2023), false} // US Eastern time + }); + TEST_DB.put(pair(ZonedDateTime.class, Year.class), new Object[][] { + {zdt("1888-01-01T15:00:00Z"), Year.of(1888), false}, // 1888-01-02 00:00 Tokyo + {zdt("1969-12-30T15:00:00Z"), Year.of(1969), false}, // 1969-12-31 00:00 Tokyo + {zdt("1969-12-31T15:00:00Z"), Year.of(1970), false}, // 1970-01-01 00:00 Tokyo + {zdt("2023-06-14T15:00:00Z"), Year.of(2023), false}, // 2023-06-15 00:00 Tokyo + + // One-way tests (false) - various times before Tokyo midnight + {zdt("2023-06-15T12:30:45Z"), Year.of(2023), false}, // 21:30:45 Tokyo + {zdt("2023-06-15T14:59:59.999Z"), Year.of(2023), false}, // 23:59:59.999 Tokyo + {zdt("2023-06-15T00:00:01Z"), Year.of(2023), false}, // 09:00:01 Tokyo + + // One-way tests (false) - same time in different zones + {ZonedDateTime.of(2023, 6, 15, 0, 0, 0, 0, ZoneId.of("Asia/Tokyo")), Year.of(2023), false}, + {ZonedDateTime.of(2023, 6, 15, 0, 0, 0, 0, ZoneId.of("America/New_York")), Year.of(2023), false} + }); + TEST_DB.put(pair(Timestamp.class, Year.class), new Object[][] { + // Bidirectional tests (true) - all at midnight Tokyo (+09:00) + {timestamp("1888-01-01T15:00:00Z"), Year.of(1888), false}, // 1888-01-02 00:00 Tokyo + {timestamp("1969-12-30T15:00:00Z"), Year.of(1969), false}, // 1969-12-31 00:00 Tokyo + {timestamp("1969-12-31T15:00:00Z"), Year.of(1970), false}, // 1970-01-01 00:00 Tokyo + {timestamp("2023-06-14T15:00:00Z"), Year.of(2023), false}, // 2023-06-15 00:00 Tokyo + + // One-way tests (false) - various times before Tokyo midnight + {timestamp("2023-06-15T12:30:45.123Z"), Year.of(2023), false}, // 21:30:45 Tokyo + {timestamp("2023-06-15T14:59:59.999Z"), Year.of(2023), false}, // 23:59:59.999 Tokyo + {timestamp("2023-06-15T00:00:00.001Z"), Year.of(2023), false}, // 09:00:00.001 Tokyo + + // One-way tests (false) - with nanosecond precision + {timestamp("2023-06-15T12:00:00.123456789Z"), Year.of(2023), false} // 21:00:00.123456789 Tokyo + }); TEST_DB.put(pair(String.class, Year.class), new Object[][]{ {"", null}, {"2024-03-23T04:10", Year.of(2024)}, @@ -1620,6 +1752,101 @@ private static void loadYearMonthTests() { {YearMonth.of(1970, 1), YearMonth.of(1970, 1), true}, {YearMonth.of(1999, 6), YearMonth.of(1999, 6), true}, }); + TEST_DB.put(pair(Date.class, YearMonth.class), new Object[][] { + {date("1888-01-01T15:00:00Z"), YearMonth.of(1888, 1), false}, // 1888-01-02 00:00 Tokyo + {date("1969-12-30T15:00:00Z"), YearMonth.of(1969, 12), false}, // 1969-12-31 00:00 Tokyo + {date("1969-12-31T15:00:00Z"), YearMonth.of(1970, 1), false}, // 1970-01-01 00:00 Tokyo + {date("2023-06-14T15:00:00Z"), YearMonth.of(2023, 6), false}, // 2023-06-15 00:00 Tokyo + {date("2023-06-15T12:30:45Z"), YearMonth.of(2023, 6), false}, // 2023-06-15 21:30:45 Tokyo + {date("2023-06-15T14:59:59Z"), YearMonth.of(2023, 6), false}, // 2023-06-15 23:59:59 Tokyo + {date("2023-06-15T00:00:01Z"), YearMonth.of(2023, 6), false} // 2023-06-15 09:00:01 Tokyo + }); + TEST_DB.put(pair(java.sql.Date.class, YearMonth.class), new Object[][] { + {java.sql.Date.valueOf("1888-01-02"), YearMonth.of(1888, 1), false}, + {java.sql.Date.valueOf("1969-12-31"), YearMonth.of(1969, 12), false}, + {java.sql.Date.valueOf("1970-01-01"), YearMonth.of(1970, 1), false}, + {java.sql.Date.valueOf("2023-06-15"), YearMonth.of(2023, 6), false}, + {java.sql.Date.valueOf("2023-06-01"), YearMonth.of(2023, 6), false}, + {java.sql.Date.valueOf("2023-06-30"), YearMonth.of(2023, 6), false} + }); + TEST_DB.put(pair(LocalDate.class, YearMonth.class), new Object[][] { + {LocalDate.of(1888, 1, 2), YearMonth.of(1888, 1), false}, + {LocalDate.of(1969, 12, 31), YearMonth.of(1969, 12), false}, + {LocalDate.of(1970, 1, 1), YearMonth.of(1970, 1), false}, + {LocalDate.of(2023, 6, 15), YearMonth.of(2023, 6), false}, + {LocalDate.of(2023, 6, 1), YearMonth.of(2023, 6), false}, + {LocalDate.of(2023, 6, 30), YearMonth.of(2023, 6), false} + }); + TEST_DB.put(pair(LocalDateTime.class, YearMonth.class), new Object[][] { + {LocalDateTime.of(1888, 1, 2, 0, 0), YearMonth.of(1888, 1), false}, + {LocalDateTime.of(1969, 12, 31, 0, 0), YearMonth.of(1969, 12), false}, + {LocalDateTime.of(1970, 1, 1, 0, 0), YearMonth.of(1970, 1), false}, + {LocalDateTime.of(2023, 6, 15, 0, 0), YearMonth.of(2023, 6), false}, + + // One-way tests (false) - various times on same date + {LocalDateTime.of(2023, 6, 15, 12, 30, 45), YearMonth.of(2023, 6), false}, + {LocalDateTime.of(2023, 6, 15, 23, 59, 59, 999_999_999), YearMonth.of(2023, 6), false}, + {LocalDateTime.of(2023, 6, 15, 0, 0, 0, 1), YearMonth.of(2023, 6), false}, + + // One-way tests (false) - different days in same month + {LocalDateTime.of(2023, 6, 1, 12, 0), YearMonth.of(2023, 6), false}, + {LocalDateTime.of(2023, 6, 30, 12, 0), YearMonth.of(2023, 6), false} + }); + TEST_DB.put(pair(OffsetDateTime.class, YearMonth.class), new Object[][] { + // Bidirectional tests (true) - all at midnight Tokyo (+09:00) + {odt("1888-01-01T15:00:00Z"), YearMonth.of(1888, 1), false}, // 1888-01-02 00:00 Tokyo + {odt("1969-12-30T15:00:00Z"), YearMonth.of(1969, 12), false}, // 1969-12-31 00:00 Tokyo + {odt("1969-12-31T15:00:00Z"), YearMonth.of(1970, 1), false}, // 1970-01-01 00:00 Tokyo + {odt("2023-06-14T15:00:00Z"), YearMonth.of(2023, 6), false}, // 2023-06-15 00:00 Tokyo + + // One-way tests (false) - various times before Tokyo midnight + {odt("2023-06-15T12:30:45Z"), YearMonth.of(2023, 6), false}, // 21:30:45 Tokyo + {odt("2023-06-15T14:59:59.999Z"), YearMonth.of(2023, 6), false}, // 23:59:59.999 Tokyo + {odt("2023-06-15T00:00:01Z"), YearMonth.of(2023, 6), false}, // 09:00:01 Tokyo + + // One-way tests (false) - same date in different offset + {odt("2023-06-15T00:00:00+09:00"), YearMonth.of(2023, 6), false}, // Tokyo local time + {odt("2023-06-15T00:00:00-05:00"), YearMonth.of(2023, 6), false} // US Eastern time + }); + TEST_DB.put(pair(ZonedDateTime.class, YearMonth.class), new Object[][] { + {zdt("1888-01-01T15:00:00Z"), YearMonth.of(1888, 1), false}, // 1888-01-02 00:00 Tokyo + {zdt("1969-12-30T15:00:00Z"), YearMonth.of(1969, 12), false}, // 1969-12-31 00:00 Tokyo + {zdt("1969-12-31T15:00:00Z"), YearMonth.of(1970, 1), false}, // 1970-01-01 00:00 Tokyo + {zdt("2023-06-14T15:00:00Z"), YearMonth.of(2023, 6), false}, // 2023-06-15 00:00 Tokyo + + // One-way tests (false) - various times before Tokyo midnight + {zdt("2023-06-15T12:30:45Z"), YearMonth.of(2023, 6), false}, // 21:30:45 Tokyo + {zdt("2023-06-15T14:59:59.999Z"), YearMonth.of(2023, 6), false}, // 23:59:59.999 Tokyo + {zdt("2023-06-15T00:00:01Z"), YearMonth.of(2023, 6), false}, // 09:00:01 Tokyo + + // One-way tests (false) - same time in different zones + {ZonedDateTime.of(2023, 6, 15, 0, 0, 0, 0, ZoneId.of("Asia/Tokyo")), YearMonth.of(2023, 6), false}, + {ZonedDateTime.of(2023, 6, 15, 0, 0, 0, 0, ZoneId.of("America/New_York")), YearMonth.of(2023, 6), false} + }); + TEST_DB.put(pair(Timestamp.class, YearMonth.class), new Object[][] { + // Bidirectional tests (true) - all at midnight Tokyo (+09:00) + {timestamp("1888-01-01T15:00:00Z"), YearMonth.of(1888, 1), false}, // 1888-01-02 00:00 Tokyo + {timestamp("1969-12-30T15:00:00Z"), YearMonth.of(1969, 12), false}, // 1969-12-31 00:00 Tokyo + {timestamp("1969-12-31T15:00:00Z"), YearMonth.of(1970, 1), false}, // 1970-01-01 00:00 Tokyo + {timestamp("2023-06-14T15:00:00Z"), YearMonth.of(2023, 6), false}, // 2023-06-15 00:00 Tokyo + + // One-way tests (false) - various times before Tokyo midnight + {timestamp("2023-06-15T12:30:45.123Z"), YearMonth.of(2023, 6), false}, // 21:30:45 Tokyo + {timestamp("2023-06-15T14:59:59.999Z"), YearMonth.of(2023, 6), false}, // 23:59:59.999 Tokyo + {timestamp("2023-06-15T00:00:00.001Z"), YearMonth.of(2023, 6), false}, // 09:00:00.001 Tokyo + + // One-way tests (false) - with nanosecond precision + {timestamp("2023-06-15T12:00:00.123456789Z"), YearMonth.of(2023, 6), false} // 21:00:00.123456789 Tokyo + }); + TEST_DB.put(pair(Calendar.class, YearMonth.class), new Object[][] { + {createCalendar(1888, 1, 2, 0, 0, 0), YearMonth.of(1888, 1), false}, + {createCalendar(1969, 12, 31, 0, 0, 0), YearMonth.of(1969, 12), false}, + {createCalendar(1970, 1, 1, 0, 0, 0), YearMonth.of(1970, 1), false}, + {createCalendar(2023, 6, 15, 0, 0, 0), YearMonth.of(2023, 6), false}, + {createCalendar(2023, 6, 15, 12, 30, 45), YearMonth.of(2023, 6), false}, + {createCalendar(2023, 12, 31, 23, 59, 59), YearMonth.of(2023, 12), false}, + {createCalendar(2023, 1, 1, 1, 0, 1), YearMonth.of(2023, 1), false} + }); TEST_DB.put(pair(String.class, YearMonth.class), new Object[][]{ {"", null}, {"2024-01", YearMonth.of(2024, 1), true}, @@ -1650,6 +1877,97 @@ private static void loadMonthDayTests() { {MonthDay.of(12, 31), MonthDay.of(12, 31)}, {MonthDay.of(6, 30), MonthDay.of(6, 30)}, }); + TEST_DB.put(pair(Calendar.class, MonthDay.class), new Object[][] { + {createCalendar(1888, 1, 2, 0, 0, 0), MonthDay.of(1, 2), false}, + {createCalendar(1969, 12, 31, 0, 0, 0), MonthDay.of(12, 31), false}, + {createCalendar(1970, 1, 1, 0, 0, 0), MonthDay.of(1, 1), false}, + {createCalendar(2023, 6, 15, 0, 0, 0), MonthDay.of(6, 15), false}, + {createCalendar(2023, 6, 15, 12, 30, 45), MonthDay.of(6, 15), false}, + {createCalendar(2023, 6, 15, 23, 59, 59), MonthDay.of(6, 15), false}, + {createCalendar(2023, 6, 15, 1, 0, 1), MonthDay.of(6, 15), false} + }); + TEST_DB.put(pair(Date.class, MonthDay.class), new Object[][] { + {date("1888-01-02T00:00:00Z"), MonthDay.of(1, 2), false}, + {date("1969-12-31T00:00:00Z"), MonthDay.of(12, 31), false}, + {date("1970-01-01T00:00:00Z"), MonthDay.of(1, 1), false}, + {date("2023-06-15T00:00:00Z"), MonthDay.of(6, 15), false}, + {date("2023-06-15T12:30:45Z"), MonthDay.of(6, 15), false}, + {date("2023-06-14T23:59:59Z"), MonthDay.of(6, 15), false}, + {date("2023-06-15T00:00:01Z"), MonthDay.of(6, 15), false} + }); + TEST_DB.put(pair(java.sql.Date.class, MonthDay.class), new Object[][] { + // Bidirectional tests (true) - dates represent same month/day regardless of timezone + {java.sql.Date.valueOf("1888-01-02"), MonthDay.of(1, 2), false}, + {java.sql.Date.valueOf("1969-12-31"), MonthDay.of(12, 31), false}, + {java.sql.Date.valueOf("1970-01-01"), MonthDay.of(1, 1), false}, + {java.sql.Date.valueOf("2023-06-15"), MonthDay.of(6, 15), false} + }); + TEST_DB.put(pair(LocalDate.class, MonthDay.class), new Object[][] { + {LocalDate.of(1888, 1, 2), MonthDay.of(1, 2), false}, + {LocalDate.of(1969, 12, 31), MonthDay.of(12, 31), false}, + {LocalDate.of(1970, 1, 1), MonthDay.of(1, 1), false}, + {LocalDate.of(2023, 6, 15), MonthDay.of(6, 15), false}, + {LocalDate.of(2022, 6, 15), MonthDay.of(6, 15), false}, + {LocalDate.of(2024, 6, 15), MonthDay.of(6, 15), false} + }); + TEST_DB.put(pair(LocalDateTime.class, MonthDay.class), new Object[][] { + // One-way + {LocalDateTime.of(1888, 1, 2, 0, 0), MonthDay.of(1, 2), false}, + {LocalDateTime.of(1969, 12, 31, 0, 0), MonthDay.of(12, 31), false}, + {LocalDateTime.of(1970, 1, 1, 0, 0), MonthDay.of(1, 1), false}, + {LocalDateTime.of(2023, 6, 15, 0, 0), MonthDay.of(6, 15), false}, + + // One-way tests (false) - various times on same date + {LocalDateTime.of(2023, 6, 15, 12, 30, 45), MonthDay.of(6, 15), false}, + {LocalDateTime.of(2023, 6, 15, 23, 59, 59, 999_999_999), MonthDay.of(6, 15), false}, + {LocalDateTime.of(2023, 6, 15, 0, 0, 0, 1), MonthDay.of(6, 15), false}, + + // One-way tests (false) - same month-day in different years + {LocalDateTime.of(2022, 6, 15, 12, 0), MonthDay.of(6, 15), false}, + {LocalDateTime.of(2024, 6, 15, 12, 0), MonthDay.of(6, 15), false} + }); + TEST_DB.put(pair(OffsetDateTime.class, MonthDay.class), new Object[][] { + {odt("1888-01-01T15:00:00Z"), MonthDay.of(1, 2), false}, // 1888-01-02 00:00 Tokyo + {odt("1969-12-30T15:00:00Z"), MonthDay.of(12, 31), false}, // 1969-12-31 00:00 Tokyo + {odt("1969-12-31T15:00:00Z"), MonthDay.of(1, 1), false}, // 1970-01-01 00:00 Tokyo + {odt("2023-06-14T15:00:00Z"), MonthDay.of(6, 15), false}, // 2023-06-15 00:00 Tokyo + + // One-way tests (false) - various times before Tokyo midnight + {odt("2023-06-15T12:30:45Z"), MonthDay.of(6, 15), false}, // 21:30:45 Tokyo + {odt("2023-06-15T14:59:59.999Z"), MonthDay.of(6, 15), false}, // 23:59:59.999 Tokyo + {odt("2023-06-15T00:00:01Z"), MonthDay.of(6, 15), false}, // 09:00:01 Tokyo + + // One-way tests (false) - same date in different offset + {odt("2023-06-15T00:00:00+09:00"), MonthDay.of(6, 15), false}, // Tokyo local time + {odt("2023-06-15T00:00:00-05:00"), MonthDay.of(6, 15), false} // US Eastern time + }); + TEST_DB.put(pair(ZonedDateTime.class, MonthDay.class), new Object[][] { + {zdt("1888-01-01T15:00:00Z"), MonthDay.of(1, 2), false}, // 1888-01-02 00:00 Tokyo + {zdt("1969-12-30T15:00:00Z"), MonthDay.of(12, 31), false}, // 1969-12-31 00:00 Tokyo + {zdt("1969-12-31T15:00:00Z"), MonthDay.of(1, 1), false}, // 1970-01-01 00:00 Tokyo + {zdt("2023-06-14T15:00:00Z"), MonthDay.of(6, 15), false}, // 2023-06-15 00:00 Tokyo + {zdt("2023-06-15T12:30:45Z"), MonthDay.of(6, 15), false}, // 21:30:45 Tokyo + {zdt("2023-06-15T14:59:59.999Z"), MonthDay.of(6, 15), false}, // 23:59:59.999 Tokyo + {zdt("2023-06-15T00:00:01Z"), MonthDay.of(6, 15), false}, // 09:00:01 Tokyo + + // One-way tests (false) - same time in different zones + {ZonedDateTime.of(2023, 6, 15, 0, 0, 0, 0, ZoneId.of("Asia/Tokyo")), MonthDay.of(6, 15), false}, + {ZonedDateTime.of(2023, 6, 15, 0, 0, 0, 0, ZoneId.of("America/New_York")), MonthDay.of(6, 15), false} + }); + TEST_DB.put(pair(Timestamp.class, MonthDay.class), new Object[][] { + {timestamp("1888-01-01T15:00:00Z"), MonthDay.of(1, 2), false}, // 1888-01-02 00:00 Tokyo + {timestamp("1969-12-30T15:00:00Z"), MonthDay.of(12, 31), false}, // 1969-12-31 00:00 Tokyo + {timestamp("1969-12-31T15:00:00Z"), MonthDay.of(1, 1), false}, // 1970-01-01 00:00 Tokyo + {timestamp("2023-06-14T15:00:00Z"), MonthDay.of(6, 15), false}, // 2023-06-15 00:00 Tokyo + + // One-way tests (false) - various times before Tokyo midnight + {timestamp("2023-06-15T12:30:45.123Z"), MonthDay.of(6, 15), false}, // 21:30:45 Tokyo + {timestamp("2023-06-15T14:59:59.999Z"), MonthDay.of(6, 15), false}, // 23:59:59.999 Tokyo + {timestamp("2023-06-15T00:00:00.001Z"), MonthDay.of(6, 15), false}, // 09:00:00.001 Tokyo + + // One-way tests (false) - with nanosecond precision + {timestamp("2023-06-15T12:00:00.123456789Z"), MonthDay.of(6, 15), false} // 21:00:00.123456789 Tokyo + }); TEST_DB.put(pair(String.class, MonthDay.class), new Object[][]{ {"", null}, {"1-1", MonthDay.of(1, 1)}, @@ -1997,6 +2315,13 @@ private static void loadSqlDateTests() { { Instant.parse("1970-01-01T00:00:00.001Z"), java.sql.Date.valueOf("1970-01-01"), false }, { Instant.parse("1970-01-01T00:00:00.999Z"), java.sql.Date.valueOf("1970-01-01"), false }, }); + TEST_DB.put(pair(java.sql.Date.class, Instant.class), new Object[][] { + // Bidirectional tests (true) - all at midnight Tokyo + {java.sql.Date.valueOf("1888-01-02"), Instant.parse("1888-01-01T15:00:00Z"), true}, // 1888-01-02 00:00 Tokyo + {java.sql.Date.valueOf("1969-12-31"), Instant.parse("1969-12-30T15:00:00Z"), true}, // 1969-12-31 00:00 Tokyo + {java.sql.Date.valueOf("1970-01-01"), Instant.parse("1969-12-31T15:00:00Z"), true}, // 1970-01-01 00:00 Tokyo + {java.sql.Date.valueOf("2023-06-15"), Instant.parse("2023-06-14T15:00:00Z"), true}, // 2023-06-15 00:00 Tokyo + }); TEST_DB.put(pair(ZonedDateTime.class, java.sql.Date.class), new Object[][]{ // When it's midnight in Tokyo (UTC+9), it's 15:00 the previous day in UTC {zdt("1888-01-01T15:00:00+00:00"), java.sql.Date.valueOf("1888-01-02"), true}, @@ -2342,6 +2667,31 @@ private static void loadBigDecimalTests() { {date("1970-01-01T00:00:00Z"), BigDecimal.ZERO, true}, {date("1970-01-01T00:00:00.001Z"), new BigDecimal("0.001"), true}, }); + TEST_DB.put(pair(BigDecimal.class, java.sql.Date.class), new Object[][] { + // Bidirectional tests (true) - all representing midnight Tokyo time + {new BigDecimal("1686754800"), java.sql.Date.valueOf("2023-06-15"), true}, // 2023-06-15 00:00 Tokyo + {new BigDecimal("-32400"), java.sql.Date.valueOf("1970-01-01"), true}, // 1970-01-01 00:00 Tokyo + {new BigDecimal("-118800"), java.sql.Date.valueOf("1969-12-31"), true}, // 1969-12-31 00:00 Tokyo + + // Pre-epoch dates + {new BigDecimal("-86400"), java.sql.Date.valueOf("1969-12-31"), false}, // 1 day before epoch + {new BigDecimal("-172800"), java.sql.Date.valueOf("1969-12-30"), false}, // 2 days before epoch + + // Epoch + {new BigDecimal("0"), java.sql.Date.valueOf("1970-01-01"), false}, // epoch + {new BigDecimal("86400"), java.sql.Date.valueOf("1970-01-02"), false}, // 1 day after epoch + + // Recent dates + {new BigDecimal("1686787200"), java.sql.Date.valueOf("2023-06-15"), false}, + + // Fractional seconds (should truncate to same date) + {new BigDecimal("86400.123"), java.sql.Date.valueOf("1970-01-02"), false}, + {new BigDecimal("86400.999"), java.sql.Date.valueOf("1970-01-02"), false}, + + // Scientific notation + {new BigDecimal("8.64E4"), java.sql.Date.valueOf("1970-01-02"), false}, // 1 day after epoch + {new BigDecimal("1.686787200E9"), java.sql.Date.valueOf("2023-06-15"), false} + }); TEST_DB.put(pair(LocalDateTime.class, BigDecimal.class), new Object[][]{ {zdt("0000-01-01T00:00:00Z").toLocalDateTime(), new BigDecimal("-62167219200.0"), true}, {zdt("0000-01-01T00:00:00.000000001Z").toLocalDateTime(), new BigDecimal("-62167219199.999999999"), true}, @@ -2641,11 +2991,11 @@ private static void loadCharacterTests() { {mapOf("_v", mapOf("_v", 65535)), (char) 65535}, {mapOf("_v", "0"), (char) 48}, {mapOf("_v", 65536), new IllegalArgumentException("Value '65536' out of range to be converted to character")}, - {mapOf(VALUE, (char)0), (char) 0, true}, - {mapOf(VALUE, (char)1), (char) 1, true}, - {mapOf(VALUE, (char)65535), (char) 65535, true}, - {mapOf(VALUE, '0'), (char) 48, true}, - {mapOf(VALUE, '1'), (char) 49, true}, + {mapOf(V, (char)0), (char) 0, true}, + {mapOf(V, (char)1), (char) 1, true}, + {mapOf(V, (char)65535), (char) 65535, true}, + {mapOf(V, '0'), (char) 48, true}, + {mapOf(V, '1'), (char) 49, true}, }); TEST_DB.put(pair(String.class, Character.class), new Object[][]{ {"", (char) 0}, @@ -2762,13 +3112,13 @@ private static void loadBooleanTests() { {BigDecimal.valueOf(2L), true}, }); TEST_DB.put(pair(Map.class, Boolean.class), new Object[][]{ - {mapOf("_v", 16), true}, - {mapOf("_v", 0), false}, - {mapOf("_v", "0"), false}, - {mapOf("_v", "1"), true}, - {mapOf("_v", mapOf("_v", 5.0)), true}, - {mapOf(VALUE, true), true, true}, - {mapOf(VALUE, false), false, true}, + {mapOf(V, 16), true}, + {mapOf(V, 0), false}, + {mapOf(V, "0"), false}, + {mapOf(V, "1"), true}, + {mapOf(V, mapOf(V, 5.0)), true}, + {mapOf(V, true), true, true}, + {mapOf(V, false), false, true}, }); TEST_DB.put(pair(String.class, Boolean.class), new Object[][]{ {"0", false}, @@ -3140,9 +3490,9 @@ private static void loadLongTests() { {new BigDecimal("9223372036854775808"), Long.MIN_VALUE}, // wrap around }); TEST_DB.put(pair(Map.class, Long.class), new Object[][]{ - {mapOf("_v", "-1"), -1L}, - {mapOf("_v", -1L), -1L, true}, - {mapOf("value", "-1"), -1L}, + {mapOf(V, "-1"), -1L}, + {mapOf(V, -1L), -1L, true}, + {mapOf(V, "-1"), -1L}, {mapOf("value", -1L), -1L}, {mapOf("_v", "0"), 0L}, @@ -3766,29 +4116,33 @@ private static void loadByteTest() { {new BigDecimal("128"), Byte.MIN_VALUE}, }); TEST_DB.put(pair(Map.class, Byte.class), new Object[][]{ - {mapOf("_v", "-1"), (byte) -1}, - {mapOf("_v", -1), (byte) -1}, - {mapOf("value", "-1"), (byte) -1}, - {mapOf("value", -1L), (byte) -1}, + {mapOf(V, "-1"), (byte) -1}, + {mapOf(V, -1), (byte) -1}, + {mapOf(VALUE, "-1"), (byte) -1}, + {mapOf(VALUE, -1L), (byte) -1}, + + {mapOf(V, "0"), (byte) 0}, + {mapOf(V, 0), (byte) 0}, - {mapOf("_v", "0"), (byte) 0}, - {mapOf("_v", 0), (byte) 0}, + {mapOf(V, "1"), (byte) 1}, + {mapOf(V, 1), (byte) 1}, - {mapOf("_v", "1"), (byte) 1}, - {mapOf("_v", 1), (byte) 1}, + {mapOf(V, "-128"), Byte.MIN_VALUE}, + {mapOf(V, -128), Byte.MIN_VALUE}, - {mapOf("_v", "-128"), Byte.MIN_VALUE}, - {mapOf("_v", -128), Byte.MIN_VALUE}, + {mapOf(V, "127"), Byte.MAX_VALUE}, + {mapOf(V, 127), Byte.MAX_VALUE}, - {mapOf("_v", "127"), Byte.MAX_VALUE}, - {mapOf("_v", 127), Byte.MAX_VALUE}, + {mapOf(V, "-129"), new IllegalArgumentException("'-129' not parseable as a byte value or outside -128 to 127")}, + {mapOf(V, -129), Byte.MAX_VALUE}, - {mapOf("_v", "-129"), new IllegalArgumentException("'-129' not parseable as a byte value or outside -128 to 127")}, - {mapOf("_v", -129), Byte.MAX_VALUE}, + {mapOf(V, "128"), new IllegalArgumentException("'128' not parseable as a byte value or outside -128 to 127")}, + {mapOf(V, 128), Byte.MIN_VALUE}, + {mapOf(V, mapOf(V, 128L)), Byte.MIN_VALUE}, // Prove use of recursive call to .convert() + {mapOf(V, (byte)1), (byte)1, true}, + {mapOf(V, (byte)2), (byte)2, true}, + {mapOf(VALUE, "nope"), new IllegalArgumentException("Value 'nope' not parseable as a byte value or outside -128 to 127")}, - {mapOf("_v", "128"), new IllegalArgumentException("'128' not parseable as a byte value or outside -128 to 127")}, - {mapOf("_v", 128), Byte.MIN_VALUE}, - {mapOf("_v", mapOf("_v", 128L)), Byte.MIN_VALUE}, // Prove use of recursive call to .convert() }); TEST_DB.put(pair(Year.class, Byte.class), new Object[][]{ {Year.of(2024), new IllegalArgumentException("Unsupported conversion, source type [Year (2024)] target type 'Byte'") }, @@ -3813,11 +4167,6 @@ private static void loadByteTest() { {"-129", new IllegalArgumentException("'-129' not parseable as a byte value or outside -128 to 127")}, {"128", new IllegalArgumentException("'128' not parseable as a byte value or outside -128 to 127")}, }); - TEST_DB.put(pair(Map.class, Byte.class), new Object[][]{ - {mapOf(VALUE, (byte)1), (byte)1, true}, - {mapOf(VALUE, (byte)2), (byte)2, true}, - {mapOf(VALUE, "nope"), new IllegalArgumentException("Value 'nope' not parseable as a byte value or outside -128 to 127")}, - }); } /** diff --git a/src/test/java/com/cedarsoftware/util/convert/DateConversionsTest.java b/src/test/java/com/cedarsoftware/util/convert/DateConversionsTest.java deleted file mode 100644 index 931fb72e..00000000 --- a/src/test/java/com/cedarsoftware/util/convert/DateConversionsTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.cedarsoftware.util.convert; - -import java.time.LocalDate; -import java.time.MonthDay; -import java.time.Year; -import java.time.YearMonth; -import java.time.ZoneId; -import java.util.Calendar; -import java.util.Date; -import java.util.TimeZone; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * @author John DeRegnaucourt (jdereg@gmail.com) - *
- * Copyright (c) Cedar Software LLC - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * License - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -class DateConversionsTest { - private final Converter converter = new Converter(new DefaultConverterOptions()); - - private Date createDate(String dateStr, int hour, int minute, int second) { - return Date.from(LocalDate.parse(dateStr) - .atTime(hour, minute, second) - .atZone(ZoneId.systemDefault()) - .toInstant()); - } - - @Test - void testDateToYearMonth() { - assertEquals(YearMonth.of(1888, 1), converter.convert(createDate("1888-01-02", 12, 30, 45), YearMonth.class)); - assertEquals(YearMonth.of(1969, 12), converter.convert(createDate("1969-12-31", 23, 59, 59), YearMonth.class)); - assertEquals(YearMonth.of(1970, 1), converter.convert(createDate("1970-01-01", 0, 0, 1), YearMonth.class)); - assertEquals(YearMonth.of(2023, 6), converter.convert(createDate("2023-06-15", 15, 30, 0), YearMonth.class)); - } - - @Test - void testDateToYear() { - assertEquals(Year.of(1888), converter.convert(createDate("1888-01-02", 9, 15, 30), Year.class)); - assertEquals(Year.of(1969), converter.convert(createDate("1969-12-31", 18, 45, 15), Year.class)); - assertEquals(Year.of(1970), converter.convert(createDate("1970-01-01", 6, 20, 10), Year.class)); - assertEquals(Year.of(2023), converter.convert(createDate("2023-06-15", 21, 5, 55), Year.class)); - } - - @Test - void testDateToMonthDay() { - assertEquals(MonthDay.of(1, 2), converter.convert(createDate("1888-01-02", 3, 45, 20), MonthDay.class)); - assertEquals(MonthDay.of(12, 31), converter.convert(createDate("1969-12-31", 14, 25, 35), MonthDay.class)); - assertEquals(MonthDay.of(1, 1), converter.convert(createDate("1970-01-01", 8, 50, 40), MonthDay.class)); - assertEquals(MonthDay.of(6, 15), converter.convert(createDate("2023-06-15", 17, 10, 5), MonthDay.class)); - } - - @Test - void testDateToCalendarTimeZone() { - Date date = new Date(); - TimeZone timeZone = TimeZone.getTimeZone("America/New_York"); - Calendar cal = Calendar.getInstance(timeZone); - cal.setTime(date); - } -} \ No newline at end of file diff --git a/src/test/java/com/cedarsoftware/util/convert/LocalDateConversionsTest.java b/src/test/java/com/cedarsoftware/util/convert/LocalDateConversionsTest.java deleted file mode 100644 index 74414234..00000000 --- a/src/test/java/com/cedarsoftware/util/convert/LocalDateConversionsTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.cedarsoftware.util.convert; - -import java.time.LocalDate; -import java.time.MonthDay; -import java.time.Year; -import java.time.YearMonth; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * @author John DeRegnaucourt (jdereg@gmail.com) - *
- * Copyright (c) Cedar Software LLC - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * License - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -class LocalDateConversionsTest { - private final Converter converter = new Converter(new DefaultConverterOptions()); - - @Test - void testLocalDateToYearMonth() { - assertEquals(YearMonth.of(1888, 1), - converter.convert(LocalDate.of(1888, 1, 2), YearMonth.class)); - assertEquals(YearMonth.of(1969, 12), - converter.convert(LocalDate.of(1969, 12, 31), YearMonth.class)); - assertEquals(YearMonth.of(1970, 1), - converter.convert(LocalDate.of(1970, 1, 1), YearMonth.class)); - assertEquals(YearMonth.of(2023, 6), - converter.convert(LocalDate.of(2023, 6, 15), YearMonth.class)); - } - - @Test - void testLocalDateToYear() { - assertEquals(Year.of(1888), - converter.convert(LocalDate.of(1888, 1, 2), Year.class)); - assertEquals(Year.of(1969), - converter.convert(LocalDate.of(1969, 12, 31), Year.class)); - assertEquals(Year.of(1970), - converter.convert(LocalDate.of(1970, 1, 1), Year.class)); - assertEquals(Year.of(2023), - converter.convert(LocalDate.of(2023, 6, 15), Year.class)); - } - - @Test - void testLocalDateToMonthDay() { - assertEquals(MonthDay.of(1, 2), - converter.convert(LocalDate.of(1888, 1, 2), MonthDay.class)); - assertEquals(MonthDay.of(12, 31), - converter.convert(LocalDate.of(1969, 12, 31), MonthDay.class)); - assertEquals(MonthDay.of(1, 1), - converter.convert(LocalDate.of(1970, 1, 1), MonthDay.class)); - assertEquals(MonthDay.of(6, 15), - converter.convert(LocalDate.of(2023, 6, 15), MonthDay.class)); - } -} \ No newline at end of file diff --git a/src/test/java/com/cedarsoftware/util/convert/LocalDateTimeConversionsTest.java b/src/test/java/com/cedarsoftware/util/convert/LocalDateTimeConversionsTest.java deleted file mode 100644 index 3f7bcdf3..00000000 --- a/src/test/java/com/cedarsoftware/util/convert/LocalDateTimeConversionsTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.cedarsoftware.util.convert; - -import java.time.LocalDateTime; -import java.time.MonthDay; -import java.time.Year; -import java.time.YearMonth; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * @author John DeRegnaucourt (jdereg@gmail.com) - *
- * Copyright (c) Cedar Software LLC - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * License - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -class LocalDateTimeConversionsTest { - private final Converter converter = new Converter(new DefaultConverterOptions()); - - @Test - void testLocalDateTimeToYearMonth() { - assertEquals(YearMonth.of(1888, 1), - converter.convert(LocalDateTime.of(1888, 1, 2, 12, 30, 45, 123_456_789), YearMonth.class)); - assertEquals(YearMonth.of(1969, 12), - converter.convert(LocalDateTime.of(1969, 12, 31, 23, 59, 59, 999_999_999), YearMonth.class)); - assertEquals(YearMonth.of(1970, 1), - converter.convert(LocalDateTime.of(1970, 1, 1, 0, 0, 1, 1), YearMonth.class)); - assertEquals(YearMonth.of(2023, 6), - converter.convert(LocalDateTime.of(2023, 6, 15, 15, 30, 0, 500_000_000), YearMonth.class)); - } - - @Test - void testLocalDateTimeToYear() { - assertEquals(Year.of(1888), - converter.convert(LocalDateTime.of(1888, 1, 2, 9, 15, 30, 333_333_333), Year.class)); - assertEquals(Year.of(1969), - converter.convert(LocalDateTime.of(1969, 12, 31, 18, 45, 15, 777_777_777), Year.class)); - assertEquals(Year.of(1970), - converter.convert(LocalDateTime.of(1970, 1, 1, 6, 20, 10, 111_111_111), Year.class)); - assertEquals(Year.of(2023), - converter.convert(LocalDateTime.of(2023, 6, 15, 21, 5, 55, 888_888_888), Year.class)); - } - - @Test - void testLocalDateTimeToMonthDay() { - assertEquals(MonthDay.of(1, 2), - converter.convert(LocalDateTime.of(1888, 1, 2, 3, 45, 20, 222_222_222), MonthDay.class)); - assertEquals(MonthDay.of(12, 31), - converter.convert(LocalDateTime.of(1969, 12, 31, 14, 25, 35, 444_444_444), MonthDay.class)); - assertEquals(MonthDay.of(1, 1), - converter.convert(LocalDateTime.of(1970, 1, 1, 8, 50, 40, 666_666_666), MonthDay.class)); - assertEquals(MonthDay.of(6, 15), - converter.convert(LocalDateTime.of(2023, 6, 15, 17, 10, 5, 999_999_999), MonthDay.class)); - } -} \ No newline at end of file diff --git a/src/test/java/com/cedarsoftware/util/convert/OffsetDateTimeConversionsTest.java b/src/test/java/com/cedarsoftware/util/convert/OffsetDateTimeConversionsTest.java deleted file mode 100644 index 5768745f..00000000 --- a/src/test/java/com/cedarsoftware/util/convert/OffsetDateTimeConversionsTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.cedarsoftware.util.convert; - -import java.time.MonthDay; -import java.time.OffsetDateTime; -import java.time.Year; -import java.time.YearMonth; -import java.time.ZoneOffset; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * @author John DeRegnaucourt (jdereg@gmail.com) - *
- * Copyright (c) Cedar Software LLC - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * License - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -class OffsetDateTimeConversionsTest { - private final Converter converter = new Converter(new DefaultConverterOptions()); - - // Some interesting offsets to test with - private static final ZoneOffset TOKYO = ZoneOffset.ofHours(9); // UTC+9 - private static final ZoneOffset PARIS = ZoneOffset.ofHours(1); // UTC+1 - private static final ZoneOffset NY = ZoneOffset.ofHours(-5); // UTC-5 - private static final ZoneOffset NEPAL = ZoneOffset.ofHoursMinutes(5, 45); // UTC+5:45 - - @Test - void testOffsetDateTimeToYearMonth() { - assertEquals(YearMonth.of(1888, 1), - converter.convert(OffsetDateTime.of(1888, 1, 2, 12, 30, 45, 123_456_789, TOKYO), YearMonth.class)); - assertEquals(YearMonth.of(1969, 12), - converter.convert(OffsetDateTime.of(1969, 12, 31, 23, 59, 59, 999_999_999, PARIS), YearMonth.class)); - assertEquals(YearMonth.of(1970, 1), - converter.convert(OffsetDateTime.of(1970, 1, 1, 0, 0, 1, 1, NY), YearMonth.class)); - assertEquals(YearMonth.of(2023, 6), - converter.convert(OffsetDateTime.of(2023, 6, 15, 15, 30, 0, 500_000_000, NEPAL), YearMonth.class)); - } - - @Test - void testOffsetDateTimeToYear() { - assertEquals(Year.of(1888), - converter.convert(OffsetDateTime.of(1888, 1, 2, 9, 15, 30, 333_333_333, PARIS), Year.class)); - assertEquals(Year.of(1969), - converter.convert(OffsetDateTime.of(1969, 12, 31, 18, 45, 15, 777_777_777, NY), Year.class)); - assertEquals(Year.of(1969), - converter.convert(OffsetDateTime.of(1970, 1, 1, 6, 20, 10, 111_111_111, TOKYO), Year.class)); - assertEquals(Year.of(2023), - converter.convert(OffsetDateTime.of(2023, 6, 15, 21, 5, 55, 888_888_888, NEPAL), Year.class)); - } - - @Test - void testOffsetDateTimeToMonthDay() { - assertEquals(MonthDay.of(1, 2), - converter.convert(OffsetDateTime.of(1888, 1, 2, 3, 45, 20, 222_222_222, NY), MonthDay.class)); - assertEquals(MonthDay.of(12, 31), - converter.convert(OffsetDateTime.of(1969, 12, 31, 14, 25, 35, 444_444_444, TOKYO), MonthDay.class)); - assertEquals(MonthDay.of(1, 1), - converter.convert(OffsetDateTime.of(1970, 1, 1, 8, 50, 40, 666_666_666, PARIS), MonthDay.class)); - assertEquals(MonthDay.of(6, 15), - converter.convert(OffsetDateTime.of(2023, 6, 15, 17, 10, 5, 999_999_999, NEPAL), MonthDay.class)); - } -} \ No newline at end of file diff --git a/src/test/java/com/cedarsoftware/util/convert/SqlDateConversionTest.java b/src/test/java/com/cedarsoftware/util/convert/SqlDateConversionTest.java deleted file mode 100644 index 65c67b3e..00000000 --- a/src/test/java/com/cedarsoftware/util/convert/SqlDateConversionTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.cedarsoftware.util.convert; - -import java.time.MonthDay; -import java.time.Year; -import java.time.YearMonth; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * @author John DeRegnaucourt (jdereg@gmail.com) - *
- * Copyright (c) Cedar Software LLC - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * License - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -class SqlDateConversionTest { - private final Converter converter = new Converter(new DefaultConverterOptions()); - - @Test - void testSqlDateToYearMonth() { - assertEquals(converter.convert(java.sql.Date.valueOf("1888-01-02"), YearMonth.class), YearMonth.of(1888, 1)); - assertEquals(converter.convert(java.sql.Date.valueOf("1969-12-31"), YearMonth.class), YearMonth.of(1969, 12)); - assertEquals(converter.convert(java.sql.Date.valueOf("1970-01-01"), YearMonth.class), YearMonth.of(1970, 1)); - assertEquals(converter.convert(java.sql.Date.valueOf("2023-06-15"), YearMonth.class), YearMonth.of(2023, 6)); - } - - @Test - void testSqlDateToYear() { - assertEquals(converter.convert(java.sql.Date.valueOf("1888-01-02"), Year.class), Year.of(1888)); - assertEquals(converter.convert(java.sql.Date.valueOf("1969-12-31"), Year.class), Year.of(1969)); - assertEquals(converter.convert(java.sql.Date.valueOf("1970-01-01"), Year.class), Year.of(1970)); - assertEquals(converter.convert(java.sql.Date.valueOf("2023-06-15"), Year.class), Year.of(2023)); - } - - @Test - void testSqlDateToMonthDay() { - assertEquals(converter.convert(java.sql.Date.valueOf("1888-01-02"), MonthDay.class), MonthDay.of(1, 2)); - assertEquals(converter.convert(java.sql.Date.valueOf("1969-12-31"), MonthDay.class), MonthDay.of(12, 31)); - assertEquals(converter.convert(java.sql.Date.valueOf("1970-01-01"), MonthDay.class), MonthDay.of(1, 1)); - assertEquals(converter.convert(java.sql.Date.valueOf("2023-06-15"), MonthDay.class), MonthDay.of(6, 15)); - } -} \ No newline at end of file diff --git a/src/test/java/com/cedarsoftware/util/convert/ZonedDateTimeConversionsTest.java b/src/test/java/com/cedarsoftware/util/convert/ZonedDateTimeConversionsTest.java deleted file mode 100644 index 06fe78d2..00000000 --- a/src/test/java/com/cedarsoftware/util/convert/ZonedDateTimeConversionsTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.cedarsoftware.util.convert; - -import java.time.MonthDay; -import java.time.Year; -import java.time.YearMonth; -import java.time.ZoneId; -import java.time.ZonedDateTime; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * @author John DeRegnaucourt (jdereg@gmail.com) - *
- * Copyright (c) Cedar Software LLC - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * License - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -class ZonedDateTimeConversionsTest { - private final Converter converter = new Converter(new DefaultConverterOptions()); - - // Some interesting timezones to test with - private static final ZoneId TOKYO = ZoneId.of("Asia/Tokyo"); // UTC+9 - private static final ZoneId PARIS = ZoneId.of("Europe/Paris"); // UTC+1/+2 - private static final ZoneId NEW_YORK = ZoneId.of("America/New_York"); // UTC-5/-4 - - @Test - void testZonedDateTimeToYearMonth() { - assertEquals(YearMonth.of(1888, 1), - converter.convert(ZonedDateTime.of(1888, 1, 2, 12, 30, 45, 123_456_789, TOKYO), YearMonth.class)); - assertEquals(YearMonth.of(1969, 12), - converter.convert(ZonedDateTime.of(1969, 12, 31, 23, 59, 59, 999_999_999, PARIS), YearMonth.class)); - assertEquals(YearMonth.of(1970, 1), - converter.convert(ZonedDateTime.of(1970, 1, 1, 0, 0, 1, 1, NEW_YORK), YearMonth.class)); - assertEquals(YearMonth.of(2023, 6), - converter.convert(ZonedDateTime.of(2023, 6, 15, 15, 30, 0, 500_000_000, TOKYO), YearMonth.class)); - } - - @Test - void testZonedDateTimeToYear() { - assertEquals(Year.of(1888), - converter.convert(ZonedDateTime.of(1888, 1, 2, 9, 15, 30, 333_333_333, PARIS), Year.class)); - assertEquals(Year.of(1969), - converter.convert(ZonedDateTime.of(1969, 12, 31, 18, 45, 15, 777_777_777, NEW_YORK), Year.class)); - assertEquals(Year.of(1969), // was 1970 - converter.convert(ZonedDateTime.of(1970, 1, 1, 6, 20, 10, 111_111_111, TOKYO), Year.class)); - assertEquals(Year.of(2023), - converter.convert(ZonedDateTime.of(2023, 6, 15, 21, 5, 55, 888_888_888, PARIS), Year.class)); - } - - @Test - void testZonedDateTimeToMonthDay() { - assertEquals(MonthDay.of(1, 2), - converter.convert(ZonedDateTime.of(1888, 1, 2, 3, 45, 20, 222_222_222, NEW_YORK), MonthDay.class)); - assertEquals(MonthDay.of(12, 31), - converter.convert(ZonedDateTime.of(1969, 12, 31, 14, 25, 35, 444_444_444, TOKYO), MonthDay.class)); - assertEquals(MonthDay.of(1, 1), - converter.convert(ZonedDateTime.of(1970, 1, 1, 8, 50, 40, 666_666_666, PARIS), MonthDay.class)); - assertEquals(MonthDay.of(6, 15), - converter.convert(ZonedDateTime.of(2023, 6, 15, 17, 10, 5, 999_999_999, NEW_YORK), MonthDay.class)); - } -} \ No newline at end of file