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;
    14
    15  import java.beans.PropertyChangeEvent;
    16  import java.beans.PropertyChangeListener;
    17  import java.util.ArrayList;
    18  import java.util.Iterator;
    19  import java.util.List;
    20
    21  import com.eviware.soapui.SoapUI;
    22  import com.eviware.soapui.config.RequestAssertionConfig;
    23  import com.eviware.soapui.impl.wsdl.support.assertions.Assertable;
    24  import com.eviware.soapui.impl.wsdl.support.assertions.AssertionsListener;
    25  import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry;
    26
    27  /***
    28   * Utility for implementing the Assertable interface
    29   *
    30   * @author ole.matzura
    31   */
    32
    33  public class AssertionsSupport implements PropertyChangeListener
    34  {
    35  	private List<AssertionsListener> assertionsListeners = new ArrayList<AssertionsListener>();
    36  	private List<WsdlMessageAssertion> assertions = new ArrayList<WsdlMessageAssertion>();
    37  	private final Assertable assertable;
    38
    39  	public AssertionsSupport( Assertable assertable, List<RequestAssertionConfig> assertionList )
    40  	{
    41  		this.assertable = assertable;
    42
    43  		for( RequestAssertionConfig rac : assertionList )
    44  		{
    45  			addWsdlAssertion( rac );
    46  		}
    47  	}
    48
    49  	public WsdlMessageAssertion addWsdlAssertion( RequestAssertionConfig config )
    50  	{
    51  		try
    52  		{
    53  			WsdlMessageAssertion assertion = WsdlAssertionRegistry.getInstance().buildAssertion(
    54  						config, assertable );
    55  			if( assertion == null )
    56  			{
    57  				return null;
    58  			}
    59  			else
    60  			{
    61  				assertions.add( assertion );
    62  				assertion.addPropertyChangeListener( this );
    63
    64  				return assertion;
    65  			}
    66  		}
    67  		catch( Exception e )
    68  		{
    69  			SoapUI.logError( e );
    70  			return null;
    71  		}
    72  	}
    73
    74  	public void propertyChange( PropertyChangeEvent arg0 )
    75  	{
    76  		if( assertable instanceof PropertyChangeListener )
    77  			((PropertyChangeListener)assertable).propertyChange( arg0 );
    78  	}
    79
    80  	public int getAssertionCount()
    81  	{
    82  		return assertions.size();
    83  	}
    84
    85  	public WsdlMessageAssertion getAssertionAt( int c )
    86  	{
    87  		return assertions.get( c );
    88  	}
    89
    90  	public void addAssertionsListener( AssertionsListener listener )
    91  	{
    92  		assertionsListeners.add( listener );
    93  	}
    94
    95  	public void removeAssertionsListener( AssertionsListener listener )
    96  	{
    97  		assertionsListeners.remove( listener );
    98  	}
    99
    100 	public int removeAssertion( WsdlMessageAssertion assertion )
    101 	{
    102 		int ix = assertions.indexOf( assertion );
    103 		if( ix == -1 )
    104 		{
    105 			throw new RuntimeException( "assertion [" + assertion.getName() + "] not available " );
    106 		}
    107
    108 		assertion.removePropertyChangeListener( this );
    109 		assertions.remove( ix );
    110 		fireAssertionRemoved( assertion );
    111
    112 		return ix;
    113 	}
    114
    115 	public void release()
    116 	{
    117 		for( WsdlMessageAssertion assertion : assertions )
    118 			assertion.release();
    119
    120 	}
    121
    122 	public Iterator<WsdlMessageAssertion> iterator()
    123 	{
    124 		return assertions.iterator();
    125 	}
    126
    127 	public void fireAssertionAdded( WsdlMessageAssertion assertion )
    128 	{
    129 		AssertionsListener[] listeners = assertionsListeners
    130 					.toArray( new AssertionsListener[assertionsListeners.size()] );
    131
    132 		for( int c = 0; c < listeners.length; c++ )
    133 		{
    134 			listeners[c].assertionAdded( assertion );
    135 		}
    136 	}
    137
    138 	public void fireAssertionRemoved( WsdlMessageAssertion assertion )
    139 	{
    140 		AssertionsListener[] listeners = assertionsListeners
    141 					.toArray( new AssertionsListener[assertionsListeners.size()] );
    142
    143 		for( int c = 0; c < listeners.length; c++ )
    144 		{
    145 			listeners[c].assertionRemoved( assertion );
    146 		}
    147 	}
    148
    149 	public void updateConfig( List<RequestAssertionConfig> assertionList )
    150 	{
    151 		int mod = 0;
    152
    153       for (int i = 0; i < assertionList.size(); i++)
    154       {
    155          RequestAssertionConfig config = assertionList.get( i );
    156          if( WsdlAssertionRegistry.getInstance().canBuildAssertion( config ))
    157          {
    158          	assertions.get( i-mod ).updateConfig( config );
    159          }
    160          else mod++;
    161       }
    162 	}
    163
    164 	public List<WsdlMessageAssertion> getAssertionList()
    165 	{
    166 		return assertions;
    167 	}
    168
    169 	public List<WsdlMessageAssertion> getAssertionsOfType( Class<? extends WsdlMessageAssertion> class1 )
    170 	{
    171 		List<WsdlMessageAssertion> result = new ArrayList<WsdlMessageAssertion>();
    172
    173 		for( WsdlMessageAssertion assertion : assertions )
    174 		{
    175 			if( assertion.getClass().equals( class1 ))
    176 				result.add(  assertion );
    177 		}
    178
    179 		return result;
    180 	}
    181 }