View Javadoc

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