View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.OutputStreamWriter;
16  
17  import org.apache.commons.cli.CommandLine;
18  import org.apache.commons.cli.CommandLineParser;
19  import org.apache.commons.cli.HelpFormatter;
20  import org.apache.commons.cli.Options;
21  import org.apache.commons.cli.PosixParser;
22  import org.apache.log4j.ConsoleAppender;
23  import org.apache.log4j.Logger;
24  import org.apache.log4j.PatternLayout;
25  
26  import com.eviware.soapui.DefaultSoapUICore;
27  import com.eviware.soapui.SoapUI;
28  import com.eviware.soapui.SoapUICore;
29  import com.eviware.soapui.StandaloneSoapUICore;
30  
31  public abstract class AbstractSoapUIRunner
32  {
33  	private boolean groovyLogInitialized;
34  	private String projectFile;
35  	protected final Logger log = Logger.getLogger( getClass() );
36  	private String settingsFile;
37  	private boolean enableUI;
38  
39  	public AbstractSoapUIRunner( String title )
40  	{
41  		if( title != null )
42  			System.out.println( title );
43  	}
44  
45  	protected void initGroovyLog()
46  	{
47  		if( !groovyLogInitialized )
48  		{
49  			Logger logger = Logger.getLogger( "groovy.log" );
50  			
51  			ConsoleAppender appender = new ConsoleAppender();
52  			appender.setWriter( new OutputStreamWriter( System.out ));
53  			appender.setLayout( new PatternLayout( "%d{ABSOLUTE} %-5p [%c{1}] %m%n") );
54  			logger.addAppender( appender);
55  			
56  			groovyLogInitialized = true;
57  		}
58  	}
59  	
60  	public void runFromCommandLine( String [] args )
61  	{
62  		try
63  		{
64  			if( initFromCommandLine( args, true ))
65  			{
66  				if( run() )
67  					System.exit( 0 );
68  			}
69  			else
70  			{
71  				System.exit( 1 );
72  			}
73  		}
74  		catch( Throwable e )
75  		{
76  			log.error( e );
77  			SoapUI.logError( e );
78  			System.exit( 1 );
79  		}
80  	}
81  	
82  	public boolean initFromCommandLine( String[] args, boolean printHelp ) throws Exception
83  	{
84  		SoapUIOptions options = initCommandLineOptions();
85  		
86  		CommandLineParser parser = new PosixParser();
87  		CommandLine cmd = parser.parse( options, args);
88  		
89  		if( options.requiresProject() )
90  		{
91  			args = cmd.getArgs();
92  			
93  			if( args.length != 1 )
94  			{
95  				if( printHelp )
96  				{
97  					HelpFormatter formatter = new HelpFormatter();
98  					formatter.printHelp( options.getRunnerName() + " [options] <soapui-project-file>", options );
99  				}
100 	
101 				System.err.println( "Missing soapUI project file.." );
102 				return false;
103 			}
104 			
105 			setProjectFile( args[0] );
106 		}
107 		
108 		return processCommandLine( cmd );
109 	}
110 
111 	public final boolean run() throws Exception
112 	{
113 		SoapUI.setSoapUICore( createSoapUICore() );
114 		return runRunner();
115 	}
116 	
117 	protected SoapUICore createSoapUICore()
118 	{
119 		return enableUI ? new StandaloneSoapUICore( settingsFile ) : new DefaultSoapUICore( null, settingsFile );
120 	}
121 
122 	protected abstract boolean processCommandLine( CommandLine cmd );
123 
124 	protected abstract SoapUIOptions initCommandLineOptions();
125 	
126 	protected abstract boolean runRunner() throws Exception;
127 
128 	protected String getCommandLineOptionSubstSpace( CommandLine cmd, String key )
129 	{
130 		return cmd.getOptionValue(key).replaceAll("%20", " ");
131 	}
132 	
133 	public String getProjectFile()
134 	{
135 		return projectFile;
136 	}
137 
138 	public String getSettingsFile()
139 	{
140 		return settingsFile;
141 	}
142 
143 	/***
144 	 * Sets the soapUI project file containing the tests to run
145 	 * 
146 	 * @param projectFile the soapUI project file containing the tests to run
147 	 */
148 	
149 	public void setProjectFile(String projectFile)
150 	{
151 		this.projectFile = projectFile;
152 	}
153 	
154 	
155 	/***
156 	 * Sets the soapUI settings file containing the tests to run
157 	 * 
158 	 * @param settingsFile the soapUI settings file to use
159 	 */
160 	
161 	public void setSettingsFile(String settingsFile)
162 	{
163 		this.settingsFile = settingsFile;
164 	}
165 	
166 	public void setEnableUI( boolean enableUI )
167 	{
168 		this.enableUI = enableUI;
169 	}
170 	
171 	public static class SoapUIOptions extends Options
172 	{
173 		private final String runnerName;
174 
175 		public SoapUIOptions( String runnerName )
176 		{
177 			this.runnerName = runnerName;}
178 
179 		public String getRunnerName()
180 		{
181 			return runnerName;
182 		}
183 		
184 		public boolean requiresProject()
185 		{
186 			return true;
187 		}
188 	}
189 }