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  	private boolean useRegEx;
    46  	public static final String ID = "Simple NotContains";
    47  	private static final String CONTENT = "Content";
    48  	private static final String IGNORE_CASE = "Ignore Case";
    49  	private static final String USE_REGEX = "Regular Expression";
    50
    51     public SimpleNotContainsAssertion(RequestAssertionConfig assertionConfig, Assertable assertable)
    52     {
    53        super(assertionConfig, assertable, true, true, true);
    54
    55        XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( getConfiguration() );
    56        token = reader.readString( "token", null );
    57        ignoreCase = reader.readBoolean( "ignoreCase", false );
    58        useRegEx = reader.readBoolean( "useRegEx", false );
    59     }
    60
    61     public String internalAssertResponse(WsdlMessageExchange messageExchange, SubmitContext context) throws AssertionException
    62     {
    63     	return assertContent( context, messageExchange.getResponseContent(), "Response" );
    64     }
    65
    66  	private String assertContent( SubmitContext context, String content, String type ) throws AssertionException
    67  	{
    68  		if( token == null ) token = "";
    69
    70        String replToken = PropertyExpansionRequestFilter.expandProperties( context, token );
    71
    72        if( replToken.length() > 0 )
    73        {
    74        	int ix = -1;
    75
    76        	if( useRegEx )
    77        	{
    78        		if( content.matches( replToken ))
    79        			ix = 0;
    80        	}
    81        	else
    82        	{
    83  	      	ix = ignoreCase ?
    84  	      				content.toUpperCase().indexOf( replToken.toUpperCase() ) : content.indexOf( replToken );
    85        	}
    86
    87           if( ix != -1 )
    88           	throw new AssertionException( new AssertionError( type + " contains token [" + replToken + "]") );
    89        }
    90
    91        return type + " does not contain token [" + replToken + "]";
    92  	}
    93
    94     public boolean configure()
    95     {
    96     	if( dialog == null )
    97     		buildDialog();
    98
    99     	StringToStringMap values = new StringToStringMap();
    100    	values.put( CONTENT, token );
    101    	values.put( IGNORE_CASE, ignoreCase );
    102    	values.put( USE_REGEX, useRegEx );
    103
    104    	values = dialog.show( values );
    105    	if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
    106    	{
    107    		token = values.get( CONTENT );
    108    		ignoreCase = values.getBoolean( IGNORE_CASE );
    109    		useRegEx = values.getBoolean( USE_REGEX );
    110    	}
    111
    112       setConfiguration( createConfiguration() );
    113       return true;
    114    }
    115
    116    protected XmlObject createConfiguration()
    117    {
    118    	XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
    119    	builder.add( "token", token );
    120    	builder.add( "ignoreCase", ignoreCase );
    121    	builder.add( "useRegEx", useRegEx );
    122    	return builder.finish();
    123    }
    124
    125    private void buildDialog()
    126 	{
    127 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder("NotContains Assertion");
    128 		XForm mainForm = builder.createForm( "Basic" );
    129
    130 		mainForm.addTextField( CONTENT, "Content to check for", XForm.FieldType.TEXT ).setWidth( 20 );
    131 		mainForm.addCheckBox( IGNORE_CASE, "Ignore case in comparison" );
    132 		mainForm.addCheckBox( USE_REGEX, "Use token as Regular Expression" );
    133
    134 		dialog = builder.buildDialog( builder.buildOkCancelHelpActions( HelpUrls.SIMPLE_NOT_CONTAINS_HELP_URL ),
    135       		"Specify options", UISupport.OPTIONS_ICON );
    136 	}
    137
    138 	protected String internalAssertRequest( WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException
    139 	{
    140       return assertContent( context, messageExchange.getRequestContent(), "Request" );
    141 	}
    142
    143 	public boolean isIgnoreCase()
    144 	{
    145 		return ignoreCase;
    146 	}
    147
    148 	public void setIgnoreCase( boolean ignoreCase )
    149 	{
    150 		this.ignoreCase = ignoreCase;
    151 		setConfiguration( createConfiguration() );
    152 	}
    153
    154 	public String getToken()
    155 	{
    156 		return token;
    157 	}
    158
    159 	public void setToken( String token )
    160 	{
    161 		this.token = token;
    162 		setConfiguration( createConfiguration() );
    163 	}
    164 }