Skip to content

Commit

Permalink
Allowed customization of Spring step definitions context
Browse files Browse the repository at this point in the history
SpringFactory now searches for cucumber-steps.xml and if found it - uses for configuring  step definitions context.
  • Loading branch information
vladimirkl committed Feb 13, 2012
1 parent ae23c7e commit 8062fd5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
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-steps.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-steps.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-steps.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>

0 comments on commit 8062fd5

Please sign in to comment.