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.io.File;
    16  import java.io.IOException;
    17  import java.math.BigDecimal;
    18  import java.util.HashMap;
    19  import java.util.Map;
    20  import java.util.Set;
    21
    22  import org.apache.xmlbeans.XmlOptions;
    23
    24  import com.eviware.soapui.junit.Properties;
    25  import com.eviware.soapui.junit.Property;
    26  import com.eviware.soapui.junit.Testcase;
    27  import com.eviware.soapui.junit.Testsuite;
    28  import com.eviware.soapui.junit.TestsuiteDocument;
    29  import com.eviware.soapui.junit.FailureDocument.Failure;
    30
    31  /***
    32   * Wrapper for a number of Test runs
    33   */
    34
    35  public class JUnitReport {
    36  	TestsuiteDocument testsuiteDoc;
    37  	int noofTestCases, noofFailures, noofErrors;
    38  	double totalTime;
    39  	StringBuffer systemOut;
    40  	StringBuffer systemErr;
    41
    42  	public JUnitReport(){
    43  		systemOut = new StringBuffer();
    44  		systemErr = new StringBuffer();
    45
    46  		testsuiteDoc = TestsuiteDocument.Factory.newInstance();
    47  		Testsuite testsuite = testsuiteDoc.addNewTestsuite();
    48  		Properties properties = testsuite.addNewProperties();
    49  		setSystemProperties(properties);
    50  	}
    51
    52  	public void setTotalTime(double time) {
    53  		testsuiteDoc.getTestsuite().setTime(new BigDecimal(time));
    54  	}
    55
    56  	public void setTestSuiteName(String name) {
    57  		testsuiteDoc.getTestsuite().setName(name);
    58  	}
    59
    60  	public void setNoofErrorsInTestSuite(int errors) {
    61  		testsuiteDoc.getTestsuite().setErrors(errors);
    62  	}
    63
    64  	public void setNoofFailuresInTestSuite(int failures) {
    65  		testsuiteDoc.getTestsuite().setFailures(failures);
    66  	}
    67
    68  	public void systemOut(String systemout) {
    69  		systemOut.append(systemout);
    70  	}
    71
    72  	public void systemErr(String systemerr) {
    73  		systemErr.append(systemerr);
    74  	}
    75
    76  	public void setSystemOut(String systemout) {
    77  		testsuiteDoc.getTestsuite().setSystemOut(systemout);
    78  	}
    79
    80  	public void setSystemErr(String systemerr) {
    81  		testsuiteDoc.getTestsuite().setSystemErr(systemerr);
    82  	}
    83
    84  	public Testcase addTestCase(String name, double time) {
    85  		Testcase testcase = testsuiteDoc.getTestsuite().addNewTestcase();
    86  		testcase.setName(name);
    87  		testcase.setTime(time);
    88  		noofTestCases++;
    89  		totalTime += time;
    90  		return testcase;
    91  	}
    92
    93  	public Testcase addTestCaseWithFailure(String name, double time, String failure, String stacktrace) {
    94  		Testcase testcase = testsuiteDoc.getTestsuite().addNewTestcase();
    95  		testcase.setName(name);
    96  		testcase.setTime(time);
    97  		Failure fail = testcase.addNewFailure();
    98  		fail.setType(failure);
    99  		fail.setStringValue(stacktrace);
    100 		noofTestCases++;
    101 		noofFailures++;
    102 		totalTime += time;
    103 		return testcase;
    104 	}
    105
    106 	public Testcase addTestCaseWithError(String name, double time, String error, String stacktrace) {
    107 		Testcase testcase = testsuiteDoc.getTestsuite().addNewTestcase();
    108 		testcase.setName(name);
    109 		testcase.setTime(time);
    110 		com.eviware.soapui.junit.ErrorDocument.Error err = testcase.addNewError();
    111 		err.setType(error);
    112 		err.setStringValue(stacktrace);
    113 		noofTestCases++;
    114 		noofErrors++;
    115 		totalTime += time;
    116 		return testcase;
    117 	}
    118
    119 	private void setSystemProperties(Properties properties) {
    120 		Set keys = System.getProperties().keySet();
    121 		for (Object keyO : keys) {
    122 			String key = keyO.toString();
    123 			String value = System.getProperty(key);
    124 			Property prop = properties.addNewProperty();
    125 			prop.setName(key);
    126 			prop.setValue(value);
    127 		}
    128 	}
    129
    130 	@SuppressWarnings("unchecked")
    131 	public void save( File file ) throws IOException {
    132 		finishReport();
    133
    134 		Map prefixes = new HashMap();
    135 		prefixes.put("", "http://eviware.com/soapui/junit");
    136
    137 		testsuiteDoc.save( file,
    138 				new XmlOptions()
    139 					.setSaveOuter()
    140 					.setCharacterEncoding( "utf-8" )
    141 					.setUseDefaultNamespace()
    142 					.setSaveImplicitNamespaces(prefixes));
    143 	}
    144
    145 	public TestsuiteDocument finishReport()
    146 	{
    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 }