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 import java.util.StringTokenizer;
21
22 import javax.swing.Action;
23
24 import org.apache.log4j.Logger;
25
26 import com.eviware.soapui.SoapUI;
27 import com.eviware.soapui.impl.wsdl.WsdlProject;
28 import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
29 import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
30 import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
31 import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
32 import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
33 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
34 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
35 import com.eviware.soapui.model.iface.Interface;
36 import com.eviware.soapui.model.support.ModelSupport;
37 import com.eviware.soapui.model.testsuite.TestCase;
38 import com.eviware.soapui.model.testsuite.TestSuite;
39 import com.eviware.soapui.support.StringUtils;
40 import com.eviware.soapui.support.UISupport;
41 import com.eviware.soapui.support.types.StringToStringMap;
42 import com.eviware.x.form.XForm;
43 import com.eviware.x.form.XFormDialog;
44 import com.eviware.x.form.XFormDialogBuilder;
45 import com.eviware.x.form.XFormFactory;
46 import com.eviware.x.form.XFormField;
47 import com.eviware.x.form.XFormFieldListener;
48 import com.eviware.x.impl.swing.JTextAreaFormField;
49
50 /***
51 * Invokes soapUI TestRunner tool
52 *
53 * @author Ole.Matzura
54 */
55
56 public class TestRunnerAction extends AbstractToolsAction<WsdlProject>
57 {
58 private static final String ALL_VALUE = "<all>";
59 private static final String ENDPOINT = "Endpoint";
60 private static final String HOSTPORT = "Host:Port";
61 private static final String TESTSUITE = "TestSuite";
62 private static final String TESTCASE = "TestCase";
63 private static final String USERNAME = "Username";
64 private static final String PASSWORD = "Password";
65 private static final String WSSTYPE = "WSS Password Type";
66 private static final String DOMAIN = "Domain";
67 private static final String PRINTREPORT = "Print Report";
68 private static final String ROOTFOLDER = "Root Folder";
69 private static final String EXPORTJUNITRESULTS = "Export JUnit Results";
70 private static final String EXPORTALL = "Export All";
71 private static final String ENABLEUI = "Enable UI";
72 private static final String TESTRUNNERPATH = "TestRunner Path";
73 private static final String SAVEPROJECT = "Save Project";
74 private static final String ADDSETTINGS = "Add Settings";
75 private static final String OPEN_REPORT = "Open Report";
76 private static final String COVERAGE = "Coverage Report";
77 private static final String PROJECTPASSWORD = "Project Password";
78 private static final String SOAPUISETTINGSPASSWORD = "soapui-setings.xml Password";
79 private static final String IGNOREERRORS = "Ignore Errors";
80 private static final String GENERATEREPORTSEACHTESTCASE = "Report to Generate";
81 private static final String REPORTFORMAT = "Report Format(s)";
82 private static final String SAVEAFTER = "Save After";
83 private static final String GLOBALPROPERTIES = "Global Properties";
84 private static final String SYSTEMPROPERTIES = "System Properties";
85 private static final String PROJECTPROPERTIES = "Project Properties";
86
87 private XForm mainForm;
88
89 private final static Logger log = Logger.getLogger( TestRunnerAction.class );
90
91 public static final String SOAPUI_ACTION_ID = "TestRunnerAction";
92
93 private XForm advForm;
94 private XForm propertyForm;
95 private XForm reportForm;
96
97 private List<TestSuite> testSuites;
98
99 private boolean proVersion;
100
101 public TestRunnerAction()
102 {
103 super( "Launch TestRunner", "Launch command-line TestRunner for this project" );
104 }
105
106 protected XFormDialog buildDialog( WsdlProject modelItem )
107 {
108 if( modelItem == null )
109 return null;
110
111 proVersion = isProVersion( modelItem );
112
113 XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Launch TestRunner" );
114
115 mainForm = builder.createForm( "Basic" );
116 mainForm.addComboBox( TESTSUITE, new String[] {}, "The TestSuite to run" ).addFormFieldListener(
117 new XFormFieldListener()
118 {
119
120 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
121 {
122 List<String> testCases = new ArrayList<String>();
123 String tc = mainForm.getComponentValue( TESTCASE );
124
125 if( newValue.equals( ALL_VALUE ) )
126 {
127 for( TestSuite testSuite : testSuites )
128 {
129 for( TestCase testCase : testSuite.getTestCaseList() )
130 {
131 if( !testCases.contains( testCase.getName() ) )
132 testCases.add( testCase.getName() );
133 }
134 }
135 }
136 else
137 {
138 TestSuite testSuite = getModelItem().getTestSuiteByName( newValue );
139 if( testSuite != null )
140 testCases.addAll( Arrays.asList( ModelSupport.getNames( testSuite.getTestCaseList() ) ) );
141 }
142
143 testCases.add( 0, ALL_VALUE );
144 mainForm.setOptions( TESTCASE, testCases.toArray() );
145
146 if( testCases.contains( tc ) )
147 {
148 mainForm.getFormField( TESTCASE ).setValue( tc );
149 }
150 }
151 } );
152
153 mainForm.addComboBox( TESTCASE, new String[] {}, "The TestCase to run" );
154 mainForm.addSeparator();
155
156 mainForm.addCheckBox( ENABLEUI, "Enables UI components in scripts" );
157 mainForm.addTextField( TESTRUNNERPATH, "Folder containing TestRunner.bat to use", XForm.FieldType.FOLDER );
158 mainForm.addCheckBox( SAVEPROJECT, "Saves project before running" ).setEnabled( !modelItem.isRemote() );
159 mainForm.addCheckBox( ADDSETTINGS, "Adds global settings to command-line" );
160 mainForm.addSeparator();
161 mainForm.addTextField( PROJECTPASSWORD, "Set project password", XForm.FieldType.PASSWORD );
162 mainForm.addTextField( SOAPUISETTINGSPASSWORD, "Set soapui-settings.xml password", XForm.FieldType.PASSWORD );
163 mainForm.addCheckBox( IGNOREERRORS, "Do not stop if error occurs, ignore them" );
164 mainForm.addCheckBox( SAVEAFTER, "Sets to save the project file after tests have been run" );
165
166 advForm = builder.createForm( "Overrides" );
167 advForm.addComboBox( ENDPOINT, new String[] { "" }, "endpoint to forward to" );
168 advForm.addTextField( HOSTPORT, "Host:Port to use for requests", XForm.FieldType.TEXT );
169 advForm.addSeparator();
170 advForm.addTextField( USERNAME, "The username to set for all requests", XForm.FieldType.TEXT );
171 advForm.addTextField( PASSWORD, "The password to set for all requests", XForm.FieldType.PASSWORD );
172 advForm.addTextField( DOMAIN, "The domain to set for all requests", XForm.FieldType.TEXT );
173 advForm.addComboBox( WSSTYPE, new String[] { "", "Text", "Digest" }, "The username to set for all requests" );
174
175 reportForm = builder.createForm( "Reports" );
176 reportForm.addCheckBox( PRINTREPORT, "Prints a summary report to the console" );
177 reportForm.addCheckBox( EXPORTJUNITRESULTS, "Exports results to a JUnit-Style report" );
178 reportForm.addCheckBox( EXPORTALL, "Exports all results (not only errors)" );
179 reportForm.addTextField( ROOTFOLDER, "Folder to export to", XForm.FieldType.FOLDER );
180 reportForm.addSeparator();
181 reportForm.addCheckBox( COVERAGE, "Generate WSDL Coverage report (soapUI Pro only)" ).setEnabled( proVersion );
182 reportForm.addCheckBox( OPEN_REPORT, "Open generated HTML report in browser (soapUI Pro only)" ).setEnabled(
183 proVersion );
184 reportForm.addTextField( GENERATEREPORTSEACHTESTCASE, "Report to Generate (soapUI Pro only)",
185 XForm.FieldType.TEXT ).setEnabled( proVersion );
186 reportForm.addTextField( REPORTFORMAT, "Choose report format(s), comma-separated (soapUI Pro only)",
187 XForm.FieldType.TEXT ).setEnabled( proVersion );
188
189 propertyForm = builder.createForm( "Properties" );
190 propertyForm.addComponent( GLOBALPROPERTIES, createTextArea() );
191 propertyForm.addComponent( SYSTEMPROPERTIES, createTextArea() );
192 propertyForm.addComponent( PROJECTPROPERTIES, createTextArea() );
193
194 setToolsSettingsAction( null );
195 buildArgsForm( builder, false, "TestRunner" );
196
197 return builder.buildDialog( buildDefaultActions( HelpUrls.TESTRUNNER_HELP_URL, modelItem ),
198 "Specify arguments for launching soapUI TestRunner", UISupport.TOOL_ICON );
199 }
200
201 private JTextAreaFormField createTextArea()
202 {
203 JTextAreaFormField textArea = new JTextAreaFormField();
204 textArea.setWidth( 40 );
205 textArea.getTextArea().setRows( 4 );
206 textArea.setToolTip( "name=value pairs separated by space or enter" );
207 return textArea;
208 }
209
210 /***
211 * check whether this is Pro or Core version
212 *
213 * @param modelItem
214 * @return boolean
215 */
216 private boolean isProVersion( WsdlProject modelItem )
217 {
218 if( modelItem.getClass().getName().contains( "WsdlProjectPro" ) )
219 {
220 return true;
221 }
222 return false;
223 }
224
225 protected Action createRunOption( WsdlProject modelItem )
226 {
227 Action action = super.createRunOption( modelItem );
228 action.putValue( Action.NAME, "Launch" );
229 return action;
230 }
231
232 protected StringToStringMap initValues( WsdlProject modelItem, Object param )
233 {
234 if( modelItem != null && mainForm != null )
235 {
236 List<String> endpoints = new ArrayList<String>();
237
238 for( Interface iface : modelItem.getInterfaceList() )
239 {
240 for( String endpoint : iface.getEndpoints() )
241 {
242 if( !endpoints.contains( endpoint ) )
243 endpoints.add( endpoint );
244 }
245 }
246
247 endpoints.add( 0, null );
248 advForm.setOptions( ENDPOINT, endpoints.toArray() );
249
250 testSuites = modelItem.getTestSuiteList();
251 for( int c = 0; c < testSuites.size(); c++ )
252 {
253 if( testSuites.get( c ).getTestCaseCount() == 0 )
254 {
255 testSuites.remove( c );
256 c-- ;
257 }
258 }
259
260 mainForm.setOptions( TESTSUITE, ModelSupport.getNames( new String[] { ALL_VALUE }, testSuites ) );
261
262 List<String> testCases = new ArrayList<String>();
263
264 for( TestSuite testSuite : testSuites )
265 {
266 for( TestCase testCase : testSuite.getTestCaseList() )
267 {
268 if( !testCases.contains( testCase.getName() ) )
269 testCases.add( testCase.getName() );
270 }
271 }
272
273 testCases.add( 0, ALL_VALUE );
274 mainForm.setOptions( TESTCASE, testCases.toArray() );
275 }
276 else if( mainForm != null )
277 {
278 mainForm.setOptions( ENDPOINT, new String[] { null } );
279 }
280
281 StringToStringMap values = super.initValues( modelItem, param );
282
283 if( mainForm != null )
284 {
285 if( param instanceof WsdlTestCase )
286 {
287 mainForm.getFormField( TESTSUITE ).setValue( ( ( WsdlTestCase )param ).getTestSuite().getName() );
288 mainForm.getFormField( TESTCASE ).setValue( ( ( WsdlTestCase )param ).getName() );
289
290 values.put( TESTSUITE, ( ( WsdlTestCase )param ).getTestSuite().getName() );
291 values.put( TESTCASE, ( ( WsdlTestCase )param ).getName() );
292 }
293 else if( param instanceof WsdlTestSuite )
294 {
295 mainForm.getFormField( TESTSUITE ).setValue( ( ( WsdlTestSuite )param ).getName() );
296 values.put( TESTSUITE, ( ( WsdlTestSuite )param ).getName() );
297 }
298
299 mainForm.getComponent( SAVEPROJECT ).setEnabled( !modelItem.isRemote() );
300 }
301
302 return values;
303 }
304
305 protected void generate( StringToStringMap values, ToolHost toolHost, WsdlProject modelItem ) throws Exception
306 {
307 String testRunnerDir = mainForm.getComponentValue( TESTRUNNERPATH );
308
309 ProcessBuilder builder = new ProcessBuilder();
310 ArgumentBuilder args = buildArgs( modelItem );
311 builder.command( args.getArgs() );
312 if( StringUtils.isNullOrEmpty( testRunnerDir ) )
313 builder.directory( new File( "." ) );
314 else
315 builder.directory( new File( testRunnerDir ) );
316
317 if( mainForm.getComponentValue( SAVEPROJECT ).equals( Boolean.TRUE.toString() ) )
318 {
319 modelItem.save();
320 }
321 else if( StringUtils.isNullOrEmpty( modelItem.getPath() ) )
322 {
323 UISupport.showErrorMessage( "Project [" + modelItem.getName() + "] has not been saved to file." );
324 return;
325 }
326
327 if( log.isDebugEnabled() )
328 log.debug( "Launching testrunner in directory [" + builder.directory() + "] with arguments ["
329 + args.toString() + "]" );
330
331 toolHost.run( new ProcessToolRunner( builder, "soapUI TestRunner", modelItem, args ) );
332 }
333
334 private ArgumentBuilder buildArgs( WsdlProject modelItem ) throws IOException
335 {
336 if( dialog == null )
337 {
338 ArgumentBuilder builder = new ArgumentBuilder( new StringToStringMap() );
339 builder.startScript( "testrunner", ".bat", ".sh" );
340 return builder;
341 }
342
343 StringToStringMap values = dialog.getValues();
344
345 ArgumentBuilder builder = new ArgumentBuilder( values );
346
347 builder.startScript( "testrunner", ".bat", ".sh" );
348
349 builder.addString( ENDPOINT, "-e", "" );
350 builder.addString( HOSTPORT, "-h", "" );
351
352 if( !values.get( TESTSUITE ).equals( ALL_VALUE ) )
353 builder.addString( TESTSUITE, "-s", "" );
354
355 if( !values.get( TESTCASE ).equals( ALL_VALUE ) )
356 builder.addString( TESTCASE, "-c", "" );
357
358 builder.addString( USERNAME, "-u", "" );
359 builder.addStringShadow( PASSWORD, "-p", "" );
360 builder.addString( DOMAIN, "-d", "" );
361 builder.addString( WSSTYPE, "-w", "" );
362
363 builder.addBoolean( PRINTREPORT, "-r" );
364 builder.addBoolean( EXPORTALL, "-a" );
365 builder.addBoolean( EXPORTJUNITRESULTS, "-j" );
366 builder.addString( ROOTFOLDER, "-f" );
367
368 if( proVersion )
369 {
370 builder.addBoolean( OPEN_REPORT, "-o" );
371 builder.addBoolean( COVERAGE, "-g" );
372 builder.addString( GENERATEREPORTSEACHTESTCASE, "-R", "" );
373 builder.addString( REPORTFORMAT, "-F", "" );
374 }
375
376 builder.addStringShadow( PROJECTPASSWORD, "-x", "" );
377 builder.addStringShadow( SOAPUISETTINGSPASSWORD, "-v", "" );
378 builder.addBoolean( IGNOREERRORS, "-I" );
379 builder.addBoolean( SAVEAFTER, "-S" );
380
381 addPropertyArguments( builder );
382
383 if( dialog.getBooleanValue( ADDSETTINGS ) )
384 {
385 try
386 {
387 builder.addBoolean( ADDSETTINGS, "-t" + SoapUI.saveSettings() );
388 }
389 catch( Exception e )
390 {
391 SoapUI.logError( e );
392 }
393 }
394
395 builder.addBoolean( ENABLEUI, "-i" );
396 builder.addArgs( new String[] { modelItem.getPath() } );
397
398 addToolArgs( values, builder );
399
400 return builder;
401 }
402
403 private void addPropertyArguments( ArgumentBuilder builder )
404 {
405 List<String> propertyArguments = new ArrayList<String>();
406
407 addProperties( propertyArguments, GLOBALPROPERTIES, "-G" );
408 addProperties( propertyArguments, SYSTEMPROPERTIES, "-D" );
409 addProperties( propertyArguments, PROJECTPROPERTIES, "-P" );
410
411 builder.addArgs( propertyArguments.toArray( new String[propertyArguments.size()] ) );
412 }
413
414 private void addProperties( List<String> propertyArguments, String propertiyDomain, String arg )
415 {
416 StringTokenizer tokenizer = new StringTokenizer( dialog.getValue( propertiyDomain ) );
417
418 while( tokenizer.hasMoreTokens() )
419 {
420 propertyArguments.add( arg + tokenizer.nextToken() );
421 }
422 }
423 }