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