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