View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.FileWriter;
17  import java.util.Arrays;
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Set;
21  
22  import com.eviware.soapui.impl.wsdl.panels.support.assertions.Assertable.AssertionStatus;
23  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
24  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
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  
34  public class JUnitReportCollector implements TestRunListener {
35  
36  	HashMap<String, JUnitReport> reports;
37  	HashMap<TestCase, StringBuffer> failures;
38  	
39  	public JUnitReportCollector() {
40  		reports = new HashMap<String, JUnitReport>();
41  		failures = new HashMap<TestCase, StringBuffer>();
42  	}
43  	
44  	public void saveReports(String path) throws Exception {
45  		Iterator<String> keyset = reports.keySet().iterator();
46  		while (keyset.hasNext()) {
47  			String name = keyset.next();
48  			JUnitReport report = reports.get(name);
49  			saveReport(report, path + File.separatorChar + "TEST-" + name + ".xml");
50  		}
51  	}
52  	
53  	private void saveReport(JUnitReport report, String filename) throws Exception {
54  		FileWriter fileWriter = new FileWriter(filename);
55  		fileWriter.write(report.toString());
56  		fileWriter.close();
57  	}
58  	
59  	public String getReport() {
60  		Set<String> keys = reports.keySet();
61  		if (keys.size() > 0) {
62  			String key = (String)keys.toArray()[0];
63  			return reports.get(key).toString();
64  		}
65  		return "No reports..:";
66  	}
67  	
68  	public void afterRun(TestRunner testRunner, TestRunContext runContext) {
69  		TestCase testCase = testRunner.getTestCase();
70  		JUnitReport report = reports.get(testCase.getTestSuite().getName());
71  		
72  		if (Status.INITIALIZED != testRunner.getStatus()
73  				&& Status.RUNNING != testRunner.getStatus()) {
74  			if (Status.CANCELED == testRunner.getStatus()) {
75  				report.addTestCaseWithFailure(testCase.getName(), testRunner.getTimeTaken(), testRunner.getReason(), "");
76  			}
77  			if ( Status.FAILED == testRunner.getStatus()) {
78  				String msg = "";
79  				if (failures.containsKey(testCase)) {
80  					msg = failures.get(testCase).toString();
81  				}
82  				report.addTestCaseWithFailure(testCase.getName(), testRunner.getTimeTaken(), testRunner.getReason(), msg);
83  			}
84  			if (Status.FINISHED == testRunner.getStatus()) {
85  				report.addTestCase(testCase.getName(), testRunner.getTimeTaken());
86  			}
87  			
88  		}
89  	}
90  
91  	public void afterStep(TestRunner testRunner, TestRunContext runContext, TestStepResult result) {
92  		TestStep currentStep = runContext.getCurrentStep();
93  		TestCase testCase = currentStep.getTestCase();
94  		
95  		if( currentStep instanceof WsdlTestRequestStep )
96  		{
97  			WsdlTestRequestStep requestStep = (WsdlTestRequestStep) currentStep;
98  			for( int c = 0; c < requestStep.getAssertionCount(); c++ )
99  			{
100 				WsdlMessageAssertion assertion = requestStep.getAssertionAt( c );
101 				
102 				if( assertion.getStatus() != AssertionStatus.VALID ) {
103 					StringBuffer buf = new StringBuffer();
104 					if (failures.containsKey(testCase)) {
105 						buf = failures.get(testCase);
106 					} else
107 						failures.put(testCase, buf);
108 					
109 					buf.append( assertion.getName() + " in [" + requestStep.getName() + "] failed;\n" );
110 					buf.append( Arrays.toString( assertion.getErrors() ) + "\n" );
111 					buf.append( "Request: " + requestStep.getTestRequest().getRequestContent() + "\n" );
112 					buf.append( "Response: " + requestStep.getTestRequest().getResponse().getContentAsString() + "\n" );
113 				}
114 			}
115 		}
116 	}
117 
118 	public void beforeRun(TestRunner testRunner, TestRunContext runContext) {
119 		TestCase testCase = testRunner.getTestCase();
120 		TestSuite testSuite = testCase.getTestSuite();
121 		if (!reports.containsKey(testSuite.getName())) {
122 			JUnitReport report = new JUnitReport();
123 			report.setTestSuiteName(testSuite.getProject().getName() + "." + testSuite.getName());
124 			reports.put(testSuite.getName(), report);
125 		}
126 	}
127 
128 	public void beforeStep(TestRunner testRunner, TestRunContext runContext) {
129 		
130 	}
131 
132 
133 }