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
@@ -1,92 +0,0 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import org.joda.time.LocalDate;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.sql.Date;
import java.util.TimeZone;
import static org.assertj.core.api.Assertions.assertThat;
public class ScalarTypeJodaLocalDateTest {
private ScalarTypeJodaLocalDate type = new ScalarTypeJodaLocalDate(JsonConfig.Date.MILLIS);
@Test
public void convertToMillis_convertFromMillis() {
LocalDate localDate = new LocalDate();
long millis = type.convertToMillis(localDate);
LocalDate localDate1 = type.convertFromMillis(millis);
assertThat(localDate).isEqualTo(localDate1);
}
@Test
public void convertToDate_convertFromDate_westOfUtc() {
final TimeZone originaDefaultTimezone = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("America/Chicago"));
convertDate(new LocalDate());
convertDate(new LocalDate(1899, 12, 1));
convertDate(new LocalDate(1900, 1, 1));
convertDate(new LocalDate(2021, 2, 8));
} finally {
TimeZone.setDefault(originaDefaultTimezone);
}
}
@Test
public void convertToDate_convertFromDate() {
convertDate(new LocalDate());
convertDate(new LocalDate(1899, 12, 1));
convertDate(new LocalDate(1900, 1, 1));
convertDate(new LocalDate(2021, 2, 8));
}
private void convertDate(LocalDate localDate) {
Date dateValue = type.convertToDate(localDate);
LocalDate localDate1 = type.convertFromDate(dateValue);
assertThat(localDate).isEqualTo(localDate1);
}
@Test
public void toJdbcType() {
LocalDate localDate = new LocalDate();
Object jdbcType = type.toJdbcType(localDate);
Date dateValue = type.convertToDate(localDate);
assertThat(jdbcType).isEqualTo(dateValue);
}
@Test
public void toBeanType() {
LocalDate localDate = new LocalDate();
Date dateValue = type.convertToDate(localDate);
LocalDate beanType = type.toBeanType(dateValue);
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,43 +0,0 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.junit.jupiter.api.Test;
import java.sql.Timestamp;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ScalarTypeJodaLocalDateTimeTest {
ScalarTypeJodaLocalDateTime type = new ScalarTypeJodaLocalDateTime(JsonConfig.DateTime.ISO8601);
@Test
public void testConvertFromTimestamp() throws Exception {
long now = System.currentTimeMillis();
Timestamp nowTs = new Timestamp(now);
LocalDateTime ldt1 = type.convertFromTimestamp(nowTs);
LocalDateTime ldt2 = localConvertFromTimestamp(nowTs);
assertEquals(ldt1, ldt2);
Timestamp ts1 = type.convertToTimestamp(ldt1);
Timestamp ts2 = localConvertToTimestamp(ldt2);
assertEquals(ts1, ts2);
}
LocalDateTime localConvertFromTimestamp(Timestamp ts) {
return new LocalDateTime(ts.getTime(), DateTimeZone.getDefault());
}
Timestamp localConvertToTimestamp(LocalDateTime t) {
return new Timestamp(t.toDateTime(DateTimeZone.getDefault()).getMillis());
}
}
@@ -1,64 +0,0 @@
package io.ebeaninternal.server.type;
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;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ScalarTypeJodaLocalTimeTest {
ScalarTypeJodaLocalTime type = new ScalarTypeJodaLocalTime();
@Test
public void toJdbcType_toBeanType() {
LocalTime localTime0 = new LocalTime().withMillisOfSecond(0);
Object time = type.toJdbcType(localTime0);
LocalTime localTime1 = type.toBeanType(time);
assertThat(localTime0).isEqualTo(localTime1);
}
@Test
public void test() {
long now = System.currentTimeMillis();
//DateTimeZone timeZone = DateTimeZone.getDefault();
//ISOChronology instance = ISOChronology.getInstance();
LocalDateTime ldt1 = new LocalDateTime(now, DateTimeZone.getDefault());
LocalDateTime ldt2 = new LocalDateTime(now);
assertEquals(ldt1, ldt2);
Timestamp ts1 = new Timestamp(ldt1.toDateTime(DateTimeZone.getDefault()).getMillis());
Timestamp ts2 = new Timestamp(ldt2.toDateTime().getMillis());
assertEquals(ts1, ts2);
}
@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,43 +0,0 @@
package io.ebeaninternal.server.type;
import org.joda.time.Period;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ScalarTypeJodaPeriodTest {
private ScalarTypeJodaPeriod scalarType = new ScalarTypeJodaPeriod();
@Test
public void getLength() {
assertThat(scalarType.length()).isEqualTo(50);
}
@Test
public void formatAndParse() {
Period original = Period.years(1).plusMonths(2).plusDays(4)
.plusHours(12).plusMinutes(19).plusSeconds(20);
String value = scalarType.formatValue(original);
assertThat(value).isEqualTo("P1Y2M4DT12H19M20S");
Period period = scalarType.parse(value);
assertThat(period).isEqualTo(original);
}
@Test
public void convertFromDbString() {
Period original = Period.years(1).plusMonths(2).plusDays(4)
.plusHours(23).plusMinutes(19).plusSeconds(20).plusMillis(987);
String stringVal = scalarType.convertToDbString(original);
Period period = scalarType.convertFromDbString(stringVal);
assertThat(period).isEqualTo(original);
}
}
@@ -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;
}
}