1 | |
package org.jbehave.core.steps.spring; |
2 | |
|
3 | |
import java.lang.reflect.Modifier; |
4 | |
import java.util.ArrayList; |
5 | |
import java.util.List; |
6 | |
|
7 | |
import org.jbehave.core.configuration.Configuration; |
8 | |
import org.jbehave.core.steps.AbstractStepsFactory; |
9 | |
import org.jbehave.core.steps.InjectableStepsFactory; |
10 | |
import org.springframework.context.ApplicationContext; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public class SpringStepsFactory extends AbstractStepsFactory { |
21 | |
|
22 | |
private final ApplicationContext context; |
23 | |
|
24 | |
public SpringStepsFactory(Configuration configuration, ApplicationContext context) { |
25 | 6 | super(configuration); |
26 | 6 | this.context = context; |
27 | 6 | } |
28 | |
|
29 | |
@Override |
30 | |
protected List<Class<?>> stepsTypes() { |
31 | 6 | List<Class<?>> types = new ArrayList<Class<?>>(); |
32 | 33 | for (String name : context.getBeanDefinitionNames()) { |
33 | 27 | Class<?> type = context.getType(name); |
34 | 27 | if (isAllowed(type) && hasAnnotatedMethods(type)) { |
35 | 5 | types.add(type); |
36 | |
} |
37 | |
} |
38 | 6 | return types; |
39 | |
} |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
protected boolean isAllowed(Class<?> type) { |
49 | 27 | return type != null && !Modifier.isAbstract(type.getModifiers()); |
50 | |
} |
51 | |
|
52 | |
public Object createInstanceOfType(Class<?> type) { |
53 | 20 | for (String name : context.getBeanDefinitionNames()) { |
54 | 20 | Class<?> beanType = context.getType(name); |
55 | 20 | if (type.equals(beanType)) { |
56 | 6 | return context.getBean(name); |
57 | |
} |
58 | |
} |
59 | |
|
60 | 0 | throw new StepsInstanceNotFound(type, this); |
61 | |
} |
62 | |
|
63 | |
} |