Skip to content

Commit

Permalink
#659 - ENH: Add support for Instant with @WhenCreated @WhenModified
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Apr 22, 2016
1 parent b79bb89 commit e769abf
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 1 deletion.
Expand Up @@ -36,6 +36,17 @@ public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
}
}

/**
* Instant support.
*/
public static class InstantDT extends Base {

@Override
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
return JavaTimeUtils.toInstant(now);
}
}

/**
* LocalDateTime support.
*/
Expand Down
Expand Up @@ -31,6 +31,22 @@ public boolean isDDLNotNullable() {
}
}

/**
* Instant support.
*/
public static class InstantDT extends Base {

@Override
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
return JavaTimeUtils.toInstant(now);
}

@Override
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
return JavaTimeUtils.toInstant(now);
}
}

/**
* LocalDateTime support.
*/
Expand Down
Expand Up @@ -5,6 +5,7 @@

import javax.persistence.PersistenceException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
Expand All @@ -27,6 +28,7 @@ public InsertTimestampFactory(ClassLoadConfig classLoadConfig) {
map.put(long.class, longTime);

if (classLoadConfig.isJavaTimePresent()) {
map.put(Instant.class, new GeneratedInsertJavaTime.InstantDT());
map.put(LocalDateTime.class, new GeneratedInsertJavaTime.LocalDT());
map.put(OffsetDateTime.class, new GeneratedInsertJavaTime.OffsetDT());
map.put(ZonedDateTime.class, new GeneratedInsertJavaTime.ZonedDT());
Expand Down
Expand Up @@ -11,6 +11,13 @@
*/
public class JavaTimeUtils {

/**
* Return the system millis time as a LocalDateTime.
*/
public static Object toInstant(long systemMillis) {
return Instant.ofEpochMilli(systemMillis);
}

/**
* Return the system millis time as a LocalDateTime.
*/
Expand Down
Expand Up @@ -5,6 +5,7 @@

import javax.persistence.PersistenceException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
Expand All @@ -27,6 +28,7 @@ public UpdateTimestampFactory(ClassLoadConfig classLoadConfig) {
map.put(long.class, longTime);

if (classLoadConfig.isJavaTimePresent()) {
map.put(Instant.class, new GeneratedUpdateJavaTime.InstantDT());
map.put(LocalDateTime.class, new GeneratedUpdateJavaTime.LocalDT());
map.put(OffsetDateTime.class, new GeneratedUpdateJavaTime.OffsetDT());
map.put(ZonedDateTime.class, new GeneratedUpdateJavaTime.ZonedDT());
Expand Down
Expand Up @@ -5,6 +5,7 @@
import org.junit.Test;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;

Expand All @@ -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() {

Expand Down
Expand Up @@ -5,6 +5,7 @@
import org.junit.Test;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;

Expand All @@ -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() {

Expand Down
Expand Up @@ -31,16 +31,19 @@ public void test_insert() {
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);
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/avaje/tests/model/EGenProps.java
Expand Up @@ -9,6 +9,7 @@
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;
Expand Down Expand Up @@ -54,6 +55,12 @@ public class EGenProps {
@UpdatedTimestamp
ZonedDateTime zdtUpdated;

@CreatedTimestamp
Instant instantCreated;

@UpdatedTimestamp
Instant instantUpdated;

@CreatedTimestamp
long longCreated;

Expand Down Expand Up @@ -179,4 +186,20 @@ public long getLongUpdated() {
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;
}
}

0 comments on commit e769abf

Please sign in to comment.