1
2
3
4
5
6
7
8
9
10
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.HashMap;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.xmlbeans.XmlObject;
24
25 import com.eviware.soapui.SoapUI;
26 import com.eviware.soapui.config.TestAssertionConfig;
27 import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
28 import com.eviware.soapui.impl.wsdl.teststeps.assertions.TestAssertionRegistry;
29 import com.eviware.soapui.model.testsuite.Assertable;
30 import com.eviware.soapui.model.testsuite.AssertionsListener;
31 import com.eviware.soapui.model.testsuite.TestAssertion;
32 import com.eviware.soapui.support.resolver.ResolveContext;
33
34 /***
35 * Utility for implementing the Assertable interface
36 *
37 * @author ole.matzura
38 */
39
40 public class AssertionsSupport implements PropertyChangeListener
41 {
42 private List<AssertionsListener> assertionsListeners = new ArrayList<AssertionsListener>();
43 private List<WsdlMessageAssertion> assertions = new ArrayList<WsdlMessageAssertion>();
44 private final Assertable assertable;
45 private AssertableConfig modelItemConfig;
46
47 public AssertionsSupport( Assertable assertable, AssertableConfig modelItemConfig )
48 {
49 this.assertable = assertable;
50 this.modelItemConfig = modelItemConfig;
51
52 for( TestAssertionConfig rac : modelItemConfig.getAssertionList() )
53 {
54 addWsdlAssertion( rac );
55 }
56 }
57
58 public WsdlMessageAssertion addWsdlAssertion( TestAssertionConfig config )
59 {
60 try
61 {
62 WsdlMessageAssertion assertion = TestAssertionRegistry.getInstance().buildAssertion( config, assertable );
63 if( assertion == null )
64 {
65 return null;
66 }
67 else
68 {
69 assertions.add( assertion );
70 assertion.addPropertyChangeListener( this );
71
72 return assertion;
73 }
74 }
75 catch( Exception e )
76 {
77 SoapUI.logError( e );
78 return null;
79 }
80 }
81
82 public void propertyChange( PropertyChangeEvent arg0 )
83 {
84 if( assertable instanceof PropertyChangeListener )
85 ( ( PropertyChangeListener )assertable ).propertyChange( arg0 );
86 }
87
88 public int getAssertionCount()
89 {
90 return assertions.size();
91 }
92
93 public WsdlMessageAssertion getAssertionAt( int c )
94 {
95 return assertions.get( c );
96 }
97
98 public void addAssertionsListener( AssertionsListener listener )
99 {
100 assertionsListeners.add( listener );
101 }
102
103 public void removeAssertionsListener( AssertionsListener listener )
104 {
105 assertionsListeners.remove( listener );
106 }
107
108 public void removeAssertion( WsdlMessageAssertion assertion )
109 {
110 int ix = assertions.indexOf( assertion );
111 if( ix == -1 )
112 {
113 throw new RuntimeException( "assertion [" + assertion.getName() + "] not available " );
114 }
115
116 assertion.removePropertyChangeListener( this );
117 assertions.remove( ix );
118 fireAssertionRemoved( assertion );
119
120 assertion.release();
121
122 modelItemConfig.removeAssertion( ix );
123 }
124
125 public WsdlMessageAssertion moveAssertion( int ix, int offset )
126 {
127
128 WsdlMessageAssertion assertion = getAssertionAt( ix );
129 if( ix == -1 )
130 {
131 throw new RuntimeException( "assertion [" + assertion.getName() + "] not available " );
132 }
133
134 if( ( ix == 0 && offset == -1 ) || ( ix == assertions.size() - 1 && offset == 1 ) )
135 {
136 return assertion;
137 }
138 TestAssertionConfig conf = assertion.getConfig();
139 XmlObject newXmlObject = conf.copy();
140
141 TestAssertionConfig newConf = TestAssertionConfig.Factory.newInstance();
142 newConf.set( newXmlObject );
143 WsdlMessageAssertion newAssertion = TestAssertionRegistry.getInstance().buildAssertion( newConf, assertable );
144
145 assertion.removePropertyChangeListener( this );
146 assertions.remove( ix );
147
148 assertion.release();
149
150 modelItemConfig.removeAssertion( ix );
151
152 newAssertion.addPropertyChangeListener( this );
153 assertions.add( ix + offset, newAssertion );
154
155 modelItemConfig.insertAssertion( newConf, ix + offset );
156 fireAssertionMoved( newAssertion, ix, offset );
157 return newAssertion;
158
159 }
160
161 public void release()
162 {
163 for( WsdlMessageAssertion assertion : assertions )
164 assertion.release();
165 }
166
167 public Iterator<WsdlMessageAssertion> iterator()
168 {
169 return assertions.iterator();
170 }
171
172 public void fireAssertionAdded( WsdlMessageAssertion assertion )
173 {
174 AssertionsListener[] listeners = assertionsListeners.toArray( new AssertionsListener[assertionsListeners.size()] );
175
176 for( int c = 0; c < listeners.length; c++ )
177 {
178 listeners[c].assertionAdded( assertion );
179 }
180 }
181
182 public void fireAssertionRemoved( WsdlMessageAssertion assertion )
183 {
184 AssertionsListener[] listeners = assertionsListeners.toArray( new AssertionsListener[assertionsListeners.size()] );
185
186 for( int c = 0; c < listeners.length; c++ )
187 {
188 listeners[c].assertionRemoved( assertion );
189 }
190 }
191
192 public void fireAssertionMoved( WsdlMessageAssertion assertion, int ix, int offset )
193 {
194 AssertionsListener[] listeners = assertionsListeners.toArray( new AssertionsListener[assertionsListeners.size()] );
195
196 for( int c = 0; c < listeners.length; c++ )
197 {
198 listeners[c].assertionMoved( assertion, ix, offset );
199 }
200 }
201
202 public void refresh()
203 {
204 int mod = 0;
205
206 List<TestAssertionConfig> assertionList = modelItemConfig.getAssertionList();
207
208 for( int i = 0; i < assertionList.size(); i++ )
209 {
210 TestAssertionConfig config = assertionList.get( i );
211 if( TestAssertionRegistry.getInstance().canBuildAssertion( config ) )
212 {
213 assertions.get( i - mod ).updateConfig( config );
214 }
215 else
216 mod++ ;
217 }
218 }
219
220 public List<WsdlMessageAssertion> getAssertionList()
221 {
222 return assertions;
223 }
224
225 public List<WsdlMessageAssertion> getAssertionsOfType( Class<? extends WsdlMessageAssertion> class1 )
226 {
227 List<WsdlMessageAssertion> result = new ArrayList<WsdlMessageAssertion>();
228
229 for( WsdlMessageAssertion assertion : assertions )
230 {
231 if( assertion.getClass().equals( class1 ) )
232 result.add( assertion );
233 }
234
235 return result;
236 }
237
238 public WsdlMessageAssertion getAssertionByName( String name )
239 {
240 for( WsdlMessageAssertion assertion : assertions )
241 {
242 if( assertion.getName().equals( name ) )
243 return assertion;
244 }
245
246 return null;
247 }
248
249 public Map<String, TestAssertion> getAssertions()
250 {
251 Map<String, TestAssertion> result = new HashMap<String, TestAssertion>();
252
253 for( TestAssertion assertion : assertions )
254 result.put( assertion.getName(), assertion );
255
256 return result;
257 }
258
259 public WsdlMessageAssertion importAssertion( WsdlMessageAssertion source, boolean overwrite, boolean createCopy )
260 {
261 TestAssertionConfig conf = modelItemConfig.addNewAssertion();
262 conf.set( source.getConfig() );
263 if( createCopy && conf.isSetId() )
264 conf.unsetId();
265
266 if( !source.isAllowMultiple() )
267 {
268 List<WsdlMessageAssertion> existing = getAssertionsOfType( source.getClass() );
269 if( !existing.isEmpty() && !overwrite )
270 return null;
271
272 while( !existing.isEmpty() )
273 {
274 removeAssertion( existing.remove( 0 ) );
275 }
276 }
277
278 WsdlMessageAssertion result = addWsdlAssertion( conf );
279 fireAssertionAdded( result );
280 return result;
281 }
282
283 public TestAssertion cloneAssertion( TestAssertion source, String name )
284 {
285 TestAssertionConfig conf = modelItemConfig.addNewAssertion();
286 conf.set( ( ( WsdlMessageAssertion )source ).getConfig() );
287 conf.setName( name );
288
289 WsdlMessageAssertion result = addWsdlAssertion( conf );
290 fireAssertionAdded( result );
291 return result;
292
293 }
294
295 public WsdlMessageAssertion addWsdlAssertion( String assertionLabel )
296 {
297 try
298 {
299 TestAssertionConfig assertionConfig = modelItemConfig.addNewAssertion();
300 assertionConfig.setType( TestAssertionRegistry.getInstance().getAssertionTypeForName( assertionLabel ) );
301
302 WsdlMessageAssertion assertion = addWsdlAssertion( assertionConfig );
303 if( assertion == null )
304 return null;
305
306 fireAssertionAdded( assertion );
307
308 return assertion;
309 }
310 catch( Exception e )
311 {
312 SoapUI.logError( e );
313 return null;
314 }
315 }
316
317 public void resolve( ResolveContext<?> context )
318 {
319 for( WsdlMessageAssertion assertion : assertions )
320 {
321 assertion.resolve( context );
322 }
323 }
324 }