/
AbstractSoapUIRunner.java
444 lines (377 loc) · 14 KB
/
AbstractSoapUIRunner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* SoapUI, Copyright (C) 2004-2017 SmartBear Software
*
* Licensed under the EUPL, Version 1.1 or - as soon as they will be approved by the European Commission - subsequent
* versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under the Licence is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the Licence for the specific language governing permissions and limitations
* under the Licence.
*/
package com.eviware.soapui.tools;
import com.eviware.soapui.DefaultSoapUICore;
import com.eviware.soapui.SoapUI;
import com.eviware.soapui.SoapUICore;
import com.eviware.soapui.SoapUIExtensionClassLoader;
import com.eviware.soapui.SoapUIExtensionClassLoader.SoapUIClassLoaderState;
import com.eviware.soapui.StandaloneSoapUICore;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.impl.wsdl.submit.filters.GlobalHttpHeadersRequestFilter;
import com.eviware.soapui.impl.wsdl.support.PathUtils;
import com.eviware.soapui.model.ModelItem;
import com.eviware.soapui.model.project.Project;
import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
import com.eviware.soapui.support.StringUtils;
import com.eviware.soapui.support.UISupport;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import java.io.File;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class AbstractSoapUIRunner implements CmdLineRunner {
public static final int NORMAL_TERMINATION = 0;
public static final int ABNORMAL_TERMINATION = -1;
private boolean groovyLogInitialized;
private String projectFile;
protected final Logger log = Logger.getLogger(getClass());
private String settingsFile;
private String soapUISettingsPassword;
private String projectPassword;
private boolean enableUI;
private String outputFolder;
private String[] projectProperties;
private Map<String, String> runnerGlobalProperties = new HashMap<String, String>();
public AbstractSoapUIRunner(String title) {
if (title != null) {
System.out.println(title);
}
SoapUI.setCmdLineRunner(this);
}
protected void initGroovyLog() {
if (!groovyLogInitialized) {
ensureConsoleAppenderIsDefined(Logger.getLogger("groovy.log"));
groovyLogInitialized = true;
}
}
/**
* Ensure there is one (and only one) ConsoleAppender instance configured for <code>logger</code>.
*
* @param logger
*/
protected void ensureConsoleAppenderIsDefined(Logger logger) {
if (logger != null) {
// ensure there is a ConsoleAppender defined, adding one if necessary
for (Object appender : Collections.list(logger.getAllAppenders())) {
if (appender instanceof ConsoleAppender) {
return;
}
}
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.setWriter(new OutputStreamWriter(System.out));
consoleAppender.setLayout(new PatternLayout("%d{ABSOLUTE} %-5p [%c{1}] %m%n"));
logger.addAppender(consoleAppender);
}
}
/**
* Validates the command line arguments and runs the test runner if vaild
*
* @param args the commandline arguments to the runner
* @return status code to be used with System.exit()
* @see java.lang.System
*/
public int runFromCommandLine(String[] args) {
int results = ABNORMAL_TERMINATION;
if (validateCommandLineArgument(args)) {
results = run(args);
}
return results;
}
public boolean validateCommandLineArgument(String[] args) {
boolean commandLineArgumentsAreValid = false;
try {
commandLineArgumentsAreValid = initFromCommandLine(args, true);
} catch (Exception e) {
log.error(e);
SoapUI.logError(e);
}
return commandLineArgumentsAreValid;
}
/**
* Runs the testrunner
*
* @param args the command line arguments to be passed to the testrunner
* @return status code to be used with System.exit()
* @see java.lang.System
*/
public int run(String[] args) {
try {
if (run()) {
return NORMAL_TERMINATION;
}
} catch (Exception e) {
log.error(e);
SoapUI.logError(e);
}
return ABNORMAL_TERMINATION;
}
public boolean initFromCommandLine(String[] args, boolean printHelp) throws Exception {
SoapUIOptions options = initCommandLineOptions();
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse(options, args);
if (requiresProjectArgument(cmd)) {
args = cmd.getArgs();
if (args.length != 1) {
if (printHelp) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(options.getRunnerName() + " [options] <soapui-project-file>", options);
}
System.err.println("Missing SoapUI project file..");
return false;
}
setProjectFile(args[0]);
}
return processCommandLine(cmd);
}
/**
* Checks if the command line arguments require a project file
* @param cmd The command line
* @return true as default
*/
protected boolean requiresProjectArgument(CommandLine cmd) {
return true;
}
/**
* Main method to use for running the configured tests. Call after setting
* properties, etc as desired.
*
* @return true if execution should be blocked
* @throws Exception if an error or failure occurs during test execution
*/
public final boolean run() throws Exception {
if (SoapUI.getSoapUICore() == null) {
SoapUI.setSoapUICore(createSoapUICore(), true);
SoapUI.initGCTimer();
}
for (String name : runnerGlobalProperties.keySet()) {
PropertyExpansionUtils.getGlobalProperties().setPropertyValue(name, runnerGlobalProperties.get(name));
}
SoapUIClassLoaderState state = SoapUIExtensionClassLoader.ensure();
try {
return runRunner();
} finally {
state.restore();
}
}
protected SoapUICore createSoapUICore() {
if (enableUI) {
StandaloneSoapUICore core = new StandaloneSoapUICore(settingsFile);
log.info("Enabling UI Components");
core.prepareUI();
UISupport.setMainFrame(null);
return core;
} else {
return new DefaultSoapUICore(null, settingsFile, soapUISettingsPassword);
}
}
protected abstract boolean processCommandLine(CommandLine cmd);
protected abstract SoapUIOptions initCommandLineOptions();
protected abstract boolean runRunner() throws Exception;
protected String getCommandLineOptionSubstSpace(CommandLine cmd, String key) {
return cmd.getOptionValue(key).replaceAll("%20", " ");
}
/*
* (non-Javadoc)
*
* @see com.eviware.soapui.tools.CmdLineRunner#getProjectFile()
*/
@Override
public String getProjectFile() {
return projectFile;
}
/*
* (non-Javadoc)
*
* @see com.eviware.soapui.tools.CmdLineRunner#getSettingsFile()
*/
@Override
public String getSettingsFile() {
return settingsFile;
}
public void setOutputFolder(String outputFolder) {
this.outputFolder = outputFolder;
}
/*
* (non-Javadoc)
*
* @see com.eviware.soapui.tools.CmdLineRunner#getOutputFolder()
*/
@Override
public String getOutputFolder() {
return this.outputFolder;
}
public String getAbsoluteOutputFolder(ModelItem modelItem) {
String folder = PropertyExpander.expandProperties(modelItem, outputFolder);
if (StringUtils.isNullOrEmpty(folder)) {
folder = PathUtils.getExpandedResourceRoot(modelItem);
} else if (PathUtils.isRelativePath(folder)) {
folder = PathUtils.resolveResourcePath(folder, modelItem);
}
return folder;
}
public String getModelItemOutputFolder(ModelItem modelItem) {
List<ModelItem> chain = new ArrayList<ModelItem>();
ModelItem p = modelItem;
while (!(p instanceof Project)) {
chain.add(0, p);
p = p.getParent();
}
File dir = new File(getAbsoluteOutputFolder(modelItem));
dir.mkdir();
for (ModelItem item : chain) {
dir = new File(dir, StringUtils.createFileName(item.getName(), '-'));
dir.mkdir();
}
return dir.getAbsolutePath();
}
protected void ensureOutputFolder(ModelItem modelItem) {
ensureFolder(getAbsoluteOutputFolder(modelItem));
}
public void ensureFolder(String path) {
if (path == null) {
return;
}
File folder = new File(path);
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdirs();
}
}
/**
* Sets the SoapUI project file containing the tests to run
*
* @param projectFile the SoapUI project file containing the tests to run
*/
public void setProjectFile(String projectFile) {
this.projectFile = projectFile;
}
/**
* Sets the SoapUI settings file containing the tests to run
*
* @param settingsFile the SoapUI settings file to use
*/
public void setSettingsFile(String settingsFile) {
this.settingsFile = settingsFile;
}
public void setEnableUI(boolean enableUI) {
this.enableUI = enableUI;
}
public static class SoapUIOptions extends Options {
private final String runnerName;
public SoapUIOptions(String runnerName) {
this.runnerName = runnerName;
}
public String getRunnerName() {
return runnerName;
}
}
public String getSoapUISettingsPassword() {
return soapUISettingsPassword;
}
public void setSoapUISettingsPassword(String soapUISettingsPassword) {
this.soapUISettingsPassword = soapUISettingsPassword;
}
public void setSystemProperties(String[] optionValues) {
for (String option : optionValues) {
int ix = option.indexOf('=');
if (ix != -1) {
System.setProperty(option.substring(0, ix), option.substring(ix + 1));
}
}
}
public void setCustomHeaders( String[] optionValues )
{
for( String option : optionValues )
{
int ix = option.indexOf( '=' );
if( ix != -1 )
{
// not optimal - it would be nicer if the filter could access command-line options via some
// generic mechanism.
String name = option.substring(0, ix);
String value = option.substring(ix + 1);
log.info( "Adding global HTTP Header [" + name + "] = [" + value + "]");
GlobalHttpHeadersRequestFilter.addGlobalHeader(name, value);
}
}
}
public void setGlobalProperties(String[] optionValues) {
for (String option : optionValues) {
int ix = option.indexOf('=');
if (ix != -1) {
String name = option.substring(0, ix);
String value = option.substring(ix + 1);
log.info("Setting global property [" + name + "] to [" + value + "]");
// PropertyExpansionUtils.getGlobalProperties().setPropertyValue( name, value );
runnerGlobalProperties.put(name, value);
}
}
}
public void setProjectProperties(String[] projectProperties) {
this.projectProperties = projectProperties;
}
/*
* (non-Javadoc)
*
* @see com.eviware.soapui.tools.CmdLineRunner#getLog()
*/
@Override
public Logger getLog() {
return log;
}
/*
* (non-Javadoc)
*
* @see com.eviware.soapui.tools.CmdLineRunner#getProjectProperties()
*/
@Override
public String[] getProjectProperties() {
return projectProperties;
}
protected void initProjectProperties(WsdlProject project) {
if (projectProperties != null) {
for (String option : projectProperties) {
int ix = option.indexOf('=');
if (ix != -1) {
String name = option.substring(0, ix);
String value = option.substring(ix + 1);
log.info("Setting project property [" + name + "] to [" + value + "]");
project.setPropertyValue(name, value);
}
}
}
}
public boolean isEnableUI() {
return enableUI;
}
public String getProjectPassword() {
return projectPassword;
}
public void setProjectPassword(String projectPassword) {
this.projectPassword = projectPassword;
}
}