1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.support;
14
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.InputStream;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Properties;
25 import java.util.Set;
26
27 import javax.xml.namespace.QName;
28
29 import org.apache.xmlbeans.XmlString;
30
31 import com.eviware.soapui.SoapUI;
32 import com.eviware.soapui.impl.wsdl.MutableTestPropertyHolder;
33 import com.eviware.soapui.impl.wsdl.support.wsdl.UrlWsdlLoader;
34 import com.eviware.soapui.model.ModelItem;
35 import com.eviware.soapui.model.testsuite.TestProperty;
36 import com.eviware.soapui.model.testsuite.TestPropertyListener;
37 import com.eviware.soapui.support.types.StringList;
38
39 public class MapTestPropertyHolder implements MutableTestPropertyHolder
40 {
41 private Map<String, TestProperty> propertyMap = new HashMap<String, TestProperty>();
42 private Set<TestPropertyListener> listeners = new HashSet<TestPropertyListener>();
43 private List<TestProperty> properties = new ArrayList<TestProperty>();
44 public ModelItem modelItem;
45 private String propertiesLabel = "Test Properties";
46
47 public MapTestPropertyHolder( ModelItem modelItem )
48 {
49 this.modelItem = modelItem;
50 }
51
52 private void firePropertyAdded( String name )
53 {
54 TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
55 for( TestPropertyListener listener : listenersArray )
56 {
57 listener.propertyAdded( name );
58 }
59 }
60
61 private void firePropertyRemoved( String name )
62 {
63 TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
64 for( TestPropertyListener listener : listenersArray )
65 {
66 listener.propertyRemoved( name );
67 }
68 }
69
70 private void firePropertyMoved( String name, int oldIndex, int newIndex )
71 {
72 TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
73 for( TestPropertyListener listener : listenersArray )
74 {
75 listener.propertyMoved( name, oldIndex, newIndex );
76 }
77 }
78
79 private void firePropertyRenamed( String oldName, String newName )
80 {
81 TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
82 for( TestPropertyListener listener : listenersArray )
83 {
84 listener.propertyRenamed( oldName, newName );
85 }
86 }
87
88 private void firePropertyValueChanged( String name, String oldValue, String newValue )
89 {
90 TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
91 for( TestPropertyListener listener : listenersArray )
92 {
93 listener.propertyValueChanged( name, oldValue, newValue );
94 }
95 }
96
97 public TestProperty addProperty( String name )
98 {
99 TestProperty result = new InternalTestProperty( name, null );
100 propertyMap.put( name.toUpperCase(), result );
101 properties.add( result );
102 firePropertyAdded( name );
103 return result;
104 }
105
106 public void addTestPropertyListener( TestPropertyListener listener )
107 {
108 listeners.add( listener );
109 }
110
111 public TestProperty getProperty( String name )
112 {
113 return propertyMap.get( name.toUpperCase() );
114 }
115
116 public String[] getPropertyNames()
117 {
118 StringList result = new StringList();
119 for( String name : propertyMap.keySet() )
120 result.add( propertyMap.get( name ).getName() );
121
122 return result.toStringArray();
123 }
124
125 public List<TestProperty> getPropertyList()
126 {
127 return Collections.unmodifiableList( properties );
128 }
129
130 public String getPropertyValue( String name )
131 {
132 TestProperty property = getProperty( name );
133 return property == null ? null : property.getValue();
134 }
135
136 public TestProperty removeProperty( String propertyName )
137 {
138 TestProperty property = getProperty( propertyName );
139 if( property != null )
140 {
141 properties.remove( property );
142 propertyMap.remove( propertyName.toUpperCase() );
143 firePropertyRemoved( propertyName );
144 }
145
146 return property;
147 }
148
149 public void removeTestPropertyListener( TestPropertyListener listener )
150 {
151 listeners.remove( listener );
152 }
153
154 public void setPropertyValue( String name, String value )
155 {
156 InternalTestProperty property = ( InternalTestProperty )getProperty( name );
157 if( property != null )
158 property.setValue( value );
159 }
160
161 public boolean renameProperty( String name, String newName )
162 {
163 if( getProperty( newName ) != null )
164 return false;
165
166 InternalTestProperty property = ( InternalTestProperty )getProperty( name );
167 if( property == null )
168 return false;
169
170 property.setName( newName );
171 return true;
172 }
173
174 /***
175 * Internal property class
176 *
177 * @author ole
178 */
179
180 public class InternalTestProperty implements TestProperty
181 {
182 private String name;
183 private String value;
184
185 public InternalTestProperty( String name, String value )
186 {
187 this.name = name;
188 this.value = value;
189 }
190
191 public String getName()
192 {
193 return name;
194 }
195
196 public void setName( String name )
197 {
198 String oldName = getName();
199
200 propertyMap.remove( oldName.toUpperCase() );
201 propertyMap.put( name.toUpperCase(), this );
202
203 this.name = name;
204
205 firePropertyRenamed( oldName, name );
206 }
207
208 public String getDescription()
209 {
210 return null;
211 }
212
213 public String getValue()
214 {
215 return value;
216 }
217
218 public void setValue( String value )
219 {
220 String oldValue = getValue();
221 this.value = value;
222
223 firePropertyValueChanged( getName(), oldValue, value );
224 }
225
226 public boolean isReadOnly()
227 {
228 return false;
229 }
230
231 public QName getType()
232 {
233 return XmlString.type.getName();
234 }
235
236 public ModelItem getModelItem()
237 {
238 return modelItem;
239 }
240
241 public String getDefaultValue()
242 {
243
244 return null;
245 }
246 }
247
248 public void saveTo( Properties props )
249 {
250 int cnt = 0;
251 for( TestProperty p : properties )
252 {
253 String name = p.getName();
254 String value = p.getValue();
255 if( value == null )
256 value = "";
257
258 props.setProperty( name, value );
259 cnt++ ;
260 }
261 }
262
263 public Map<String, TestProperty> getProperties()
264 {
265 Map<String, TestProperty> result = new HashMap<String, TestProperty>();
266 for( String name : propertyMap.keySet() )
267 result.put( name, propertyMap.get( name ) );
268
269 return result;
270 }
271
272 public boolean hasProperty( String name )
273 {
274 return propertyMap.containsKey( name.toUpperCase() );
275 }
276
277 public int addPropertiesFromFile( String propFile )
278 {
279 try
280 {
281 InputStream input = null;
282
283 File file = new File( propFile );
284 if( file.exists() )
285 {
286 input = new FileInputStream( file );
287 }
288 else if( propFile.toLowerCase().startsWith( "http://" ) || propFile.toLowerCase().startsWith( "https://" ) )
289 {
290 UrlWsdlLoader loader = new UrlWsdlLoader( propFile, getModelItem() );
291 loader.setUseWorker( false );
292 input = loader.load();
293 }
294
295 Properties properties = new Properties();
296 properties.load( input );
297
298 for( Object key : properties.keySet() )
299 {
300 String name = key.toString();
301 if( !hasProperty( name ) )
302 addProperty( name ).setValue( properties.getProperty( name ) );
303 else
304 setPropertyValue( name, properties.getProperty( name ) );
305 }
306
307 return properties.size();
308 }
309 catch( Exception e )
310 {
311 SoapUI.logError( e );
312 }
313
314 return 0;
315 }
316
317 public ModelItem getModelItem()
318 {
319 return modelItem;
320 }
321
322 public void moveProperty( String propertyName, int targetIndex )
323 {
324 TestProperty property = getProperty( propertyName );
325 int ix = properties.indexOf( property );
326
327 if( ix == targetIndex )
328 return;
329
330 if( targetIndex < 0 )
331 targetIndex = 0;
332
333 if( targetIndex < properties.size() )
334 properties.add( targetIndex, properties.remove( ix ) );
335 else
336 properties.add( properties.remove( ix ) );
337
338 if( targetIndex > properties.size() )
339 targetIndex = properties.size();
340
341 firePropertyMoved( propertyName, ix, targetIndex );
342 }
343
344 public TestProperty getPropertyAt( int index )
345 {
346 return properties.get( index );
347 }
348
349 public int getPropertyCount()
350 {
351 return properties.size();
352 }
353
354 public void setPropertiesLabel( String propertiesLabel )
355 {
356 this.propertiesLabel = propertiesLabel;
357 }
358
359 public String getPropertiesLabel()
360 {
361 return propertiesLabel;
362 }
363 }