View Javadoc

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