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