View Javadoc

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