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