View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.report;
    14
    15  import java.math.BigDecimal;
    16  import java.util.HashMap;
    17  import java.util.Map;
    18  import java.util.Set;
    19
    20  import org.apache.xmlbeans.XmlOptions;
    21
    22  import com.eviware.soapui.junit.Properties;
    23  import com.eviware.soapui.junit.Property;
    24  import com.eviware.soapui.junit.Testcase;
    25  import com.eviware.soapui.junit.Testsuite;
    26  import com.eviware.soapui.junit.TestsuiteDocument;
    27  import com.eviware.soapui.junit.FailureDocument.Failure;
    28
    29  /***
    30   * Wrapper for a number of Test runs
    31   */
    32
    33  public class JUnitReport {
    34  	TestsuiteDocument testsuiteDoc;
    35  	int noofTestCases, noofFailures, noofErrors;
    36  	double totalTime;
    37  	StringBuffer systemOut;
    38  	StringBuffer systemErr;
    39
    40  	public JUnitReport(){
    41  		systemOut = new StringBuffer();
    42  		systemErr = new StringBuffer();
    43
    44  		testsuiteDoc = TestsuiteDocument.Factory.newInstance();
    45  		Testsuite testsuite = testsuiteDoc.addNewTestsuite();
    46  		Properties properties = testsuite.addNewProperties();
    47  		setSystemProperties(properties);
    48  	}
    49
    50  	public void setTotalTime(double time) {
    51  		testsuiteDoc.getTestsuite().setTime(new BigDecimal(time));
    52  	}
    53
    54  	public void setTestSuiteName(String name) {
    55  		testsuiteDoc.getTestsuite().setName(name);
    56  	}
    57
    58  	public void setNoofErrorsInTestSuite(int errors) {
    59  		testsuiteDoc.getTestsuite().setErrors(errors);
    60  	}
    61
    62  	public void setNoofFailuresInTestSuite(int failures) {
    63  		testsuiteDoc.getTestsuite().setFailures(failures);
    64  	}
    65
    66  	public void systemOut(String systemout) {
    67  		systemOut.append(systemout);
    68  	}
    69
    70  	public void systemErr(String systemerr) {
    71  		systemErr.append(systemerr);
    72  	}
    73
    74  	public void setSystemOut(String systemout) {
    75  		testsuiteDoc.getTestsuite().setSystemOut(systemout);
    76  	}
    77
    78  	public void setSystemErr(String systemerr) {
    79  		testsuiteDoc.getTestsuite().setSystemErr(systemerr);
    80  	}
    81
    82  	public Testcase addTestCase(String name, double time) {
    83  		Testcase testcase = testsuiteDoc.getTestsuite().addNewTestcase();
    84  		testcase.setName(name);
    85  		testcase.setTime(time);
    86  		noofTestCases++;
    87  		totalTime += time;
    88  		return testcase;
    89  	}
    90
    91  	public Testcase addTestCaseWithFailure(String name, double time, String failure, String stacktrace) {
    92  		Testcase testcase = testsuiteDoc.getTestsuite().addNewTestcase();
    93  		testcase.setName(name);
    94  		testcase.setTime(time);
    95  		Failure fail = testcase.addNewFailure();
    96  		fail.setType(failure);
    97  		fail.setStringValue(stacktrace);
    98  		noofTestCases++;
    99  		noofFailures++;
    100 		totalTime += time;
    101 		return testcase;
    102 	}
    103
    104 	public Testcase addTestCaseWithError(String name, double time, String error, String stacktrace) {
    105 		Testcase testcase = testsuiteDoc.getTestsuite().addNewTestcase();
    106 		testcase.setName(name);
    107 		testcase.setTime(time);
    108 		com.eviware.soapui.junit.ErrorDocument.Error err = testcase.addNewError();
    109 		err.setType(error);
    110 		err.setStringValue(stacktrace);
    111 		noofTestCases++;
    112 		noofErrors++;
    113 		totalTime += time;
    114 		return testcase;
    115 	}
    116
    117 	private void setSystemProperties(Properties properties) {
    118 		Set keys = System.getProperties().keySet();
    119 		for (Object keyO : keys) {
    120 			String key = keyO.toString();
    121 			String value = System.getProperty(key);
    122 			Property prop = properties.addNewProperty();
    123 			prop.setName(key);
    124 			prop.setValue(value);
    125 		}
    126 	}
    127
    128 	@SuppressWarnings("unchecked")
    129 	public String toString() {
    130 		finishReport();
    131
    132 		Map prefixes = new HashMap();
    133 		prefixes.put("", "http://eviware.com/soapui/junit");
    134
    135 		return testsuiteDoc.xmlText(
    136 				new XmlOptions()
    137 					.setSaveOuter()
    138 					.setCharacterEncoding( "utf-8" )
    139 					.setUseDefaultNamespace()
    140 					.setSaveImplicitNamespaces(prefixes));
    141 	}
    142
    143 	public TestsuiteDocument finishReport()
    144 	{
    145 		testsuiteDoc.getTestsuite().setTests(noofTestCases);
    146 		testsuiteDoc.getTestsuite().setFailures(noofFailures);
    147 		testsuiteDoc.getTestsuite().setErrors(noofErrors);
    148 		testsuiteDoc.getTestsuite().setTime( new BigDecimal( totalTime ));
    149
    150 		return testsuiteDoc;
    151 	}
    152 }