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