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