View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.config.TestAssertionConfig;
17  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
18  import com.eviware.soapui.impl.wsdl.teststeps.assertions.TestAssertionRegistry;
19  import com.eviware.soapui.model.testsuite.Assertable;
20  import com.eviware.soapui.model.testsuite.AssertionsListener;
21  import com.eviware.soapui.model.testsuite.TestAssertion;
22  import com.eviware.soapui.support.resolver.ResolveContext;
23  
24  import java.beans.PropertyChangeEvent;
25  import java.beans.PropertyChangeListener;
26  import java.util.*;
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     private AssertableConfig modelItemConfig;
40  
41     public AssertionsSupport( Assertable assertable, AssertableConfig modelItemConfig )
42     {
43        this.assertable = assertable;
44        this.modelItemConfig = modelItemConfig;
45  
46        for( TestAssertionConfig rac : modelItemConfig.getAssertionList() )
47        {
48           addWsdlAssertion( rac );
49        }
50     }
51  
52     public WsdlMessageAssertion addWsdlAssertion( TestAssertionConfig config )
53     {
54        try
55        {
56           WsdlMessageAssertion assertion = TestAssertionRegistry.getInstance().buildAssertion(
57                   config, assertable );
58           if( assertion == null )
59           {
60              return null;
61           }
62           else
63           {
64              assertions.add( assertion );
65              assertion.addPropertyChangeListener( this );
66  
67              return assertion;
68           }
69        }
70        catch( Exception e )
71        {
72           SoapUI.logError( e );
73           return null;
74        }
75     }
76  
77     public void propertyChange( PropertyChangeEvent arg0 )
78     {
79        if( assertable instanceof PropertyChangeListener )
80           ((PropertyChangeListener) assertable).propertyChange( arg0 );
81     }
82  
83     public int getAssertionCount()
84     {
85        return assertions.size();
86     }
87  
88     public WsdlMessageAssertion getAssertionAt( int c )
89     {
90        return assertions.get( c );
91     }
92  
93     public void addAssertionsListener( AssertionsListener listener )
94     {
95        assertionsListeners.add( listener );
96     }
97  
98     public void removeAssertionsListener( AssertionsListener listener )
99     {
100       assertionsListeners.remove( listener );
101    }
102 
103    public void removeAssertion( WsdlMessageAssertion assertion )
104    {
105       int ix = assertions.indexOf( assertion );
106       if( ix == -1 )
107       {
108          throw new RuntimeException( "assertion [" + assertion.getName() + "] not available " );
109       }
110 
111       assertion.removePropertyChangeListener( this );
112       assertions.remove( ix );
113       fireAssertionRemoved( assertion );
114 
115       assertion.release();
116 
117       modelItemConfig.removeAssertion( ix );
118    }
119 
120    public void release()
121    {
122       for( WsdlMessageAssertion assertion : assertions )
123          assertion.release();
124    }
125 
126    public Iterator<WsdlMessageAssertion> iterator()
127    {
128       return assertions.iterator();
129    }
130 
131    public void fireAssertionAdded( WsdlMessageAssertion assertion )
132    {
133       AssertionsListener[] listeners = assertionsListeners
134               .toArray( new AssertionsListener[assertionsListeners.size()] );
135 
136       for( int c = 0; c < listeners.length; c++ )
137       {
138          listeners[c].assertionAdded( assertion );
139       }
140    }
141 
142    public void fireAssertionRemoved( WsdlMessageAssertion assertion )
143    {
144       AssertionsListener[] listeners = assertionsListeners
145               .toArray( new AssertionsListener[assertionsListeners.size()] );
146 
147       for( int c = 0; c < listeners.length; c++ )
148       {
149          listeners[c].assertionRemoved( assertion );
150       }
151    }
152 
153    public void refresh()
154    {
155       int mod = 0;
156 
157       List<TestAssertionConfig> assertionList = modelItemConfig.getAssertionList();
158 
159       for( int i = 0; i < assertionList.size(); i++ )
160       {
161          TestAssertionConfig config = assertionList.get( i );
162          if( TestAssertionRegistry.getInstance().canBuildAssertion( config ) )
163          {
164             assertions.get( i - mod ).updateConfig( config );
165          }
166          else mod++;
167       }
168    }
169 
170    public List<WsdlMessageAssertion> getAssertionList()
171    {
172       return assertions;
173    }
174 
175    public List<WsdlMessageAssertion> getAssertionsOfType( Class<? extends WsdlMessageAssertion> class1 )
176    {
177       List<WsdlMessageAssertion> result = new ArrayList<WsdlMessageAssertion>();
178 
179       for( WsdlMessageAssertion assertion : assertions )
180       {
181          if( assertion.getClass().equals( class1 ) )
182             result.add( assertion );
183       }
184 
185       return result;
186    }
187 
188    public WsdlMessageAssertion getAssertionByName( String name )
189    {
190       for( WsdlMessageAssertion assertion : assertions )
191       {
192          if( assertion.getName().equals( name ) )
193             return assertion;
194       }
195 
196       return null;
197    }
198 
199    public Map<String, TestAssertion> getAssertions()
200    {
201       Map<String, TestAssertion> result = new HashMap<String, TestAssertion>();
202 
203       for( TestAssertion assertion : assertions )
204          result.put( assertion.getName(), assertion );
205 
206       return result;
207    }
208 
209    public WsdlMessageAssertion importAssertion( WsdlMessageAssertion source, boolean overwrite, boolean createCopy )
210    {
211       TestAssertionConfig conf = modelItemConfig.addNewAssertion();
212       conf.set( source.getConfig() );
213       if( createCopy && conf.isSetId() )
214          conf.unsetId();
215 
216       if( !source.isAllowMultiple() )
217       {
218          List<WsdlMessageAssertion> existing = getAssertionsOfType( source.getClass() );
219          if( !existing.isEmpty() && !overwrite )
220             return null;
221 
222          while( !existing.isEmpty() )
223          {
224             removeAssertion( existing.remove( 0 ) );
225          }
226       }
227 
228       WsdlMessageAssertion result = addWsdlAssertion( conf );
229       fireAssertionAdded( result );
230       return result;
231    }
232 
233    public TestAssertion cloneAssertion( TestAssertion source, String name )
234    {
235       TestAssertionConfig conf = modelItemConfig.addNewAssertion();
236       conf.set( ((WsdlMessageAssertion) source).getConfig() );
237       conf.setName( name );
238 
239       WsdlMessageAssertion result = addWsdlAssertion( conf );
240       fireAssertionAdded( result );
241       return result;
242 
243    }
244 
245    public WsdlMessageAssertion addWsdlAssertion( String assertionLabel )
246    {
247       try
248       {
249          TestAssertionConfig assertionConfig = modelItemConfig.addNewAssertion();
250          assertionConfig.setType( TestAssertionRegistry.getInstance().getAssertionTypeForName( assertionLabel ) );
251 
252          WsdlMessageAssertion assertion = addWsdlAssertion( assertionConfig );
253          if( assertion == null )
254             return null;
255 
256          fireAssertionAdded( assertion );
257 
258          return assertion;
259       }
260       catch( Exception e )
261       {
262          SoapUI.logError( e );
263          return null;
264       }
265    }
266 
267    public void resolve( ResolveContext context )
268    {
269       for( WsdlMessageAssertion assertion : assertions )
270       {
271          assertion.resolve( context );
272       }
273    }
274 }