Skip to content

Commit ea9ba16

Browse files
committed
HHH-19912 Add automatic test data cleanup feature to @SessionFactory annotation
1 parent 8a56476 commit ea9ba16

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
/**
8+
* Enumeration of when to drop test data automatically.
9+
*
10+
* @author inpink
11+
*/
12+
public enum DropDataTiming {
13+
/**
14+
* Never drop test data automatically
15+
*/
16+
NEVER,
17+
18+
/**
19+
* Drop test data before each test method
20+
*/
21+
BEFORE_EACH,
22+
23+
/**
24+
* Drop test data after each test method
25+
*/
26+
AFTER_EACH,
27+
28+
/**
29+
* Drop test data before all test methods (once per test class)
30+
*/
31+
BEFORE_ALL,
32+
33+
/**
34+
* Drop test data after all test methods (once per test class)
35+
*/
36+
AFTER_ALL
37+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/**
2121
* @author Steve Ebersole
22+
* @author inpink
2223
*/
2324
@Inherited
2425
@Target({ElementType.TYPE, ElementType.METHOD})
@@ -56,4 +57,11 @@
5657
boolean useCollectingStatementInspector() default false;
5758

5859
boolean applyCollectionsInDefaultFetchGroup() default true;
60+
61+
/**
62+
* When to automatically drop test data.
63+
*
64+
* @return the timing for dropping test data
65+
*/
66+
DropDataTiming dropTestData() default DropDataTiming.NEVER;
5967
}

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
2323
import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.ActionGrouping;
2424
import org.jboss.logging.Logger;
25+
import org.junit.jupiter.api.extension.AfterAllCallback;
26+
import org.junit.jupiter.api.extension.AfterEachCallback;
27+
import org.junit.jupiter.api.extension.BeforeAllCallback;
2528
import org.junit.jupiter.api.extension.BeforeEachCallback;
2629
import org.junit.jupiter.api.extension.ExtensionContext;
2730
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
@@ -43,9 +46,11 @@
4346
* @see DomainModelExtension
4447
*
4548
* @author Steve Ebersole
49+
* @author inpink
4650
*/
4751
public class SessionFactoryExtension
48-
implements TestInstancePostProcessor, BeforeEachCallback, TestExecutionExceptionHandler {
52+
implements TestInstancePostProcessor, BeforeAllCallback, BeforeEachCallback,
53+
AfterEachCallback, AfterAllCallback, TestExecutionExceptionHandler {
4954

5055
private static final Logger log = Logger.getLogger( SessionFactoryExtension.class );
5156
private static final String SESSION_FACTORY_KEY = SessionFactoryScope.class.getName();
@@ -83,6 +88,8 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
8388

8489
@Override
8590
public void beforeEach(ExtensionContext context) {
91+
handleDropData(context, DropDataTiming.BEFORE_EACH);
92+
8693
final Optional<SessionFactory> sfAnnRef = AnnotationSupport.findAnnotation(
8794
context.getRequiredTestMethod(),
8895
SessionFactory.class
@@ -231,6 +238,42 @@ public void handleTestExecutionException(ExtensionContext context, Throwable thr
231238
throw throwable;
232239
}
233240

241+
@Override
242+
public void beforeAll(ExtensionContext context) throws Exception {
243+
handleDropData(context, DropDataTiming.BEFORE_ALL);
244+
}
245+
246+
@Override
247+
public void afterEach(ExtensionContext context) throws Exception {
248+
handleDropData(context, DropDataTiming.AFTER_EACH);
249+
}
250+
251+
@Override
252+
public void afterAll(ExtensionContext context) throws Exception {
253+
handleDropData(context, DropDataTiming.AFTER_ALL);
254+
}
255+
256+
private void handleDropData(ExtensionContext context, DropDataTiming timing) {
257+
try {
258+
final Object testInstance = context.getRequiredTestInstance();
259+
final SessionFactoryScope scope = findSessionFactoryScope(testInstance, context);
260+
261+
final Optional<SessionFactory> sfAnnRef = AnnotationSupport.findAnnotation(
262+
context.getRequiredTestClass(),
263+
SessionFactory.class
264+
);
265+
266+
if (sfAnnRef.isPresent()) {
267+
DropDataTiming configuredTiming = sfAnnRef.get().dropTestData();
268+
if (configuredTiming == timing) {
269+
scope.dropData();
270+
}
271+
}
272+
} catch (Exception e) {
273+
log.debugf("Failed to drop data at timing %s: %s", timing, e.getMessage());
274+
}
275+
}
276+
234277
private static class SessionFactoryScopeImpl implements SessionFactoryScope, AutoCloseable {
235278
private final DomainModelScope modelScope;
236279
private final SessionFactoryProducer producer;

0 commit comments

Comments
 (0)