View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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  
20  import javax.swing.Action;
21  
22  import org.apache.log4j.Logger;
23  
24  import com.eviware.soapui.SoapUI;
25  import com.eviware.soapui.impl.wsdl.WsdlProject;
26  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
27  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
28  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
29  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
30  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
31  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
32  import com.eviware.soapui.model.iface.Interface;
33  import com.eviware.soapui.model.support.ModelSupport;
34  import com.eviware.soapui.model.testsuite.LoadTest;
35  import com.eviware.soapui.model.testsuite.TestCase;
36  import com.eviware.soapui.model.testsuite.TestSuite;
37  import com.eviware.soapui.support.StringUtils;
38  import com.eviware.soapui.support.UISupport;
39  import com.eviware.soapui.support.types.StringToStringMap;
40  import com.eviware.x.form.XForm;
41  import com.eviware.x.form.XFormDialog;
42  import com.eviware.x.form.XFormDialogBuilder;
43  import com.eviware.x.form.XFormFactory;
44  import com.eviware.x.form.XFormField;
45  import com.eviware.x.form.XFormFieldListener;
46  
47  /***
48   * Invokes soapUI TestRunner tool
49   * 
50   * @author Ole.Matzura
51   */
52  
53  public class LoadTestRunnerAction extends AbstractToolsAction<WsdlProject>
54  {
55  	private static final String ALL_VALUE = "<all>";
56  	private static final String ENDPOINT = "Endpoint";
57  	private static final String HOSTPORT = "Host:Port";
58  	private static final String LIMIT = "Limit";
59  	private static final String TESTSUITE = "TestSuite";
60  	private static final String TESTCASE = "TestCase";
61  	private static final String LOADTEST = "LoadTest";
62  	private static final String THREADCOUNT = "ThreadCount";
63  	private static final String USERNAME = "Username";
64  	private static final String PASSWORD = "Password";
65  	private static final String DOMAIN = "Domain";
66  	private static final String PRINTREPORT = "Print Report";
67  	private static final String ROOTFOLDER = "Root Folder";
68  	private static final String TESTRUNNERPATH = "TestRunner Path";
69  	private static final String SAVEPROJECT = "Save Project";
70  	private static final String ADDSETTINGS = "Add Settings";
71  
72  	private XForm mainForm;
73  	private final static Logger log = Logger.getLogger( LoadTestRunnerAction.class );
74  	public static final String SOAPUI_ACTION_ID = "LoadTestRunnerAction";
75  	private XForm advForm;
76  	private boolean updating;
77  
78  	public LoadTestRunnerAction()
79  	{
80  		super( "Launch LoadTestRunner", "Launch command-line LoadTestRunner for this project" );
81  	}
82  
83  	protected XFormDialog buildDialog( WsdlProject modelItem )
84  	{
85  		if( modelItem == null )
86  			return null;
87  
88  		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Launch LoadTestRunner" );
89  
90  		mainForm = builder.createForm( "Basic" );
91  		mainForm.addComboBox( TESTSUITE, new String[] {}, "The TestSuite to run" ).addFormFieldListener(
92  				new XFormFieldListener()
93  				{
94  
95  					public void valueChanged( XFormField sourceField, String newValue, String oldValue )
96  					{
97  						updateCombos();
98  					}
99  				} );
100 
101 		mainForm.addComboBox( TESTCASE, new String[] {}, "The TestCase to run" ).addFormFieldListener(
102 				new XFormFieldListener()
103 				{
104 
105 					public void valueChanged( XFormField sourceField, String newValue, String oldValue )
106 					{
107 						updateCombos();
108 					}
109 				} );
110 		mainForm.addComboBox( LOADTEST, new String[] {}, "The LoadTest to run" );
111 		mainForm.addSeparator();
112 		mainForm.addCheckBox( PRINTREPORT, "Creates a report in the specified folder" );
113 		mainForm.addTextField( ROOTFOLDER, "Folder for reporting", XForm.FieldType.FOLDER );
114 		mainForm.addSeparator();
115 		mainForm.addTextField( TESTRUNNERPATH, "Folder containing TestRunner.bat to use", XForm.FieldType.FOLDER );
116 		mainForm.addCheckBox( SAVEPROJECT, "Saves project before running" ).setEnabled( !modelItem.isRemote() );
117 		;
118 		mainForm.addCheckBox( ADDSETTINGS, "Adds global settings to command-line" );
119 
120 		advForm = builder.createForm( "Overrides" );
121 		advForm.addComboBox( ENDPOINT, new String[] { "" }, "endpoint to forward to" );
122 		advForm.addTextField( HOSTPORT, "Host:Port to use for requests", XForm.FieldType.TEXT );
123 		advForm.addTextField( LIMIT, "Limit for LoadTest", XForm.FieldType.TEXT );
124 		advForm.addTextField( THREADCOUNT, "ThreadCount for LoadTest", XForm.FieldType.TEXT );
125 		advForm.addSeparator();
126 		advForm.addTextField( USERNAME, "The username to set for all requests", XForm.FieldType.TEXT );
127 		advForm.addTextField( PASSWORD, "The password to set for all requests", XForm.FieldType.PASSWORD );
128 		advForm.addTextField( DOMAIN, "The domain to set for all requests", XForm.FieldType.TEXT );
129 
130 		setToolsSettingsAction( null );
131 		buildArgsForm( builder, false, "TestRunner" );
132 
133 		return builder.buildDialog( buildDefaultActions( HelpUrls.TESTRUNNER_HELP_URL, modelItem ),
134 				"Specify arguments for launching soapUI LoadTestRunner", UISupport.TOOL_ICON );
135 	}
136 
137 	protected Action createRunOption( WsdlProject modelItem )
138 	{
139 		Action action = super.createRunOption( modelItem );
140 		action.putValue( Action.NAME, "Launch" );
141 		return action;
142 	}
143 
144 	protected StringToStringMap initValues( WsdlProject modelItem, Object param )
145 	{
146 		if( modelItem != null && mainForm != null )
147 		{
148 			List<String> endpoints = new ArrayList<String>();
149 
150 			for( Interface iface : modelItem.getInterfaceList() )
151 			{
152 				for( String endpoint : iface.getEndpoints() )
153 				{
154 					if( !endpoints.contains( endpoint ) )
155 						endpoints.add( endpoint );
156 				}
157 			}
158 
159 			endpoints.add( 0, null );
160 			advForm.setOptions( ENDPOINT, endpoints.toArray() );
161 			List<TestSuite> testSuites = modelItem.getTestSuiteList();
162 			for( int c = 0; c < testSuites.size(); c++ )
163 			{
164 				int cnt = 0;
165 
166 				for( TestCase testCase : testSuites.get( c ).getTestCaseList() )
167 				{
168 					cnt += testCase.getLoadTestCount();
169 				}
170 
171 				if( cnt == 0 )
172 				{
173 					testSuites.remove( c );
174 					c-- ;
175 				}
176 			}
177 
178 			mainForm.setOptions( TESTSUITE, ModelSupport.getNames( new String[] { ALL_VALUE }, testSuites ) );
179 		}
180 		else if( mainForm != null )
181 		{
182 			mainForm.setOptions( ENDPOINT, new String[] { null } );
183 		}
184 
185 		StringToStringMap values = super.initValues( modelItem, param );
186 		updateCombos();
187 
188 		if( mainForm != null && param instanceof WsdlLoadTest )
189 		{
190 			mainForm.getFormField( TESTSUITE ).setValue( ( ( WsdlLoadTest )param ).getTestCase().getTestSuite().getName() );
191 			mainForm.getFormField( TESTCASE ).setValue( ( ( WsdlLoadTest )param ).getTestCase().getName() );
192 			mainForm.getFormField( LOADTEST ).setValue( ( ( WsdlLoadTest )param ).getName() );
193 
194 			values.put( TESTSUITE, mainForm.getComponentValue( TESTSUITE ) );
195 			values.put( TESTCASE, mainForm.getComponentValue( TESTCASE ) );
196 			values.put( LOADTEST, mainForm.getComponentValue( LOADTEST ) );
197 
198 			mainForm.getComponent( SAVEPROJECT ).setEnabled( !modelItem.isRemote() );
199 		}
200 
201 		return values;
202 	}
203 
204 	protected void generate( StringToStringMap values, ToolHost toolHost, WsdlProject modelItem ) throws Exception
205 	{
206 		String testRunnerDir = mainForm.getComponentValue( TESTRUNNERPATH );
207 
208 		ProcessBuilder builder = new ProcessBuilder();
209 		ArgumentBuilder args = buildArgs( modelItem );
210 		builder.command( args.getArgs() );
211 		if( StringUtils.isNullOrEmpty( testRunnerDir ) )
212 			builder.directory( new File( "." ) );
213 		else
214 			builder.directory( new File( testRunnerDir ) );
215 
216 		if( mainForm.getComponentValue( SAVEPROJECT ).equals( Boolean.TRUE.toString() ) )
217 		{
218 			modelItem.save();
219 		}
220 		else if( StringUtils.isNullOrEmpty( modelItem.getPath() ) )
221 		{
222 			UISupport.showErrorMessage( "Project [" + modelItem.getName() + "] has not been saved to file." );
223 			return;
224 		}
225 
226 		if( log.isDebugEnabled() )
227 			log.debug( "Launching loadtestrunner in directory [" + builder.directory() + "] with arguments ["
228 					+ args.toString() + "]" );
229 
230 		toolHost.run( new ProcessToolRunner( builder, "soapUI LoadTestRunner", modelItem, args ) );
231 	}
232 
233 	private ArgumentBuilder buildArgs( WsdlProject modelItem ) throws IOException
234 	{
235 		if( dialog == null )
236 		{
237 			ArgumentBuilder builder = new ArgumentBuilder( new StringToStringMap() );
238 			builder.startScript( "loadtestrunner", ".bat", ".sh" );
239 			return builder;
240 		}
241 
242 		StringToStringMap values = dialog.getValues();
243 
244 		ArgumentBuilder builder = new ArgumentBuilder( values );
245 
246 		builder.startScript( "loadtestrunner", ".bat", ".sh" );
247 
248 		builder.addString( ENDPOINT, "-e", "" );
249 		builder.addString( HOSTPORT, "-h", "" );
250 
251 		if( !values.get( TESTSUITE ).equals( ALL_VALUE ) )
252 			builder.addString( TESTSUITE, "-s", "" );
253 
254 		if( !values.get( TESTCASE ).equals( ALL_VALUE ) )
255 			builder.addString( TESTCASE, "-c", "" );
256 
257 		if( !values.get( LOADTEST ).equals( ALL_VALUE ) )
258 			builder.addString( LOADTEST, "-l", "" );
259 
260 		builder.addString( LIMIT, "-m", "" );
261 		builder.addString( THREADCOUNT, "-h", "" );
262 		builder.addString( USERNAME, "-u", "" );
263 		builder.addStringShadow( PASSWORD, "-p", "" );
264 		builder.addString( DOMAIN, "-d", "" );
265 
266 		builder.addBoolean( PRINTREPORT, "-r" );
267 		builder.addString( ROOTFOLDER, "-f", "" );
268 
269 		if( dialog.getBooleanValue( ADDSETTINGS ) )
270 		{
271 			try
272 			{
273 				builder.addBoolean( ADDSETTINGS, "-t" + SoapUI.saveSettings() );
274 			}
275 			catch( Exception e )
276 			{
277 				SoapUI.logError( e );
278 			}
279 		}
280 
281 		builder.addArgs( new String[] { modelItem.getPath() } );
282 
283 		addToolArgs( values, builder );
284 
285 		return builder;
286 	}
287 
288 	private void updateCombos()
289 	{
290 		if( updating )
291 			return;
292 
293 		updating = true;
294 
295 		List<String> testCases = new ArrayList<String>();
296 		List<String> loadTests = new ArrayList<String>();
297 
298 		TestSuite ts = getModelItem().getTestSuiteByName( mainForm.getComponentValue( TESTSUITE ) );
299 		String testCaseName = mainForm.getComponentValue( TESTCASE );
300 		if( ALL_VALUE.equals( testCaseName ) )
301 			testCaseName = null;
302 
303 		for( TestSuite testSuite : getModelItem().getTestSuiteList() )
304 		{
305 			if( ts != null && testSuite != ts )
306 				continue;
307 
308 			for( TestCase testCase : testSuite.getTestCaseList() )
309 			{
310 				if( testCase.getLoadTestCount() == 0 )
311 					continue;
312 
313 				if( !testCases.contains( testCase.getName() ) )
314 					testCases.add( testCase.getName() );
315 
316 				if( testCaseName != null && !testCase.getName().equals( testCaseName ) )
317 					continue;
318 
319 				for( LoadTest loadTest : testCase.getLoadTestList() )
320 				{
321 					if( !loadTests.contains( loadTest.getName() ) )
322 						loadTests.add( loadTest.getName() );
323 				}
324 			}
325 		}
326 
327 		testCases.add( 0, ALL_VALUE );
328 		mainForm.setOptions( TESTCASE, testCases.toArray() );
329 
330 		loadTests.add( 0, ALL_VALUE );
331 		mainForm.setOptions( LOADTEST, loadTests.toArray() );
332 
333 		updating = false;
334 	}
335 }