#659 - ENH: Add support for Instant with @WhenCreated @WhenModified

This commit is contained in:
Robin Bygrave
2016-04-22 16:51:30 +12:00
parent b79bb892f7
commit e769abf737
9 changed files with 87 additions and 1 deletions
@@ -5,6 +5,7 @@ import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import org.junit.Test;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@@ -14,6 +15,16 @@ public class InsertTimestampFactoryTest {
InsertTimestampFactory factory = new InsertTimestampFactory(new ClassLoadConfig());
@Test
public void test_createdTimestamp_Instant() {
DeployBeanProperty prop = new DeployBeanProperty(null, Instant.class, null, null);
GeneratedProperty insertTimestamp = factory.createInsertTimestamp(prop);
Object value = insertTimestamp.getInsertValue(null, null, System.currentTimeMillis());
assertTrue(value instanceof Instant);
}
@Test
public void test_createdTimestamp_LocalDateTime() {
@@ -5,6 +5,7 @@ import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import org.junit.Test;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@@ -15,6 +16,16 @@ public class UpdateTimestampFactoryTest {
UpdateTimestampFactory factory = new UpdateTimestampFactory(new ClassLoadConfig());
@Test
public void test_createdTimestamp_Instant() {
DeployBeanProperty prop = new DeployBeanProperty(null, Instant.class, null, null);
GeneratedProperty generatedProperty = factory.createUpdateTimestamp(prop);
Object value = generatedProperty.getInsertValue(null, null, System.currentTimeMillis());
assertTrue(value instanceof Instant);
}
@Test
public void test_createdTimestamp_LocalDateTime() {
@@ -31,16 +31,19 @@ public class TestGeneratedProperties extends BaseTestCase {
assertNotNull(bean.getZdtUpdated());
assertNotNull(bean.getLongCreated());
assertNotNull(bean.getLongUpdated());
assertNotNull(bean.getInstantCreated());
assertNotNull(bean.getInstantUpdated());
assertThat(bean.getWhenCreated().toInstant().toEpochMilli()).isEqualTo(bean.getLongCreated());
assertThat(bean.getWhenModified().toInstant().toEpochMilli()).isEqualTo(bean.getLongCreated());
assertThat(bean.getWhenModified().toInstant().toEpochMilli()).isEqualTo(bean.getLongCreated());
bean.setName("updating...");
Ebean.save(bean);
assertThat(bean.getWhenModified()).isNotEqualTo(bean.getLongCreated());
assertThat(bean.getWhenModified().toInstant().toEpochMilli()).isEqualTo(bean.getLongUpdated());
assertThat(bean.getInstantUpdated().toEpochMilli()).isEqualTo(bean.getLongUpdated());
assertThat(bean.getInstantCreated().toEpochMilli()).isEqualTo(bean.getLongCreated());
Ebean.delete(bean);
}
@@ -9,6 +9,7 @@ import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
@@ -54,6 +55,12 @@ public class EGenProps {
@UpdatedTimestamp
ZonedDateTime zdtUpdated;
@CreatedTimestamp
Instant instantCreated;
@UpdatedTimestamp
Instant instantUpdated;
@CreatedTimestamp
long longCreated;
@@ -179,4 +186,20 @@ public class EGenProps {
public void setLongUpdated(long longUpdated) {
this.longUpdated = longUpdated;
}
public Instant getInstantCreated() {
return instantCreated;
}
public void setInstantCreated(Instant instantCreated) {
this.instantCreated = instantCreated;
}
public Instant getInstantUpdated() {
return instantUpdated;
}
public void setInstantUpdated(Instant instantUpdated) {
this.instantUpdated = instantUpdated;
}
}