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  package com.eviware.soapui.impl.wsdl.panels.testcase;
13  
14  import java.text.SimpleDateFormat;
15  import java.util.Date;
16  
17  import javax.swing.SwingUtilities;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner;
21  import com.eviware.soapui.model.support.TestRunListenerAdapter;
22  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
23  import com.eviware.soapui.model.testsuite.TestCaseRunner;
24  import com.eviware.soapui.model.testsuite.TestStepResult;
25  
26  public class TestRunLogTestRunListener extends TestRunListenerAdapter
27  {
28  	protected SimpleDateFormat dateFormat;
29  	protected final JTestRunLog runLog;
30  	protected final boolean clearOnRun;
31  
32  	public TestRunLogTestRunListener( JTestRunLog runLog, boolean clearOnRun )
33  	{
34  		this.runLog = runLog;
35  		this.clearOnRun = clearOnRun;
36  		dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS" );
37  	}
38  
39  	public void beforeRun( TestCaseRunner testRunner, TestCaseRunContext runContext )
40  	{
41  		if( SoapUI.getTestMonitor().hasRunningLoadTest( testRunner.getTestCase() ) )
42  			return;
43  
44  		if( clearOnRun )
45  			runLog.clear();
46  
47  		String testCaseName = testRunner.getTestCase().getName();
48  		runLog.addBoldText( "TestCase [" + testCaseName + "] started at " + dateFormat.format( new Date() ) );
49  		runLog.setStepIndex( 0 );
50  	}
51  
52  	public void afterRun( TestCaseRunner testRunner, TestCaseRunContext runContext )
53  	{
54  		if( SoapUI.getTestMonitor().hasRunningLoadTest( testRunner.getTestCase() ) )
55  			return;
56  
57  		WsdlTestCaseRunner wsdlRunner = ( WsdlTestCaseRunner )testRunner;
58  
59  		String testCaseName = testRunner.getTestCase().getName();
60  		if( testRunner.getStatus() == TestCaseRunner.Status.CANCELED )
61  			runLog.addText( "TestCase [" + testCaseName + "] canceled [" + testRunner.getReason() + "], time taken = "
62  					+ wsdlRunner.getTimeTaken() );
63  		else if( testRunner.getStatus() == TestCaseRunner.Status.FAILED )
64  		{
65  			String msg = wsdlRunner.getReason();
66  			if( wsdlRunner.getError() != null )
67  			{
68  				if( msg != null )
69  					msg += ":";
70  
71  				msg += wsdlRunner.getError();
72  			}
73  
74  			runLog.addText( "TestCase [" + testCaseName + "] failed [" + msg + "], time taken = "
75  					+ wsdlRunner.getTimeTaken() );
76  		}
77  		else
78  			runLog.addText( "TestCase [" + testCaseName + "] finished with status [" + testRunner.getStatus()
79  					+ "], time taken = " + wsdlRunner.getTimeTaken() );
80  	}
81  
82  	public synchronized void afterStep( TestCaseRunner testRunner, TestCaseRunContext runContext,
83  			final TestStepResult stepResult )
84  	{
85  		if( SoapUI.getTestMonitor().hasRunningLoadTest( testRunner.getTestCase() ) )
86  			return;
87  
88  		SwingUtilities.invokeLater( new Runnable()
89  		{
90  
91  			public void run()
92  			{
93  		runLog.addTestStepResult( stepResult );
94  	}
95  		} );
96  	}
97  }