Skip to content

Commit

Permalink
Replace manual annotation checking with AnnotatedElementUtils.isAnnot…
Browse files Browse the repository at this point in the history
…ated
  • Loading branch information
mpkorstanje committed Nov 1, 2022
1 parent 4636803 commit bfb1e2b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.web.WebAppConfiguration;

import java.lang.annotation.Annotation;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.Set;

/**
* Spring based implementation of ObjectFactory.
Expand Down Expand Up @@ -70,16 +65,14 @@ public boolean addClass(final Class<?> stepClass) {
}

private static void checkNoComponentAnnotations(Class<?> type) {
for (Annotation annotation : type.getAnnotations()) {
if (hasComponentAnnotation(annotation)) {
throw new CucumberBackendException(String.format("" +
"Glue class %1$s was annotated with @%2$s; marking it as a candidate for auto-detection by " +
"Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by "
+
"spring may lead to duplicate bean definitions. Please remove the @%2$s annotation",
type.getName(),
annotation.annotationType().getSimpleName()));
}
if (AnnotatedElementUtils.isAnnotated(type, Component.class)) {
throw new CucumberBackendException(String.format("" +
"Glue class %1$s was (meta-)annotated with @Component; marking it as a candidate for auto-detection by "
+
"Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by "
+
"spring may lead to duplicate bean definitions. Please remove the @Component (meta-)annotation",
type.getName()));
}
}

Expand All @@ -90,7 +83,7 @@ static boolean hasCucumberContextConfiguration(Class<?> stepClass) {
private void checkOnlyOneClassHasCucumberContextConfiguration(Class<?> stepClass) {
if (withCucumberContextConfiguration != null) {
throw new CucumberBackendException(String.format("" +
"Glue class %1$s and %2$s are both annotated with @CucumberContextConfiguration.\n" +
"Glue class %1$s and %2$s are both (meta-)annotated with @CucumberContextConfiguration.\n" +
"Please ensure only one class configures the spring context\n" +
"\n" +
"By default Cucumber scans the entire classpath for context configuration.\n" +
Expand All @@ -101,32 +94,6 @@ private void checkOnlyOneClassHasCucumberContextConfiguration(Class<?> stepClass
}
}

private static boolean hasComponentAnnotation(Annotation annotation) {
return hasAnnotation(annotation, Collections.singleton(Component.class));
}

private static boolean hasAnnotation(Annotation annotation, Collection<Class<? extends Annotation>> desired) {
Set<Class<? extends Annotation>> seen = new HashSet<>();
Deque<Class<? extends Annotation>> toCheck = new ArrayDeque<>();
toCheck.add(annotation.annotationType());

while (!toCheck.isEmpty()) {
Class<? extends Annotation> annotationType = toCheck.pop();
if (desired.contains(annotationType)) {
return true;
}

seen.add(annotationType);
for (Annotation annotationTypesAnnotations : annotationType.getAnnotations()) {
if (!seen.contains(annotationTypesAnnotations.annotationType())) {
toCheck.add(annotationTypesAnnotations.annotationType());
}
}

}
return false;
}

@Override
public void start() {
if (withCucumberContextConfiguration == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void ignoresAbstractClassWithCucumberContextConfiguration() {
@Test
void ignoresInterfaceWithCucumberContextConfiguration() {
backend.loadGlue(glue, singletonList(
URI.create("classpath:io/cucumber/spring/cucumbercontextconfigannotation")));
URI.create("classpath:io/cucumber/spring/cucumbercontextconfigannotation")));
backend.buildWorld();
verify(factory, times(0)).addClass(AnnotatedInterface.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void shouldFailIfMultipleClassesWithSpringAnnotationsAreFound() {
Executable testMethod = () -> factory.addClass(BellyStepDefinitions.class);
CucumberBackendException actualThrown = assertThrows(CucumberBackendException.class, testMethod);
assertThat(actualThrown.getMessage(), startsWith(
"Glue class class io.cucumber.spring.contextconfig.BellyStepDefinitions and class io.cucumber.spring.SpringFactoryTest$WithSpringAnnotations are both annotated with @CucumberContextConfiguration.\n"
"Glue class class io.cucumber.spring.contextconfig.BellyStepDefinitions and class io.cucumber.spring.SpringFactoryTest$WithSpringAnnotations are both (meta-)annotated with @CucumberContextConfiguration.\n"
+
"Please ensure only one class configures the spring context"));
}
Expand All @@ -275,7 +275,7 @@ void shouldFailIfClassWithSpringComponentAnnotationsIsFound() {
Executable testMethod = () -> factory.addClass(WithComponentAnnotation.class);
CucumberBackendException actualThrown = assertThrows(CucumberBackendException.class, testMethod);
assertThat(actualThrown.getMessage(), is(equalTo(
"Glue class io.cucumber.spring.componentannotation.WithComponentAnnotation was annotated with @Component; marking it as a candidate for auto-detection by Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by spring may lead to duplicate bean definitions. Please remove the @Component annotation")));
"Glue class io.cucumber.spring.componentannotation.WithComponentAnnotation was (meta-)annotated with @Component; marking it as a candidate for auto-detection by Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by spring may lead to duplicate bean definitions. Please remove the @Component (meta-)annotation")));
}

@Test
Expand All @@ -285,7 +285,7 @@ void shouldFailIfClassWithAnnotationAnnotatedWithSpringComponentAnnotationsIsFou
Executable testMethod = () -> factory.addClass(WithControllerAnnotation.class);
CucumberBackendException actualThrown = assertThrows(CucumberBackendException.class, testMethod);
assertThat(actualThrown.getMessage(), is(equalTo(
"Glue class io.cucumber.spring.componentannotation.WithControllerAnnotation was annotated with @Controller; marking it as a candidate for auto-detection by Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by spring may lead to duplicate bean definitions. Please remove the @Controller annotation")));
"Glue class io.cucumber.spring.componentannotation.WithControllerAnnotation was (meta-)annotated with @Component; marking it as a candidate for auto-detection by Spring. Glue classes are detected and registered by Cucumber. Auto-detection of glue classes by spring may lead to duplicate bean definitions. Please remove the @Component (meta-)annotation")));
}

@Test
Expand Down

0 comments on commit bfb1e2b

Please sign in to comment.