1 | |
package org.jbehave.core.embedder; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.util.ArrayList; |
5 | |
import java.util.Arrays; |
6 | |
import java.util.List; |
7 | |
import java.util.Properties; |
8 | |
|
9 | |
import org.apache.commons.lang.StringUtils; |
10 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
11 | |
import org.apache.commons.lang.builder.ToStringStyle; |
12 | |
import org.jbehave.core.Embeddable; |
13 | |
import org.jbehave.core.configuration.Configuration; |
14 | |
import org.jbehave.core.configuration.MostUsefulConfiguration; |
15 | |
import org.jbehave.core.failures.BatchFailures; |
16 | |
import org.jbehave.core.junit.AnnotatedEmbedderRunner; |
17 | |
import org.jbehave.core.model.Story; |
18 | |
import org.jbehave.core.model.StoryMaps; |
19 | |
import org.jbehave.core.reporters.ReportsCount; |
20 | |
import org.jbehave.core.reporters.StepdocReporter; |
21 | |
import org.jbehave.core.reporters.StoryReporterBuilder; |
22 | |
import org.jbehave.core.reporters.ViewGenerator; |
23 | |
import org.jbehave.core.steps.CandidateSteps; |
24 | |
import org.jbehave.core.steps.StepCollector.Stage; |
25 | |
import org.jbehave.core.steps.StepFinder; |
26 | |
import org.jbehave.core.steps.Stepdoc; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class Embedder { |
33 | |
|
34 | 74 | private Configuration configuration = new MostUsefulConfiguration(); |
35 | 74 | private List<CandidateSteps> candidateSteps = new ArrayList<CandidateSteps>(); |
36 | 74 | private EmbedderClassLoader classLoader = new EmbedderClassLoader(this.getClass().getClassLoader()); |
37 | 74 | private EmbedderControls embedderControls = new EmbedderControls(); |
38 | 74 | private List<String> metaFilters = Arrays.asList(); |
39 | 74 | private Properties systemProperties = new Properties(); |
40 | |
private StoryMapper storyMapper; |
41 | |
private StoryRunner storyRunner; |
42 | |
private EmbedderMonitor embedderMonitor; |
43 | |
|
44 | |
public Embedder() { |
45 | 47 | this(new StoryMapper(), new StoryRunner(), new PrintStreamEmbedderMonitor()); |
46 | 47 | } |
47 | |
|
48 | 74 | public Embedder(StoryMapper storyMapper, StoryRunner storyRunner, EmbedderMonitor embedderMonitor) { |
49 | 74 | this.storyMapper = storyMapper; |
50 | 74 | this.storyRunner = storyRunner; |
51 | 74 | this.embedderMonitor = embedderMonitor; |
52 | 74 | } |
53 | |
|
54 | |
public void mapStoriesAsPaths(List<String> storyPaths) { |
55 | 1 | processSystemProperties(); |
56 | 1 | EmbedderControls embedderControls = embedderControls(); |
57 | 1 | if (embedderControls.skip()) { |
58 | 0 | embedderMonitor.storiesSkipped(storyPaths); |
59 | 0 | return; |
60 | |
} |
61 | |
|
62 | 1 | for (String storyPath : storyPaths) { |
63 | 2 | Story story = storyRunner.storyOfPath(configuration, storyPath); |
64 | 2 | embedderMonitor.mappingStory(storyPath, metaFilters); |
65 | 2 | storyMapper.map(story, new MetaFilter("")); |
66 | 2 | for (String filter : metaFilters) { |
67 | 0 | storyMapper.map(story, new MetaFilter(filter)); |
68 | |
} |
69 | 2 | } |
70 | |
|
71 | 1 | generateMapsView(storyMapper.getStoryMaps()); |
72 | |
|
73 | 1 | } |
74 | |
|
75 | |
private void generateMapsView(StoryMaps storyMaps) { |
76 | 1 | StoryReporterBuilder builder = configuration().storyReporterBuilder(); |
77 | 1 | File outputDirectory = builder.outputDirectory(); |
78 | 1 | Properties viewResources = builder.viewResources(); |
79 | 1 | ViewGenerator viewGenerator = configuration().viewGenerator(); |
80 | |
try { |
81 | 1 | embedderMonitor.generatingMapsView(outputDirectory, storyMaps, viewResources); |
82 | 1 | viewGenerator.generateMapsView(outputDirectory, storyMaps, viewResources); |
83 | 0 | } catch (RuntimeException e) { |
84 | 0 | embedderMonitor.mapsViewGenerationFailed(outputDirectory, storyMaps, viewResources, e); |
85 | 0 | throw new ViewGenerationFailed(outputDirectory, storyMaps, viewResources, e); |
86 | 1 | } |
87 | 1 | } |
88 | |
|
89 | |
public void runAsEmbeddables(List<String> classNames) { |
90 | 10 | EmbedderControls embedderControls = embedderControls(); |
91 | 10 | if (embedderControls.skip()) { |
92 | 1 | embedderMonitor.embeddablesSkipped(classNames); |
93 | 1 | return; |
94 | |
} |
95 | |
|
96 | 9 | BatchFailures batchFailures = new BatchFailures(); |
97 | 9 | for (Embeddable embeddable : embeddables(classNames, classLoader())) { |
98 | 15 | String name = embeddable.getClass().getName(); |
99 | |
try { |
100 | 15 | embedderMonitor.runningEmbeddable(name); |
101 | 15 | embeddable.useEmbedder(this); |
102 | 15 | embeddable.run(); |
103 | 4 | } catch (Throwable e) { |
104 | 4 | if (embedderControls.batch()) { |
105 | |
|
106 | 2 | batchFailures.put(name, e); |
107 | |
} else { |
108 | 2 | if (embedderControls.ignoreFailureInStories()) { |
109 | 1 | embedderMonitor.embeddableFailed(name, e); |
110 | |
} else { |
111 | 1 | throw new RunningEmbeddablesFailed(name, e); |
112 | |
} |
113 | |
} |
114 | 11 | } |
115 | 14 | } |
116 | |
|
117 | 8 | if (embedderControls.batch() && batchFailures.size() > 0) { |
118 | 2 | if (embedderControls.ignoreFailureInStories()) { |
119 | 1 | embedderMonitor.batchFailed(batchFailures); |
120 | |
} else { |
121 | 1 | throw new RunningEmbeddablesFailed(batchFailures); |
122 | |
} |
123 | |
} |
124 | |
|
125 | 7 | } |
126 | |
|
127 | |
private List<Embeddable> embeddables(List<String> classNames, EmbedderClassLoader classLoader) { |
128 | 9 | List<Embeddable> embeddables = new ArrayList<Embeddable>(); |
129 | 9 | for (String className : classNames) { |
130 | 17 | if (!classLoader.isAbstract(className)) { |
131 | 16 | embeddables.add(classLoader.newInstance(Embeddable.class, className)); |
132 | |
} |
133 | |
} |
134 | 9 | return embeddables; |
135 | |
} |
136 | |
|
137 | |
public void runStoriesWithAnnotatedEmbedderRunner(String runnerClass, List<String> classNames) { |
138 | 4 | List<AnnotatedEmbedderRunner> runners = annotatedEmbedderRunners(runnerClass, classNames, classLoader()); |
139 | 3 | for (AnnotatedEmbedderRunner runner : runners) { |
140 | |
try { |
141 | 3 | Object annotatedInstance = runner.createTest(); |
142 | 3 | if (annotatedInstance instanceof Embeddable) { |
143 | 2 | ((Embeddable) annotatedInstance).run(); |
144 | |
} else { |
145 | 1 | embedderMonitor.annotatedInstanceNotOfType(annotatedInstance, Embeddable.class); |
146 | |
} |
147 | 1 | } catch (Throwable e) { |
148 | 1 | throw new AnnotatedEmbedderRunFailed(runner, e); |
149 | 2 | } |
150 | |
} |
151 | 2 | } |
152 | |
|
153 | |
private List<AnnotatedEmbedderRunner> annotatedEmbedderRunners(String runnerClassName, List<String> classNames, |
154 | |
EmbedderClassLoader classLoader) { |
155 | 4 | Class<?> runnerClass = loadClass(runnerClassName, classLoader); |
156 | 4 | List<AnnotatedEmbedderRunner> runners = new ArrayList<AnnotatedEmbedderRunner>(); |
157 | 4 | for (String annotatedClassName : classNames) { |
158 | 4 | runners.add(newAnnotatedEmbedderRunner(runnerClass, annotatedClassName, classLoader)); |
159 | |
} |
160 | 3 | return runners; |
161 | |
} |
162 | |
|
163 | |
private AnnotatedEmbedderRunner newAnnotatedEmbedderRunner(Class<?> runnerClass, String annotatedClassName, |
164 | |
EmbedderClassLoader classLoader) { |
165 | |
try { |
166 | 4 | Class<?> annotatedClass = loadClass(annotatedClassName, classLoader); |
167 | 3 | return (AnnotatedEmbedderRunner) runnerClass.getConstructor(Class.class).newInstance(annotatedClass); |
168 | 1 | } catch (Exception e) { |
169 | 1 | throw new AnnotatedEmbedderRunnerInstantiationFailed(runnerClass, annotatedClassName, classLoader, e); |
170 | |
} |
171 | |
} |
172 | |
|
173 | |
private Class<?> loadClass(String className, EmbedderClassLoader classLoader) { |
174 | |
try { |
175 | 8 | return classLoader.loadClass(className); |
176 | 1 | } catch (ClassNotFoundException e) { |
177 | 1 | throw new ClassLoadingFailed(className, classLoader, e); |
178 | |
} |
179 | |
} |
180 | |
|
181 | |
public void runStoriesAsPaths(List<String> storyPaths) { |
182 | 19 | processSystemProperties(); |
183 | |
|
184 | 19 | EmbedderControls embedderControls = embedderControls(); |
185 | 19 | if (embedderControls.skip()) { |
186 | 1 | embedderMonitor.storiesSkipped(storyPaths); |
187 | 1 | return; |
188 | |
} |
189 | |
|
190 | 18 | Configuration configuration = configuration(); |
191 | 18 | List<CandidateSteps> candidateSteps = candidateSteps(); |
192 | |
|
193 | 18 | storyRunner.runBeforeOrAfterStories(configuration, candidateSteps, Stage.BEFORE); |
194 | |
|
195 | 18 | BatchFailures batchFailures = new BatchFailures(); |
196 | 18 | buildReporters(configuration, storyPaths); |
197 | 18 | MetaFilter filter = new MetaFilter(StringUtils.join(metaFilters, " "), embedderMonitor); |
198 | 18 | for (String storyPath : storyPaths) { |
199 | |
try { |
200 | 25 | embedderMonitor.runningStory(storyPath); |
201 | 25 | Story story = storyRunner.storyOfPath(configuration, storyPath); |
202 | 25 | storyRunner.run(configuration, candidateSteps, story, filter); |
203 | 7 | } catch (Throwable e) { |
204 | 7 | if (embedderControls.batch()) { |
205 | |
|
206 | 4 | batchFailures.put(storyPath, e); |
207 | |
} else { |
208 | 3 | if (embedderControls.ignoreFailureInStories()) { |
209 | 2 | embedderMonitor.storyFailed(storyPath, e); |
210 | |
} else { |
211 | 1 | throw new RunningStoriesFailed(storyPath, e); |
212 | |
} |
213 | |
} |
214 | 42 | } |
215 | |
} |
216 | |
|
217 | 17 | storyRunner.runBeforeOrAfterStories(configuration, candidateSteps, Stage.AFTER); |
218 | |
|
219 | 17 | if (embedderControls.batch() && batchFailures.size() > 0) { |
220 | 2 | if (embedderControls.ignoreFailureInStories()) { |
221 | 1 | embedderMonitor.batchFailed(batchFailures); |
222 | |
} else { |
223 | 1 | throw new RunningStoriesFailed(batchFailures); |
224 | |
} |
225 | |
} |
226 | |
|
227 | 16 | if (embedderControls.generateViewAfterStories()) { |
228 | 13 | generateReportsView(); |
229 | |
} |
230 | |
|
231 | 16 | } |
232 | |
|
233 | |
public void processSystemProperties() { |
234 | 22 | Properties properties = systemProperties(); |
235 | 22 | embedderMonitor.processingSystemProperties(properties); |
236 | 22 | if ( !properties.isEmpty() ){ |
237 | 1 | for (Object key : properties.keySet()) { |
238 | 2 | String name = (String)key; |
239 | 2 | String value = properties.getProperty(name); |
240 | 2 | System.setProperty(name, value); |
241 | 2 | embedderMonitor.systemPropertySet(name, value); |
242 | 2 | } |
243 | |
} |
244 | 22 | } |
245 | |
|
246 | |
private void buildReporters(Configuration configuration, List<String> storyPaths) { |
247 | 18 | StoryReporterBuilder reporterBuilder = configuration.storyReporterBuilder(); |
248 | 18 | configuration.useStoryReporters(reporterBuilder.build(storyPaths)); |
249 | 18 | } |
250 | |
|
251 | |
public void generateReportsView() { |
252 | 13 | StoryReporterBuilder builder = configuration().storyReporterBuilder(); |
253 | 13 | File outputDirectory = builder.outputDirectory(); |
254 | 13 | List<String> formatNames = builder.formatNames(true); |
255 | 13 | generateReportsView(outputDirectory, formatNames, builder.viewResources()); |
256 | 13 | } |
257 | |
|
258 | |
public void generateReportsView(File outputDirectory, List<String> formats, Properties viewResources) { |
259 | 18 | EmbedderControls embedderControls = embedderControls(); |
260 | |
|
261 | 18 | if (embedderControls.skip()) { |
262 | 1 | embedderMonitor.reportsViewNotGenerated(); |
263 | 1 | return; |
264 | |
} |
265 | 17 | ViewGenerator viewGenerator = configuration().viewGenerator(); |
266 | |
try { |
267 | 17 | embedderMonitor.generatingReportsView(outputDirectory, formats, viewResources); |
268 | 17 | viewGenerator.generateReportsView(outputDirectory, formats, viewResources); |
269 | 1 | } catch (RuntimeException e) { |
270 | 1 | embedderMonitor.reportsViewGenerationFailed(outputDirectory, formats, viewResources, e); |
271 | 1 | throw new ViewGenerationFailed(outputDirectory, formats, viewResources, e); |
272 | 16 | } |
273 | 16 | ReportsCount count = viewGenerator.getReportsCount(); |
274 | 16 | embedderMonitor.reportsViewGenerated(count); |
275 | 16 | if (!embedderControls.ignoreFailureInView() && count.getScenariosFailed() > 0) { |
276 | 1 | throw new RunningStoriesFailed(count.getStories(), count.getScenarios(), count.getScenariosFailed()); |
277 | |
} |
278 | |
|
279 | 15 | } |
280 | |
|
281 | |
public void reportStepdocs() { |
282 | 1 | Configuration configuration = configuration(); |
283 | 1 | List<CandidateSteps> candidateSteps = candidateSteps(); |
284 | 1 | StepFinder finder = configuration.stepFinder(); |
285 | 1 | StepdocReporter reporter = configuration.stepdocReporter(); |
286 | 1 | List<Object> stepsInstances = finder.stepsInstances(candidateSteps); |
287 | 1 | reporter.stepdocs(finder.stepdocs(candidateSteps), stepsInstances); |
288 | 1 | } |
289 | |
|
290 | |
public void reportMatchingStepdocs(String stepAsString) { |
291 | 3 | Configuration configuration = configuration(); |
292 | 3 | List<CandidateSteps> candidateSteps = candidateSteps(); |
293 | 3 | StepFinder finder = configuration.stepFinder(); |
294 | 3 | StepdocReporter reporter = configuration.stepdocReporter(); |
295 | 3 | List<Stepdoc> matching = finder.findMatching(stepAsString, candidateSteps); |
296 | 3 | List<Object> stepsInstances = finder.stepsInstances(candidateSteps); |
297 | 3 | reporter.stepdocsMatching(stepAsString, matching, stepsInstances); |
298 | 3 | } |
299 | |
|
300 | |
public EmbedderClassLoader classLoader(){ |
301 | 13 | return classLoader; |
302 | |
} |
303 | |
|
304 | |
public Configuration configuration() { |
305 | 93 | return configuration; |
306 | |
} |
307 | |
|
308 | |
public List<CandidateSteps> candidateSteps() { |
309 | 34 | return candidateSteps; |
310 | |
} |
311 | |
|
312 | |
public EmbedderControls embedderControls() { |
313 | 55 | return embedderControls; |
314 | |
} |
315 | |
|
316 | |
public EmbedderMonitor embedderMonitor() { |
317 | 2 | return embedderMonitor; |
318 | |
} |
319 | |
|
320 | |
public List<String> metaFilters() { |
321 | 2 | return metaFilters; |
322 | |
} |
323 | |
|
324 | |
public StoryRunner storyRunner() { |
325 | 2 | return storyRunner; |
326 | |
} |
327 | |
|
328 | |
public Properties systemProperties() { |
329 | 23 | return systemProperties; |
330 | |
} |
331 | |
|
332 | |
public void useClassLoader(EmbedderClassLoader classLoader) { |
333 | 14 | this.classLoader = classLoader; |
334 | 14 | } |
335 | |
|
336 | |
public void useConfiguration(Configuration configuration) { |
337 | 19 | this.configuration = configuration; |
338 | 19 | } |
339 | |
|
340 | |
public void useCandidateSteps(List<CandidateSteps> candidateSteps) { |
341 | 22 | this.candidateSteps = candidateSteps; |
342 | 22 | } |
343 | |
|
344 | |
public void useEmbedderControls(EmbedderControls embedderControls) { |
345 | 27 | this.embedderControls = embedderControls; |
346 | 27 | } |
347 | |
|
348 | |
public void useEmbedderMonitor(EmbedderMonitor embedderMonitor) { |
349 | 1 | this.embedderMonitor = embedderMonitor; |
350 | 1 | } |
351 | |
|
352 | |
public void useMetaFilters(List<String> metaFilters) { |
353 | 2 | this.metaFilters = metaFilters; |
354 | 2 | } |
355 | |
|
356 | |
public void useStoryRunner(StoryRunner storyRunner) { |
357 | 1 | this.storyRunner = storyRunner; |
358 | 1 | } |
359 | |
|
360 | |
public void useSystemProperties(Properties systemProperties) { |
361 | 1 | this.systemProperties = systemProperties; |
362 | 1 | } |
363 | |
|
364 | |
@Override |
365 | |
public String toString() { |
366 | 1 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
367 | |
} |
368 | |
|
369 | |
@SuppressWarnings("serial") |
370 | |
public static class ClassLoadingFailed extends RuntimeException { |
371 | |
|
372 | |
public ClassLoadingFailed(String className, EmbedderClassLoader classLoader, Throwable cause) { |
373 | 1 | super("Failed to load class " + className + " with classLoader " + classLoader, cause); |
374 | 1 | } |
375 | |
|
376 | |
} |
377 | |
|
378 | |
@SuppressWarnings("serial") |
379 | |
public static class AnnotatedEmbedderRunnerInstantiationFailed extends RuntimeException { |
380 | |
|
381 | |
public AnnotatedEmbedderRunnerInstantiationFailed(Class<?> runnerClass, String annotatedClassName, |
382 | |
EmbedderClassLoader classLoader, Throwable cause) { |
383 | 1 | super("Failed to instantiate annotated embedder runner " + runnerClass + " with annotatedClassName " |
384 | |
+ annotatedClassName + " and classLoader " + classLoader, cause); |
385 | 1 | } |
386 | |
|
387 | |
} |
388 | |
|
389 | |
@SuppressWarnings("serial") |
390 | |
public static class AnnotatedEmbedderRunFailed extends RuntimeException { |
391 | |
|
392 | |
public AnnotatedEmbedderRunFailed(AnnotatedEmbedderRunner runner, Throwable cause) { |
393 | 1 | super("Annotated embedder run failed with runner " + runner.toString(), cause); |
394 | 1 | } |
395 | |
|
396 | |
} |
397 | |
|
398 | |
@SuppressWarnings("serial") |
399 | |
public static class RunningEmbeddablesFailed extends RuntimeException { |
400 | |
|
401 | |
public RunningEmbeddablesFailed(String name, Throwable cause) { |
402 | 1 | super("Failures in running embeddable " + name, cause); |
403 | 1 | } |
404 | |
|
405 | |
public RunningEmbeddablesFailed(BatchFailures batchFailures) { |
406 | 1 | super("Failures in running embeddables in batch: " + batchFailures); |
407 | 1 | } |
408 | |
|
409 | |
} |
410 | |
|
411 | |
@SuppressWarnings("serial") |
412 | |
public static class RunningStoriesFailed extends RuntimeException { |
413 | |
|
414 | |
public RunningStoriesFailed(int stories, int scenarios, int failedScenarios) { |
415 | 1 | super("Failures in running " + stories + " stories containing " + scenarios + " scenarios (of which " |
416 | |
+ failedScenarios + " failed)"); |
417 | 1 | } |
418 | |
|
419 | |
public RunningStoriesFailed(BatchFailures failures) { |
420 | 1 | super("Failures in running stories in batch: " + failures); |
421 | 1 | } |
422 | |
|
423 | |
public RunningStoriesFailed(String name, Throwable cause) { |
424 | 1 | super("Failures in running stories " + name, cause); |
425 | 1 | } |
426 | |
} |
427 | |
|
428 | |
@SuppressWarnings("serial") |
429 | |
public static class ViewGenerationFailed extends RuntimeException { |
430 | |
public ViewGenerationFailed(File outputDirectory, List<String> formats, Properties viewResources, |
431 | |
RuntimeException cause) { |
432 | 1 | super("View generation failed to " + outputDirectory + " for formats " + formats + " and resources " |
433 | |
+ viewResources, cause); |
434 | 1 | } |
435 | |
|
436 | |
public ViewGenerationFailed(File outputDirectory, StoryMaps storyMaps, Properties viewResources, |
437 | |
RuntimeException cause) { |
438 | 0 | super("View generation failed to " + outputDirectory + " for story maps "+ storyMaps +" for resources " |
439 | |
+ viewResources, cause); |
440 | 0 | } |
441 | |
} |
442 | |
} |