1 | |
package org.jbehave.core.configuration; |
2 | |
|
3 | |
import java.io.ByteArrayInputStream; |
4 | |
import java.io.IOException; |
5 | |
import java.lang.reflect.Constructor; |
6 | |
import java.util.ArrayList; |
7 | |
import java.util.List; |
8 | |
import java.util.Properties; |
9 | |
|
10 | |
import org.jbehave.core.ConfigurableEmbedder; |
11 | |
import org.jbehave.core.Embeddable; |
12 | |
import org.jbehave.core.annotations.Configure; |
13 | |
import org.jbehave.core.annotations.UsingEmbedder; |
14 | |
import org.jbehave.core.annotations.UsingPaths; |
15 | |
import org.jbehave.core.annotations.UsingSteps; |
16 | |
import org.jbehave.core.embedder.Embedder; |
17 | |
import org.jbehave.core.embedder.StoryControls; |
18 | |
import org.jbehave.core.failures.FailureStrategy; |
19 | |
import org.jbehave.core.failures.PendingStepStrategy; |
20 | |
import org.jbehave.core.io.PathCalculator; |
21 | |
import org.jbehave.core.io.StoryFinder; |
22 | |
import org.jbehave.core.io.StoryLoader; |
23 | |
import org.jbehave.core.io.StoryPathResolver; |
24 | |
import org.jbehave.core.parsers.StepPatternParser; |
25 | |
import org.jbehave.core.parsers.StoryParser; |
26 | |
import org.jbehave.core.reporters.StepdocReporter; |
27 | |
import org.jbehave.core.reporters.StoryReporter; |
28 | |
import org.jbehave.core.reporters.StoryReporterBuilder; |
29 | |
import org.jbehave.core.reporters.ViewGenerator; |
30 | |
import org.jbehave.core.steps.CandidateSteps; |
31 | |
import org.jbehave.core.steps.InjectableStepsFactory; |
32 | |
import org.jbehave.core.steps.InstanceStepsFactory; |
33 | |
import org.jbehave.core.steps.ParameterControls; |
34 | |
import org.jbehave.core.steps.ParameterConverters; |
35 | |
import org.jbehave.core.steps.ParameterConverters.ParameterConverter; |
36 | |
import org.jbehave.core.steps.StepCollector; |
37 | |
import org.jbehave.core.steps.StepFinder; |
38 | |
import org.jbehave.core.steps.StepMonitor; |
39 | |
|
40 | |
import com.thoughtworks.paranamer.Paranamer; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class AnnotationBuilder { |
50 | |
|
51 | |
private final AnnotationMonitor annotationMonitor; |
52 | |
|
53 | |
private final Class<?> annotatedClass; |
54 | |
private final AnnotationFinder finder; |
55 | |
|
56 | |
public AnnotationBuilder(Class<?> annotatedClass) { |
57 | 17 | this(annotatedClass, new PrintStreamAnnotationMonitor()); |
58 | 17 | } |
59 | |
|
60 | 20 | public AnnotationBuilder(Class<?> annotatedClass, AnnotationMonitor annotationMonitor) { |
61 | 20 | this.annotationMonitor = annotationMonitor; |
62 | 20 | this.annotatedClass = annotatedClass; |
63 | 20 | this.finder = new AnnotationFinder(annotatedClass); |
64 | 20 | } |
65 | |
|
66 | |
public Class<?> annotatedClass() { |
67 | 1 | return annotatedClass; |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public Configuration buildConfiguration() throws AnnotationRequired { |
77 | |
|
78 | 16 | if (!finder.isAnnotationPresent(Configure.class)) { |
79 | |
|
80 | |
|
81 | 4 | return new MostUsefulConfiguration(); |
82 | |
} |
83 | |
|
84 | 12 | Configuration configuration = configurationElement(finder, "using", Configuration.class); |
85 | 12 | configuration.useKeywords(configurationElement(finder, "keywords", Keywords.class)); |
86 | 12 | configuration.useFailureStrategy(configurationElement(finder, "failureStrategy", FailureStrategy.class)); |
87 | 12 | configuration.usePendingStepStrategy(configurationElement(finder, "pendingStepStrategy", |
88 | |
PendingStepStrategy.class)); |
89 | 12 | configuration.useParanamer(configurationElement(finder, "paranamer", Paranamer.class)); |
90 | 12 | configuration.useStoryControls(configurationElement(finder, "storyControls", StoryControls.class)); |
91 | 12 | configuration.useStepCollector(configurationElement(finder, "stepCollector", StepCollector.class)); |
92 | 12 | configuration.useStepdocReporter(configurationElement(finder, "stepdocReporter", StepdocReporter.class)); |
93 | 12 | configuration.useStepFinder(configurationElement(finder, "stepFinder", StepFinder.class)); |
94 | 12 | configuration.useStepMonitor(configurationElement(finder, "stepMonitor", StepMonitor.class)); |
95 | 12 | configuration.useStepPatternParser(configurationElement(finder, "stepPatternParser", StepPatternParser.class)); |
96 | 12 | configuration.useStoryLoader(configurationElement(finder, "storyLoader", StoryLoader.class)); |
97 | 12 | configuration.useStoryParser(configurationElement(finder, "storyParser", StoryParser.class)); |
98 | 12 | configuration.useStoryPathResolver(configurationElement(finder, "storyPathResolver", StoryPathResolver.class)); |
99 | 12 | configuration |
100 | |
.useDefaultStoryReporter(configurationElement(finder, "defaultStoryReporter", StoryReporter.class)); |
101 | 12 | configuration.useStoryReporterBuilder(configurationElement(finder, "storyReporterBuilder", |
102 | |
StoryReporterBuilder.class)); |
103 | 12 | configuration.useViewGenerator(configurationElement(finder, "viewGenerator", ViewGenerator.class)); |
104 | 12 | configuration.useParameterConverters(parameterConverters(finder)); |
105 | 12 | configuration.useParameterControls(configurationElement(finder, "parameterControls", ParameterControls.class)); |
106 | 12 | configuration.usePathCalculator(configurationElement(finder, "pathCalculator", PathCalculator.class)); |
107 | 12 | return configuration; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public List<CandidateSteps> buildCandidateSteps() { |
118 | 6 | return buildCandidateSteps(buildConfiguration()); |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public List<CandidateSteps> buildCandidateSteps(Configuration configuration) { |
129 | 8 | return buildStepsFactory(configuration).createCandidateSteps(); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public InjectableStepsFactory buildStepsFactory(Configuration configuration) { |
141 | 15 | List<Object> stepsInstances = new ArrayList<Object>(); |
142 | 15 | InjectableStepsFactory factory = null; |
143 | 15 | if (finder.isAnnotationPresent(UsingSteps.class)) { |
144 | 11 | List<Class<Object>> stepsClasses = finder.getAnnotatedClasses(UsingSteps.class, Object.class, "instances"); |
145 | 11 | for (Class<Object> stepsClass : stepsClasses) { |
146 | 14 | stepsInstances.add(instanceOf(Object.class, stepsClass)); |
147 | 13 | } |
148 | 10 | factory = new InstanceStepsFactory(configuration, stepsInstances); |
149 | 10 | } else { |
150 | 4 | annotationMonitor.annotationNotFound(UsingSteps.class, annotatedClass); |
151 | |
} |
152 | |
|
153 | 14 | if (factory == null) { |
154 | 4 | factory = new InstanceStepsFactory(configuration); |
155 | |
} |
156 | 14 | return factory; |
157 | |
} |
158 | |
|
159 | |
public Embedder buildEmbedder() { |
160 | 10 | if (!finder.isAnnotationPresent(UsingEmbedder.class)) { |
161 | 1 | return new Embedder(); |
162 | |
} |
163 | |
|
164 | 9 | boolean batch = control(finder, "batch"); |
165 | 9 | boolean skip = control(finder, "skip"); |
166 | 9 | boolean generateViewAfterStories = control(finder, "generateViewAfterStories"); |
167 | 9 | boolean ignoreFailureInStories = control(finder, "ignoreFailureInStories"); |
168 | 9 | boolean ignoreFailureInView = control(finder, "ignoreFailureInView"); |
169 | 9 | boolean verboseFailures = control(finder, "verboseFailures"); |
170 | 9 | boolean verboseFiltering = control(finder, "verboseFiltering"); |
171 | 9 | long storyTimeoutInSecs = finder.getAnnotatedValue(UsingEmbedder.class, Long.class, "storyTimeoutInSecs"); |
172 | 9 | boolean failOnStoryTimeout = control(finder, "failOnStoryTimeout"); |
173 | 9 | int threads = finder.getAnnotatedValue(UsingEmbedder.class, Integer.class, "threads"); |
174 | 9 | Embedder embedder = embedder(); |
175 | 9 | embedder.embedderControls().doBatch(batch).doSkip(skip).doGenerateViewAfterStories(generateViewAfterStories) |
176 | |
.doIgnoreFailureInStories(ignoreFailureInStories).doIgnoreFailureInView(ignoreFailureInView) |
177 | |
.doVerboseFailures(verboseFailures).doVerboseFiltering(verboseFiltering).useStoryTimeoutInSecs(storyTimeoutInSecs) |
178 | |
.doFailOnStoryTimeout(failOnStoryTimeout).useThreads(threads); |
179 | 9 | Configuration configuration = buildConfiguration(); |
180 | 9 | embedder.useConfiguration(configuration); |
181 | 9 | boolean useStepsFactory = finder.getAnnotatedValue(UsingEmbedder.class, Boolean.class, "stepsFactory"); |
182 | 9 | if (useStepsFactory) { |
183 | 7 | embedder.useStepsFactory(buildStepsFactory(configuration)); |
184 | |
} else { |
185 | 2 | embedder.useCandidateSteps(buildCandidateSteps(configuration)); |
186 | |
} |
187 | 9 | List<String> metaFilters = finder.getAnnotatedValues(UsingEmbedder.class, String.class, "metaFilters"); |
188 | 9 | if (!metaFilters.isEmpty()) { |
189 | 4 | embedder.useMetaFilters(metaFilters); |
190 | |
} |
191 | 9 | Properties systemProperties = loadProperties(finder.getAnnotatedValue(UsingEmbedder.class, String.class, |
192 | |
"systemProperties")); |
193 | 9 | if (!systemProperties.isEmpty()) { |
194 | 1 | embedder.useSystemProperties(systemProperties); |
195 | |
} |
196 | 9 | return embedder; |
197 | |
} |
198 | |
|
199 | |
@SuppressWarnings("unchecked") |
200 | |
private Embedder embedder() { |
201 | 9 | return instanceOf(Embedder.class, |
202 | |
(Class<? extends Embedder>) finder.getAnnotatedValue(UsingEmbedder.class, Class.class, "embedder")); |
203 | |
} |
204 | |
|
205 | |
public List<String> findPaths() { |
206 | 2 | if (!finder.isAnnotationPresent(UsingPaths.class)) { |
207 | 1 | return new ArrayList<String>(); |
208 | |
} |
209 | |
|
210 | 1 | String searchIn = finder.getAnnotatedValue(UsingPaths.class, String.class, "searchIn"); |
211 | 1 | List<String> includes = finder.getAnnotatedValues(UsingPaths.class, String.class, "includes"); |
212 | 1 | List<String> excludes = finder.getAnnotatedValues(UsingPaths.class, String.class, "excludes"); |
213 | 1 | return storyFinder().findPaths(searchIn, includes, excludes); |
214 | |
} |
215 | |
|
216 | |
@SuppressWarnings("unchecked") |
217 | |
private StoryFinder storyFinder() { |
218 | 1 | return instanceOf(StoryFinder.class, (Class<? extends StoryFinder>)finder.getAnnotatedValue(UsingPaths.class, Class.class, "storyFinder")); |
219 | |
} |
220 | |
|
221 | |
private boolean control(AnnotationFinder finder, String name) { |
222 | 72 | return finder.getAnnotatedValue(UsingEmbedder.class, Boolean.class, name); |
223 | |
} |
224 | |
|
225 | |
private <T> T configurationElement(AnnotationFinder finder, String name, Class<T> type) { |
226 | 228 | Class<T> implementation = elementImplementation(finder, name); |
227 | 228 | return instanceOf(type, implementation); |
228 | |
} |
229 | |
|
230 | |
@SuppressWarnings("unchecked") |
231 | |
private <T> Class<T> elementImplementation(AnnotationFinder finder, String name) { |
232 | 228 | return finder.getAnnotatedValue(Configure.class, Class.class, name); |
233 | |
} |
234 | |
|
235 | |
private Properties loadProperties(String systemPropertiesCSV) { |
236 | 9 | Properties properties = new Properties(); |
237 | |
try { |
238 | 9 | properties.load(new ByteArrayInputStream(systemPropertiesCSV.replace(",", "\n").getBytes())); |
239 | 0 | } catch (IOException e) { |
240 | |
|
241 | 9 | } |
242 | 9 | return properties; |
243 | |
} |
244 | |
|
245 | |
protected ParameterConverters parameterConverters(AnnotationFinder annotationFinder) { |
246 | 12 | List<ParameterConverter> converters = new ArrayList<ParameterConverter>(); |
247 | 12 | for (Class<ParameterConverter> converterClass : annotationFinder.getAnnotatedClasses(Configure.class, |
248 | |
ParameterConverter.class, "parameterConverters")) { |
249 | 3 | converters.add(instanceOf(ParameterConverter.class, converterClass)); |
250 | 3 | } |
251 | 12 | return new ParameterConverters().addConverters(converters); |
252 | |
} |
253 | |
|
254 | |
protected <T, V extends T> T instanceOf(Class<T> type, Class<V> ofClass) { |
255 | |
try { |
256 | |
|
257 | |
try { |
258 | 255 | Constructor<V> constructor = |
259 | |
ofClass.getConstructor(new Class<?>[]{ClassLoader.class}); |
260 | 12 | return constructor.newInstance(annotatedClass.getClassLoader()); |
261 | |
} |
262 | 243 | catch(NoSuchMethodException ns){ |
263 | |
} |
264 | |
|
265 | |
try { |
266 | 243 | Constructor<V> constructor = |
267 | |
ofClass.getConstructor(new Class<?>[]{Class.class}); |
268 | 12 | return constructor.newInstance(annotatedClass); |
269 | |
} |
270 | 231 | catch(NoSuchMethodException ns){ |
271 | |
} |
272 | |
|
273 | 231 | return ofClass.newInstance(); |
274 | |
} |
275 | 1 | catch (Exception e) { |
276 | 1 | annotationMonitor.elementCreationFailed(ofClass, e); |
277 | 1 | throw new InstantiationFailed(ofClass, type, e); |
278 | |
} |
279 | |
} |
280 | |
|
281 | |
protected AnnotationMonitor annotationMonitor() { |
282 | 1 | return annotationMonitor; |
283 | |
} |
284 | |
|
285 | |
protected AnnotationFinder annotationFinder() { |
286 | 1 | return finder; |
287 | |
} |
288 | |
|
289 | |
public Object embeddableInstance() { |
290 | 8 | return injectEmbedder(buildEmbedder(), annotatedClass); |
291 | |
} |
292 | |
|
293 | |
protected Object injectEmbedder(Embedder embedder, Class<?> annotatedClass) { |
294 | |
try { |
295 | 8 | Object instance = annotatedClass.newInstance(); |
296 | 7 | if (instance instanceof Embeddable) { |
297 | 6 | Embeddable embeddable = (Embeddable) instance; |
298 | 6 | embeddable.useEmbedder(embedder); |
299 | |
} |
300 | 7 | if (instance instanceof ConfigurableEmbedder) { |
301 | 2 | ConfigurableEmbedder configurableEmbedder = (ConfigurableEmbedder) instance; |
302 | 2 | configurableEmbedder.useConfiguration(embedder.configuration()); |
303 | 2 | configurableEmbedder.addSteps(embedder.candidateSteps()); |
304 | 2 | configurableEmbedder.useStepsFactory(embedder.stepsFactory()); |
305 | |
} |
306 | 7 | return instance; |
307 | 1 | } catch (Exception e) { |
308 | 1 | annotationMonitor.elementCreationFailed(annotatedClass, e); |
309 | 1 | throw new InstantiationFailed(annotatedClass, e); |
310 | |
} |
311 | |
} |
312 | |
|
313 | |
@SuppressWarnings("serial") |
314 | |
public static class InstantiationFailed extends RuntimeException { |
315 | |
|
316 | |
public InstantiationFailed(Class<?> ofClass, Class<?> type, Throwable cause) { |
317 | 1 | super("Failed to instantiate " + ofClass + " of type " + type, cause); |
318 | 1 | } |
319 | |
|
320 | |
public InstantiationFailed(Class<?> ofClass, Throwable cause) { |
321 | 1 | super("Failed to instantiate " + ofClass, cause); |
322 | 1 | } |
323 | |
|
324 | |
} |
325 | |
|
326 | |
} |