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.Arrays;
016:
017: import org.apache.xmlbeans.XmlObject;
018:
019: import com.eviware.soapui.config.LoadTestAssertionConfig;
020: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
021: import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics;
022: import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics.Statistic;
023: import com.eviware.soapui.impl.wsdl.support.Configurable;
024: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
025: import com.eviware.soapui.model.testsuite.LoadTestRunContext;
026: import com.eviware.soapui.model.testsuite.LoadTestRunner;
027: import com.eviware.soapui.model.testsuite.TestRunContext;
028: import com.eviware.soapui.model.testsuite.TestRunner;
029: import com.eviware.soapui.model.testsuite.TestStep;
030: import com.eviware.soapui.model.testsuite.TestStepResult;
031: import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
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 status of a teststep
044: *
045: * @author Ole.Matzura
046: */
047:
048: public class TestStepStatusAssertion 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 MINIMUM_REQUESTS_FIELD = "Minimum Requests";
053: private static final String MIN_REQUESTS_ELEMENT = "min-requests";
054: private static final String MAX_ERRORS_ELEMENT = "max-errors";
055: private static final String MAX_ERRORS_FIELD = "Max Errors";
056:
057: private int minRequests;
058: private int maxErrors;
059: private XFormDialog dialog;
060: public static final String STEP_STATUS_TYPE = "Step Status";
061:
062: public TestStepStatusAssertion(
063: LoadTestAssertionConfig assertionConfig,
064: WsdlLoadTest loadTest) {
065: super (assertionConfig, loadTest);
066:
067: init(assertionConfig);
068: initIcon("/status_loadtest_assertion.gif");
069: }
070:
071: private void init(LoadTestAssertionConfig assertionConfig) {
072: XmlObject configuration = assertionConfig.getConfiguration();
073: XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader(
074: configuration);
075:
076: setName(reader.readString(TestStepStatusAssertion.NAME_ELEMENT,
077: "Step Status"));
078: minRequests = reader.readInt(
079: TestStepStatusAssertion.MIN_REQUESTS_ELEMENT, 0);
080: setTargetStep(reader.readString(
081: TestStepStatusAssertion.TEST_STEP_ELEMENT,
082: ANY_TEST_STEP));
083: maxErrors = reader.readInt(MAX_ERRORS_ELEMENT, -1);
084: }
085:
086: public String getDescription() {
087: return "testStep: " + getTargetStep() + ", minRequests: "
088: + minRequests + ", maxErrors: " + maxErrors;
089: }
090:
091: public String assertResult(LoadTestRunner loadTestRunner,
092: LoadTestRunContext context, TestStepResult result,
093: TestRunner testRunner, TestRunContext runContext) {
094: WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner
095: .getLoadTest();
096: LoadTestStatistics statisticsModel = loadTest
097: .getStatisticsModel();
098:
099: TestStep step = result.getTestStep();
100:
101: if (targetStepMatches(step)) {
102: int index = step.getTestCase().getIndexOfTestStep(step);
103:
104: if (statisticsModel.getStatistic(index, Statistic.COUNT) >= minRequests
105: && result.getStatus() == TestStepStatus.FAILED) {
106: return returnErrorOrFail("TestStep [" + step.getName()
107: + "] result status is "
108: + result.getStatus().toString() + "; "
109: + Arrays.toString(result.getMessages()),
110: maxErrors, loadTestRunner, context);
111: } else
112: return null;
113: }
114:
115: return null;
116: }
117:
118: public String assertResults(LoadTestRunner loadTestRunner,
119: LoadTestRunContext context, TestRunner testRunner,
120: TestRunContext runContext) {
121: return null;
122: }
123:
124: public boolean configure() {
125: if (dialog == null) {
126: buildDialog();
127: }
128:
129: StringToStringMap values = new StringToStringMap();
130:
131: values.put(NAME_FIELD, getName());
132: values.put(MINIMUM_REQUESTS_FIELD, String.valueOf(minRequests));
133: values.put(TEST_STEP_FIELD, getTargetStep());
134: values.put(MAX_ERRORS_FIELD, String.valueOf(maxErrors));
135:
136: dialog.setOptions(TestStepStatusAssertion.TEST_STEP_FIELD,
137: getTargetStepOptions(false));
138: values = dialog.show(values);
139:
140: if (dialog.getReturnValue() == XFormDialog.OK_OPTION) {
141: try {
142: minRequests = Integer.parseInt(values
143: .get(MINIMUM_REQUESTS_FIELD));
144: maxErrors = Integer.parseInt(values
145: .get(MAX_ERRORS_FIELD));
146: setName(values.get(NAME_FIELD));
147: setTargetStep(values.get(TEST_STEP_FIELD));
148: } catch (Exception e) {
149: UISupport.showErrorMessage(e.getMessage());
150: }
151:
152: updateConfiguration();
153: return true;
154: }
155:
156: return false;
157: }
158:
159: private void buildDialog() {
160: XFormDialogBuilder builder = XFormFactory
161: .createDialogBuilder("TestStep Status Assertion");
162: XForm form = builder.createForm("Basic");
163:
164: form.addTextField(NAME_FIELD, "Name of this assertion",
165: FieldType.TEXT);
166: form.addTextField(MINIMUM_REQUESTS_FIELD,
167: "Minimum number of runs before asserting",
168: FieldType.TEXT);
169: form.addTextField(MAX_ERRORS_FIELD,
170: "Maximum number of errors before failing",
171: FieldType.TEXT);
172: form.addComboBox(TEST_STEP_FIELD, new String[0],
173: "TestStep to assert");
174:
175: dialog = builder
176: .buildDialog(
177: builder
178: .buildOkCancelHelpActions(HelpUrls.STEP_STATUS_LOAD_TEST_ASSERTION_HELP_URL),
179: "Specify options for this TestStep Status Assertion",
180: UISupport.OPTIONS_ICON);
181: }
182:
183: protected void updateConfiguration() {
184: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
185:
186: builder.add(NAME_ELEMENT, getName());
187: builder.add(MIN_REQUESTS_ELEMENT, minRequests);
188: builder.add(TEST_STEP_ELEMENT, getTargetStep());
189: builder.add(MAX_ERRORS_ELEMENT, maxErrors);
190:
191: setConfiguration(builder.finish());
192: }
193: }
|