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 static Logger log;
    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  				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 void run() throws Exception
    112 	{
    113 		SoapUI.setSoapUICore( createSoapUICore() );
    114 		log = Logger.getLogger( this.getClass() );
    115
    116 		runRunner();
    117 	}
    118
    119 	protected SoapUICore createSoapUICore()
    120 	{
    121 		return enableUI ? new StandaloneSoapUICore( settingsFile ) : new DefaultSoapUICore( null, settingsFile );
    122 	}
    123
    124 	protected abstract boolean processCommandLine( CommandLine cmd );
    125
    126 	protected abstract SoapUIOptions initCommandLineOptions();
    127
    128 	protected abstract void runRunner() throws Exception;
    129
    130 	public String getProjectFile()
    131 	{
    132 		return projectFile;
    133 	}
    134
    135 	public String getSettingsFile()
    136 	{
    137 		return settingsFile;
    138 	}
    139
    140 	/***
    141 	 * Sets the soapUI project file containing the tests to run
    142 	 *
    143 	 * @param projectFile the soapUI project file containing the tests to run
    144 	 */
    145
    146 	public void setProjectFile(String projectFile)
    147 	{
    148 		this.projectFile = projectFile;
    149 	}
    150
    151
    152 	/***
    153 	 * Sets the soapUI settings file containing the tests to run
    154 	 *
    155 	 * @param settingsFile the soapUI settings file to use
    156 	 */
    157
    158 	public void setSettingsFile(String settingsFile)
    159 	{
    160 		this.settingsFile = settingsFile;
    161 	}
    162
    163 	public void setEnableUI( boolean enableUI )
    164 	{
    165 		this.enableUI = enableUI;
    166 	}
    167
    168 	public static class SoapUIOptions extends Options
    169 	{
    170 		private final String runnerName;
    171
    172 		public SoapUIOptions( String runnerName )
    173 		{
    174 			this.runnerName = runnerName;}
    175
    176 		public String getRunnerName()
    177 		{
    178 			return runnerName;
    179 		}
    180
    181 		public boolean requiresProject()
    182 		{
    183 			return true;
    184 		}
    185 	}
    186 }