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.impl.wsdl.teststeps;
14  
15  import java.util.ArrayList;
16  import java.util.Collections;
17  import java.util.HashMap;
18  import java.util.HashSet;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import com.eviware.soapui.config.TestStepConfig;
24  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
25  import com.eviware.soapui.model.testsuite.TestProperty;
26  import com.eviware.soapui.model.testsuite.TestPropertyListener;
27  
28  /***
29   * Base class for WSDL TestCase test steps.
30   * 
31   * @author Ole.Matzura
32   */
33  
34  abstract public class WsdlTestStepWithProperties extends WsdlTestStep
35  {
36  	private Map<String, TestProperty> properties;
37  	private List<TestProperty> propertyList = new ArrayList<TestProperty>();
38  	private Map<String, Set<String>> normalizedPropertyNames = new HashMap<String, Set<String>>();
39  	private Set<TestPropertyListener> listeners = new HashSet<TestPropertyListener>();
40  
41  	protected WsdlTestStepWithProperties( WsdlTestCase testCase, TestStepConfig config, boolean hasEditor,
42  			boolean forLoadTest )
43  	{
44  		super( testCase, config, hasEditor, forLoadTest );
45  	}
46  
47  	public String[] getPropertyNames()
48  	{
49  		if( properties == null )
50  			return new String[0];
51  
52  		String[] result = new String[properties.size()];
53  		int ix = 0;
54  		for( TestProperty property : properties.values() )
55  			result[ix++ ] = property.getName();
56  
57  		return result;
58  	}
59  
60  	public TestProperty getProperty( String name )
61  	{
62  		return properties == null || name == null ? null : properties.get( getPropertyKeyName( name ) );
63  	}
64  
65  	public String getPropertyValue( String name )
66  	{
67  		if( properties == null )
68  			return null;
69  
70  		TestProperty testStepProperty = properties.get( getPropertyKeyName( name ) );
71  		return testStepProperty == null ? null : testStepProperty.getValue();
72  	}
73  
74  	public void setPropertyValue( String name, String value )
75  	{
76  		if( properties == null )
77  			return;
78  
79  		TestProperty testStepProperty = properties.get( getPropertyKeyName( name ) );
80  		if( testStepProperty != null )
81  		{
82  			testStepProperty.setValue( value );
83  		}
84  	}
85  
86  	protected void addProperty( TestProperty property )
87  	{
88  		addProperty( property, false );
89  	}
90  
91  	protected void addProperty( TestProperty property, boolean notify )
92  	{
93  		if( properties == null )
94  			properties = new HashMap<String, TestProperty>();
95  		
96  		String name = property.getName();
97  		String upper = name.toUpperCase();
98  		
99  		if( !normalizedPropertyNames.containsKey( upper ) )
100 			normalizedPropertyNames.put( upper, new HashSet<String>() );
101 		
102 		normalizedPropertyNames.get( upper ).add( name );
103 
104 		properties.put( name, property );
105 		propertyList.add( property );
106 
107 		if( notify )
108 		{
109 			firePropertyAdded( name );
110 		}
111 	}
112 	
113 	private String getPropertyKeyName(String name)
114 	{
115 		if(properties.containsKey( name ))
116 			return name;
117 		
118 		Set<String> props = normalizedPropertyNames.get( name.toUpperCase() );
119 		if(props != null && !props.isEmpty())
120 		{
121 			return props.iterator().next();
122 		}
123 		return name;
124 	}
125 
126 	protected TestProperty deleteProperty( String name, boolean notify )
127 	{
128 		if( properties != null )
129 		{
130 			name = getPropertyKeyName(name);
131 			TestProperty result = properties.remove( name );
132 			
133 			if( result != null )
134 			{
135 				normalizedPropertyNames.get( name.toUpperCase() ).remove( name );
136 				propertyList.remove( result );
137 
138 				if( notify )
139 					firePropertyRemoved( name );
140 
141 				return result;
142 			}
143 		}
144 
145 		return null;
146 	}
147 
148 	public void propertyRenamed( String oldName )
149 	{
150 		if( properties == null )
151 			return;
152 		
153 		oldName = getPropertyKeyName( oldName );
154 		String upper = oldName.toUpperCase();
155 
156 		TestProperty testStepProperty = properties.get( oldName );
157 		if( testStepProperty == null )
158 			return;
159 
160 		Set<String> props = normalizedPropertyNames.get( upper );
161 		properties.remove( oldName );		
162 		props.remove( oldName );
163 		String newName = testStepProperty.getName();
164 		properties.put( newName, testStepProperty );
165 		
166 		upper = newName.toUpperCase();
167 		if( !normalizedPropertyNames.containsKey( upper ) )
168 			normalizedPropertyNames.put( upper, new HashSet<String>() );
169 		normalizedPropertyNames.get( upper ).add( newName );
170 
171 		firePropertyRenamed( oldName, newName );
172 	}
173 
174 	public void addTestPropertyListener( TestPropertyListener listener )
175 	{
176 		listeners.add( listener );
177 	}
178 
179 	public void removeTestPropertyListener( TestPropertyListener listener )
180 	{
181 		listeners.remove( listener );
182 	}
183 
184 	protected void firePropertyAdded( String name )
185 	{
186 		TestPropertyListener[] array = listeners.toArray( new TestPropertyListener[listeners.size()] );
187 		for( TestPropertyListener listener : array )
188 		{
189 			listener.propertyAdded( name );
190 		}
191 	}
192 
193 	protected void firePropertyRemoved( String name )
194 	{
195 		TestPropertyListener[] array = listeners.toArray( new TestPropertyListener[listeners.size()] );
196 		for( TestPropertyListener listener : array )
197 		{
198 			listener.propertyRemoved( name );
199 		}
200 	}
201 
202 	protected void firePropertyRenamed( String oldName, String newName )
203 	{
204 		TestPropertyListener[] array = listeners.toArray( new TestPropertyListener[listeners.size()] );
205 		for( TestPropertyListener listener : array )
206 		{
207 			listener.propertyRenamed( oldName, newName );
208 		}
209 	}
210 
211 	public void firePropertyValueChanged( String name, String oldValue, String newValue )
212 	{
213 		if( oldValue == null && newValue == null )
214 			return;
215 
216 		if( oldValue != null && oldValue.equals( newValue ) )
217 			return;
218 
219 		if( newValue != null && newValue.equals( oldValue ) )
220 			return;
221 
222 		TestPropertyListener[] array = listeners.toArray( new TestPropertyListener[listeners.size()] );
223 		for( TestPropertyListener listener : array )
224 		{
225 			listener.propertyValueChanged( name, oldValue, newValue );
226 		}
227 	}
228 
229 	public Map<String, TestProperty> getProperties()
230 	{
231 		Map<String, TestProperty> result = new HashMap<String, TestProperty>();
232 
233 		if( properties != null )
234 		{
235 			for( String name : properties.keySet() )
236 				result.put( properties.get( name ).getName(), properties.get( name ) );
237 		}
238 
239 		return result;
240 	}
241 
242 	public boolean hasProperty( String name )
243 	{
244 		return properties != null && properties.containsKey( getPropertyKeyName( name ) );
245 	}
246 
247 	public boolean hasProperties()
248 	{
249 		return true;
250 	}
251 
252 	public TestProperty getPropertyAt( int index )
253 	{
254 		return propertyList.get( index );
255 	}
256 
257 	public int getPropertyCount()
258 	{
259 		return propertyList.size();
260 	}
261 
262 	public List<TestProperty> getPropertyList()
263 	{
264 		return Collections.unmodifiableList( propertyList );
265 	}
266 
267 	protected void firePropertyMoved( String name, int oldIndex, int newIndex )
268 	{
269 		TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
270 		for( TestPropertyListener listener : listenersArray )
271 		{
272 			listener.propertyMoved( name, oldIndex, newIndex );
273 		}
274 	}
275 
276 	public void moveProperty( String propertyName, int targetIndex )
277 	{
278 		TestProperty property = getProperty( propertyName );
279 		int ix = propertyList.indexOf( property );
280 
281 		if( ix == targetIndex )
282 			return;
283 
284 		if( targetIndex < 0 )
285 			targetIndex = 0;
286 
287 		if( targetIndex < properties.size() )
288 			propertyList.add( targetIndex, propertyList.remove( ix ) );
289 		else
290 			propertyList.add( propertyList.remove( ix ) );
291 
292 		if( targetIndex > properties.size() )
293 			targetIndex = properties.size();
294 
295 		firePropertyMoved( propertyName, ix, targetIndex );
296 
297 	}
298 
299 }