View Javadoc

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