diff --git a/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/testsupport/util/extension/SearchSetupHelper.java b/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/testsupport/util/extension/SearchSetupHelper.java index 39a9c015c52..b9be6d5b26a 100644 --- a/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/testsupport/util/extension/SearchSetupHelper.java +++ b/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/testsupport/util/extension/SearchSetupHelper.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -43,26 +44,24 @@ import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingInitiator; import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingKey; import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingSchemaManagementStrategy; +import org.hibernate.search.util.impl.test.extension.AbstractScopeTrackingExtension; +import org.hibernate.search.util.impl.test.extension.ExtensionScope; import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterEachCallback; -import org.junit.jupiter.api.extension.BeforeAllCallback; -import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; public class SearchSetupHelper - implements AfterAllCallback, AfterEachCallback, BeforeAllCallback, BeforeEachCallback, TestExecutionExceptionHandler { + extends AbstractScopeTrackingExtension + implements AfterAllCallback, AfterEachCallback, TestExecutionExceptionHandler { private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() ); private final TestConfigurationProvider configurationProvider = new TestConfigurationProvider(); private TckBackendSetupStrategy setupStrategy; - private final List environments = new ArrayList<>(); - private final List integrationPartialBuildStates = new ArrayList<>(); - private final List mappings = new ArrayList<>(); private TckBackendAccessor backendAccessor; - private boolean callOncePerClass = false; + private final Map scopeContexts = new HashMap<>(); public static SearchSetupHelper create() { return new SearchSetupHelper(); @@ -113,10 +112,14 @@ public TckBackendAccessor getBackendAccessor() { return backendAccessor; } + private Context currentContext() { + return scopeContexts.get( currentScope() ); + } + @Override public void afterAll(ExtensionContext context) throws Exception { configurationProvider.afterAll( context ); - if ( !runningInNestedContext( context ) && callOncePerClass ) { + if ( !runningInNestedContext( context ) ) { cleanUp(); } } @@ -124,26 +127,21 @@ public void afterAll(ExtensionContext context) throws Exception { @Override public void afterEach(ExtensionContext context) throws Exception { configurationProvider.afterEach( context ); - if ( !callOncePerClass ) { - cleanUp(); - } + cleanUp(); } @Override - public void beforeAll(ExtensionContext context) throws Exception { + protected void actualBeforeAll(ExtensionContext context) { if ( !runningInNestedContext( context ) ) { - // BeforeAll callback can be called if an extension - // is added in a static context, i.e. @RegisterExtension static MyExtension = ....; - // Or when the test class is annotated with @TestInstance(TestInstance.Lifecycle.PER_CLASS) - // which also implies that we'd want to have only a BeforeAll, AfterAll callbacks: - callOncePerClass = true; configurationProvider.beforeAll( context ); + scopeContexts.put( ExtensionScope.CLASS, new Context() ); } } @Override - public void beforeEach(ExtensionContext context) throws Exception { + protected void actualBeforeEach(ExtensionContext context) { configurationProvider.beforeEach( context ); + scopeContexts.put( ExtensionScope.TEST, new Context() ); } @Override @@ -164,12 +162,13 @@ public void cleanUp() throws IOException { } private void cleanUp(Closer closer) { - closer.pushAll( StubMappingImpl::close, mappings ); - mappings.clear(); - closer.pushAll( SearchIntegrationPartialBuildState::closeOnFailure, integrationPartialBuildStates ); - integrationPartialBuildStates.clear(); - closer.pushAll( SearchIntegrationEnvironment::close, environments ); - environments.clear(); + Context context = currentContext(); + closer.pushAll( StubMappingImpl::close, context.mappings ); + context.mappings.clear(); + closer.pushAll( SearchIntegrationPartialBuildState::closeOnFailure, context.integrationPartialBuildStates ); + context.integrationPartialBuildStates.clear(); + closer.pushAll( SearchIntegrationEnvironment::close, context.environments ); + context.environments.clear(); closer.push( TckBackendAccessor::close, backendAccessor ); backendAccessor = null; } @@ -263,11 +262,12 @@ public PartialSetup setupFirstPhaseOnly() { } public PartialSetup setupFirstPhaseOnly(Optional previousMapping) { + Context context = currentContext(); SearchIntegrationEnvironment environment = SearchIntegrationEnvironment.builder( propertySource, unusedPropertyChecker ) .beanProvider( beanProvider ) .build(); - environments.add( environment ); + context.environments.add( environment ); SearchIntegration.Builder integrationBuilder = ( previousMapping.isPresent() ) ? previousMapping.get().integration().restartBuilder( environment ) @@ -280,7 +280,7 @@ public PartialSetup setupFirstPhaseOnly(Optional previousMapping) { integrationBuilder.addMappingInitiator( mappingKey, initiator ); SearchIntegrationPartialBuildState integrationPartialBuildState = integrationBuilder.prepareBuild(); - integrationPartialBuildStates.add( integrationPartialBuildState ); + context.integrationPartialBuildStates.add( integrationPartialBuildState ); return overrides -> { SearchIntegrationFinalizer finalizer = @@ -288,12 +288,12 @@ public PartialSetup setupFirstPhaseOnly(Optional previousMapping) { unusedPropertyChecker ); StubMappingImpl mapping = finalizer.finalizeMapping( mappingKey, - (context, partialMapping) -> partialMapping.finalizeMapping( schemaManagementStrategy ) + (ctx, partialMapping) -> partialMapping.finalizeMapping( schemaManagementStrategy ) ); - mappings.add( mapping ); + context.mappings.add( mapping ); finalizer.finalizeIntegration(); - integrationPartialBuildStates.remove( integrationPartialBuildState ); + context.integrationPartialBuildStates.remove( integrationPartialBuildState ); return mapping; }; @@ -326,4 +326,9 @@ private static boolean runningInNestedContext(ExtensionContext context) { return context.getRequiredTestClass().isMemberClass(); } + private static class Context { + private final List environments = new ArrayList<>(); + private final List integrationPartialBuildStates = new ArrayList<>(); + private final List mappings = new ArrayList<>(); + } } diff --git a/integrationtest/v5migrationhelper/engine/src/test/java/org/hibernate/search/testsupport/junit/SearchFactoryHolder.java b/integrationtest/v5migrationhelper/engine/src/test/java/org/hibernate/search/testsupport/junit/SearchFactoryHolder.java index e9460819144..50ab56524d2 100644 --- a/integrationtest/v5migrationhelper/engine/src/test/java/org/hibernate/search/testsupport/junit/SearchFactoryHolder.java +++ b/integrationtest/v5migrationhelper/engine/src/test/java/org/hibernate/search/testsupport/junit/SearchFactoryHolder.java @@ -15,7 +15,6 @@ import org.hibernate.search.spi.SearchIntegrator; import org.hibernate.search.testsupport.migration.V5MigrationStandalonePojoSearchIntegratorAdapter; -import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.BeforeEachCallback; @@ -29,7 +28,7 @@ * @author Sanne Grinovero * @since 4.1 */ -public class SearchFactoryHolder implements AfterAllCallback, BeforeAllCallback, BeforeEachCallback, AfterEachCallback { +public class SearchFactoryHolder implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback { private final V5MigrationHelperEngineSetupHelper setupHelper = V5MigrationHelperEngineSetupHelper.create(); @@ -38,7 +37,6 @@ public class SearchFactoryHolder implements AfterAllCallback, BeforeAllCallback, private SearchMapping mapping; private SearchIntegrator searchIntegrator; - private boolean callOncePerClass = false; public SearchFactoryHolder(Class... entities) { this.entities = entities; @@ -59,41 +57,22 @@ public SearchFactoryHolder withProperty(String key, Object value) { return this; } - @Override - public void afterAll(ExtensionContext extensionContext) throws Exception { - if ( callOncePerClass ) { - doAfter( extensionContext ); - } - } - @Override public void afterEach(ExtensionContext extensionContext) throws Exception { - if ( !callOncePerClass ) { - doAfter( extensionContext ); - } - } - - private void doAfter(ExtensionContext extensionContext) throws Exception { mapping = null; searchIntegrator = null; setupHelper.afterAll( extensionContext ); } @Override - public void beforeAll(ExtensionContext extensionContext) throws Exception { - callOncePerClass = true; - doBefore( extensionContext ); + public void beforeAll(ExtensionContext extensionContext) { + throw new IllegalStateException( + "SearchFactoryHolder is only available as nonstatic extension, i.e. @RegisterExtension SearchFactoryHolder searchFactoryHolder = new SearchFactoryHolder();" ); } @Override public void beforeEach(ExtensionContext extensionContext) throws Exception { - if ( !callOncePerClass ) { - doBefore( extensionContext ); - } - } - - private void doBefore(ExtensionContext extensionContext) throws Exception { - setupHelper.beforeAll( extensionContext ); + setupHelper.beforeEach( extensionContext ); mapping = setupHelper.start() .withProperties( configuration ) diff --git a/integrationtest/v5migrationhelper/orm/src/test/java/org/hibernate/search/test/testsupport/StaticIndexingSwitch.java b/integrationtest/v5migrationhelper/orm/src/test/java/org/hibernate/search/test/testsupport/StaticIndexingSwitch.java index 4a66c7985f2..78adaebdb2f 100644 --- a/integrationtest/v5migrationhelper/orm/src/test/java/org/hibernate/search/test/testsupport/StaticIndexingSwitch.java +++ b/integrationtest/v5migrationhelper/orm/src/test/java/org/hibernate/search/test/testsupport/StaticIndexingSwitch.java @@ -12,19 +12,16 @@ import org.hibernate.search.mapper.pojo.bridge.runtime.RoutingBridgeRouteContext; import org.hibernate.search.mapper.pojo.route.DocumentRoutes; -import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; -public class StaticIndexingSwitch - implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback, AfterAllCallback { +public class StaticIndexingSwitch implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback { private static StaticIndexingSwitch activeInstance = null; private boolean enabled = true; - private boolean callOncePerClass = false; public void enable(boolean enabled) { this.enabled = enabled; @@ -48,35 +45,19 @@ public static StaticIndexingSwitch activeSwitch() { } @Override - public void afterAll(ExtensionContext extensionContext) throws Exception { - if ( callOncePerClass ) { - activeInstance = null; - reset(); - } - } - - @Override - public void afterEach(ExtensionContext extensionContext) throws Exception { - if ( !callOncePerClass ) { - activeInstance = null; - reset(); - } + public void afterEach(ExtensionContext extensionContext) { + activeInstance = null; + reset(); } @Override - public void beforeAll(ExtensionContext extensionContext) throws Exception { - callOncePerClass = true; - doBefore(); + public void beforeAll(ExtensionContext extensionContext) { + throw new IllegalStateException( + "StaticIndexingSwitch is only available as nonstatic extension, i.e. @RegisterExtension StaticIndexingSwitch staticIndexingSwitch = new StaticIndexingSwitch();" ); } @Override - public void beforeEach(ExtensionContext extensionContext) throws Exception { - if ( !callOncePerClass ) { - doBefore(); - } - } - - private void doBefore() { + public void beforeEach(ExtensionContext extensionContext) { if ( activeInstance != null ) { throw new IllegalStateException( "Using StaticCounters twice in a single test is forbidden." + " Make sure you added one (and only one)" diff --git a/integrationtest/v5migrationhelper/orm/src/test/java/org/hibernate/search/test/util/FullTextSessionBuilder.java b/integrationtest/v5migrationhelper/orm/src/test/java/org/hibernate/search/test/util/FullTextSessionBuilder.java index 5e7a01f62d2..a0e7552a385 100644 --- a/integrationtest/v5migrationhelper/orm/src/test/java/org/hibernate/search/test/util/FullTextSessionBuilder.java +++ b/integrationtest/v5migrationhelper/orm/src/test/java/org/hibernate/search/test/util/FullTextSessionBuilder.java @@ -7,6 +7,7 @@ package org.hibernate.search.test.util; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -24,12 +25,12 @@ import org.hibernate.search.hcore.util.impl.ContextHelper; import org.hibernate.search.impl.ImplementationFactory; import org.hibernate.search.test.testsupport.V5MigrationHelperOrmSetupHelper; +import org.hibernate.search.util.impl.test.extension.AbstractScopeTrackingExtension; +import org.hibernate.search.util.impl.test.extension.ExtensionScope; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterEachCallback; -import org.junit.jupiter.api.extension.BeforeAllCallback; -import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; /** @@ -40,17 +41,14 @@ * @author Sanne Grinovero * @author Hardy Ferentschik */ -public class FullTextSessionBuilder - implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback, - AfterAllCallback { +public class FullTextSessionBuilder extends AbstractScopeTrackingExtension implements AfterEachCallback, AfterAllCallback { private final V5MigrationHelperOrmSetupHelper setupHelper = V5MigrationHelperOrmSetupHelper.create(); private final Map cfg = new HashMap<>(); private final Set> annotatedClasses = new HashSet<>(); - private SessionFactoryImplementor sessionFactory; + private final Map sessionFactory = new HashMap<>(); private final List additionalLoadEventListeners = new ArrayList<>(); - private boolean callOncePerClass = false; /** * Override before building any parameter, or add new ones. @@ -81,10 +79,10 @@ public FullTextSessionBuilder addAnnotatedClass(Class annotatedClass) { * @return a new FullTextSession based upon the built configuration. */ public FullTextSession openFullTextSession() { - if ( sessionFactory == null ) { + if ( sessionFactory() == null ) { build(); } - Session session = sessionFactory.openSession(); + Session session = sessionFactory().openSession(); return Search.getFullTextSession( session ); } @@ -98,9 +96,9 @@ public FullTextSessionBuilder build() { setupContext = setupContext.withConfiguration( builder -> builder.addAnnotatedClasses( annotatedClasses ) ); - sessionFactory = setupContext.setup().unwrap( SessionFactoryImplementor.class ); + currentSessionFactory( setupContext.setup().unwrap( SessionFactoryImplementor.class ) ); - ServiceRegistryImplementor serviceRegistryImplementor = sessionFactory.getServiceRegistry(); + ServiceRegistryImplementor serviceRegistryImplementor = sessionFactory().getServiceRegistry(); EventListenerRegistry registry = serviceRegistryImplementor.getService( EventListenerRegistry.class ); for ( LoadEventListener listener : additionalLoadEventListeners ) { @@ -114,10 +112,10 @@ public FullTextSessionBuilder build() { * @return the SearchFactory */ public SearchFactory getSearchFactory() { - if ( sessionFactory == null ) { + if ( sessionFactory() == null ) { build(); } - return ImplementationFactory.createSearchFactory( ContextHelper.getSearchIntegratorBySFI( sessionFactory ) ); + return ImplementationFactory.createSearchFactory( ContextHelper.getSearchIntegratorBySFI( sessionFactory() ) ); } public FullTextSessionBuilder addLoadEventListener(LoadEventListener additionalLoadEventListener) { @@ -127,30 +125,35 @@ public FullTextSessionBuilder addLoadEventListener(LoadEventListener additionalL @Override public void afterEach(ExtensionContext extensionContext) throws Exception { - if ( !callOncePerClass ) { - sessionFactory = null; - setupHelper.afterEach( extensionContext ); - } + currentSessionFactory( null ); + setupHelper.afterEach( extensionContext ); } @Override - public void beforeEach(ExtensionContext extensionContext) throws Exception { - if ( !callOncePerClass ) { - setupHelper.beforeEach( extensionContext ); - } + protected void actualBeforeEach(ExtensionContext extensionContext) throws Exception { + setupHelper.beforeEach( extensionContext ); } @Override public void afterAll(ExtensionContext extensionContext) throws Exception { - if ( callOncePerClass ) { - sessionFactory = null; - setupHelper.afterEach( extensionContext ); - } + currentSessionFactory( null ); + setupHelper.afterAll( extensionContext ); } @Override - public void beforeAll(ExtensionContext extensionContext) throws Exception { - callOncePerClass = true; + protected void actualBeforeAll(ExtensionContext extensionContext) throws Exception { setupHelper.beforeAll( extensionContext ); } + + private SessionFactoryImplementor sessionFactory() { + Collection values = sessionFactory.values(); + return values.isEmpty() ? null : values.iterator().next(); + } + + private void currentSessionFactory(SessionFactoryImplementor sessionFactory) { + SessionFactoryImplementor removed = this.sessionFactory.put( currentScope(), sessionFactory ); + if ( removed != null ) { + removed.close(); + } + } } diff --git a/pom.xml b/pom.xml index 8cd14212c69..a7a7ce04003 100644 --- a/pom.xml +++ b/pom.xml @@ -832,10 +832,10 @@ ${java-version.test.launcher} ${surefire.jvm.args} + Better test names in test reports. + See https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html#Surefire_Extensions_and_Reports_Configuration_for_.40DisplayName + Don't ask me why this isn't the default + --> true true diff --git a/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/extension/BackendMock.java b/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/extension/BackendMock.java index 6dcf25fe31f..eab7c006487 100644 --- a/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/extension/BackendMock.java +++ b/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/extension/BackendMock.java @@ -36,23 +36,22 @@ import org.hibernate.search.util.impl.integrationtest.common.stub.backend.index.impl.StubBackendFactory; import org.hibernate.search.util.impl.integrationtest.common.stub.backend.index.impl.StubIndexCreateContext; import org.hibernate.search.util.impl.integrationtest.common.stub.backend.search.query.impl.StubSearchWork; +import org.hibernate.search.util.impl.test.extension.ExtensionScope; -import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; import org.junit.jupiter.api.extension.ExtensionContext; import org.opentest4j.TestAbortedException; -public class BackendMock implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback, AfterAllCallback { +public class BackendMock implements BeforeTestExecutionCallback, AfterEachCallback, BeforeAllCallback, BeforeEachCallback { private final VerifyingStubBackendBehavior backendBehavior = new VerifyingStubBackendBehavior( this::indexingWorkExpectations ); - private volatile boolean started = false; - private boolean callOncePerClass = false; - + private ExtensionScope startingScope = ExtensionScope.TEST; private volatile BackendIndexingWorkExpectations indexingWorkExpectations = BackendIndexingWorkExpectations.sync(); private final Map> documentDiffers = new ConcurrentHashMap<>(); @@ -65,61 +64,49 @@ protected BackendMock() { } @Override - public void beforeAll(ExtensionContext context) { - callOncePerClass = true; - doBefore(); + public void beforeAll(ExtensionContext extensionContext) { + started = true; + startingScope = ExtensionScope.CLASS; } @Override - public void afterAll(ExtensionContext context) { - if ( callOncePerClass ) { - doAfter( context ); - } + public void beforeEach(ExtensionContext extensionContext) { + started = true; } @Override - public void beforeEach(ExtensionContext context) { - if ( !callOncePerClass ) { - doBefore(); - } + public void beforeTestExecution(ExtensionContext extensionContext) throws Exception { + // this means we are done with any @Before setups + // and we want to make sure that anything we've expected there is OK: + verifyAndReset(); + // if we don't do it here ^ then any unmet expectations from @Before will only fail after the test is executed + // and it'll be harder to track the mismatch. } @Override public void afterEach(ExtensionContext context) { - if ( !callOncePerClass ) { - doAfter( context ); + if ( context.getExecutionException().map( e -> e instanceof TestAbortedException ).orElse( Boolean.FALSE ) ) { + // test was aborted - hence let's not do verification. cleanups will happen in finally block. + return; } + verifyAndReset(); + if ( ExtensionScope.TEST.equals( startingScope ) ) { + backendBehavior().resetBackends(); + } + started = false; } - private void doBefore() { - started = true; - } - - private void doAfter(ExtensionContext context) { + private void verifyAndReset() { try { - if ( context.getExecutionException().map( e -> e instanceof TestAbortedException ).orElse( Boolean.FALSE ) ) { - // test was aborted - hence let's not do verification. cleanups will happen in finally block. - return; - } - // Workaround for a problem in Hibernate ORM's CustomRunner - // (used by BytecodeEnhancerRunner in particular) - // which applies class rules twices, resulting in "started" being false - // when we get here in the outermost statement... - if ( started ) { - verifyExpectationsMet(); - } + backendBehavior().verifyExpectationsMet(); } finally { - if ( started ) { - resetExpectations(); - started = false; - backendBehavior.resetBackends(); - } + backendBehavior().resetExpectations(); } } public BackendMock ignoreSchema() { - backendBehavior.ignoreSchema( true ); + backendBehavior().ignoreSchema( true ); return this; } @@ -129,7 +116,7 @@ public BackendMock documentDiffer(String indexName, StubTreeNodeDiffer mappingHandlePromise) { - return new StubBackendFactory( backendBehavior, mappingHandlePromise ); + return new StubBackendFactory( backendBehavior(), mappingHandlePromise ); } public void indexingWorkExpectations(BackendIndexingWorkExpectations expectations) { diff --git a/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/extension/MappingSetupHelper.java b/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/extension/MappingSetupHelper.java index 382697f584a..b2bdfdce21e 100644 --- a/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/extension/MappingSetupHelper.java +++ b/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/extension/MappingSetupHelper.java @@ -22,6 +22,7 @@ import org.hibernate.search.util.impl.integrationtest.common.TestConfigurationProvider; import org.hibernate.search.util.impl.integrationtest.common.assertion.MappingAssertionHelper; import org.hibernate.search.util.impl.integrationtest.common.stub.backend.BackendMappingHandle; +import org.hibernate.search.util.impl.test.extension.AbstractScopeTrackingExtension; import org.hibernate.search.util.impl.test.extension.ExtensionScope; import org.junit.jupiter.api.extension.AfterAllCallback; @@ -33,12 +34,12 @@ import org.junit.jupiter.api.extension.ReflectiveInvocationContext; public abstract class MappingSetupHelper.AbstractSetupContext, B, BC, R, SV> + extends AbstractScopeTrackingExtension implements AfterAllCallback, AfterEachCallback, BeforeAllCallback, BeforeEachCallback, InvocationInterceptor { private final TestConfigurationProvider configurationProvider; protected final BackendSetupStrategy backendSetupStrategy; private final Map> toClose = new EnumMap<>( ExtensionScope.class ); - private ExtensionScope scope = ExtensionScope.CLASS; protected MappingSetupHelper(BackendSetupStrategy backendSetupStrategy) { this.configurationProvider = new TestConfigurationProvider(); @@ -63,14 +64,6 @@ public C start(SV setupVariant) { return backendSetupStrategy.start( setupContext, configurationProvider, setupContext.backendMappingHandlePromise ); } - protected void updateScope(ExtensionScope scope) { - this.scope = scope; - } - - protected ExtensionScope currentScope() { - return scope; - } - @Override public T interceptTestClassConstructor(Invocation invocation, ReflectiveInvocationContext> invocationContext, ExtensionContext extensionContext) @@ -94,17 +87,15 @@ public void afterEach(ExtensionContext context) throws Exception { } @Override - public void beforeAll(ExtensionContext context) { + protected void actualBeforeAll(ExtensionContext context) { configurationProvider.beforeAll( context ); - updateScope( ExtensionScope.CLASS ); - init( ExtensionScope.CLASS ); + init( currentScope() ); } @Override - public void beforeEach(ExtensionContext context) { + protected void actualBeforeEach(ExtensionContext context) { configurationProvider.beforeEach( context ); - updateScope( ExtensionScope.TEST ); - init( ExtensionScope.TEST ); + init( currentScope() ); } private void cleanUp(ExtensionScope scope) throws Exception { diff --git a/util/internal/integrationtest/mapper/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/mapper/orm/OrmSetupHelper.java b/util/internal/integrationtest/mapper/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/mapper/orm/OrmSetupHelper.java index e173b189d29..9267230387a 100644 --- a/util/internal/integrationtest/mapper/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/mapper/orm/OrmSetupHelper.java +++ b/util/internal/integrationtest/mapper/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/mapper/orm/OrmSetupHelper.java @@ -181,8 +181,8 @@ protected void close(SessionFactory toClose) { } @Override - public void beforeEach(ExtensionContext context) { - super.beforeEach( context ); + public void actualBeforeEach(ExtensionContext context) { + super.actualBeforeEach( context ); // if test was aborted then we don't want to clean the data since the test wasn't executed. if ( !context.getExecutionException().map( Object::getClass ) .map( org.opentest4j.TestAbortedException.class::equals ).orElse( Boolean.FALSE ) ) { diff --git a/util/internal/test/common/src/main/java/org/hibernate/search/util/impl/test/extension/AbstractScopeTrackingExtension.java b/util/internal/test/common/src/main/java/org/hibernate/search/util/impl/test/extension/AbstractScopeTrackingExtension.java new file mode 100644 index 00000000000..bc50afd4734 --- /dev/null +++ b/util/internal/test/common/src/main/java/org/hibernate/search/util/impl/test/extension/AbstractScopeTrackingExtension.java @@ -0,0 +1,46 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.search.util.impl.test.extension; + +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +import org.jboss.logging.Logger; + +public abstract class AbstractScopeTrackingExtension implements BeforeEachCallback, BeforeAllCallback { + private static final Logger log = Logger.getLogger( AbstractScopeTrackingExtension.class.getName() ); + + private ExtensionScope scope = ExtensionScope.CLASS; + + protected final void updateScope(ExtensionScope scope) { + log.debug( "Scope changed to: " + scope ); + this.scope = scope; + } + + protected final ExtensionScope currentScope() { + return scope; + } + + @Override + public final void beforeAll(ExtensionContext extensionContext) throws Exception { + updateScope( ExtensionScope.CLASS ); + actualBeforeAll( extensionContext ); + } + + protected void actualBeforeAll(ExtensionContext extensionContext) throws Exception { + } + + @Override + public final void beforeEach(ExtensionContext extensionContext) throws Exception { + updateScope( ExtensionScope.TEST ); + actualBeforeEach( extensionContext ); + } + + protected void actualBeforeEach(ExtensionContext extensionContext) throws Exception { + } +}