Skip to content

Commit e18b59a

Browse files
authored
Remove deprecated ExtendedServer.setClock() migrate to DatabaseBuilder.clock() (#3566)
So we can no longer dynamically change the Clock being used and instead for testing purposes need to create the Database with a test clock instance as needed. Refer to the ExtendedServerTest for an example
1 parent cc48dce commit e18b59a

File tree

14 files changed

+63
-148
lines changed

14 files changed

+63
-148
lines changed

ebean-api/src/main/java/io/ebean/Database.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -772,18 +772,6 @@ static DatabaseBuilder builder() {
772772
*/
773773
<T> T reference(Class<T> beanType, Object id);
774774

775-
/**
776-
* Return the extended API for Database.
777-
* <p>
778-
* The extended API has the options for executing queries that take an explicit
779-
* transaction as an argument.
780-
* <p>
781-
* Typically, we only need to use the extended API when we do NOT want to use the
782-
* usual ThreadLocal based mechanism to obtain the current transaction but instead
783-
* supply the transaction explicitly.
784-
*/
785-
ExtendedServer extended();
786-
787775
/**
788776
* Either Insert or Update the bean depending on its state.
789777
* <p>

ebean-api/src/main/java/io/ebean/ExtendedServer.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

ebean-core/src/main/java/io/ebeaninternal/api/SpiEbeanServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* Service Provider extension to EbeanServer.
2828
*/
29-
public interface SpiEbeanServer extends SpiServer, ExtendedServer, BeanCollectionLoader {
29+
public interface SpiEbeanServer extends SpiServer, BeanCollectionLoader {
3030

3131
/**
3232
* Return the NOW time from the Clock.

ebean-core/src/main/java/io/ebeaninternal/server/core/ClockService.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
8383
private final QueryPlanManager queryPlanManager;
8484
private final ExtraMetrics extraMetrics;
8585
private final DataTimeZone dataTimeZone;
86-
private final ClockService clockService;
86+
private final Clock clock;
8787
private final CallOriginFactory callStackFactory;
8888
private final Persister persister;
8989
private final OrmQueryEngine queryEngine;
@@ -151,7 +151,7 @@ public DefaultServer(InternalConfiguration config, ServerCacheManager cache) {
151151
this.beanLoader = new DefaultBeanLoader(this);
152152
this.jsonContext = config.createJsonContext(this);
153153
this.dataTimeZone = config.getDataTimeZone();
154-
this.clockService = config.getClockService();
154+
this.clock = config.clock();
155155

156156
DocStoreIntegration docStoreComponents = config.createDocStoreIntegration(this);
157157
this.transactionManager = config.createTransactionManager(this, docStoreComponents.updateProcessor());
@@ -413,19 +413,9 @@ public String name() {
413413
return serverName;
414414
}
415415

416-
@Override
417-
public ExtendedServer extended() {
418-
return this;
419-
}
420-
421416
@Override
422417
public long clockNow() {
423-
return clockService.nowMillis();
424-
}
425-
426-
@Override
427-
public void setClock(Clock clock) {
428-
this.clockService.setClock(clock);
418+
return clock.millis();
429419
}
430420

431421
@Override

ebean-core/src/main/java/io/ebeaninternal/server/core/InternalConfiguration.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import io.ebeanservice.docstore.api.DocStoreUpdateProcessor;
5656
import io.ebeanservice.docstore.none.NoneDocStoreFactory;
5757

58+
import java.time.Clock;
5859
import java.util.*;
5960

6061
import static java.lang.System.Logger.Level.*;
@@ -75,7 +76,7 @@ public final class InternalConfiguration {
7576
private final DeployInherit deployInherit;
7677
private final TypeManager typeManager;
7778
private final DtoBeanManager dtoBeanManager;
78-
private final ClockService clockService;
79+
private final Clock clock;
7980
private final DataTimeZone dataTimeZone;
8081
private final Binder binder;
8182
private final DeployCreateProperties deployCreateProperties;
@@ -103,7 +104,7 @@ public final class InternalConfiguration {
103104
this.online = online;
104105
this.config = config;
105106
this.jacksonCorePresent = config.getClassLoadConfig().isJacksonCorePresent();
106-
this.clockService = new ClockService(config.settings().getClock());
107+
this.clock = config.settings().getClock();
107108
this.tableModState = new TableModState();
108109
this.logManager = initLogManager();
109110
this.docStoreFactory = initDocStoreFactory(service(DocStoreFactory.class));
@@ -189,8 +190,8 @@ public DocStoreFactory getDocStoreFactory() {
189190
return docStoreFactory;
190191
}
191192

192-
ClockService getClockService() {
193-
return clockService;
193+
Clock clock() {
194+
return clock;
194195
}
195196

196197
public ExtraMetrics getExtraMetrics() {
@@ -391,7 +392,7 @@ TransactionManager createTransactionManager(SpiServer server, DocStoreUpdateProc
391392
TransactionManagerOptions options =
392393
new TransactionManagerOptions(server, notifyL2CacheInForeground, config, scopeManager, clusterManager, backgroundExecutor,
393394
indexUpdateProcessor, beanDescriptorManager, dataSource(), profileHandler(), logManager,
394-
tableModState, cacheNotify, clockService);
395+
tableModState, cacheNotify);
395396

396397
if (config.isDocStoreOnly()) {
397398
return new DocStoreTransactionManager(options);

ebean-core/src/main/java/io/ebeaninternal/server/transaction/TransactionManagerOptions.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import io.ebeaninternal.api.SpiLogManager;
88
import io.ebeaninternal.api.SpiProfileHandler;
99
import io.ebeaninternal.server.cluster.ClusterManager;
10-
import io.ebeaninternal.server.core.ClockService;
1110
import io.ebeaninternal.server.deploy.BeanDescriptorManager;
1211
import io.ebeanservice.docstore.api.DocStoreUpdateProcessor;
1312

@@ -30,13 +29,11 @@ public final class TransactionManagerOptions {
3029
final SpiLogManager logManager;
3130
final TableModState tableModState;
3231
final ServerCacheNotify cacheNotify;
33-
final ClockService clockService;
34-
3532

3633
public TransactionManagerOptions(SpiServer server, boolean notifyL2CacheInForeground, DatabaseBuilder.Settings config, TransactionScopeManager scopeManager,
3734
ClusterManager clusterManager, BackgroundExecutor backgroundExecutor, DocStoreUpdateProcessor docStoreUpdateProcessor,
3835
BeanDescriptorManager descMgr, DataSourceSupplier dataSourceSupplier, SpiProfileHandler profileHandler,
39-
SpiLogManager logManager, TableModState tableModState, ServerCacheNotify cacheNotify, ClockService clockService) {
36+
SpiLogManager logManager, TableModState tableModState, ServerCacheNotify cacheNotify) {
4037
this.server = server;
4138
this.notifyL2CacheInForeground = notifyL2CacheInForeground;
4239
this.config = config;
@@ -50,7 +47,6 @@ public TransactionManagerOptions(SpiServer server, boolean notifyL2CacheInForegr
5047
this.logManager = logManager;
5148
this.tableModState = tableModState;
5249
this.cacheNotify = cacheNotify;
53-
this.clockService = clockService;
5450
}
5551

5652
}
Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package io.ebean.xtest.base;
22

33
import io.ebean.*;
4+
import io.ebean.annotation.Platform;
45
import io.ebean.xtest.BaseTestCase;
5-
import org.junit.jupiter.api.AfterEach;
6+
import io.ebean.xtest.ForPlatform;
67
import org.junit.jupiter.api.Test;
78
import org.tests.model.basic.Customer;
9+
import org.tests.model.basic.EBasicVer;
810
import org.tests.model.basic.ResetBasicData;
911

1012
import java.time.Clock;
@@ -14,18 +16,10 @@
1416

1517
import static org.assertj.core.api.Assertions.assertThat;
1618

17-
public class ExtendedServerTest extends BaseTestCase {
18-
19-
@AfterEach
20-
public void cleanup() {
21-
DB.getDefault()
22-
.extended()
23-
.setClock(Clock.systemUTC());
24-
}
19+
class ExtendedServerTest extends BaseTestCase {
2520

2621
@Test
27-
public void findList() {
28-
22+
void findList() {
2923
ResetBasicData.reset();
3024

3125
Database server = DB.getDefault();
@@ -51,47 +45,57 @@ public void findList() {
5145
}
5246
}
5347

48+
@ForPlatform(Platform.H2)
5449
@Test
55-
public void mockClock() {
56-
57-
Database database = DB.getDefault();
58-
final Instant snapshot = Instant.now();
59-
Instant backedSnapshot = snapshot.minus(1, ChronoUnit.DAYS);
50+
void fixedClock() {
51+
final Instant now = Instant.now();
52+
Instant backedSnapshot = now.minus(1, ChronoUnit.DAYS);
6053
Clock snapshotClock = Clock.fixed(backedSnapshot, Clock.systemUTC().getZone());
6154

62-
database.extended().setClock(snapshotClock);
63-
64-
ResetBasicData.reset();
65-
66-
int count = database
67-
.find(Customer.class)
55+
Database db = Database.builder()
56+
.name("db")
57+
.loadFromProperties()
58+
.clock(snapshotClock)
59+
.addClass(EBasicVer.class)
60+
.ddlExtra(false)
61+
.name("fixed-clock-db")
62+
.register(false)
63+
.defaultDatabase(false)
64+
.build();
65+
66+
EBasicVer e0 = new EBasicVer("CheckClock");
67+
db.save(e0);
68+
69+
EBasicVer found = db.find(EBasicVer.class, e0.getId());
70+
assertThat(found).isNotNull();
71+
72+
int count = db
73+
.find(EBasicVer.class)
6874
.where()
69-
.gt("cretime", snapshot)
75+
.gt("lastUpdate", now)
7076
.findCount();
7177
assertThat(count).isEqualTo(0);
7278

73-
int count2 = database
74-
.find(Customer.class)
79+
int count2 = db
80+
.find(EBasicVer.class)
7581
.where()
76-
.ge("cretime", backedSnapshot)
82+
.ge("lastUpdate", snapshotClock.instant().minusSeconds(60))
7783
.findCount();
78-
assertThat(count2).isGreaterThan(0);
84+
assertThat(count2).isEqualTo(1);
7985

80-
int count3 = database
81-
.find(Customer.class)
86+
int count3 = db
87+
.find(EBasicVer.class)
8288
.where()
83-
.gt("updtime", snapshot)
89+
.gt("lastUpdate", now)
8490
.findCount();
8591
assertThat(count3).isEqualTo(0);
8692

87-
int count4 = database
88-
.find(Customer.class)
93+
int count4 = db
94+
.find(EBasicVer.class)
8995
.where()
90-
.ge("updtime", backedSnapshot)
96+
.ge("lastUpdate", backedSnapshot.minus(2, ChronoUnit.DAYS))
9197
.findCount();
92-
assertThat(count4).isGreaterThan(0);
93-
94-
98+
assertThat(count4).isEqualTo(1);
9599
}
96100

97101
}

ebean-test/src/test/java/io/ebean/xtest/internal/api/TDSpiEbeanServer.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,11 @@ public TDSpiEbeanServer(String name) {
5050
this.name = name;
5151
}
5252

53-
@Override
54-
public ExtendedServer extended() {
55-
return this;
56-
}
57-
5853
@Override
5954
public long clockNow() {
6055
return System.currentTimeMillis();
6156
}
6257

63-
@Override
64-
public void setClock(Clock clock) {
65-
}
66-
6758
@Override
6859
public boolean isDisableL2Cache() {
6960
return false;

ebean-test/src/test/java/io/ebean/xtest/internal/api/TDSpiServer.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,6 @@ public <T> T reference(Class<T> beanType, Object id) {
253253
return null;
254254
}
255255

256-
@Override
257-
public ExtendedServer extended() {
258-
return null;
259-
}
260-
261256
@Override
262257
public void save(Object bean) throws OptimisticLockException {
263258

0 commit comments

Comments
 (0)