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