01: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.reporting;
03:
04: import java.io.ByteArrayOutputStream;
05: import java.io.File;
06: import java.io.FileOutputStream;
07: import java.io.IOException;
08: import java.io.PrintStream;
09: import java.io.PrintWriter;
10: import java.util.Iterator;
11:
12: import com.canoo.webtest.boundary.PackageBoundary;
13:
14: /**
15: * @author unknown
16: * @author Marc Guillemot
17: */
18: public class PlainTextReporter implements IResultReporter {
19:
20: public static String getBuildFailMessage(final RootStepResult result) {
21: final StringBuffer sb = new StringBuffer();
22: sb.append(PackageBoundary.versionMessage()).append(
23: "\nTest failed.");
24:
25: if (result.isError()) {
26: sb.append("\nException raised: ");
27: sb.append(result.getException());
28: final ByteArrayOutputStream bs = new ByteArrayOutputStream();
29:
30: result.getException().printStackTrace(new PrintStream(bs));
31: sb.append(bs.toString());
32: }
33: if (result.isFailure()) {
34: final StepResult failedStepResult = result
35: .getFailingTaskResult();
36:
37: sb.append("\nTest step ");
38: sb.append(result.getTaskName());
39: sb.append(" (").append(failedStepResult.getLocation())
40: .append(") ");
41: sb.append(failedStepResult.getTaskDescription());
42: sb.append(" failed with message \"");
43: sb.append(result.getException().getMessage());
44: sb.append("\"");
45: }
46: return sb.toString();
47: }
48:
49: public void generateReport(final RootStepResult result)
50: throws IOException {
51: final File outputFile = new File(result.getConfig()
52: .getWebTestResultDir(), "WebTestReport.txt");
53: final PrintWriter out = new PrintWriter(new FileOutputStream(
54: outputFile));
55:
56: print(out, result);
57: out.close();
58: }
59:
60: protected void print(final PrintWriter out,
61: final RootStepResult result) {
62: out.println("Test step summary for specification: \""
63: + result.getWebtestName() + "\"");
64: out.println(PackageBoundary.versionMessage());
65:
66: int count = 1;
67:
68: for (final Iterator i = result.getChildren().iterator(); i
69: .hasNext();) {
70: final StepResult stepResult = (StepResult) i.next();
71:
72: out.println("\t" + count++ + ". "
73: + stepResult.getTaskName());
74: if (stepResult.isCompleted()) {
75: out.println("\t\t" + "Duration: "
76: + stepResult.getDuration() + " ms");
77: } else {
78: out.println("\t\t==> Step was not completed!");
79: }
80: out.println();
81: }
82: if (!result.isSuccessful()) {
83: out.println(getBuildFailMessage(result));
84: }
85: }
86: }
|