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.impl.wsdl.teststeps;
14  
15  import com.eviware.soapui.impl.wsdl.mock.MockRunnerManager;
16  import com.eviware.soapui.impl.wsdl.mock.MockRunnerManagerException;
17  import com.eviware.soapui.impl.wsdl.mock.MockRunnerManagerImpl;
18  import com.eviware.soapui.model.support.TestRunListenerAdapter;
19  import com.eviware.soapui.model.testsuite.LoadTestRunner;
20  import com.eviware.soapui.model.testsuite.TestCase;
21  import com.eviware.soapui.model.testsuite.TestRunContext;
22  import com.eviware.soapui.model.testsuite.TestRunner;
23  import org.apache.log4j.Logger;
24  
25  import java.util.List;
26  
27  public class TestRunListenerImpl extends TestRunListenerAdapter
28  {
29  	private final static Logger log = 
30  		Logger.getLogger(TestRunListenerImpl.class);
31  	
32  	public void beforeRun(TestRunner testRunner, TestRunContext runContext)
33  	{
34  		LoadTestRunner loadTestRunner = 
35  			(LoadTestRunner) runContext.getProperty("LoadTestRunner");
36  
37  		if (loadTestRunner == null)
38  		{
39  			TestCase testCase = testRunner.getTestCase();
40  
41  			if (needsMockRunnerManager(testCase))
42  			{
43  				createMockServices(testCase);
44  				
45  				MockRunnerManager manager = MockRunnerManagerImpl.getInstance(
46  						testCase);
47  				
48  				try
49  				{
50  					manager.start();
51  				}
52  				catch (MockRunnerManagerException e)
53  				{
54  					log.error("Unable to start MockRunnerManager", e);
55  				}
56  			}
57  		}
58  	}
59  
60  	public void afterRun(TestRunner testRunner, TestRunContext runContext)
61  	{
62  		LoadTestRunner loadTestRunner = 
63  			(LoadTestRunner) runContext.getProperty("LoadTestRunner");
64  
65  		if (loadTestRunner == null)
66  		{
67  			TestCase testCase = testRunner.getTestCase();
68  
69  			MockRunnerManager manager = MockRunnerManagerImpl.getInstance(
70  					testCase);
71  
72  			if (manager != null)
73  			{
74  				manager.stop();
75  			}
76  		}
77  	}
78  	
79  	private boolean needsMockRunnerManager(TestCase testCase)
80  	{
81  		return testCase.getTestStepsOfType(
82  				WsdlAsyncResponseTestStep.class).size() > 0;
83  	}
84  
85  	private void createMockServices(TestCase testCase)
86  	{
87  		List<WsdlAsyncResponseTestStep> teststeps = 
88  			testCase.getTestStepsOfType(WsdlAsyncResponseTestStep.class);
89  		
90  		for (WsdlAsyncResponseTestStep teststep : teststeps)
91  		{
92  			teststep.createMockService();
93  		}
94  	}
95  }