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 | |
.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 | |
packages.toArray(new String[packages.size()])) |
163 | |
.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 new Embedder(); |
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 | boolean failOnStoryTimeout = control(finder, "failOnStoryTimeout"); |
189 | 9 | int threads = finder.getAnnotatedValue(UsingEmbedder.class, Integer.class, "threads"); |
190 | 9 | Embedder embedder = embedder(); |
191 | 9 | embedder.embedderControls().doBatch(batch).doSkip(skip).doGenerateViewAfterStories(generateViewAfterStories) |
192 | |
.doIgnoreFailureInStories(ignoreFailureInStories).doIgnoreFailureInView(ignoreFailureInView) |
193 | |
.doVerboseFailures(verboseFailures).doVerboseFiltering(verboseFiltering).useStoryTimeoutInSecs(storyTimeoutInSecs) |
194 | |
.doFailOnStoryTimeout(failOnStoryTimeout).useThreads(threads); |
195 | 9 | Configuration configuration = buildConfiguration(); |
196 | 9 | embedder.useConfiguration(configuration); |
197 | 9 | boolean useStepsFactory = finder.getAnnotatedValue(UsingEmbedder.class, Boolean.class, "stepsFactory"); |
198 | 9 | if (useStepsFactory) { |
199 | 7 | embedder.useStepsFactory(buildStepsFactory(configuration)); |
200 | |
} else { |
201 | 2 | embedder.useCandidateSteps(buildCandidateSteps(configuration)); |
202 | |
} |
203 | 9 | List<String> metaFilters = finder.getAnnotatedValues(UsingEmbedder.class, String.class, "metaFilters"); |
204 | 9 | if (!metaFilters.isEmpty()) { |
205 | 4 | embedder.useMetaFilters(metaFilters); |
206 | |
} |
207 | 9 | Properties systemProperties = loadProperties(finder.getAnnotatedValue(UsingEmbedder.class, String.class, |
208 | |
"systemProperties")); |
209 | 9 | if (!systemProperties.isEmpty()) { |
210 | 1 | embedder.useSystemProperties(systemProperties); |
211 | |
} |
212 | 9 | return embedder; |
213 | |
} |
214 | |
|
215 | |
@SuppressWarnings("unchecked") |
216 | |
private Embedder embedder() { |
217 | 9 | return instanceOf(Embedder.class, |
218 | |
(Class<? extends Embedder>) finder.getAnnotatedValue(UsingEmbedder.class, Class.class, "embedder")); |
219 | |
} |
220 | |
|
221 | |
public List<String> findPaths() { |
222 | 2 | if (!finder.isAnnotationPresent(UsingPaths.class)) { |
223 | 1 | return new ArrayList<String>(); |
224 | |
} |
225 | |
|
226 | 1 | String searchIn = finder.getAnnotatedValue(UsingPaths.class, String.class, "searchIn"); |
227 | 1 | List<String> includes = finder.getAnnotatedValues(UsingPaths.class, String.class, "includes"); |
228 | 1 | List<String> excludes = finder.getAnnotatedValues(UsingPaths.class, String.class, "excludes"); |
229 | 1 | return storyFinder().findPaths(searchIn, includes, excludes); |
230 | |
} |
231 | |
|
232 | |
@SuppressWarnings("unchecked") |
233 | |
private StoryFinder storyFinder() { |
234 | 1 | return instanceOf(StoryFinder.class, (Class<? extends StoryFinder>)finder.getAnnotatedValue(UsingPaths.class, Class.class, "storyFinder")); |
235 | |
} |
236 | |
|
237 | |
private boolean control(AnnotationFinder finder, String name) { |
238 | 72 | return finder.getAnnotatedValue(UsingEmbedder.class, Boolean.class, name); |
239 | |
} |
240 | |
|
241 | |
private <T> T configurationElement(AnnotationFinder finder, String name, Class<T> type) { |
242 | 247 | Class<T> implementation = elementImplementation(finder, name); |
243 | 247 | return instanceOf(type, implementation); |
244 | |
} |
245 | |
|
246 | |
@SuppressWarnings("unchecked") |
247 | |
private <T> Class<T> elementImplementation(AnnotationFinder finder, String name) { |
248 | 247 | return finder.getAnnotatedValue(Configure.class, Class.class, name); |
249 | |
} |
250 | |
|
251 | |
private Properties loadProperties(String systemPropertiesCSV) { |
252 | 9 | Properties properties = new Properties(); |
253 | |
try { |
254 | 9 | properties.load(new ByteArrayInputStream(systemPropertiesCSV.replace(",", "\n").getBytes())); |
255 | 0 | } catch (IOException e) { |
256 | |
|
257 | 9 | } |
258 | 9 | return properties; |
259 | |
} |
260 | |
|
261 | |
protected ParameterConverters parameterConverters(AnnotationFinder annotationFinder) { |
262 | 13 | List<ParameterConverter> converters = new ArrayList<ParameterConverter>(); |
263 | 13 | for (Class<ParameterConverter> converterClass : annotationFinder.getAnnotatedClasses(Configure.class, |
264 | |
ParameterConverter.class, "parameterConverters")) { |
265 | 3 | converters.add(instanceOf(ParameterConverter.class, converterClass)); |
266 | 3 | } |
267 | 13 | return new ParameterConverters().addConverters(converters); |
268 | |
} |
269 | |
|
270 | |
protected <T, V extends T> T instanceOf(Class<T> type, Class<V> ofClass) { |
271 | |
try { |
272 | |
|
273 | |
try { |
274 | 274 | Constructor<V> constructor = |
275 | |
ofClass.getConstructor(new Class<?>[]{ClassLoader.class}); |
276 | 13 | return constructor.newInstance(annotatedClass.getClassLoader()); |
277 | |
} |
278 | 261 | catch(NoSuchMethodException ns){ |
279 | |
} |
280 | |
|
281 | |
try { |
282 | 261 | Constructor<V> constructor = |
283 | |
ofClass.getConstructor(new Class<?>[]{Class.class}); |
284 | 13 | return constructor.newInstance(annotatedClass); |
285 | |
} |
286 | 248 | catch(NoSuchMethodException ns){ |
287 | |
} |
288 | |
|
289 | 248 | return ofClass.newInstance(); |
290 | |
} |
291 | 1 | catch (Exception e) { |
292 | 1 | annotationMonitor.elementCreationFailed(ofClass, e); |
293 | 1 | throw new InstantiationFailed(ofClass, type, e); |
294 | |
} |
295 | |
} |
296 | |
|
297 | |
protected AnnotationMonitor annotationMonitor() { |
298 | 1 | return annotationMonitor; |
299 | |
} |
300 | |
|
301 | |
protected AnnotationFinder annotationFinder() { |
302 | 1 | return finder; |
303 | |
} |
304 | |
|
305 | |
public Object embeddableInstance() { |
306 | 8 | return injectEmbedder(buildEmbedder(), annotatedClass); |
307 | |
} |
308 | |
|
309 | |
protected Object injectEmbedder(Embedder embedder, Class<?> annotatedClass) { |
310 | |
try { |
311 | 8 | Object instance = annotatedClass.newInstance(); |
312 | 7 | if (instance instanceof Embeddable) { |
313 | 6 | Embeddable embeddable = (Embeddable) instance; |
314 | 6 | embeddable.useEmbedder(embedder); |
315 | |
} |
316 | 7 | if (instance instanceof ConfigurableEmbedder) { |
317 | 2 | ConfigurableEmbedder configurableEmbedder = (ConfigurableEmbedder) instance; |
318 | 2 | configurableEmbedder.useConfiguration(embedder.configuration()); |
319 | 2 | configurableEmbedder.addSteps(embedder.candidateSteps()); |
320 | 2 | configurableEmbedder.useStepsFactory(embedder.stepsFactory()); |
321 | |
} |
322 | 7 | return instance; |
323 | 1 | } catch (Exception e) { |
324 | 1 | annotationMonitor.elementCreationFailed(annotatedClass, e); |
325 | 1 | throw new InstantiationFailed(annotatedClass, e); |
326 | |
} |
327 | |
} |
328 | |
|
329 | |
@SuppressWarnings("serial") |
330 | |
public static class InstantiationFailed extends RuntimeException { |
331 | |
|
332 | |
public InstantiationFailed(Class<?> ofClass, Class<?> type, Throwable cause) { |
333 | 1 | super("Failed to instantiate " + ofClass + " of type " + type, cause); |
334 | 1 | } |
335 | |
|
336 | |
public InstantiationFailed(Class<?> ofClass, Throwable cause) { |
337 | 1 | super("Failed to instantiate " + ofClass, cause); |
338 | 1 | } |
339 | |
|
340 | |
} |
341 | |
|
342 | |
} |