1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps;
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.config.RequestAssertionConfig;
22 import com.eviware.soapui.impl.wsdl.panels.support.assertions.Assertable;
23 import com.eviware.soapui.impl.wsdl.panels.support.assertions.AssertionsListener;
24 import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry;
25
26 public class AssertionsSupport implements PropertyChangeListener
27 {
28 private List<AssertionsListener> assertionsListeners = new ArrayList<AssertionsListener>();
29 private List<WsdlMessageAssertion> assertions = new ArrayList<WsdlMessageAssertion>();
30 private final Assertable assertable;
31
32 public AssertionsSupport( Assertable assertable, List<RequestAssertionConfig> assertionList )
33 {
34 this.assertable = assertable;
35
36 for( RequestAssertionConfig rac : assertionList )
37 {
38 addWsdlAssertion( rac );
39 }
40 }
41
42 public WsdlMessageAssertion addWsdlAssertion( RequestAssertionConfig config )
43 {
44 try
45 {
46 WsdlMessageAssertion assertion = WsdlAssertionRegistry.getInstance().buildAssertion(
47 config, assertable );
48 if( assertion == null )
49 {
50 return null;
51 }
52 else
53 {
54 assertions.add( assertion );
55 assertion.addPropertyChangeListener( this );
56
57 return assertion;
58 }
59 }
60 catch( Exception e )
61 {
62 e.printStackTrace();
63 return null;
64 }
65 }
66
67 public void propertyChange( PropertyChangeEvent arg0 )
68 {
69 if( assertable instanceof PropertyChangeListener )
70 ((PropertyChangeListener)assertable).propertyChange( arg0 );
71 }
72
73 public int getAssertionCount()
74 {
75 return assertions.size();
76 }
77
78 public WsdlMessageAssertion getAssertionAt( int c )
79 {
80 return assertions.get( c );
81 }
82
83 public void addAssertionsListener( AssertionsListener listener )
84 {
85 assertionsListeners.add( listener );
86 }
87
88 public void removeAssertionsListener( AssertionsListener listener )
89 {
90 assertionsListeners.remove( listener );
91 }
92
93 public int removeAssertion( WsdlMessageAssertion assertion )
94 {
95 int ix = assertions.indexOf( assertion );
96 if( ix == -1 )
97 {
98 throw new RuntimeException( "assertion [" + assertion.getName() + "] not available " );
99 }
100
101 assertion.removePropertyChangeListener( this );
102 assertions.remove( ix );
103 fireAssertionRemoved( assertion );
104
105 return ix;
106 }
107
108 public void release()
109 {
110 for( WsdlMessageAssertion assertion : assertions )
111 assertion.release();
112
113 }
114
115 public Iterator<WsdlMessageAssertion> iterator()
116 {
117 return assertions.iterator();
118 }
119
120 public void fireAssertionAdded( WsdlMessageAssertion assertion )
121 {
122 AssertionsListener[] listeners = assertionsListeners
123 .toArray( new AssertionsListener[assertionsListeners.size()] );
124
125 for( int c = 0; c < listeners.length; c++ )
126 {
127 listeners[c].assertionAdded( assertion );
128 }
129 }
130
131 public void fireAssertionRemoved( 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].assertionRemoved( assertion );
139 }
140 }
141
142 public void updateConfig( List<RequestAssertionConfig> assertionList )
143 {
144 for (int i = 0; i < assertionList.size(); i++)
145 {
146 RequestAssertionConfig config = assertionList.get( i );
147 assertions.get( i ).updateConfig( config );
148 }
149 }
150 }