1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.actions.iface.tools.soapui;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20
21 import javax.swing.Action;
22
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.WsdlTestSuite;
28 import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
29 import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
30 import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
31 import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
32 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
33 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
34 import com.eviware.soapui.model.iface.Interface;
35 import com.eviware.soapui.model.support.ModelSupport;
36 import com.eviware.soapui.model.testsuite.TestCase;
37 import com.eviware.soapui.model.testsuite.TestSuite;
38 import com.eviware.soapui.support.StringUtils;
39 import com.eviware.soapui.support.UISupport;
40 import com.eviware.soapui.support.types.StringToStringMap;
41 import com.eviware.x.form.XForm;
42 import com.eviware.x.form.XFormDialog;
43 import com.eviware.x.form.XFormDialogBuilder;
44 import com.eviware.x.form.XFormFactory;
45 import com.eviware.x.form.XFormField;
46 import com.eviware.x.form.XFormFieldListener;
47
48 /***
49 * Invokes soapUI TestRunner tool
50 *
51 * @author Ole.Matzura
52 */
53
54 public class TestRunnerAction extends AbstractToolsAction<WsdlProject>
55 {
56 private static final String ALL_VALUE = "<all>";
57 private static final String ENDPOINT = "Endpoint";
58 private static final String HOSTPORT = "Host:Port";
59 private static final String TESTSUITE = "TestSuite";
60 private static final String TESTCASE = "TestCase";
61 private static final String USERNAME = "Username";
62 private static final String PASSWORD = "Password";
63 private static final String WSSTYPE = "WSS Password Type";
64 private static final String DOMAIN = "Domain";
65 private static final String PRINTREPORT = "Print Report";
66 private static final String ROOTFOLDER = "Root Folder";
67 private static final String EXPORTJUNITRESULTS = "Export JUnit Results";
68 private static final String EXPORTALL = "Export All";
69 private static final String ENABLEUI = "Enable UI";
70 private static final String TESTRUNNERPATH = "TestRunner Path";
71 private static final String SAVEPROJECT = "Save Project";
72 private static final String ADDSETTINGS = "Add Settings";
73 private static final String OPEN_REPORT = "Open Report";
74 private static final String COVERAGE = "Coverage Report";
75
76 private XForm mainForm;
77
78 private final static Logger log = Logger.getLogger( TestRunnerAction.class );
79
80 public static final String SOAPUI_ACTION_ID = "TestRunnerAction";
81
82 private XForm advForm;
83
84 private List<TestSuite> testSuites;
85
86 public TestRunnerAction()
87 {
88 super( "Launch TestRunner", "Launch command-line TestRunner for this project" );
89 }
90
91 protected XFormDialog buildDialog( WsdlProject modelItem )
92 {
93 if( modelItem == null )
94 return null;
95
96 XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Launch TestRunner" );
97
98 mainForm = builder.createForm( "Basic" );
99 mainForm.addComboBox( TESTSUITE, new String[] {}, "The TestSuite to run" ).addFormFieldListener(
100 new XFormFieldListener()
101 {
102
103 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
104 {
105 List<String> testCases = new ArrayList<String>();
106 String tc = mainForm.getComponentValue( TESTCASE );
107
108 if( newValue.equals( ALL_VALUE ) )
109 {
110 for( TestSuite testSuite : testSuites )
111 {
112 for( TestCase testCase : testSuite.getTestCaseList() )
113 {
114 if( !testCases.contains( testCase.getName() ) )
115 testCases.add( testCase.getName() );
116 }
117 }
118 }
119 else
120 {
121 TestSuite testSuite = getModelItem().getTestSuiteByName( newValue );
122 if( testSuite != null )
123 testCases.addAll( Arrays.asList( ModelSupport.getNames( testSuite.getTestCaseList() ) ) );
124 }
125
126 testCases.add( 0, ALL_VALUE );
127 mainForm.setOptions( TESTCASE, testCases.toArray() );
128
129 if( testCases.contains( tc ) )
130 {
131 mainForm.getFormField( TESTCASE ).setValue( tc );
132 }
133 }
134 } );
135
136 mainForm.addComboBox( TESTCASE, new String[] {}, "The TestCase to run" );
137 mainForm.addSeparator();
138 mainForm.addCheckBox( PRINTREPORT, "Prints a summary report to the console" );
139 mainForm.addCheckBox( EXPORTJUNITRESULTS, "Exports results to a JUnit-Style report" );
140 mainForm.addCheckBox( EXPORTALL, "Exports all results (not only errors)" );
141 mainForm.addTextField( ROOTFOLDER, "Folder to export to", XForm.FieldType.FOLDER );
142 mainForm.addCheckBox( COVERAGE, "Generate WSDL Coverage report (soapUI Pro only)" );
143 mainForm.addCheckBox( OPEN_REPORT, "Open generated HTML report in browser (soapUI Pro only)" );
144 mainForm.addSeparator();
145 mainForm.addCheckBox( ENABLEUI, "Enables UI components in scripts" );
146 mainForm.addTextField( TESTRUNNERPATH, "Folder containing TestRunner.bat to use", XForm.FieldType.FOLDER );
147 mainForm.addCheckBox( SAVEPROJECT, "Saves project before running" ).setEnabled( !modelItem.isRemote() );
148 mainForm.addCheckBox( ADDSETTINGS, "Adds global settings to command-line" );
149
150 advForm = builder.createForm( "Overrides" );
151 advForm.addComboBox( ENDPOINT, new String[] { "" }, "endpoint to forward to" );
152 advForm.addTextField( HOSTPORT, "Host:Port to use for requests", XForm.FieldType.TEXT );
153 advForm.addSeparator();
154 advForm.addTextField( USERNAME, "The username to set for all requests", XForm.FieldType.TEXT );
155 advForm.addTextField( PASSWORD, "The password to set for all requests", XForm.FieldType.PASSWORD );
156 advForm.addTextField( DOMAIN, "The domain to set for all requests", XForm.FieldType.TEXT );
157 advForm.addComboBox( WSSTYPE, new String[] { "", "Text", "Digest" }, "The username to set for all requests" );
158
159 setToolsSettingsAction( null );
160 buildArgsForm( builder, false, "TestRunner" );
161
162 return builder.buildDialog( buildDefaultActions( HelpUrls.TESTRUNNER_HELP_URL, modelItem ),
163 "Specify arguments for launching soapUI TestRunner", UISupport.TOOL_ICON );
164 }
165
166 protected Action createRunOption( WsdlProject modelItem )
167 {
168 Action action = super.createRunOption( modelItem );
169 action.putValue( Action.NAME, "Launch" );
170 return action;
171 }
172
173 protected StringToStringMap initValues( WsdlProject modelItem, Object param )
174 {
175 if( modelItem != null && mainForm != null )
176 {
177 List<String> endpoints = new ArrayList<String>();
178
179 for( Interface iface : modelItem.getInterfaceList() )
180 {
181 for( String endpoint : iface.getEndpoints() )
182 {
183 if( !endpoints.contains( endpoint ) )
184 endpoints.add( endpoint );
185 }
186 }
187
188 endpoints.add( 0, null );
189 advForm.setOptions( ENDPOINT, endpoints.toArray() );
190
191 testSuites = modelItem.getTestSuiteList();
192 for( int c = 0; c < testSuites.size(); c++ )
193 {
194 if( testSuites.get( c ).getTestCaseCount() == 0 )
195 {
196 testSuites.remove( c );
197 c-- ;
198 }
199 }
200
201 mainForm.setOptions( TESTSUITE, ModelSupport.getNames( new String[] { ALL_VALUE }, testSuites ) );
202
203 List<String> testCases = new ArrayList<String>();
204
205 for( TestSuite testSuite : testSuites )
206 {
207 for( TestCase testCase : testSuite.getTestCaseList() )
208 {
209 if( !testCases.contains( testCase.getName() ) )
210 testCases.add( testCase.getName() );
211 }
212 }
213
214 testCases.add( 0, ALL_VALUE );
215 mainForm.setOptions( TESTCASE, testCases.toArray() );
216 }
217 else if( mainForm != null )
218 {
219 mainForm.setOptions( ENDPOINT, new String[] { null } );
220 }
221
222 StringToStringMap values = super.initValues( modelItem, param );
223
224 if( mainForm != null )
225 {
226 if( param instanceof WsdlTestCase )
227 {
228 mainForm.getFormField( TESTSUITE ).setValue( ( ( WsdlTestCase )param ).getTestSuite().getName() );
229 mainForm.getFormField( TESTCASE ).setValue( ( ( WsdlTestCase )param ).getName() );
230
231 values.put( TESTSUITE, ( ( WsdlTestCase )param ).getTestSuite().getName() );
232 values.put( TESTCASE, ( ( WsdlTestCase )param ).getName() );
233 }
234 else if( param instanceof WsdlTestSuite )
235 {
236 mainForm.getFormField( TESTSUITE ).setValue( ( ( WsdlTestSuite )param ).getName() );
237 values.put( TESTSUITE, ( ( WsdlTestSuite )param ).getName() );
238 }
239
240 mainForm.getComponent( SAVEPROJECT ).setEnabled( !modelItem.isRemote() );
241 }
242
243 return values;
244 }
245
246 protected void generate( StringToStringMap values, ToolHost toolHost, WsdlProject modelItem ) throws Exception
247 {
248 String testRunnerDir = mainForm.getComponentValue( TESTRUNNERPATH );
249
250 ProcessBuilder builder = new ProcessBuilder();
251 ArgumentBuilder args = buildArgs( modelItem );
252 builder.command( args.getArgs() );
253 if( StringUtils.isNullOrEmpty( testRunnerDir ) )
254 builder.directory( new File( "." ) );
255 else
256 builder.directory( new File( testRunnerDir ) );
257
258 if( mainForm.getComponentValue( SAVEPROJECT ).equals( Boolean.TRUE.toString() ) )
259 {
260 modelItem.save();
261 }
262 else if( StringUtils.isNullOrEmpty( modelItem.getPath() ) )
263 {
264 UISupport.showErrorMessage( "Project [" + modelItem.getName() + "] has not been saved to file." );
265 return;
266 }
267
268 if( log.isDebugEnabled() )
269 log.debug( "Launching testrunner in directory [" + builder.directory() + "] with arguments ["
270 + args.toString() + "]" );
271
272 toolHost.run( new ProcessToolRunner( builder, "soapUI TestRunner", modelItem, args ) );
273 }
274
275 private ArgumentBuilder buildArgs( WsdlProject modelItem ) throws IOException
276 {
277 if( dialog == null )
278 {
279 ArgumentBuilder builder = new ArgumentBuilder( new StringToStringMap() );
280 builder.startScript( "testrunner", ".bat", ".sh" );
281 return builder;
282 }
283
284 StringToStringMap values = dialog.getValues();
285
286 ArgumentBuilder builder = new ArgumentBuilder( values );
287
288 builder.startScript( "testrunner", ".bat", ".sh" );
289
290 builder.addString( ENDPOINT, "-e", "" );
291 builder.addString( HOSTPORT, "-h", "" );
292
293 if( !values.get( TESTSUITE ).equals( ALL_VALUE ) )
294 builder.addString( TESTSUITE, "-s", "" );
295
296 if( !values.get( TESTCASE ).equals( ALL_VALUE ) )
297 builder.addString( TESTCASE, "-c", "" );
298
299 builder.addString( USERNAME, "-u", "" );
300 builder.addStringShadow( PASSWORD, "-p", "" );
301 builder.addString( DOMAIN, "-d", "" );
302 builder.addString( WSSTYPE, "-w", "" );
303
304 builder.addBoolean( PRINTREPORT, "-r" );
305 builder.addBoolean( EXPORTALL, "-a" );
306 builder.addBoolean( EXPORTJUNITRESULTS, "-j" );
307 builder.addString( ROOTFOLDER, "-f" );
308 builder.addBoolean( OPEN_REPORT, "-o" );
309 builder.addBoolean( COVERAGE, "-g" );
310
311 if( dialog.getBooleanValue( ADDSETTINGS ) )
312 {
313 try
314 {
315 builder.addBoolean( ADDSETTINGS, "-t" + SoapUI.saveSettings() );
316 }
317 catch( Exception e )
318 {
319 SoapUI.logError( e );
320 }
321 }
322
323 builder.addBoolean( ENABLEUI, "-i" );
324 builder.addArgs( new String[] { modelItem.getPath() } );
325
326 addToolArgs( values, builder );
327
328 return builder;
329 }
330 }