Skip to content

Commit

Permalink
HSEARCH-3654 Make sure that scope tracking extensions are static in p…
Browse files Browse the repository at this point in the history
…arameterized class tests

- otherwise we won't register the callbacks as the constructor interceptor is never called, leading to incorrect cleanups
  • Loading branch information
marko-bekhta authored and yrodiere committed Oct 18, 2023
1 parent bc39e6d commit df0ce1b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static List<? extends Arguments> params() {
public String strategy;

@RegisterExtension
public SearchSetupHelper setupHelper = SearchSetupHelper.create();
public static SearchSetupHelper setupHelper = SearchSetupHelper.create();

public List<String> shardIds;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static List<? extends Arguments> params() {
}

@RegisterExtension
public SearchSetupHelper setupHelper = SearchSetupHelper.create();
public static SearchSetupHelper setupHelper = SearchSetupHelper.create();
private final SimpleMappedIndex<IndexBinding> index = SimpleMappedIndex.of( IndexBinding::new );

@ParameterizedSetup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static List<? extends Arguments> params() {
private final SimpleMappedIndex<IndexBinding> index = SimpleMappedIndex.of( IndexBinding::new );

@RegisterExtension
public final SearchSetupHelper setupHelper = SearchSetupHelper.create();
public static final SearchSetupHelper setupHelper = SearchSetupHelper.create();
private StubSession sessionContext;

@ParameterizedSetup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
class BytecodeEnhancementIT {

@RegisterExtension
public BackendMock backendMock = BackendMock.create();
public static BackendMock backendMock = BackendMock.create();

@RegisterExtension
public OrmSetupHelper ormSetupHelper = OrmSetupHelper.withBackendMock( backendMock );
public static OrmSetupHelper ormSetupHelper = OrmSetupHelper.withBackendMock( backendMock );
private SessionFactory sessionFactory;

@ParameterizedSetup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
import static org.junit.platform.commons.util.AnnotationUtils.isAnnotated;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.hibernate.search.util.impl.test.extension.AbstractScopeTrackingExtension;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.engine.descriptor.ClassTestDescriptor;
import org.junit.jupiter.engine.descriptor.MethodBasedTestDescriptor;
import org.junit.jupiter.engine.descriptor.NestedClassTestDescriptor;
Expand Down Expand Up @@ -44,6 +49,7 @@ public FilterResult apply(TestDescriptor testDescriptor) {
+ " contains some methods annotated with @BeforeEach. " +
"Use @ParameterizedSetupBeforeTest instead." );
}
checkNonStaticOrmHelper( testClass );
}
}
// nested class inside a parameterized test -- we are not handling this option for now, so.... failing:
Expand Down Expand Up @@ -86,6 +92,22 @@ else if ( parameterizedClass ) {
return FilterResult.included( "This filter does not care about this case." );
}

private boolean checkNonStaticOrmHelper(Class<?> testClass) {
if ( Object.class.equals( testClass ) ) {
return false;
}
for ( Field field : testClass.getDeclaredFields() ) {
if ( isAnnotated( field, RegisterExtension.class )
&& AbstractScopeTrackingExtension.class.isAssignableFrom( field.getType() )
&& !Modifier.isStatic( field.getModifiers() ) ) {
throw new IllegalStateException( "Scope tracking extension " + field.getName()
+ " must be declared as static field in a @ParameterizedClass test " + testClass.getName() + "." );
}
}

return checkNonStaticOrmHelper( testClass.getSuperclass() );
}

private boolean hasParameterizedSetup(Class<?> testClass) {
return hasMethodAnnotated( testClass, ParameterizedSetup.class );
}
Expand Down

0 comments on commit df0ce1b

Please sign in to comment.