1 package com.eviware.soapui.impl.wsdl.testcase;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Set;
7
8 import com.eviware.soapui.SoapUI;
9 import com.eviware.soapui.model.propertyexpansion.DefaultPropertyExpansionContext;
10 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
11 import com.eviware.soapui.model.support.TestRunListenerAdapter;
12 import com.eviware.soapui.model.testsuite.TestCase;
13 import com.eviware.soapui.model.testsuite.TestRunContext;
14 import com.eviware.soapui.model.testsuite.TestRunner;
15 import com.eviware.soapui.model.testsuite.TestSuite.TestSuiteRunType;
16
17 public class WsdlTestScenario implements Runnable
18 {
19 private boolean canceled;
20 private List<TestRunner> runners = new ArrayList<TestRunner>();
21 private InternalTestRunListener internalTestRunListener = new InternalTestRunListener();
22 private PropertyExpansionContext context;
23 private List<TestCase> testCaseList = new ArrayList<TestCase>();
24 private TestSuiteRunType runType;
25 private boolean skipTestCasesWithRunningLoadTest = true;
26 private boolean abortOnFail = false;
27 private Set<TestScenarioListener> listeners = new HashSet<TestScenarioListener>();
28
29 public WsdlTestScenario( TestSuiteRunType runType )
30 {
31 this.runType = runType;
32 }
33
34 public void cancel()
35 {
36 canceled = true;
37
38 for( TestRunner runner : runners )
39 {
40 runner.cancel( "Canceled from TestSuite" );
41 }
42 }
43
44 public void addTestCase( TestCase testCase )
45 {
46 testCaseList.add( testCase );
47 }
48
49 public void run()
50 {
51 canceled = false;
52
53 context = createContext();
54
55 beforeRun( context );
56
57 try
58 {
59 for( int c = 0; c < testCaseList.size(); c++ )
60 {
61 TestCase testCase = testCaseList.get( c );
62 if( testCase.isDisabled() ||
63 (skipTestCasesWithRunningLoadTest && SoapUI.getTestMonitor().hasRunningLoadTest( testCase ) ))
64 {
65 continue;
66 }
67
68 if( runType == TestSuiteRunType.PARALLEL )
69 {
70 testCase.addTestRunListener( internalTestRunListener );
71 }
72
73 beforeTestCase( testCase );
74
75 TestRunner runner = testCase.run( context.getProperties(), true );
76 runners.add( runner );
77
78 if( runType == TestSuiteRunType.SEQUENTIAL )
79 {
80 runner.waitUntilFinished();
81 afterTestCase( testCase, runner );
82 runners.remove( runner );
83
84 if( abortOnFail && runner.getStatus() == TestRunner.Status.FAILED )
85 break;
86 }
87
88 if( canceled )
89 break;
90 }
91 }
92 catch( Exception e )
93 {
94 SoapUI.logError( e );
95 }
96 finally
97 {
98 if( runners.isEmpty() )
99 afterRun( context );
100 }
101 }
102
103 protected PropertyExpansionContext createContext()
104 {
105 return new DefaultPropertyExpansionContext( null );
106 }
107
108 protected void afterTestCase( TestCase testCase, TestRunner runner )
109 {
110 for( TestScenarioListener listener : listeners.toArray( new TestScenarioListener[listeners.size()] ))
111 {
112 listener.afterTestCase( testCase, runner );
113 }
114 }
115
116 protected void beforeTestCase( TestCase testCase )
117 {
118 for( TestScenarioListener listener : listeners.toArray( new TestScenarioListener[listeners.size()] ))
119 {
120 listener.beforeTestCase( testCase );
121 }
122 }
123
124 protected void beforeRun(PropertyExpansionContext context)
125 {
126 for( TestScenarioListener listener : listeners.toArray( new TestScenarioListener[listeners.size()] ))
127 {
128 listener.beforeRun(context);
129 }
130 }
131
132 protected void afterRun(PropertyExpansionContext context)
133 {
134 for( TestScenarioListener listener : listeners.toArray( new TestScenarioListener[listeners.size()] ))
135 {
136 listener.afterRun(context);
137 }
138 }
139
140 /***
141 * Waits for running tests to finish when running in parallel
142 */
143
144 private class InternalTestRunListener extends TestRunListenerAdapter
145 {
146 public void afterRun( TestRunner testRunner, TestRunContext runContext )
147 {
148 runners.remove( testRunner );
149
150 WsdlTestCase testCase = ( WsdlTestCase ) testRunner.getTestCase();
151
152 testRunner.getTestCase().removeTestRunListener( this );
153 afterTestCase( testCase, testRunner );
154
155 if( runners.isEmpty() )
156 WsdlTestScenario.this.afterRun( context );
157
158 if( testRunner.getStatus() == TestRunner.Status.FAILED )
159 {
160 testCaseFailed( testCase );
161
162 if( abortOnFail && !canceled )
163 cancel();
164 }
165 }
166 }
167
168 public void testCaseFailed( WsdlTestCase testCase )
169 {
170 }
171
172 public boolean isAbortOnFail()
173 {
174 return abortOnFail;
175 }
176
177 public void setAbortOnFail( boolean abortOnFail )
178 {
179 this.abortOnFail = abortOnFail;
180 }
181
182 public TestSuiteRunType getRunType()
183 {
184 return runType;
185 }
186
187 public void setRunType( TestSuiteRunType runType )
188 {
189 this.runType = runType;
190 }
191
192 public boolean isSkipTestCasesWithRunningLoadTest()
193 {
194 return skipTestCasesWithRunningLoadTest;
195 }
196
197 public void setSkipTestCasesWithRunningLoadTest( boolean skipTestCasesWithRunningLoadTest )
198 {
199 this.skipTestCasesWithRunningLoadTest = skipTestCasesWithRunningLoadTest;
200 }
201
202 public boolean isCanceled()
203 {
204 return canceled;
205 }
206
207 public List<TestRunner> getRunners()
208 {
209 return runners;
210 }
211
212 public void addTestScenarioListener( TestScenarioListener listener )
213 {
214 listeners.add( listener );
215 }
216
217 public void removeTestScenarioListener( TestScenarioListener listener )
218 {
219 listeners.remove( listener );
220 }
221
222 protected List<TestCase> getTestCaseList()
223 {
224 return testCaseList;
225 }
226
227 public int getTestCaseCount()
228 {
229 return testCaseList.size();
230 }
231
232 public TestCase getTestCaseAt( int index )
233 {
234 return testCaseList.get( index );
235 }
236
237 public void removeTestCase( TestCase testCase )
238 {
239 testCaseList.remove( testCase );
240 }
241
242 public void removeAllTestCases()
243 {
244 testCaseList.clear();
245 }
246 }