|
1 | 1 | package cucumber.runtime.java.spring;
|
2 | 2 |
|
3 |
| -import cucumber.runtime.CucumberException; |
4 |
| -import cucumber.runtime.java.ObjectFactory; |
5 |
| -import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.HashSet; |
| 5 | + |
6 | 6 | import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
7 | 7 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
8 |
| -import org.springframework.context.support.AbstractApplicationContext; |
9 |
| -import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 8 | +import org.springframework.context.ConfigurableApplicationContext; |
| 9 | +import org.springframework.context.support.GenericXmlApplicationContext; |
| 10 | +import org.springframework.test.context.TestContextManager; |
10 | 11 |
|
11 |
| -import java.util.Collection; |
12 |
| -import java.util.HashSet; |
| 12 | +import cucumber.runtime.CucumberException; |
| 13 | +import cucumber.runtime.java.ObjectFactory; |
13 | 14 |
|
14 | 15 | /**
|
15 | 16 | * Spring based implementation of ObjectFactory.
|
|
31 | 32 | */
|
32 | 33 | public class SpringFactory implements ObjectFactory {
|
33 | 34 |
|
34 |
| - private static AbstractApplicationContext applicationContext; |
35 |
| - |
36 |
| - private final Collection<Class<?>> stepClasses = new HashSet<Class<?>>(); |
37 |
| - |
38 |
| - public SpringFactory() { |
39 |
| - } |
40 |
| - |
41 |
| - static { |
42 |
| - applicationContext = new ClassPathXmlApplicationContext( |
43 |
| - "cucumber/runtime/java/spring/cucumber-glue.xml", |
44 |
| - "cucumber.xml"); |
45 |
| - applicationContext.registerShutdownHook(); |
46 |
| - } |
47 |
| - |
48 |
| - @Override |
49 |
| - public void addClass(final Class<?> stepClass) { |
50 |
| - if (!stepClasses.contains(stepClass)) { |
51 |
| - stepClasses.add(stepClass); |
52 |
| - |
53 |
| - BeanDefinitionRegistry registry = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory(); |
54 |
| - registry.registerBeanDefinition(stepClass.getName(), |
55 |
| - BeanDefinitionBuilder.genericBeanDefinition(stepClass) |
56 |
| - .setScope(GlueCodeScope.NAME) |
57 |
| - .getBeanDefinition()); |
58 |
| - |
59 |
| - } |
60 |
| - } |
61 |
| - |
62 |
| - @Override |
63 |
| - public void start() { |
64 |
| - GlueCodeContext.INSTANCE.start(); |
65 |
| - } |
66 |
| - |
67 |
| - @Override |
68 |
| - public void stop() { |
69 |
| - GlueCodeContext.INSTANCE.stop(); |
70 |
| - } |
71 |
| - |
72 |
| - @SuppressWarnings("unchecked") |
73 |
| - @Override |
74 |
| - public <T> T getInstance(final Class<T> type) { |
75 |
| - try { |
76 |
| - return applicationContext.getBean(type); |
77 |
| - } catch (NoSuchBeanDefinitionException exception) { |
78 |
| - throw new CucumberException(exception.getMessage(), exception); |
79 |
| - } |
80 |
| - } |
| 35 | + private static ConfigurableApplicationContext applicationContext; |
| 36 | + |
| 37 | + private final Collection<Class<?>> stepClasses = new HashSet<Class<?>>(); |
| 38 | + |
| 39 | + public SpringFactory() { |
| 40 | + } |
| 41 | + |
| 42 | + static { |
| 43 | + applicationContext = new GenericXmlApplicationContext( |
| 44 | + "cucumber/runtime/java/spring/cucumber-glue.xml"); |
| 45 | + applicationContext.registerShutdownHook(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void addClass(final Class<?> stepClass) { |
| 50 | + if (!stepClasses.contains(stepClass)) { |
| 51 | + stepClasses.add(stepClass); |
| 52 | + |
| 53 | + BeanDefinitionRegistry registry = (BeanDefinitionRegistry) applicationContext |
| 54 | + .getAutowireCapableBeanFactory(); |
| 55 | + registry.registerBeanDefinition(stepClass.getName(), |
| 56 | + BeanDefinitionBuilder.genericBeanDefinition(stepClass) |
| 57 | + .setScope(GlueCodeScope.NAME).getBeanDefinition()); |
| 58 | + |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void start() { |
| 64 | + GlueCodeContext.INSTANCE.start(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void stop() { |
| 69 | + GlueCodeContext.INSTANCE.stop(); |
| 70 | + applicationContext.getBeanFactory().destroySingletons(); |
| 71 | + } |
| 72 | + |
| 73 | + @SuppressWarnings("unchecked") |
| 74 | + @Override |
| 75 | + public <T> T getInstance(final Class<T> type) { |
| 76 | + T instance = null; |
| 77 | + if (!applicationContext.getBeanFactory().containsSingleton(type.getName())) { |
| 78 | + try{ |
| 79 | + instance = createTest(type); |
| 80 | + TestContextManager contextManager = new TestContextManager(type); |
| 81 | + contextManager.prepareTestInstance(instance); |
| 82 | + } catch (Exception e) { |
| 83 | + new CucumberException(e.getMessage(), e); |
| 84 | + } |
| 85 | + applicationContext.getBeanFactory().registerSingleton(type.getName(), instance); |
| 86 | + } else { |
| 87 | + instance = applicationContext.getBean(type); |
| 88 | + } |
| 89 | + return instance; |
| 90 | + } |
| 91 | + |
| 92 | + @SuppressWarnings("unchecked") |
| 93 | + protected <T> T createTest(Class<T> type) throws Exception { |
| 94 | + return (T) type.getConstructors()[0].newInstance(); |
| 95 | + } |
81 | 96 |
|
82 | 97 | }
|
0 commit comments