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 a specified text token in the associated
035: * WsdlTestRequests response XML message
036: *
037: * @author Ole.Matzura
038: */
039:
040: public class SimpleContainsAssertion extends WsdlMessageAssertion
041: implements RequestAssertion, ResponseAssertion {
042: private String token;
043: private XFormDialog dialog;
044: private boolean ignoreCase;
045: public static final String ID = "Simple Contains";
046: private static final String CONTENT = "Content";
047: private static final String IGNORE_CASE = "Ignore Case";
048:
049: public SimpleContainsAssertion(
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: String replToken = PropertyExpansionRequestFilter
072: .expandProperties(context, token);
073:
074: if (replToken.length() > 0) {
075: int ix = ignoreCase ? content.toUpperCase().indexOf(
076: replToken.toUpperCase()) : content
077: .indexOf(replToken);
078:
079: if (ix == -1)
080: throw new AssertionException(new AssertionError(
081: "Missing token [" + replToken + "] in " + type));
082: }
083:
084: return "Response contains token [" + replToken + "]";
085: }
086:
087: public boolean configure() {
088: if (dialog == null)
089: buildDialog();
090:
091: StringToStringMap values = new StringToStringMap();
092: values.put(CONTENT, token);
093: values.put(IGNORE_CASE, ignoreCase);
094:
095: values = dialog.show(values);
096: if (dialog.getReturnValue() == XFormDialog.OK_OPTION) {
097: token = values.get(CONTENT);
098: ignoreCase = values.getBoolean(IGNORE_CASE);
099: }
100:
101: setConfiguration(createConfiguration());
102: return true;
103: }
104:
105: public boolean isIgnoreCase() {
106: return ignoreCase;
107: }
108:
109: public void setIgnoreCase(boolean ignoreCase) {
110: this .ignoreCase = ignoreCase;
111: setConfiguration(createConfiguration());
112: }
113:
114: public String getToken() {
115: return token;
116: }
117:
118: public void setToken(String token) {
119: this .token = token;
120: setConfiguration(createConfiguration());
121: }
122:
123: protected XmlObject createConfiguration() {
124: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
125: builder.add("token", token);
126: builder.add("ignoreCase", ignoreCase);
127: return builder.finish();
128: }
129:
130: private void buildDialog() {
131: XFormDialogBuilder builder = XFormFactory
132: .createDialogBuilder("Simple Contains Assertion");
133: XForm mainForm = builder.createForm("Basic");
134:
135: mainForm.addTextField(CONTENT, "Content to check for",
136: XForm.FieldType.TEXT).setWidth(20);
137: mainForm.addCheckBox(IGNORE_CASE, "Ignore case in comparison");
138:
139: dialog = builder
140: .buildDialog(
141: builder
142: .buildOkCancelHelpActions(HelpUrls.SIMPLE_CONTAINS_HELP_URL),
143: "Specify options", UISupport.OPTIONS_ICON);
144: }
145:
146: @Override
147: protected String internalAssertRequest(
148: WsdlMessageExchange messageExchange, SubmitContext context)
149: throws AssertionException {
150: return assertContent(context, messageExchange
151: .getRequestContent(), "Request");
152: }
153: }
|