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