View Javadoc

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