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