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