1 | |
package org.jbehave.core.configuration.spring; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
import java.util.Map; |
6 | |
|
7 | |
import org.jbehave.core.annotations.spring.UsingSpring; |
8 | |
import org.jbehave.core.configuration.AnnotationBuilder; |
9 | |
import org.jbehave.core.configuration.AnnotationFinder; |
10 | |
import org.jbehave.core.configuration.AnnotationMonitor; |
11 | |
import org.jbehave.core.configuration.AnnotationRequired; |
12 | |
import org.jbehave.core.configuration.Configuration; |
13 | |
import org.jbehave.core.steps.CompositeStepsFactory; |
14 | |
import org.jbehave.core.steps.InjectableStepsFactory; |
15 | |
import org.jbehave.core.steps.ParameterConverters; |
16 | |
import org.jbehave.core.steps.ParameterConverters.ParameterConverter; |
17 | |
import org.jbehave.core.steps.spring.SpringApplicationContextFactory; |
18 | |
import org.jbehave.core.steps.spring.SpringStepsFactory; |
19 | |
import org.springframework.context.ApplicationContext; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class SpringAnnotationBuilder extends AnnotationBuilder { |
29 | |
|
30 | |
private ApplicationContext context; |
31 | |
|
32 | |
public SpringAnnotationBuilder(Class<?> annotatedClass) { |
33 | 10 | super(annotatedClass); |
34 | 10 | } |
35 | |
|
36 | |
public SpringAnnotationBuilder(Class<?> annotatedClass, AnnotationMonitor annotationMonitor) { |
37 | 1 | super(annotatedClass, annotationMonitor); |
38 | 1 | } |
39 | |
|
40 | |
@Override |
41 | |
public Configuration buildConfiguration() throws AnnotationRequired { |
42 | 11 | if (annotationFinder().isAnnotationPresent(UsingSpring.class)) { |
43 | 9 | List<String> resources = annotationFinder() |
44 | |
.getAnnotatedValues(UsingSpring.class, String.class, "resources"); |
45 | 9 | if (resources.size() > 0) { |
46 | |
try { |
47 | 7 | context = createApplicationContext(annotatedClass().getClassLoader(), resources); |
48 | 1 | } catch (Exception e) { |
49 | 1 | annotationMonitor().elementCreationFailed(ApplicationContext.class, e); |
50 | 1 | boolean ignoreContextFailure = annotationFinder().getAnnotatedValue(UsingSpring.class, boolean.class, "ignoreContextFailure"); |
51 | 1 | if ( !ignoreContextFailure ){ |
52 | 1 | throw new InstantiationFailed(annotatedClass(), e); |
53 | |
} |
54 | 6 | } |
55 | |
} |
56 | 8 | } else { |
57 | 2 | annotationMonitor().annotationNotFound(UsingSpring.class, annotatedClass()); |
58 | |
} |
59 | 10 | return super.buildConfiguration(); |
60 | |
} |
61 | |
|
62 | |
@Override |
63 | |
public InjectableStepsFactory buildStepsFactory(Configuration configuration) { |
64 | 5 | InjectableStepsFactory factoryUsingSteps = super.buildStepsFactory(configuration); |
65 | 5 | if (context != null) { |
66 | 3 | return new CompositeStepsFactory(new SpringStepsFactory(configuration, context), factoryUsingSteps); |
67 | |
} |
68 | 2 | return factoryUsingSteps; |
69 | |
} |
70 | |
|
71 | |
@Override |
72 | |
protected ParameterConverters parameterConverters(AnnotationFinder annotationFinder) { |
73 | 8 | ParameterConverters converters = super.parameterConverters(annotationFinder); |
74 | 8 | if (context != null) { |
75 | 6 | return converters.addConverters(getBeansOfType(context, ParameterConverter.class)); |
76 | |
} |
77 | 2 | return converters; |
78 | |
} |
79 | |
|
80 | |
private List<ParameterConverter> getBeansOfType(ApplicationContext context, Class<ParameterConverter> type) { |
81 | 6 | Map<String, ParameterConverter> beansOfType = context.getBeansOfType(type); |
82 | 6 | List<ParameterConverter> converters = new ArrayList<ParameterConverter>(); |
83 | 6 | for (ParameterConverter converter : beansOfType.values()) { |
84 | 6 | converters.add(converter); |
85 | 6 | } |
86 | 6 | return converters; |
87 | |
} |
88 | |
|
89 | |
@Override |
90 | |
protected <T, V extends T> T instanceOf(Class<T> type, Class<V> ofClass) { |
91 | 154 | if (context != null) { |
92 | 116 | if (!type.equals(Object.class)) { |
93 | 114 | if (context.getBeansOfType(type).size() > 0) { |
94 | 36 | return context.getBeansOfType(type).values().iterator().next(); |
95 | |
} |
96 | |
} else { |
97 | 2 | if (context.getBeansOfType(ofClass).size() > 0) { |
98 | 0 | return context.getBeansOfType(ofClass).values().iterator().next(); |
99 | |
} |
100 | |
} |
101 | |
} |
102 | 118 | return super.instanceOf(type, ofClass); |
103 | |
} |
104 | |
|
105 | |
protected ApplicationContext createApplicationContext(ClassLoader classLoader, List<String> resources) { |
106 | 7 | if (context != null) { |
107 | 1 | return context; |
108 | |
} |
109 | 6 | return new SpringApplicationContextFactory(classLoader, resources.toArray(new String[resources.size()])) |
110 | |
.createApplicationContext(); |
111 | |
} |
112 | |
|
113 | |
protected ApplicationContext applicationContext() { |
114 | 2 | return context; |
115 | |
} |
116 | |
|
117 | |
} |