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