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.math.BigDecimal;
016: import java.util.HashMap;
017: import java.util.Map;
018: import java.util.Set;
019:
020: import org.apache.xmlbeans.XmlOptions;
021:
022: import com.eviware.soapui.junit.Properties;
023: import com.eviware.soapui.junit.Property;
024: import com.eviware.soapui.junit.Testcase;
025: import com.eviware.soapui.junit.Testsuite;
026: import com.eviware.soapui.junit.TestsuiteDocument;
027: import com.eviware.soapui.junit.FailureDocument.Failure;
028:
029: /**
030: * Wrapper for a number of Test runs
031: */
032:
033: public class JUnitReport {
034: TestsuiteDocument testsuiteDoc;
035: int noofTestCases, noofFailures, noofErrors;
036: double totalTime;
037: StringBuffer systemOut;
038: StringBuffer systemErr;
039:
040: public JUnitReport() {
041: systemOut = new StringBuffer();
042: systemErr = new StringBuffer();
043:
044: testsuiteDoc = TestsuiteDocument.Factory.newInstance();
045: Testsuite testsuite = testsuiteDoc.addNewTestsuite();
046: Properties properties = testsuite.addNewProperties();
047: setSystemProperties(properties);
048: }
049:
050: public void setTotalTime(double time) {
051: testsuiteDoc.getTestsuite().setTime(new BigDecimal(time));
052: }
053:
054: public void setTestSuiteName(String name) {
055: testsuiteDoc.getTestsuite().setName(name);
056: }
057:
058: public void setNoofErrorsInTestSuite(int errors) {
059: testsuiteDoc.getTestsuite().setErrors(errors);
060: }
061:
062: public void setNoofFailuresInTestSuite(int failures) {
063: testsuiteDoc.getTestsuite().setFailures(failures);
064: }
065:
066: public void systemOut(String systemout) {
067: systemOut.append(systemout);
068: }
069:
070: public void systemErr(String systemerr) {
071: systemErr.append(systemerr);
072: }
073:
074: public void setSystemOut(String systemout) {
075: testsuiteDoc.getTestsuite().setSystemOut(systemout);
076: }
077:
078: public void setSystemErr(String systemerr) {
079: testsuiteDoc.getTestsuite().setSystemErr(systemerr);
080: }
081:
082: public Testcase addTestCase(String name, double time) {
083: Testcase testcase = testsuiteDoc.getTestsuite()
084: .addNewTestcase();
085: testcase.setName(name);
086: testcase.setTime(time);
087: noofTestCases++;
088: totalTime += time;
089: return testcase;
090: }
091:
092: public Testcase addTestCaseWithFailure(String name, double time,
093: String failure, String stacktrace) {
094: Testcase testcase = testsuiteDoc.getTestsuite()
095: .addNewTestcase();
096: testcase.setName(name);
097: testcase.setTime(time);
098: Failure fail = testcase.addNewFailure();
099: fail.setType(failure);
100: fail.setStringValue(stacktrace);
101: noofTestCases++;
102: noofFailures++;
103: totalTime += time;
104: return testcase;
105: }
106:
107: public Testcase addTestCaseWithError(String name, double time,
108: String error, String stacktrace) {
109: Testcase testcase = testsuiteDoc.getTestsuite()
110: .addNewTestcase();
111: testcase.setName(name);
112: testcase.setTime(time);
113: com.eviware.soapui.junit.ErrorDocument.Error err = testcase
114: .addNewError();
115: err.setType(error);
116: err.setStringValue(stacktrace);
117: noofTestCases++;
118: noofErrors++;
119: totalTime += time;
120: return testcase;
121: }
122:
123: private void setSystemProperties(Properties properties) {
124: Set keys = System.getProperties().keySet();
125: for (Object keyO : keys) {
126: String key = keyO.toString();
127: String value = System.getProperty(key);
128: Property prop = properties.addNewProperty();
129: prop.setName(key);
130: prop.setValue(value);
131: }
132: }
133:
134: @SuppressWarnings("unchecked")
135: public String toString() {
136: finishReport();
137:
138: Map prefixes = new HashMap();
139: prefixes.put("", "http://eviware.com/soapui/junit");
140:
141: return testsuiteDoc.xmlText(new XmlOptions().setSaveOuter()
142: .setCharacterEncoding("utf-8").setUseDefaultNamespace()
143: .setSaveImplicitNamespaces(prefixes));
144: }
145:
146: public TestsuiteDocument finishReport() {
147: testsuiteDoc.getTestsuite().setTests(noofTestCases);
148: testsuiteDoc.getTestsuite().setFailures(noofFailures);
149: testsuiteDoc.getTestsuite().setErrors(noofErrors);
150: testsuiteDoc.getTestsuite().setTime(new BigDecimal(totalTime));
151:
152: return testsuiteDoc;
153: }
154: }
|