mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 03:57:45 +00:00
#1428 - ENH: Use java.time.Clock for @WhenModified and @WhenCreated and allow it to be Replaceable for testing
Adjust such that Clock is on DefaultServer and can be set via ExtendedServer interface
This commit is contained in:
@@ -3,6 +3,7 @@ package io.ebean;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.persistence.NonUniqueResultException;
|
||||
import java.time.Clock;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -28,6 +29,19 @@ import java.util.function.Predicate;
|
||||
*/
|
||||
public interface ExtendedServer {
|
||||
|
||||
/**
|
||||
* Return the NOW time from the Clock.
|
||||
*/
|
||||
long clockNow();
|
||||
|
||||
/**
|
||||
* Set the Clock to use for <code>@WhenCreated</code> and <code>@WhenModified</code>.
|
||||
* <p>
|
||||
* Note that we only expect to change the Clock for testing purposes.
|
||||
* </p>
|
||||
*/
|
||||
void setClock(Clock clock);
|
||||
|
||||
/**
|
||||
* Return the number of 'top level' or 'root' entities this query should return.
|
||||
*
|
||||
@@ -370,7 +384,7 @@ public interface ExtendedServer {
|
||||
/**
|
||||
* Execute the update query returning the number of rows updated.
|
||||
* <p>
|
||||
* The update query must be created using {@link #update(Class)}.
|
||||
* The update query must be created using {@link EbeanServer#update(Class)}.
|
||||
* </p>
|
||||
*
|
||||
* @param query the update query to execute
|
||||
|
||||
@@ -119,10 +119,10 @@ import javax.persistence.NonUniqueResultException;
|
||||
import javax.persistence.OptimisticLockException;
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.sql.DataSource;
|
||||
import java.time.Clock;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -151,6 +151,11 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
|
||||
|
||||
private final DataTimeZone dataTimeZone;
|
||||
|
||||
/**
|
||||
* Clock to use for WhenModified and WhenCreated.
|
||||
*/
|
||||
private Clock clock;
|
||||
|
||||
private final CallStackFactory callStackFactory;
|
||||
|
||||
/**
|
||||
@@ -278,6 +283,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
|
||||
this.beanLoader = new DefaultBeanLoader(this);
|
||||
this.jsonContext = config.createJsonContext(this);
|
||||
this.dataTimeZone = config.getDataTimeZone();
|
||||
this.clock = config.getServerConfig().getClock();
|
||||
|
||||
DocStoreIntegration docStoreComponents = config.createDocStoreIntegration(this);
|
||||
this.transactionManager = config.createTransactionManager(docStoreComponents.updateProcessor());
|
||||
@@ -507,6 +513,16 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long clockNow() {
|
||||
return clock.millis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClock(Clock clock) {
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BeanState getBeanState(Object bean) {
|
||||
if (bean instanceof EntityBean) {
|
||||
|
||||
@@ -1275,7 +1275,7 @@ public final class PersistRequestBean<T> extends PersistRequest implements BeanP
|
||||
*/
|
||||
public long now() {
|
||||
if (now == 0) {
|
||||
now = ebeanServer.getServerConfig().getClock().millis();
|
||||
now = ebeanServer.clockNow();
|
||||
}
|
||||
return now;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package io.ebean;
|
||||
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
@@ -17,8 +16,8 @@ public class ExtendedServerTest extends BaseTestCase {
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
((SpiEbeanServer) Ebean.getDefaultServer())
|
||||
.getServerConfig()
|
||||
Ebean.getDefaultServer()
|
||||
.extended()
|
||||
.setClock(Clock.systemUTC());
|
||||
}
|
||||
|
||||
@@ -52,11 +51,13 @@ public class ExtendedServerTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void mockClock() {
|
||||
SpiEbeanServer server = (SpiEbeanServer) Ebean.getDefaultServer();
|
||||
|
||||
EbeanServer server = 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);
|
||||
|
||||
server.extended().setClock(snapshotClock);
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
|
||||
@@ -55,6 +55,7 @@ import io.ebeaninternal.server.transaction.RemoteTransactionEvent;
|
||||
import javax.persistence.OptimisticLockException;
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.Clock;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -85,6 +86,16 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long clockNow() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClock(Clock clock) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiLogManager log() {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user