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.loadtest.strategy;
014:
015: import javax.swing.JComponent;
016: import javax.swing.JLabel;
017: import javax.swing.JPanel;
018: import javax.swing.JTextField;
019: import javax.swing.text.Document;
020:
021: import org.apache.xmlbeans.XmlObject;
022:
023: import com.eviware.soapui.SoapUI;
024: import com.eviware.soapui.model.testsuite.LoadTestRunContext;
025: import com.eviware.soapui.model.testsuite.LoadTestRunner;
026: import com.eviware.soapui.model.testsuite.TestRunContext;
027: import com.eviware.soapui.model.testsuite.TestRunner;
028: import com.eviware.soapui.support.DocumentListenerAdapter;
029: import com.eviware.soapui.support.UISupport;
030: import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
031: import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
032: import com.jgoodies.forms.builder.ButtonBarBuilder;
033:
034: /**
035: * LoadStrategy allowing maximum runs and request delays
036: *
037: * @author Ole.Matzura
038: */
039:
040: public class SimpleLoadStrategy extends AbstractLoadStrategy {
041: private static final int DEFAULT_TEST_DELAY = 1000;
042:
043: private static final float DEFAULT_RANDOM_FACTOR = 0.5F;
044:
045: public static final String STRATEGY_TYPE = "Simple";
046:
047: private int testDelay = DEFAULT_TEST_DELAY;
048: private float randomFactor = DEFAULT_RANDOM_FACTOR;
049:
050: private JPanel configPanel;
051: private JTextField testDelayField;
052: private JTextField randomFactorField;
053:
054: public SimpleLoadStrategy(XmlObject config) {
055: super (STRATEGY_TYPE);
056:
057: if (config != null) {
058: XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader(
059: config);
060: testDelay = reader.readInt("testDelay", DEFAULT_TEST_DELAY);
061: randomFactor = reader.readFloat("randomFactor",
062: DEFAULT_RANDOM_FACTOR);
063: }
064: }
065:
066: public XmlObject getConfig() {
067: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
068: builder.add("testDelay", testDelay);
069: builder.add("randomFactor", randomFactor);
070: return builder.finish();
071: }
072:
073: public void beforeTestCase(LoadTestRunner loadTestRunner,
074: LoadTestRunContext context, TestRunner testRunner,
075: TestRunContext runContext) {
076: int delay = calculateDelay(testDelay);
077: if (delay == 0)
078: return;
079: try {
080: Thread.sleep(delay);
081: } catch (InterruptedException e) {
082: SoapUI.logError(e);
083: }
084: }
085:
086: public int calculateDelay(int delay) {
087: if (delay == 0 || randomFactor == 0)
088: return delay;
089:
090: int fixDelay = (int) ((float) delay * (1 - randomFactor));
091: int randDelay = (int) (randomFactor == 0 ? 0
092: : (float) (delay - fixDelay) * Math.random());
093: return fixDelay + randDelay;
094: }
095:
096: public JComponent getConfigurationPanel() {
097: if (configPanel == null) {
098: ButtonBarBuilder builder = new ButtonBarBuilder();
099:
100: testDelayField = new JTextField(5);
101: UISupport.setPreferredHeight(testDelayField, 18);
102: testDelayField.setHorizontalAlignment(JTextField.RIGHT);
103: testDelayField.setText(String.valueOf(testDelay));
104: testDelayField
105: .setToolTipText("Sets the delay between each test run in milliseconds");
106: testDelayField.getDocument().addDocumentListener(
107: new ConfigDocumentListener());
108:
109: builder.addFixed(new JLabel("Test Delay"));
110: builder.addRelatedGap();
111:
112: builder.addFixed(testDelayField);
113: builder.addRelatedGap();
114:
115: randomFactorField = new JTextField(4);
116: UISupport.setPreferredHeight(randomFactorField, 18);
117: randomFactorField.setHorizontalAlignment(JTextField.RIGHT);
118: randomFactorField.setText(String.valueOf(randomFactor));
119: randomFactorField
120: .setToolTipText("Specifies the relativt amount of randomization for delay (0 = no random, 1 = all random)");
121: randomFactorField.getDocument().addDocumentListener(
122: new ConfigDocumentListener());
123:
124: builder.addFixed(new JLabel("Random"));
125: builder.addRelatedGap();
126: builder.addFixed(randomFactorField);
127:
128: configPanel = builder.getPanel();
129: }
130:
131: return configPanel;
132: }
133:
134: private final class ConfigDocumentListener extends
135: DocumentListenerAdapter {
136: public void update(Document document) {
137: try {
138: if (document == testDelayField.getDocument())
139: testDelay = Integer.parseInt(testDelayField
140: .getText());
141: if (document == randomFactorField.getDocument())
142: randomFactor = Float.parseFloat(randomFactorField
143: .getText().replace(',', '.'));
144:
145: notifyConfigurationChanged();
146: } catch (NumberFormatException e) {
147: }
148: }
149: }
150:
151: /**
152: * Factory for SimpleLoadStrategy class
153: *
154: * @author Ole.Matzura
155: */
156:
157: public static class Factory implements LoadStrategyFactory {
158: public String getType() {
159: return STRATEGY_TYPE;
160: }
161:
162: public LoadStrategy build(XmlObject config) {
163: return new SimpleLoadStrategy(config);
164: }
165:
166: public LoadStrategy create() {
167: return new SimpleLoadStrategy(null);
168: }
169: }
170: }
|