001: /*
002: * Created on 01.11.2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package org.jzonic.webtester.writer;
008:
009: import java.io.PrintWriter;
010: import java.io.StringWriter;
011: import java.util.Iterator;
012: import java.util.List;
013:
014: import org.jzonic.webtester.WebTestResult;
015: import org.jzonic.webtester.WebTestSuiteResult;
016: import org.jzonic.webtester.commands.WebTestNodeResult;
017:
018: /**
019: * @author amecky
020: *
021: */
022: public abstract class AbstractWebTestResultWriter implements
023: WebTestResultWriter {
024:
025: protected String generateTestResult(WebTestResult result) {
026: StringBuffer buffer = new StringBuffer();
027: buffer.append("Test:........: ");
028: buffer.append(result.getWebTestName());
029: buffer.append("\n");
030: buffer.append("Duration:....: ");
031: buffer.append(result.getDuration());
032: buffer.append("ms");
033: buffer.append("\n");
034: List results = result.getTestResults();
035: for (Iterator iter = results.iterator(); iter.hasNext();) {
036: WebTestNodeResult element = (WebTestNodeResult) iter.next();
037: buffer.append(element.getLineNumber() + 1);
038: buffer.append(" : ");
039: buffer.append(element.getCommand());
040: buffer.append(" | ");
041: buffer.append(element.getParam());
042: if (element.isSuccess()) {
043: buffer.append(" ... OK ");
044: } else {
045: buffer.append(" ... Failure : ");
046: buffer.append(element.getErrorMessage());
047: }
048: buffer.append("\n");
049: }
050: if (result.isSuccess()) {
051: buffer.append("Result:......: OK");
052: buffer.append("\n");
053: } else {
054: buffer.append("Result:......: Failure");
055: buffer.append("\n");
056: if (result.getErrorNode() != null) {
057: WebTestNodeResult errorNode = result.getErrorNode();
058: if (errorNode.getErrorMessage() != null) {
059: buffer.append("Error:.......: ");
060: buffer.append(errorNode.getErrorMessage());
061: buffer.append("\n");
062: }
063: if (errorNode.getException() != null) {
064: buffer.append("Exception:");
065: buffer.append(getStackTrace(errorNode
066: .getException()));
067: buffer.append("\n");
068: }
069: }
070: }
071: return buffer.toString();
072: }
073:
074: private String getStackTrace(Exception e) {
075: StringWriter sw = new StringWriter();
076: PrintWriter pw = new PrintWriter(sw);
077: e.printStackTrace(pw);
078: pw.close();
079: return sw.getBuffer().toString();
080: }
081:
082: protected String generateSuiteHeader(WebTestSuiteResult suiteResult) {
083: StringBuffer buffer = new StringBuffer();
084: buffer.append("Suite:.......: ");
085: buffer.append(suiteResult.getTestSuiteName());
086: buffer.append("\n");
087: buffer.append("Total tests..: ");
088: buffer.append(suiteResult.getTotalCounter());
089: buffer.append("\n");
090: buffer.append("Errors:......: ");
091: buffer.append(suiteResult.getErrorCounter());
092: buffer.append("\n");
093: buffer.append("Failures:....: ");
094: buffer.append(formatPercentage(suiteResult.getTotalCounter(),
095: suiteResult.getErrorCounter()));
096: buffer.append("\n");
097: return buffer.toString();
098: }
099:
100: private String formatPercentage(int total, int errors) {
101: if (total == 0) {
102: return "-";
103: }
104: double per = (double) errors / (double) total;
105: per *= 100.0;
106: int sx = (int) per;
107: return String.valueOf(sx) + "%";
108: }
109:
110: protected void generateSuiteResult(WebTestSuiteResult suiteResult) {
111: List results = suiteResult.getTestResults();
112: for (int i = 0; i < results.size(); i++) {
113: WebTestResult result = (WebTestResult) results.get(i);
114: generateResult(result);
115: }
116: }
117: }
|