1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.tools;
14
15 import java.io.File;
16 import java.io.OutputStreamWriter;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.apache.commons.cli.CommandLine;
21 import org.apache.commons.cli.CommandLineParser;
22 import org.apache.commons.cli.HelpFormatter;
23 import org.apache.commons.cli.Options;
24 import org.apache.commons.cli.PosixParser;
25 import org.apache.log4j.ConsoleAppender;
26 import org.apache.log4j.Logger;
27 import org.apache.log4j.PatternLayout;
28
29 import com.eviware.soapui.DefaultSoapUICore;
30 import com.eviware.soapui.SoapUI;
31 import com.eviware.soapui.SoapUICore;
32 import com.eviware.soapui.StandaloneSoapUICore;
33 import com.eviware.soapui.impl.wsdl.WsdlProject;
34 import com.eviware.soapui.impl.wsdl.support.PathUtils;
35 import com.eviware.soapui.model.ModelItem;
36 import com.eviware.soapui.model.project.Project;
37 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
38 import com.eviware.soapui.support.StringUtils;
39 import com.eviware.soapui.support.UISupport;
40
41 public abstract class AbstractSoapUIRunner
42 {
43 private boolean groovyLogInitialized;
44 private String projectFile;
45 protected final Logger log = Logger.getLogger( getClass() );
46 private String settingsFile;
47 private String soapUISettingsPassword;
48
49 private boolean enableUI;
50 private String outputFolder;
51 private String[] projectProperties;
52
53 public AbstractSoapUIRunner( String title )
54 {
55 if( title != null )
56 System.out.println( title );
57 }
58
59 protected void initGroovyLog()
60 {
61 if( !groovyLogInitialized )
62 {
63 Logger logger = Logger.getLogger( "groovy.log" );
64
65 ConsoleAppender appender = new ConsoleAppender();
66 appender.setWriter( new OutputStreamWriter( System.out ) );
67 appender.setLayout( new PatternLayout( "%d{ABSOLUTE} %-5p [%c{1}] %m%n" ) );
68 logger.addAppender( appender );
69
70 groovyLogInitialized = true;
71 }
72 }
73
74 public int runFromCommandLine( String[] args )
75 {
76 try
77 {
78 if( initFromCommandLine( args, true ) )
79 {
80 if( run() )
81 {
82 return 0;
83 }
84 }
85 }
86 catch( Throwable e )
87 {
88 log.error( e );
89 SoapUI.logError( e );
90 }
91
92 return -1;
93 }
94
95 public boolean initFromCommandLine( String[] args, boolean printHelp ) throws Exception
96 {
97 SoapUIOptions options = initCommandLineOptions();
98
99 CommandLineParser parser = new PosixParser();
100 CommandLine cmd = parser.parse( options, args );
101
102 if( options.requiresProject() )
103 {
104 args = cmd.getArgs();
105
106 if( args.length != 1 )
107 {
108 if( printHelp )
109 {
110 HelpFormatter formatter = new HelpFormatter();
111 formatter.printHelp( options.getRunnerName() + " [options] <soapui-project-file>", options );
112 }
113
114 System.err.println( "Missing soapUI project file.." );
115 return false;
116 }
117
118 setProjectFile( args[0] );
119 }
120
121 return processCommandLine( cmd );
122 }
123
124 /***
125 * Main method to use for running the configured tests. Call after setting
126 * properties, etc as desired.
127 *
128 * @return true if execution should be blocked
129 * @throws Exception
130 * if an error or failure occurs during test execution
131 */
132
133 public final boolean run() throws Exception
134 {
135 SoapUI.setSoapUICore( createSoapUICore() );
136 SoapUI.initGCTimer();
137
138 return runRunner();
139 }
140
141 protected SoapUICore createSoapUICore()
142 {
143 if( enableUI )
144 {
145 StandaloneSoapUICore core = new StandaloneSoapUICore( settingsFile );
146 log.info( "Enabling UI Components" );
147 core.prepareUI();
148 UISupport.setMainFrame( null );
149 return core;
150 }
151 else
152 {
153 return new DefaultSoapUICore( null, settingsFile, soapUISettingsPassword );
154 }
155 }
156
157 protected abstract boolean processCommandLine( CommandLine cmd );
158
159 protected abstract SoapUIOptions initCommandLineOptions();
160
161 protected abstract boolean runRunner() throws Exception;
162
163 protected String getCommandLineOptionSubstSpace( CommandLine cmd, String key )
164 {
165 return cmd.getOptionValue( key ).replaceAll( "%20", " " );
166 }
167
168 public String getProjectFile()
169 {
170 return projectFile;
171 }
172
173 public String getSettingsFile()
174 {
175 return settingsFile;
176 }
177
178 public void setOutputFolder( String outputFolder )
179 {
180 this.outputFolder = outputFolder;
181 }
182
183 public String getOutputFolder()
184 {
185 return this.outputFolder;
186 }
187
188 public String getAbsoluteOutputFolder( ModelItem modelItem )
189 {
190 String folder = outputFolder;
191
192 if( StringUtils.isNullOrEmpty( folder ) )
193 {
194 folder = PathUtils.getExpandedResourceRoot( modelItem );
195 }
196 else if( PathUtils.isRelativePath( folder ) )
197 {
198 folder = PathUtils.resolveResourcePath( folder, modelItem );
199 }
200
201 return folder;
202 }
203
204 public String getModelItemOutputFolder( ModelItem modelItem )
205 {
206 List<ModelItem> chain = new ArrayList<ModelItem>();
207 ModelItem p = modelItem;
208
209 while( !( p instanceof Project ) )
210 {
211 chain.add( 0, p );
212 p = p.getParent();
213 }
214
215 File dir = new File( getAbsoluteOutputFolder( modelItem ) );
216 dir.mkdir();
217
218 for( ModelItem item : chain )
219 {
220 dir = new File( dir, StringUtils.createFileName( item.getName(), '-' ) );
221 dir.mkdir();
222 }
223
224 return dir.getAbsolutePath();
225 }
226
227 protected void ensureOutputFolder( ModelItem modelItem )
228 {
229 ensureFolder( getAbsoluteOutputFolder( modelItem ) );
230 }
231
232 public void ensureFolder( String path )
233 {
234 if( path == null )
235 return;
236
237 File folder = new File( path );
238 if( !folder.exists() || !folder.isDirectory() )
239 folder.mkdirs();
240 }
241
242 /***
243 * Sets the soapUI project file containing the tests to run
244 *
245 * @param projectFile
246 * the soapUI project file containing the tests to run
247 */
248
249 public void setProjectFile( String projectFile )
250 {
251 this.projectFile = projectFile;
252 }
253
254 /***
255 * Sets the soapUI settings file containing the tests to run
256 *
257 * @param settingsFile
258 * the soapUI settings file to use
259 */
260
261 public void setSettingsFile( String settingsFile )
262 {
263 this.settingsFile = settingsFile;
264 }
265
266 public void setEnableUI( boolean enableUI )
267 {
268 this.enableUI = enableUI;
269 }
270
271 public static class SoapUIOptions extends Options
272 {
273 private final String runnerName;
274
275 public SoapUIOptions( String runnerName )
276 {
277 this.runnerName = runnerName;
278 }
279
280 public String getRunnerName()
281 {
282 return runnerName;
283 }
284
285 public boolean requiresProject()
286 {
287 return true;
288 }
289 }
290
291 public String getSoapUISettingsPassword()
292 {
293 return soapUISettingsPassword;
294 }
295
296 public void setSoapUISettingsPassword( String soapUISettingsPassword )
297 {
298 this.soapUISettingsPassword = soapUISettingsPassword;
299 }
300
301 public void setSystemProperties( String[] optionValues )
302 {
303 for( String option : optionValues )
304 {
305 int ix = option.indexOf( '=' );
306 if( ix != -1 )
307 {
308 System.setProperty( option.substring( 0, ix ), option.substring( ix + 1 ) );
309 }
310 }
311 }
312
313 public void setGlobalProperties( String[] optionValues )
314 {
315 for( String option : optionValues )
316 {
317 int ix = option.indexOf( '=' );
318 if( ix != -1 )
319 {
320 String name = option.substring( 0, ix );
321 String value = option.substring( ix + 1 );
322 log.info( "Setting global property [" + name + "] to [" + value + "]" );
323 PropertyExpansionUtils.getGlobalProperties().setPropertyValue( name, value );
324 }
325 }
326 }
327
328 public void setProjectProperties( String[] projectProperties )
329 {
330 this.projectProperties = projectProperties;
331 }
332
333 protected void initProjectProperties( WsdlProject project )
334 {
335 if( projectProperties != null )
336 {
337 for( String option : projectProperties )
338 {
339 int ix = option.indexOf( '=' );
340 if( ix != -1 )
341 {
342 String name = option.substring( 0, ix );
343 String value = option.substring( ix + 1 );
344 log.info( "Setting project property [" + name + "] to [" + value + "]" );
345 project.setPropertyValue( name, value );
346 }
347 }
348 }
349 }
350
351 protected boolean isEnableUI()
352 {
353 return enableUI;
354 }
355 }