View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.wsdl.WsdlProject;
17  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
18  import com.eviware.soapui.model.mock.MockResult;
19  import com.eviware.soapui.model.mock.MockRunner;
20  import com.eviware.soapui.model.mock.MockService;
21  import com.eviware.soapui.model.project.ProjectFactoryRegistry;
22  import com.eviware.soapui.model.support.MockRunListenerAdapter;
23  import org.apache.commons.cli.CommandLine;
24  
25  import java.text.SimpleDateFormat;
26  import java.util.ArrayList;
27  import java.util.Date;
28  import java.util.List;
29  
30  /***
31   * Standalone tool-runner used from maven-plugin, can also be used from command-line (see xdocs) or
32   * directly from other classes.
33   * <p>
34   * For standalone usage, set the project file (with setProjectFile) and other desired properties before
35   * calling run</p>
36   *
37   * @author Ole.Matzura
38   */
39  
40  public class SoapUIMockServiceRunner extends AbstractSoapUIRunner
41  {
42     private String mockService;
43     private String port;
44     private String path;
45     private List<MockRunner> runners = new ArrayList<MockRunner>();
46     private boolean block;
47     private String projectPassword;
48     private WsdlProject project;
49  
50     public static String TITLE = "soapUI " + SoapUI.SOAPUI_VERSION + " MockService Runner";
51  
52     /***
53      * Runs the specified MockService in the specified soapUI project file, see soapUI xdocs for details.
54      *
55      * @param args
56      * @throws Exception
57      */
58  
59     public static void main( String[] args ) throws Exception
60     {
61        new SoapUIMockServiceRunner().runFromCommandLine( args );
62     }
63  
64     public void setMockService( String mockService )
65     {
66        this.mockService = mockService;
67     }
68  
69     public void setPath( String path )
70     {
71        this.path = path;
72     }
73  
74     public void setPort( String port )
75     {
76        this.port = port;
77     }
78  
79     public SoapUIMockServiceRunner()
80     {
81        super( TITLE );
82     }
83  
84     public SoapUIMockServiceRunner( String title )
85     {
86        super( title );
87     }
88  
89     public boolean runRunner() throws Exception
90     {
91        initGroovyLog();
92  
93        String projectFile = getProjectFile();
94  
95  //		WsdlProject project = new WsdlProject( projectFile, getProjectPassword() );
96        WsdlProject project = (WsdlProject) ProjectFactoryRegistry.getProjectFactory( "wsdl" ).createNew( projectFile, getProjectPassword() );
97        if( project.isDisabled() )
98           throw new Exception( "Failed to load soapUI project file [" + projectFile + "]" );
99  
100       initProject();
101 
102       if( mockService == null )
103          log.info( "Running all MockServices in project [" + project.getName() + "]" );
104       else
105          log.info( "Running MockService [" + mockService + "] in project [" + project.getName() + "]" );
106 
107       log.info( "Press any key to terminate" );
108 
109       long startTime = System.nanoTime();
110 
111       for( int c = 0; c < project.getMockServiceCount(); c++ )
112       {
113          MockService ms = project.getMockServiceAt( c );
114          if( mockService == null || ms.getName().equals( mockService ) )
115             runMockService( (WsdlMockService) ms );
116       }
117 
118       log.info( "Started " + runners.size() + " runner" + ( ( runners.size() == 1 ) ? "" : "s" ) );
119 
120       if( block )
121       {
122          System.out.println( "Press any key to terminate..." );
123          System.in.read();
124          for( MockRunner runner : runners )
125             runner.stop();
126       }
127 
128       long timeTaken = ( System.nanoTime() - startTime ) / 1000000;
129       log.info( "time taken: " + timeTaken + "ms" );
130 
131       exportReports();
132 
133       return block;
134    }
135 
136    protected void initProject() throws Exception
137    {
138    }
139 
140    protected void exportReports() throws Exception
141    {
142    }
143 
144    /***
145     * Runs the specified MockService
146     *
147     * @param mockService
148     */
149 
150    public void runMockService( WsdlMockService mockService )
151    {
152       try
153       {
154          if( path != null )
155             mockService.setPath( path );
156 
157          if( port != null )
158             mockService.setPort( Integer.parseInt( port ) );
159 
160          mockService.addMockRunListener( new LogListener() );
161          runners.add( mockService.start() );
162       }
163       catch( Exception e )
164       {
165          SoapUI.logError( e );
166       }
167    }
168 
169    public class LogListener extends MockRunListenerAdapter
170    {
171       private SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS" );
172       private int responseCount;
173 
174       public void onMockRunnerStart( MockRunner mockRunner )
175       {
176          log.info( "MockService started on port " + mockRunner.getMockService().getPort() + " at path [" +
177                  mockRunner.getMockService().getPath() + "]" );
178       }
179 
180       public void onMockRunnerStop( MockRunner mockRunner )
181       {
182          log.info( "MockService stopped, handled " + responseCount + " requests" );
183       }
184 
185       public void onMockResult( MockResult result )
186       {
187          responseCount++;
188          log.info( "Handled request " + responseCount + "; [" + result.getMockResponse().getMockOperation().getName() +
189                  "] with [" + result.getMockResponse().getName() + "] in [" + result.getTimeTaken() +
190                  "ms] at [" + dateFormat.format( new Date( result.getTimestamp() ) ) + "]" );
191       }
192    }
193 
194    @Override
195    protected SoapUIOptions initCommandLineOptions()
196    {
197       SoapUIOptions options = new SoapUIOptions( "mockservicerunner" );
198       options.addOption( "m", true, "Specified the name of the MockService to run" );
199       options.addOption( "p", true, "Sets the local port to listen on" );
200       options.addOption( "a", true, "Sets the url path to listen on" );
201       options.addOption( "s", true, "Sets the soapui-settings.xml file to use" );
202       options.addOption( "b", false, "Turns off blocking read for termination" );
203       options.addOption( "x", true, "Sets project password for decryption if project is encrypted" );
204       options.addOption( "v", true, "Sets password for soapui-settings.xml file" );
205       options.addOption( "D", true, "Sets system property with name=value");
206       options.addOption( "G", true, "Sets global property with name=value" );
207       
208       return options;
209    }
210 
211    @Override
212    protected boolean processCommandLine( CommandLine cmd )
213    {
214       if( cmd.hasOption( "m" ) )
215          setMockService( getCommandLineOptionSubstSpace( cmd, "m" ) );
216 
217       if( cmd.hasOption( "a" ) )
218          setPath( getCommandLineOptionSubstSpace( cmd, "a" ) );
219 
220       if( cmd.hasOption( "p" ) )
221          setPort( cmd.getOptionValue( "p" ) );
222 
223       if( cmd.hasOption( "s" ) )
224          setSettingsFile( getCommandLineOptionSubstSpace( cmd, "s" ) );
225 
226       setBlock( !cmd.hasOption( 'b' ) );
227 
228       if( cmd.hasOption( "x" ) )
229       {
230          setProjectPassword( cmd.getOptionValue( "x" ) );
231       }
232 
233       if( cmd.hasOption( "v" ) )
234       {
235          setSoapUISettingsPassword( cmd.getOptionValue( "v" ) );
236       }
237 
238       if( cmd.hasOption( "D" ) )
239       {
240          setSystemProperties( cmd.getOptionValues( "D" ) );
241       }
242 
243       if( cmd.hasOption( "G" ) )
244       {
245          setGlobalProperties( cmd.getOptionValues( "G" ) );
246       }
247 
248       return true;
249    }
250 
251    public void setProjectPassword( String projectPassword )
252    {
253       this.projectPassword = projectPassword;
254    }
255 
256    public String getProjectPassword()
257    {
258       return projectPassword;
259    }
260 
261    public void setBlock( boolean block )
262    {
263       this.block = block;
264    }
265 
266    public WsdlProject getProject()
267    {
268       return project;
269 	   }
270 }