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.impl.wsdl.mock;
14  
15  import java.util.Collection;
16  import java.util.Map;
17  import java.util.Set;
18  
19  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
20  import com.eviware.soapui.model.ModelItem;
21  import com.eviware.soapui.model.mock.MockRunContext;
22  import com.eviware.soapui.model.mock.MockRunner;
23  import com.eviware.soapui.model.propertyexpansion.DefaultPropertyExpansionContext;
24  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
25  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
26  import com.eviware.soapui.model.settings.Settings;
27  import com.eviware.soapui.model.testsuite.TestCase;
28  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
29  import com.eviware.soapui.model.testsuite.TestCaseRunner;
30  import com.eviware.soapui.model.testsuite.TestProperty;
31  import com.eviware.soapui.model.testsuite.TestStep;
32  import com.eviware.soapui.support.types.StringToObjectMap;
33  import com.eviware.soapui.support.types.StringToStringMap;
34  
35  /***
36   * MockRunContext available during dispatching of a WsdlMockRequest
37   * 
38   * @author ole.matzura
39   */
40  
41  public class WsdlMockRunContext implements MockRunContext, Map<String, Object>, TestCaseRunContext
42  {
43  	private DefaultPropertyExpansionContext properties;
44  	private final WsdlMockService mockService;
45  	private final WsdlTestRunContext context;
46  	private WsdlMockResponse mockResponse;
47  
48  	public WsdlMockRunContext( WsdlMockService mockService, WsdlTestRunContext context )
49  	{
50  		this.mockService = mockService;
51  		this.context = context;
52  
53  		properties = ( DefaultPropertyExpansionContext )( context == null ? new DefaultPropertyExpansionContext(
54  				mockService ) : context.getProperties() );
55  	}
56  
57  	public WsdlMockService getMockService()
58  	{
59  		return mockService;
60  	}
61  
62  	public Object getProperty( String name )
63  	{
64  		return get( name );
65  	}
66  
67  	public synchronized boolean hasProperty( String name )
68  	{
69  		synchronized( properties )
70  	{
71  		return properties.containsKey( name );
72  	}
73  	}
74  
75  	public synchronized Object removeProperty( String name )
76  	{
77  		synchronized( properties )
78  	{
79  		return properties.remove( name );
80  	}
81  	}
82  
83  	public synchronized void setProperty( String name, Object value )
84  	{
85  		if( context != null )
86  		{
87  			int ix = name.indexOf( PropertyExpansion.PROPERTY_SEPARATOR );
88  			if( ix > 0 )
89  			{
90  				String teststepname = name.substring( 0, ix );
91  				TestStep refTestStep = context.getTestCase().getTestStepByName( teststepname );
92  				if( refTestStep != null )
93  				{
94  					TestProperty property = refTestStep.getProperty( name.substring( ix + 1 ) );
95  					if( property != null && !property.isReadOnly() )
96  					{
97  						property.setValue( value.toString() );
98  						return;
99  					}
100 				}
101 			}
102 		}
103 
104 		synchronized( properties )
105 		{
106 		properties.put( name, value );
107 	}
108 	}
109 
110 	public synchronized StringToStringMap toStringToStringMap()
111 	{
112 		synchronized( properties )
113 	{
114 		StringToStringMap result = new StringToStringMap();
115 
116 		for( String key : properties.keySet() )
117 		{
118 			Object value = properties.get( key );
119 			if( value != null )
120 				result.put( key, value.toString() );
121 		}
122 
123 		return result;
124 	}
125 	}
126 
127 	public synchronized void clear()
128 	{
129 		synchronized( properties )
130 	{
131 		properties.clear();
132 	}
133 	}
134 
135 	public synchronized Object clone()
136 	{
137 		synchronized( properties )
138 	{
139 		return properties.clone();
140 	}
141 	}
142 
143 	public synchronized boolean containsKey( Object arg0 )
144 	{
145 		synchronized( properties )
146 	{
147 		return properties.containsKey( arg0 );
148 	}
149 	}
150 
151 	public synchronized boolean containsValue( Object arg0 )
152 	{
153 		synchronized( properties )
154 	{
155 		return properties.containsValue( arg0 );
156 	}
157 	}
158 
159 	public synchronized Set<Entry<String, Object>> entrySet()
160 	{
161 		synchronized( properties )
162 	{
163 		return properties.entrySet();
164 	}
165 	}
166 
167 	public synchronized boolean equals( Object arg0 )
168 	{
169 		synchronized( properties )
170 	{
171 		return properties.equals( arg0 );
172 	}
173 	}
174 
175 	public synchronized Object get( Object arg0 )
176 	{
177 		if( "mockService".equals( arg0 ) )
178 			return getMockService();
179 
180 		if( "mockResponse".equals( arg0 ) )
181 			return getMockResponse();
182 
183 		if( "modelItem".equals( arg0 ) )
184 			return getModelItem();
185 
186 		if( "currentStep".equals( arg0 ) )
187 			return getCurrentStep();
188 
189 		if( "currentStepIndex".equals( arg0 ) )
190 			return getCurrentStepIndex();
191 
192 		if( "settings".equals( arg0 ) )
193 			return getSettings();
194 
195 		if( "testCase".equals( arg0 ) )
196 			return getTestCase();
197 
198 		if( "testRunner".equals( arg0 ) )
199 			return getTestRunner();
200 
201 		synchronized( properties )
202 		{
203 		return properties.get( arg0 );
204 	}
205 	}
206 
207 	public synchronized int hashCode()
208 	{
209 		synchronized( properties )
210 	{
211 		return properties.hashCode();
212 	}
213 	}
214 
215 	public synchronized boolean isEmpty()
216 	{
217 		synchronized( properties )
218 	{
219 		return properties.isEmpty();
220 	}
221 	}
222 
223 	public synchronized Set<String> keySet()
224 	{
225 		synchronized( properties )
226 	{
227 		return properties.keySet();
228 	}
229 	}
230 
231 	public synchronized Object put( String arg0, Object arg1 )
232 	{
233 		synchronized( properties )
234 	{
235 		return properties.put( arg0, arg1 );
236 	}
237 	}
238 
239 	public synchronized void putAll( Map<? extends String, ? extends Object> arg0 )
240 	{
241 		synchronized( arg0 )
242 		{
243 			synchronized( properties )
244 	{
245 		properties.putAll( arg0 );
246 	}
247 		}
248 	}
249 
250 	public synchronized Object remove( Object arg0 )
251 	{
252 		synchronized( properties )
253 	{
254 		return properties.remove( arg0 );
255 	}
256 	}
257 
258 	public synchronized int size()
259 	{
260 		synchronized( properties )
261 	{
262 		return properties.size();
263 	}
264 	}
265 
266 	public synchronized String toString()
267 	{
268 		synchronized( properties )
269 	{
270 		return properties.toString();
271 	}
272 	}
273 
274 	public synchronized Collection<Object> values()
275 	{
276 		synchronized( properties )
277 	{
278 		return properties.values();
279 	}
280 	}
281 
282 	public synchronized TestStep getCurrentStep()
283 	{
284 		return context == null ? null : context.getCurrentStep();
285 	}
286 
287 	public synchronized int getCurrentStepIndex()
288 	{
289 		return context == null ? -1 : context.getCurrentStepIndex();
290 	}
291 
292 	public synchronized Object getProperty( String testStep, String propertyName )
293 	{
294 		return context == null ? null : context.getProperty( testStep, propertyName );
295 	}
296 
297 	public synchronized TestCaseRunner getTestRunner()
298 	{
299 		return context == null ? null : context.getTestRunner();
300 	}
301 
302 	public synchronized TestCase getTestCase()
303 	{
304 		return context == null ? null : context.getTestCase();
305 	}
306 
307 	public synchronized Settings getSettings()
308 	{
309 		return context == null ? mockService.getSettings() : context.getTestCase().getSettings();
310 	}
311 
312 	public void setMockResponse( WsdlMockResponse mockResponse )
313 	{
314 		this.mockResponse = mockResponse;
315 	}
316 
317 	public WsdlMockResponse getMockResponse()
318 	{
319 		return mockResponse;
320 	}
321 
322 	public ModelItem getModelItem()
323 	{
324 		return mockService;
325 	}
326 
327 	public synchronized String expand( String content )
328 	{
329 		synchronized( properties )
330 	{
331 		return PropertyExpander.expandProperties( this, content );
332 	}
333 	}
334 
335 	public synchronized String[] getPropertyNames()
336 	{
337 		synchronized( properties )
338 	{
339 		return properties.keySet().toArray( new String[properties.size()] );
340 	}
341 	}
342 
343 	public synchronized StringToObjectMap getProperties()
344 	{
345 		synchronized( properties )
346 	{
347 		return properties;
348 	}
349 	}
350 
351 	public MockRunner getMockRunner()
352 	{
353 		return mockService.getMockRunner();
354 	}
355 }