001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.teststeps;
014:
015: import com.eviware.soapui.SoapUI;
016: import com.eviware.soapui.config.TestStepConfig;
017: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
018: import com.eviware.soapui.model.ModelItem;
019: import com.eviware.soapui.model.testsuite.TestRunContext;
020: import com.eviware.soapui.model.testsuite.TestRunner;
021: import com.eviware.soapui.model.testsuite.TestStepResult;
022: import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
023: import com.eviware.soapui.support.UISupport;
024: import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
025: import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
026:
027: /**
028: * TestStep that delays execution for a number of milliseconds
029: *
030: * @author ole.matzura
031: */
032:
033: public class WsdlDelayTestStep extends WsdlTestStep {
034: private static final int DEFAULT_DELAY = 1000;
035: private static final int DELAY_CHUNK = 100;
036: private int delay = WsdlDelayTestStep.DEFAULT_DELAY;
037: private int timeWaited = 0;
038: private boolean canceled;
039:
040: public WsdlDelayTestStep(WsdlTestCase testCase,
041: TestStepConfig config, boolean forLoadTest) {
042: super (testCase, config, false, forLoadTest);
043:
044: if (!forLoadTest) {
045: setIcon(UISupport.createImageIcon("/wait.gif"));
046: }
047:
048: if (config.getConfig() == null) {
049: if (!forLoadTest)
050: saveDelay(config);
051: } else {
052: readConfig(config);
053: }
054: }
055:
056: private void readConfig(TestStepConfig config) {
057: XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader(
058: config.getConfig());
059: delay = reader.readInt("delay", 1000);
060: }
061:
062: public String getName() {
063: return super .getName() + " [" + (delay - timeWaited) + "ms]";
064: }
065:
066: private void saveDelay(TestStepConfig config) {
067: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
068: builder.add("delay", delay);
069: config.setConfig(builder.finish());
070: }
071:
072: public void resetConfigOnMove(TestStepConfig config) {
073: super .resetConfigOnMove(config);
074: readConfig(config);
075: }
076:
077: public int getDelay() {
078: return delay;
079: }
080:
081: public void setDelay(int delay) {
082: if (this .delay == delay)
083: return;
084:
085: String oldName = getName();
086:
087: this .delay = delay;
088: saveDelay(getConfig());
089: notifyPropertyChanged(ModelItem.NAME_PROPERTY, oldName,
090: getName());
091: }
092:
093: public TestStepResult run(TestRunner testRunner,
094: TestRunContext context) {
095: WsdlTestStepResult result = new WsdlTestStepResult(this );
096: result.startTimer();
097: String oldName = getName();
098:
099: try {
100: canceled = false;
101:
102: // sleep in chunks for canceling
103: for (timeWaited = 0; !canceled && timeWaited < delay; timeWaited += DELAY_CHUNK) {
104: if (timeWaited % 1000 == 0
105: && context
106: .getProperty(TestRunContext.LOAD_TEST_RUNNER) == null) {
107: notifyPropertyChanged(ModelItem.NAME_PROPERTY,
108: oldName, getName());
109: oldName = getName();
110: }
111:
112: if (timeWaited <= delay - DELAY_CHUNK)
113: Thread.sleep(DELAY_CHUNK);
114: else
115: Thread.sleep(delay % DELAY_CHUNK);
116: }
117: } catch (InterruptedException e) {
118: SoapUI.logError(e);
119: }
120:
121: result.stopTimer();
122: result.setStatus(canceled ? TestStepStatus.CANCELED
123: : TestStepStatus.OK);
124:
125: timeWaited = 0;
126:
127: if (context.getProperty(TestRunContext.LOAD_TEST_RUNNER) == null)
128: notifyPropertyChanged(ModelItem.NAME_PROPERTY, oldName,
129: getName());
130:
131: return result;
132: }
133:
134: public boolean cancel() {
135: canceled = true;
136: return true;
137: }
138: }
|