#1652 - Conversion problem between org.joda.time.LocalDate and java.sql.Date field for dates before Jan 2 1900

This commit is contained in:
rob bygrave
2019-03-14 16:08:04 +13:00
parent 435e1f61fe
commit 409c1d06b2
4 changed files with 53 additions and 13 deletions
@@ -10,10 +10,10 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ScalarTypeJodaLocalDateTest {
ScalarTypeJodaLocalDate type = new ScalarTypeJodaLocalDate();
private ScalarTypeJodaLocalDate type = new ScalarTypeJodaLocalDate();
@Test
public void convertToMillis_convertFromMillis() throws Exception {
public void convertToMillis_convertFromMillis() {
LocalDate localDate = new LocalDate();
long millis = type.convertToMillis(localDate);
@@ -23,18 +23,23 @@ public class ScalarTypeJodaLocalDateTest {
}
@Test
public void convertToDate_convertFromDate() throws Exception {
public void convertToDate_convertFromDate() {
convertDate(new LocalDate());
convertDate(new LocalDate(1899, 12, 1));
convertDate(new LocalDate(1900, 1, 1));
}
private void convertDate(LocalDate localDate) {
LocalDate localDate = new LocalDate();
Date dateValue = type.convertToDate(localDate);
LocalDate localDate1 = type.convertFromDate(dateValue);
assertThat(localDate).isEqualTo(localDate1);
}
@Test
public void toJdbcType() throws Exception {
public void toJdbcType() {
LocalDate localDate = new LocalDate();
Object jdbcType = type.toJdbcType(localDate);
@@ -44,7 +49,7 @@ public class ScalarTypeJodaLocalDateTest {
}
@Test
public void toBeanType() throws Exception {
public void toBeanType() {
LocalDate localDate = new LocalDate();
Date dateValue = type.convertToDate(localDate);
@@ -3,6 +3,7 @@ package org.tests.model.joda;
import io.ebean.annotation.CreatedTimestamp;
import io.ebean.annotation.UpdatedTimestamp;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.Period;
@@ -26,6 +27,8 @@ public class BasicJodaEntity {
Period period;
LocalDate localDate;
@Version
LocalDateTime version;
@@ -76,4 +79,12 @@ public class BasicJodaEntity {
public void setVersion(LocalDateTime version) {
this.version = version;
}
public void setLocalDate(LocalDate localDate) {
this.localDate = localDate;
}
public LocalDate getLocalDate() {
return localDate;
}
}
@@ -1,8 +1,9 @@
package org.tests.model.joda;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.DB;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.Period;
import org.junit.Test;
@@ -19,8 +20,8 @@ public class TestJodaInsertUpdate extends BaseTestCase {
BasicJodaEntity e0 = new BasicJodaEntity();
e0.setName("foo");
Ebean.save(e0);
e0.setLocalDate(new LocalDate(1899, 12, 1));
DB.save(e0);
LocalDateTime created = e0.getCreated();
DateTime updated = e0.getUpdated();
@@ -32,7 +33,7 @@ public class TestJodaInsertUpdate extends BaseTestCase {
Thread.sleep(10);
e0.setName("bar");
e0.setPeriod(Period.years(12).plusDays(1));
Ebean.save(e0);
DB.save(e0);
LocalDateTime created1 = e0.getCreated();
DateTime updated1 = e0.getUpdated();
@@ -43,8 +44,31 @@ public class TestJodaInsertUpdate extends BaseTestCase {
assertNotSame(version, version1);
BasicJodaEntity found = Ebean.find(BasicJodaEntity.class, e0.getId());
BasicJodaEntity found = DB.find(BasicJodaEntity.class, e0.getId());
assertThat(found.getPeriod()).isEqualTo(e0.getPeriod());
assertThat(found.getLocalDate()).isEqualTo(e0.getLocalDate());
}
@Test
public void test_various() {
test_at(new LocalDate(1700, 12, 1));
test_at(new LocalDate(1899, 12, 1));
test_at(new LocalDate(1900, 1, 1));
test_at(new LocalDate());
test_at(LocalDate.now());
}
private void test_at(LocalDate date) {
BasicJodaEntity e0 = new BasicJodaEntity();
e0.setName("Various local dates");
e0.setLocalDate(date);
DB.save(e0);
BasicJodaEntity found = DB.find(BasicJodaEntity.class, e0.getId());
assertThat(found.getLocalDate()).isEqualTo(e0.getLocalDate());
DB.delete(found);
}
}