|
22 | 22 | import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator; |
23 | 23 | import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.ActionGrouping; |
24 | 24 | 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; |
25 | 28 | import org.junit.jupiter.api.extension.BeforeEachCallback; |
26 | 29 | import org.junit.jupiter.api.extension.ExtensionContext; |
27 | 30 | import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; |
|
43 | 46 | * @see DomainModelExtension |
44 | 47 | * |
45 | 48 | * @author Steve Ebersole |
| 49 | + * @author inpink |
46 | 50 | */ |
47 | 51 | public class SessionFactoryExtension |
48 | | - implements TestInstancePostProcessor, BeforeEachCallback, TestExecutionExceptionHandler { |
| 52 | + implements TestInstancePostProcessor, BeforeAllCallback, BeforeEachCallback, |
| 53 | + AfterEachCallback, AfterAllCallback, TestExecutionExceptionHandler { |
49 | 54 |
|
50 | 55 | private static final Logger log = Logger.getLogger( SessionFactoryExtension.class ); |
51 | 56 | private static final String SESSION_FACTORY_KEY = SessionFactoryScope.class.getName(); |
@@ -83,6 +88,8 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex |
83 | 88 |
|
84 | 89 | @Override |
85 | 90 | public void beforeEach(ExtensionContext context) { |
| 91 | + handleDropData(context, DropDataTiming.BEFORE_EACH); |
| 92 | + |
86 | 93 | final Optional<SessionFactory> sfAnnRef = AnnotationSupport.findAnnotation( |
87 | 94 | context.getRequiredTestMethod(), |
88 | 95 | SessionFactory.class |
@@ -231,6 +238,42 @@ public void handleTestExecutionException(ExtensionContext context, Throwable thr |
231 | 238 | throw throwable; |
232 | 239 | } |
233 | 240 |
|
| 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 | + |
234 | 277 | private static class SessionFactoryScopeImpl implements SessionFactoryScope, AutoCloseable { |
235 | 278 | private final DomainModelScope modelScope; |
236 | 279 | private final SessionFactoryProducer producer; |
|
0 commit comments