#1272 - ENH: Add built in support for Joda Period

This commit is contained in:
Rob Bygrave
2018-02-23 17:41:01 +13:00
parent 7d0ed261b5
commit 265bda8cf2
5 changed files with 105 additions and 1 deletions
@@ -0,0 +1,43 @@
package io.ebeaninternal.server.type;
import org.joda.time.Period;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ScalarTypeJodaPeriodTest {
private ScalarTypeJodaPeriod scalarType = new ScalarTypeJodaPeriod();
@Test
public void getLength() {
assertThat(scalarType.getLength()).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);
}
}
@@ -4,6 +4,7 @@ import io.ebean.annotation.CreatedTimestamp;
import io.ebean.annotation.UpdatedTimestamp;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.Period;
import javax.persistence.Entity;
import javax.persistence.Id;
@@ -23,6 +24,8 @@ public class BasicJodaEntity {
@UpdatedTimestamp
DateTime updated;
Period period;
@Version
LocalDateTime version;
@@ -42,6 +45,14 @@ public class BasicJodaEntity {
this.name = name;
}
public Period getPeriod() {
return period;
}
public void setPeriod(Period period) {
this.period = period;
}
public LocalDateTime getCreated() {
return created;
}
@@ -4,9 +4,13 @@ import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.joda.time.Period;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
public class TestJodaInsertUpdate extends BaseTestCase {
@@ -27,6 +31,7 @@ public class TestJodaInsertUpdate extends BaseTestCase {
Thread.sleep(10);
e0.setName("bar");
e0.setPeriod(Period.years(12).plusDays(1));
Ebean.save(e0);
LocalDateTime created1 = e0.getCreated();
@@ -36,5 +41,10 @@ public class TestJodaInsertUpdate extends BaseTestCase {
assertSame(created, created1);
assertNotSame(updated, updated1);
assertNotSame(version, version1);
BasicJodaEntity found = Ebean.find(BasicJodaEntity.class, e0.getId());
assertThat(found.getPeriod()).isEqualTo(e0.getPeriod());
}
}