Skip to content

Commit

Permalink
Many more tests written.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdereg committed Mar 12, 2024
1 parent f92dc4e commit 80c78a4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 26 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(AtomicBoolean.class, AtomicInteger.class), AtomicBooleanConversions::toAtomicInteger);
CONVERSION_DB.put(pair(AtomicLong.class, AtomicInteger.class), NumberConversions::toAtomicInteger);
CONVERSION_DB.put(pair(LocalTime.class, AtomicInteger.class), LocalTimeConversions::toAtomicInteger);
CONVERSION_DB.put(pair(LocalDate.class, AtomicInteger.class), LocalDateConversions::toAtomicLong);
CONVERSION_DB.put(pair(Number.class, AtomicBoolean.class), NumberConversions::toAtomicInteger);
CONVERSION_DB.put(pair(Number.class, AtomicInteger.class), NumberConversions::toAtomicInteger);
CONVERSION_DB.put(pair(Map.class, AtomicInteger.class), MapConversions::toAtomicInteger);
CONVERSION_DB.put(pair(String.class, AtomicInteger.class), StringConversions::toAtomicInteger);
CONVERSION_DB.put(pair(Year.class, AtomicInteger.class), YearConversions::toAtomicInteger);
Expand Down Expand Up @@ -538,7 +537,7 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Timestamp.class, LocalDate.class), DateConversions::toLocalDate);
CONVERSION_DB.put(pair(Date.class, LocalDate.class), DateConversions::toLocalDate);
CONVERSION_DB.put(pair(Instant.class, LocalDate.class), InstantConversions::toLocalDate);
CONVERSION_DB.put(pair(LocalDate.class, LocalDate.class), LocalDateConversions::toLocalDate);
CONVERSION_DB.put(pair(LocalDate.class, LocalDate.class), Converter::identity);
CONVERSION_DB.put(pair(LocalDateTime.class, LocalDate.class), LocalDateTimeConversions::toLocalDate);
CONVERSION_DB.put(pair(ZonedDateTime.class, LocalDate.class), ZonedDateTimeConversions::toLocalDate);
CONVERSION_DB.put(pair(OffsetDateTime.class, LocalDate.class), OffsetDateTimeConversions::toLocalDate);
Expand Down Expand Up @@ -587,7 +586,6 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Date.class, LocalTime.class), DateConversions::toLocalTime);
CONVERSION_DB.put(pair(Instant.class, LocalTime.class), InstantConversions::toLocalTime);
CONVERSION_DB.put(pair(LocalDateTime.class, LocalTime.class), LocalDateTimeConversions::toLocalTime);
CONVERSION_DB.put(pair(LocalDate.class, LocalTime.class), LocalDateConversions::toLocalTime);
CONVERSION_DB.put(pair(LocalTime.class, LocalTime.class), Converter::identity);
CONVERSION_DB.put(pair(ZonedDateTime.class, LocalTime.class), ZonedDateTimeConversions::toLocalTime);
CONVERSION_DB.put(pair(OffsetDateTime.class, LocalTime.class), OffsetDateTimeConversions::toLocalTime);
Expand Down Expand Up @@ -893,9 +891,9 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(AtomicInteger.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(AtomicLong.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(Date.class, Map.class), DateConversions::toMap);
CONVERSION_DB.put(pair(java.sql.Date.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(java.sql.Date.class, Map.class), DateConversions::toMap);
CONVERSION_DB.put(pair(Timestamp.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(LocalDate.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(LocalDate.class, Map.class), LocalDateConversions::toMap);
CONVERSION_DB.put(pair(LocalDateTime.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(ZonedDateTime.class, Map.class), MapConversions::initMap);
CONVERSION_DB.put(pair(Duration.class, Map.class), DurationConversions::toMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import com.cedarsoftware.util.CompactLinkedMap;

import static com.cedarsoftware.util.convert.Converter.VALUE;

/**
* @author Kenny Partlow ([email protected])
* <br>
Expand Down Expand Up @@ -47,14 +51,6 @@ static LocalDateTime toLocalDateTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDateTime();
}

static LocalDate toLocalDate(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalDate();
}

static LocalTime toLocalTime(Object from, Converter converter) {
return toZonedDateTime(from, converter).toLocalTime();
}

static ZonedDateTime toZonedDateTime(Object from, Converter converter) {
ZoneId zoneId = converter.getOptions().getZoneId();
return ((LocalDate) from).atStartOfDay(zoneId);
Expand All @@ -69,7 +65,8 @@ static AtomicLong toAtomicLong(Object from, Converter converter) {
}

static Timestamp toTimestamp(Object from, Converter converter) {
return new Timestamp(toLong(from, converter));
LocalDate localDate = (LocalDate) from;
return new Timestamp(localDate.toEpochDay() * 86400 * 1000);
}

static Calendar toCalendar(Object from, Converter converter) {
Expand Down Expand Up @@ -101,4 +98,11 @@ static String toString(Object from, Converter converter) {
LocalDate localDate = (LocalDate) from;
return localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
}

static Map<String, Object> toMap(Object from, Converter converter) {
LocalDate localDate = (LocalDate) from;
Map<String, Object> target = new CompactLinkedMap<>();
target.put(VALUE, localDate.toString());
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ static AtomicLong toAtomicLong(Object from, Converter converter) {
}

static Timestamp toTimestamp(Object from, Converter converter) {
return new Timestamp(toLong(from, converter));
ZonedDateTime zdt = (ZonedDateTime) from;
return Timestamp.from(zdt.toInstant());
}

static Calendar toCalendar(Object from, Converter converter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ private static void loadMapTests() {
{ new Date(now), mapOf(VALUE, now), true},
{ new Date(1L), mapOf(VALUE, 1L), true},
});
TEST_DB.put(pair(LocalDate.class, Map.class), new Object[][] {
{LocalDate.parse("1969-12-31"), mapOf(VALUE, "1969-12-31"), true},
{LocalDate.parse("1970-01-01"), mapOf(VALUE, "1970-01-01"), true},
{LocalDate.parse("1970-01-02"), mapOf(VALUE, "1970-01-02"), true},
});
TEST_DB.put(pair(java.sql.Date.class, Map.class), new Object[][] {
{ new java.sql.Date(-1L), mapOf(VALUE, -1L), true},
{ new java.sql.Date(0L), mapOf(VALUE, 0L), true},
{ new java.sql.Date(now), mapOf(VALUE, now), true},
{ new java.sql.Date(1L), mapOf(VALUE, 1L), true},
});
TEST_DB.put(pair(Duration.class, Map.class), new Object[][] {
{ Duration.ofMillis(-1), mapOf("seconds", -1L, "nanos", 999000000)},
});
Expand Down Expand Up @@ -747,6 +758,12 @@ private static void loadLocalDateTimeTests() {
{Instant.parse("1970-01-01T00:00:00Z"), ZonedDateTime.parse("1970-01-01T00:00:00Z").withZoneSameInstant(TOKYO_Z).toLocalDateTime(), true},
{Instant.parse("1970-01-01T00:00:00.000000001Z"), ZonedDateTime.parse("1970-01-01T00:00:00.000000001Z").withZoneSameInstant(TOKYO_Z).toLocalDateTime(), true},
});
TEST_DB.put(pair(LocalDate.class, LocalDateTime.class), new Object[][] {
{LocalDate.parse("0000-01-01"), ZonedDateTime.parse("0000-01-01T00:00:00Z").toLocalDateTime(), true},
{LocalDate.parse("1969-12-31"), ZonedDateTime.parse("1969-12-31T00:00:00Z").toLocalDateTime(), true},
{LocalDate.parse("1970-01-01"), ZonedDateTime.parse("1970-01-01T00:00:00Z").toLocalDateTime(), true},
{LocalDate.parse("1970-01-02"), ZonedDateTime.parse("1970-01-02T00:00:00Z").toLocalDateTime(), true},
});
}

/**
Expand Down Expand Up @@ -830,6 +847,14 @@ private static void loadLocalTimeTests() {
{ new Date(86399999L), LocalTime.parse("08:59:59.999")},
{ new Date(86400000L), LocalTime.parse("09:00:00")},
});
TEST_DB.put(pair(java.sql.Date.class, LocalTime.class), new Object[][]{
{ new java.sql.Date(-1L), LocalTime.parse("08:59:59.999")},
{ new java.sql.Date(0L), LocalTime.parse("09:00:00")},
{ new java.sql.Date(1L), LocalTime.parse("09:00:00.001")},
{ new java.sql.Date(1001L), LocalTime.parse("09:00:01.001")},
{ new java.sql.Date(86399999L), LocalTime.parse("08:59:59.999")},
{ new java.sql.Date(86400000L), LocalTime.parse("09:00:00")},
});
TEST_DB.put(pair(Instant.class, LocalTime.class), new Object[][]{ // no reverse option (Time local to Tokyo)
{ Instant.parse("1969-12-31T23:59:59.999999999Z"), LocalTime.parse("08:59:59.999999999")},
{ Instant.parse("1970-01-01T00:00:00Z"), LocalTime.parse("09:00:00")},
Expand Down Expand Up @@ -938,6 +963,13 @@ private static void loadTimestampTests() {
return cal;
}, new Timestamp(now), true},
});
TEST_DB.put(pair(LocalDate.class, Timestamp.class), new Object[][] {
{LocalDate.parse("0000-01-01"), Timestamp.from(Instant.parse("0000-01-01T00:00:00Z")), true },
{LocalDate.parse("0000-01-02"), Timestamp.from(Instant.parse("0000-01-02T00:00:00Z")), true },
{LocalDate.parse("1969-12-31"), Timestamp.from(Instant.parse("1969-12-31T00:00:00Z")), true },
{LocalDate.parse("1970-01-01"), Timestamp.from(Instant.parse("1970-01-01T00:00:00Z")), true },
{LocalDate.parse("1970-01-02"), Timestamp.from(Instant.parse("1970-01-02T00:00:00Z")), true },
});
TEST_DB.put(pair(Duration.class, Timestamp.class), new Object[][]{
{Duration.ofSeconds(-62167219200L), Timestamp.from(Instant.parse("0000-01-01T00:00:00Z")), true},
{Duration.ofSeconds(-62167219200L, 1), Timestamp.from(Instant.parse("0000-01-01T00:00:00.000000001Z")), true},
Expand Down Expand Up @@ -1266,6 +1298,19 @@ private static void loadSqlDateTests() {
{new Date(1), new java.sql.Date(1), true },
{new Date(Long.MAX_VALUE), new java.sql.Date(Long.MAX_VALUE), true },
});
TEST_DB.put(pair(Timestamp.class, java.sql.Date.class), new Object[][]{
{new Timestamp(Long.MIN_VALUE), new java.sql.Date(Long.MIN_VALUE), true},
{new Timestamp(Integer.MIN_VALUE), new java.sql.Date(Integer.MIN_VALUE), true},
{new Timestamp(now), new java.sql.Date(now), true},
{new Timestamp(-1), new java.sql.Date(-1), true},
{new Timestamp(0), new java.sql.Date(0), true},
{new Timestamp(1), new java.sql.Date(1), true},
{new Timestamp(Integer.MAX_VALUE), new java.sql.Date(Integer.MAX_VALUE), true},
{new Timestamp(Long.MAX_VALUE), new java.sql.Date(Long.MAX_VALUE), true},
{Timestamp.from(Instant.parse("1969-12-31T23:59:59.999Z")), new java.sql.Date(-1), true},
{Timestamp.from(Instant.parse("1970-01-01T00:00:00.000Z")), new java.sql.Date(0), true},
{Timestamp.from(Instant.parse("1970-01-01T00:00:00.001Z")), new java.sql.Date(1), true},
});
TEST_DB.put(pair(LocalDate.class, java.sql.Date.class), new Object[][] {
{ZonedDateTime.parse("0000-01-01T00:00:00Z").withZoneSameInstant(TOKYO_Z).toLocalDate(), new java.sql.Date(-62167252739000L), true},
{ZonedDateTime.parse("0000-01-01T00:00:00.001Z").withZoneSameInstant(TOKYO_Z).toLocalDate(), new java.sql.Date(-62167252739000L), true},
Expand Down Expand Up @@ -1298,6 +1343,15 @@ private static void loadSqlDateTests() {
{Instant.parse("1970-01-01T00:00:00.001Z"), new java.sql.Date(1L), true},
{Instant.parse("1970-01-01T00:00:00.999Z"), new java.sql.Date(999L), true},
});
TEST_DB.put(pair(ZonedDateTime.class, java.sql.Date.class), new Object[][]{
{ZonedDateTime.parse("0000-01-01T00:00:00Z").withZoneSameInstant(TOKYO_Z), new java.sql.Date(-62167219200000L), true},
{ZonedDateTime.parse("0000-01-01T00:00:00.001Z").withZoneSameInstant(TOKYO_Z), new java.sql.Date(-62167219199999L), true},
{ZonedDateTime.parse("1969-12-31T23:59:59Z").withZoneSameInstant(TOKYO_Z), new java.sql.Date(-1000), true},
{ZonedDateTime.parse("1969-12-31T23:59:59.999Z").withZoneSameInstant(TOKYO_Z), new java.sql.Date(-1), true},
{ZonedDateTime.parse("1970-01-01T00:00:00Z").withZoneSameInstant(TOKYO_Z), new java.sql.Date(0), true},
{ZonedDateTime.parse("1970-01-01T00:00:00.001Z").withZoneSameInstant(TOKYO_Z), new java.sql.Date(1), true},
{ZonedDateTime.parse("1970-01-01T00:00:00.999Z").withZoneSameInstant(TOKYO_Z), new java.sql.Date(999), true},
});
}

/**
Expand Down Expand Up @@ -1357,6 +1411,15 @@ private static void loadDateTests() {
{ZonedDateTime.parse("1970-01-01T00:00:00.001Z").withZoneSameInstant(TOKYO_Z).toLocalDateTime(), new Date(1L), true},
{ZonedDateTime.parse("1970-01-01T00:00:00.999Z").withZoneSameInstant(TOKYO_Z).toLocalDateTime(), new Date(999L), true},
});
TEST_DB.put(pair(ZonedDateTime.class, Date.class), new Object[][]{
{ZonedDateTime.parse("0000-01-01T00:00:00Z").withZoneSameInstant(TOKYO_Z), new Date(-62167219200000L), true},
{ZonedDateTime.parse("0000-01-01T00:00:00.001Z").withZoneSameInstant(TOKYO_Z), new Date(-62167219199999L), true},
{ZonedDateTime.parse("1969-12-31T23:59:59Z").withZoneSameInstant(TOKYO_Z), new Date(-1000), true},
{ZonedDateTime.parse("1969-12-31T23:59:59.999Z").withZoneSameInstant(TOKYO_Z), new Date(-1), true},
{ZonedDateTime.parse("1970-01-01T00:00:00Z").withZoneSameInstant(TOKYO_Z), new Date(0), true},
{ZonedDateTime.parse("1970-01-01T00:00:00.001Z").withZoneSameInstant(TOKYO_Z), new Date(1), true},
{ZonedDateTime.parse("1970-01-01T00:00:00.999Z").withZoneSameInstant(TOKYO_Z), new Date(999), true},
});
}

/**
Expand Down Expand Up @@ -2550,15 +2613,6 @@ private static void loadLongTests() {
{ZonedDateTime.parse("1970-01-01T00:00:00.001Z").withZoneSameInstant(TOKYO_Z), 1L, true},
{ZonedDateTime.parse("1970-01-01T00:00:00.999Z").withZoneSameInstant(TOKYO_Z), 999L, true},
});
TEST_DB.put(pair(ZonedDateTime.class, Date.class), new Object[][]{
{ZonedDateTime.parse("0000-01-01T00:00:00Z").withZoneSameInstant(TOKYO_Z), new Date(-62167219200000L), true},
{ZonedDateTime.parse("0000-01-01T00:00:00.001Z").withZoneSameInstant(TOKYO_Z), new Date(-62167219199999L), true},
{ZonedDateTime.parse("1969-12-31T23:59:59Z").withZoneSameInstant(TOKYO_Z), new Date(-1000), true},
{ZonedDateTime.parse("1969-12-31T23:59:59.999Z").withZoneSameInstant(TOKYO_Z), new Date(-1), true},
{ZonedDateTime.parse("1970-01-01T00:00:00Z").withZoneSameInstant(TOKYO_Z), new Date(0), true},
{ZonedDateTime.parse("1970-01-01T00:00:00.001Z").withZoneSameInstant(TOKYO_Z), new Date(1), true},
{ZonedDateTime.parse("1970-01-01T00:00:00.999Z").withZoneSameInstant(TOKYO_Z), new Date(999), true},
});
TEST_DB.put(pair(Calendar.class, Long.class), new Object[][]{
{(Supplier<Calendar>) () -> {
Calendar cal = Calendar.getInstance(TOKYO_TZ);
Expand Down

0 comments on commit 80c78a4

Please sign in to comment.