1 | |
package org.jbehave.core.reporters; |
2 | |
|
3 | |
import org.apache.commons.collections.CollectionUtils; |
4 | |
import org.apache.commons.collections.Transformer; |
5 | |
import org.apache.commons.lang.ArrayUtils; |
6 | |
import org.apache.commons.lang.StringUtils; |
7 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
8 | |
import org.apache.commons.lang.builder.ToStringStyle; |
9 | |
import org.jbehave.core.configuration.Keywords; |
10 | |
import org.jbehave.core.failures.UUIDExceptionWrapper; |
11 | |
import org.jbehave.core.model.ExamplesTable; |
12 | |
import org.jbehave.core.model.GivenStories; |
13 | |
import org.jbehave.core.model.GivenStory; |
14 | |
import org.jbehave.core.model.Meta; |
15 | |
import org.jbehave.core.model.Narrative; |
16 | |
import org.jbehave.core.model.OutcomesTable; |
17 | |
import org.jbehave.core.model.OutcomesTable.Outcome; |
18 | |
import org.jbehave.core.model.Scenario; |
19 | |
import org.jbehave.core.model.Story; |
20 | |
|
21 | |
import java.io.ByteArrayOutputStream; |
22 | |
import java.io.PrintStream; |
23 | |
import java.text.MessageFormat; |
24 | |
import java.util.Arrays; |
25 | |
import java.util.List; |
26 | |
import java.util.Locale; |
27 | |
import java.util.Map; |
28 | |
import java.util.Properties; |
29 | |
import java.util.regex.Pattern; |
30 | |
|
31 | |
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; |
32 | |
import static org.apache.commons.lang.StringEscapeUtils.escapeXml; |
33 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_END; |
34 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_NEWLINE; |
35 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_START; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public abstract class PrintStreamOutput implements StoryReporter { |
78 | |
|
79 | |
private static final String EMPTY = ""; |
80 | |
|
81 | 5 | public enum Format { TXT, HTML, XML } |
82 | |
|
83 | |
private final Format format; |
84 | |
private final PrintStream output; |
85 | |
private final Properties outputPatterns; |
86 | |
private final Keywords keywords; |
87 | |
private boolean reportFailureTrace; |
88 | |
private boolean compressFailureTrace; |
89 | |
private Throwable cause; |
90 | |
|
91 | |
protected PrintStreamOutput(Format format, PrintStream output, Properties outputPatterns, |
92 | 266 | Keywords keywords, boolean reportFailureTrace, boolean compressFailureTrace) { |
93 | 266 | this.format = format; |
94 | 266 | this.output = output; |
95 | 266 | this.outputPatterns = outputPatterns; |
96 | 266 | this.keywords = keywords; |
97 | 266 | this.reportFailureTrace = reportFailureTrace; |
98 | 266 | this.compressFailureTrace = compressFailureTrace; |
99 | 266 | } |
100 | |
|
101 | |
public void successful(String step) { |
102 | 44 | print(format("successful", "{0}\n", step)); |
103 | 44 | } |
104 | |
|
105 | |
public void ignorable(String step) { |
106 | 12 | print(format("ignorable", "{0}\n", step)); |
107 | 12 | } |
108 | |
|
109 | |
public void pending(String step) { |
110 | 16 | print(format("pending", "{0} ({1})\n", step, keywords.pending())); |
111 | 16 | } |
112 | |
|
113 | |
public void notPerformed(String step) { |
114 | 16 | print(format("notPerformed", "{0} ({1})\n", step, keywords.notPerformed())); |
115 | 16 | } |
116 | |
|
117 | |
public void failed(String step, Throwable storyFailure) { |
118 | |
|
119 | 17 | if (storyFailure instanceof UUIDExceptionWrapper) { |
120 | 17 | this.cause = storyFailure.getCause(); |
121 | 17 | print(format("failed", "{0} ({1})\n({2})\n", step, keywords.failed(), storyFailure.getCause(), ((UUIDExceptionWrapper) storyFailure).getUUID())); |
122 | |
} else { |
123 | 0 | throw new ClassCastException(storyFailure +" should be an instance of UUIDExceptionWrapper"); |
124 | |
} |
125 | 17 | } |
126 | |
|
127 | |
public void failedOutcomes(String step, OutcomesTable table) { |
128 | 12 | failed(step, table.failureCause()); |
129 | 12 | print(table); |
130 | 12 | } |
131 | |
|
132 | |
private void print(OutcomesTable table) { |
133 | 12 | print(format("outcomesTableStart", "\n")); |
134 | 12 | List<Outcome<?>> rows = table.getOutcomes(); |
135 | 12 | print(format("outcomesTableHeadStart", "|")); |
136 | |
|
137 | 12 | for (String field : table.getOutcomeFields()) { |
138 | 48 | print(format("outcomesTableHeadCell", "{0}|", field)); |
139 | |
} |
140 | 12 | print(format("outcomesTableHeadEnd", "\n")); |
141 | 12 | print(format("outcomesTableBodyStart", EMPTY)); |
142 | 12 | for (Outcome<?> outcome : rows) { |
143 | 12 | print(format("outcomesTableRowStart", "|", outcome.isVerified()?"verified":"notVerified")); |
144 | 12 | print(format("outcomesTableCell", "{0}|", outcome.getDescription())); |
145 | 12 | print(format("outcomesTableCell", "{0}|", outcome.getValue())); |
146 | 12 | print(format("outcomesTableCell", "{0}|", outcome.getMatcher())); |
147 | 12 | print(format("outcomesTableCell", "{0}|", outcome.isVerified())); |
148 | 12 | print(format("outcomesTableRowEnd", "\n")); |
149 | |
} |
150 | 12 | print(format("outcomesTableBodyEnd", "\n")); |
151 | 12 | print(format("outcomesTableEnd", "\n")); |
152 | 12 | } |
153 | |
|
154 | |
public void storyNotAllowed(Story story, String filter) { |
155 | 3 | print(format("filter", "{0}\n", filter)); |
156 | 3 | } |
157 | |
|
158 | |
public void beforeStory(Story story, boolean givenStory) { |
159 | 15 | print(format("beforeStory", "{0}\n({1})\n", story.getDescription().asString(), story.getPath())); |
160 | 15 | if (!story.getMeta().isEmpty()) { |
161 | 15 | Meta meta = story.getMeta(); |
162 | 15 | print(meta); |
163 | |
} |
164 | 15 | } |
165 | |
|
166 | |
public void narrative(Narrative narrative) { |
167 | 12 | if (!narrative.isEmpty()) { |
168 | 12 | print(format("narrative", "{0}\n{1} {2}\n{3} {4}\n{5} {6}\n", keywords.narrative(), keywords.inOrderTo(), |
169 | |
narrative.inOrderTo(), keywords.asA(), narrative.asA(), keywords.iWantTo(), narrative.iWantTo())); |
170 | |
} |
171 | 12 | } |
172 | |
|
173 | |
private void print(Meta meta) { |
174 | 18 | print(format("metaStart", "{0}\n", keywords.meta())); |
175 | 18 | for (String name : meta.getPropertyNames() ){ |
176 | 36 | print(format("metaProperty", "{0}{1} {2}", keywords.metaProperty(), name, meta.getProperty(name))); |
177 | |
} |
178 | 18 | print(format("metaEnd", "\n")); |
179 | 18 | } |
180 | |
|
181 | |
public void afterStory(boolean givenStory) { |
182 | 15 | print(format("afterStory", "\n")); |
183 | 15 | } |
184 | |
|
185 | |
public void givenStories(GivenStories givenStories) { |
186 | 12 | print(format("givenStoriesStart", "{0}\n", keywords.givenStories())); |
187 | 12 | for (GivenStory givenStory : givenStories.getStories()) { |
188 | 24 | print(format("givenStory", "{0} {1}\n", givenStory.asString(), (givenStory.hasAnchor() ? givenStory.getParameters() : ""))); |
189 | |
} |
190 | 12 | print(format("givenStoriesEnd", "\n")); |
191 | 12 | } |
192 | |
|
193 | |
public void givenStories(List<String> storyPaths) { |
194 | 12 | givenStories(new GivenStories(StringUtils.join(storyPaths, ","))); |
195 | 12 | } |
196 | |
|
197 | |
public void scenarioNotAllowed(Scenario scenario, String filter) { |
198 | 3 | print(format("filter", "{0}\n", filter)); |
199 | 3 | } |
200 | |
|
201 | |
public void beforeScenario(String title) { |
202 | 17 | cause = null; |
203 | 17 | print(format("beforeScenario", "{0} {1}\n", keywords.scenario(), title)); |
204 | 17 | } |
205 | |
|
206 | |
public void scenarioMeta(Meta meta) { |
207 | 3 | if (!meta.isEmpty()) { |
208 | 3 | print(meta); |
209 | |
} |
210 | 3 | } |
211 | |
|
212 | |
public void afterScenario() { |
213 | 17 | if (cause != null && reportFailureTrace) { |
214 | 3 | print(format("afterScenarioWithFailure", "\n{0}\n", stackTrace(cause))); |
215 | |
} else { |
216 | 14 | print(format("afterScenario", "\n")); |
217 | |
} |
218 | 17 | } |
219 | |
|
220 | |
private String stackTrace(Throwable cause) { |
221 | 3 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
222 | 3 | cause.printStackTrace(new PrintStream(out)); |
223 | 3 | return stackTrace(out.toString()); |
224 | |
} |
225 | |
|
226 | |
protected String stackTrace(String stackTrace) { |
227 | 4 | if ( !compressFailureTrace ){ |
228 | 3 | return stackTrace; |
229 | |
} |
230 | |
|
231 | 1 | stackTrace = cutOff(stackTrace, "org.jbehave.core.embedder."); |
232 | 1 | stackTrace = cutOff(stackTrace, "org.junit.runners."); |
233 | 1 | stackTrace = cutOff(stackTrace, "org.apache.maven.surefire."); |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | 9 | for (Replacement replacement : REPLACEMENTS) { |
239 | 8 | stackTrace = replacement.from.matcher(stackTrace).replaceAll(replacement.to); |
240 | |
} |
241 | 1 | return stackTrace; |
242 | |
} |
243 | |
|
244 | |
private String cutOff(String stackTrace, String at) { |
245 | 3 | if (stackTrace.indexOf(at) > -1) { |
246 | 1 | int ix = stackTrace.indexOf(at); |
247 | 1 | ix = stackTrace.indexOf("\n", ix); |
248 | 1 | if (ix != -1) { |
249 | 0 | stackTrace = stackTrace.substring(0,ix); |
250 | |
} |
251 | |
} |
252 | 3 | return stackTrace; |
253 | |
} |
254 | |
|
255 | |
public void beforeExamples(List<String> steps, ExamplesTable table) { |
256 | 12 | print(format("beforeExamples", "{0}\n", keywords.examplesTable())); |
257 | 12 | for (String step : steps) { |
258 | 24 | print(format("examplesStep", "{0}\n", step)); |
259 | |
} |
260 | 12 | print(table); |
261 | 12 | } |
262 | |
|
263 | |
private void print(ExamplesTable table) { |
264 | 12 | print(format("examplesTableStart", "\n")); |
265 | 12 | List<Map<String, String>> rows = table.getRows(); |
266 | 12 | List<String> headers = table.getHeaders(); |
267 | 12 | print(format("examplesTableHeadStart", "|")); |
268 | 12 | for (String header : headers) { |
269 | 24 | print(format("examplesTableHeadCell", "{0}|", header)); |
270 | |
} |
271 | 12 | print(format("examplesTableHeadEnd", "\n")); |
272 | 12 | print(format("examplesTableBodyStart", EMPTY)); |
273 | 12 | for (Map<String, String> row : rows) { |
274 | 24 | print(format("examplesTableRowStart", "|")); |
275 | 24 | for (String header : headers) { |
276 | 48 | print(format("examplesTableCell", "{0}|", row.get(header))); |
277 | |
} |
278 | 24 | print(format("examplesTableRowEnd", "\n")); |
279 | |
} |
280 | 12 | print(format("examplesTableBodyEnd", "\n")); |
281 | 12 | print(format("examplesTableEnd", "\n")); |
282 | 12 | } |
283 | |
|
284 | |
public void example(Map<String, String> tableRow) { |
285 | 24 | print(format("example", "\n{0} {1}\n", keywords.examplesTableRow(), tableRow)); |
286 | 24 | } |
287 | |
|
288 | |
public void afterExamples() { |
289 | 12 | print(format("afterExamples", "\n")); |
290 | 12 | } |
291 | |
|
292 | |
public void dryRun() { |
293 | 12 | print(format("dryRun", "{0}\n", keywords.dryRun())); |
294 | 12 | } |
295 | |
|
296 | |
|
297 | |
public void pendingMethods(List<String> methods) { |
298 | 12 | for (String method : methods) { |
299 | 24 | print(format("pendingMethod", "{0}\n", method)); |
300 | |
} |
301 | 12 | } |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
protected String format(String key, String defaultPattern, Object... args) { |
313 | 5593 | return MessageFormat.format(lookupPattern(key, escape(defaultPattern)), escapeAll(args)); |
314 | |
} |
315 | |
|
316 | |
private String escape(String defaultPattern) { |
317 | 5593 | return (String) escapeAll(defaultPattern)[0]; |
318 | |
} |
319 | |
|
320 | |
private Object[] escapeAll(Object... args) { |
321 | 11186 | return escape(format, args); |
322 | |
} |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
protected Object[] escape(final Format format, Object... args) { |
332 | |
|
333 | 11186 | Transformer escapingTransformer = new Transformer( ) { |
334 | |
public Object transform(Object object) { |
335 | 6424 | switch ( format ){ |
336 | 2066 | case HTML: return escapeHtml(asString(object)); |
337 | 605 | case XML: return escapeXml(asString(object)); |
338 | 3753 | default: return object; |
339 | |
} |
340 | |
} |
341 | |
|
342 | |
private String asString(Object object) { |
343 | 2671 | return ( object != null ? object.toString() : EMPTY ); |
344 | |
} |
345 | |
}; |
346 | 11186 | List<?> list = Arrays.asList( ArrayUtils.clone( args ) ); |
347 | 11186 | CollectionUtils.transform( list, escapingTransformer ); |
348 | 11186 | return list.toArray(); |
349 | |
} |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
protected String lookupPattern(String key, String defaultPattern) { |
364 | 5593 | if (outputPatterns.containsKey(key)) { |
365 | 3182 | return outputPatterns.getProperty(key); |
366 | |
} |
367 | 2411 | return defaultPattern; |
368 | |
} |
369 | |
|
370 | |
public PrintStreamOutput doReportFailureTrace(boolean reportFailureTrace){ |
371 | 18 | this.reportFailureTrace = reportFailureTrace; |
372 | 18 | return this; |
373 | |
} |
374 | |
|
375 | |
public PrintStreamOutput doCompressFailureTrace(boolean compressFailureTrace){ |
376 | 18 | this.compressFailureTrace = compressFailureTrace; |
377 | 18 | return this; |
378 | |
} |
379 | |
|
380 | |
protected void overwritePattern(String key, String pattern) { |
381 | 0 | outputPatterns.put(key, pattern); |
382 | 0 | } |
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
protected void print(String text) { |
390 | 799 | output.print(text.replace(format(PARAMETER_VALUE_START, PARAMETER_VALUE_START), format("parameterValueStart", EMPTY)) |
391 | |
.replace(format(PARAMETER_VALUE_END, PARAMETER_VALUE_END), format("parameterValueEnd", EMPTY)) |
392 | |
.replace(format(PARAMETER_VALUE_NEWLINE, PARAMETER_VALUE_NEWLINE), format("parameterValueNewline", "\n"))); |
393 | 799 | } |
394 | |
|
395 | |
@Override |
396 | |
public String toString() { |
397 | 1 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
398 | |
} |
399 | |
|
400 | 24 | private static class Replacement { |
401 | |
private final Pattern from; |
402 | |
private final String to; |
403 | 8 | private Replacement(Pattern from, String to) { |
404 | 8 | this.from = from; |
405 | 8 | this.to = to; |
406 | 8 | } |
407 | |
} |
408 | |
|
409 | 1 | private static Replacement[] REPLACEMENTS = new Replacement[]{ |
410 | |
new Replacement( |
411 | |
Pattern.compile( |
412 | |
"\\tat sun.reflect.NativeMethodAccessorImpl.invoke0\\(Native Method\\)\\n" + |
413 | |
"\\tat sun.reflect.NativeMethodAccessorImpl.invoke\\(NativeMethodAccessorImpl.java:\\d+\\)\\n" + |
414 | |
"\\tat sun.reflect.DelegatingMethodAccessorImpl.invoke\\(DelegatingMethodAccessorImpl.java:\\d+\\)\\n" + |
415 | |
"\\tat java.lang.reflect.Method.invoke\\(Method.java:\\d+\\)" |
416 | |
), |
417 | |
"\t(reflection-invoke)"), |
418 | |
new Replacement( |
419 | |
Pattern.compile( |
420 | |
"\\tat org.codehaus.groovy.reflection.CachedMethod.invoke\\(CachedMethod.java:\\d+\\)\\n" + |
421 | |
"\\tat org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod.invoke\\(ClosureMetaMethod.java:\\d+\\)\\n" + |
422 | |
"\\tat org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite\\$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke\\(PojoMetaMethodSite.java:\\d+\\)\\n" + |
423 | |
"\\tat org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call\\(PojoMetaMethodSite.java:\\d+\\)\\n" + |
424 | |
"\\tat org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall\\(CallSiteArray.java:\\d+\\)\\n" + |
425 | |
"\\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.call\\(AbstractCallSite.java:\\d+\\)\\n" + |
426 | |
"\\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.call\\(AbstractCallSite.java:\\d+\\)" |
427 | |
), |
428 | |
"\t(groovy-closure-invoke)"), |
429 | |
|
430 | |
|
431 | |
new Replacement( |
432 | |
Pattern.compile( |
433 | |
"\\tat org.codehaus.groovy.reflection.CachedMethod.invoke\\(CachedMethod.java:\\d+\\)\\n" + |
434 | |
"\\tat groovy.lang.MetaMethod.doMethodInvoke\\(MetaMethod.java:\\d+\\)\\n" + |
435 | |
"\\tat org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod\\(ClosureMetaClass.java:\\d+\\)\\n" + |
436 | |
"\\tat org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN\\(ScriptBytecodeAdapter.java:\\d+\\)" |
437 | |
), |
438 | |
"\t(groovy-instance-method-invoke)"), |
439 | |
|
440 | |
new Replacement( |
441 | |
Pattern.compile( |
442 | |
"\\tat org.codehaus.groovy.reflection.CachedMethod.invoke\\(CachedMethod.java:\\d+\\)\n" + |
443 | |
"\\tat org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod.invoke\\(ClosureMetaMethod.java:\\d+\\)\n" + |
444 | |
"\\tat org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite\\$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke\\(PojoMetaMethodSite.java:\\d+\\)\n" + |
445 | |
"\\tat org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call\\(PojoMetaMethodSite.java:\\d+\\)\n" + |
446 | |
"\\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.call\\(AbstractCallSite.java:\\d+\\)" |
447 | |
), |
448 | |
"\t(groovy-abstract-method-invoke)"), |
449 | |
|
450 | |
new Replacement( |
451 | |
Pattern.compile( |
452 | |
"\\tat org.codehaus.groovy.reflection.CachedMethod.invoke\\(CachedMethod.java:\\d+\\)\\n" + |
453 | |
"\\tat groovy.lang.MetaMethod.doMethodInvoke\\(MetaMethod.java:\\d+\\)\\n" + |
454 | |
"\\tat groovy.lang.MetaClassImpl.invokeStaticMethod\\(MetaClassImpl.java:\\d+\\)\\n" + |
455 | |
"\\tat org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod\\(InvokerHelper.java:\\d+\\)\\n" + |
456 | |
"\\tat org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeStaticMethodN\\(ScriptBytecodeAdapter.java:\\d+\\)" |
457 | |
), |
458 | |
"\t(groovy-static-method-invoke)"), |
459 | |
|
460 | |
new Replacement( |
461 | |
Pattern.compile( |
462 | |
"\\tat sun.reflect.NativeConstructorAccessorImpl.newInstance0\\(Native Method\\)\\n" + |
463 | |
"\\tat sun.reflect.NativeConstructorAccessorImpl.newInstance\\(NativeConstructorAccessorImpl.java:\\d+\\)\\n" + |
464 | |
"\\tat sun.reflect.DelegatingConstructorAccessorImpl.newInstance\\(DelegatingConstructorAccessorImpl.java:\\d+\\)\\n" + |
465 | |
"\\tat java.lang.reflect.Constructor.newInstance\\(Constructor.java:\\d+\\)" |
466 | |
), |
467 | |
"\t(reflection-construct)"), |
468 | |
|
469 | |
new Replacement( |
470 | |
Pattern.compile( |
471 | |
"\\tat org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(Current|)\\(CallSiteArray.java:\\d+\\)\\n" + |
472 | |
"\\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(Current|)\\(AbstractCallSite.java:\\d+\\)\\n" + |
473 | |
"\\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(Current|)\\(AbstractCallSite.java:\\d+\\)" |
474 | |
|
475 | |
), |
476 | |
"\t(groovy-call)"), |
477 | |
|
478 | |
|
479 | |
new Replacement( |
480 | |
Pattern.compile( |
481 | |
"\\t\\(reflection\\-invoke\\)\\n" + |
482 | |
"\\t\\(groovy\\-"), |
483 | |
"\t(groovy-") |
484 | |
|
485 | |
}; |
486 | |
|
487 | |
|
488 | |
|
489 | |
} |