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.Collections;
22  import java.util.Enumeration;
23  import java.util.Map;
24  
25  import javax.swing.ImageIcon;
26  
27  import com.eviware.soapui.config.PropertiesStepConfig;
28  import com.eviware.soapui.config.TestStepConfig;
29  import com.eviware.soapui.impl.wsdl.MutableTestPropertyHolder;
30  import com.eviware.soapui.impl.wsdl.support.XmlBeansPropertiesTestPropertyHolder;
31  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
32  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
33  import com.eviware.soapui.model.testsuite.TestProperty;
34  import com.eviware.soapui.model.testsuite.TestPropertyListener;
35  import com.eviware.soapui.model.testsuite.TestRunContext;
36  import com.eviware.soapui.model.testsuite.TestRunner;
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 implements MutableTestPropertyHolder
49  {
50  	private PropertiesStepConfig propertiesStepConfig;
51  	private ImageIcon okIcon;
52  	private ImageIcon failedIcon;
53  	private XmlBeansPropertiesTestPropertyHolder propertyHolderSupport;
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  				propertiesStepConfig.addNewProperties();
79  			}
80  			
81  			if( !propertiesStepConfig.isSetSaveFirst() )
82  				propertiesStepConfig.setSaveFirst( true );
83  		}
84  	   
85  	   propertyHolderSupport = new XmlBeansPropertiesTestPropertyHolder( this, propertiesStepConfig.getProperties() );
86  	}
87  	
88  	public TestStepResult run(TestRunner testRunner, TestRunContext testRunContext)
89  	{
90  		WsdlTestStepResult result = new WsdlTestStepResult( this );
91  		
92  		if( okIcon != null )
93  			setIcon( okIcon );
94  		
95  		result.setStatus( TestStepStatus.OK );
96  		result.startTimer();
97  		
98  		if( isSaveFirst() ) 
99  			saveDuringRun( result, testRunContext );
100 		
101 		String source = getSource();
102 		if( source != null && source.trim().length() > 0  )
103 		{
104 			try
105 			{
106 				source = testRunContext.expand( source );
107 				int cnt = loadProperties( source, isCreateMissingOnLoad());
108 				
109 				result.setStatus( TestStepStatus.OK );
110 				result.addMessage( "Loaded " + cnt + " properties from [" + source + "]" );
111 			}
112 			catch (IOException e)
113 			{
114 				result.stopTimer();
115 				result.addMessage( "Failed to load properties from [" + source + "]" );
116 				result.setStatus( TestStepStatus.FAILED );
117 				result.setError( e );
118 				
119 				if( failedIcon != null )
120 					setIcon( failedIcon );
121 			}			
122 		}
123 		
124 		if( !isSaveFirst() )
125 			saveDuringRun( result, testRunContext );
126 		
127 		result.stopTimer();
128 
129 		return result;
130 	}
131 
132 	private boolean saveDuringRun( WsdlTestStepResult result, TestRunContext context )
133 	{
134 		String target = getTarget();
135 		if( target != null && target.trim().length() > 0 )
136 		{
137 			try
138 			{
139 				target = context.expand( target );
140 				int cnt = saveProperties(target);
141 				
142 				result.setStatus( TestStepStatus.OK );
143 				result.addMessage( "Saved " + cnt + " properties to [" + target + "]" );
144 			}
145 			catch (IOException e)
146 			{
147 				result.stopTimer();
148 				result.addMessage( "Failed to save properties to [" + target + "]" );
149 				result.setStatus( TestStepStatus.FAILED );
150 				result.setError( e );
151 				
152 				if( failedIcon != null )
153 					setIcon( failedIcon );
154 				
155 				return false;
156 			}			
157 		}
158 		
159 		return true;
160 	}
161 
162 	private int saveProperties( String target ) throws IOException
163 	{
164 		java.util.Properties props = new java.util.Properties();
165 		propertyHolderSupport.saveTo( props );
166 		props.store(getPropertiesOutputStream(target), "TestStep [" + getName() + "] properties");
167 		return props.size();
168 	}
169 
170 	private FileOutputStream getPropertiesOutputStream(String target) throws FileNotFoundException
171 	{
172 		String fileProperty = System.getProperty( target );
173 		if( fileProperty != null )
174 			target = fileProperty;
175 		
176 		return new FileOutputStream(target);
177 	}
178 	
179 	private int loadProperties(String source, boolean createMissing) throws IOException
180 	{
181 		// override methods so propertynames are returned in readorder
182 		java.util.Properties props = new java.util.Properties()
183 		{
184 			public StringList names = new StringList();
185 
186 			@Override
187 			public synchronized Object put( Object key, Object value )
188 			{
189 				names.add( key.toString() );
190 				return super.put( key, value );
191 			}
192 
193 			@Override
194 			public Enumeration<?> propertyNames()
195 			{
196 				return Collections.enumeration( names );
197 			}
198 		};
199 		
200 		props.load(getPropertiesInputStream(source));
201 		
202 		int cnt = 0;
203 		Enumeration names = props.propertyNames();
204 		while( names.hasMoreElements() )
205 		{
206 			String name = names.nextElement().toString();
207 			TestProperty property = getProperty( name );
208 			if( property != null )
209 			{
210 				property.setValue( props.get( name ).toString() );
211 				cnt++;
212 			}
213 			else if( createMissing )
214 			{
215 				addProperty( name ).setValue( props.get( name ).toString() );
216 				cnt++;
217 			}
218 		}
219 		
220 		return cnt;
221 	}
222 
223 	private InputStream getPropertiesInputStream(String source) throws IOException
224 	{
225 		String fileProperty = System.getProperty( source );
226 		if( fileProperty != null )
227 			source = fileProperty;
228 		
229 		URL url = null;
230 		
231 		try
232 		{
233 			url = new URL( source );
234 		}
235 		catch( MalformedURLException e )
236 		{
237 			url = new URL( "file:" + source );
238 		}
239 
240 		return url.openStream();
241 	}
242 	
243 	public TestProperty getTestStepPropertyAt( int index )
244 	{
245 		return propertyHolderSupport.getPropertyAt( index );
246 	}
247 	
248 	public int getStepPropertyCount()
249 	{
250 		return propertyHolderSupport.getPropertyCount();
251 	}
252 	
253 	public String getSource()
254 	{
255 		return propertiesStepConfig.getSource();
256 	}
257 	
258 	public void setSource( String source )
259 	{
260 		propertiesStepConfig.setSource( source );
261 	}
262 	
263 	public String getTarget()
264 	{
265 		return propertiesStepConfig.getTarget();
266 	}
267 	
268 	public String getLabel()
269 	{
270 		String str = super.getName() + " (" + getPropertyCount() + ")";
271 		
272 		if( isDisabled() )
273 			str += " (disabled)";
274 		
275 		return str;
276 	}
277 	
278 	public void setTarget( String target )
279 	{
280 		propertiesStepConfig.setTarget( target );
281 	}
282 
283 	public void resetConfigOnMove(TestStepConfig config)
284 	{
285 		super.resetConfigOnMove( config );
286 		
287 		propertiesStepConfig = (PropertiesStepConfig) config.getConfig().changeType(PropertiesStepConfig.type);
288 		propertyHolderSupport.resetPropertiesConfig( propertiesStepConfig.getProperties() );
289 	}
290 	
291 	public int loadProperties( boolean createMissing ) throws IOException
292 	{
293 		String source = PropertyExpansionUtils.expandProperties( this ,getSource());
294 		return loadProperties( source, createMissing );
295 	}
296 
297 	public int saveProperties() throws IOException
298 	{
299 		String target = PropertyExpansionUtils.expandProperties( this, getTarget());
300 		return saveProperties( target );
301 	}
302 	
303 	public boolean isCreateMissingOnLoad()
304 	{
305 		return propertiesStepConfig.getCreateMissingOnLoad();
306 	}
307 	
308 	public void setCreateMissingOnLoad( boolean b )
309 	{
310 		propertiesStepConfig.setCreateMissingOnLoad( b );
311 	}
312 	
313 	public boolean isSaveFirst()
314 	{
315 		return propertiesStepConfig.getSaveFirst();
316 	}
317 	
318 	public void setSaveFirst( boolean b )
319 	{
320 		propertiesStepConfig.setSaveFirst( b );
321 	}
322 
323 	public boolean isDiscardValuesOnSave()
324 	{
325 		return propertiesStepConfig.getDiscardValuesOnSave();
326 	}
327 	
328 	public void setDiscardValuesOnSave( boolean b )
329 	{
330 		propertiesStepConfig.setDiscardValuesOnSave( b );
331 	}
332 	
333 	public void setPropertyValue( String name, String value )
334 	{
335 		if( isCreateMissingOnLoad() && getProperty( name ) == null )
336 			addProperty( name );
337 		
338 		propertyHolderSupport.setPropertyValue( name, value );
339 	}
340 
341 	@Override
342 	public void beforeSave()
343 	{
344 		super.beforeSave();
345 		
346 		if( isDiscardValuesOnSave() )
347 		{
348 			clearPropertyValues();
349 		}
350 	}
351 
352 	public void clearPropertyValues()
353 	{
354 		for( TestProperty property : propertyHolderSupport.getProperties().values() )
355 			property.setValue( null );
356 	}
357 
358 	public boolean renameProperty( String name, String newName )
359 	{
360 		return PropertyExpansionUtils.renameProperty( propertyHolderSupport, getProperty( name ), newName, getTestCase() ) != null;
361 		
362 //		return propertyHolderSupport.renameProperty( name, newName );
363 	}
364 
365 	public TestProperty addProperty( String name )
366 	{
367 		String oldLabel = getLabel();
368 		
369 		TestProperty property = propertyHolderSupport.addProperty( name );
370 	   notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
371 		
372 		return property;
373 	}
374 
375 	public void addTestPropertyListener( TestPropertyListener listener )
376 	{
377 		propertyHolderSupport.addTestPropertyListener( listener );
378 	}
379 
380 	public Map<String, TestProperty> getProperties()
381 	{
382 		return propertyHolderSupport.getProperties();
383 	}
384 
385 	public TestProperty getProperty( String name )
386 	{
387 		return propertyHolderSupport.getProperty( name );
388 	}
389 
390 	public TestProperty getPropertyAt( int index )
391 	{
392 		return propertyHolderSupport.getPropertyAt( index );
393 	}
394 
395 	public int getPropertyCount()
396 	{
397 		return propertyHolderSupport.getPropertyCount();
398 	}
399 
400 	public String[] getPropertyNames()
401 	{
402 		return propertyHolderSupport.getPropertyNames();
403 	}
404 
405 	public String getPropertyValue( String name )
406 	{
407 		return propertyHolderSupport.getPropertyValue( name );
408 	}
409 
410 	public void removeProperty( String propertyName )
411 	{
412 		String oldLabel = getLabel();
413 		
414 		propertyHolderSupport.removeProperty( propertyName );
415 	   notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
416 	}
417 
418 	public void removeTestPropertyListener( TestPropertyListener listener )
419 	{
420 		propertyHolderSupport.removeTestPropertyListener( listener );
421 	}
422 
423 	public boolean hasProperty( String name )
424 	{
425 		return propertyHolderSupport.hasProperty( name );
426 	}
427 }