View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.impl.wsdl.teststeps.assertions;
    14
    15  import org.apache.xmlbeans.XmlObject;
    16
    17  import com.eviware.soapui.config.RequestAssertionConfig;
    18  import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
    19  import com.eviware.soapui.impl.wsdl.submit.filters.PropertyExpansionRequestFilter;
    20  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    21  import com.eviware.soapui.impl.wsdl.support.assertions.Assertable;
    22  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
    23  import com.eviware.soapui.model.iface.SubmitContext;
    24  import com.eviware.soapui.support.UISupport;
    25  import com.eviware.soapui.support.types.StringToStringMap;
    26  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
    27  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
    28  import com.eviware.x.form.XForm;
    29  import com.eviware.x.form.XFormDialog;
    30  import com.eviware.x.form.XFormDialogBuilder;
    31  import com.eviware.x.form.XFormFactory;
    32
    33  /***
    34   * Assertion that checks for the non-existence of a specified text token in the associated
    35   * WsdlTestRequests response message
    36   *
    37   * @author Ole.Matzura
    38   */
    39
    40  public class SimpleNotContainsAssertion extends WsdlMessageAssertion implements RequestAssertion, ResponseAssertion
    41  {
    42     private String token;
    43  	private boolean ignoreCase;
    44  	private XFormDialog dialog;
    45  	public static final String ID = "Simple NotContains";
    46  	private static final String CONTENT = "Content";
    47  	private static final String IGNORE_CASE = "Ignore Case";
    48
    49     public SimpleNotContainsAssertion(RequestAssertionConfig assertionConfig, Assertable assertable)
    50     {
    51        super(assertionConfig, assertable, true, true);
    52
    53        XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( getConfiguration() );
    54        token = reader.readString( "token", null );
    55        ignoreCase = reader.readBoolean( "ignoreCase", false );
    56     }
    57
    58     public String internalAssertResponse(WsdlMessageExchange messageExchange, SubmitContext context) throws AssertionException
    59     {
    60     	return assertContent( context, messageExchange.getResponseContent(), "Response" );
    61     }
    62
    63  	private String assertContent( SubmitContext context, String content, String type ) throws AssertionException
    64  	{
    65  		if( token == null ) token = "";
    66
    67        String replToken = PropertyExpansionRequestFilter.expandProperties( context, token );
    68
    69        if( replToken.length() > 0 )
    70        {
    71        	int ix = ignoreCase ?
    72        				content.toUpperCase().indexOf( replToken.toUpperCase() ) : content.indexOf( replToken );
    73
    74           if( ix != -1 )
    75           	throw new AssertionException( new AssertionError( type + " contains token [" + replToken + "]") );
    76        }
    77
    78        return type + " does not contain token [" + replToken + "]";
    79  	}
    80
    81     public boolean configure()
    82     {
    83     	if( dialog == null )
    84     		buildDialog();
    85
    86     	StringToStringMap values = new StringToStringMap();
    87     	values.put( CONTENT, token );
    88     	values.put( IGNORE_CASE, ignoreCase );
    89
    90     	values = dialog.show( values );
    91     	if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
    92     	{
    93     		token = values.get( CONTENT );
    94     		ignoreCase = values.getBoolean( IGNORE_CASE );
    95     	}
    96
    97        setConfiguration( createConfiguration() );
    98        return true;
    99     }
    100
    101    protected XmlObject createConfiguration()
    102    {
    103    	XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
    104    	builder.add( "token", token );
    105    	builder.add( "ignoreCase", ignoreCase );
    106    	return builder.finish();
    107    }
    108
    109    private void buildDialog()
    110 	{
    111 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Simple NotContains Assertion");
    112 		XForm mainForm = builder.createForm( "Basic" );
    113
    114 		mainForm.addTextField( CONTENT, "Content to check for", XForm.FieldType.TEXT ).setWidth( 20 );
    115 		mainForm.addCheckBox( IGNORE_CASE, "Ignore case in comparison" );
    116
    117 		dialog = builder.buildDialog( builder.buildOkCancelHelpActions( HelpUrls.SIMPLE_NOT_CONTAINS_HELP_URL ),
    118       		"Specify options", UISupport.OPTIONS_ICON );
    119 	}
    120
    121 	protected String internalAssertRequest( WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException
    122 	{
    123       return assertContent( context, messageExchange.getRequestContent(), "Request" );
    124 	}
    125
    126 	public boolean isIgnoreCase()
    127 	{
    128 		return ignoreCase;
    129 	}
    130
    131 	public void setIgnoreCase( boolean ignoreCase )
    132 	{
    133 		this.ignoreCase = ignoreCase;
    134 		setConfiguration( createConfiguration() );
    135 	}
    136
    137 	public String getToken()
    138 	{
    139 		return token;
    140 	}
    141
    142 	public void setToken( String token )
    143 	{
    144 		this.token = token;
    145 		setConfiguration( createConfiguration() );
    146 	}
    147 }