1 | |
package org.jbehave.mojo; |
2 | |
|
3 | |
import static java.util.Arrays.asList; |
4 | |
|
5 | |
import java.io.File; |
6 | |
import java.util.List; |
7 | |
import java.util.Properties; |
8 | |
|
9 | |
import org.apache.maven.plugin.AbstractMojo; |
10 | |
import org.jbehave.core.InjectableEmbedder; |
11 | |
import org.jbehave.core.embedder.Embedder; |
12 | |
import org.jbehave.core.embedder.EmbedderClassLoader; |
13 | |
import org.jbehave.core.embedder.EmbedderControls; |
14 | |
import org.jbehave.core.embedder.EmbedderMonitor; |
15 | |
import org.jbehave.core.embedder.MetaFilter; |
16 | |
import org.jbehave.core.embedder.UnmodifiableEmbedderControls; |
17 | |
import org.jbehave.core.failures.BatchFailures; |
18 | |
import org.jbehave.core.io.StoryFinder; |
19 | |
import org.jbehave.core.junit.AnnotatedEmbedderRunner; |
20 | |
import org.jbehave.core.model.Meta; |
21 | |
import org.jbehave.core.model.StoryMaps; |
22 | |
import org.jbehave.core.reporters.ReportsCount; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public abstract class AbstractEmbedderMojo extends AbstractMojo { |
31 | |
|
32 | |
static final String TEST_SCOPE = "test"; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
String sourceDirectory; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
String testSourceDirectory; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
String scope; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
List<String> includes; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
List<String> excludes; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
List<String> compileClasspathElements; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
List<String> testClasspathElements; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | 0 | boolean skip = false; |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | 0 | boolean batch = false; |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | 0 | boolean ignoreFailureInStories = false; |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | 0 | boolean ignoreFailureInView = false; |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | 0 | boolean generateViewAfterStories = true; |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | 0 | String embedderClass = Embedder.class.getName(); |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
String injectableEmbedderClass; |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | 0 | protected String annotatedEmbedderRunnerClass = AnnotatedEmbedderRunner.class.getName(); |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | 0 | String storyFinderClass = StoryFinder.class.getName(); |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | 0 | List<String> metaFilters = asList(); |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | 0 | Properties systemProperties = new Properties(); |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
private EmbedderClassLoader classLoader; |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
boolean isTestScope() { |
177 | 0 | return TEST_SCOPE.equals(scope); |
178 | |
} |
179 | |
|
180 | |
String searchDirectory() { |
181 | 0 | if (isTestScope()) { |
182 | 0 | return testSourceDirectory; |
183 | |
} |
184 | 0 | return sourceDirectory; |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
protected EmbedderClassLoader classLoader() { |
194 | 0 | if ( classLoader == null ){ |
195 | 0 | classLoader = new EmbedderClassLoader(classpathElements()); |
196 | |
} |
197 | 0 | return classLoader; |
198 | |
} |
199 | |
|
200 | |
List<String> classpathElements() { |
201 | 0 | List<String> classpathElements = compileClasspathElements; |
202 | 0 | if (isTestScope()) { |
203 | 0 | classpathElements = testClasspathElements; |
204 | |
} |
205 | 0 | return classpathElements; |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
protected List<String> storyPaths() { |
216 | 0 | getLog().debug("Searching for story paths including " + includes + " and excluding " + excludes); |
217 | 0 | List<String> storyPaths = newStoryFinder().findPaths(searchDirectory(), includes, excludes); |
218 | 0 | getLog().info("Found story paths: " + storyPaths); |
219 | 0 | return storyPaths; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
protected List<String> classNames() { |
230 | 0 | getLog().debug("Searching for class names including " + includes + " and excluding " + excludes); |
231 | 0 | List<String> classNames = newStoryFinder().findClassNames(searchDirectory(), includes, excludes); |
232 | 0 | getLog().info("Found class names: " + classNames); |
233 | 0 | return classNames; |
234 | |
} |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
protected StoryFinder newStoryFinder() { |
242 | 0 | return classLoader().newInstance(StoryFinder.class, storyFinderClass); |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
protected Embedder newEmbedder() { |
253 | 0 | Embedder embedder = null; |
254 | 0 | EmbedderClassLoader classLoader = classLoader(); |
255 | 0 | if (injectableEmbedderClass != null) { |
256 | 0 | embedder = classLoader.newInstance(InjectableEmbedder.class, injectableEmbedderClass).injectedEmbedder(); |
257 | |
} else { |
258 | 0 | embedder = classLoader.newInstance(Embedder.class, embedderClass); |
259 | |
} |
260 | 0 | embedder.useClassLoader(classLoader); |
261 | 0 | embedder.useSystemProperties(systemProperties); |
262 | 0 | EmbedderMonitor embedderMonitor = embedderMonitor(); |
263 | 0 | embedder.useEmbedderMonitor(embedderMonitor); |
264 | 0 | if ( !metaFilters.isEmpty() ) { |
265 | 0 | embedder.useMetaFilters(metaFilters); |
266 | |
} |
267 | 0 | embedder.useEmbedderControls(embedderControls()); |
268 | 0 | return embedder; |
269 | |
} |
270 | |
|
271 | |
protected EmbedderMonitor embedderMonitor() { |
272 | 0 | return new MavenEmbedderMonitor(); |
273 | |
} |
274 | |
|
275 | |
protected EmbedderControls embedderControls() { |
276 | 0 | return new UnmodifiableEmbedderControls(new EmbedderControls().doBatch(batch).doSkip(skip) |
277 | |
.doGenerateViewAfterStories(generateViewAfterStories).doIgnoreFailureInStories(ignoreFailureInStories) |
278 | |
.doIgnoreFailureInView(ignoreFailureInView)); |
279 | |
} |
280 | |
|
281 | 0 | protected class MavenEmbedderMonitor implements EmbedderMonitor { |
282 | |
|
283 | |
public void batchFailed(BatchFailures failures) { |
284 | 0 | getLog().warn("Failed to run batch " + failures); |
285 | 0 | } |
286 | |
|
287 | |
public void embeddableFailed(String name, Throwable cause) { |
288 | 0 | getLog().warn("Failed to run embeddable " + name, cause); |
289 | 0 | } |
290 | |
|
291 | |
public void embeddablesSkipped(List<String> classNames) { |
292 | 0 | getLog().info("Skipped embeddables " + classNames); |
293 | 0 | } |
294 | |
|
295 | |
public void metaNotAllowed(Meta meta, MetaFilter filter) { |
296 | 0 | getLog().info(meta + " not allowed by filter '" + filter.asString() + "'"); |
297 | 0 | } |
298 | |
|
299 | |
public void runningEmbeddable(String name) { |
300 | 0 | getLog().info("Running embeddable " + name); |
301 | 0 | } |
302 | |
|
303 | |
public void runningStory(String path) { |
304 | 0 | getLog().info("Running story " + path); |
305 | 0 | } |
306 | |
|
307 | |
public void storiesSkipped(List<String> storyPaths) { |
308 | 0 | getLog().info("Skipped stories " + storyPaths); |
309 | 0 | } |
310 | |
|
311 | |
public void storyFailed(String path, Throwable cause) { |
312 | 0 | getLog().warn("Failed to run story " + path, cause); |
313 | 0 | } |
314 | |
|
315 | |
public void annotatedInstanceNotOfType(Object annotatedInstance, Class<?> type) { |
316 | 0 | getLog().warn("Annotated instance " + annotatedInstance + " not of type " + type); |
317 | 0 | } |
318 | |
|
319 | |
public void generatingReportsView(File outputDirectory, List<String> formats, Properties viewProperties) { |
320 | 0 | getLog().info( |
321 | |
"Generating reports view to '" + outputDirectory + "' using formats '" + formats + "'" |
322 | |
+ " and view properties '" + viewProperties + "'"); |
323 | 0 | } |
324 | |
|
325 | |
public void reportsViewGenerationFailed(File outputDirectory, List<String> formats, Properties viewProperties, |
326 | |
Throwable cause) { |
327 | 0 | String message = "Failed to generate reports view to '" + outputDirectory + "' using formats '" + formats + "'" |
328 | |
+ " and view properties '" + viewProperties + "'"; |
329 | 0 | getLog().warn(message, cause); |
330 | 0 | } |
331 | |
|
332 | |
public void reportsViewGenerated(ReportsCount count) { |
333 | 0 | getLog().info("Reports view generated with " + count.getStories() + " stories containing " + count.getScenarios() + " scenarios (of which " |
334 | |
+ count.getScenariosFailed() + " failed)"); |
335 | 0 | if (count.getStoriesNotAllowed() > 0 || count.getScenariosNotAllowed() > 0) { |
336 | 0 | getLog().info("Meta filters did not allow " + count.getStoriesNotAllowed() + " stories and " + count.getScenariosNotAllowed() |
337 | |
+ " scenarios"); |
338 | |
} |
339 | 0 | } |
340 | |
|
341 | |
public void reportsViewNotGenerated() { |
342 | 0 | getLog().info("Reports view not generated"); |
343 | 0 | } |
344 | |
|
345 | |
public void mappingStory(String storyPath, List<String> metaFilters) { |
346 | 0 | getLog().info("Mapping story "+storyPath+" with meta filters "+metaFilters); |
347 | 0 | } |
348 | |
|
349 | |
public void generatingMapsView(File outputDirectory, StoryMaps storyMaps, Properties viewProperties) { |
350 | 0 | getLog().info("Generating maps view to '" + outputDirectory + "' using story maps '" + storyMaps + "'" |
351 | |
+ " and view properties '" + viewProperties + "'"); |
352 | 0 | } |
353 | |
|
354 | |
public void mapsViewGenerationFailed(File outputDirectory, StoryMaps storyMaps, Properties viewProperties, |
355 | |
Throwable cause) { |
356 | 0 | getLog().info("Generating maps view to '" + outputDirectory + "' using story maps '" + storyMaps + "'" |
357 | |
+ " and view properties '" + viewProperties + "'"); |
358 | 0 | } |
359 | |
|
360 | |
public void processingSystemProperties(Properties properties) { |
361 | 0 | getLog().info("Processing system properties " + properties); |
362 | 0 | } |
363 | |
|
364 | |
public void systemPropertySet(String name, String value) { |
365 | 0 | getLog().info("System property '" + name + "' set to '"+value+"'"); |
366 | 0 | } |
367 | |
|
368 | |
@Override |
369 | |
public String toString() { |
370 | 0 | return this.getClass().getSimpleName(); |
371 | |
} |
372 | |
|
373 | |
} |
374 | |
} |