1 | |
package org.jbehave.mojo; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.net.MalformedURLException; |
5 | |
import java.net.URL; |
6 | |
import java.util.ArrayList; |
7 | |
import java.util.List; |
8 | |
import java.util.Properties; |
9 | |
import java.util.concurrent.ExecutorService; |
10 | |
|
11 | |
import org.apache.maven.plugin.AbstractMojo; |
12 | |
import org.jbehave.core.ConfigurableEmbedder; |
13 | |
import org.jbehave.core.InjectableEmbedder; |
14 | |
import org.jbehave.core.embedder.Embedder; |
15 | |
import org.jbehave.core.embedder.EmbedderClassLoader; |
16 | |
import org.jbehave.core.embedder.EmbedderControls; |
17 | |
import org.jbehave.core.embedder.EmbedderMonitor; |
18 | |
import org.jbehave.core.embedder.MetaFilter; |
19 | |
import org.jbehave.core.embedder.NullEmbedderMonitor; |
20 | |
import org.jbehave.core.embedder.UnmodifiableEmbedderControls; |
21 | |
import org.jbehave.core.failures.BatchFailures; |
22 | |
import org.jbehave.core.io.StoryFinder; |
23 | |
import org.jbehave.core.junit.AnnotatedEmbedderRunner; |
24 | |
import org.jbehave.core.model.Meta; |
25 | |
import org.jbehave.core.model.Story; |
26 | |
import org.jbehave.core.model.StoryDuration; |
27 | |
import org.jbehave.core.model.StoryMaps; |
28 | |
import org.jbehave.core.reporters.ReportsCount; |
29 | |
|
30 | |
import static org.apache.commons.lang.ArrayUtils.isNotEmpty; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 27 | public abstract class AbstractEmbedderMojo extends AbstractMojo { |
39 | |
|
40 | |
static final String TEST_SCOPE = "test"; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
String sourceDirectory; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
String testSourceDirectory; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
String outputDirectory; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
String testOutputDirectory; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
String scope; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
List<String> includes; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
List<String> excludes; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
List<String> compileClasspathElements; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
List<String> testClasspathElements; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | 27 | boolean skip = false; |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | 27 | boolean batch = false; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | 27 | boolean ignoreFailureInStories = false; |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | 27 | boolean ignoreFailureInView = false; |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 27 | boolean generateViewAfterStories = true; |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | 27 | boolean verboseFailures = false; |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | 27 | boolean verboseFiltering = false; |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | 27 | long storyTimeoutInSecs = 300; |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | 27 | int threads = 1; |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | 27 | String embedderClass = Embedder.class.getName(); |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
String injectableEmbedderClass; |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | 27 | String annotatedEmbedderRunnerClass = AnnotatedEmbedderRunner.class.getName(); |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | 27 | String storyFinderClass = StoryFinder.class.getName(); |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
String[] metaFilters; |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | 27 | Properties systemProperties = new Properties(); |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
private EmbedderClassLoader classLoader; |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
boolean isTestScope() { |
224 | 30 | return TEST_SCOPE.equals(scope); |
225 | |
} |
226 | |
|
227 | |
String searchDirectory() { |
228 | 11 | if (isTestScope()) { |
229 | 1 | return testSourceDirectory; |
230 | |
} |
231 | 10 | return sourceDirectory; |
232 | |
} |
233 | |
|
234 | |
String outputDirectory() { |
235 | 9 | if (isTestScope()) { |
236 | 0 | return testOutputDirectory; |
237 | |
} |
238 | 9 | return outputDirectory; |
239 | |
} |
240 | |
|
241 | |
URL codeLocation() { |
242 | 9 | String outputDirectory = outputDirectory(); |
243 | |
try { |
244 | 9 | return outputDirectory != null ? new File(outputDirectory).toURI().toURL() : null; |
245 | 0 | } catch (MalformedURLException e) { |
246 | 0 | throw new IllegalArgumentException("Failed to create code location from " + outputDirectory, e); |
247 | |
} |
248 | |
} |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
protected EmbedderClassLoader classLoader() { |
257 | 9 | if (classLoader == null) { |
258 | 9 | classLoader = new EmbedderClassLoader(classpathElements()); |
259 | |
} |
260 | 9 | return classLoader; |
261 | |
} |
262 | |
|
263 | |
List<String> classpathElements() { |
264 | 10 | List<String> classpathElements = compileClasspathElements; |
265 | 10 | if (isTestScope()) { |
266 | 1 | classpathElements = testClasspathElements; |
267 | |
} |
268 | 10 | return classpathElements; |
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
protected List<String> storyPaths() { |
279 | 4 | getLog().debug("Searching for story paths including " + includes + " and excluding " + excludes); |
280 | 4 | List<String> storyPaths = newStoryFinder().findPaths(searchDirectory(), includes, excludes); |
281 | 4 | getLog().info("Found story paths: " + storyPaths); |
282 | 4 | return storyPaths; |
283 | |
} |
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
protected List<String> classNames() { |
293 | 6 | getLog().debug("Searching for class names including " + includes + " and excluding " + excludes); |
294 | 6 | List<String> classNames = newStoryFinder().findClassNames(searchDirectory(), includes, excludes); |
295 | 6 | getLog().info("Found class names: " + classNames); |
296 | 6 | return classNames; |
297 | |
} |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
protected StoryFinder newStoryFinder() { |
305 | 11 | return classLoader().newInstance(StoryFinder.class, storyFinderClass); |
306 | |
} |
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
protected Embedder newEmbedder() { |
316 | 8 | Embedder embedder = null; |
317 | 8 | EmbedderClassLoader classLoader = classLoader(); |
318 | 8 | if (injectableEmbedderClass != null) { |
319 | 1 | embedder = classLoader.newInstance(InjectableEmbedder.class, injectableEmbedderClass).injectedEmbedder(); |
320 | |
} else { |
321 | 7 | embedder = classLoader.newInstance(Embedder.class, embedderClass); |
322 | |
} |
323 | |
|
324 | 8 | URL codeLocation = codeLocation(); |
325 | 8 | if (codeLocation != null) { |
326 | 0 | embedder.configuration().storyReporterBuilder().withCodeLocation(codeLocation); |
327 | |
} |
328 | |
|
329 | 8 | embedder.useClassLoader(classLoader); |
330 | 8 | embedder.useEmbedderControls(embedderControls()); |
331 | 8 | embedder.useEmbedderMonitor(embedderMonitor()); |
332 | 8 | if (isNotEmpty(metaFilters)) { |
333 | 2 | List<String> filters = new ArrayList<String>(); |
334 | 6 | for (String filter : metaFilters) { |
335 | 4 | if (filter != null) { |
336 | 3 | filters.add(filter); |
337 | |
} |
338 | |
} |
339 | 2 | embedder.useMetaFilters(filters); |
340 | |
} |
341 | 8 | if (!systemProperties.isEmpty()) { |
342 | 1 | embedder.useSystemProperties(systemProperties); |
343 | |
} |
344 | 8 | return embedder; |
345 | |
} |
346 | |
|
347 | |
protected EmbedderMonitor embedderMonitor() { |
348 | 8 | return new MavenEmbedderMonitor(); |
349 | |
} |
350 | |
|
351 | |
protected EmbedderControls embedderControls() { |
352 | 8 | return new UnmodifiableEmbedderControls(new EmbedderControls().doBatch(batch).doSkip(skip) |
353 | |
.doGenerateViewAfterStories(generateViewAfterStories).doIgnoreFailureInStories(ignoreFailureInStories) |
354 | |
.doIgnoreFailureInView(ignoreFailureInView).doVerboseFailures(verboseFailures) |
355 | |
.doVerboseFiltering(verboseFiltering).useStoryTimeoutInSecs(storyTimeoutInSecs).useThreads(threads)); |
356 | |
} |
357 | |
|
358 | 27 | protected class MavenEmbedderMonitor extends NullEmbedderMonitor { |
359 | |
|
360 | |
public void batchFailed(BatchFailures failures) { |
361 | 1 | getLog().warn("Failed to run batch " + failures); |
362 | 1 | } |
363 | |
|
364 | |
public void beforeOrAfterStoriesFailed() { |
365 | 0 | getLog().warn("Failed to run before or after stories steps"); |
366 | 0 | } |
367 | |
|
368 | |
public void embeddableFailed(String name, Throwable cause) { |
369 | 1 | getLog().warn("Failed to run embeddable " + name, cause); |
370 | 1 | } |
371 | |
|
372 | |
public void embeddableNotConfigurable(String name) { |
373 | 0 | getLog().warn("Embeddable " + name + " must be an instance of " + ConfigurableEmbedder.class); |
374 | 0 | } |
375 | |
|
376 | |
public void embeddablesSkipped(List<String> classNames) { |
377 | 1 | getLog().info("Skipped embeddables " + classNames); |
378 | 1 | } |
379 | |
|
380 | |
public void metaNotAllowed(Meta meta, MetaFilter filter) { |
381 | 0 | getLog().debug(meta + " excluded by filter '" + filter.asString() + "'"); |
382 | 0 | } |
383 | |
|
384 | |
public void runningEmbeddable(String name) { |
385 | 1 | getLog().info("Running embeddable " + name); |
386 | 1 | } |
387 | |
|
388 | |
public void runningStory(String path) { |
389 | 1 | getLog().info("Running story " + path); |
390 | 1 | } |
391 | |
|
392 | |
public void storyFailed(String path, Throwable cause) { |
393 | 1 | getLog().warn("Failed to run story " + path, cause); |
394 | 1 | } |
395 | |
|
396 | |
public void storiesSkipped(List<String> storyPaths) { |
397 | 1 | getLog().info("Skipped stories " + storyPaths); |
398 | 1 | } |
399 | |
|
400 | |
public void storiesNotAllowed(List<Story> stories, MetaFilter filter, boolean verbose) { |
401 | 0 | StringBuffer sb = new StringBuffer(); |
402 | 0 | sb.append(stories.size() + " stories excluded by filter: " + filter.asString() + "\n"); |
403 | 0 | if (verbose) { |
404 | 0 | for (Story story : stories) { |
405 | 0 | sb.append(story.getPath()).append("\n"); |
406 | |
} |
407 | |
} |
408 | 0 | getLog().info(sb.toString()); |
409 | 0 | } |
410 | |
|
411 | |
public void runningWithAnnotatedEmbedderRunner(String className) { |
412 | 0 | getLog().info("Running with AnnotatedEmbedderRunner '" + className + "'"); |
413 | 0 | } |
414 | |
|
415 | |
public void annotatedInstanceNotOfType(Object annotatedInstance, Class<?> type) { |
416 | 1 | getLog().warn("Annotated instance " + annotatedInstance + " not of type " + type); |
417 | 1 | } |
418 | |
|
419 | |
public void generatingReportsView(File outputDirectory, List<String> formats, Properties viewProperties) { |
420 | 1 | getLog().info( |
421 | |
"Generating reports view to '" + outputDirectory + "' using formats '" + formats + "'" |
422 | |
+ " and view properties '" + viewProperties + "'"); |
423 | 1 | } |
424 | |
|
425 | |
public void reportsViewGenerationFailed(File outputDirectory, List<String> formats, Properties viewProperties, |
426 | |
Throwable cause) { |
427 | 1 | String message = "Failed to generate reports view to '" + outputDirectory + "' using formats '" + formats |
428 | |
+ "'" + " and view properties '" + viewProperties + "'"; |
429 | 1 | getLog().warn(message, cause); |
430 | 1 | } |
431 | |
|
432 | |
public void reportsViewGenerated(ReportsCount count) { |
433 | 1 | getLog().info( |
434 | |
"Reports view generated with " + count.getStories() + " stories (of which " |
435 | |
+ count.getStoriesPending() + " pending) containing " + count.getScenarios() |
436 | |
+ " scenarios (of which " + count.getScenariosPending() + " pending)"); |
437 | 1 | if (count.getStoriesNotAllowed() > 0 || count.getScenariosNotAllowed() > 0) { |
438 | 1 | getLog().info( |
439 | |
"Meta filters excluded " + count.getStoriesNotAllowed() + " stories and " |
440 | |
+ count.getScenariosNotAllowed() + " scenarios"); |
441 | |
} |
442 | 1 | } |
443 | |
|
444 | |
public void reportsViewFailures(ReportsCount count) { |
445 | 0 | getLog().warn("Failures in reports view: " + count.getScenariosFailed() + " scenarios failed"); |
446 | 0 | } |
447 | |
|
448 | |
public void reportsViewNotGenerated() { |
449 | 1 | getLog().info("Reports view not generated"); |
450 | 1 | } |
451 | |
|
452 | |
public void mappingStory(String storyPath, List<String> metaFilters) { |
453 | 0 | getLog().info("Mapping story " + storyPath + " with meta filters " + metaFilters); |
454 | 0 | } |
455 | |
|
456 | |
public void generatingMapsView(File outputDirectory, StoryMaps storyMaps, Properties viewProperties) { |
457 | 0 | getLog().info( |
458 | |
"Generating maps view to '" + outputDirectory + "' using story maps '" + storyMaps + "'" |
459 | |
+ " and view properties '" + viewProperties + "'"); |
460 | 0 | } |
461 | |
|
462 | |
public void mapsViewGenerationFailed(File outputDirectory, StoryMaps storyMaps, Properties viewProperties, |
463 | |
Throwable cause) { |
464 | 0 | getLog().warn( |
465 | |
"Failed to generate maps view to '" + outputDirectory + "' using story maps '" + storyMaps + "'" |
466 | |
+ " and view properties '" + viewProperties + "'", cause); |
467 | 0 | } |
468 | |
|
469 | |
public void generatingNavigatorView(File outputDirectory, Properties viewProperties) { |
470 | 0 | getLog().info( |
471 | |
"Generating navigator view to '" + outputDirectory + "' using view properties '" + viewProperties |
472 | |
+ "'"); |
473 | 0 | } |
474 | |
|
475 | |
public void navigatorViewGenerationFailed(File outputDirectory, Properties viewProperties, Throwable cause) { |
476 | 0 | getLog().warn( |
477 | |
"Failed to generate navigator view to '" + outputDirectory + "' using view properties '" |
478 | |
+ viewProperties + "'", cause); |
479 | 0 | } |
480 | |
|
481 | |
public void navigatorViewNotGenerated() { |
482 | 0 | getLog().warn( |
483 | |
"Navigator view not generated, as the CrossReference has not been declared in the StoryReporterBuilder"); |
484 | 0 | } |
485 | |
|
486 | |
public void processingSystemProperties(Properties properties) { |
487 | 0 | getLog().info("Processing system properties " + properties); |
488 | 0 | } |
489 | |
|
490 | |
public void systemPropertySet(String name, String value) { |
491 | 0 | getLog().info("System property '" + name + "' set to '" + value + "'"); |
492 | 0 | } |
493 | |
|
494 | |
public void storyTimeout(Story story, StoryDuration storyDuration) { |
495 | 0 | getLog().warn( |
496 | |
"Story " + story.getPath() + " duration of " + storyDuration.getDurationInSecs() |
497 | |
+ " seconds has exceeded timeout of " + storyDuration.getTimeoutInSecs() + " seconds"); |
498 | 0 | } |
499 | |
|
500 | |
public void usingThreads(int threads) { |
501 | 0 | getLog().info("Using " + threads + " threads"); |
502 | 0 | } |
503 | |
|
504 | |
public void usingExecutorService(ExecutorService executorService) { |
505 | 0 | getLog().info("Using executor service " + executorService); |
506 | 0 | } |
507 | |
|
508 | |
public void usingControls(EmbedderControls embedderControls) { |
509 | 0 | getLog().info("Using controls " + embedderControls); |
510 | 0 | } |
511 | |
|
512 | |
@Override |
513 | |
public String toString() { |
514 | 1 | return this.getClass().getSimpleName(); |
515 | |
} |
516 | |
|
517 | |
} |
518 | |
} |