| 1 | |
package org.jbehave.core.reporters; |
| 2 | |
|
| 3 | |
import java.io.ByteArrayOutputStream; |
| 4 | |
import java.io.OutputStream; |
| 5 | |
import java.io.PrintStream; |
| 6 | |
import java.text.MessageFormat; |
| 7 | |
import java.util.Arrays; |
| 8 | |
import java.util.List; |
| 9 | |
import java.util.Locale; |
| 10 | |
import java.util.Map; |
| 11 | |
import java.util.Properties; |
| 12 | |
|
| 13 | |
import org.apache.commons.collections.CollectionUtils; |
| 14 | |
import org.apache.commons.collections.Transformer; |
| 15 | |
import org.apache.commons.lang.ArrayUtils; |
| 16 | |
import org.apache.commons.lang.StringUtils; |
| 17 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
| 18 | |
import org.apache.commons.lang.builder.ToStringStyle; |
| 19 | |
import org.jbehave.core.configuration.Keywords; |
| 20 | |
import org.jbehave.core.failures.KnownFailure; |
| 21 | |
import org.jbehave.core.failures.UUIDExceptionWrapper; |
| 22 | |
import org.jbehave.core.model.ExamplesTable; |
| 23 | |
import org.jbehave.core.model.GivenStories; |
| 24 | |
import org.jbehave.core.model.GivenStory; |
| 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.OutcomesTable.Outcome; |
| 29 | |
import org.jbehave.core.model.Scenario; |
| 30 | |
import org.jbehave.core.model.Story; |
| 31 | |
|
| 32 | |
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; |
| 33 | |
import static org.apache.commons.lang.StringEscapeUtils.escapeXml; |
| 34 | |
import static org.apache.commons.lang.StringUtils.substringBetween; |
| 35 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_TABLE_END; |
| 36 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_TABLE_START; |
| 37 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_END; |
| 38 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_NEWLINE; |
| 39 | |
import static org.jbehave.core.steps.StepCreator.PARAMETER_VALUE_START; |
| 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 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public abstract class PrintStreamOutput implements StoryReporter { |
| 85 | |
|
| 86 | |
private static final String EMPTY = ""; |
| 87 | |
|
| 88 | 5 | public enum Format { |
| 89 | 1 | TXT, HTML, XML |
| 90 | |
} |
| 91 | |
|
| 92 | |
private final Format format; |
| 93 | |
private final PrintStream output; |
| 94 | |
private final Properties outputPatterns; |
| 95 | |
private final Keywords keywords; |
| 96 | 364 | private ThreadLocal<Boolean> reportFailureTrace = new ThreadLocal<Boolean>(); |
| 97 | 364 | private ThreadLocal<Boolean> compressFailureTrace = new ThreadLocal<Boolean>(); |
| 98 | 364 | private ThreadLocal<Throwable> cause = new ThreadLocal<Throwable>(); |
| 99 | |
|
| 100 | |
protected PrintStreamOutput(Format format, PrintStream output, Properties outputPatterns, Keywords keywords, |
| 101 | 364 | boolean reportFailureTrace, boolean compressFailureTrace) { |
| 102 | 364 | this.format = format; |
| 103 | 364 | this.output = output; |
| 104 | 364 | this.outputPatterns = outputPatterns; |
| 105 | 364 | this.keywords = keywords; |
| 106 | 364 | doReportFailureTrace(reportFailureTrace); |
| 107 | 364 | doCompressFailureTrace(compressFailureTrace); |
| 108 | 364 | } |
| 109 | |
|
| 110 | |
public void successful(String step) { |
| 111 | 44 | print(format("successful", "{0}\n", step)); |
| 112 | 44 | } |
| 113 | |
|
| 114 | |
public void ignorable(String step) { |
| 115 | 12 | print(format("ignorable", "{0}\n", step)); |
| 116 | 12 | } |
| 117 | |
|
| 118 | |
public void pending(String step) { |
| 119 | 16 | print(format("pending", "{0} ({1})\n", step, keywords.pending())); |
| 120 | 16 | } |
| 121 | |
|
| 122 | |
public void notPerformed(String step) { |
| 123 | 16 | print(format("notPerformed", "{0} ({1})\n", step, keywords.notPerformed())); |
| 124 | 16 | } |
| 125 | |
|
| 126 | |
public void failed(String step, Throwable storyFailure) { |
| 127 | |
|
| 128 | |
|
| 129 | 19 | if (storyFailure instanceof UUIDExceptionWrapper) { |
| 130 | 19 | this.cause.set(storyFailure.getCause()); |
| 131 | 19 | print(format("failed", "{0} ({1})\n({2})\n", step, keywords.failed(), storyFailure.getCause(), |
| 132 | |
((UUIDExceptionWrapper) storyFailure).getUUID())); |
| 133 | |
} else { |
| 134 | 0 | throw new ClassCastException(storyFailure + " should be an instance of UUIDExceptionWrapper"); |
| 135 | |
} |
| 136 | 19 | } |
| 137 | |
|
| 138 | |
public void failedOutcomes(String step, OutcomesTable table) { |
| 139 | 12 | failed(step, table.failureCause()); |
| 140 | 12 | print(table); |
| 141 | 12 | } |
| 142 | |
|
| 143 | |
private void print(OutcomesTable table) { |
| 144 | 12 | print(format("outcomesTableStart", "\n")); |
| 145 | 12 | List<Outcome<?>> rows = table.getOutcomes(); |
| 146 | 12 | print(format("outcomesTableHeadStart", "|")); |
| 147 | |
|
| 148 | 12 | for (String field : table.getOutcomeFields()) { |
| 149 | 48 | print(format("outcomesTableHeadCell", "{0}|", field)); |
| 150 | |
} |
| 151 | 12 | print(format("outcomesTableHeadEnd", "\n")); |
| 152 | 12 | print(format("outcomesTableBodyStart", EMPTY)); |
| 153 | 12 | for (Outcome<?> outcome : rows) { |
| 154 | 12 | print(format("outcomesTableRowStart", "|", outcome.isVerified() ? "verified" : "notVerified")); |
| 155 | 12 | print(format("outcomesTableCell", "{0}|", outcome.getDescription())); |
| 156 | 12 | print(format("outcomesTableCell", "{0}|", outcome.getValue())); |
| 157 | 12 | print(format("outcomesTableCell", "{0}|", outcome.getMatcher())); |
| 158 | 12 | print(format("outcomesTableCell", "{0}|", outcome.isVerified())); |
| 159 | 12 | print(format("outcomesTableRowEnd", "\n")); |
| 160 | |
} |
| 161 | 12 | print(format("outcomesTableBodyEnd", "\n")); |
| 162 | 12 | print(format("outcomesTableEnd", "\n")); |
| 163 | 12 | } |
| 164 | |
|
| 165 | |
public void storyNotAllowed(Story story, String filter) { |
| 166 | 3 | print(format("filter", "{0}\n", filter)); |
| 167 | 3 | } |
| 168 | |
|
| 169 | |
public void beforeStory(Story story, boolean givenStory) { |
| 170 | 15 | print(format("beforeStory", "{0}\n({1})\n", story.getDescription().asString(), story.getPath())); |
| 171 | 15 | if (!story.getMeta().isEmpty()) { |
| 172 | 15 | Meta meta = story.getMeta(); |
| 173 | 15 | print(meta); |
| 174 | |
} |
| 175 | 15 | } |
| 176 | |
|
| 177 | |
public void narrative(Narrative narrative) { |
| 178 | 12 | if (!narrative.isEmpty()) { |
| 179 | 12 | print(format("narrative", "{0}\n{1} {2}\n{3} {4}\n{5} {6}\n", keywords.narrative(), keywords.inOrderTo(), |
| 180 | |
narrative.inOrderTo(), keywords.asA(), narrative.asA(), keywords.iWantTo(), narrative.iWantTo())); |
| 181 | |
} |
| 182 | 12 | } |
| 183 | |
|
| 184 | |
private void print(Meta meta) { |
| 185 | 18 | print(format("metaStart", "{0}\n", keywords.meta())); |
| 186 | 18 | for (String name : meta.getPropertyNames()) { |
| 187 | 36 | print(format("metaProperty", "{0}{1} {2}", keywords.metaProperty(), name, meta.getProperty(name))); |
| 188 | |
} |
| 189 | 18 | print(format("metaEnd", "\n")); |
| 190 | 18 | } |
| 191 | |
|
| 192 | |
public void afterStory(boolean givenStory) { |
| 193 | 15 | print(format("afterStory", "\n")); |
| 194 | 15 | } |
| 195 | |
|
| 196 | |
public void givenStories(GivenStories givenStories) { |
| 197 | 12 | print(format("givenStoriesStart", "{0}\n", keywords.givenStories())); |
| 198 | 12 | for (GivenStory givenStory : givenStories.getStories()) { |
| 199 | 24 | print(format("givenStory", "{0} {1}\n", givenStory.asString(), |
| 200 | |
(givenStory.hasAnchor() ? givenStory.getParameters() : ""))); |
| 201 | |
} |
| 202 | 12 | print(format("givenStoriesEnd", "\n")); |
| 203 | 12 | } |
| 204 | |
|
| 205 | |
public void givenStories(List<String> storyPaths) { |
| 206 | 12 | givenStories(new GivenStories(StringUtils.join(storyPaths, ","))); |
| 207 | 12 | } |
| 208 | |
|
| 209 | |
public void scenarioNotAllowed(Scenario scenario, String filter) { |
| 210 | 3 | print(format("filter", "{0}\n", filter)); |
| 211 | 3 | } |
| 212 | |
|
| 213 | |
public void beforeScenario(String title) { |
| 214 | 17 | cause.set(null); |
| 215 | 17 | print(format("beforeScenario", "{0} {1}\n", keywords.scenario(), title)); |
| 216 | 17 | } |
| 217 | |
|
| 218 | |
public void scenarioMeta(Meta meta) { |
| 219 | 3 | if (!meta.isEmpty()) { |
| 220 | 3 | print(meta); |
| 221 | |
} |
| 222 | 3 | } |
| 223 | |
|
| 224 | |
public void afterScenario() { |
| 225 | 19 | if (cause.get() != null && reportFailureTrace.get() && !(cause.get() instanceof KnownFailure)) { |
| 226 | 4 | print(format("afterScenarioWithFailure", "\n{0}\n", new StackTraceFormatter(compressFailureTrace()).stackTrace(cause.get()))); |
| 227 | |
} else { |
| 228 | 15 | print(format("afterScenario", "\n")); |
| 229 | |
} |
| 230 | 19 | } |
| 231 | |
|
| 232 | |
public void beforeExamples(List<String> steps, ExamplesTable table) { |
| 233 | 12 | print(format("beforeExamples", "{0}\n", keywords.examplesTable())); |
| 234 | 12 | for (String step : steps) { |
| 235 | 24 | print(format("examplesStep", "{0}\n", step)); |
| 236 | |
} |
| 237 | 12 | print(formatTable(table)); |
| 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 | |
public void pendingMethods(List<String> methods) { |
| 253 | 12 | for (String method : methods) { |
| 254 | 24 | print(format("pendingMethod", "{0}\n", method)); |
| 255 | |
} |
| 256 | 12 | } |
| 257 | |
|
| 258 | |
public void restarted(String step, Throwable cause) { |
| 259 | 12 | print(format("restarted", "{0} {1}\n", step, cause.getMessage())); |
| 260 | 12 | } |
| 261 | |
|
| 262 | |
public void cancelled() { |
| 263 | 12 | String cancelled = format("cancelled", "\n"); |
| 264 | 12 | print(cancelled); |
| 265 | 12 | } |
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
protected String format(String key, String defaultPattern, Object... args) { |
| 277 | 6011 | String escape = escape(defaultPattern); |
| 278 | 6011 | String s = lookupPattern(key, escape); |
| 279 | 6011 | Object[] objects = escapeAll(args); |
| 280 | 6011 | return MessageFormat.format(s, objects); |
| 281 | |
} |
| 282 | |
|
| 283 | |
protected String formatTable(ExamplesTable table) { |
| 284 | 12 | OutputStream formatted = new ByteArrayOutputStream(); |
| 285 | 12 | PrintStream out = new PrintStream(formatted); |
| 286 | 12 | out.print(format("examplesTableStart", "\n")); |
| 287 | 12 | List<Map<String, String>> rows = table.getRows(); |
| 288 | 12 | List<String> headers = table.getHeaders(); |
| 289 | 12 | out.print(format("examplesTableHeadStart", "|")); |
| 290 | 12 | for (String header : headers) { |
| 291 | 24 | out.print(format("examplesTableHeadCell", "{0}|", header)); |
| 292 | |
} |
| 293 | 12 | out.print(format("examplesTableHeadEnd", "\n")); |
| 294 | 12 | out.print(format("examplesTableBodyStart", EMPTY)); |
| 295 | 12 | for (Map<String, String> row : rows) { |
| 296 | 24 | out.print(format("examplesTableRowStart", "|")); |
| 297 | 24 | for (String header : headers) { |
| 298 | 48 | out.print(format("examplesTableCell", "{0}|", row.get(header))); |
| 299 | |
} |
| 300 | 24 | out.print(format("examplesTableRowEnd", "\n")); |
| 301 | |
} |
| 302 | 12 | out.print(format("examplesTableBodyEnd", "")); |
| 303 | 12 | out.print(format("examplesTableEnd", "")); |
| 304 | 12 | return formatted.toString(); |
| 305 | |
} |
| 306 | |
|
| 307 | |
private String escape(String defaultPattern) { |
| 308 | 6011 | return (String) escapeAll(defaultPattern)[0]; |
| 309 | |
} |
| 310 | |
|
| 311 | |
private Object[] escapeAll(Object... args) { |
| 312 | 12022 | return escape(format, args); |
| 313 | |
} |
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
protected Object[] escape(final Format format, Object... args) { |
| 323 | |
|
| 324 | 12022 | Transformer escapingTransformer = new Transformer() { |
| 325 | |
public Object transform(Object object) { |
| 326 | 6883 | switch (format) { |
| 327 | |
case HTML: |
| 328 | 2182 | return escapeHtml(asString(object)).replace("\n", "<br/>"); |
| 329 | |
case XML: |
| 330 | 655 | return escapeXml(asString(object)); |
| 331 | |
default: |
| 332 | 4046 | return object; |
| 333 | |
} |
| 334 | |
} |
| 335 | |
|
| 336 | |
private String asString(Object object) { |
| 337 | 2837 | return (object != null ? object.toString() : EMPTY); |
| 338 | |
} |
| 339 | |
}; |
| 340 | 12022 | List<?> list = Arrays.asList(ArrayUtils.clone(args)); |
| 341 | 12022 | CollectionUtils.transform(list, escapingTransformer); |
| 342 | 12022 | return list.toArray(); |
| 343 | |
} |
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
protected String lookupPattern(String key, String defaultPattern) { |
| 358 | 6025 | if (outputPatterns.containsKey(key)) { |
| 359 | 2761 | return outputPatterns.getProperty(key); |
| 360 | |
} |
| 361 | 3264 | return defaultPattern; |
| 362 | |
} |
| 363 | |
|
| 364 | |
public boolean reportFailureTrace() { |
| 365 | 1 | return reportFailureTrace.get(); |
| 366 | |
} |
| 367 | |
|
| 368 | |
public PrintStreamOutput doReportFailureTrace(boolean reportFailureTrace) { |
| 369 | 382 | this.reportFailureTrace.set(reportFailureTrace); |
| 370 | 382 | return this; |
| 371 | |
} |
| 372 | |
|
| 373 | |
public boolean compressFailureTrace() { |
| 374 | 5 | return compressFailureTrace.get(); |
| 375 | |
} |
| 376 | |
|
| 377 | |
public PrintStreamOutput doCompressFailureTrace(boolean compressFailureTrace) { |
| 378 | 382 | this.compressFailureTrace.set(compressFailureTrace); |
| 379 | 382 | return this; |
| 380 | |
} |
| 381 | |
|
| 382 | |
protected void overwritePattern(String key, String pattern) { |
| 383 | 7 | outputPatterns.put(key, pattern); |
| 384 | 7 | } |
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
protected void print(String text) { |
| 393 | 647 | if (containsTable(text)) { |
| 394 | 0 | String tableStart = format(PARAMETER_TABLE_START, PARAMETER_TABLE_START); |
| 395 | 0 | String tableEnd = format(PARAMETER_TABLE_END, PARAMETER_TABLE_END); |
| 396 | 0 | String tableAsString = substringBetween(text, tableStart, tableEnd); |
| 397 | 0 | output.print(text |
| 398 | |
.replace(tableAsString, formatTable(new ExamplesTable(tableAsString))) |
| 399 | |
.replace(tableStart, format("parameterValueStart", EMPTY)) |
| 400 | |
.replace(tableEnd, format("parameterValueEnd", EMPTY)) |
| 401 | |
.replace(format(PARAMETER_VALUE_NEWLINE, PARAMETER_VALUE_NEWLINE), |
| 402 | |
format("parameterValueNewline", "\n"))); |
| 403 | 0 | } else { |
| 404 | 647 | output.print(text |
| 405 | |
.replace(format(PARAMETER_VALUE_START, PARAMETER_VALUE_START), format("parameterValueStart", EMPTY)) |
| 406 | |
.replace(format(PARAMETER_VALUE_END, PARAMETER_VALUE_END), format("parameterValueEnd", EMPTY)) |
| 407 | |
.replace(format(PARAMETER_VALUE_NEWLINE, PARAMETER_VALUE_NEWLINE), |
| 408 | |
format("parameterValueNewline", "\n"))); |
| 409 | |
} |
| 410 | 647 | } |
| 411 | |
|
| 412 | |
private boolean containsTable(String text) { |
| 413 | 647 | String tableStart = format(PARAMETER_TABLE_START, PARAMETER_TABLE_START); |
| 414 | 647 | String tableEnd = format(PARAMETER_TABLE_END, PARAMETER_TABLE_END); |
| 415 | 647 | return text.contains(tableStart) && text.contains(tableEnd); |
| 416 | |
} |
| 417 | |
|
| 418 | |
@Override |
| 419 | |
public String toString() { |
| 420 | 0 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(format).append(output).toString(); |
| 421 | |
} |
| 422 | |
|
| 423 | |
} |