Skip to content

Commit

Permalink
[Spring] Deprecate context configuration by more than one class
Browse files Browse the repository at this point in the history
  • Loading branch information
brasmusson committed Oct 15, 2017
1 parent 9a951ec commit 0d0baef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class SpringFactory implements ObjectFactory {

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

public SpringFactory() {
}
Expand Down Expand Up @@ -119,6 +120,14 @@ private static boolean hasComponentStereoType(Annotation annotation) {


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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
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,17 +246,37 @@ public void shouldUseCucumberXmlIfNoClassWithSpringAnnotationIsFound() {
@Test
public void shouldAllowClassesWithSameSpringAnnotations() {
final ObjectFactory factory = new SpringFactory();
factory.addClass(WithSpringAnnotations.class);
factory.addClass(BellyStepdefs.class);
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() {
expectedException.expect(CucumberException.class);
expectedException.expectMessage("Annotations differs on glue classes found: cucumber.runtime.java.spring.contexthierarchyconfig.WithContextHierarchyAnnotation, cucumber.runtime.java.spring.contexthierarchyconfig.WithDifferentContextHierarchyAnnotation");
final ObjectFactory factory = new SpringFactory();
factory.addClass(WithContextHierarchyAnnotation.class);
factory.addClass(WithDifferentContextHierarchyAnnotation.class);
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);
}
}

@Test
Expand Down

0 comments on commit 0d0baef

Please sign in to comment.