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.awt.event.ActionEvent;
016: import java.beans.PropertyChangeEvent;
017: import java.beans.PropertyChangeListener;
018: import java.beans.PropertyChangeSupport;
019:
020: import javax.swing.AbstractAction;
021: import javax.swing.Action;
022: import javax.swing.ImageIcon;
023:
024: import org.apache.log4j.Logger;
025: import org.apache.xmlbeans.XmlObject;
026:
027: import com.eviware.soapui.config.LoadTestAssertionConfig;
028: import com.eviware.soapui.impl.wsdl.loadtest.LoadTestAssertion;
029: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
030: import com.eviware.soapui.impl.wsdl.support.Configurable;
031: import com.eviware.soapui.model.support.ModelSupport;
032: import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
033: import com.eviware.soapui.model.testsuite.LoadTestRunContext;
034: import com.eviware.soapui.model.testsuite.LoadTestRunner;
035: import com.eviware.soapui.model.testsuite.TestStep;
036: import com.eviware.soapui.support.UISupport;
037:
038: /**
039: * Base class for LoadTestAssertions
040: *
041: * @author Ole.Matzura
042: */
043:
044: public abstract class AbstractLoadTestAssertion implements
045: LoadTestAssertion {
046: private LoadTestAssertionConfig assertionConfig;
047: @SuppressWarnings("unused")
048: private final static Logger log = Logger
049: .getLogger(AbstractLoadTestAssertion.class);
050: private ImageIcon icon;
051: private final WsdlLoadTest loadTest;
052: private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
053: this );
054: private String testStepName;
055: private TestStep testStep;
056: private TestStepPropertyChangeListener testStepPropertyChangeListener = new TestStepPropertyChangeListener();
057: private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
058:
059: protected static final String TEST_STEP_ELEMENT = "test-step";
060: protected static final String TEST_STEP_FIELD = "TestStep";
061:
062: public AbstractLoadTestAssertion(
063: LoadTestAssertionConfig assertionConfig,
064: WsdlLoadTest loadTest) {
065: this .assertionConfig = assertionConfig;
066: this .loadTest = loadTest;
067:
068: loadTest.getTestCase().getTestSuite().addTestSuiteListener(
069: testSuiteListener);
070: }
071:
072: public void initIcon(String url) {
073: icon = UISupport.createImageIcon(url);
074: }
075:
076: public LoadTestAssertionConfig getConfiguration() {
077: return assertionConfig;
078: }
079:
080: public void updateConfiguration(
081: LoadTestAssertionConfig configuration) {
082: assertionConfig = configuration;
083: }
084:
085: protected void setConfiguration(XmlObject configuration) {
086: XmlObject oldConfig = assertionConfig.getConfiguration();
087: assertionConfig.setConfiguration(configuration);
088: propertyChangeSupport.firePropertyChange(
089: AbstractLoadTestAssertion.CONFIGURATION_PROPERTY,
090: oldConfig, configuration);
091: }
092:
093: public String getName() {
094: return assertionConfig.isSetName() ? assertionConfig.getName()
095: : assertionConfig.getType();
096: }
097:
098: public void setName(String name) {
099: String old = getName();
100: assertionConfig.setName(name);
101: propertyChangeSupport.firePropertyChange(NAME_PROPERTY, old,
102: name);
103: }
104:
105: public WsdlLoadTest getLoadTest() {
106: return loadTest;
107: }
108:
109: public class RenameAssertionAction extends AbstractAction {
110: public RenameAssertionAction() {
111: super ("Rename");
112: putValue(Action.SHORT_DESCRIPTION, "Renames this assertion");
113: }
114:
115: public void actionPerformed(ActionEvent e) {
116: String name = UISupport.prompt(
117: "Specify name for this assertion",
118: "Rename Assertion", AbstractLoadTestAssertion.this
119: .getName());
120: if (name == null
121: || name.equals(AbstractLoadTestAssertion.this
122: .getName()))
123: return;
124:
125: setName(name);
126: }
127: }
128:
129: public class ConfigureAssertionAction extends AbstractAction {
130: public ConfigureAssertionAction() {
131: super ("Configure");
132: putValue(Action.SHORT_DESCRIPTION,
133: "Configures this assertion");
134: }
135:
136: public void actionPerformed(ActionEvent e) {
137: ((Configurable) AbstractLoadTestAssertion.this ).configure();
138: }
139: }
140:
141: public ImageIcon getIcon() {
142: return icon;
143: }
144:
145: public void addPropertyChangeListener(
146: PropertyChangeListener listener) {
147: propertyChangeSupport.addPropertyChangeListener(listener);
148: }
149:
150: public void addPropertyChangeListener(String propertyName,
151: PropertyChangeListener listener) {
152: propertyChangeSupport.addPropertyChangeListener(propertyName,
153: listener);
154: }
155:
156: public void removePropertyChangeListener(
157: PropertyChangeListener listener) {
158: propertyChangeSupport.removePropertyChangeListener(listener);
159: }
160:
161: public void removePropertyChangeListener(String propertyName,
162: PropertyChangeListener listener) {
163: propertyChangeSupport.removePropertyChangeListener(
164: propertyName, listener);
165: }
166:
167: protected String returnErrorOrFail(String message, int maxErrors,
168: LoadTestRunner testRunner, LoadTestRunContext context) {
169: String propertyKey = getClass().getName() + hashCode();
170: Long errorCount = (Long) context.getProperty(propertyKey);
171:
172: if (errorCount == null) {
173: errorCount = 1L;
174: } else {
175: errorCount = new Long(errorCount.longValue() + 1);
176: }
177:
178: if (maxErrors >= 0 && errorCount >= maxErrors) {
179: testRunner.fail("Maximum number of errors [" + maxErrors
180: + "] for assertion [" + getName() + "] exceeded");
181: }
182:
183: context.setProperty(propertyKey, errorCount);
184:
185: return message;
186: }
187:
188: public String getTargetStep() {
189: return testStepName;
190: }
191:
192: public void setTargetStep(String name) {
193: testStepName = name;
194: initTestStep();
195: }
196:
197: abstract protected void updateConfiguration();
198:
199: protected boolean targetStepMatches(TestStep testStep) {
200: return testStepName == null
201: || testStepName.equals(ANY_TEST_STEP)
202: || testStep.getName().equals(testStepName);
203: }
204:
205: protected String[] getTargetStepOptions(boolean includeAll) {
206: if (includeAll)
207: return ModelSupport.getNames(new String[] { ANY_TEST_STEP,
208: ALL_TEST_STEPS }, getLoadTest().getTestCase()
209: .getTestStepList());
210: else
211: return ModelSupport.getNames(
212: new String[] { ANY_TEST_STEP }, getLoadTest()
213: .getTestCase().getTestStepList());
214: }
215:
216: private void initTestStep() {
217: if (testStep != null) {
218: testStep
219: .removePropertyChangeListener(testStepPropertyChangeListener);
220: }
221:
222: testStep = getLoadTest().getTestCase().getTestStepByName(
223: testStepName);
224: if (testStep != null) {
225: testStep.addPropertyChangeListener(TestStep.NAME_PROPERTY,
226: testStepPropertyChangeListener);
227: }
228: }
229:
230: public void release() {
231: if (testStep != null) {
232: testStep
233: .removePropertyChangeListener(testStepPropertyChangeListener);
234: loadTest.getTestCase().getTestSuite()
235: .removeTestSuiteListener(testSuiteListener);
236: }
237: }
238:
239: private final class InternalTestSuiteListener extends
240: TestSuiteListenerAdapter {
241: public void testStepRemoved(TestStep removedTestStep, int index) {
242: if (removedTestStep.getName().equals(testStepName)
243: && removedTestStep.getTestCase() == testStep
244: .getTestCase()) {
245: testStepName = ANY_TEST_STEP;
246: updateConfiguration();
247: }
248: }
249: }
250:
251: private final class TestStepPropertyChangeListener implements
252: PropertyChangeListener {
253: public void propertyChange(PropertyChangeEvent evt) {
254: testStepName = evt.getNewValue().toString();
255: updateConfiguration();
256: }
257: }
258: }
|