View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 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.PropertyExpansionUtils;
19  import com.eviware.soapui.model.support.DefaultTestStepProperty;
20  import com.eviware.soapui.model.testsuite.TestRunContext;
21  import com.eviware.soapui.model.testsuite.TestRunner;
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,
64                new DefaultTestStepProperty.PropertyHandlerAdapter()
65                {
66  
67                   public String getValue( DefaultTestStepProperty property )
68                   {
69                      return getDelayString();
70                   }
71  
72                   @Override
73                   public void setValue( DefaultTestStepProperty property, String value )
74                   {
75                      setDelayString( value );
76                   }
77                }, this ) );
78     }
79  
80     private void readConfig( TestStepConfig config )
81     {
82        XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config.getConfig() );
83        delayString = reader.readString( "delay", DEFAULT_DELAY );
84     }
85  
86     public String getLabel()
87     {
88        String str = running ?
89                super.getName() + " [" + ( delay - timeWaited ) + "ms]" :
90                super.getName() + " [" + delayString + "]";
91  
92        if( isDisabled() )
93           str += " (disabled)";
94  
95        return str;
96     }
97  
98     public String getDefaultSourcePropertyName()
99     {
100       return "delay";
101    }
102 
103    public String getDefaultTargetPropertyName()
104    {
105       return "delay";
106    }
107 
108    private void saveDelay( TestStepConfig config )
109    {
110       XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
111       builder.add( "delay", delayString );
112       config.setConfig( builder.finish() );
113    }
114 
115    public void resetConfigOnMove( TestStepConfig config )
116    {
117       super.resetConfigOnMove( config );
118       readConfig( config );
119    }
120 
121    public void setDelayString( String delayString )
122    {
123       if( this.delayString.equals( delayString ) )
124          return;
125 
126       String oldLabel = getLabel();
127 
128       this.delayString = delayString;
129       saveDelay( getConfig() );
130       notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
131    }
132 
133    public String getDelayString()
134    {
135       return delayString;
136    }
137 
138    public int getDelay()
139    {
140       return delay;
141    }
142 
143    public void setDelay( int delay )
144    {
145       if( this.delay == delay )
146          return;
147 
148       String oldLabel = getLabel();
149 
150       this.delay = delay;
151       saveDelay( getConfig() );
152       notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
153    }
154 
155    public TestStepResult run( TestRunner testRunner, TestRunContext context )
156    {
157       WsdlTestStepResult result = new WsdlTestStepResult( this );
158       result.startTimer();
159       String oldLabel = getLabel();
160 
161       try
162       {
163          canceled = false;
164          running = true;
165 
166          try
167          {
168             delay = Integer.parseInt( PropertyExpansionUtils.expandProperties( context, delayString ) );
169          }
170          catch( NumberFormatException e )
171          {
172             delay = Integer.parseInt( DEFAULT_DELAY );
173          }
174 
175          // sleep in chunks for canceling
176          for( timeWaited = 0; !canceled && timeWaited < delay; timeWaited += DELAY_CHUNK )
177          {
178             if( timeWaited % 1000 == 0 && context.getProperty( TestRunContext.LOAD_TEST_RUNNER ) == null )
179             {
180                String newLabel = getLabel();
181                notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, newLabel );
182                oldLabel = newLabel;
183             }
184 
185             if( timeWaited <= delay - DELAY_CHUNK )
186                Thread.sleep( DELAY_CHUNK );
187             else
188                Thread.sleep( delay % DELAY_CHUNK );
189          }
190       }
191       catch( InterruptedException e )
192       {
193          SoapUI.logError( e );
194       }
195 
196       result.stopTimer();
197       result.setStatus( canceled ? TestStepStatus.CANCELED : TestStepStatus.OK );
198 
199       timeWaited = 0;
200 
201       if( context.getProperty( TestRunContext.LOAD_TEST_RUNNER ) == null )
202          notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
203 
204       return result;
205    }
206 
207    public boolean cancel()
208    {
209       canceled = true;
210       return true;
211    }
212 }