View Javadoc

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