1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps;
14
15 import com.eviware.soapui.SoapUI;
16 import com.eviware.soapui.config.TestStepConfig;
17 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
18 import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
19 import com.eviware.soapui.model.support.DefaultTestStepProperty;
20 import com.eviware.soapui.model.testsuite.TestCaseRunContext;
21 import com.eviware.soapui.model.testsuite.TestCaseRunner;
22 import com.eviware.soapui.model.testsuite.TestStepResult;
23 import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
24 import com.eviware.soapui.support.UISupport;
25 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
26 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
27
28 /***
29 * TestStep that delays execution for a number of milliseconds
30 *
31 * @author ole.matzura
32 */
33
34 public class WsdlDelayTestStep extends WsdlTestStepWithProperties
35 {
36 private static final String DEFAULT_DELAY = "1000";
37 private static final int DELAY_CHUNK = 100;
38 private int delay = 0;
39 private String delayString = WsdlDelayTestStep.DEFAULT_DELAY;
40 private int timeWaited = 0;
41 private boolean canceled;
42 private boolean running;
43
44 public WsdlDelayTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
45 {
46 super( testCase, config, false, forLoadTest );
47
48 if( !forLoadTest )
49 {
50 setIcon( UISupport.createImageIcon( "/wait.gif" ) );
51 }
52
53 if( config.getConfig() == null )
54 {
55 if( !forLoadTest )
56 saveDelay( config );
57 }
58 else
59 {
60 readConfig( config );
61 }
62
63 addProperty( new DefaultTestStepProperty( "delay", true, new DefaultTestStepProperty.PropertyHandlerAdapter()
64 {
65
66 public String getValue( DefaultTestStepProperty property )
67 {
68 return getDelayString();
69 }
70
71 @Override
72 public void setValue( DefaultTestStepProperty property, String value )
73 {
74 setDelayString( value );
75 }
76 }, this ) );
77 }
78
79 private void readConfig( TestStepConfig config )
80 {
81 XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config.getConfig() );
82 delayString = reader.readString( "delay", DEFAULT_DELAY );
83 }
84
85 public String getLabel()
86 {
87 String str = running ? super.getName() + " [" + ( delay - timeWaited ) + "ms]" : super.getName() + " ["
88 + delayString + "]";
89
90 if( isDisabled() )
91 str += " (disabled)";
92
93 return str;
94 }
95
96 public String getDefaultSourcePropertyName()
97 {
98 return "delay";
99 }
100
101 public String getDefaultTargetPropertyName()
102 {
103 return "delay";
104 }
105
106 private void saveDelay( TestStepConfig config )
107 {
108 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
109 builder.add( "delay", delayString );
110 config.setConfig( builder.finish() );
111 }
112
113 public void resetConfigOnMove( TestStepConfig config )
114 {
115 super.resetConfigOnMove( config );
116 readConfig( config );
117 }
118
119 public void setDelayString( String delayString )
120 {
121 if( this.delayString.equals( delayString ) )
122 return;
123
124 String oldLabel = getLabel();
125
126 this.delayString = delayString;
127 saveDelay( getConfig() );
128 notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
129 }
130
131 public String getDelayString()
132 {
133 return delayString;
134 }
135
136 public int getDelay()
137 {
138 try
139 {
140 return Integer.parseInt( PropertyExpander.expandProperties( this, delayString ));
141 }
142 catch( NumberFormatException e )
143 {
144 return -1;
145 }
146 }
147
148 public void setDelay( int delay )
149 {
150 String oldLabel = getLabel();
151
152 this.delayString = String.valueOf( delay );
153 saveDelay( getConfig() );
154 notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
155 }
156
157 public TestStepResult run( TestCaseRunner testRunner, TestCaseRunContext context )
158 {
159 WsdlTestStepResult result = new WsdlTestStepResult( this );
160 result.startTimer();
161 String oldLabel = getLabel();
162
163 try
164 {
165 canceled = false;
166 running = true;
167
168 try
169 {
170 delay = Integer.parseInt( PropertyExpander.expandProperties( context, delayString ) );
171 }
172 catch( NumberFormatException e )
173 {
174 delay = Integer.parseInt( DEFAULT_DELAY );
175 }
176
177
178 for( timeWaited = 0; !canceled && timeWaited < delay; timeWaited += DELAY_CHUNK )
179 {
180 if( timeWaited % 1000 == 0 && context.getProperty( TestCaseRunContext.LOAD_TEST_RUNNER ) == null )
181 {
182 String newLabel = getLabel();
183 notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, newLabel );
184 oldLabel = newLabel;
185 }
186
187 if( timeWaited <= delay - DELAY_CHUNK )
188 Thread.sleep( DELAY_CHUNK );
189 else
190 Thread.sleep( delay % DELAY_CHUNK );
191 }
192 }
193 catch( InterruptedException e )
194 {
195 SoapUI.logError( e );
196 }
197
198 result.stopTimer();
199 result.setStatus( canceled ? TestStepStatus.CANCELED : TestStepStatus.OK );
200
201 timeWaited = 0;
202 running = false;
203
204 if( context.getProperty( TestCaseRunContext.LOAD_TEST_RUNNER ) == null )
205 notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
206
207 return result;
208 }
209
210 public boolean cancel()
211 {
212 canceled = true;
213 return true;
214 }
215 }