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.assertions;
014:
015: import org.apache.xmlbeans.XmlObject;
016:
017: import com.eviware.soapui.config.RequestAssertionConfig;
018: import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
019: import com.eviware.soapui.impl.wsdl.submit.filters.PropertyExpansionRequestFilter;
020: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
021: import com.eviware.soapui.impl.wsdl.support.assertions.Assertable;
022: import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
023: import com.eviware.soapui.model.iface.SubmitContext;
024: import com.eviware.soapui.support.UISupport;
025: import com.eviware.soapui.support.types.StringToStringMap;
026: import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
027: import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
028: import com.eviware.x.form.XForm;
029: import com.eviware.x.form.XFormDialog;
030: import com.eviware.x.form.XFormDialogBuilder;
031: import com.eviware.x.form.XFormFactory;
032:
033: /**
034: * Assertion that checks for the non-existence of a specified text token in the associated
035: * WsdlTestRequests response message
036: *
037: * @author Ole.Matzura
038: */
039:
040: public class SimpleNotContainsAssertion extends WsdlMessageAssertion
041: implements RequestAssertion, ResponseAssertion {
042: private String token;
043: private boolean ignoreCase;
044: private XFormDialog dialog;
045: public static final String ID = "Simple NotContains";
046: private static final String CONTENT = "Content";
047: private static final String IGNORE_CASE = "Ignore Case";
048:
049: public SimpleNotContainsAssertion(
050: RequestAssertionConfig assertionConfig,
051: Assertable assertable) {
052: super (assertionConfig, assertable, true, true);
053:
054: XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader(
055: getConfiguration());
056: token = reader.readString("token", null);
057: ignoreCase = reader.readBoolean("ignoreCase", false);
058: }
059:
060: public String internalAssertResponse(
061: WsdlMessageExchange messageExchange, SubmitContext context)
062: throws AssertionException {
063: return assertContent(context, messageExchange
064: .getResponseContent(), "Response");
065: }
066:
067: private String assertContent(SubmitContext context, String content,
068: String type) throws AssertionException {
069: if (token == null)
070: token = "";
071:
072: String replToken = PropertyExpansionRequestFilter
073: .expandProperties(context, token);
074:
075: if (replToken.length() > 0) {
076: int ix = ignoreCase ? content.toUpperCase().indexOf(
077: replToken.toUpperCase()) : content
078: .indexOf(replToken);
079:
080: if (ix != -1)
081: throw new AssertionException(new AssertionError(type
082: + " contains token [" + replToken + "]"));
083: }
084:
085: return type + " does not contain token [" + replToken + "]";
086: }
087:
088: public boolean configure() {
089: if (dialog == null)
090: buildDialog();
091:
092: StringToStringMap values = new StringToStringMap();
093: values.put(CONTENT, token);
094: values.put(IGNORE_CASE, ignoreCase);
095:
096: values = dialog.show(values);
097: if (dialog.getReturnValue() == XFormDialog.OK_OPTION) {
098: token = values.get(CONTENT);
099: ignoreCase = values.getBoolean(IGNORE_CASE);
100: }
101:
102: setConfiguration(createConfiguration());
103: return true;
104: }
105:
106: protected XmlObject createConfiguration() {
107: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
108: builder.add("token", token);
109: builder.add("ignoreCase", ignoreCase);
110: return builder.finish();
111: }
112:
113: private void buildDialog() {
114: XFormDialogBuilder builder = XFormFactory
115: .createDialogBuilder("Simple NotContains Assertion");
116: XForm mainForm = builder.createForm("Basic");
117:
118: mainForm.addTextField(CONTENT, "Content to check for",
119: XForm.FieldType.TEXT).setWidth(20);
120: mainForm.addCheckBox(IGNORE_CASE, "Ignore case in comparison");
121:
122: dialog = builder
123: .buildDialog(
124: builder
125: .buildOkCancelHelpActions(HelpUrls.SIMPLE_NOT_CONTAINS_HELP_URL),
126: "Specify options", UISupport.OPTIONS_ICON);
127: }
128:
129: protected String internalAssertRequest(
130: WsdlMessageExchange messageExchange, SubmitContext context)
131: throws AssertionException {
132: return assertContent(context, messageExchange
133: .getRequestContent(), "Request");
134: }
135:
136: public boolean isIgnoreCase() {
137: return ignoreCase;
138: }
139:
140: public void setIgnoreCase(boolean ignoreCase) {
141: this .ignoreCase = ignoreCase;
142: setConfiguration(createConfiguration());
143: }
144:
145: public String getToken() {
146: return token;
147: }
148:
149: public void setToken(String token) {
150: this.token = token;
151: setConfiguration(createConfiguration());
152: }
153: }
|