Java tutorial
/* * soapui, copyright (C) 2006 eviware.com * * SoapUI is free software; you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * SoapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details at gnu.org. */ package com.eviware.soapui.impl.wsdl.teststeps; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.swing.ImageIcon; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.eviware.soapui.SoapUI; import com.eviware.soapui.config.AttachmentConfig; import com.eviware.soapui.config.CallConfig; import com.eviware.soapui.config.RequestAssertionConfig; import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl; import com.eviware.soapui.impl.wsdl.WsdlRequest; import com.eviware.soapui.impl.wsdl.WsdlSubmitContext; import com.eviware.soapui.impl.wsdl.actions.request.DeleteRequestAction; import com.eviware.soapui.impl.wsdl.actions.request.RenameRequestAction; import com.eviware.soapui.impl.wsdl.panels.teststeps.WsdlTestRequestPanelBuilder; import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse; import com.eviware.soapui.impl.wsdl.teststeps.WsdlRequestAssertion.AssertionStatus; import com.eviware.soapui.impl.wsdl.teststeps.actions.AddAssertionAction; import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry; import com.eviware.soapui.model.PanelBuilder; import com.eviware.soapui.model.iface.Operation; import com.eviware.soapui.model.iface.SubmitContext; import com.eviware.soapui.model.testsuite.TestCase; import com.eviware.soapui.monitor.TestMonitor; import com.eviware.soapui.support.UISupport; /** * WsdlRequest extension that adds WsdlAssertions * * @author Ole.Matzura */ public class WsdlTestRequest extends WsdlRequest implements PropertyChangeListener { public static final String RESPONSE_PROPERTY = WsdlTestRequest.class.getName() + "@response"; public static final String STATUS_PROPERTY = WsdlTestRequest.class.getName() + "@status"; private List<WsdlRequestAssertion> assertions = new ArrayList<WsdlRequestAssertion>(); private final static Log log = LogFactory.getLog(WsdlTestRequest.class); private ImageIcon validRequestIcon; private ImageIcon failedRequestIcon; private ImageIcon disabledRequestIcon; private ImageIcon unknownRequestIcon; private WsdlRequestAssertion.AssertionStatus currentStatus; private final WsdlTestRequestStep testStep; private List<WsdlTestRequestListener> testRequestListeners = new ArrayList<WsdlTestRequestListener>(); public WsdlTestRequest(Operation operation, CallConfig callConfig, WsdlTestRequestStep testStep) { super(operation, callConfig); setSettings(new XmlBeansSettingsImpl(this, testStep.getSettings(), callConfig.getSettings())); this.testStep = testStep; initAssertions(); } protected PanelBuilder initPanelBuilder() { return new WsdlTestRequestPanelBuilder(this); } public TestCase getTestCase() { return testStep.getTestCase(); } public WsdlTestRequestStep getRequestStep() { return testStep; } protected void initIcons() { super.initIcons(); validRequestIcon = UISupport.createImageIcon("/valid_request.gif"); failedRequestIcon = UISupport.createImageIcon("/invalid_request.gif"); unknownRequestIcon = UISupport.createImageIcon("/unknown_request.gif"); disabledRequestIcon = UISupport.createImageIcon("/disabled_request.gif"); } protected void initActions() { addAction(new RenameRequestAction(this)); addAction(new DeleteRequestAction(this)); addAction(new AddAssertionAction(this)); } private void initAssertions() { List<RequestAssertionConfig> assertionConfigs = getConfig().getAssertionList(); for (RequestAssertionConfig rac : assertionConfigs) { addWsdlAssertion(rac); } } private WsdlRequestAssertion addWsdlAssertion(RequestAssertionConfig config) { try { WsdlRequestAssertion assertion = WsdlAssertionRegistry.getInstance().buildAssertion(config, this); assertions.add(assertion); assertion.addPropertyChangeListener(this); return assertion; } catch (Exception e) { e.printStackTrace(); return null; } } public int getAssertionCount() { return assertions.size(); } public WsdlRequestAssertion getAssertionAt(int c) { return assertions.get(c); } public void setResponse(WsdlResponse response, SubmitContext context) { super.setResponse(response, context); assertRequest(context); } public void assertRequest(SubmitContext context) { PropertyChangeNotifier notifier = new PropertyChangeNotifier(); // assert! for (Iterator<WsdlRequestAssertion> iter = assertions.iterator(); iter.hasNext();) { iter.next().assertResponse(this, context); } notifier.notifyChange(); } private class PropertyChangeNotifier { private AssertionStatus oldStatus; private ImageIcon oldIcon; public PropertyChangeNotifier() { oldStatus = getAssertionStatus(); oldIcon = getIcon(); } public void notifyChange() { AssertionStatus newStatus = getAssertionStatus(); ImageIcon newIcon = getIcon(); if (oldStatus != newStatus) notifyPropertyChanged(STATUS_PROPERTY, oldStatus, newStatus); if (oldIcon != newIcon) notifyPropertyChanged(ICON_PROPERTY, oldIcon, getIcon()); } } public WsdlRequestAssertion addAssertion(String assertionName) { PropertyChangeNotifier notifier = new PropertyChangeNotifier(); RequestAssertionConfig assertionConfig = (RequestAssertionConfig) getConfig().addNewAssertion(); assertionConfig.setType(assertionName); try { WsdlRequestAssertion assertion = addWsdlAssertion(assertionConfig); fireAssertionAdded(assertion); if (getResponse() != null) { assertion.assertResponse(this, new WsdlSubmitContext(testStep)); notifier.notifyChange(); } return assertion; } catch (Exception e) { e.printStackTrace(); return null; } } private void fireAssertionAdded(WsdlRequestAssertion assertion) { WsdlTestRequestListener[] listeners = testRequestListeners .toArray(new WsdlTestRequestListener[testRequestListeners.size()]); for (int c = 0; c < listeners.length; c++) { listeners[c].assertionAdded(assertion); } } private void fireAssertionRemoved(WsdlRequestAssertion assertion) { WsdlTestRequestListener[] listeners = testRequestListeners .toArray(new WsdlTestRequestListener[testRequestListeners.size()]); for (int c = 0; c < listeners.length; c++) { listeners[c].assertionRemoved(assertion); } } public void removeAssertion(WsdlRequestAssertion assertion) { int ix = assertions.indexOf(assertion); if (ix == -1) { throw new RuntimeException("assertion [" + assertion.getName() + "] not available " + "in test request [" + getName() + "]"); } PropertyChangeNotifier notifier = new PropertyChangeNotifier(); assertion.removePropertyChangeListener(this); assertions.remove(ix); try { fireAssertionRemoved(assertion); } finally { assertion.release(); getConfig().removeAssertion(ix); notifier.notifyChange(); } } public AssertionStatus getAssertionStatus() { currentStatus = AssertionStatus.NONE; int c = getAssertionCount(); if (c == 0) return currentStatus; currentStatus = AssertionStatus.VALID; for (c = 0; c < getAssertionCount(); c++) { AssertionStatus status = getAssertionAt(c).getStatus(); if (status == AssertionStatus.FAILED) { currentStatus = AssertionStatus.FAILED; break; } else if (status == AssertionStatus.UNKNOWN) { currentStatus = AssertionStatus.UNKNOWN; break; } } return currentStatus; } public ImageIcon getIcon() { TestMonitor testMonitor = SoapUI.getTestMonitor(); if (testMonitor != null && testMonitor.hasRunningLoadTest(testStep.getTestCase())) return disabledRequestIcon; ImageIcon icon = getIconManager().getIcon(); if (icon == getIconManager().getRequestIcon()) { AssertionStatus status = getAssertionStatus(); if (status == AssertionStatus.VALID) return validRequestIcon; else if (status == AssertionStatus.FAILED) return failedRequestIcon; else if (status == AssertionStatus.UNKNOWN) return unknownRequestIcon; } return icon; } public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equals(WsdlRequestAssertion.CONFIGURATION_PROPERTY)) assertRequest(new WsdlSubmitContext(testStep)); } public void addWsdlTestRequestListener(WsdlTestRequestListener listener) { testRequestListeners.add(listener); } public void removeWsdlTestRequestListener(WsdlTestRequestListener listener) { testRequestListeners.remove(listener); } /** * Listener for assertion events.. should be added to core model * * @author Ole.Matzura */ public interface WsdlTestRequestListener { public void assertionAdded(WsdlRequestAssertion assertion); public void assertionRemoved(WsdlRequestAssertion assertion); } /** * Called when a testrequest is moved in a testcase */ public void updateConfig(CallConfig request) { super.updateConfig(request); List<RequestAssertionConfig> assertionConfigs = getConfig().getAssertionList(); for (int i = 0; i < assertionConfigs.size(); i++) { RequestAssertionConfig config = assertionConfigs.get(i); assertions.get(i).updateConfig(config); } List<AttachmentConfig> attachmentConfigs = getConfig().getAttachmentList(); for (int i = 0; i < attachmentConfigs.size(); i++) { AttachmentConfig config = attachmentConfigs.get(i); attachments.get(i).updateConfig(config); } } public void release() { super.release(); for (WsdlRequestAssertion assertion : assertions) assertion.release(); } }