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.teststeps;
014:
015: import javax.swing.ImageIcon;
016:
017: import org.apache.xmlbeans.XmlObject;
018:
019: import com.eviware.soapui.config.RequestAssertionConfig;
020: import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
021: import com.eviware.soapui.impl.wsdl.support.assertions.Assertable;
022: import com.eviware.soapui.impl.wsdl.support.assertions.Assertable.AssertionStatus;
023: import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError;
024: import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionException;
025: import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry;
026: import com.eviware.soapui.model.iface.SubmitContext;
027: import com.eviware.soapui.model.settings.Settings;
028: import com.eviware.soapui.model.support.AbstractModelItem;
029: import com.eviware.soapui.support.UISupport;
030:
031: /**
032: * Base class for WsdlAssertions
033: *
034: * @author Ole.Matzura
035: */
036:
037: public abstract class WsdlMessageAssertion extends AbstractModelItem {
038: private RequestAssertionConfig assertionConfig;
039: private final Assertable assertable;
040: private AssertionStatus assertionStatus = AssertionStatus.UNKNOWN;
041: private com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] assertionErrors;
042: private ImageIcon validIcon;
043: private ImageIcon failedIcon;
044: private ImageIcon unknownIcon;
045:
046: public final static String STATUS_PROPERTY = WsdlMessageAssertion.class
047: .getName()
048: + "@status";
049: public final static String ERRORS_PROPERTY = WsdlMessageAssertion.class
050: .getName()
051: + "@errors";
052: public final static String CONFIGURATION_PROPERTY = WsdlMessageAssertion.class
053: .getName()
054: + "@configuration";
055: private final boolean cloneable;
056: private final boolean configurable;
057:
058: protected WsdlMessageAssertion(
059: RequestAssertionConfig assertionConfig,
060: Assertable modelItem, boolean cloneable,
061: boolean configurable) {
062: this .assertionConfig = assertionConfig;
063: this .assertable = modelItem;
064: this .cloneable = cloneable;
065: this .configurable = configurable;
066:
067: validIcon = UISupport.createImageIcon("/valid_assertion.gif");
068: failedIcon = UISupport.createImageIcon("/failed_assertion.gif");
069: unknownIcon = UISupport
070: .createImageIcon("/unknown_assertion.gif");
071: }
072:
073: public XmlObject getConfiguration() {
074: if (null == assertionConfig.getConfiguration()) {
075: assertionConfig.addNewConfiguration();
076: }
077:
078: return assertionConfig.getConfiguration();
079: }
080:
081: public void setConfiguration(XmlObject configuration) {
082: XmlObject oldConfig = assertionConfig.getConfiguration();
083: assertionConfig.setConfiguration(configuration);
084: notifyPropertyChanged(
085: WsdlMessageAssertion.CONFIGURATION_PROPERTY, oldConfig,
086: configuration);
087: }
088:
089: public String getName() {
090: return assertionConfig.isSetName() ? assertionConfig.getName()
091: : WsdlAssertionRegistry.getInstance()
092: .getAssertionNameForType(
093: assertionConfig.getType());
094: }
095:
096: public void setName(String name) {
097: String old = getName();
098: assertionConfig.setName(name);
099: notifyPropertyChanged(NAME_PROPERTY, old, name);
100: }
101:
102: public AssertionStatus getStatus() {
103: return assertionStatus;
104: }
105:
106: public AssertionError[] getErrors() {
107: return assertionErrors;
108: }
109:
110: public AssertionStatus assertResponse(
111: WsdlMessageExchange messageExchange, SubmitContext context) {
112: AssertionStatus oldStatus = assertionStatus;
113: AssertionError[] oldErrors = getErrors();
114: ImageIcon oldIcon = getIcon();
115:
116: if (!messageExchange.hasResponse()) {
117: if (messageExchange.getOperation().isOneWay()) {
118: assertionStatus = AssertionStatus.VALID;
119: assertionErrors = null;
120: } else {
121: assertionStatus = AssertionStatus.FAILED;
122: assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError(
123: "null/empty response") };
124: }
125: } else {
126: try {
127: internalAssertResponse(messageExchange, context);
128: assertionStatus = AssertionStatus.VALID;
129: assertionErrors = null;
130: } catch (AssertionException e) {
131: assertionStatus = AssertionStatus.FAILED;
132: assertionErrors = e.getErrors();
133: } catch (Throwable e) {
134: assertionStatus = AssertionStatus.FAILED;
135: assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError(
136: e.getMessage()) };
137: }
138: }
139:
140: notifyPropertyChanged(STATUS_PROPERTY, oldStatus,
141: assertionStatus);
142: notifyPropertyChanged(ERRORS_PROPERTY, oldErrors,
143: assertionErrors);
144: notifyPropertyChanged(ICON_PROPERTY, oldIcon, getIcon());
145:
146: return assertionStatus;
147: }
148:
149: protected abstract String internalAssertResponse(
150: WsdlMessageExchange messageExchange, SubmitContext context)
151: throws AssertionException;
152:
153: public AssertionStatus assertRequest(
154: WsdlMessageExchange messageExchange, SubmitContext context) {
155: AssertionStatus oldStatus = assertionStatus;
156: ImageIcon oldIcon = getIcon();
157:
158: if (!messageExchange.hasRequest(true)) {
159: assertionStatus = AssertionStatus.FAILED;
160: assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError(
161: "null/empty response") };
162: } else {
163: try {
164: internalAssertRequest(messageExchange, context);
165: assertionStatus = AssertionStatus.VALID;
166: assertionErrors = null;
167: } catch (AssertionException e) {
168: assertionStatus = AssertionStatus.FAILED;
169: assertionErrors = e.getErrors();
170: } catch (Throwable e) {
171: assertionStatus = AssertionStatus.FAILED;
172: assertionErrors = new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError[] { new com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError(
173: e.getMessage()) };
174: }
175: }
176:
177: notifyPropertyChanged(STATUS_PROPERTY, oldStatus,
178: assertionStatus);
179: notifyPropertyChanged(ICON_PROPERTY, oldIcon, getIcon());
180:
181: return assertionStatus;
182: }
183:
184: protected abstract String internalAssertRequest(
185: WsdlMessageExchange messageExchange, SubmitContext context)
186: throws AssertionException;
187:
188: public boolean isConfigurable() {
189: return configurable;
190: }
191:
192: public boolean isClonable() {
193: return cloneable;
194: }
195:
196: public boolean configure() {
197: return true;
198: }
199:
200: public String getDescription() {
201: return getConfig().getDescription();
202: }
203:
204: public ImageIcon getIcon() {
205: switch (getStatus()) {
206: case FAILED:
207: return failedIcon;
208: case UNKNOWN:
209: return unknownIcon;
210: case VALID:
211: return validIcon;
212: }
213:
214: return null;
215: }
216:
217: public void updateConfig(RequestAssertionConfig config) {
218: this .assertionConfig = config;
219: }
220:
221: public RequestAssertionConfig getConfig() {
222: return assertionConfig;
223: }
224:
225: public Settings getSettings() {
226: return assertable.getTestStep().getSettings();
227: }
228:
229: public void release() {
230: }
231:
232: public Assertable getAssertable() {
233: return assertable;
234: }
235: }
|