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