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