mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Replaceable clock (#1428)
ENH: Use java.time.Clock for @WhenModified and @WhenCreated and allow it to be Replaceable for testing
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user