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