001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.teststeps;
014:
015: import java.beans.PropertyChangeEvent;
016: import java.beans.PropertyChangeListener;
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020:
021: import com.eviware.soapui.SoapUI;
022: import com.eviware.soapui.config.RequestAssertionConfig;
023: import com.eviware.soapui.impl.wsdl.support.assertions.Assertable;
024: import com.eviware.soapui.impl.wsdl.support.assertions.AssertionsListener;
025: import com.eviware.soapui.impl.wsdl.teststeps.assertions.WsdlAssertionRegistry;
026:
027: /**
028: * Utility for implementing the Assertable interface
029: *
030: * @author ole.matzura
031: */
032:
033: public class AssertionsSupport implements PropertyChangeListener {
034: private List<AssertionsListener> assertionsListeners = new ArrayList<AssertionsListener>();
035: private List<WsdlMessageAssertion> assertions = new ArrayList<WsdlMessageAssertion>();
036: private final Assertable assertable;
037:
038: public AssertionsSupport(Assertable assertable,
039: List<RequestAssertionConfig> assertionList) {
040: this .assertable = assertable;
041:
042: for (RequestAssertionConfig rac : assertionList) {
043: addWsdlAssertion(rac);
044: }
045: }
046:
047: public WsdlMessageAssertion addWsdlAssertion(
048: RequestAssertionConfig config) {
049: try {
050: WsdlMessageAssertion assertion = WsdlAssertionRegistry
051: .getInstance().buildAssertion(config, assertable);
052: if (assertion == null) {
053: return null;
054: } else {
055: assertions.add(assertion);
056: assertion.addPropertyChangeListener(this );
057:
058: return assertion;
059: }
060: } catch (Exception e) {
061: SoapUI.logError(e);
062: return null;
063: }
064: }
065:
066: public void propertyChange(PropertyChangeEvent arg0) {
067: if (assertable instanceof PropertyChangeListener)
068: ((PropertyChangeListener) assertable).propertyChange(arg0);
069: }
070:
071: public int getAssertionCount() {
072: return assertions.size();
073: }
074:
075: public WsdlMessageAssertion getAssertionAt(int c) {
076: return assertions.get(c);
077: }
078:
079: public void addAssertionsListener(AssertionsListener listener) {
080: assertionsListeners.add(listener);
081: }
082:
083: public void removeAssertionsListener(AssertionsListener listener) {
084: assertionsListeners.remove(listener);
085: }
086:
087: public int removeAssertion(WsdlMessageAssertion assertion) {
088: int ix = assertions.indexOf(assertion);
089: if (ix == -1) {
090: throw new RuntimeException("assertion ["
091: + assertion.getName() + "] not available ");
092: }
093:
094: assertion.removePropertyChangeListener(this );
095: assertions.remove(ix);
096: fireAssertionRemoved(assertion);
097:
098: return ix;
099: }
100:
101: public void release() {
102: for (WsdlMessageAssertion assertion : assertions)
103: assertion.release();
104:
105: }
106:
107: public Iterator<WsdlMessageAssertion> iterator() {
108: return assertions.iterator();
109: }
110:
111: public void fireAssertionAdded(WsdlMessageAssertion assertion) {
112: AssertionsListener[] listeners = assertionsListeners
113: .toArray(new AssertionsListener[assertionsListeners
114: .size()]);
115:
116: for (int c = 0; c < listeners.length; c++) {
117: listeners[c].assertionAdded(assertion);
118: }
119: }
120:
121: public void fireAssertionRemoved(WsdlMessageAssertion assertion) {
122: AssertionsListener[] listeners = assertionsListeners
123: .toArray(new AssertionsListener[assertionsListeners
124: .size()]);
125:
126: for (int c = 0; c < listeners.length; c++) {
127: listeners[c].assertionRemoved(assertion);
128: }
129: }
130:
131: public void updateConfig(List<RequestAssertionConfig> assertionList) {
132: int mod = 0;
133:
134: for (int i = 0; i < assertionList.size(); i++) {
135: RequestAssertionConfig config = assertionList.get(i);
136: if (WsdlAssertionRegistry.getInstance().canBuildAssertion(
137: config)) {
138: assertions.get(i - mod).updateConfig(config);
139: } else
140: mod++;
141: }
142: }
143:
144: // public WsdlMessageAssertion cloneAssertion( WsdlMessageAssertion source, String name )
145: // {
146: // RequestAssertionConfig config = ( RequestAssertionConfig ) source.getConfig().copy();
147: // config.setName( name );
148: // WsdlMessageAssertion assertion = addWsdlAssertion( config );
149: // fireAssertionAdded( assertion );
150: // return assertion;
151: // }
152: }
|