mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1684 - ENH: Support ISO date format for JSON marshalling on LocalDate, sql.Date etc
This commit is contained in:
@@ -61,6 +61,7 @@ public class ServerConfigTest {
|
||||
props.setProperty("backgroundExecutorSchedulePoolSize", "4");
|
||||
props.setProperty("dbOffline", "true");
|
||||
props.setProperty("jsonDateTime", "ISO8601");
|
||||
props.setProperty("jsonDate", "ISO8601");
|
||||
props.setProperty("autoReadOnlyDataSource", "true");
|
||||
props.setProperty("disableL2Cache", "true");
|
||||
props.setProperty("notifyL2CacheInForeground", "true");
|
||||
@@ -86,6 +87,8 @@ public class ServerConfigTest {
|
||||
assertEquals(PersistBatch.ALL, serverConfig.getPersistBatchOnCascade());
|
||||
assertEquals(PlatformConfig.DbUuid.BINARY, serverConfig.getPlatformConfig().getDbUuid());
|
||||
assertEquals(JsonConfig.DateTime.ISO8601, serverConfig.getJsonDateTime());
|
||||
assertEquals(JsonConfig.Date.ISO8601, serverConfig.getJsonDate());
|
||||
|
||||
assertEquals("r0,users,orgs", serverConfig.getEnabledL2Regions());
|
||||
|
||||
assertEquals(42, serverConfig.getJdbcFetchSizeFindEach());
|
||||
@@ -126,6 +129,8 @@ public class ServerConfigTest {
|
||||
|
||||
serverConfig.setIdGeneratorAutomatic(false);
|
||||
assertFalse(serverConfig.isIdGeneratorAutomatic());
|
||||
assertEquals(JsonConfig.DateTime.MILLIS, serverConfig.getJsonDateTime());
|
||||
assertEquals(JsonConfig.Date.MILLIS, serverConfig.getJsonDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
public class DateTimeJsonParserTest {
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_hasMillis() throws Exception {
|
||||
|
||||
DateTimeJsonParser parser = new DateTimeJsonParser();
|
||||
|
||||
String input = "2016-02-28T20:39:00.123Z";
|
||||
String formatted = parseAndFormat(parser, input);
|
||||
|
||||
assertThat(formatted).isEqualTo("2016-02-28T20:39:00.123Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_noMillis() throws Exception {
|
||||
|
||||
DateTimeJsonParser parser = new DateTimeJsonParser();
|
||||
|
||||
String input = "2016-02-28T20:39:00Z";
|
||||
String formatted = parseAndFormat(parser, input);
|
||||
|
||||
assertThat(formatted).isEqualTo("2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_1dp() throws Exception {
|
||||
|
||||
DateTimeJsonParser parser = new DateTimeJsonParser();
|
||||
|
||||
String input = "2016-02-28T20:39:00.0Z";
|
||||
String formatted = parseAndFormat(parser, input);
|
||||
|
||||
assertThat(formatted).isEqualTo("2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_2dp() throws Exception {
|
||||
|
||||
DateTimeJsonParser parser = new DateTimeJsonParser();
|
||||
|
||||
String input = "2016-02-28T20:39:00.00Z";
|
||||
String formatted = parseAndFormat(parser, input);
|
||||
|
||||
assertThat(formatted).isEqualTo("2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_3dp() throws Exception {
|
||||
|
||||
DateTimeJsonParser parser = new DateTimeJsonParser();
|
||||
|
||||
String input = "2016-02-28T20:39:00.000Z";
|
||||
String formatted = parseAndFormat(parser, input);
|
||||
|
||||
assertThat(formatted).isEqualTo("2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
private String parseAndFormat(DateTimeJsonParser parser, String input) {
|
||||
Timestamp timestamp = parser.parse(input);
|
||||
return parser.format(timestamp);
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ public class JsonTester<T> {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void test(T value) throws IOException {
|
||||
public String test(T value) throws IOException {
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
@@ -48,5 +48,6 @@ public class JsonTester<T> {
|
||||
T val1 = type.jsonRead(parser);
|
||||
assertEquals(value, val1);
|
||||
|
||||
return writer.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ScalarTypeDateTest {
|
||||
|
||||
ScalarTypeDate type = new ScalarTypeDate();
|
||||
private ScalarTypeDate type = new ScalarTypeDate(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void formatParse_PG_DATE_POSITIVE_INFINITY() {
|
||||
@@ -19,4 +23,16 @@ public class ScalarTypeDateTest {
|
||||
Date parsed = type.parse(format);
|
||||
assertEquals(parsed, postgresInfinityDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
Date val = Date.valueOf(LocalDate.of(2019, 5, 9));
|
||||
|
||||
JsonTester<Date> jsonMillis = new JsonTester<>(type);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557316800000}");
|
||||
|
||||
JsonTester<Date> jsonIso = new JsonTester<>(new ScalarTypeDate(JsonConfig.Date.ISO8601));
|
||||
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -10,7 +12,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ScalarTypeJodaLocalDateTest {
|
||||
|
||||
private ScalarTypeJodaLocalDate type = new ScalarTypeJodaLocalDate();
|
||||
private ScalarTypeJodaLocalDate type = new ScalarTypeJodaLocalDate(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void convertToMillis_convertFromMillis() {
|
||||
@@ -58,4 +60,15 @@ public class ScalarTypeJodaLocalDateTest {
|
||||
assertThat(beanType).isEqualTo(localDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
LocalDate val = new LocalDate(2019, 5, 9);
|
||||
|
||||
JsonTester<LocalDate> jsonMillis = new JsonTester<>(type);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557316800000}");
|
||||
|
||||
JsonTester<LocalDate> jsonIso = new JsonTester<>(new ScalarTypeJodaLocalDate(JsonConfig.Date.ISO8601) );
|
||||
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ScalarTypeLocalDateTest {
|
||||
|
||||
ScalarTypeLocalDate type = new ScalarTypeLocalDate();
|
||||
ScalarTypeLocalDate type = new ScalarTypeLocalDate(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void testConvertToMillis() throws Exception {
|
||||
public void testConvertToMillis() {
|
||||
|
||||
LocalDate date = LocalDate.of(2014, 5, 20);
|
||||
long millis = type.convertToMillis(date);
|
||||
@@ -23,7 +26,7 @@ public class ScalarTypeLocalDateTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFromDate() throws Exception {
|
||||
public void testConvertFromDate() {
|
||||
|
||||
LocalDate localDate = LocalDate.now();
|
||||
Date date = Date.valueOf(localDate);
|
||||
@@ -37,7 +40,7 @@ public class ScalarTypeLocalDateTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
public void testToJdbcType() {
|
||||
|
||||
LocalDate localDate = LocalDate.now();
|
||||
Object o = type.toJdbcType(localDate);
|
||||
@@ -45,7 +48,7 @@ public class ScalarTypeLocalDateTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBeanType() throws Exception {
|
||||
public void testToBeanType() {
|
||||
|
||||
LocalDate localDate = LocalDate.now();
|
||||
Date date = Date.valueOf(localDate);
|
||||
@@ -54,4 +57,15 @@ public class ScalarTypeLocalDateTest {
|
||||
assertEquals(localDate, localDate1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
LocalDate val = LocalDate.of(2019, 5, 9);
|
||||
|
||||
JsonTester<LocalDate> jsonMillis = new JsonTester<>(type);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557316800000}");
|
||||
|
||||
JsonTester<LocalDate> jsonIso = new JsonTester<>(new ScalarTypeLocalDate(JsonConfig.Date.ISO8601) );
|
||||
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
public class ScalarTypeUtilDateTest {
|
||||
|
||||
private ScalarTypeUtilDate.DateType dateType = new ScalarTypeUtilDate.DateType(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
Date val = dateType.parse("2019-05-09");
|
||||
|
||||
JsonTester<Date> jsonMillis = new JsonTester<>(dateType);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557316800000}");
|
||||
|
||||
JsonTester<Date> jsonIso = new JsonTester<>(new ScalarTypeUtilDate.DateType(JsonConfig.Date.ISO8601) );
|
||||
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-09\"}");
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,22 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.time.YearMonth;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ScalarTypeYearMonthDateTest {
|
||||
|
||||
ScalarTypeYearMonthDate type = new ScalarTypeYearMonthDate();
|
||||
private ScalarTypeYearMonthDate type = new ScalarTypeYearMonthDate(JsonConfig.Date.MILLIS);
|
||||
|
||||
@Test
|
||||
public void testConvertFromMillis() throws Exception {
|
||||
public void testConvertFromMillis() {
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate firstMonthDay = today.withDayOfMonth(1);
|
||||
@@ -27,7 +30,7 @@ public class ScalarTypeYearMonthDateTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertDate() throws Exception {
|
||||
public void testConvertDate() {
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate firstMonthDay = today.withDayOfMonth(1);
|
||||
@@ -40,7 +43,7 @@ public class ScalarTypeYearMonthDateTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testToJdbcType() throws Exception {
|
||||
public void testToJdbcType() {
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate firstMonthDay = today.withDayOfMonth(1);
|
||||
@@ -51,4 +54,16 @@ public class ScalarTypeYearMonthDateTest {
|
||||
assertEquals(date, val1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void json() throws IOException {
|
||||
|
||||
YearMonth val = YearMonth.of(2019, 5);
|
||||
|
||||
JsonTester<YearMonth> jsonMillis = new JsonTester<>(type);
|
||||
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1556625600000}");
|
||||
|
||||
JsonTester<YearMonth> jsonIso = new JsonTester<>(new ScalarTypeYearMonthDate(JsonConfig.Date.ISO8601) );
|
||||
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-01\"}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class UtilDateParserTest {
|
||||
|
||||
@Test
|
||||
public void parse() {
|
||||
|
||||
Date val = UtilDateParser.parse("2019-05-09");
|
||||
String format = UtilDateParser.format(val);
|
||||
assertThat(format).isEqualTo("2019-05-09");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
public class UtilDateTimeParserTest {
|
||||
|
||||
private UtilDateTimeParser parser = new UtilDateTimeParser();
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_hasMillis() {
|
||||
parseAndFormat("2016-02-28T20:39:00.123Z", "2016-02-28T20:39:00.123Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_noMillis() {
|
||||
parseAndFormat("2016-02-28T20:39:00Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_1dp() {
|
||||
parseAndFormat("2016-02-28T20:39:00.0Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_2dp() {
|
||||
parseAndFormat("2016-02-28T20:39:00.00Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFormat_when_millis_3dp() {
|
||||
parseAndFormat("2016-02-28T20:39:00.000Z", "2016-02-28T20:39:00.000Z");
|
||||
}
|
||||
|
||||
private void parseAndFormat(String input, String expected) {
|
||||
Timestamp timestamp = parser.parse(input);
|
||||
String format = parser.format(timestamp);
|
||||
assertThat(format).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.tests.transaction;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.EbeanServer;
|
||||
import io.ebean.EbeanServerFactory;
|
||||
@@ -9,6 +7,7 @@ import io.ebean.Query;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.ForPlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.config.ServerConfig;
|
||||
import io.ebean.config.properties.PropertiesLoader;
|
||||
import io.ebean.datasource.DataSourceConfig;
|
||||
@@ -19,7 +18,6 @@ import org.junit.Test;
|
||||
import org.tests.model.basic.UTDetail;
|
||||
import org.tests.model.basic.UTMaster;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
@@ -127,14 +125,8 @@ public class TestExplicitTransactionMode extends BaseTestCase {
|
||||
|
||||
public static class ScalarTypeLocalDateAsString extends ScalarTypeLocalDate {
|
||||
|
||||
@Override
|
||||
public LocalDate jsonRead(JsonParser parser) throws IOException {
|
||||
return super.jsonRead(parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator writer, LocalDate value) throws IOException {
|
||||
writer.writeString(value.toString());
|
||||
public ScalarTypeLocalDateAsString() {
|
||||
super(JsonConfig.Date.ISO8601);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user