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

Allowed customization of Spring step definitions context #203

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import cucumber.runtime.CucumberException;
import cucumber.runtime.java.ObjectFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.StaticApplicationContext;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -34,9 +33,12 @@ public class SpringFactory implements ObjectFactory {

private static AbstractApplicationContext applicationContext;

private StaticApplicationContext stepContext;
private ClassPathXmlApplicationContext stepContext;
private final Collection<Class<?>> stepClasses = new ArrayList<Class<?>>();

public SpringFactory() {
}

static {
applicationContext = new ClassPathXmlApplicationContext(new String[]{"cucumber.xml"});
applicationContext.registerShutdownHook();
Expand All @@ -54,18 +56,16 @@ public void createInstances() {
}

private void createNewStepContext() {
stepContext = new StaticApplicationContext(applicationContext);
AutowiredAnnotationBeanPostProcessor autowirer = new AutowiredAnnotationBeanPostProcessor();
autowirer.setBeanFactory(stepContext.getBeanFactory());
stepContext.getBeanFactory().addBeanPostProcessor(autowirer);
stepContext.getBeanFactory().addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
stepContext = new ClassPathXmlApplicationContext(new String[]{"classpath*:cucumber-glue.xml"},
applicationContext);
}

private void populateStepContext() {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) stepContext.getAutowireCapableBeanFactory();
for (Class<?> stepClass : stepClasses) {
stepContext.registerSingleton(stepClass.getName(), stepClass);
registry.registerBeanDefinition(stepClass.getName(),
BeanDefinitionBuilder.genericBeanDefinition(stepClass).getBeanDefinition());
}
stepContext.refresh();
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions spring/src/main/resources/cucumber-glue.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config/>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import cucumber.runtime.java.ObjectFactory;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class SpringFactoryTest {

Expand Down Expand Up @@ -63,4 +60,14 @@ public void shouldRespectCommonAnnotationsInStepDefs() {
assertTrue(stepdef.isPreDestroyCalled());
}

@Test
public void shouldRespectCustomPropertyPlaceholderConfigurer() {
final ObjectFactory factory = new SpringFactory();
factory.addClass(WithSpringAnnotations.class);
factory.createInstances();
WithSpringAnnotations stepdef = factory.getInstance(WithSpringAnnotations.class);
factory.disposeInstances();

assertEquals("property value", stepdef.getProperty());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cucumber.runtime.java.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
Expand All @@ -11,6 +12,9 @@ public class WithSpringAnnotations {
private boolean postConstructCalled;
private boolean autowired;

@Value("${cukes.test.property}")
private String property;

@PostConstruct
public void postConstruct() {
postConstructCalled = true;
Expand Down Expand Up @@ -38,4 +42,7 @@ public boolean isPreDestroyCalled() {
return preDestroyCalled;
}

public String getProperty() {
return property;
}
}
13 changes: 13 additions & 0 deletions spring/src/test/resources/cucumber-glue.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<props>
<prop key="cukes.test.property">property value</prop>
</props>
</property>
</bean>
</beans>