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