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