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