001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.report;
014:
015: import java.io.File;
016: import java.io.FileWriter;
017: import java.io.PrintWriter;
018: import java.io.StringWriter;
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025:
026: import com.eviware.soapui.model.testsuite.TestCase;
027: import com.eviware.soapui.model.testsuite.TestRunContext;
028: import com.eviware.soapui.model.testsuite.TestRunListener;
029: import com.eviware.soapui.model.testsuite.TestRunner;
030: import com.eviware.soapui.model.testsuite.TestStep;
031: import com.eviware.soapui.model.testsuite.TestStepResult;
032: import com.eviware.soapui.model.testsuite.TestSuite;
033: import com.eviware.soapui.model.testsuite.TestRunner.Status;
034: import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
035: import com.eviware.soapui.support.xml.XmlUtils;
036:
037: /**
038: * Collects TestRun results and creates JUnitReports
039: *
040: * @author ole.matzura
041: */
042:
043: public class JUnitReportCollector implements TestRunListener {
044:
045: HashMap<String, JUnitReport> reports;
046: HashMap<TestCase, StringBuffer> failures;
047:
048: public JUnitReportCollector() {
049: reports = new HashMap<String, JUnitReport>();
050: failures = new HashMap<TestCase, StringBuffer>();
051: }
052:
053: public List<String> saveReports(String path) throws Exception {
054:
055: List<String> result = new ArrayList<String>();
056:
057: Iterator<String> keyset = reports.keySet().iterator();
058: while (keyset.hasNext()) {
059: String name = keyset.next();
060: JUnitReport report = reports.get(name);
061: String fileName = path + File.separatorChar + "TEST-"
062: + name + ".xml";
063: saveReport(report, fileName);
064: result.add(fileName);
065: }
066:
067: return result;
068: }
069:
070: public HashMap<String, JUnitReport> getReports() {
071: return reports;
072: }
073:
074: private void saveReport(JUnitReport report, String filename)
075: throws Exception {
076: FileWriter fileWriter = new FileWriter(filename);
077: fileWriter.write(report.toString());
078: fileWriter.close();
079: }
080:
081: public String getReport() {
082: Set<String> keys = reports.keySet();
083: if (keys.size() > 0) {
084: String key = (String) keys.toArray()[0];
085: return reports.get(key).toString();
086: }
087: return "No reports..:";
088: }
089:
090: public void afterRun(TestRunner testRunner,
091: TestRunContext runContext) {
092: TestCase testCase = testRunner.getTestCase();
093: JUnitReport report = reports.get(testCase.getTestSuite()
094: .getName());
095:
096: if (Status.INITIALIZED != testRunner.getStatus()
097: && Status.RUNNING != testRunner.getStatus()) {
098: if (Status.CANCELED == testRunner.getStatus()) {
099: report.addTestCaseWithFailure(testCase.getName(),
100: testRunner.getTimeTaken(), testRunner
101: .getReason(), "");
102: }
103: if (Status.FAILED == testRunner.getStatus()) {
104: String msg = "";
105: if (failures.containsKey(testCase)) {
106: msg = failures.get(testCase).toString();
107: }
108: report.addTestCaseWithFailure(testCase.getName(),
109: testRunner.getTimeTaken(), testRunner
110: .getReason(), msg);
111: }
112: if (Status.FINISHED == testRunner.getStatus()) {
113: report.addTestCase(testCase.getName(), testRunner
114: .getTimeTaken());
115: }
116:
117: }
118: }
119:
120: public void afterStep(TestRunner testRunner,
121: TestRunContext runContext, TestStepResult result) {
122: TestStep currentStep = runContext.getCurrentStep();
123: TestCase testCase = currentStep.getTestCase();
124:
125: if (result.getStatus() == TestStepStatus.FAILED) {
126: StringBuffer buf = new StringBuffer();
127: if (failures.containsKey(testCase)) {
128: buf = failures.get(testCase);
129: } else
130: failures.put(testCase, buf);
131:
132: buf.append("<b>" + result.getTestStep().getName()
133: + " Failed</b>");
134: buf.append("<pre>"
135: + XmlUtils.entitize(Arrays.toString(result
136: .getMessages())) + "</pre>\n");
137:
138: StringWriter stringWriter = new StringWriter();
139: PrintWriter writer = new PrintWriter(stringWriter);
140: result.writeTo(writer);
141:
142: buf.append(XmlUtils.entitize(stringWriter.toString()));
143: }
144: }
145:
146: public void beforeRun(TestRunner testRunner,
147: TestRunContext runContext) {
148: TestCase testCase = testRunner.getTestCase();
149: TestSuite testSuite = testCase.getTestSuite();
150: if (!reports.containsKey(testSuite.getName())) {
151: JUnitReport report = new JUnitReport();
152: report.setTestSuiteName(testSuite.getName());
153: reports.put(testSuite.getName(), report);
154: }
155: }
156:
157: public void beforeStep(TestRunner testRunner,
158: TestRunContext runContext) {
159: }
160:
161: public void reset() {
162: reports.clear();
163: failures.clear();
164: }
165: }
|