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