Skip to content

Commit 1c97048

Browse files
committed
Limit spring context configuration to a single class
Fixes #1240
1 parent 23ce675 commit 1c97048

File tree

3 files changed

+10
-98
lines changed

3 files changed

+10
-98
lines changed

spring/src/main/java/cucumber/runtime/java/spring/SpringFactory.java

+6-42
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@
5858
* the @Component stereotype an exception will be thrown
5959
* </li>
6060
* <li>
61-
* If more that one step definition class is used to configure the spring context, the annotations must be equal
62-
* on the different step definition. Please note that doing so is deprecated.
61+
* If more that one step definition class is used to configure the spring context an exception will be thrown.
6362
* </li>
6463
* </ul>
6564
*/
@@ -70,7 +69,6 @@ public class SpringFactory implements ObjectFactory {
7069

7170
private final Collection<Class<?>> stepClasses = new HashSet<Class<?>>();
7271
private Class<?> stepClassWithSpringContext = null;
73-
private boolean deprecationWarningIssued = false;
7472

7573
public SpringFactory() {
7674
}
@@ -80,11 +78,12 @@ public boolean addClass(final Class<?> stepClass) {
8078
if (!stepClasses.contains(stepClass)) {
8179
checkNoComponentAnnotations(stepClass);
8280
if (dependsOnSpringContext(stepClass)) {
83-
if (stepClassWithSpringContext == null) {
84-
stepClassWithSpringContext = stepClass;
85-
} else {
86-
checkAnnotationsEqual(stepClassWithSpringContext, stepClass);
81+
if (stepClassWithSpringContext != null) {
82+
throw new CucumberException(String.format("" +
83+
"Glue class %1$s and %2$s both attempt to configure the spring context. Please ensure only one " +
84+
"glue class configures the spring context", stepClass, stepClassWithSpringContext));
8785
}
86+
stepClassWithSpringContext = stepClass;
8887
}
8988
stepClasses.add(stepClass);
9089
}
@@ -130,41 +129,6 @@ private static boolean hasAnnotation(Annotation annotation, Collection<Class<? e
130129
return false;
131130
}
132131

133-
134-
private void checkAnnotationsEqual(Class<?> stepClassWithSpringContext, Class<?> stepClass) {
135-
if (!deprecationWarningIssued) {
136-
deprecationWarningIssued = true;
137-
System.err.println(String.format("" +
138-
"WARNING: Having more than one glue class that configures " +
139-
"the spring context is deprecated. Found both %1$s and %2$s " +
140-
"that attempt to configure the spring context.",
141-
stepClass, stepClassWithSpringContext));
142-
}
143-
Annotation[] annotations1 = stepClassWithSpringContext.getAnnotations();
144-
Annotation[] annotations2 = stepClass.getAnnotations();
145-
if (annotations1.length != annotations2.length) {
146-
throw new CucumberException("Annotations differs on glue classes found: " +
147-
stepClassWithSpringContext.getName() + ", " +
148-
stepClass.getName());
149-
}
150-
for (Annotation annotation : annotations1) {
151-
if (!isAnnotationInArray(annotation, annotations2)) {
152-
throw new CucumberException("Annotations differs on glue classes found: " +
153-
stepClassWithSpringContext.getName() + ", " +
154-
stepClass.getName());
155-
}
156-
}
157-
}
158-
159-
private boolean isAnnotationInArray(Annotation annotation, Annotation[] annotations) {
160-
for (Annotation annotationFromArray: annotations) {
161-
if (annotation.equals(annotationFromArray)) {
162-
return true;
163-
}
164-
}
165-
return false;
166-
}
167-
168132
@Override
169133
public void start() {
170134
if (stepClassWithSpringContext != null) {

spring/src/test/java/cucumber/runtime/java/spring/SpringFactoryTest.java

+4-34
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
import cucumber.runtime.java.spring.contextconfig.BellyStepdefs;
1414
import cucumber.runtime.java.spring.contextconfig.WithSpringAnnotations;
1515
import cucumber.runtime.java.spring.contexthierarchyconfig.WithContextHierarchyAnnotation;
16-
import cucumber.runtime.java.spring.contexthierarchyconfig.WithDifferentContextHierarchyAnnotation;
1716
import cucumber.runtime.java.spring.dirtiescontextconfig.DirtiesContextBellyStepDefs;
1817
import cucumber.runtime.java.spring.metaconfig.dirties.DirtiesContextBellyMetaStepDefs;
19-
import java.io.ByteArrayOutputStream;
20-
import java.io.PrintStream;
2118
import org.junit.Rule;
2219
import org.junit.Test;
2320
import org.junit.rules.ExpectedException;
@@ -244,39 +241,12 @@ public void shouldUseCucumberXmlIfNoClassWithSpringAnnotationIsFound() {
244241
}
245242

246243
@Test
247-
public void shouldAllowClassesWithSameSpringAnnotations() {
248-
final ObjectFactory factory = new SpringFactory();
249-
PrintStream originalErr = System.err;
250-
try {
251-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
252-
System.setErr(new PrintStream(baos));
253-
factory.addClass(WithSpringAnnotations.class);
254-
factory.addClass(BellyStepdefs.class);
255-
assertEquals("WARNING: Having more than one glue class that configures " +
256-
"the spring context is deprecated. Found both class " +
257-
"cucumber.runtime.java.spring.contextconfig.BellyStepdefs and class " +
258-
"cucumber.runtime.java.spring.contextconfig.WithSpringAnnotations " +
259-
"that attempt to configure the spring context.\n",
260-
baos.toString());
261-
} finally {
262-
System.setErr(originalErr);
263-
}
264-
}
265-
266-
@Test
267-
public void shouldFailIfClassesWithDifferentSpringAnnotationsAreFound() {
244+
public void shouldFailIfMultipleClassesWithSpringAnnotationsAreFound() {
268245
expectedException.expect(CucumberException.class);
269-
expectedException.expectMessage("Annotations differs on glue classes found: cucumber.runtime.java.spring.contexthierarchyconfig.WithContextHierarchyAnnotation, cucumber.runtime.java.spring.contexthierarchyconfig.WithDifferentContextHierarchyAnnotation");
246+
expectedException.expectMessage("Glue class class cucumber.runtime.java.spring.contextconfig.BellyStepdefs and class cucumber.runtime.java.spring.contextconfig.WithSpringAnnotations both attempt to configure the spring context");
270247
final ObjectFactory factory = new SpringFactory();
271-
PrintStream originalErr = System.err;
272-
try {
273-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
274-
System.setErr(new PrintStream(baos));
275-
factory.addClass(WithContextHierarchyAnnotation.class);
276-
factory.addClass(WithDifferentContextHierarchyAnnotation.class);
277-
} finally {
278-
System.setErr(originalErr);
279-
}
248+
factory.addClass(WithSpringAnnotations.class);
249+
factory.addClass(BellyStepdefs.class);
280250
}
281251

282252
@Test

spring/src/test/java/cucumber/runtime/java/spring/contexthierarchyconfig/WithDifferentContextHierarchyAnnotation.java

-22
This file was deleted.

0 commit comments

Comments
 (0)