View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.TestCase;
26  import com.eviware.soapui.model.testsuite.TestRunContext;
27  import com.eviware.soapui.model.testsuite.TestRunListener;
28  import com.eviware.soapui.model.testsuite.TestRunner;
29  import com.eviware.soapui.model.testsuite.TestStep;
30  import com.eviware.soapui.model.testsuite.TestStepResult;
31  import com.eviware.soapui.model.testsuite.TestSuite;
32  import com.eviware.soapui.model.testsuite.TestRunner.Status;
33  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
34  import com.eviware.soapui.support.StringUtils;
35  import com.eviware.soapui.support.xml.XmlUtils;
36  
37  /***
38   * Collects TestRun results and creates JUnitReports
39   * 
40   * @author ole.matzura
41   */
42  
43  public class JUnitReportCollector implements TestRunListener {
44  
45  	HashMap<String, JUnitReport> reports;
46  	HashMap<TestCase, StringBuffer> failures;
47  	
48  	public JUnitReportCollector() {
49  		reports = new HashMap<String, JUnitReport>();
50  		failures = new HashMap<TestCase, StringBuffer>();
51  	}
52  	
53  	public List<String> saveReports(String path) throws Exception {
54  		
55  		File file = new File( path );
56  		if( !file.exists() || !file.isDirectory() )
57  			file.mkdirs();
58  		
59  		List<String> result = new ArrayList<String>();
60  		
61  		Iterator<String> keyset = reports.keySet().iterator();
62  		while (keyset.hasNext()) {
63  			String name = keyset.next();
64  			JUnitReport report = reports.get(name);
65  			String fileName = path + File.separatorChar + "TEST-" + StringUtils.createFileName( name, '_' ) + ".xml";
66  			saveReport(report, fileName);
67  			result.add( fileName );
68  		}
69  		
70  		return result;
71  	}
72  	
73  	public HashMap<String, JUnitReport> getReports()
74  	{
75  		return reports;
76  	}
77  
78  	private void saveReport(JUnitReport report, String filename) throws Exception {
79  		report.save(new File( filename ));
80  	}
81  	
82  	public String getReport() {
83  		Set<String> keys = reports.keySet();
84  		if (keys.size() > 0) {
85  			String key = (String)keys.toArray()[0];
86  			return reports.get(key).toString();
87  		}
88  		return "No reports..:";
89  	}
90  	
91  	public void afterRun(TestRunner testRunner, TestRunContext runContext) {
92  		TestCase testCase = testRunner.getTestCase();
93  		JUnitReport report = reports.get(testCase.getTestSuite().getName());
94  		
95  		if (Status.INITIALIZED != testRunner.getStatus()
96  				&& Status.RUNNING != testRunner.getStatus()) {
97  			if (Status.CANCELED == testRunner.getStatus()) {
98  				report.addTestCaseWithFailure(testCase.getName(), testRunner.getTimeTaken(), testRunner.getReason(), "");
99  			}
100 			if ( Status.FAILED == testRunner.getStatus()) {
101 				String msg = "";
102 				if (failures.containsKey(testCase)) {
103 					msg = failures.get(testCase).toString();
104 				}
105 				report.addTestCaseWithFailure(testCase.getName(), testRunner.getTimeTaken(), testRunner.getReason(), msg);
106 			}
107 			if (Status.FINISHED == testRunner.getStatus()) {
108 				report.addTestCase(testCase.getName(), testRunner.getTimeTaken());
109 			}
110 			
111 		}
112 	}
113 
114 	public void afterStep(TestRunner testRunner, TestRunContext runContext, TestStepResult result) {
115 		TestStep currentStep = runContext.getCurrentStep();
116 		TestCase testCase = currentStep.getTestCase();
117 		
118 		if( result.getStatus() == TestStepStatus.FAILED )
119 		{
120 			StringBuffer buf = new StringBuffer();
121 			if (failures.containsKey(testCase)) {
122 				buf = failures.get(testCase);
123 			} else
124 				failures.put(testCase, buf);
125 			
126 			buf.append( "<h3><b>" + result.getTestStep().getName() + " Failed</b></h3>" );
127 			buf.append( "<pre>" + XmlUtils.entitize( Arrays.toString( result.getMessages() )) + "\n" );
128 			
129 			StringWriter stringWriter = new StringWriter();
130 			PrintWriter writer = new PrintWriter( stringWriter );
131 			result.writeTo( writer );
132 			
133 			buf.append( XmlUtils.entitize( stringWriter.toString()) );
134 			buf.append( "</pre><hr/>" );
135 		}
136 	}
137 
138 	public void beforeRun(TestRunner testRunner, TestRunContext runContext) {
139 		TestCase testCase = testRunner.getTestCase();
140 		TestSuite testSuite = testCase.getTestSuite();
141 		if (!reports.containsKey(testSuite.getName())) {
142 			JUnitReport report = new JUnitReport();
143 			report.setTestSuiteName( testSuite.getName());
144 			reports.put(testSuite.getName(), report);
145 		}
146 	}
147 
148 	public void beforeStep(TestRunner testRunner, TestRunContext runContext) 
149 	{
150 	}
151 
152 	public void reset()
153 	{
154 		reports.clear();
155 		failures.clear();
156 	}
157 }