View Javadoc

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