View Javadoc

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