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.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  
24  import com.eviware.soapui.SoapUI;
25  import com.eviware.soapui.impl.wsdl.WsdlProject;
26  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
27  import com.eviware.soapui.model.mock.MockResult;
28  import com.eviware.soapui.model.mock.MockRunListener;
29  import com.eviware.soapui.model.mock.MockRunner;
30  import com.eviware.soapui.model.mock.MockService;
31  
32  /***
33   * Standalone tool-runner used from maven-plugin, can also be used from command-line (see xdocs) or
34   * directly from other classes.
35   * <p>
36   * For standalone usage, set the project file (with setProjectFile) and other desired properties before
37   * calling run</p> 
38   * 
39   * @author Ole.Matzura
40   */
41  
42  public class SoapUIMockServiceRunner extends AbstractSoapUIRunner
43  {
44  	private String mockService;
45  	private String port;
46  	private String path;
47  	
48  	public static String TITLE = "soapUI " + SoapUI.SOAPUI_VERSION + " MockService Runner";
49  
50  	/***
51  	 * Runs the specified MockService in the specified soapUI project file, see soapUI xdocs for details.
52  	 * 
53  	 * @param args
54  	 * @throws Exception
55  	 */
56  
57  	@SuppressWarnings("static-access")
58  	public static void main( String [] args) throws Exception
59  	{
60  		new SoapUIMockServiceRunner().runFromCommandLine( args );
61  	}
62  
63  	public void setMockService(String mockService)
64  	{
65  		this.mockService = mockService;
66  	}
67  
68  	public void setPath( String path )
69  	{
70  		this.path = path;
71  	}
72  
73  	public void setPort( String port )
74  	{
75  		this.port = port;
76  	}
77  
78  	public SoapUIMockServiceRunner()
79  	{
80  		super( TITLE );
81  	}
82  	
83  	public SoapUIMockServiceRunner( String title )
84  	{
85  		super( title );
86  	}
87  	
88  	public void run() throws Exception
89  	{
90  		String projectFile = getProjectFile();
91  		
92  		if( !new File( projectFile ).exists() )
93  			throw new Exception( "soapUI project file [" + projectFile + "] not found" );
94  		
95  		WsdlProject project = new WsdlProject( projectFile, null );
96  		log.info( "Running MockService [" + mockService + "] in project [" + project.getName() + "]" );
97  		log.info( "Press any key to terminate" );
98  		
99  		long startTime = System.nanoTime();
100 		
101 		for( int c = 0; c < project.getMockServiceCount(); c++ )
102 		{
103 			MockService ms = project.getMockServiceAt( c );
104 			if( ms.getName().equals( mockService ))
105 				runMockService( ( WsdlMockService ) ms );
106 		}
107 		
108 		long timeTaken = (System.nanoTime()-startTime)/1000000;
109 		log.info( "time taken: " + timeTaken + "ms" );
110 	}
111 	
112 	/***
113 	 * Runs the configured tool for the specified interface.. needs to be refactored to use
114 	 * some kind of registry/factory pattern for tools
115 	 * 
116 	 * @param iface
117 	 */
118 	
119 	public void runMockService( WsdlMockService mockService )
120 	{
121 		try
122 		{
123 			if( path != null )
124 				mockService.setPath( path );
125 			
126 			if( port != null )
127 				mockService.setPort( Integer.parseInt( port ));
128 			
129 			mockService.addMockRunListener( new LogListener() );
130 			MockRunner runner = mockService.start();
131 			
132 			System.in.read();
133 			runner.stop();
134 		}
135 		catch (Exception e)
136 		{
137 			SoapUI.logError( e );
138 		}
139 	}
140 
141    public class LogListener implements MockRunListener
142 	{
143    	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
144 		private int responseCount;
145    	
146 		public void onMockRunnerStart( MockRunner mockRunner )
147 		{
148 			log.info( "MockService started on port " + mockRunner.getMockService().getPort() + " at path [" + 
149 						mockRunner.getMockService().getPath() + "]" );
150 		}
151 
152 		public void onMockRunnerStop( MockRunner mockRunner )
153 		{
154 			log.info( "MockService stopped, handled " + responseCount + " requests" );
155 		}
156 
157 		public void onMockResult( MockResult result )
158 		{
159 			responseCount++;
160 			log.info( "Handled request " + responseCount + "; [" + result.getMockResponse().getMockOperation().getName() + 
161 						"] with [" + result.getMockResponse().getName() + "] in [" + result.getTimeTaken() + 
162 						"ms] at [" + dateFormat.format( new Date( result.getTimestamp())) + "]" );
163 		}
164 
165 		public void onMockRequest( MockRunner runner, HttpServletRequest request, HttpServletResponse response )
166 		{
167 		}
168 	}
169 
170 	@Override
171 	protected SoapUIOptions initCommandLineOptions()
172 	{
173 		SoapUIOptions options = new SoapUIOptions( "mockservicerunner" );
174 		options.addOption( "m", true, "Sets the MockService" );
175 		options.addOption( "p", true, "Sets the local port to listen on" );
176 		options.addOption( "a", true, "Sets the url path to listen on" );
177 		options.addOption( "s", false, "Sets the soapui-settings.xml file to use" );
178 		
179 		return options;
180 	}
181 
182 	@Override
183 	protected boolean processCommandLine( CommandLine cmd )
184 	{
185 		setMockService( cmd.getOptionValue( "m") );
186 		
187 		if( cmd.hasOption( "a"))
188 			setPath( cmd.getOptionValue( "a" ) );
189 
190 		if( cmd.hasOption( "p"))
191 			setPort( cmd.getOptionValue( "p" ) );
192 		
193 		if( cmd.hasOption( "s"))
194 			SoapUI.initSettings( cmd.getOptionValue( "s" ));
195 		
196 		return true;
197 	}
198 }