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.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Set;
22
23 import com.eviware.soapui.model.testsuite.ProjectRunContext;
24 import com.eviware.soapui.model.testsuite.ProjectRunListener;
25 import com.eviware.soapui.model.testsuite.ProjectRunner;
26 import com.eviware.soapui.model.testsuite.TestCase;
27 import com.eviware.soapui.model.testsuite.TestCaseRunContext;
28 import com.eviware.soapui.model.testsuite.TestCaseRunner;
29 import com.eviware.soapui.model.testsuite.TestRunListener;
30 import com.eviware.soapui.model.testsuite.TestStep;
31 import com.eviware.soapui.model.testsuite.TestStepResult;
32 import com.eviware.soapui.model.testsuite.TestSuite;
33 import com.eviware.soapui.model.testsuite.TestSuiteRunContext;
34 import com.eviware.soapui.model.testsuite.TestSuiteRunListener;
35 import com.eviware.soapui.model.testsuite.TestSuiteRunner;
36 import com.eviware.soapui.model.testsuite.TestRunner.Status;
37 import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
38 import com.eviware.soapui.support.StringUtils;
39 import com.eviware.soapui.support.xml.XmlUtils;
40
41 /***
42 * Collects TestRun results and creates JUnitReports
43 *
44 * @author ole.matzura
45 */
46
47 public class JUnitReportCollector implements TestRunListener, TestSuiteRunListener, ProjectRunListener
48 {
49 HashMap<String, JUnitReport> reports;
50 HashMap<TestCase, String> failures;
51
52 public JUnitReportCollector()
53 {
54 reports = new HashMap<String, JUnitReport>();
55 failures = new HashMap<TestCase, String>();
56 }
57
58 public List<String> saveReports( String path ) throws Exception
59 {
60
61 File file = new File( path );
62 if( !file.exists() || !file.isDirectory() )
63 file.mkdirs();
64
65 List<String> result = new ArrayList<String>();
66
67 Iterator<String> keyset = reports.keySet().iterator();
68 while( keyset.hasNext() )
69 {
70 String name = keyset.next();
71 JUnitReport report = reports.get( name );
72 String fileName = path + File.separatorChar + "TEST-" + StringUtils.createFileName( name, '_' ) + ".xml";
73 saveReport( report, fileName );
74 result.add( fileName );
75 }
76
77 return result;
78 }
79
80 public HashMap<String, JUnitReport> getReports()
81 {
82 return reports;
83 }
84
85 private void saveReport( JUnitReport report, String filename ) throws Exception
86 {
87 report.save( new File( filename ) );
88 }
89
90 public String getReport()
91 {
92 Set<String> keys = reports.keySet();
93 if( keys.size() > 0 )
94 {
95 String key = ( String )keys.toArray()[0];
96 return reports.get( key ).toString();
97 }
98 return "No reports..:";
99 }
100
101 public void afterRun( TestCaseRunner testRunner, TestCaseRunContext runContext )
102 {
103 TestCase testCase = testRunner.getTestCase();
104 JUnitReport report = reports.get( testCase.getTestSuite().getName() );
105
106 if( Status.INITIALIZED != testRunner.getStatus() && Status.RUNNING != testRunner.getStatus() )
107 {
108 if( Status.CANCELED == testRunner.getStatus() )
109 {
110 report.addTestCaseWithFailure( testCase.getName(), testRunner.getTimeTaken(), testRunner.getReason(), "" );
111 }
112 if( Status.FAILED == testRunner.getStatus() )
113 {
114 String msg = "";
115 if( failures.containsKey( testCase ) )
116 {
117 msg = failures.get( testCase ).toString();
118 }
119 report.addTestCaseWithFailure( testCase.getName(), testRunner.getTimeTaken(), testRunner.getReason(), msg );
120 }
121 if( Status.FINISHED == testRunner.getStatus() )
122 {
123 report.addTestCase( testCase.getName(), testRunner.getTimeTaken() );
124 }
125
126 }
127 }
128
129 public void afterStep( TestCaseRunner testRunner, TestCaseRunContext runContext, TestStepResult result )
130 {
131 TestStep currentStep = result.getTestStep();
132 TestCase testCase = currentStep.getTestCase();
133
134 if( result.getStatus() == TestStepStatus.FAILED )
135 {
136 StringBuffer buf = new StringBuffer();
137
138
139
140
141
142 buf.append( "<h3><b>" + result.getTestStep().getName() + " Failed</b></h3><pre>" );
143 buf.append( "<pre>" + XmlUtils.entitize( Arrays.toString( result.getMessages() ) ) + "\n" );
144
145
146
147
148
149
150 buf.append( "</pre><hr/>" );
151
152 failures.put( testCase, buf.toString() );
153 }
154 }
155
156 public void beforeRun( TestCaseRunner testRunner, TestCaseRunContext runContext )
157 {
158 TestCase testCase = testRunner.getTestCase();
159 TestSuite testSuite = testCase.getTestSuite();
160 if( !reports.containsKey( testSuite.getName() ) )
161 {
162 JUnitReport report = new JUnitReport();
163 report.setTestSuiteName( testSuite.getName() );
164 reports.put( testSuite.getName(), report );
165 }
166 }
167
168 public void beforeStep( TestCaseRunner testRunner, TestCaseRunContext runContext )
169 {
170 }
171
172 public void beforeStep( TestCaseRunner testRunner, TestCaseRunContext runContext, TestStep testStep )
173 {
174 }
175
176 public void reset()
177 {
178 reports.clear();
179 failures.clear();
180 }
181
182 public void afterRun( TestSuiteRunner testRunner, TestSuiteRunContext runContext )
183 {
184 }
185
186 public void afterTestCase( TestSuiteRunner testRunner, TestSuiteRunContext runContext, TestCaseRunner testCaseRunner )
187 {
188 testCaseRunner.getTestCase().removeTestRunListener( this );
189 }
190
191 public void beforeRun( TestSuiteRunner testRunner, TestSuiteRunContext runContext )
192 {
193 }
194
195 public void beforeTestCase( TestSuiteRunner testRunner, TestSuiteRunContext runContext, TestCase testCase )
196 {
197 testCase.addTestRunListener( this );
198 }
199
200 public void afterRun( ProjectRunner testScenarioRunner, ProjectRunContext runContext )
201 {
202 }
203
204 public void afterTestSuite( ProjectRunner testScenarioRunner, ProjectRunContext runContext,
205 TestSuiteRunner testRunner )
206 {
207 testRunner.getTestSuite().removeTestSuiteRunListener( this );
208 }
209
210 public void beforeRun( ProjectRunner testScenarioRunner, ProjectRunContext runContext )
211 {
212 }
213
214 public void beforeTestSuite( ProjectRunner testScenarioRunner, ProjectRunContext runContext, TestSuite testSuite )
215 {
216 testSuite.addTestSuiteRunListener( this );
217 }
218 }