1
2
3
4
5
6
7
8
9
10
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.Enumeration;
23 import java.util.List;
24
25 import javax.swing.ImageIcon;
26
27 import com.eviware.soapui.SoapUI;
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.actions.support.ShowOnlineHelpAction;
33 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
34 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
35 import com.eviware.soapui.impl.wsdl.teststeps.actions.CloneTestStepAction;
36 import com.eviware.soapui.model.testsuite.TestRunContext;
37 import com.eviware.soapui.model.testsuite.TestRunner;
38 import com.eviware.soapui.model.testsuite.TestStep;
39 import com.eviware.soapui.model.testsuite.TestStepProperty;
40 import com.eviware.soapui.model.testsuite.TestStepResult;
41 import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
42 import com.eviware.soapui.monitor.TestMonitor;
43 import com.eviware.soapui.support.UISupport;
44 import com.eviware.soapui.support.action.ActionSupport;
45
46 public class WsdlPropertiesTestStep extends WsdlTestStep
47 {
48 private PropertiesStepConfig propertiesStepConfig;
49 private List<StepProperty> properties = new ArrayList<StepProperty>();
50 private ImageIcon okIcon;
51 private ImageIcon failedIcon;
52
53 public WsdlPropertiesTestStep(WsdlTestCase testCase, TestStepConfig config)
54 {
55 super(testCase, config, true);
56
57 okIcon = UISupport.createImageIcon("/properties_step.gif");
58 failedIcon = UISupport.createImageIcon("/properties_step_failed.gif");
59
60 setIcon( okIcon );
61
62 addAction( ActionSupport.SEPARATOR_ACTION );
63 addAction( new CloneTestStepAction( this, "PropertiesStep" ) );
64 addAction( ActionSupport.SEPARATOR_ACTION );
65 addAction( new ShowOnlineHelpAction( HelpUrls.PROPERTIESSTEP_HELP_URL ));
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 TestMonitor testMonitor = SoapUI.getTestMonitor();
99 boolean hasRunningLoadTest = testMonitor == null ? false : testMonitor.hasRunningLoadTest( getTestCase() );
100
101 WsdlTestStepResult result = new WsdlTestStepResult( this );
102
103 if( !hasRunningLoadTest )
104 setIcon( okIcon );
105
106 result.setStatus( TestStepStatus.OK );
107 result.startTimer();
108
109 if( isSaveFirst() )
110 saveDuringRun( hasRunningLoadTest, result );
111
112 String source = getSource();
113 if( source != null && source.trim().length() > 0 )
114 {
115 try
116 {
117 int cnt = loadProperties(source,isCreateMissingOnLoad());
118
119 result.setStatus( TestStepStatus.OK );
120 result.addMessage( "Loaded " + cnt + " properties from [" + source + "]" );
121 }
122 catch (IOException e)
123 {
124 result.stopTimer();
125 result.addMessage( "Failed to load properties from [" + source + "]" );
126 result.setStatus( TestStepStatus.FAILED );
127 result.setError( e );
128
129 if( !hasRunningLoadTest )
130 setIcon( failedIcon );
131 }
132 }
133
134 if( !isSaveFirst() )
135 saveDuringRun( hasRunningLoadTest, result );
136
137 result.stopTimer();
138
139 return result;
140 }
141
142 private boolean saveDuringRun( boolean hasRunningLoadTest, WsdlTestStepResult result )
143 {
144 String target = getTarget();
145 if( target != null && target.trim().length() > 0 )
146 {
147 try
148 {
149 int cnt = saveProperties(target);
150
151 result.setStatus( TestStepStatus.OK );
152 result.addMessage( "Saved " + cnt + " properties to [" + target + "]" );
153 }
154 catch (IOException e)
155 {
156 result.stopTimer();
157 result.addMessage( "Failed to save properties to [" + target + "]" );
158 result.setStatus( TestStepStatus.FAILED );
159 result.setError( e );
160
161 if( !hasRunningLoadTest )
162 setIcon( failedIcon );
163
164 return false;
165 }
166 }
167
168 return true;
169 }
170
171 private int saveProperties( String target ) throws IOException
172 {
173 java.util.Properties props = new java.util.Properties();
174
175 int cnt = 0;
176 for( StepProperty p : properties )
177 {
178 String name = p.getName();
179 props.setProperty( name, p.getValue() );
180 cnt++;
181 }
182
183 props.store(getPropertiesOutputStream(target), "TestStep [" + getName() + "] properties");
184
185 return cnt;
186 }
187
188 private FileOutputStream getPropertiesOutputStream(String target) throws FileNotFoundException
189 {
190 String fileProperty = System.getProperty( target );
191 if( fileProperty != null )
192 target = fileProperty;
193
194 return new FileOutputStream(target);
195 }
196
197 private int loadProperties(String source, boolean createMissing) throws IOException
198 {
199 java.util.Properties props = new java.util.Properties();
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 TestStepProperty 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 StepProperty getTestStepPropertyAt( int index )
244 {
245 return properties.get( index );
246 }
247
248 public int getStepPropertyCount()
249 {
250 return properties.size();
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 void setTarget( String target )
269 {
270 propertiesStepConfig.setTarget( target );
271 }
272
273 public void resetConfigOnMove(TestStepConfig config)
274 {
275 super.resetConfigOnMove( config );
276
277 propertiesStepConfig = (PropertiesStepConfig) config.getConfig().changeType(PropertiesStepConfig.type);
278 for( int c = 0; c < propertiesStepConfig.getProperties().sizeOfPropertyArray(); c++ )
279 {
280 properties.get( c ).setConfig( propertiesStepConfig.getProperties().getPropertyArray( c ));
281 }
282 }
283
284 public StepProperty addProperty( String name )
285 {
286 PropertyConfig property = propertiesStepConfig.getProperties().addNewProperty();
287 property.setName( name );
288 StepProperty stepProperty = new StepProperty( property );
289 properties.add( stepProperty );
290 addProperty( stepProperty );
291 firePropertyAdded( name );
292 return stepProperty;
293 }
294
295 public void removeProperty( String name )
296 {
297 for( int c = 0; c < properties.size(); c++ )
298 {
299 if( properties.get( c ).getName().equalsIgnoreCase( name ))
300 {
301 removePropertyAt( c );
302 return;
303 }
304 }
305 }
306
307 public void removePropertyAt( int index )
308 {
309 String name = properties.get( index ).getName();
310 properties.remove( index );
311 deleteProperty( name );
312 firePropertyRemoved( name );
313 propertiesStepConfig.getProperties().removeProperty( index );
314 }
315
316 /***
317 * Internal property class
318 *
319 * @author ole
320 */
321
322 public class StepProperty implements TestStepProperty
323 {
324 private PropertyConfig propertyConfig;
325
326 public StepProperty(PropertyConfig propertyConfig)
327 {
328 this.propertyConfig = propertyConfig;
329 }
330
331 public void setConfig(PropertyConfig propertyConfig)
332 {
333 this.propertyConfig = propertyConfig;
334 }
335
336 public String getName()
337 {
338 return propertyConfig.getName();
339 }
340
341 public void setName( String name )
342 {
343 String oldName = getName();
344 propertyConfig.setName( name );
345 propertyRenamed( oldName );
346 }
347
348 public String getDescription()
349 {
350 return null;
351 }
352
353 public String getValue()
354 {
355 return propertyConfig.getValue();
356 }
357
358 public void setValue(String value)
359 {
360 String oldValue = getValue();
361 propertyConfig.setValue( value );
362
363 firePropertyValueChanged( getName(), oldValue, value );
364 }
365
366 public boolean isReadOnly()
367 {
368 return false;
369 }
370
371 public TestStep getTestStep()
372 {
373 return WsdlPropertiesTestStep.this;
374 }
375 }
376
377 public int loadProperties( boolean createMissing ) throws IOException
378 {
379 return loadProperties( getSource(), createMissing );
380 }
381
382 public int saveProperties() throws IOException
383 {
384 return saveProperties( getTarget() );
385 }
386
387 public boolean isCreateMissingOnLoad()
388 {
389 return propertiesStepConfig.getCreateMissingOnLoad();
390 }
391
392 public void setCreateMissingOnLoad( boolean b )
393 {
394 propertiesStepConfig.setCreateMissingOnLoad( b );
395 }
396
397 public boolean isSaveFirst()
398 {
399 return propertiesStepConfig.getSaveFirst();
400 }
401
402 public void setSaveFirst( boolean b )
403 {
404 propertiesStepConfig.setSaveFirst( b );
405 }
406 }