1 | |
package org.jbehave.core.reporters; |
2 | |
|
3 | |
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; |
4 | |
import static org.apache.commons.lang.StringEscapeUtils.escapeXml; |
5 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_END; |
6 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_NEWLINE; |
7 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_START; |
8 | |
|
9 | |
import java.io.ByteArrayOutputStream; |
10 | |
import java.io.PrintStream; |
11 | |
import java.text.MessageFormat; |
12 | |
import java.util.Arrays; |
13 | |
import java.util.List; |
14 | |
import java.util.Locale; |
15 | |
import java.util.Map; |
16 | |
import java.util.Properties; |
17 | |
|
18 | |
import org.apache.commons.collections.CollectionUtils; |
19 | |
import org.apache.commons.collections.Transformer; |
20 | |
import org.apache.commons.lang.ArrayUtils; |
21 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
22 | |
import org.apache.commons.lang.builder.ToStringStyle; |
23 | |
import org.jbehave.core.configuration.Keywords; |
24 | |
import org.jbehave.core.model.ExamplesTable; |
25 | |
import org.jbehave.core.model.Meta; |
26 | |
import org.jbehave.core.model.Narrative; |
27 | |
import org.jbehave.core.model.OutcomesTable; |
28 | |
import org.jbehave.core.model.Scenario; |
29 | |
import org.jbehave.core.model.Story; |
30 | |
import org.jbehave.core.model.OutcomesTable.Outcome; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
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 | |
public abstract class PrintStreamOutput implements StoryReporter { |
73 | |
|
74 | |
private static final String EMPTY = ""; |
75 | |
|
76 | 5 | public enum Format { TXT, HTML, XML } |
77 | |
|
78 | |
private final Format format; |
79 | |
private final PrintStream output; |
80 | |
private final Properties outputPatterns; |
81 | |
private final Keywords keywords; |
82 | |
private boolean reportFailureTrace; |
83 | |
private Throwable cause; |
84 | |
|
85 | |
protected PrintStreamOutput(Format format, PrintStream output, Properties outputPatterns, |
86 | 427 | Keywords keywords, boolean reportFailureTrace) { |
87 | 427 | this.format = format; |
88 | 427 | this.output = output; |
89 | 427 | this.outputPatterns = outputPatterns; |
90 | 427 | this.keywords = keywords; |
91 | 427 | this.reportFailureTrace = reportFailureTrace; |
92 | 427 | } |
93 | |
|
94 | |
public void successful(String step) { |
95 | 44 | print(format("successful", "{0}\n", step)); |
96 | 44 | } |
97 | |
|
98 | |
public void ignorable(String step) { |
99 | 12 | print(format("ignorable", "{0}\n", step)); |
100 | 12 | } |
101 | |
|
102 | |
public void pending(String step) { |
103 | 16 | print(format("pending", "{0} ({1})\n", step, keywords.pending())); |
104 | 16 | } |
105 | |
|
106 | |
public void notPerformed(String step) { |
107 | 16 | print(format("notPerformed", "{0} ({1})\n", step, keywords.notPerformed())); |
108 | 16 | } |
109 | |
|
110 | |
public void failed(String step, Throwable cause) { |
111 | 17 | this.cause = cause; |
112 | 17 | print(format("failed", "{0} ({1})\n({2})\n", step, keywords.failed(), cause)); |
113 | 17 | } |
114 | |
|
115 | |
public void failedOutcomes(String step, OutcomesTable table) { |
116 | 12 | failed(step, table.failureCause()); |
117 | 12 | print(table); |
118 | 12 | } |
119 | |
|
120 | |
private void print(OutcomesTable table) { |
121 | 12 | print(format("outcomesTableStart", "\n")); |
122 | 12 | List<Outcome<?>> rows = table.getOutcomes(); |
123 | 12 | print(format("outcomesTableHeadStart", "|")); |
124 | |
|
125 | 12 | for (String field : table.getOutcomeFields()) { |
126 | 48 | print(format("outcomesTableHeadCell", "{0}|", field)); |
127 | |
} |
128 | 12 | print(format("outcomesTableHeadEnd", "\n")); |
129 | 12 | print(format("outcomesTableBodyStart", EMPTY)); |
130 | 12 | for (Outcome<?> outcome : rows) { |
131 | 12 | print(format("outcomesTableRowStart", "|", outcome.isVerified()?"verified":"notVerified")); |
132 | 12 | print(format("outcomesTableCell", "{0}|", outcome.getDescription())); |
133 | 12 | print(format("outcomesTableCell", "{0}|", outcome.getValue())); |
134 | 12 | print(format("outcomesTableCell", "{0}|", outcome.getMatcher())); |
135 | 12 | print(format("outcomesTableCell", "{0}|", outcome.isVerified())); |
136 | 12 | print(format("outcomesTableRowEnd", "\n")); |
137 | |
} |
138 | 12 | print(format("outcomesTableBodyEnd", "\n")); |
139 | 12 | print(format("outcomesTableEnd", "\n")); |
140 | 12 | } |
141 | |
|
142 | |
public void storyNotAllowed(Story story, String filter) { |
143 | 3 | print(format("beforeStory", "{0}\n({1})\n", story.getDescription().asString(), story.getPath())); |
144 | 3 | if (!story.getMeta().isEmpty()) { |
145 | 3 | Meta meta = story.getMeta(); |
146 | 3 | print(meta); |
147 | |
} |
148 | 3 | print(format("filter", "{0}\n", filter)); |
149 | 3 | } |
150 | |
|
151 | |
public void beforeStory(Story story, boolean givenStory) { |
152 | 12 | print(format("beforeStory", "{0}\n({1})\n", story.getDescription().asString(), story.getPath())); |
153 | 12 | if (!story.getMeta().isEmpty()) { |
154 | 12 | Meta meta = story.getMeta(); |
155 | 12 | print(meta); |
156 | |
} |
157 | 12 | if (!story.getNarrative().isEmpty()) { |
158 | 12 | Narrative narrative = story.getNarrative(); |
159 | 12 | print(format("narrative", "{0}\n{1} {2}\n{3} {4}\n{5} {6}\n", keywords.narrative(), keywords.inOrderTo(), |
160 | |
narrative.inOrderTo(), keywords.asA(), narrative.asA(), keywords.iWantTo(), narrative.iWantTo())); |
161 | |
} |
162 | 12 | } |
163 | |
|
164 | |
private void print(Meta meta) { |
165 | 18 | print(format("metaStart", "{0}\n", keywords.meta())); |
166 | 18 | for (String name : meta.getPropertyNames() ){ |
167 | 36 | print(format("metaProperty", "{0}{1} {2}", keywords.metaProperty(), name, meta.getProperty(name))); |
168 | |
} |
169 | 18 | print(format("metaEnd", "\n")); |
170 | 18 | } |
171 | |
|
172 | |
public void afterStory(boolean givenStory) { |
173 | 12 | print(format("afterStory", "\n")); |
174 | 12 | } |
175 | |
|
176 | |
public void givenStories(List<String> storyPaths) { |
177 | 12 | print(format("givenStories", "{0} {1}\n", keywords.givenStories(), storyPaths)); |
178 | 12 | } |
179 | |
|
180 | |
public void scenarioNotAllowed(Scenario scenario, String filter) { |
181 | 3 | print(format("beforeScenario", "{0} {1}\n", keywords.scenario(), scenario.getTitle())); |
182 | 3 | scenarioMeta(scenario.getMeta()); |
183 | 3 | print(format("filter", "{0}\n", filter)); |
184 | 3 | } |
185 | |
|
186 | |
public void beforeScenario(String title) { |
187 | 14 | cause = null; |
188 | 14 | print(format("beforeScenario", "{0} {1}\n", keywords.scenario(), title)); |
189 | 14 | } |
190 | |
|
191 | |
public void scenarioMeta(Meta meta) { |
192 | 3 | if (!meta.isEmpty()) { |
193 | 3 | print(meta); |
194 | |
} |
195 | 3 | } |
196 | |
|
197 | |
public void afterScenario() { |
198 | 14 | if (cause != null && reportFailureTrace) { |
199 | 3 | print(format("afterScenarioWithFailure", "\n{0}\n", stackTrace(cause))); |
200 | |
} else { |
201 | 11 | print(format("afterScenario", "\n")); |
202 | |
} |
203 | 14 | } |
204 | |
|
205 | |
private String stackTrace(Throwable cause) { |
206 | 3 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
207 | 3 | cause.printStackTrace(new PrintStream(out)); |
208 | 3 | return out.toString(); |
209 | |
} |
210 | |
|
211 | |
public void beforeExamples(List<String> steps, ExamplesTable table) { |
212 | 12 | print(format("beforeExamples", "{0}\n", keywords.examplesTable())); |
213 | 12 | for (String step : steps) { |
214 | 24 | print(format("examplesStep", "{0}\n", step)); |
215 | |
} |
216 | 12 | print(table); |
217 | 12 | } |
218 | |
|
219 | |
private void print(ExamplesTable table) { |
220 | 12 | print(format("examplesTableStart", "\n")); |
221 | 12 | List<Map<String, String>> rows = table.getRows(); |
222 | 12 | List<String> headers = table.getHeaders(); |
223 | 12 | print(format("examplesTableHeadStart", "|")); |
224 | 12 | for (String header : headers) { |
225 | 24 | print(format("examplesTableHeadCell", "{0}|", header)); |
226 | |
} |
227 | 12 | print(format("examplesTableHeadEnd", "\n")); |
228 | 12 | print(format("examplesTableBodyStart", EMPTY)); |
229 | 12 | for (Map<String, String> row : rows) { |
230 | 24 | print(format("examplesTableRowStart", "|")); |
231 | 24 | for (String header : headers) { |
232 | 48 | print(format("examplesTableCell", "{0}|", row.get(header))); |
233 | |
} |
234 | 24 | print(format("examplesTableRowEnd", "\n")); |
235 | |
} |
236 | 12 | print(format("examplesTableBodyEnd", "\n")); |
237 | 12 | print(format("examplesTableEnd", "\n")); |
238 | 12 | } |
239 | |
|
240 | |
public void example(Map<String, String> tableRow) { |
241 | 24 | print(format("example", "\n{0} {1}\n", keywords.examplesTableRow(), tableRow)); |
242 | 24 | } |
243 | |
|
244 | |
public void afterExamples() { |
245 | 12 | print(format("afterExamples", "\n")); |
246 | 12 | } |
247 | |
|
248 | |
public void dryRun() { |
249 | 12 | print(format("dryRun", "{0}\n", keywords.dryRun())); |
250 | 12 | } |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
protected String format(String key, String defaultPattern, Object... args) { |
262 | 5131 | return MessageFormat.format(lookupPattern(key, escape(defaultPattern)), escapeAll(args)); |
263 | |
} |
264 | |
|
265 | |
private String escape(String defaultPattern) { |
266 | 5131 | return (String) escapeAll(defaultPattern)[0]; |
267 | |
} |
268 | |
|
269 | |
private Object[] escapeAll(Object... args) { |
270 | 10262 | return escape(format, args); |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
protected Object[] escape(final Format format, Object... args) { |
281 | |
|
282 | 10262 | Transformer escapingTransformer = new Transformer( ) { |
283 | |
public Object transform(Object object) { |
284 | 5885 | switch ( format ){ |
285 | 1888 | case HTML: return escapeHtml(asString(object)); |
286 | 550 | case XML: return escapeXml(asString(object)); |
287 | 3447 | default: return object; |
288 | |
} |
289 | |
} |
290 | |
|
291 | |
private String asString(Object object) { |
292 | 2438 | return ( object != null ? object.toString() : EMPTY ); |
293 | |
} |
294 | |
}; |
295 | 10262 | List<?> list = Arrays.asList( ArrayUtils.clone( args ) ); |
296 | 10262 | CollectionUtils.transform( list, escapingTransformer ); |
297 | 10262 | return list.toArray(); |
298 | |
} |
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
protected String lookupPattern(String key, String defaultPattern) { |
313 | 5131 | if (outputPatterns.containsKey(key)) { |
314 | 2932 | return outputPatterns.getProperty(key); |
315 | |
} |
316 | 2199 | return defaultPattern; |
317 | |
} |
318 | |
|
319 | |
public PrintStreamOutput doReportFailureTrace(boolean reportFailureTrace){ |
320 | 13 | this.reportFailureTrace = reportFailureTrace; |
321 | 13 | return this; |
322 | |
} |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
protected void print(String text) { |
330 | 733 | output.print(text.replace(format(PARAMETER_VALUE_START, PARAMETER_VALUE_START), format("parameterValueStart", EMPTY)) |
331 | |
.replace(format(PARAMETER_VALUE_END, PARAMETER_VALUE_END), format("parameterValueEnd", EMPTY)) |
332 | |
.replace(format(PARAMETER_VALUE_NEWLINE, PARAMETER_VALUE_NEWLINE), format("parameterValueNewline", "\n"))); |
333 | 733 | } |
334 | |
|
335 | |
@Override |
336 | |
public String toString() { |
337 | 1 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
338 | |
} |
339 | |
|
340 | |
} |