View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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.testsuite.TestRunContext;
    19  import com.eviware.soapui.model.testsuite.TestRunner;
    20  import com.eviware.soapui.model.testsuite.TestStepResult;
    21  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
    22  import com.eviware.soapui.support.UISupport;
    23  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
    24  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
    25
    26  /***
    27   * TestStep that delays execution for a number of milliseconds
    28   *
    29   * @author ole.matzura
    30   */
    31
    32  public class WsdlDelayTestStep extends WsdlTestStep
    33  {
    34  	private static final int DEFAULT_DELAY = 1000;
    35  	private static final int DELAY_CHUNK = 100;
    36  	private int delay = WsdlDelayTestStep.DEFAULT_DELAY;
    37  	private int timeWaited = 0;
    38  	private boolean canceled;
    39
    40  	public WsdlDelayTestStep(WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest)
    41  	{
    42  		super(testCase, config, false, forLoadTest );
    43
    44  		if( !forLoadTest )
    45  		{
    46  			setIcon( UISupport.createImageIcon("/wait.gif"));
    47  		}
    48
    49  		if( config.getConfig() == null )
    50  		{
    51  			if( !forLoadTest )
    52  				saveDelay( config );
    53  		}
    54  		else
    55  		{
    56  			readConfig( config );
    57  		}
    58  	}
    59
    60  	private void readConfig(TestStepConfig config)
    61  	{
    62  		XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config.getConfig() );
    63  		delay = reader.readInt( "delay", 1000 );
    64  	}
    65
    66  	public String getLabel()
    67  	{
    68  		String str = super.getName() + " [" + (delay-timeWaited) + "ms]";
    69
    70  		if( isDisabled() )
    71  			str += " (disabled)";
    72
    73  		return str;
    74  	}
    75
    76  	private void saveDelay(TestStepConfig config)
    77  	{
    78  		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
    79  		builder.add( "delay", delay );
    80  		config.setConfig( builder.finish() );
    81  	}
    82
    83  	public void resetConfigOnMove(TestStepConfig config)
    84  	{
    85  		super.resetConfigOnMove( config );
    86  		readConfig( config );
    87  	}
    88
    89  	public int getDelay()
    90  	{
    91  		return delay;
    92  	}
    93
    94  	public void setDelay( int delay )
    95  	{
    96  		if( this.delay == delay )
    97  			return;
    98
    99  		String oldLabel = getLabel();
    100
    101 		this.delay = delay;
    102 		saveDelay( getConfig() );
    103 	   notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
    104 	}
    105
    106 	public TestStepResult run(TestRunner testRunner, TestRunContext context)
    107 	{
    108 		WsdlTestStepResult result = new WsdlTestStepResult( this );
    109 		result.startTimer();
    110 		String oldLabel = getLabel();
    111
    112 		try
    113 		{
    114 			canceled = false;
    115
    116 			// sleep in chunks for canceling
    117 			for( timeWaited = 0; !canceled && timeWaited < delay; timeWaited += DELAY_CHUNK )
    118 			{
    119 				if( timeWaited % 1000 == 0 && context.getProperty( TestRunContext.LOAD_TEST_RUNNER ) == null )
    120 				{
    121 					notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
    122 					oldLabel = getLabel();
    123 				}
    124
    125 				if( timeWaited <= delay-DELAY_CHUNK )
    126 					Thread.sleep( DELAY_CHUNK );
    127 				else
    128 					Thread.sleep( delay % DELAY_CHUNK );
    129 			}
    130 		}
    131 		catch (InterruptedException e)
    132 		{
    133 			SoapUI.logError( e );
    134 		}
    135
    136 		result.stopTimer();
    137 		result.setStatus( canceled ? TestStepStatus.CANCELED : TestStepStatus.OK );
    138
    139 		timeWaited = 0;
    140
    141 		if( context.getProperty( TestRunContext.LOAD_TEST_RUNNER ) == null )
    142 			notifyPropertyChanged( WsdlTestStep.LABEL_PROPERTY, oldLabel, getLabel() );
    143
    144 		return result;
    145 	}
    146
    147 	public boolean cancel()
    148 	{
    149 		canceled = true;
    150 		return true;
    151 	}
    152 }