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