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