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