View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.text.SimpleDateFormat;
17  import java.util.Date;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.apache.commons.cli.CommandLine;
23  import org.apache.commons.cli.CommandLineParser;
24  import org.apache.commons.cli.HelpFormatter;
25  import org.apache.commons.cli.Options;
26  import org.apache.commons.cli.PosixParser;
27  import org.apache.log4j.Logger;
28  
29  import com.eviware.soapui.SoapUI;
30  import com.eviware.soapui.impl.wsdl.WsdlProject;
31  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
32  import com.eviware.soapui.model.mock.MockResult;
33  import com.eviware.soapui.model.mock.MockRunListener;
34  import com.eviware.soapui.model.mock.MockRunner;
35  import com.eviware.soapui.model.mock.MockService;
36  
37  /***
38   * Standalone tool-runner used from maven-plugin, can also be used from command-line (see xdocs) or
39   * directly from other classes.
40   * <p>
41   * For standalone usage, set the project file (with setProjectFile) and other desired properties before
42   * calling run</p> 
43   * 
44   * @author Ole.Matzura
45   */
46  
47  public class SoapUIMockServiceRunner
48  {
49  	private final static Logger log = Logger.getLogger( SoapUIMockServiceRunner.class );
50  	
51  	private String projectFile;
52  	private String mockService;
53  	private String port;
54  	private String path;
55  	
56  	public static String TITLE = "soapUI " + SoapUI.SOAPUI_VERSION + " MockService Runner";
57  
58  	/***
59  	 * Runs the specified MockService in the specified soapUI project file, see soapUI xdocs for details.
60  	 * 
61  	 * @param args
62  	 * @throws Exception
63  	 */
64  
65  	@SuppressWarnings("static-access")
66  	public static void main( String [] args) throws Exception
67  	{
68  		System.out.println( TITLE );
69  		SoapUIMockServiceRunner runner = new SoapUIMockServiceRunner();
70  		
71  		Options options = new Options();
72  		options.addOption( "m", true, "Sets the MockService" );
73  		options.addOption( "p", true, "Sets the local port to listen on" );
74  		options.addOption( "a", true, "Sets the url path to listen on" );
75  		options.addOption( "s", false, "Sets the soapui-settings.xml file to use" );
76  		
77  		CommandLineParser parser = new PosixParser();
78  		CommandLine cmd = parser.parse( options, args);
79  		
80  		String[] args2 = cmd.getArgs();
81  		if( args2.length != 1 || !cmd.hasOption( "m") )
82  		{
83  			HelpFormatter formatter = new HelpFormatter();
84  			formatter.printHelp( "mockservicerunner [options] <soapui-project-file>", options );
85  
86  			System.err.println( "Missing soapUI project file and/or MockService.." );
87  			return;
88  		}
89  		
90  		runner.setProjectFile( args2[0] );
91  		runner.setMockService( cmd.getOptionValue( "m") );
92  		
93  		if( cmd.hasOption( "a"))
94  			runner.setPath( cmd.getOptionValue( "a" ) );
95  
96  		if( cmd.hasOption( "p"))
97  			runner.setPort( cmd.getOptionValue( "p" ) );
98  		
99  		if( cmd.hasOption( "s"))
100 			SoapUI.initSettings( cmd.getOptionValue( "s" ));
101 
102 		try
103 		{
104 			SoapUI.loadExtLibs();
105 			
106 			runner.run();
107 			System.exit( 0 );
108 		}
109 		catch (Exception e)
110 		{
111 			log.error( e.toString() );
112 			e.printStackTrace();
113 			System.exit( 1 );
114 		}		
115 	}
116 
117 	public void setProjectFile(String projectFile)
118 	{
119 		this.projectFile = projectFile;
120 	}
121 
122 	public void setMockService(String mockService)
123 	{
124 		this.mockService = mockService;
125 	}
126 
127 	public void setPath( String path )
128 	{
129 		this.path = path;
130 	}
131 
132 	public void setPort( String port )
133 	{
134 		this.port = port;
135 	}
136 
137 	public SoapUIMockServiceRunner()
138 	{
139 	}
140 	
141 	public void run() throws Exception
142 	{
143 		if( !new File( projectFile ).exists() )
144 			throw new Exception( "soapUI project file [" + projectFile + "] not found" );
145 		
146 		WsdlProject project = new WsdlProject( projectFile, null );
147 		log.info( "Running MockService [" + mockService + "] in project [" + project.getName() + "]" );
148 		log.info( "Press any key to terminate" );
149 		
150 		long startTime = System.nanoTime();
151 		
152 		for( int c = 0; c < project.getMockServiceCount(); c++ )
153 		{
154 			MockService ms = project.getMockServiceAt( c );
155 			if( ms.getName().equals( mockService ))
156 				runMockService( ( WsdlMockService ) ms );
157 		}
158 		
159 		long timeTaken = (System.nanoTime()-startTime)/1000000;
160 		log.info( "time taken: " + timeTaken + "ms" );
161 	}
162 	
163 	/***
164 	 * Runs the configured tool for the specified interface.. needs to be refactored to use
165 	 * some kind of registry/factory pattern for tools
166 	 * 
167 	 * @param iface
168 	 */
169 	
170 	public void runMockService( WsdlMockService mockService )
171 	{
172 		try
173 		{
174 			if( path != null )
175 				mockService.setPath( path );
176 			
177 			if( port != null )
178 				mockService.setPort( Integer.parseInt( port ));
179 			
180 			mockService.addMockRunListener( new LogListener() );
181 			MockRunner runner = mockService.start();
182 			
183 			System.in.read();
184 			runner.stop();
185 		}
186 		catch (Exception e)
187 		{
188 			e.printStackTrace();
189 		}
190 	}
191 
192 	public void log(String msg)
193 	{
194 		System.out.print( msg );
195 	}
196 
197    public void logError( String msg )
198    {
199       System.err.println( msg );
200    }
201 
202    public class LogListener implements MockRunListener
203 	{
204    	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
205 		private int responseCount;
206    	
207 		public void onMockRunnerStart( MockRunner mockRunner )
208 		{
209 			log.info( "MockService started on port " + mockRunner.getMockService().getPort() + " at path [" + 
210 						mockRunner.getMockService().getPath() + "]" );
211 		}
212 
213 		public void onMockRunnerStop( MockRunner mockRunner )
214 		{
215 			log.info( "MockService stopped, handled " + responseCount + " requests" );
216 		}
217 
218 		public void onMockResult( MockResult result )
219 		{
220 			responseCount++;
221 			log.info( "Handled request " + responseCount + "; [" + result.getMockResponse().getMockOperation().getName() + 
222 						"] with [" + result.getMockResponse().getName() + "] in [" + result.getTimeTaken() + 
223 						"ms] at [" + dateFormat.format( new Date( result.getTimestamp())) + "]" );
224 		}
225 
226 		public void onMockRequest( MockRunner runner, HttpServletRequest request, HttpServletResponse response )
227 		{
228 			// TODO Auto-generated method stub
229 			
230 		}
231 	}
232 }