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 newAssertion.release();
156
157 modelItemConfig.insertAssertion( newConf, ix + offset );
158 fireAssertionMoved( newAssertion, ix, offset );
159 return newAssertion;
160
161 }
162
163 public void release()
164 {
165 for( WsdlMessageAssertion assertion : assertions )
166 assertion.release();
167 }
168
169 public Iterator<WsdlMessageAssertion> iterator()
170 {
171 return assertions.iterator();
172 }
173
174 public void fireAssertionAdded( WsdlMessageAssertion assertion )
175 {
176 AssertionsListener[] listeners = assertionsListeners.toArray( new AssertionsListener[assertionsListeners.size()] );
177
178 for( int c = 0; c < listeners.length; c++ )
179 {
180 listeners[c].assertionAdded( assertion );
181 }
182 }
183
184 public void fireAssertionRemoved( WsdlMessageAssertion assertion )
185 {
186 AssertionsListener[] listeners = assertionsListeners.toArray( new AssertionsListener[assertionsListeners.size()] );
187
188 for( int c = 0; c < listeners.length; c++ )
189 {
190 listeners[c].assertionRemoved( assertion );
191 }
192 }
193
194 public void fireAssertionMoved( WsdlMessageAssertion assertion, int ix, int offset )
195 {
196 AssertionsListener[] listeners = assertionsListeners.toArray( new AssertionsListener[assertionsListeners.size()] );
197
198 for( int c = 0; c < listeners.length; c++ )
199 {
200 listeners[c].assertionMoved( assertion, ix, offset );
201 }
202 }
203
204 public void refresh()
205 {
206 int mod = 0;
207
208 List<TestAssertionConfig> assertionList = modelItemConfig.getAssertionList();
209
210 for( int i = 0; i < assertionList.size(); i++ )
211 {
212 TestAssertionConfig config = assertionList.get( i );
213 if( TestAssertionRegistry.getInstance().canBuildAssertion( config ) )
214 {
215 assertions.get( i - mod ).updateConfig( config );
216 }
217 else
218 mod++ ;
219 }
220 }
221
222 public List<WsdlMessageAssertion> getAssertionList()
223 {
224 return assertions;
225 }
226
227 public List<WsdlMessageAssertion> getAssertionsOfType( Class<? extends WsdlMessageAssertion> class1 )
228 {
229 List<WsdlMessageAssertion> result = new ArrayList<WsdlMessageAssertion>();
230
231 for( WsdlMessageAssertion assertion : assertions )
232 {
233 if( assertion.getClass().equals( class1 ) )
234 result.add( assertion );
235 }
236
237 return result;
238 }
239
240 public WsdlMessageAssertion getAssertionByName( String name )
241 {
242 for( WsdlMessageAssertion assertion : assertions )
243 {
244 if( assertion.getName().equals( name ) )
245 return assertion;
246 }
247
248 return null;
249 }
250
251 public Map<String, TestAssertion> getAssertions()
252 {
253 Map<String, TestAssertion> result = new HashMap<String, TestAssertion>();
254
255 for( TestAssertion assertion : assertions )
256 result.put( assertion.getName(), assertion );
257
258 return result;
259 }
260
261 public WsdlMessageAssertion importAssertion( WsdlMessageAssertion source, boolean overwrite, boolean createCopy )
262 {
263 TestAssertionConfig conf = modelItemConfig.addNewAssertion();
264 conf.set( source.getConfig() );
265 if( createCopy && conf.isSetId() )
266 conf.unsetId();
267
268 if( !source.isAllowMultiple() )
269 {
270 List<WsdlMessageAssertion> existing = getAssertionsOfType( source.getClass() );
271 if( !existing.isEmpty() && !overwrite )
272 return null;
273
274 while( !existing.isEmpty() )
275 {
276 removeAssertion( existing.remove( 0 ) );
277 }
278 }
279
280 WsdlMessageAssertion result = addWsdlAssertion( conf );
281 fireAssertionAdded( result );
282 return result;
283 }
284
285 public TestAssertion cloneAssertion( TestAssertion source, String name )
286 {
287 TestAssertionConfig conf = modelItemConfig.addNewAssertion();
288 conf.set( ( ( WsdlMessageAssertion )source ).getConfig() );
289 conf.setName( name );
290
291 WsdlMessageAssertion result = addWsdlAssertion( conf );
292 fireAssertionAdded( result );
293 return result;
294
295 }
296
297 public WsdlMessageAssertion addWsdlAssertion( String assertionLabel )
298 {
299 try
300 {
301 TestAssertionConfig assertionConfig = modelItemConfig.addNewAssertion();
302 assertionConfig.setType( TestAssertionRegistry.getInstance().getAssertionTypeForName( assertionLabel ) );
303
304 WsdlMessageAssertion assertion = addWsdlAssertion( assertionConfig );
305 if( assertion == null )
306 return null;
307
308 fireAssertionAdded( assertion );
309
310 return assertion;
311 }
312 catch( Exception e )
313 {
314 SoapUI.logError( e );
315 return null;
316 }
317 }
318
319 public void resolve( ResolveContext<?> context )
320 {
321 for( WsdlMessageAssertion assertion : assertions )
322 {
323 assertion.resolve( context );
324 }
325 }
326 }