View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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 java.io.FileNotFoundException;
16  import java.io.FileOutputStream;
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Enumeration;
24  import java.util.List;
25  
26  import javax.swing.ImageIcon;
27  
28  import com.eviware.soapui.config.PropertiesStepConfig;
29  import com.eviware.soapui.config.PropertyConfig;
30  import com.eviware.soapui.config.TestStepConfig;
31  import com.eviware.soapui.config.PropertiesStepConfig.Properties;
32  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
33  import com.eviware.soapui.model.testsuite.TestRunContext;
34  import com.eviware.soapui.model.testsuite.TestRunner;
35  import com.eviware.soapui.model.testsuite.TestStep;
36  import com.eviware.soapui.model.testsuite.TestStepProperty;
37  import com.eviware.soapui.model.testsuite.TestStepResult;
38  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
39  import com.eviware.soapui.support.UISupport;
40  import com.eviware.soapui.support.types.StringList;
41  
42  /***
43   * TestStep that holds an arbitrary number of custom properties
44   * 
45   * @author ole.matzura
46   */
47  
48  public class WsdlPropertiesTestStep extends WsdlTestStep
49  {
50  	private PropertiesStepConfig propertiesStepConfig;
51  	private List<StepProperty> properties = new ArrayList<StepProperty>();
52  	private ImageIcon okIcon;
53  	private ImageIcon failedIcon;
54  
55  	public WsdlPropertiesTestStep(WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest)
56  	{
57  		super(testCase, config, true, forLoadTest);
58  		
59  		if( !forLoadTest )
60  		{
61  		   okIcon = UISupport.createImageIcon("/properties_step.gif");
62  		   failedIcon = UISupport.createImageIcon("/properties_step_failed.gif");
63  		   
64  			setIcon( okIcon );
65  		}
66  
67  	   if( config.getConfig() == null )
68  		{
69  			propertiesStepConfig = (PropertiesStepConfig) config.addNewConfig().changeType( PropertiesStepConfig.type );
70  			propertiesStepConfig.addNewProperties();
71  			propertiesStepConfig.setCreateMissingOnLoad( true );
72  		}
73  		else
74  		{
75  			propertiesStepConfig = (PropertiesStepConfig) config.getConfig().changeType(PropertiesStepConfig.type);
76  			if( propertiesStepConfig.isSetProperties() )
77  			{
78  				Properties props = propertiesStepConfig.getProperties();
79  				for( int c = 0; c < props.sizeOfPropertyArray(); c++ )
80  				{
81  					StepProperty stepProperty = new StepProperty( props.getPropertyArray( c ));
82  					properties.add( stepProperty );
83  					addProperty( stepProperty );
84  				}
85  			}
86  			else
87  			{	
88  				propertiesStepConfig.addNewProperties();
89  			}
90  			
91  			if( !propertiesStepConfig.isSetSaveFirst() )
92  				propertiesStepConfig.setSaveFirst( true );
93  		}
94  	}
95  	
96  	public TestStepResult run(TestRunner testRunner, TestRunContext testRunContext)
97  	{
98  		WsdlTestStepResult result = new WsdlTestStepResult( this );
99  		
100 		if( okIcon != null )
101 			setIcon( okIcon );
102 		
103 		result.setStatus( TestStepStatus.OK );
104 		result.startTimer();
105 		
106 		if( isSaveFirst() ) 
107 			saveDuringRun( result );
108 		
109 		String source = getSource();
110 		if( source != null && source.trim().length() > 0  )
111 		{
112 			try
113 			{
114 				int cnt = loadProperties(source,isCreateMissingOnLoad());
115 				
116 				result.setStatus( TestStepStatus.OK );
117 				result.addMessage( "Loaded " + cnt + " properties from [" + source + "]" );
118 			}
119 			catch (IOException e)
120 			{
121 				result.stopTimer();
122 				result.addMessage( "Failed to load properties from [" + source + "]" );
123 				result.setStatus( TestStepStatus.FAILED );
124 				result.setError( e );
125 				
126 				if( failedIcon != null )
127 					setIcon( failedIcon );
128 			}			
129 		}
130 		
131 		if( !isSaveFirst() )
132 			saveDuringRun( result );
133 		
134 		result.stopTimer();
135 
136 		return result;
137 	}
138 
139 	private boolean saveDuringRun( WsdlTestStepResult result )
140 	{
141 		String target = getTarget();
142 		if( target != null && target.trim().length() > 0 )
143 		{
144 			try
145 			{
146 				int cnt = saveProperties(target);
147 				
148 				result.setStatus( TestStepStatus.OK );
149 				result.addMessage( "Saved " + cnt + " properties to [" + target + "]" );
150 			}
151 			catch (IOException e)
152 			{
153 				result.stopTimer();
154 				result.addMessage( "Failed to save properties to [" + target + "]" );
155 				result.setStatus( TestStepStatus.FAILED );
156 				result.setError( e );
157 				
158 				if( failedIcon != null )
159 					setIcon( failedIcon );
160 				
161 				return false;
162 			}			
163 		}
164 		
165 		return true;
166 	}
167 
168 	private int saveProperties( String target ) throws IOException
169 	{
170 		java.util.Properties props = new java.util.Properties();
171 		
172 		int cnt = 0;
173 		for( StepProperty p : properties )
174 		{
175 			String name = p.getName();
176 			String value = p.getValue();
177 			if( value == null )
178 				value = "";
179 			
180 			props.setProperty( name, value );
181 			cnt++;
182 		}
183 		
184 		props.store(getPropertiesOutputStream(target), "TestStep [" + getName() + "] properties");
185 
186 		return cnt;
187 	}
188 
189 	private FileOutputStream getPropertiesOutputStream(String target) throws FileNotFoundException
190 	{
191 		String fileProperty = System.getProperty( target );
192 		if( fileProperty != null )
193 			target = fileProperty;
194 		
195 		return new FileOutputStream(target);
196 	}
197 	
198 	private int loadProperties(String source, boolean createMissing) throws IOException
199 	{
200 		// override methods so propertynames are returned in readorder
201 		java.util.Properties props = new java.util.Properties()
202 		{
203 			public StringList names = new StringList();
204 
205 			@Override
206 			public synchronized Object put( Object key, Object value )
207 			{
208 				names.add( key.toString() );
209 				return super.put( key, value );
210 			}
211 
212 			@Override
213 			public Enumeration<?> propertyNames()
214 			{
215 				return Collections.enumeration( names );
216 			}
217 		};
218 		
219 		props.load(getPropertiesInputStream(source));
220 		
221 		int cnt = 0;
222 		Enumeration names = props.propertyNames();
223 		while( names.hasMoreElements() )
224 		{
225 			String name = names.nextElement().toString();
226 			TestStepProperty property = getProperty( name );
227 			if( property != null )
228 			{
229 				property.setValue( props.get( name ).toString() );
230 				cnt++;
231 			}
232 			else if( createMissing )
233 			{
234 				addProperty( name ).setValue( props.get( name ).toString() );
235 				cnt++;
236 			}
237 		}
238 		
239 		return cnt;
240 	}
241 
242 	private InputStream getPropertiesInputStream(String source) throws IOException
243 	{
244 		String fileProperty = System.getProperty( source );
245 		if( fileProperty != null )
246 			source = fileProperty;
247 		
248 		URL url = null;
249 		
250 		try
251 		{
252 			url = new URL( source );
253 		}
254 		catch( MalformedURLException e )
255 		{
256 			url = new URL( "file:" + source );
257 		}
258 
259 		return url.openStream();
260 	}
261 	
262 	public StepProperty getTestStepPropertyAt( int index )
263 	{
264 		return properties.get( index );
265 	}
266 	
267 	public int getStepPropertyCount()
268 	{
269 		return properties.size();
270 	}
271 	
272 	public String getSource()
273 	{
274 		return propertiesStepConfig.getSource();
275 	}
276 	
277 	public void setSource( String source )
278 	{
279 		propertiesStepConfig.setSource( source );
280 	}
281 	
282 	public String getTarget()
283 	{
284 		return propertiesStepConfig.getTarget();
285 	}
286 	
287 	public void setTarget( String target )
288 	{
289 		propertiesStepConfig.setTarget( target );
290 	}
291 
292 	public void resetConfigOnMove(TestStepConfig config)
293 	{
294 		super.resetConfigOnMove( config );
295 		
296 		propertiesStepConfig = (PropertiesStepConfig) config.getConfig().changeType(PropertiesStepConfig.type);
297 		for( int c = 0; c < propertiesStepConfig.getProperties().sizeOfPropertyArray(); c++ )
298 		{
299 			properties.get( c ).setConfig( propertiesStepConfig.getProperties().getPropertyArray( c ));
300 		}	
301 	}
302 	
303 	public StepProperty addProperty( String name )
304 	{
305 		PropertyConfig property = propertiesStepConfig.getProperties().addNewProperty();
306 		property.setName( name );
307 		StepProperty stepProperty = new StepProperty( property );
308 		properties.add( stepProperty );
309 		addProperty( stepProperty );
310 		firePropertyAdded( name );
311 		return stepProperty;
312 	}
313 	
314 	public void removeProperty( String name )
315 	{
316 		for( int c = 0; c < properties.size(); c++ )
317 		{
318 			if( properties.get( c ).getName().equalsIgnoreCase( name ))
319 			{
320 				removePropertyAt( c );
321 				return;
322 			}
323 		}
324 	}
325 	
326 	public void removePropertyAt( int index )
327 	{
328 		String name = properties.get( index ).getName();
329 		properties.remove( index );
330 		deleteProperty( name );
331 		firePropertyRemoved( name );
332 		propertiesStepConfig.getProperties().removeProperty( index );
333 	}
334 	
335 	/***
336 	 * Internal property class
337 	 * 
338 	 * @author ole
339 	 */
340 	
341 	public class StepProperty implements TestStepProperty
342 	{
343 		private PropertyConfig propertyConfig;
344 
345 		public StepProperty(PropertyConfig propertyConfig)
346 		{
347 			this.propertyConfig = propertyConfig;
348 		}
349 
350 		public void setConfig(PropertyConfig propertyConfig)
351 		{
352 			this.propertyConfig = propertyConfig;
353 		}
354 
355 		public String getName()
356 		{
357 			return propertyConfig.getName();
358 		}
359 		
360 		public void setName( String name )
361 		{
362 			String oldName = getName();
363 			propertyConfig.setName( name );
364 			propertyRenamed( oldName );
365 		}
366 
367 		public String getDescription()
368 		{
369 			return null;
370 		}
371 
372 		public String getValue()
373 		{
374 			return propertyConfig.getValue();
375 		}
376 
377 		public void setValue(String value)
378 		{
379 			String oldValue = getValue();
380 			propertyConfig.setValue( value );
381 			
382 			firePropertyValueChanged( getName(), oldValue, value );
383 		}
384 
385 		public boolean isReadOnly()
386 		{
387 			return false;
388 		}
389 
390 		public TestStep getTestStep()
391 		{
392 			return WsdlPropertiesTestStep.this;
393 		}
394 	}
395 
396 	public int loadProperties( boolean createMissing ) throws IOException
397 	{
398 		return loadProperties( getSource(), createMissing );
399 	}
400 
401 	public int saveProperties() throws IOException
402 	{
403 		return saveProperties( getTarget() );
404 	}
405 	
406 	public boolean isCreateMissingOnLoad()
407 	{
408 		return propertiesStepConfig.getCreateMissingOnLoad();
409 	}
410 	
411 	public void setCreateMissingOnLoad( boolean b )
412 	{
413 		propertiesStepConfig.setCreateMissingOnLoad( b );
414 	}
415 	
416 	public boolean isSaveFirst()
417 	{
418 		return propertiesStepConfig.getSaveFirst();
419 	}
420 	
421 	public void setSaveFirst( boolean b )
422 	{
423 		propertiesStepConfig.setSaveFirst( b );
424 	}
425 
426 	public boolean isDiscardValuesOnSave()
427 	{
428 		return propertiesStepConfig.getDiscardValuesOnSave();
429 	}
430 	
431 	public void setDiscardValuesOnSave( boolean b )
432 	{
433 		propertiesStepConfig.setDiscardValuesOnSave( b );
434 	}
435 	
436 	@Override
437 	public void setPropertyValue( String name, String value )
438 	{
439 		if( isCreateMissingOnLoad() && getProperty( name ) == null )
440 			addProperty( name );
441 		
442 		super.setPropertyValue( name, value );
443 	}
444 
445 	@Override
446 	public void onSave()
447 	{
448 		super.onSave();
449 		
450 		if( isDiscardValuesOnSave() )
451 		{
452 			clearPropertyValues();
453 		}
454 	}
455 
456 	public void clearPropertyValues()
457 	{
458 		for( StepProperty property : properties )
459 			property.setValue( null );
460 	}
461 }