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.Arrays;
19  import java.util.List;
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.WsdlTestSuite;
28  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
29  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
30  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
31  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
32  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
33  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
34  import com.eviware.soapui.model.iface.Interface;
35  import com.eviware.soapui.model.support.ModelSupport;
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  
48  /***
49   * Invokes soapUI TestRunner tool
50   * 
51   * @author Ole.Matzura
52   */
53  
54  public class TestRunnerAction extends AbstractToolsAction<WsdlProject>
55  {
56  	private static final String ALL_VALUE = "<all>";
57  	private static final String ENDPOINT = "Endpoint";
58  	private static final String HOSTPORT = "Host:Port";
59  	private static final String TESTSUITE = "TestSuite";
60  	private static final String TESTCASE = "TestCase";
61  	private static final String USERNAME = "Username";
62  	private static final String PASSWORD = "Password";
63  	private static final String WSSTYPE = "WSS Password Type";
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 EXPORTJUNITRESULTS = "Export JUnit Results";
68  	private static final String EXPORTALL = "Export All";
69  	private static final String ENABLEUI = "Enable UI";
70  	private static final String TESTRUNNERPATH = "TestRunner Path";
71  	private static final String SAVEPROJECT = "Save Project";
72  	private static final String ADDSETTINGS = "Add Settings";
73  	private static final String OPEN_REPORT = "Open Report";
74  	private static final String COVERAGE = "Coverage Report";
75  
76  	private XForm mainForm;
77  
78  	private final static Logger log = Logger.getLogger(TestRunnerAction.class);
79  
80  	public static final String SOAPUI_ACTION_ID = "TestRunnerAction";
81  
82  	private XForm advForm;
83  
84  	private List<TestSuite> testSuites;
85  
86  	public TestRunnerAction()
87  	{
88  		super("Launch TestRunner", "Launch command-line TestRunner for this project");
89  	}
90  
91  	protected XFormDialog buildDialog(WsdlProject modelItem)
92  	{
93  		if (modelItem == null)
94  			return null;
95  
96  		XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Launch TestRunner");
97  
98  		mainForm = builder.createForm("Basic");
99  		mainForm.addComboBox(TESTSUITE, new String[] {}, "The TestSuite to run").addFormFieldListener(
100 				new XFormFieldListener()
101 				{
102 
103 					public void valueChanged(XFormField sourceField, String newValue, String oldValue)
104 					{
105 						List<String> testCases = new ArrayList<String>();
106 						String tc = mainForm.getComponentValue(TESTCASE);
107 
108 						if (newValue.equals(ALL_VALUE))
109 						{
110 							for (TestSuite testSuite : testSuites)
111 							{
112 								for (TestCase testCase : testSuite.getTestCaseList())
113 								{
114 									if (!testCases.contains(testCase.getName()))
115 										testCases.add(testCase.getName());
116 								}
117 							}
118 						}
119 						else
120 						{
121 							TestSuite testSuite = getModelItem().getTestSuiteByName(newValue);
122 							if (testSuite != null)
123 								testCases.addAll(Arrays.asList(ModelSupport.getNames(testSuite.getTestCaseList())));
124 						}
125 
126 						testCases.add(0, ALL_VALUE);
127 						mainForm.setOptions(TESTCASE, testCases.toArray());
128 
129 						if (testCases.contains(tc))
130 						{
131 							mainForm.getFormField(TESTCASE).setValue(tc);
132 						}
133 					}
134 				});
135 
136 		mainForm.addComboBox(TESTCASE, new String[] {}, "The TestCase to run");
137 		mainForm.addSeparator();
138 		mainForm.addCheckBox(PRINTREPORT, "Prints a summary report to the console");
139 		mainForm.addCheckBox(EXPORTJUNITRESULTS, "Exports results to a JUnit-Style report");
140 		mainForm.addCheckBox(EXPORTALL, "Exports all results (not only errors)");
141 		mainForm.addTextField(ROOTFOLDER, "Folder to export to", XForm.FieldType.FOLDER);
142 		mainForm.addCheckBox(COVERAGE, "Generate WSDL Coverage report (soapUI Pro only)");
143 		mainForm.addCheckBox(OPEN_REPORT, "Open generated HTML report in browser (soapUI Pro only)");
144 		mainForm.addSeparator();
145 		mainForm.addCheckBox(ENABLEUI, "Enables UI components in scripts");
146 		mainForm.addTextField(TESTRUNNERPATH, "Folder containing TestRunner.bat to use", XForm.FieldType.FOLDER);
147 		mainForm.addCheckBox(SAVEPROJECT, "Saves project before running");
148 		mainForm.addCheckBox(ADDSETTINGS, "Adds global settings to command-line");
149 
150 		advForm = builder.createForm("Overrides");
151 		advForm.addComboBox(ENDPOINT, new String[] { "" }, "endpoint to forward to");
152 		advForm.addTextField(HOSTPORT, "Host:Port to use for requests", XForm.FieldType.TEXT);
153 		advForm.addSeparator();
154 		advForm.addTextField(USERNAME, "The username to set for all requests", XForm.FieldType.TEXT);
155 		advForm.addTextField(PASSWORD, "The password to set for all requests", XForm.FieldType.PASSWORD);
156 		advForm.addTextField(DOMAIN, "The domain to set for all requests", XForm.FieldType.TEXT);
157 		advForm.addComboBox(WSSTYPE, new String[] { "", "Text", "Digest" }, "The username to set for all requests");
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 TestRunner", UISupport.TOOL_ICON);
164 	}
165 
166 	protected Action createRunOption(WsdlProject modelItem)
167 	{
168 		Action action = super.createRunOption(modelItem);
169 		action.putValue(Action.NAME, "Launch");
170 		return action;
171 	}
172 
173 	protected StringToStringMap initValues(WsdlProject modelItem, Object param)
174 	{
175 		if (modelItem != null && mainForm != null)
176 		{
177 			List<String> endpoints = new ArrayList<String>();
178 
179 			for (Interface iface : modelItem.getInterfaceList())
180 			{
181 				for (String endpoint : iface.getEndpoints())
182 				{
183 					if (!endpoints.contains(endpoint))
184 						endpoints.add(endpoint);
185 				}
186 			}
187 
188 			endpoints.add(0, null);
189 			advForm.setOptions(ENDPOINT, endpoints.toArray());
190 
191 			testSuites = modelItem.getTestSuiteList();
192 			for (int c = 0; c < testSuites.size(); c++)
193 			{
194 				if (testSuites.get(c).getTestCaseCount() == 0)
195 				{
196 					testSuites.remove(c);
197 					c--;
198 				}
199 			}
200 
201 			mainForm.setOptions(TESTSUITE, ModelSupport.getNames(new String[] { ALL_VALUE }, testSuites));
202 
203 			List<String> testCases = new ArrayList<String>();
204 
205 			for (TestSuite testSuite : testSuites)
206 			{
207 				for (TestCase testCase : testSuite.getTestCaseList())
208 				{
209 					if (!testCases.contains(testCase.getName()))
210 						testCases.add(testCase.getName());
211 				}
212 			}
213 
214 			testCases.add(0, ALL_VALUE);
215 			mainForm.setOptions(TESTCASE, testCases.toArray());
216 		}
217 		else if (mainForm != null)
218 		{
219 			mainForm.setOptions(ENDPOINT, new String[] { null });
220 		}
221 
222 		StringToStringMap values = super.initValues(modelItem, param);
223 
224 		if (mainForm != null)
225 		{
226 			if (param instanceof WsdlTestCase)
227 			{
228 				mainForm.getFormField(TESTSUITE).setValue(((WsdlTestCase) param).getTestSuite().getName());
229 				mainForm.getFormField(TESTCASE).setValue(((WsdlTestCase) param).getName());
230 
231 				values.put(TESTSUITE, ((WsdlTestCase) param).getTestSuite().getName());
232 				values.put(TESTCASE, ((WsdlTestCase) param).getName());
233 			}
234 			else if (param instanceof WsdlTestSuite)
235 			{
236 				mainForm.getFormField(TESTSUITE).setValue(((WsdlTestSuite) param).getName());
237 				values.put(TESTSUITE, ((WsdlTestSuite) param).getName());
238 			}
239 		}
240 
241 		return values;
242 	}
243 
244 	protected void generate(StringToStringMap values, ToolHost toolHost, WsdlProject modelItem) throws Exception
245 	{
246 		String testRunnerDir = mainForm.getComponentValue(TESTRUNNERPATH);
247 
248 		ProcessBuilder builder = new ProcessBuilder();
249 		ArgumentBuilder args = buildArgs(modelItem);
250 		builder.command(args.getArgs());
251 		if (StringUtils.isNullOrEmpty(testRunnerDir))
252 			builder.directory(new File("."));
253 		else
254 			builder.directory(new File(testRunnerDir));
255 
256 		if (mainForm.getComponentValue(SAVEPROJECT).equals(Boolean.TRUE.toString()))
257 		{
258 			modelItem.save();
259 		}
260 
261 		if (log.isDebugEnabled())
262 			log.debug("Launching testrunner in directory [" + builder.directory() + "] with arguments [" + args.toString()
263 					+ "]");
264 
265 		toolHost.run(new ProcessToolRunner(builder, "soapUI TestRunner", modelItem, args));
266 	}
267 
268 	private ArgumentBuilder buildArgs(WsdlProject modelItem) throws IOException
269 	{
270 		if (dialog == null)
271 		{
272 			ArgumentBuilder builder = new ArgumentBuilder(new StringToStringMap());
273 			builder.startScript("testrunner", ".bat", ".sh");
274 			return builder;
275 		}
276 
277 		StringToStringMap values = dialog.getValues();
278 
279 		ArgumentBuilder builder = new ArgumentBuilder(values);
280 
281 		builder.startScript("testrunner", ".bat", ".sh");
282 
283 		builder.addString(ENDPOINT, "-e", "");
284 		builder.addString(HOSTPORT, "-h", "");
285 
286 		if (!values.get(TESTSUITE).equals(ALL_VALUE))
287 			builder.addString(TESTSUITE, "-s", "");
288 
289 		if (!values.get(TESTCASE).equals(ALL_VALUE))
290 			builder.addString(TESTCASE, "-c", "");
291 
292 		builder.addString(USERNAME, "-u", "");
293 		builder.addStringShadow(PASSWORD, "-p", "");
294 		builder.addString(DOMAIN, "-d", "");
295 		builder.addString(WSSTYPE, "-w", "");
296 
297 		builder.addBoolean(PRINTREPORT, "-r");
298 		builder.addBoolean(EXPORTALL, "-a");
299 		builder.addBoolean(EXPORTJUNITRESULTS, "-j");
300 		builder.addString(ROOTFOLDER, "-f");
301 		builder.addBoolean(OPEN_REPORT, "-o");
302 		builder.addBoolean(COVERAGE, "-g");
303 
304 		if (dialog.getBooleanValue(ADDSETTINGS))
305 		{
306 			try
307 			{
308 				builder.addBoolean(ADDSETTINGS, "-t" + SoapUI.saveSettings());
309 			}
310 			catch (Exception e)
311 			{
312 				SoapUI.logError(e);
313 			}
314 		}
315 
316 		builder.addBoolean(ENABLEUI, "-i");
317 		builder.addArgs(new String[] { modelItem.getPath() });
318 
319 		addToolArgs(values, builder);
320 
321 		return builder;
322 	}
323 }