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