1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps;
14
15 import java.awt.event.ActionEvent;
16
17 import javax.swing.AbstractAction;
18
19 import com.eviware.soapui.config.TestStepConfig;
20 import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
21 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
22 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
23 import com.eviware.soapui.impl.wsdl.teststeps.actions.CloneTestStepAction;
24 import com.eviware.soapui.model.ModelItem;
25 import com.eviware.soapui.model.testsuite.TestRunContext;
26 import com.eviware.soapui.model.testsuite.TestRunner;
27 import com.eviware.soapui.model.testsuite.TestStepResult;
28 import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
29 import com.eviware.soapui.support.UISupport;
30 import com.eviware.soapui.support.action.ActionSupport;
31 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
32 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
33
34 public class WsdlDelayTestStep extends WsdlTestStep
35 {
36 private static final int DEFAULT_DELAY = 1000;
37 private static final int DELAY_CHUNK = 100;
38 private int delay = WsdlDelayTestStep.DEFAULT_DELAY;
39 private int timeWaited = 0;
40 private boolean canceled;
41
42 public WsdlDelayTestStep(WsdlTestCase testCase, TestStepConfig config)
43 {
44 super(testCase, config, false);
45
46 addAction( ActionSupport.SEPARATOR_ACTION );
47 addAction( new SetWaitTimeAction(), true );
48 addAction( new CloneTestStepAction( this, "DelayStep" ) );
49 addAction( ActionSupport.SEPARATOR_ACTION );
50 addAction( new ShowOnlineHelpAction( HelpUrls.DELAYSTEP_HELP_URL ));
51
52 setIcon( UISupport.createImageIcon("/wait.gif"));
53
54 if( config.getConfig() == null )
55 {
56 saveDelay( config );
57 }
58 else
59 {
60 readConfig( config );
61 }
62 }
63
64 private void readConfig(TestStepConfig config)
65 {
66 XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config.getConfig() );
67 delay = reader.readInt( "delay", 1000 );
68 }
69
70 public String getName()
71 {
72 return super.getName() + " [" + (delay-timeWaited) + "ms]";
73 }
74
75 private void saveDelay(TestStepConfig config)
76 {
77 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
78 builder.add( "delay", delay );
79 config.setConfig( builder.finish() );
80 }
81
82 public void resetConfigOnMove(TestStepConfig config)
83 {
84 super.resetConfigOnMove( config );
85 readConfig( config );
86 }
87
88 public int getDelay()
89 {
90 return delay;
91 }
92
93 public void setDelay( int delay )
94 {
95 if( this.delay == delay )
96 return;
97
98 String oldName = getName();
99
100 this.delay = delay;
101 saveDelay( getConfig() );
102 notifyPropertyChanged( ModelItem.NAME_PROPERTY, oldName, getName() );
103 }
104
105 public TestStepResult run(TestRunner testRunner, TestRunContext context)
106 {
107 WsdlTestStepResult result = new WsdlTestStepResult( this );
108 result.startTimer();
109 String oldName = getName();
110
111 try
112 {
113 canceled = false;
114
115
116 for( timeWaited = 0; !canceled && timeWaited < delay; timeWaited += DELAY_CHUNK )
117 {
118 if( timeWaited % 1000 == 0 && context.getProperty( TestRunContext.LOAD_TEST_RUNNER ) == null )
119 {
120 notifyPropertyChanged( ModelItem.NAME_PROPERTY, oldName, getName() );
121 oldName = getName();
122 }
123
124 if( timeWaited <= delay-DELAY_CHUNK )
125 Thread.sleep( DELAY_CHUNK );
126 else
127 Thread.sleep( delay % DELAY_CHUNK );
128 }
129 }
130 catch (InterruptedException e)
131 {
132 e.printStackTrace();
133 }
134
135 result.stopTimer();
136 result.setStatus( canceled ? TestStepStatus.CANCELED : TestStepStatus.OK );
137
138 timeWaited = 0;
139
140 if( context.getProperty( TestRunContext.LOAD_TEST_RUNNER ) == null )
141 notifyPropertyChanged( ModelItem.NAME_PROPERTY, oldName, getName() );
142
143 return result;
144 }
145
146 public boolean cancel()
147 {
148 canceled = true;
149 return true;
150 }
151
152 private class SetWaitTimeAction extends AbstractAction
153 {
154 public SetWaitTimeAction()
155 {
156 super( "Set Delay Time" );
157 }
158
159 public void actionPerformed(ActionEvent e)
160 {
161 String value = UISupport.prompt( "Specify delay in milliseconds", "Set Delay", String.valueOf( delay ));
162 if( value != null )
163 {
164 try
165 {
166 setDelay( Integer.parseInt(value) );
167 }
168 catch (NumberFormatException e1)
169 {
170 UISupport.showErrorMessage( e1 );
171 }
172 }
173 }
174 }
175 }