#1727 - Refactor internals to use Instant for json parse on date time types (#1728)

This commit is contained in:
Rob Bygrave
2019-06-10 16:16:04 +12:00
committed by GitHub
parent e26a38ecaa
commit 6af5fae8e9
20 changed files with 237 additions and 53 deletions
@@ -3,13 +3,13 @@ package io.ebeaninternal.server.type;
import org.junit.Test;
import java.sql.Timestamp;
import java.time.Instant;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class UtilDateTimeParserTest {
public class IsoJsonDateTimeParserTest {
private UtilDateTimeParser parser = new UtilDateTimeParser();
private IsoJsonDateTimeParser parser = new IsoJsonDateTimeParser();
@Test
public void parseFormat_when_hasMillis() {
@@ -42,8 +42,8 @@ public class UtilDateTimeParserTest {
}
private void parseAndFormat(String input, String expected) {
Timestamp timestamp = parser.parse(input);
String format = parser.format(timestamp);
Instant timestamp = parser.parseIso(input);
String format = parser.formatIso(timestamp);
assertThat(format).isEqualTo(expected);
}
}
@@ -0,0 +1,24 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import org.junit.Test;
import java.sql.Types;
import java.util.Calendar;
import static org.assertj.core.api.Assertions.assertThat;
public class ScalarTypeCalendarTest {
private ScalarTypeCalendar type = new ScalarTypeCalendar(JsonConfig.DateTime.ISO8601, Types.TIMESTAMP);
@Test
public void toJsonISO8601() {
Calendar instance = Calendar.getInstance();
String asUtc = type.toJsonISO8601(instance);
Calendar calendar = type.fromJsonISO8601(asUtc);
assertThat(instance).isEqualTo(calendar);
}
}
@@ -10,7 +10,10 @@ import java.io.ObjectOutputStream;
import java.sql.Timestamp;
import java.time.Instant;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class ScalarTypeInstantTest {
@@ -123,7 +126,6 @@ public class ScalarTypeInstantTest {
@Test
public void testJsonRead() throws Exception {
Instant now = now();
JsonTester<Instant> jsonTester = new JsonTester<>(type);
@@ -139,4 +141,15 @@ public class ScalarTypeInstantTest {
}
@Test
public void isoJsonParseFormat() {
ScalarTypeInstant typeIso = new ScalarTypeInstant(JsonConfig.DateTime.ISO8601);
Instant instant = Instant.now();
String asJson = typeIso.toJsonISO8601(instant);
Instant value = typeIso.fromJsonISO8601(asJson);
assertThat(instant).isEqualTo(value);
}
}
@@ -6,7 +6,10 @@ import org.junit.Test;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class ScalarTypeLocalDateTimeTest {
@@ -87,4 +90,16 @@ public class ScalarTypeLocalDateTimeTest {
jsonTester = new JsonTester<>(typeIso);
jsonTester.test(now);
}
@Test
public void isoJsonFormatParse() {
ScalarTypeLocalDateTime typeIso = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
LocalDateTime localDateTime = LocalDateTime.now();
String asJson = typeIso.toJsonISO8601(localDateTime);
LocalDateTime value = typeIso.fromJsonISO8601(asJson);
assertThat(localDateTime).isEqualToIgnoringNanos(value);
}
}
@@ -6,7 +6,10 @@ import org.junit.Test;
import java.sql.Timestamp;
import java.time.OffsetDateTime;
import static org.junit.Assert.*;
import static org.assertj.core.api.StrictAssertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class ScalarTypeOffsetDateTimeTest {
@@ -74,4 +77,16 @@ public class ScalarTypeOffsetDateTimeTest {
jsonTester = new JsonTester<>(typeIso);
jsonTester.test(now);
}
@Test
public void isoJsonFormatParse() {
ScalarTypeOffsetDateTime typeIso = new ScalarTypeOffsetDateTime(JsonConfig.DateTime.ISO8601);
OffsetDateTime now = OffsetDateTime.now();
String asJson = typeIso.toJsonISO8601(now);
OffsetDateTime value = typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualToIgnoringNanos(value);
}
}
@@ -0,0 +1,23 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import org.junit.Test;
import java.sql.Timestamp;
import static org.assertj.core.api.StrictAssertions.assertThat;
public class ScalarTypeTimestampTest {
@Test
public void toJsonISO8601() {
ScalarTypeTimestamp typeIso = new ScalarTypeTimestamp(JsonConfig.DateTime.ISO8601);
Timestamp now = new Timestamp(System.currentTimeMillis());
String asJson = typeIso.toJsonISO8601(now);
Timestamp value = typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualTo(value);
}
}
@@ -24,4 +24,16 @@ public class ScalarTypeUtilDateTest {
Date val1 = jsonIso.type.parse("2019-05-09");
assertThat(jsonIso.test(val1)).isEqualTo("{\"key\":\"2019-05-09\"}");
}
@Test
public void toJsonISO8601() {
ScalarTypeUtilDate.TimestampType typeIso = new ScalarTypeUtilDate.TimestampType(JsonConfig.DateTime.ISO8601);
Date now = new Date();
String asJson = typeIso.toJsonISO8601(now);
Date value = typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualTo(value);
}
}
@@ -6,6 +6,7 @@ import org.junit.Test;
import java.sql.Timestamp;
import java.time.ZonedDateTime;
import static org.assertj.core.api.StrictAssertions.assertThat;
import static org.junit.Assert.*;
public class ScalarTypeZonedDateTimeTest {
@@ -75,4 +76,16 @@ public class ScalarTypeZonedDateTimeTest {
jsonTester = new JsonTester<>(typeIso);
jsonTester.test(now);
}
@Test
public void toJsonISO8601() {
ScalarTypeZonedDateTime typeIso = new ScalarTypeZonedDateTime(JsonConfig.DateTime.ISO8601);
ZonedDateTime now = ZonedDateTime.now();
String asJson = typeIso.toJsonISO8601(now);
ZonedDateTime value = typeIso.fromJsonISO8601(asJson);
assertThat(now).isEqualTo(value);
}
}