1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.tools;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.apache.log4j.Logger;
19
20 import com.eviware.soapui.SoapUI;
21 import com.eviware.soapui.impl.wsdl.WsdlProject;
22 import com.eviware.soapui.impl.wsdl.teststeps.WsdlAssertion;
23 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
24 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
25 import com.eviware.soapui.impl.wsdl.teststeps.WsdlAssertion.AssertionStatus;
26 import com.eviware.soapui.model.testsuite.TestCase;
27 import com.eviware.soapui.model.testsuite.TestRunListener;
28 import com.eviware.soapui.model.testsuite.TestRunner;
29 import com.eviware.soapui.model.testsuite.TestStep;
30 import com.eviware.soapui.model.testsuite.TestSuite;
31
32 /***
33 * Standalone test-runner used from maven-plugin
34 *
35 * @author Ole.Matzura
36 */
37
38 public class SoapUITestCaseRunner implements TestRunListener
39 {
40 private String projectFile;
41 private String testSuite;
42 private String testCase;
43 private List<WsdlAssertion> assertions = new ArrayList<WsdlAssertion>();
44 private String endpoint;
45 private final static Logger log = Logger.getLogger( SoapUITestCaseRunner.class );
46 private long testCaseStartTime;
47
48 public static void main( String [] args) throws Exception
49 {
50 System.out.println( "SoapUI " + SoapUI.SOAPUI_VERSION + " TestRunner");
51
52 SoapUITestCaseRunner runner = new SoapUITestCaseRunner();
53 runner.setProjectFile( args[0] );
54 if( args.length > 1 && args[1].trim().length() > 0 )
55 runner.setEndpoint( args[1] );
56
57 if( args.length > 2 && args[2].trim().length() > 0 )
58 runner.setTestSuite( args[2] );
59
60 if( args.length > 3 && args[3].trim().length() > 0 )
61 runner.setTestCase( args[3] );
62
63 runner.run();
64 }
65
66 public void run() throws Exception
67 {
68 assertions.clear();
69
70 WsdlProject project = new WsdlProject( projectFile );
71 log.info( "Running soapui tests in project [" + project.getName() + "]" );
72
73 for( int c = 0; c < project.getTestSuiteCount(); c++ )
74 {
75 if( testSuite == null || testSuite.equals( "*") ||
76 project.getTestSuiteAt( c ).getName().equalsIgnoreCase( testSuite ))
77 {
78 runSuite( project.getTestSuiteAt( c ));
79 }
80 }
81
82 if( assertions.size() > 0 )
83 {
84 StringBuffer buf = new StringBuffer();
85 for( int c = 0; c < assertions.size(); c++ )
86 {
87 WsdlAssertion assertion = assertions.get( c );
88 buf.append( assertion.getName() + " in [" + assertion.getRequest().getName() + "] failed;\n" );
89 buf.append( assertion.getErrors() + "\n" );
90 buf.append( "Request: " + assertion.getRequest().getRequestContent() + "\n" );
91 buf.append( "Response: " + ((WsdlTestRequest)assertion.getRequest()).getResponseContent() + "\n" );
92 }
93 throw new Exception( buf.toString() );
94 }
95 }
96
97 private void runSuite(TestSuite suite)
98 {
99 log.info(( "Running soapui suite [" + suite.getName() + "]"));
100 long start = System.currentTimeMillis();
101 for( int c = 0; c < suite.getTestCaseCount(); c++ )
102 {
103 String name = suite.getTestCaseAt( c ).getName();
104 if( testCase == null || testCase.equals("*") ||
105 name.equalsIgnoreCase( testCase ))
106 {
107 runTestCase( suite.getTestCaseAt( c ));
108 }
109 else
110 System.out.println( "Skipping testcase [" + name + "], filter is [" + testCase + "]");
111 }
112 log.info( "soapui suite [" + suite.getName() + "] finished in " + (System.currentTimeMillis()-start) + "ms" );
113 }
114
115 private void runTestCase(TestCase testCase)
116 {
117 testCase.addTestRunListener( this );
118 testCase.run().waitUntilFinished();
119 }
120
121 public void setProjectFile(String projectFile)
122 {
123 log.info( "setting projectFile to [" + projectFile + "]" );
124 this.projectFile = projectFile;
125 }
126
127 public void setTestCase(String testCase)
128 {
129 log.info( "setting testCase to [" + testCase + "]" );
130 this.testCase = testCase;
131 }
132
133 public void setEndpoint(String endpoint)
134 {
135 log.info( "setting test endpoint to [" + endpoint+ "]" );
136 this.endpoint = endpoint.trim();
137 }
138
139 public void setTestSuite(String testSuite)
140 {
141 log.info( "setting testSuite to [" + testSuite + "]" );
142 this.testSuite = testSuite;
143 }
144
145 public boolean beforeRun(TestRunner testRunner)
146 {
147 testCaseStartTime = System.currentTimeMillis();
148 log.info( "Running soapui testcase [" + testRunner.getTestCase().getName() + "]");
149 return true;
150 }
151
152 public boolean beforeStep(TestRunner testRunner)
153 {
154 TestStep currentStep = testRunner.getCurrentStep();
155 log.info( "runing step [" + currentStep.getName() + "]" );
156
157 if( endpoint != null && endpoint.length() > 0 && currentStep instanceof WsdlTestRequestStep )
158 {
159 WsdlTestRequestStep requestStep = (WsdlTestRequestStep) currentStep;
160 requestStep.getTestRequest().setEndpoint( endpoint );
161 }
162
163 return true;
164 }
165
166 public boolean afterStep(TestRunner testRunner)
167 {
168 TestStep currentStep = testRunner.getCurrentStep();
169
170 if( currentStep instanceof WsdlTestRequestStep )
171 {
172 WsdlTestRequestStep requestStep = (WsdlTestRequestStep) currentStep;
173 for( int c = 0; c < requestStep.getAssertionCount(); c++ )
174 {
175 WsdlAssertion assertion = requestStep.getAssertionAt( c );
176 System.out.println( "Assertion [" + assertion.getName() + "] has status " + assertion.getStatus());
177 if( assertion.getStatus() != AssertionStatus.VALID )
178 {
179 System.out.println( "ASSERTION FAILED -> " + assertion.getErrors());
180 assertions.add( assertion );
181 }
182 }
183 }
184
185 return true;
186 }
187
188 public void afterRun(TestRunner testRunner)
189 {
190 long end = System.currentTimeMillis();
191 log.info( "Finished running soapui testcase [" + testRunner.getTestCase().getName() + "], time taken = " +
192 (end-testCaseStartTime) + "ms" );
193 }
194 }