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.assertions;
014:
015: import org.apache.xmlbeans.XmlObject;
016:
017: import com.eviware.soapui.config.LoadTestAssertionConfig;
018: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
019: import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics;
020: import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics.Statistic;
021: import com.eviware.soapui.impl.wsdl.support.Configurable;
022: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
023: import com.eviware.soapui.model.testsuite.LoadTestRunContext;
024: import com.eviware.soapui.model.testsuite.LoadTestRunner;
025: import com.eviware.soapui.model.testsuite.TestRunContext;
026: import com.eviware.soapui.model.testsuite.TestRunner;
027: import com.eviware.soapui.model.testsuite.TestStep;
028: import com.eviware.soapui.model.testsuite.TestStepResult;
029: import com.eviware.soapui.support.UISupport;
030: import com.eviware.soapui.support.types.StringToStringMap;
031: import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
032: import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
033: import com.eviware.x.form.XForm;
034: import com.eviware.x.form.XFormDialog;
035: import com.eviware.x.form.XFormDialogBuilder;
036: import com.eviware.x.form.XFormFactory;
037: import com.eviware.x.form.XForm.FieldType;
038:
039: /**
040: * LoadTestAssertion for asserting the TPS of a teststep
041: *
042: * @author Ole.Matzura
043: */
044:
045: public class TestStepTpsAssertion extends AbstractLoadTestAssertion
046: implements Configurable {
047: private static final String NAME_FIELD = "Name";
048: private static final String NAME_ELEMENT = "name";
049: private static final String MIN_VALUE_ELEMENT = "min-value";
050: private static final String MIN_REQUESTS_ELEMENT = "min-requests";
051: private static final String MIN_VALUE_FIELD = "Minimum TPS";
052: private static final String MINIMUM_REQUESTS_FIELD = "Minimum Requests";
053: private static final String MAX_ERRORS_ELEMENT = "max-errors";
054: private static final String MAX_ERRORS_FIELD = "Max Errors";
055:
056: private int minRequests;
057: private int minValue;
058: private int maxErrors;
059: private XFormDialog dialog;
060: public static final String STEP_TPS_TYPE = "Step TPS";
061:
062: public TestStepTpsAssertion(
063: LoadTestAssertionConfig assertionConfig,
064: WsdlLoadTest loadTest) {
065: super (assertionConfig, loadTest);
066:
067: init(assertionConfig);
068: initIcon("/tps_loadtest_assertion.gif");
069: }
070:
071: private void init(LoadTestAssertionConfig assertionConfig) {
072: XmlObject configuration = assertionConfig.getConfiguration();
073:
074: XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader(
075: configuration);
076: setName(reader.readString(TestStepTpsAssertion.NAME_ELEMENT,
077: "Step TPS"));
078: minRequests = reader.readInt(
079: TestStepTpsAssertion.MIN_REQUESTS_ELEMENT, 100);
080: minValue = reader.readInt(
081: TestStepTpsAssertion.MIN_VALUE_ELEMENT, 10);
082: setTargetStep(reader.readString(
083: TestStepTpsAssertion.TEST_STEP_ELEMENT,
084: TestStepTpsAssertion.ANY_TEST_STEP));
085: maxErrors = reader.readInt(MAX_ERRORS_ELEMENT, -1);
086: }
087:
088: public String assertResult(LoadTestRunner loadTestRunner,
089: LoadTestRunContext context, TestStepResult result,
090: TestRunner testRunner, TestRunContext runContext) {
091: TestStep step = result.getTestStep();
092: if (targetStepMatches(step)) {
093: WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner
094: .getLoadTest();
095: LoadTestStatistics statisticsModel = loadTest
096: .getStatisticsModel();
097:
098: int index = step.getTestCase().getIndexOfTestStep(step);
099:
100: long tps = statisticsModel.getStatistic(index,
101: Statistic.TPS);
102: if (statisticsModel.getStatistic(index, Statistic.COUNT) >= minRequests
103: && tps < minValue) {
104: return returnErrorOrFail("TPS [" + tps
105: + "] is less than limit [" + minValue + "]",
106: maxErrors, loadTestRunner, context);
107: }
108: }
109:
110: return null;
111: }
112:
113: public String assertResults(LoadTestRunner loadTestRunner,
114: LoadTestRunContext context, TestRunner testRunner,
115: TestRunContext runContext) {
116: if (ALL_TEST_STEPS.equals(getTargetStep())) {
117: WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner
118: .getLoadTest();
119: LoadTestStatistics statisticsModel = loadTest
120: .getStatisticsModel();
121:
122: long tps = statisticsModel.getStatistic(
123: LoadTestStatistics.TOTAL, Statistic.TPS);
124:
125: if (statisticsModel.getStatistic(LoadTestStatistics.TOTAL,
126: Statistic.COUNT) > minRequests
127: && tps < minValue) {
128: return returnErrorOrFail("TPS [" + tps
129: + "] is less than limit [" + minValue + "]",
130: maxErrors, loadTestRunner, context);
131: }
132: }
133:
134: return null;
135: }
136:
137: public String getDescription() {
138: return "testStep: " + getTargetStep() + ", minRequests: "
139: + minRequests + ", minValue: " + minValue
140: + ", maxErrors: " + maxErrors;
141: }
142:
143: public boolean configure() {
144: if (dialog == null) {
145: buildDialog();
146: }
147:
148: StringToStringMap values = new StringToStringMap();
149:
150: values.put(TestStepTpsAssertion.NAME_FIELD, getName());
151: values.put(TestStepTpsAssertion.MINIMUM_REQUESTS_FIELD, String
152: .valueOf(minRequests));
153: values.put(TestStepTpsAssertion.MIN_VALUE_FIELD, String
154: .valueOf(minValue));
155: values.put(TestStepTpsAssertion.TEST_STEP_FIELD,
156: getTargetStep());
157: values.put(TestStepTpsAssertion.MAX_ERRORS_FIELD, String
158: .valueOf(maxErrors));
159:
160: dialog.setOptions(TestStepTpsAssertion.TEST_STEP_FIELD,
161: getTargetStepOptions(true));
162: values = dialog.show(values);
163:
164: if (dialog.getReturnValue() == XFormDialog.OK_OPTION) {
165: try {
166: minRequests = Integer
167: .parseInt(values
168: .get(TestStepTpsAssertion.MINIMUM_REQUESTS_FIELD));
169: minValue = Integer.parseInt(values
170: .get(TestStepTpsAssertion.MIN_VALUE_FIELD));
171: maxErrors = Integer.parseInt(values
172: .get(TestStepTpsAssertion.MAX_ERRORS_FIELD));
173: setTargetStep(values
174: .get(TestStepTpsAssertion.TEST_STEP_FIELD));
175: setName(values.get(TestStepTpsAssertion.NAME_FIELD));
176: } catch (Exception e) {
177: UISupport.showErrorMessage(e.getMessage());
178: }
179:
180: updateConfiguration();
181:
182: return true;
183: }
184:
185: return false;
186: }
187:
188: protected void updateConfiguration() {
189: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
190:
191: builder.add(TestStepTpsAssertion.NAME_ELEMENT, getName());
192: builder.add(TestStepTpsAssertion.MIN_REQUESTS_ELEMENT,
193: minRequests);
194: builder.add(TestStepTpsAssertion.MIN_VALUE_ELEMENT, minValue);
195: builder.add(TestStepTpsAssertion.TEST_STEP_ELEMENT,
196: getTargetStep());
197: builder.add(TestStepTpsAssertion.MAX_ERRORS_ELEMENT, maxErrors);
198:
199: setConfiguration(builder.finish());
200: }
201:
202: private void buildDialog() {
203: XFormDialogBuilder builder = XFormFactory
204: .createDialogBuilder("TestStep TPS Assertion");
205: XForm form = builder.createForm("Basic");
206:
207: form.addTextField(TestStepTpsAssertion.NAME_FIELD,
208: "Name of this assertion", FieldType.TEXT);
209: form.addTextField(TestStepTpsAssertion.MINIMUM_REQUESTS_FIELD,
210: "Minimum steps before asserting", FieldType.TEXT);
211: form.addTextField(TestStepTpsAssertion.MIN_VALUE_FIELD,
212: "Minimum required step TPS", FieldType.TEXT);
213: form.addTextField(TestStepTpsAssertion.MAX_ERRORS_FIELD,
214: "Maximum number of errors before failing",
215: FieldType.TEXT);
216: form.addComboBox(TestStepTpsAssertion.TEST_STEP_FIELD,
217: new String[0], "TestStep to assert");
218:
219: dialog = builder
220: .buildDialog(
221: builder
222: .buildOkCancelHelpActions(HelpUrls.STEP_TPS_LOAD_TEST_ASSERTION_HELP_URL),
223: "Specify options for this TestStep TPS Assertion",
224: UISupport.OPTIONS_ICON);
225: }
226: }
|