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