From f86f1b2647e6a22f3262ef6b3e795967289fee7e Mon Sep 17 00:00:00 2001 From: tobias- Date: Fri, 15 Jun 2018 10:34:11 +0200 Subject: [PATCH] Replaceable clock (#1428) ENH: Use java.time.Clock for @WhenModified and @WhenCreated and allow it to be Replaceable for testing --- .../java/io/ebean/config/ServerConfig.java | 20 +++++++ .../server/core/PersistRequestBean.java | 2 +- .../java/io/ebean/ExtendedServerTest.java | 53 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/ebean/config/ServerConfig.java b/src/main/java/io/ebean/config/ServerConfig.java index bfb0b3999..0d0bbd70e 100644 --- a/src/main/java/io/ebean/config/ServerConfig.java +++ b/src/main/java/io/ebean/config/ServerConfig.java @@ -33,6 +33,7 @@ import io.ebean.util.StringHelper; import org.avaje.datasource.DataSourceConfig; import javax.sql.DataSource; +import java.time.Clock; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -362,6 +363,11 @@ public class ServerConfig { */ private String uuidStateFile = "ebean-uuid.state"; + /** + * The clock used for setting the timestamps (e.g. @UpdatedTimestamp) on objects. + */ + private Clock clock = Clock.systemUTC(); + private List idGenerators = new ArrayList<>(); private List findControllers = new ArrayList<>(); private List persistControllers = new ArrayList<>(); @@ -507,6 +513,20 @@ public class ServerConfig { } + /** + * Get the clock used for setting the timestamps (e.g. @UpdatedTimestamp) on objects. + */ + public Clock getClock() { + return clock; + } + + /** + * Set the clock used for setting the timestamps (e.g. @UpdatedTimestamp) on objects. + */ + public void setClock(final Clock clock) { + this.clock = clock; + } + /** * Return the slow query time in millis. */ diff --git a/src/main/java/io/ebeaninternal/server/core/PersistRequestBean.java b/src/main/java/io/ebeaninternal/server/core/PersistRequestBean.java index 20c6e04b7..6fd15caf0 100644 --- a/src/main/java/io/ebeaninternal/server/core/PersistRequestBean.java +++ b/src/main/java/io/ebeaninternal/server/core/PersistRequestBean.java @@ -1275,7 +1275,7 @@ public final class PersistRequestBean extends PersistRequest implements BeanP */ public long now() { if (now == 0) { - now = System.currentTimeMillis(); + now = ebeanServer.getServerConfig().getClock().millis(); } return now; } diff --git a/src/test/java/io/ebean/ExtendedServerTest.java b/src/test/java/io/ebean/ExtendedServerTest.java index aef467e69..ec76ee80d 100644 --- a/src/test/java/io/ebean/ExtendedServerTest.java +++ b/src/test/java/io/ebean/ExtendedServerTest.java @@ -1,15 +1,27 @@ package io.ebean; +import io.ebeaninternal.api.SpiEbeanServer; +import org.junit.After; import org.junit.Test; import org.tests.model.basic.Customer; import org.tests.model.basic.ResetBasicData; +import java.time.Clock; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; public class ExtendedServerTest extends BaseTestCase { + @After + public void cleanup() { + ((SpiEbeanServer) Ebean.getDefaultServer()) + .getServerConfig() + .setClock(Clock.systemUTC()); + } + @Test public void findList() { @@ -38,4 +50,45 @@ public class ExtendedServerTest extends BaseTestCase { } } + @Test + public void mockClock() { + SpiEbeanServer server = (SpiEbeanServer) Ebean.getDefaultServer(); + final Instant snapshot = Instant.now(); + Instant backedSnapshot = snapshot.minus(1, ChronoUnit.DAYS); + Clock snapshotClock = Clock.fixed(backedSnapshot, Clock.systemUTC().getZone()); + server.getServerConfig().setClock(snapshotClock); + + ResetBasicData.reset(); + + int count = server + .find(Customer.class) + .where() + .gt("cretime", snapshot) + .findCount(); + assertThat(count).isEqualTo(0); + + int count2 = server + .find(Customer.class) + .where() + .ge("cretime", backedSnapshot) + .findCount(); + assertThat(count2).isGreaterThan(0); + + int count3 = server + .find(Customer.class) + .where() + .gt("updtime", snapshot) + .findCount(); + assertThat(count3).isEqualTo(0); + + int count4 = server + .find(Customer.class) + .where() + .ge("updtime", backedSnapshot) + .findCount(); + assertThat(count4).isGreaterThan(0); + + + } + }