Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spring] Limit context configuration to a single class #1246

Merged
merged 2 commits into from Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,16 @@
package cucumber.examples.spring.txn;

import cucumber.api.java.Before;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;

@WebAppConfiguration
@ContextConfiguration("classpath:cucumber.xml")
public class CucumberContextConfiguration {

@Before
public void setup_cucumber_spring_context(){
// Dummy method so cucumber will recognize this class as glue
// and use its context configuration.
}
}
Expand Up @@ -14,8 +14,6 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebAppConfiguration
@ContextConfiguration("classpath:cucumber.xml")
public class SeeMessagesStepdefs {
@Autowired
private UserRepository userRepository;
Expand Down
Expand Up @@ -10,8 +10,6 @@

import java.util.List;

@WebAppConfiguration
@ContextConfiguration("classpath:cucumber.xml")
public class UserStepdefs {
@Autowired
private UserRepository userRepository;
Expand Down
Expand Up @@ -58,8 +58,7 @@
* the @Component stereotype an exception will be thrown
* </li>
* <li>
* If more that one step definition class is used to configure the spring context, the annotations must be equal
* on the different step definition. Please note that doing so is deprecated.
* If more that one step definition class is used to configure the spring context an exception will be thrown.
* </li>
* </ul>
*/
Expand All @@ -70,7 +69,6 @@ public class SpringFactory implements ObjectFactory {

private final Collection<Class<?>> stepClasses = new HashSet<Class<?>>();
private Class<?> stepClassWithSpringContext = null;
private boolean deprecationWarningIssued = false;

public SpringFactory() {
}
Expand All @@ -80,11 +78,12 @@ public boolean addClass(final Class<?> stepClass) {
if (!stepClasses.contains(stepClass)) {
checkNoComponentAnnotations(stepClass);
if (dependsOnSpringContext(stepClass)) {
if (stepClassWithSpringContext == null) {
stepClassWithSpringContext = stepClass;
} else {
checkAnnotationsEqual(stepClassWithSpringContext, stepClass);
if (stepClassWithSpringContext != null) {
throw new CucumberException(String.format("" +
"Glue class %1$s and %2$s both attempt to configure the spring context. Please ensure only one " +
"glue class configures the spring context", stepClass, stepClassWithSpringContext));
}
stepClassWithSpringContext = stepClass;
}
stepClasses.add(stepClass);
}
Expand Down Expand Up @@ -130,41 +129,6 @@ private static boolean hasAnnotation(Annotation annotation, Collection<Class<? e
return false;
}


private void checkAnnotationsEqual(Class<?> stepClassWithSpringContext, Class<?> stepClass) {
if (!deprecationWarningIssued) {
deprecationWarningIssued = true;
System.err.println(String.format("" +
"WARNING: Having more than one glue class that configures " +
"the spring context is deprecated. Found both %1$s and %2$s " +
"that attempt to configure the spring context.",
stepClass, stepClassWithSpringContext));
}
Annotation[] annotations1 = stepClassWithSpringContext.getAnnotations();
Annotation[] annotations2 = stepClass.getAnnotations();
if (annotations1.length != annotations2.length) {
throw new CucumberException("Annotations differs on glue classes found: " +
stepClassWithSpringContext.getName() + ", " +
stepClass.getName());
}
for (Annotation annotation : annotations1) {
if (!isAnnotationInArray(annotation, annotations2)) {
throw new CucumberException("Annotations differs on glue classes found: " +
stepClassWithSpringContext.getName() + ", " +
stepClass.getName());
}
}
}

private boolean isAnnotationInArray(Annotation annotation, Annotation[] annotations) {
for (Annotation annotationFromArray: annotations) {
if (annotation.equals(annotationFromArray)) {
return true;
}
}
return false;
}

@Override
public void start() {
if (stepClassWithSpringContext != null) {
Expand Down
Expand Up @@ -13,11 +13,8 @@
import cucumber.runtime.java.spring.contextconfig.BellyStepdefs;
import cucumber.runtime.java.spring.contextconfig.WithSpringAnnotations;
import cucumber.runtime.java.spring.contexthierarchyconfig.WithContextHierarchyAnnotation;
import cucumber.runtime.java.spring.contexthierarchyconfig.WithDifferentContextHierarchyAnnotation;
import cucumber.runtime.java.spring.dirtiescontextconfig.DirtiesContextBellyStepDefs;
import cucumber.runtime.java.spring.metaconfig.dirties.DirtiesContextBellyMetaStepDefs;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -244,39 +241,12 @@ public void shouldUseCucumberXmlIfNoClassWithSpringAnnotationIsFound() {
}

@Test
public void shouldAllowClassesWithSameSpringAnnotations() {
final ObjectFactory factory = new SpringFactory();
PrintStream originalErr = System.err;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setErr(new PrintStream(baos));
factory.addClass(WithSpringAnnotations.class);
factory.addClass(BellyStepdefs.class);
assertEquals("WARNING: Having more than one glue class that configures " +
"the spring context is deprecated. Found both class " +
"cucumber.runtime.java.spring.contextconfig.BellyStepdefs and class " +
"cucumber.runtime.java.spring.contextconfig.WithSpringAnnotations " +
"that attempt to configure the spring context.\n",
baos.toString());
} finally {
System.setErr(originalErr);
}
}

@Test
public void shouldFailIfClassesWithDifferentSpringAnnotationsAreFound() {
public void shouldFailIfMultipleClassesWithSpringAnnotationsAreFound() {
expectedException.expect(CucumberException.class);
expectedException.expectMessage("Annotations differs on glue classes found: cucumber.runtime.java.spring.contexthierarchyconfig.WithContextHierarchyAnnotation, cucumber.runtime.java.spring.contexthierarchyconfig.WithDifferentContextHierarchyAnnotation");
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");
final ObjectFactory factory = new SpringFactory();
PrintStream originalErr = System.err;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setErr(new PrintStream(baos));
factory.addClass(WithContextHierarchyAnnotation.class);
factory.addClass(WithDifferentContextHierarchyAnnotation.class);
} finally {
System.setErr(originalErr);
}
factory.addClass(WithSpringAnnotations.class);
factory.addClass(BellyStepdefs.class);
}

@Test
Expand Down

This file was deleted.