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.FileWriter;
    17  import java.io.PrintWriter;
    18  import java.io.StringWriter;
    19  import java.util.ArrayList;
    20  import java.util.Arrays;
    21  import java.util.HashMap;
    22  import java.util.Iterator;
    23  import java.util.List;
    24  import java.util.Set;
    25
    26  import com.eviware.soapui.model.testsuite.TestCase;
    27  import com.eviware.soapui.model.testsuite.TestRunContext;
    28  import com.eviware.soapui.model.testsuite.TestRunListener;
    29  import com.eviware.soapui.model.testsuite.TestRunner;
    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.TestRunner.Status;
    34  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
    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  		List<String> result = new ArrayList<String>();
    56
    57  		Iterator<String> keyset = reports.keySet().iterator();
    58  		while (keyset.hasNext()) {
    59  			String name = keyset.next();
    60  			JUnitReport report = reports.get(name);
    61  			String fileName = path + File.separatorChar + "TEST-" + name + ".xml";
    62  			saveReport(report, fileName);
    63  			result.add( fileName );
    64  		}
    65
    66  		return result;
    67  	}
    68
    69  	public HashMap<String, JUnitReport> getReports()
    70  	{
    71  		return reports;
    72  	}
    73
    74  	private void saveReport(JUnitReport report, String filename) throws Exception {
    75  		FileWriter fileWriter = new FileWriter(filename);
    76  		fileWriter.write(report.toString());
    77  		fileWriter.close();
    78  	}
    79
    80  	public String getReport() {
    81  		Set<String> keys = reports.keySet();
    82  		if (keys.size() > 0) {
    83  			String key = (String)keys.toArray()[0];
    84  			return reports.get(key).toString();
    85  		}
    86  		return "No reports..:";
    87  	}
    88
    89  	public void afterRun(TestRunner testRunner, TestRunContext runContext) {
    90  		TestCase testCase = testRunner.getTestCase();
    91  		JUnitReport report = reports.get(testCase.getTestSuite().getName());
    92
    93  		if (Status.INITIALIZED != testRunner.getStatus()
    94  				&& Status.RUNNING != testRunner.getStatus()) {
    95  			if (Status.CANCELED == testRunner.getStatus()) {
    96  				report.addTestCaseWithFailure(testCase.getName(), testRunner.getTimeTaken(), testRunner.getReason(), "");
    97  			}
    98  			if ( Status.FAILED == testRunner.getStatus()) {
    99  				String msg = "";
    100 				if (failures.containsKey(testCase)) {
    101 					msg = failures.get(testCase).toString();
    102 				}
    103 				report.addTestCaseWithFailure(testCase.getName(), testRunner.getTimeTaken(), testRunner.getReason(), msg);
    104 			}
    105 			if (Status.FINISHED == testRunner.getStatus()) {
    106 				report.addTestCase(testCase.getName(), testRunner.getTimeTaken());
    107 			}
    108
    109 		}
    110 	}
    111
    112 	public void afterStep(TestRunner testRunner, TestRunContext runContext, TestStepResult result) {
    113 		TestStep currentStep = runContext.getCurrentStep();
    114 		TestCase testCase = currentStep.getTestCase();
    115
    116 		if( result.getStatus() == TestStepStatus.FAILED )
    117 		{
    118 			StringBuffer buf = new StringBuffer();
    119 			if (failures.containsKey(testCase)) {
    120 				buf = failures.get(testCase);
    121 			} else
    122 				failures.put(testCase, buf);
    123
    124 			buf.append( "<b>" + result.getTestStep().getName() + " Failed</b>" );
    125 			buf.append( "<pre>" + XmlUtils.entitize( Arrays.toString( result.getMessages() )) + "</pre>\n" );
    126
    127 			StringWriter stringWriter = new StringWriter();
    128 			PrintWriter writer = new PrintWriter( stringWriter );
    129 			result.writeTo( writer );
    130
    131 			buf.append( XmlUtils.entitize( stringWriter.toString()) );
    132 		}
    133 	}
    134
    135 	public void beforeRun(TestRunner testRunner, TestRunContext runContext) {
    136 		TestCase testCase = testRunner.getTestCase();
    137 		TestSuite testSuite = testCase.getTestSuite();
    138 		if (!reports.containsKey(testSuite.getName())) {
    139 			JUnitReport report = new JUnitReport();
    140 			report.setTestSuiteName( testSuite.getName());
    141 			reports.put(testSuite.getName(), report);
    142 		}
    143 	}
    144
    145 	public void beforeStep(TestRunner testRunner, TestRunContext runContext)
    146 	{
    147 	}
    148
    149 	public void reset()
    150 	{
    151 		reports.clear();
    152 		failures.clear();
    153 	}
    154 }