View Javadoc

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