Skip to content

Commit

Permalink
HSEARCH-3654 Cleanup some extensions that were relying on the scope
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Oct 13, 2023
1 parent 0895f58 commit 17d6f93
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SearchIntegrationEnvironment> environments = new ArrayList<>();
private final List<SearchIntegrationPartialBuildState> integrationPartialBuildStates = new ArrayList<>();
private final List<StubMappingImpl> mappings = new ArrayList<>();
private TckBackendAccessor backendAccessor;
private boolean callOncePerClass = false;
private final Map<ExtensionScope, Context> scopeContexts = new HashMap<>();

public static SearchSetupHelper create() {
return new SearchSetupHelper();
Expand Down Expand Up @@ -113,37 +112,36 @@ 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();
}
}

@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
Expand All @@ -164,12 +162,13 @@ public void cleanUp() throws IOException {
}

private void cleanUp(Closer<IOException> 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;
}
Expand Down Expand Up @@ -263,11 +262,12 @@ public PartialSetup setupFirstPhaseOnly() {
}

public PartialSetup setupFirstPhaseOnly(Optional<StubMapping> 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 )
Expand All @@ -280,20 +280,20 @@ public PartialSetup setupFirstPhaseOnly(Optional<StubMapping> previousMapping) {
integrationBuilder.addMappingInitiator( mappingKey, initiator );

SearchIntegrationPartialBuildState integrationPartialBuildState = integrationBuilder.prepareBuild();
integrationPartialBuildStates.add( integrationPartialBuildState );
context.integrationPartialBuildStates.add( integrationPartialBuildState );

return overrides -> {
SearchIntegrationFinalizer finalizer =
integrationPartialBuildState.finalizer( propertySource.withOverride( overrides ),
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;
};
Expand Down Expand Up @@ -326,4 +326,9 @@ private static boolean runningInNestedContext(ExtensionContext context) {
return context.getRequiredTestClass().isMemberClass();
}

private static class Context {
private final List<SearchIntegrationEnvironment> environments = new ArrayList<>();
private final List<SearchIntegrationPartialBuildState> integrationPartialBuildStates = new ArrayList<>();
private final List<StubMappingImpl> mappings = new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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;
Expand All @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)"
Expand Down
Loading

0 comments on commit 17d6f93

Please sign in to comment.