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.iface.SubmitContext;
21  import com.eviware.soapui.model.testsuite.TestCase;
22  import com.eviware.soapui.model.testsuite.TestRunContext;
23  import com.eviware.soapui.model.testsuite.TestStep;
24  import com.eviware.soapui.model.testsuite.TestStepProperty;
25  import com.eviware.soapui.support.types.StringToObjectMap;
26  
27  /***
28   * Base-class for submit contexts
29   * 
30   * @author ole.matzura
31   */
32  
33  public abstract class AbstractSubmitContext implements SubmitContext, Map<String,Object>
34  {
35  	private StringToObjectMap properties;
36  	
37  	public AbstractSubmitContext()
38  	{
39  		setProperty( TestRunContext.RUN_COUNT, 0 );
40  		setProperty( TestRunContext.THREAD_INDEX, 0 );
41  	}
42  	
43  	public AbstractSubmitContext(PropertiesMap properties)
44  	{
45  		this();
46  		
47  		if( properties != null && properties.size() > 0 )
48  			this.properties.putAll( properties );
49  	}
50  
51  	public Object getProperty(String name, TestStep testStep, WsdlTestCase testCase )
52  	{
53  		if( properties != null && properties.containsKey( name ))
54  			return properties.get( name );
55  
56  		if( testCase != null )
57  		{
58  			int ix = name.indexOf( PROPERTY_SEPARATOR );
59  			if( ix > 0 )
60  			{
61  				String teststepname = name.substring(0, ix);
62  				TestStep refTestStep = testCase.getTestStepByName( teststepname );
63  				if( refTestStep != null )
64  				{
65  					TestStepProperty property = refTestStep.getProperty( name.substring(ix+1));
66  					return property == null ? null : property.getValue();
67  				}
68  			}
69  			
70  			if( testCase.getSearchProperties() )
71  			{
72  				ix = testStep == null ? testCase.getTestStepCount()-1 : testCase.getIndexOfTestStep( testStep );
73  				if( ix >= testCase.getTestStepCount() )
74  					ix = testCase.getTestStepCount()-1;
75  				
76  				while( ix >= 0 )
77  				{
78  					TestStepProperty property = testCase.getTestStepAt( ix ).getProperty( name );
79  					if( property != null )
80  						return property.getValue();
81  					
82  					ix--;
83  				}
84  			}
85  		}
86  		
87  		return null;
88  	}
89  	
90  	public Object removeProperty(String name)
91  	{
92  		return properties == null ? null : properties.remove( name );
93  	}
94  
95  	public void setProperty(String name, Object value)
96  	{
97  		if( properties == null )
98  			properties = new StringToObjectMap();
99  		
100 		properties.put( name, value );
101 	}
102 	
103 	public void setProperty(String name, Object value, TestCase testCase )
104 	{
105 		int ix = name.indexOf( PROPERTY_SEPARATOR );
106 		if( ix > 0 )
107 		{
108 			String teststepname = name.substring(0, ix);
109 			TestStep refTestStep = testCase.getTestStepByName( teststepname );
110 			if( refTestStep != null )
111 			{
112 				TestStepProperty property = refTestStep.getProperty( name.substring(ix+1));
113 				if( property != null && !property.isReadOnly() )
114 				{
115 					property.setValue( value.toString() );
116 					return;
117 				}
118 			}
119 		}
120 		
121 		if( properties == null )
122 			properties = new StringToObjectMap();
123 		
124 		properties.put( name, value );
125 	}
126 
127 	public boolean hasProperty(String name)
128 	{
129 		return properties == null ? false : properties.containsKey( name );
130 	}
131 	
132 	public void resetProperties()
133 	{
134 		if( properties != null )
135 			properties.clear();
136 	}
137 
138 	public void clear()
139 	{
140 		properties.clear();
141 	}
142 
143 	public Object clone()
144 	{
145 		return properties.clone();
146 	}
147 
148 	public boolean containsKey( Object key )
149 	{
150 		return properties.containsKey( key );
151 	}
152 
153 	public boolean containsValue( Object value )
154 	{
155 		return properties.containsValue( value );
156 	}
157 
158 	public Set<Entry<String, Object>> entrySet()
159 	{
160 		return properties.entrySet();
161 	}
162 
163 	public boolean equals( Object o )
164 	{
165 		return properties.equals( o );
166 	}
167 
168 	public Object get( Object key )
169 	{
170 		return properties.get( key );
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 key, Object value )
189 	{
190 		return properties.put( key, value );
191 	}
192 
193 	public void putAll( Map<? extends String, ? extends Object> m )
194 	{
195 		properties.putAll( m );
196 	}
197 
198 	public Object remove( Object key )
199 	{
200 		return properties.remove( key );
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 StringToObjectMap getProperties()
219 	{
220 		return properties;
221 	}
222 }