Skip to content

Commit fd4ac92

Browse files
committed
Add options to truncate data and evict cache in tests via SessionFactory extension
1 parent 562c5c6 commit fd4ac92

File tree

4 files changed

+70
-16
lines changed

4 files changed

+70
-16
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/proxy/concrete/ConcreteProxyToOneSecondLevelCacheTest.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@
2222
import org.hibernate.cfg.AvailableSettings;
2323
import org.hibernate.stat.Statistics;
2424
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
25+
import org.hibernate.testing.orm.junit.ClearMode;
2526
import org.hibernate.testing.orm.junit.DomainModel;
2627
import org.hibernate.testing.orm.junit.Jira;
2728
import org.hibernate.testing.orm.junit.ServiceRegistry;
2829
import org.hibernate.testing.orm.junit.SessionFactory;
2930
import org.hibernate.testing.orm.junit.SessionFactoryScope;
3031
import org.hibernate.testing.orm.junit.Setting;
31-
import org.junit.jupiter.api.AfterAll;
3232
import org.junit.jupiter.api.BeforeAll;
33-
import org.junit.jupiter.api.BeforeEach;
3433
import org.junit.jupiter.api.Test;
3534

3635
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,7 +42,7 @@
4342
@Setting(name = AvailableSettings.GENERATE_STATISTICS, value = "true"),
4443
@Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true")
4544
})
46-
@SessionFactory
45+
@SessionFactory(dropData = ClearMode.AFTER_ALL, clearCache = ClearMode.BEFORE_EACH)
4746
@BytecodeEnhanced(runNotEnhancedAsWell = true)
4847
@Jira("https://hibernate.atlassian.net/browse/HHH-18872")
4948
public class ConcreteProxyToOneSecondLevelCacheTest {
@@ -144,11 +143,6 @@ private static void assertCacheStats(final Statistics stats, final long hits, fi
144143
assertThat( stats.getSecondLevelCachePutCount() ).isEqualTo( puts );
145144
}
146145

147-
@BeforeEach
148-
public void clearCache(SessionFactoryScope scope) {
149-
scope.getSessionFactory().getCache().evictAllRegions();
150-
}
151-
152146
@BeforeAll
153147
public void setUp(SessionFactoryScope scope) {
154148
scope.inTransaction( session -> {
@@ -160,11 +154,6 @@ public void setUp(SessionFactoryScope scope) {
160154
} );
161155
}
162156

163-
@AfterAll
164-
public void tearDown(SessionFactoryScope scope) {
165-
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
166-
}
167-
168157
@Entity(name = "TestNode")
169158
@Cacheable
170159
@ConcreteProxy
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.testing.orm.junit;
6+
7+
import org.hibernate.Incubating;
8+
9+
@Incubating
10+
public enum ClearMode {
11+
BEFORE_EACH, AFTER_EACH, AFTER_ALL, NEVER
12+
}

hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactory.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.lang.annotation.RetentionPolicy;
1111
import java.lang.annotation.Target;
1212

13+
import org.hibernate.Incubating;
1314
import org.hibernate.Interceptor;
1415
import org.hibernate.resource.jdbc.spi.StatementInspector;
1516

@@ -56,4 +57,10 @@
5657
boolean useCollectingStatementInspector() default false;
5758

5859
boolean applyCollectionsInDefaultFetchGroup() default true;
60+
61+
@Incubating
62+
ClearMode dropData() default ClearMode.NEVER;
63+
@Incubating
64+
ClearMode clearCache() default ClearMode.NEVER;
65+
5966
}

hibernate-testing/src/main/java/org/hibernate/testing/orm/junit/SessionFactoryExtension.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import org.hibernate.testing.jdbc.SQLStatementInspector;
3030
import org.hibernate.testing.orm.transaction.TransactionUtil;
31+
import org.junit.jupiter.api.extension.AfterAllCallback;
32+
import org.junit.jupiter.api.extension.AfterEachCallback;
3133
import org.junit.jupiter.api.extension.BeforeEachCallback;
3234
import org.junit.jupiter.api.extension.ExtensionContext;
3335
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
@@ -46,7 +48,8 @@
4648
* @author Steve Ebersole
4749
*/
4850
public class SessionFactoryExtension
49-
implements TestInstancePostProcessor, BeforeEachCallback, TestExecutionExceptionHandler {
51+
implements TestInstancePostProcessor, BeforeEachCallback, TestExecutionExceptionHandler,
52+
AfterEachCallback, AfterAllCallback {
5053

5154
private static final Logger log = Logger.getLogger( SessionFactoryExtension.class );
5255
private static final String SESSION_FACTORY_KEY = SessionFactoryScope.class.getName();
@@ -92,13 +95,16 @@ public void beforeEach(ExtensionContext context) {
9295
if ( sfAnnRef.isEmpty() ) {
9396
// assume the annotations are defined on the class-level...
9497
// will be validated by the parameter-resolver or SFS-extension
98+
cleanup( context, ClearMode.BEFORE_EACH );
9599
return;
96100
}
97101

98102
final DomainModelScope domainModelScope = DomainModelExtension.resolveForMethodLevelSessionFactoryScope( context );
99103
final SessionFactoryScope created = createSessionFactoryScope( context.getRequiredTestInstance(), sfAnnRef, domainModelScope, context );
100104
final ExtensionContext.Store extensionStore = locateExtensionStore( context.getRequiredTestInstance(), context );
101105
extensionStore.put( SESSION_FACTORY_KEY, created );
106+
107+
cleanup( context, ClearMode.BEFORE_EACH );
102108
}
103109

104110
private static ExtensionContext.Store locateExtensionStore(Object testInstance, ExtensionContext context) {
@@ -111,6 +117,8 @@ private static SessionFactoryScopeImpl createSessionFactoryScope(
111117
DomainModelScope domainModelScope,
112118
ExtensionContext context) {
113119
SessionFactoryProducer producer = null;
120+
ClearMode dropDataMode = ClearMode.NEVER;
121+
ClearMode clearCacheMode = ClearMode.NEVER;
114122

115123
if ( testInstance instanceof SessionFactoryProducer ) {
116124
producer = (SessionFactoryProducer) testInstance;
@@ -122,6 +130,8 @@ private static SessionFactoryScopeImpl createSessionFactoryScope(
122130

123131
if ( sfAnnRef.isPresent() ) {
124132
final SessionFactory sessionFactoryConfig = sfAnnRef.get();
133+
dropDataMode = sessionFactoryConfig.dropData();
134+
clearCacheMode = sessionFactoryConfig.clearCache();
125135

126136
producer = model -> {
127137
try {
@@ -166,7 +176,7 @@ else if ( ! explicitInspectorClass.equals( StatementInspector.class ) ) {
166176
throw new IllegalStateException( "Could not determine SessionFactory producer" );
167177
}
168178

169-
final SessionFactoryScopeImpl sfScope = new SessionFactoryScopeImpl( domainModelScope, producer );
179+
final SessionFactoryScopeImpl sfScope = new SessionFactoryScopeImpl( domainModelScope, producer, dropDataMode, clearCacheMode );
170180

171181
if ( testInstance instanceof SessionFactoryScopeAware ) {
172182
( (SessionFactoryScopeAware) testInstance ).injectSessionFactoryScope( sfScope );
@@ -232,18 +242,42 @@ public void handleTestExecutionException(ExtensionContext context, Throwable thr
232242
throw throwable;
233243
}
234244

245+
@Override
246+
public void afterAll(ExtensionContext context) {
247+
cleanup( context, ClearMode.AFTER_ALL );
248+
}
249+
250+
@Override
251+
public void afterEach(ExtensionContext context) {
252+
cleanup( context, ClearMode.AFTER_EACH );
253+
}
254+
255+
private void cleanup(ExtensionContext context, ClearMode dropDataMode) {
256+
final Object testInstance = context.getRequiredTestInstance();
257+
final ExtensionContext.Store store = locateExtensionStore( testInstance, context );
258+
final SessionFactoryScopeImpl scope = (SessionFactoryScopeImpl) store.get( SESSION_FACTORY_KEY );
259+
if (scope != null ) {
260+
scope.dropData( dropDataMode );
261+
scope.clearCache( dropDataMode );
262+
}
263+
}
264+
235265
private static class SessionFactoryScopeImpl implements SessionFactoryScope, ExtensionContext.Store.CloseableResource {
236266
private final DomainModelScope modelScope;
237267
private final SessionFactoryProducer producer;
268+
private final ClearMode dropDataMode;
269+
private final ClearMode clearCacheMode;
238270

239271
private SessionFactoryImplementor sessionFactory;
240272
private boolean active = true;
241273

242274
private SessionFactoryScopeImpl(
243275
DomainModelScope modelScope,
244-
SessionFactoryProducer producer) {
276+
SessionFactoryProducer producer, ClearMode dropDataMode, ClearMode clearCacheMode) {
245277
this.modelScope = modelScope;
246278
this.producer = producer;
279+
this.dropDataMode = dropDataMode;
280+
this.clearCacheMode = clearCacheMode;
247281
}
248282

249283
@Override
@@ -416,5 +450,17 @@ public void dropData() {
416450
sessionFactory.getSchemaManager().truncateMappedObjects();
417451
}
418452
}
453+
454+
void dropData(ClearMode dropDataMode) {
455+
if ( this.dropDataMode == dropDataMode ) {
456+
dropData();
457+
}
458+
}
459+
460+
void clearCache(ClearMode clearCacheMode) {
461+
if ( this.clearCacheMode == clearCacheMode ) {
462+
sessionFactory.getCache().evictAllRegions();
463+
}
464+
}
419465
}
420466
}

0 commit comments

Comments
 (0)