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 java.beans.PropertyChangeEvent;
016: import java.beans.PropertyChangeListener;
017: import java.util.Iterator;
018: import java.util.List;
019:
020: import javax.swing.ImageIcon;
021:
022: import com.eviware.soapui.SoapUI;
023: import com.eviware.soapui.config.AttachmentConfig;
024: import com.eviware.soapui.config.CallConfig;
025: import com.eviware.soapui.config.RequestAssertionConfig;
026: import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
027: import com.eviware.soapui.impl.wsdl.WsdlInterface;
028: import com.eviware.soapui.impl.wsdl.WsdlOperation;
029: import com.eviware.soapui.impl.wsdl.WsdlRequest;
030: import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse;
031: import com.eviware.soapui.impl.wsdl.support.assertions.Assertable;
032: import com.eviware.soapui.impl.wsdl.support.assertions.AssertionsListener;
033: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
034: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
035: import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry;
036: import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry.AssertionType;
037: import com.eviware.soapui.model.iface.Submit;
038: import com.eviware.soapui.model.iface.SubmitContext;
039: import com.eviware.soapui.monitor.TestMonitor;
040: import com.eviware.soapui.support.UISupport;
041:
042: /**
043: * WsdlRequest extension that adds WsdlAssertions
044: *
045: * @author Ole.Matzura
046: */
047:
048: public class WsdlTestRequest extends WsdlRequest implements
049: PropertyChangeListener, Assertable {
050: public static final String RESPONSE_PROPERTY = WsdlTestRequest.class
051: .getName()
052: + "@response";
053: public static final String STATUS_PROPERTY = WsdlTestRequest.class
054: .getName()
055: + "@status";
056:
057: private static ImageIcon validRequestIcon;
058: private static ImageIcon failedRequestIcon;
059: private static ImageIcon disabledRequestIcon;
060: private static ImageIcon unknownRequestIcon;
061:
062: private AssertionStatus currentStatus;
063: private final WsdlTestRequestStep testStep;
064:
065: private AssertionsSupport assertionsSupport;
066: private WsdlResponseMessageExchange messageExchange;
067: private final boolean forLoadTest;
068:
069: public WsdlTestRequest(WsdlOperation operation,
070: CallConfig callConfig, WsdlTestRequestStep testStep,
071: boolean forLoadTest) {
072: super (operation, callConfig, forLoadTest);
073: this .forLoadTest = forLoadTest;
074:
075: setSettings(new XmlBeansSettingsImpl(this , testStep
076: .getSettings(), callConfig.getSettings()));
077:
078: this .testStep = testStep;
079:
080: initAssertions();
081: initIcons();
082: }
083:
084: public WsdlTestCase getTestCase() {
085: return testStep.getTestCase();
086: }
087:
088: protected void initIcons() {
089: if (validRequestIcon == null)
090: validRequestIcon = UISupport
091: .createImageIcon("/valid_request.gif");
092:
093: if (failedRequestIcon == null)
094: failedRequestIcon = UISupport
095: .createImageIcon("/invalid_request.gif");
096:
097: if (unknownRequestIcon == null)
098: unknownRequestIcon = UISupport
099: .createImageIcon("/unknown_request.gif");
100:
101: if (disabledRequestIcon == null)
102: disabledRequestIcon = UISupport
103: .createImageIcon("/disabled_request.gif");
104: }
105:
106: protected RequestIconAnimator initIconAnimator() {
107: return new TestRequestIconAnimator();
108: }
109:
110: private void initAssertions() {
111: assertionsSupport = new AssertionsSupport(this , getConfig()
112: .getAssertionList());
113: }
114:
115: public int getAssertionCount() {
116: return assertionsSupport.getAssertionCount();
117: }
118:
119: public WsdlMessageAssertion getAssertionAt(int c) {
120: return assertionsSupport.getAssertionAt(c);
121: }
122:
123: public void setResponse(WsdlResponse response, SubmitContext context) {
124: super .setResponse(response, context);
125:
126: assertResponse(context);
127: }
128:
129: public void assertResponse(SubmitContext context) {
130: PropertyChangeNotifier notifier = new PropertyChangeNotifier();
131: messageExchange = new WsdlResponseMessageExchange(this );
132:
133: // assert!
134: for (Iterator<WsdlMessageAssertion> iter = assertionsSupport
135: .iterator(); iter.hasNext();) {
136: iter.next().assertResponse(messageExchange, context);
137: }
138:
139: notifier.notifyChange();
140: }
141:
142: private class PropertyChangeNotifier {
143: private AssertionStatus oldStatus;
144: private ImageIcon oldIcon;
145:
146: public PropertyChangeNotifier() {
147: oldStatus = getAssertionStatus();
148: oldIcon = getIcon();
149: }
150:
151: public void notifyChange() {
152: AssertionStatus newStatus = getAssertionStatus();
153: ImageIcon newIcon = getIcon();
154:
155: if (oldStatus != newStatus)
156: notifyPropertyChanged(STATUS_PROPERTY, oldStatus,
157: newStatus);
158:
159: if (oldIcon != newIcon)
160: notifyPropertyChanged(ICON_PROPERTY, oldIcon, getIcon());
161: }
162: }
163:
164: public WsdlMessageAssertion addAssertion(String assertionLabel) {
165: PropertyChangeNotifier notifier = new PropertyChangeNotifier();
166:
167: try {
168: RequestAssertionConfig assertionConfig = (RequestAssertionConfig) getConfig()
169: .addNewAssertion();
170: assertionConfig.setType(WsdlAssertionRegistry.getInstance()
171: .getAssertionTypeForName(assertionLabel));
172:
173: WsdlMessageAssertion assertion = assertionsSupport
174: .addWsdlAssertion(assertionConfig);
175: assertionsSupport.fireAssertionAdded(assertion);
176:
177: if (getResponse() != null) {
178: assertion.assertResponse(
179: new WsdlResponseMessageExchange(this ),
180: new WsdlTestRunContext(testStep));
181: notifier.notifyChange();
182: }
183:
184: return assertion;
185: } catch (Exception e) {
186: SoapUI.logError(e);
187: return null;
188: }
189: }
190:
191: public void removeAssertion(WsdlMessageAssertion assertion) {
192: PropertyChangeNotifier notifier = new PropertyChangeNotifier();
193:
194: try {
195: int ix = assertionsSupport.removeAssertion(assertion);
196: getConfig().removeAssertion(ix);
197: } finally {
198: assertion.release();
199: notifier.notifyChange();
200: }
201: }
202:
203: public AssertionStatus getAssertionStatus() {
204: currentStatus = AssertionStatus.UNKNOWN;
205:
206: if (messageExchange != null) {
207: if (!messageExchange.hasResponse()
208: && !getOperation().isOneWay()) {
209: currentStatus = AssertionStatus.FAILED;
210: }
211: } else
212: return currentStatus;
213:
214: int cnt = getAssertionCount();
215: if (cnt == 0)
216: return currentStatus;
217:
218: for (int c = 0; c < cnt; c++) {
219: if (getAssertionAt(c).getStatus() == AssertionStatus.FAILED) {
220: currentStatus = AssertionStatus.FAILED;
221: break;
222: }
223: }
224:
225: if (currentStatus == AssertionStatus.UNKNOWN)
226: currentStatus = AssertionStatus.VALID;
227:
228: return currentStatus;
229: }
230:
231: public ImageIcon getIcon() {
232: if (forLoadTest)
233: return null;
234:
235: TestMonitor testMonitor = SoapUI.getTestMonitor();
236: if (testMonitor != null
237: && testMonitor.hasRunningLoadTest(testStep
238: .getTestCase()))
239: return disabledRequestIcon;
240:
241: ImageIcon icon = getIconAnimator().getIcon();
242: if (icon == getIconAnimator().getBaseIcon()) {
243: AssertionStatus status = getAssertionStatus();
244: if (status == AssertionStatus.VALID)
245: return validRequestIcon;
246: else if (status == AssertionStatus.FAILED)
247: return failedRequestIcon;
248: else if (status == AssertionStatus.UNKNOWN)
249: return unknownRequestIcon;
250: }
251:
252: return icon;
253: }
254:
255: public void propertyChange(PropertyChangeEvent evt) {
256: if (evt.getPropertyName().equals(
257: WsdlMessageAssertion.CONFIGURATION_PROPERTY)
258: && getResponse() != null)
259: assertResponse(new WsdlTestRunContext(testStep));
260: }
261:
262: public void addAssertionsListener(AssertionsListener listener) {
263: assertionsSupport.addAssertionsListener(listener);
264: }
265:
266: public void removeAssertionsListener(AssertionsListener listener) {
267: assertionsSupport.removeAssertionsListener(listener);
268: }
269:
270: /**
271: * Called when a testrequest is moved in a testcase
272: */
273:
274: public void updateConfig(CallConfig request) {
275: super .updateConfig(request);
276:
277: assertionsSupport.updateConfig(getConfig().getAssertionList());
278:
279: List<AttachmentConfig> attachmentConfigs = getConfig()
280: .getAttachmentList();
281: for (int i = 0; i < attachmentConfigs.size(); i++) {
282: AttachmentConfig config = attachmentConfigs.get(i);
283: attachments.get(i).updateConfig(config);
284: }
285: }
286:
287: public void release() {
288: super .release();
289: assertionsSupport.release();
290: }
291:
292: public String getAssertableContent() {
293: return getResponse() == null ? null : getResponse()
294: .getContentAsString();
295: }
296:
297: public WsdlTestRequestStep getTestStep() {
298: return testStep;
299: }
300:
301: public WsdlInterface getInterface() {
302: return getOperation().getInterface();
303: }
304:
305: protected class TestRequestIconAnimator extends RequestIconAnimator {
306: public boolean beforeSubmit(Submit submit, SubmitContext context) {
307: if (SoapUI.getTestMonitor() != null
308: && SoapUI.getTestMonitor().hasRunningLoadTest(
309: getTestCase()))
310: return true;
311:
312: return super .beforeSubmit(submit, context);
313: }
314:
315: public void afterSubmit(Submit submit, SubmitContext context) {
316: if (submit.getRequest() == getTarget())
317: stop();
318: }
319: }
320:
321: public AssertionType getAssertionType() {
322: return AssertionType.RESPONSE;
323: }
324:
325: public String getInterfaceName() {
326: return testStep.getInterfaceName();
327: }
328:
329: public String getOperationName() {
330: return testStep.getOperationName();
331: }
332:
333: public WsdlMessageAssertion cloneAssertion(
334: WsdlMessageAssertion source, String name) {
335: RequestAssertionConfig conf = getConfig().addNewAssertion();
336: conf.set(source.getConfig());
337: conf.setName(name);
338:
339: WsdlMessageAssertion result = assertionsSupport
340: .addWsdlAssertion(conf);
341: assertionsSupport.fireAssertionAdded(result);
342: return result;
343: }
344: }
|