2828
2929import org .hibernate .testing .jdbc .SQLStatementInspector ;
3030import org .hibernate .testing .orm .transaction .TransactionUtil ;
31+ import org .junit .jupiter .api .extension .AfterAllCallback ;
32+ import org .junit .jupiter .api .extension .AfterEachCallback ;
3133import org .junit .jupiter .api .extension .BeforeEachCallback ;
3234import org .junit .jupiter .api .extension .ExtensionContext ;
3335import org .junit .jupiter .api .extension .TestExecutionExceptionHandler ;
4648 * @author Steve Ebersole
4749 */
4850public 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