Refactor extract ebean-joda-time module - move joda ScalarTypes

Moves the joda-time ScalarTypes into a new ebean-joda-time module. Include
the ebean-joda-time module in the classpath, and the extra types will be
service loaded (by DefaultTypeManager).
This commit is contained in:
Rob Bygrave
2022-08-26 17:32:31 +12:00
parent 7b3405ca25
commit b7e62df371
22 changed files with 146 additions and 108 deletions
@@ -147,7 +147,6 @@ public final class DefaultTypeManager implements TypeManager {
initialiseStandard(config);
initialiseJavaTimeTypes(config);
initialiseJodaTypes(config);
initialiseJacksonTypes(config);
loadTypesFromProviders(config, objectMapper);
loadGeoTypeBinder(config);
@@ -261,7 +260,7 @@ public final class DefaultTypeManager implements TypeManager {
if (found == null) {
if (type.getName().equals("org.joda.time.LocalTime")) {
throw new IllegalStateException(
"ScalarType of Joda LocalTime not defined. You need to set DatabaseConfig.jodaLocalTimeMode to"
"ScalarType of Joda LocalTime not defined. 1) Check ebean-joda-time dependency has been added 2) Check DatabaseConfig.jodaLocalTimeMode is set to"
+ " either 'normal' or 'utc'. UTC is the old mode using UTC timezone but local time zone is now preferred as 'normal' mode.");
}
found = checkInheritedTypes(type);
@@ -798,36 +797,6 @@ public final class DefaultTypeManager implements TypeManager {
logicalMap.putIfAbsent(clazz.getSimpleName(), scalarType);
}
/**
* Detect if Joda classes are in the classpath and if so register the Joda data types.
*/
@SuppressWarnings("deprecation")
private void initialiseJodaTypes(DatabaseConfig config) {
// detect if Joda classes are in the classpath
if (config.getClassLoadConfig().isJodaTimePresent()) {
// Joda classes are in the classpath so register the types
log.log(DEBUG, "Registering Joda data types");
addType(LocalDateTime.class, new ScalarTypeJodaLocalDateTime(jsonDateTime));
addType(DateTime.class, new ScalarTypeJodaDateTime(jsonDateTime));
if (config.getDatabasePlatform().supportsNativeJavaTime()) {
addType(LocalDate.class, new ScalarTypeJodaLocalDateNative(jsonDate));
} else {
addType(LocalDate.class, new ScalarTypeJodaLocalDate(jsonDate));
}
addType(org.joda.time.DateMidnight.class, new ScalarTypeJodaDateMidnight(jsonDate));
addType(org.joda.time.Period.class, new ScalarTypeJodaPeriod());
String jodaLocalTimeMode = config.getJodaLocalTimeMode();
if ("normal".equalsIgnoreCase(jodaLocalTimeMode)) {
// use the expected/normal local time zone
addType(LocalTime.class, new ScalarTypeJodaLocalTime());
} else if ("utc".equalsIgnoreCase(jodaLocalTimeMode)) {
// use the old UTC based
addType(LocalTime.class, new ScalarTypeJodaLocalTimeUTC());
}
}
}
/**
* Register all the standard types supported. This is the standard JDBC types
* plus some other common types such as java.util.Date and java.util.Calendar.
@@ -1,32 +0,0 @@
package org.tests.model.basic;
import org.joda.time.LocalTime;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class TJodaEntity {
@Id
Integer id;
LocalTime localTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalTime getLocalTime() {
return localTime;
}
public void setLocalTime(LocalTime localTime) {
this.localTime = localTime;
}
}
+44
View File
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ebean-parent</artifactId>
<groupId>io.ebean</groupId>
<version>13.8.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ebean-joda-time</artifactId>
<dependencies>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-core-type</artifactId>
<version>13.8.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,40 @@
package io.ebean.joda.time;
import io.ebean.config.DatabaseConfig;
import io.ebean.core.type.ExtraTypeFactory;
import io.ebean.core.type.ScalarType;
import java.util.ArrayList;
import java.util.List;
/**
* Register the Joda-Time ScalarType support.
*/
public class JodaExtraTypeFactory implements ExtraTypeFactory {
@Override
public List<? extends ScalarType<?>> createTypes(DatabaseConfig config, Object objectMapper) {
var jsonDateTime = config.getJsonDateTime();
var jsonDate = config.getJsonDate();
List<ScalarType<?>> types = new ArrayList<>();
types.add(new ScalarTypeJodaLocalDateTime(jsonDateTime));
types.add(new ScalarTypeJodaDateTime(jsonDateTime));
if (config.getDatabasePlatform().supportsNativeJavaTime()) {
types.add(new ScalarTypeJodaLocalDateNative(jsonDate));
} else {
types.add(new ScalarTypeJodaLocalDate(jsonDate));
}
types.add(new ScalarTypeJodaDateMidnight(jsonDate));
types.add(new ScalarTypeJodaPeriod());
String jodaLocalTimeMode = config.getJodaLocalTimeMode();
if ("normal".equalsIgnoreCase(jodaLocalTimeMode)) {
// use the expected/normal local time zone
types.add(new ScalarTypeJodaLocalTime());
} else if ("utc".equalsIgnoreCase(jodaLocalTimeMode)) {
// use the old UTC based
types.add(new ScalarTypeJodaLocalTimeUTC());
}
return types;
}
}
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDate;
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import java.sql.Date;
import java.sql.Types;
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import java.sql.SQLException;
import java.sql.Types;
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import io.ebean.core.type.ScalarTypeBaseVarchar;
import org.joda.time.Period;
@@ -0,0 +1,9 @@
module io.ebean.joda.time {
requires io.ebean.core.type;
requires org.joda.time;
requires com.fasterxml.jackson.core;
provides io.ebean.core.type.ExtraTypeFactory with io.ebean.joda.time.JodaExtraTypeFactory;
}
@@ -0,0 +1 @@
io.ebean.joda.time.JodaExtraTypeFactory
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import io.ebean.config.JsonConfig;
import org.joda.time.LocalDate;
@@ -78,15 +78,5 @@ 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\":1557360000000}");
JsonTester<LocalDate> jsonIso = new JsonTester<>(new ScalarTypeJodaLocalDate(JsonConfig.Date.ISO8601) );
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-09\"}");
}
}
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import io.ebean.config.JsonConfig;
import org.joda.time.DateTimeZone;
@@ -1,12 +1,9 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import io.ebean.DB;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.TJodaEntity;
import java.sql.Timestamp;
@@ -47,18 +44,5 @@ public class ScalarTypeJodaLocalTimeTest {
}
@Test
public void toJson() {
LocalTime now = new LocalTime();
TJodaEntity bean = new TJodaEntity();
bean.setId(42);
bean.setLocalTime(now);
String json = DB.json().toJson(bean);
TJodaEntity bean1 = DB.json().toBean(TJodaEntity.class, json);
Assertions.assertEquals(bean1.getLocalTime(), now);
}
}
@@ -1,4 +1,4 @@
package io.ebeaninternal.server.type;
package io.ebean.joda.time;
import org.joda.time.Period;
import org.junit.jupiter.api.Test;
+8 -1
View File
@@ -102,6 +102,13 @@
<!-- Test dependencies -->
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-joda-time</artifactId>
<version>13.8.2-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-platform-all</artifactId>
@@ -148,7 +155,7 @@
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.7</version>
<version>2.11.0</version>
<scope>test</scope>
</dependency>
@@ -6,18 +6,19 @@ import io.ebean.core.type.ScalarType;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.TJodaEntity;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class TestJodaType extends BaseTestCase {
class TestJodaType extends BaseTestCase {
@Test
public void test() {
void test() {
SpiEbeanServer server = (SpiEbeanServer) DB.getDefault();
BeanDescriptor<TJodaEntity> beanDescriptor = server.descriptor(TJodaEntity.class);
BeanProperty beanProperty = beanDescriptor.beanProperty("localTime");
@@ -27,8 +28,7 @@ public class TestJodaType extends BaseTestCase {
}
@Test
public void test_insert_find() {
void test_insert_find() {
LocalTime now = new LocalTime().withMillisOfSecond(0);
TJodaEntity bean = new TJodaEntity();
@@ -40,4 +40,19 @@ public class TestJodaType extends BaseTestCase {
assertThat(foundBean.getLocalTime()).isEqualTo(bean.getLocalTime());
}
@Test
void toJson() {
LocalTime now = new LocalTime();
TJodaEntity bean = new TJodaEntity();
bean.setId(42);
bean.setLocalTime(now);
bean.setLocalDate(LocalDate.parse("2022-04-07"));
String json = DB.json().toJson(bean);
TJodaEntity bean1 = DB.json().toBean(TJodaEntity.class, json);
assertEquals(bean1.getLocalTime(), now);
assertEquals(bean1.getLocalDate(), LocalDate.parse("2022-04-07"));
}
}
@@ -1,5 +1,6 @@
package org.tests.model.basic;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import javax.persistence.Entity;
@@ -12,6 +13,7 @@ public class TJodaEntity {
Integer id;
LocalTime localTime;
LocalDate localDate;
public Integer getId() {
return id;
@@ -29,4 +31,12 @@ public class TJodaEntity {
this.localTime = localTime;
}
public LocalDate getLocalDate() {
return localDate;
}
public TJodaEntity setLocalDate(LocalDate localDate) {
this.localDate = localDate;
return this;
}
}
+1
View File
@@ -89,6 +89,7 @@
<module>ebean-redis</module>
<module>platforms</module>
<module>composites</module>
<module>ebean-joda-time</module>
<module>ebean-csv-reader</module>
<!-- <module>ebean-kotlin</module>-->