1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.tools;
14
15 import java.io.File;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import org.apache.commons.cli.CommandLine;
20 import org.apache.commons.cli.CommandLineParser;
21 import org.apache.commons.cli.Options;
22 import org.apache.commons.cli.PosixParser;
23 import org.apache.log4j.Logger;
24
25 import com.eviware.soapui.SoapUI;
26 import com.eviware.soapui.impl.wsdl.WsdlProject;
27 import com.eviware.soapui.impl.wsdl.teststeps.WsdlAssertion;
28 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
29 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
30 import com.eviware.soapui.impl.wsdl.teststeps.WsdlAssertion.AssertionStatus;
31 import com.eviware.soapui.model.testsuite.TestCase;
32 import com.eviware.soapui.model.testsuite.TestRunListener;
33 import com.eviware.soapui.model.testsuite.TestRunner;
34 import com.eviware.soapui.model.testsuite.TestStep;
35 import com.eviware.soapui.model.testsuite.TestSuite;
36
37 /***
38 * Standalone test-runner used from maven-plugin, can also be used from command-line (see xdocs) or
39 * directly from other classes.
40 * <p>
41 * For standalone usage, set the project file (with setProjectFile) and other desired properties before
42 * calling run</p>
43 *
44 * @author Ole.Matzura
45 */
46
47 public class SoapUITestCaseRunner implements TestRunListener
48 {
49 private String projectFile;
50 private String testSuite;
51 private String testCase;
52 private List<WsdlAssertion> assertions = new ArrayList<WsdlAssertion>();
53 private String endpoint;
54 private final static Logger log = Logger.getLogger( SoapUITestCaseRunner.class );
55 private long testCaseStartTime;
56 private String domain;
57 private String password;
58 private String username;
59
60 /***
61 * Runs the tests in the specified soapui project file, see soapui xdocs for details.
62 *
63 * @param args
64 * @throws Exception
65 */
66
67 public static void main( String [] args) throws Exception
68 {
69 System.out.println( "SoapUI " + SoapUI.SOAPUI_VERSION + " TestRunner");
70
71 SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
72
73 Options options = new Options();
74 options.addOption( "e", true, "Sets the endpoint" );
75 options.addOption( "s", true, "Sets the testsuite" );
76 options.addOption( "c", true, "Sets the testcase" );
77 options.addOption( "u", true, "Sets the username" );
78 options.addOption( "p", true, "Sets the password" );
79 options.addOption( "d", true, "Sets the domain" );
80
81 CommandLineParser parser = new PosixParser();
82 CommandLine cmd = parser.parse( options, args);
83
84 String[] args2 = cmd.getArgs();
85 if( args2.length != 1 )
86 {
87 System.out.println( "Missing soapui project file.." );
88 return;
89 }
90
91 runner.setProjectFile( args2[0] );
92
93 if( cmd.hasOption( "e"))
94 runner.setEndpoint( cmd.getOptionValue( "e" ) );
95
96 if( cmd.hasOption( "s"))
97 runner.setTestSuite( cmd.getOptionValue( "s") );
98
99 if( cmd.hasOption( "c"))
100 runner.setTestCase( cmd.getOptionValue( "c") );
101
102 if( cmd.hasOption( "u"))
103 runner.setUsername( cmd.getOptionValue( "u") );
104
105 if( cmd.hasOption( "p"))
106 runner.setPassword( cmd.getOptionValue( "p") );
107
108 if( cmd.hasOption( "d"))
109 runner.setDomain( cmd.getOptionValue( "d") );
110
111 runner.run();
112 }
113
114 /***
115 * Sets the domain to use for any authentications
116 *
117 * @param domain the domain to use for any authentications
118 */
119
120 public void setDomain(String domain)
121 {
122 log.info( "Setting domain to [" + domain + "]" );
123 this.domain = domain;
124 }
125
126 /***
127 * Sets the password to use for any authentications
128 *
129 * @param domain the password to use for any authentications
130 */
131
132 public void setPassword(String password)
133 {
134 log.info( "Setting password to [" + password + "]" );
135 this.password = password;
136 }
137
138 /***
139 * Sets the username to use for any authentications
140 *
141 * @param domain the username to use for any authentications
142 */
143
144 public void setUsername(String username)
145 {
146 log.info( "Setting username to [" + username + "]" );
147 this.username = username;
148 }
149
150 /***
151 * Runs the testcases as configured with setXXX methods
152 *
153 * @throws Exception thrown if any tests fail
154 */
155
156 public void run() throws Exception
157 {
158 assertions.clear();
159
160 if( !new File( projectFile ).exists() )
161 throw new Exception( "soapui project file [" + projectFile + "] not found" );
162
163 WsdlProject project = new WsdlProject( projectFile );
164 log.info( "Running soapui tests in project [" + project.getName() + "]" );
165
166 for( int c = 0; c < project.getTestSuiteCount(); c++ )
167 {
168 if( testSuite == null ||
169 project.getTestSuiteAt( c ).getName().equalsIgnoreCase( testSuite ))
170 {
171 runSuite( project.getTestSuiteAt( c ));
172 }
173 }
174
175 if( assertions.size() > 0 )
176 {
177 StringBuffer buf = new StringBuffer();
178 for( int c = 0; c < assertions.size(); c++ )
179 {
180 WsdlAssertion assertion = assertions.get( c );
181 buf.append( assertion.getName() + " in [" + assertion.getRequest().getName() + "] failed;\n" );
182 buf.append( assertion.getErrors() + "\n" );
183 buf.append( "Request: " + assertion.getRequest().getRequestContent() + "\n" );
184 buf.append( "Response: " + ((WsdlTestRequest)assertion.getRequest()).getResponseContent() + "\n" );
185 }
186
187 throw new Exception( buf.toString() );
188 }
189 }
190
191 /***
192 * Run tests in the specified TestSuite
193 *
194 * @param suite the TestSuite to run
195 */
196
197 public void runSuite(TestSuite suite)
198 {
199 log.info(( "Running soapui suite [" + suite.getName() + "]"));
200 long start = System.currentTimeMillis();
201 for( int c = 0; c < suite.getTestCaseCount(); c++ )
202 {
203 String name = suite.getTestCaseAt( c ).getName();
204 if( testCase == null ||
205 name.equalsIgnoreCase( testCase ))
206 {
207 runTestCase( suite.getTestCaseAt( c ));
208 }
209 else
210 System.out.println( "Skipping testcase [" + name + "], filter is [" + testCase + "]");
211 }
212 log.info( "soapui suite [" + suite.getName() + "] finished in " + (System.currentTimeMillis()-start) + "ms" );
213 }
214
215 /***
216 * Runs the specified TestCase
217 *
218 * @param testCase the testcase to run
219 */
220
221 private void runTestCase(TestCase testCase)
222 {
223 testCase.addTestRunListener( this );
224 testCase.run().waitUntilFinished();
225 }
226
227 /***
228 * Sets the soapui project file containing the tests to run
229 *
230 * @param projectFile the soapui project file containing the tests to run
231 */
232
233 public void setProjectFile(String projectFile)
234 {
235 log.info( "setting projectFile to [" + projectFile + "]" );
236 this.projectFile = projectFile;
237 }
238
239 /***
240 * Sets the testcase to run
241 *
242 * @param testCase the testcase to run
243 */
244
245 public void setTestCase(String testCase)
246 {
247 log.info( "setting testCase to [" + testCase + "]" );
248 this.testCase = testCase;
249 }
250
251 /***
252 * Sets the endpoint to use for all test requests
253 *
254 * @param endpoint the endpoint to use for all test requests
255 */
256
257 public void setEndpoint(String endpoint)
258 {
259 log.info( "setting test endpoint to [" + endpoint+ "]" );
260 this.endpoint = endpoint.trim();
261 }
262
263 /***
264 * Sets the TestSuite to run. If not set all TestSuites in the specified project file are run
265 *
266 * @param testSuite the testSuite to run.
267 */
268
269 public void setTestSuite(String testSuite)
270 {
271 log.info( "setting testSuite to [" + testSuite + "]" );
272 this.testSuite = testSuite;
273 }
274
275 public boolean beforeRun(TestRunner testRunner)
276 {
277 testCaseStartTime = System.currentTimeMillis();
278 log.info( "Running soapui testcase [" + testRunner.getTestCase().getName() + "]");
279 return true;
280 }
281
282 public boolean beforeStep(TestRunner testRunner)
283 {
284 TestStep currentStep = testRunner.getCurrentStep();
285 log.info( "runing step [" + currentStep.getName() + "]" );
286
287 if( currentStep instanceof WsdlTestRequestStep )
288 {
289 WsdlTestRequestStep requestStep = (WsdlTestRequestStep) currentStep;
290 if( endpoint != null && endpoint.length() > 0 )
291 {
292 requestStep.getTestRequest().setEndpoint( endpoint );
293 }
294
295 if( username != null && username.length() > 0 )
296 {
297 requestStep.getTestRequest().setUsername( username );
298 }
299
300 if( password != null && password.length() > 0 )
301 {
302 requestStep.getTestRequest().setPassword( password );
303 }
304
305 if( domain != null && domain.length() > 0 )
306 {
307 requestStep.getTestRequest().setDomain( domain );
308 }
309 }
310
311 return true;
312 }
313
314 public boolean afterStep(TestRunner testRunner)
315 {
316 TestStep currentStep = testRunner.getCurrentStep();
317
318 if( currentStep instanceof WsdlTestRequestStep )
319 {
320 WsdlTestRequestStep requestStep = (WsdlTestRequestStep) currentStep;
321 for( int c = 0; c < requestStep.getAssertionCount(); c++ )
322 {
323 WsdlAssertion assertion = requestStep.getAssertionAt( c );
324 System.out.println( "Assertion [" + assertion.getName() + "] has status " + assertion.getStatus());
325 if( assertion.getStatus() != AssertionStatus.VALID )
326 {
327 System.out.println( "ASSERTION FAILED -> " + assertion.getErrors());
328 assertions.add( assertion );
329 }
330 }
331 }
332
333 return true;
334 }
335
336 public void afterRun(TestRunner testRunner)
337 {
338 long end = System.currentTimeMillis();
339 log.info( "Finished running soapui testcase [" + testRunner.getTestCase().getName() + "], time taken = " +
340 (end-testCaseStartTime) + "ms" );
341 }
342 }