View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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( new XFormFieldListener() {
92  
93  			public void valueChanged( XFormField sourceField, String newValue, String oldValue )
94  			{
95  				updateCombos();
96  			}} );
97  		
98  		mainForm.addComboBox( TESTCASE, new String[] {}, "The TestCase to run" ).addFormFieldListener( new XFormFieldListener() {
99  
100 			public void valueChanged( XFormField sourceField, String newValue, String oldValue )
101 			{
102 				updateCombos();
103 			}} );
104 		mainForm.addComboBox( LOADTEST, new String[] {}, "The LoadTest to run" );
105 		mainForm.addSeparator();
106 		mainForm.addCheckBox( PRINTREPORT, "Creates a report in the specified folder" );
107 		mainForm.addTextField( ROOTFOLDER, "Folder for reporting", XForm.FieldType.FOLDER );
108 		mainForm.addSeparator();
109 		mainForm.addTextField( TESTRUNNERPATH, "Folder containing TestRunner.bat to use", XForm.FieldType.FOLDER );
110 		mainForm.addCheckBox( SAVEPROJECT, "Saves project before running" );
111 		mainForm.addCheckBox( ADDSETTINGS, "Adds global settings to command-line" );
112 		
113 		advForm = builder.createForm( "Overrides" );
114 		advForm.addComboBox( ENDPOINT, new String[] {""}, "endpoint to forward to" );
115 		advForm.addTextField( HOSTPORT, "Host:Port to use for requests", XForm.FieldType.TEXT );
116 		advForm.addTextField( LIMIT, "Limit for LoadTest", XForm.FieldType.TEXT );
117 		advForm.addTextField( THREADCOUNT, "ThreadCount for LoadTest", XForm.FieldType.TEXT );
118 		advForm.addSeparator();
119 		advForm.addTextField( USERNAME, "The username to set for all requests", XForm.FieldType.TEXT );
120 		advForm.addTextField( PASSWORD, "The password to set for all requests", XForm.FieldType.PASSWORD );
121 		advForm.addTextField( DOMAIN, "The domain to set for all requests", XForm.FieldType.TEXT );
122       
123 		setToolsSettingsAction( null );
124 		buildArgsForm( builder, false, "TestRunner" );
125 		
126 		return builder.buildDialog( buildDefaultActions(HelpUrls.TESTRUNNER_HELP_URL, modelItem),
127       		"Specify arguments for launching soapUI LoadTestRunner", UISupport.TOOL_ICON );
128 	}
129    
130 	protected Action createRunOption( WsdlProject modelItem)
131 	{
132 		Action action = super.createRunOption( modelItem );
133 		action.putValue( Action.NAME, "Launch" );
134 		return action;
135 	}
136 
137 	protected StringToStringMap initValues(WsdlProject modelItem, Object param)
138 	{
139 		if( modelItem != null && mainForm != null )
140 		{
141 			List<String> endpoints = new ArrayList<String>();
142 			
143 			for( Interface iface : modelItem.getInterfaceList())
144 			{
145 				for( String endpoint : iface.getEndpoints() )
146 				{
147 					if( !endpoints.contains( endpoint ))
148 						endpoints.add( endpoint );
149 				}
150 			}
151 			
152 			endpoints.add( 0, null );
153 			advForm.setOptions( ENDPOINT, endpoints.toArray() );
154 			List<TestSuite> testSuites = modelItem.getTestSuiteList();
155 			for( int c = 0; c < testSuites.size(); c++ )
156 			{
157 				int cnt = 0;
158 				
159 				for( TestCase testCase : testSuites.get(c).getTestCaseList() )
160 				{
161 					cnt += testCase.getLoadTestCount();
162 				}
163 				
164 				if( cnt == 0 )
165 				{
166 					testSuites.remove( c );
167 					c--;
168 				}
169 			}
170 			
171 			mainForm.setOptions( TESTSUITE, ModelSupport.getNames( new String[]{ALL_VALUE}, testSuites ) );
172 		}
173 		else if( mainForm != null )
174 		{
175 			mainForm.setOptions( ENDPOINT, new String[] { null } );
176 		}
177 		
178 		StringToStringMap values = super.initValues( modelItem,  param );
179 		updateCombos();
180 		
181 		if( mainForm != null && param instanceof WsdlLoadTest )
182 		{
183 			mainForm.getFormField( TESTSUITE ).setValue( ((WsdlLoadTest)param).getTestCase().getTestSuite().getName() );
184 			mainForm.getFormField( TESTCASE ).setValue( ((WsdlLoadTest)param).getTestCase().getName() );
185 			mainForm.getFormField( LOADTEST ).setValue( ((WsdlLoadTest)param).getName() );
186 			
187 			values.put( TESTSUITE, mainForm.getComponentValue( TESTSUITE ) );
188 			values.put( TESTCASE, mainForm.getComponentValue( TESTCASE ) );
189 			values.put( LOADTEST, mainForm.getComponentValue( LOADTEST ) );
190 		}
191 		
192 		return values;
193 	}
194 	
195 	protected void generate(StringToStringMap values, ToolHost toolHost, WsdlProject modelItem ) throws Exception
196 	{
197 		String testRunnerDir = mainForm.getComponentValue( TESTRUNNERPATH );
198 		
199 		ProcessBuilder builder = new ProcessBuilder();
200 		ArgumentBuilder args = buildArgs(modelItem);
201 		builder.command(args.getArgs());
202 		if( StringUtils.isNullOrEmpty( testRunnerDir))
203 			builder.directory(new File("."));
204 		else
205 			builder.directory(new File(testRunnerDir));
206 		
207 		if( mainForm.getComponentValue( SAVEPROJECT ).equals( Boolean.TRUE.toString() ))
208 		{
209 			modelItem.save();
210 		}
211 		
212 		if( log.isDebugEnabled() )
213 			log.debug( "Launching loadtestrunner in directory [" + builder.directory() + "] with arguments [" +
214 					args.toString() + "]" );
215 		
216 		toolHost.run( new ProcessToolRunner( builder, "soapUI LoadTestRunner", modelItem, args ));
217 	}
218 
219 	private ArgumentBuilder buildArgs(WsdlProject modelItem) throws IOException
220 	{
221 		if( dialog == null )
222 		{
223 			ArgumentBuilder builder = new ArgumentBuilder( new StringToStringMap() );
224 			builder.startScript( "loadtestrunner", ".bat", ".sh" );
225 			return builder;
226 		}
227 		
228 		StringToStringMap values = dialog.getValues();
229 		
230 		ArgumentBuilder builder = new ArgumentBuilder( values );
231 		
232 		builder.startScript( "loadtestrunner", ".bat", ".sh" );
233 
234 		builder.addString( ENDPOINT, "-e", "" );
235 		builder.addString( HOSTPORT, "-h", "" );
236 		
237 		if( !values.get( TESTSUITE ).equals( ALL_VALUE ))
238 			builder.addString( TESTSUITE, "-s", "" );
239 		
240 		if( !values.get( TESTCASE ).equals( ALL_VALUE ))
241 			builder.addString( TESTCASE, "-c", "" );
242 		
243 		if( !values.get( LOADTEST ).equals( ALL_VALUE ))
244 			builder.addString( LOADTEST, "-l", "" );
245 		
246 		builder.addString( LIMIT, "-m", "" );
247 		builder.addString( THREADCOUNT, "-h", "" );
248 		builder.addString( USERNAME, "-u", "" );
249 		builder.addStringShadow( PASSWORD, "-p", "" );
250 		builder.addString( DOMAIN, "-d", "" );
251 		
252 		builder.addBoolean( PRINTREPORT, "-r" );
253 		builder.addString( ROOTFOLDER, "-f", "" );
254 		
255 		if( dialog.getBooleanValue( ADDSETTINGS ))
256 		{
257 			try
258 			{
259 				builder.addBoolean( ADDSETTINGS, "-t" + SoapUI.saveSettings() );
260 			}
261 			catch( Exception e )
262 			{
263 				SoapUI.logError( e );
264 			}
265 		}
266 		
267 		builder.addArgs( new String [] {modelItem.getPath()} );
268 		
269 		addToolArgs( values, builder );
270 		
271 		return builder;
272 	}
273 
274 	private void updateCombos()
275 	{
276 		if( updating )
277 			return;
278 		
279 		updating = true;
280 		
281 		List<String> testCases = new ArrayList<String>();
282 		List<String> loadTests = new ArrayList<String>();
283 		
284 		TestSuite ts = getModelItem().getTestSuiteByName( mainForm.getComponentValue( TESTSUITE ) );
285 		String testCaseName = mainForm.getComponentValue( TESTCASE );
286 		if( ALL_VALUE.equals( testCaseName ))
287 			testCaseName = null;
288 		
289 		for( TestSuite	testSuite : getModelItem().getTestSuiteList() )
290 		{
291 			if( ts != null && testSuite != ts )
292 				continue;
293 			
294 			for( TestCase testCase : testSuite.getTestCaseList() )
295 			{
296 				if( testCase.getLoadTestCount() == 0 )
297 					continue;
298 				
299 				if( !testCases.contains( testCase.getName() ))
300 					testCases.add( testCase.getName()  );
301 				
302 				if( testCaseName != null && !testCase.getName().equals( testCaseName ))
303 					continue;
304 				
305 				for( LoadTest loadTest : testCase.getLoadTestList() )
306 				{
307 					if( !loadTests.contains( loadTest.getName() ))
308 						loadTests.add( loadTest.getName()  );
309 				}
310 			}
311 		}
312 		
313 		testCases.add( 0, ALL_VALUE );
314 		mainForm.setOptions( TESTCASE, testCases.toArray() );
315 		
316 		loadTests.add( 0, ALL_VALUE );
317 		mainForm.setOptions( LOADTEST, loadTests.toArray() );
318 		
319 		updating = false;
320 	}
321 }