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.impl.support.AbstractHttpRequest;
16  import com.eviware.soapui.impl.support.http.HttpRequestTestStep;
17  import com.eviware.soapui.impl.wsdl.WsdlRequest;
18  import com.eviware.soapui.impl.wsdl.teststeps.WsdlRunTestCaseTestStep;
19  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
20  import com.eviware.soapui.model.testsuite.*;
21  import com.eviware.soapui.support.StringUtils;
22  import com.eviware.soapui.support.Tools;
23  
24  public abstract class AbstractSoapUITestRunner extends AbstractSoapUIRunner implements TestRunListener
25  {
26     private String endpoint;
27     private String domain;
28     private String password;
29     private String username;
30     private String host;
31     private String wssPasswordType;
32     private String projectPassword;
33  
34     public AbstractSoapUITestRunner( String title )
35     {
36        super( title );
37     }
38  
39     public void setProjectPassword( String projectPassword )
40     {
41        this.projectPassword = projectPassword;
42     }
43  
44     /***
45      * Sets the host to use by all test-requests, the existing endpoint port and path will be used
46      *
47      * @param host the host to use by all requests
48      */
49  
50     public void setHost( String host )
51     {
52        this.host = host;
53     }
54  
55     /***
56      * Sets the domain to use for any authentications
57      *
58      * @param domain the domain to use for any authentications
59      */
60  
61     public void setDomain( String domain )
62     {
63        this.domain = domain;
64     }
65  
66     /***
67      * Sets the password to use for any authentications
68      *
69      * @param password the password to use for any authentications
70      */
71  
72     public void setPassword( String password )
73     {
74        this.password = password;
75     }
76  
77     /***
78      * Sets the WSS password-type to use for any authentications. Setting this will result
79      * in the addition of WS-Security UsernamePassword tokens to any outgoing request containing
80      * the specified username and password.
81      *
82      * @param wssPasswordType the wss-password type to use, either 'Text' or 'Digest'
83      */
84  
85     public void setWssPasswordType( String wssPasswordType )
86     {
87        this.wssPasswordType = wssPasswordType;
88     }
89  
90     /***
91      * Sets the username to use for any authentications
92      *
93      * @param username the username to use for any authentications
94      */
95  
96     public void setUsername( String username )
97     {
98        this.username = username;
99     }
100 
101    public String getProjectPassword()
102    {
103       return projectPassword;
104    }
105 
106    /***
107     * Sets the endpoint to use for all test requests
108     *
109     * @param endpoint the endpoint to use for all test requests
110     */
111 
112    public void setEndpoint( String endpoint )
113    {
114       this.endpoint = endpoint.trim();
115    }
116 
117    public String getEndpoint()
118    {
119       return endpoint;
120    }
121 
122    public String getDomain()
123    {
124       return domain;
125    }
126 
127    public String getPassword()
128    {
129       return password;
130    }
131 
132    public String getUsername()
133    {
134       return username;
135    }
136 
137    public String getHost()
138    {
139       return host;
140    }
141 
142    public String getWssPasswordType()
143    {
144       return wssPasswordType;
145    }
146 
147    protected void prepareRequestStep( HttpRequestTestStep requestStep )
148    {
149       AbstractHttpRequest httpRequest = requestStep.getHttpRequest();
150       if( StringUtils.hasContent( endpoint ) )
151       {
152          httpRequest.setEndpoint( endpoint );
153       }
154       else if( StringUtils.hasContent( host ) )
155       {
156          try
157          {
158             String ep = Tools.replaceHost( httpRequest.getEndpoint(), host );
159             httpRequest.setEndpoint( ep );
160          }
161          catch( Exception e )
162          {
163             log.error( "Failed to set host on endpoint", e );
164          }
165       }
166 
167       if( StringUtils.hasContent( username ) )
168       {
169          httpRequest.setUsername( username );
170       }
171 
172       if(StringUtils.hasContent( password ) )
173       {
174          httpRequest.setPassword( password );
175       }
176 
177       if( StringUtils.hasContent( domain ) )
178       {
179          httpRequest.setDomain( domain );
180       }
181 
182       if( httpRequest instanceof WsdlRequest )
183       {
184 
185          if( wssPasswordType != null && wssPasswordType.length() > 0 )
186          {
187             ( (WsdlRequest) httpRequest ).setWssPasswordType(
188                     wssPasswordType.equals( "Digest" ) ? WsdlTestRequest.PW_TYPE_DIGEST : WsdlTestRequest.PW_TYPE_TEXT );
189          }
190       }
191    }
192 
193    public void beforeRun( TestRunner testRunner, TestRunContext runContext )
194    {
195    }
196 
197    public void beforeStep( TestRunner testRunner, TestRunContext runContext )
198    {
199       TestStep currentStep = runContext.getCurrentStep();
200       if( currentStep instanceof HttpRequestTestStep )
201       {
202          prepareRequestStep( (HttpRequestTestStep) currentStep );
203       }
204       else if( currentStep instanceof WsdlRunTestCaseTestStep )
205       {
206          ( (WsdlRunTestCaseTestStep) currentStep ).addTestRunListener( this );
207       }
208    }
209 
210    public void afterStep( TestRunner testRunner, TestRunContext runContext, TestStepResult result )
211    {
212       TestStep currentStep = runContext.getCurrentStep();
213       if( currentStep instanceof WsdlRunTestCaseTestStep )
214       {
215          ( (WsdlRunTestCaseTestStep) currentStep ).removeTestRunListener( this );
216       }
217    }
218 
219    public void afterRun( TestRunner testRunner, TestRunContext runContext )
220    {
221    }
222 
223   
224 }