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.beans.PropertyChangeSupport;
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.apache.log4j.Logger;
022: import org.apache.xmlbeans.XmlBoolean;
023: import org.apache.xmlbeans.XmlObject;
024:
025: import com.eviware.soapui.config.GotoConditionConfig;
026: import com.eviware.soapui.config.GotoConditionTypeConfig;
027: import com.eviware.soapui.config.GotoStepConfig;
028: import com.eviware.soapui.config.TestStepConfig;
029: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
030: import com.eviware.soapui.model.testsuite.TestRunContext;
031: import com.eviware.soapui.model.testsuite.TestRunner;
032: import com.eviware.soapui.model.testsuite.TestStep;
033: import com.eviware.soapui.model.testsuite.TestStepResult;
034: import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
035: import com.eviware.soapui.support.UISupport;
036:
037: /**
038: * TestStep that moves execution to another step based on the contents of a XML Property
039: *
040: * @author ole.matzura
041: */
042:
043: public class WsdlGotoTestStep extends WsdlTestStep {
044: private GotoStepConfig gotoStepConfig;
045: private List<GotoCondition> conditions = new ArrayList<GotoCondition>();
046: private boolean canceled;
047:
048: private final static Logger log = Logger
049: .getLogger(WsdlGotoTestStep.class);
050:
051: public WsdlGotoTestStep(WsdlTestCase testCase,
052: TestStepConfig config, boolean forLoadTest) {
053: super (testCase, config, true, forLoadTest);
054:
055: if (!forLoadTest) {
056: setIcon(UISupport.createImageIcon("/goto.gif"));
057: }
058: }
059:
060: public void postInit(TestStepConfig config) {
061: if (config.getConfig() == null) {
062: gotoStepConfig = (GotoStepConfig) config.addNewConfig()
063: .changeType(GotoStepConfig.type);
064: } else {
065: gotoStepConfig = (GotoStepConfig) config.getConfig()
066: .changeType(GotoStepConfig.type);
067: for (int c = 0; c < gotoStepConfig.sizeOfConditionArray(); c++) {
068: conditions.add(new GotoCondition(gotoStepConfig
069: .getConditionArray(c)));
070: }
071: }
072: }
073:
074: public void resetConfigOnMove(TestStepConfig config) {
075: super .resetConfigOnMove(config);
076:
077: gotoStepConfig = (GotoStepConfig) config.getConfig()
078: .changeType(GotoStepConfig.type);
079: for (int c = 0; c < gotoStepConfig.sizeOfConditionArray(); c++) {
080: conditions.get(c).setConfig(
081: gotoStepConfig.getConditionArray(c));
082: }
083: }
084:
085: public TestStepResult run(TestRunner runner, TestRunContext context) {
086: WsdlTestStepResult result = new WsdlTestStepResult(this );
087: canceled = false;
088:
089: result.startTimer();
090:
091: WsdlTestRequestStep previousStep = (WsdlTestRequestStep) getTestCase()
092: .findPreviousStepOfType(this , WsdlTestRequestStep.class);
093:
094: if (previousStep == null) {
095: result.stopTimer();
096: result
097: .addMessage("Failed to find previous request step from ["
098: + getName() + "]");
099: result.setStatus(TestStepStatus.FAILED);
100: return result;
101: }
102:
103: GotoCondition target = runConditions(previousStep);
104: if (target == null) {
105: result.addMessage("Missing matching condition, moving on.");
106: } else {
107: String targetStepName = target.getTargetStep();
108: result.addMessage("Matched condition [" + targetStepName
109: + "]");
110: runner.gotoStep(runner.getTestCase()
111: .getTestStepIndexByName(targetStepName));
112: }
113:
114: result.stopTimer();
115: result.setStatus(TestStepStatus.OK);
116: return result;
117: }
118:
119: public GotoCondition runConditions(WsdlTestRequestStep previousStep) {
120: for (GotoCondition condition : conditions) {
121: if (canceled)
122: break;
123:
124: try {
125: if (condition.evaluate(previousStep)) {
126: return condition;
127: }
128: } catch (Exception e) {
129: log.error("Error making condition "
130: + condition.getName() + "; " + e);
131: }
132: }
133:
134: return null;
135: }
136:
137: public boolean cancel() {
138: canceled = true;
139: return canceled;
140: }
141:
142: public int getConditionCount() {
143: return conditions.size();
144: }
145:
146: public GotoCondition getConditionAt(int index) {
147: return conditions.get(index);
148: }
149:
150: public GotoCondition addCondition(String name) {
151: GotoCondition condition = new GotoCondition(gotoStepConfig
152: .addNewCondition());
153: condition.setName(name);
154: condition.setType(GotoConditionTypeConfig.XPATH.toString());
155: conditions.add(condition);
156: return condition;
157: }
158:
159: public void removeConditionAt(int index) {
160: conditions.remove(index);
161: gotoStepConfig.removeCondition(index);
162: }
163:
164: public void release() {
165: super .release();
166:
167: for (GotoCondition condition : conditions) {
168: condition.release();
169: }
170: }
171:
172: public class GotoCondition implements PropertyChangeListener {
173: public final static String TARGET_STEP_PROPERTY = "target_step";
174:
175: private GotoConditionConfig conditionConfig;
176: private TestStep currentStep;
177: private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
178: this );
179:
180: public GotoCondition(GotoConditionConfig conditionConfig) {
181: this .conditionConfig = conditionConfig;
182: initListeners();
183: }
184:
185: public void addPropertyChangeListener(String propertyName,
186: PropertyChangeListener listener) {
187: propertyChangeSupport.addPropertyChangeListener(
188: propertyName, listener);
189: }
190:
191: public void addPropertyChangeListener(
192: PropertyChangeListener listener) {
193: propertyChangeSupport.addPropertyChangeListener(listener);
194: }
195:
196: public void removePropertyChangeListener(String propertyName,
197: PropertyChangeListener listener) {
198: propertyChangeSupport.removePropertyChangeListener(
199: propertyName, listener);
200: }
201:
202: public void removePropertyChangeListener(
203: PropertyChangeListener listener) {
204: propertyChangeSupport
205: .removePropertyChangeListener(listener);
206: }
207:
208: private void initListeners() {
209: release();
210:
211: if (getTargetStep() != null) {
212: int index = getTestCase().getTestStepIndexByName(
213: getTargetStep());
214: if (index != -1) {
215: currentStep = getTestCase().getTestStepAt(index);
216: currentStep.addPropertyChangeListener(
217: TestStep.NAME_PROPERTY, this );
218: }
219: }
220: }
221:
222: public void release() {
223: if (currentStep != null)
224: currentStep.removePropertyChangeListener(this );
225: }
226:
227: public boolean evaluate(WsdlTestRequestStep previousStep)
228: throws Exception {
229: if (getExpression() == null
230: || getExpression().trim().length() == 0)
231: throw new Exception("Missing expression in condition ["
232: + getName() + "]");
233:
234: if (getTargetStep() == null
235: || getTargetStep().trim().length() == 0)
236: throw new Exception(
237: "Missing target step in condition ["
238: + getName() + "]");
239:
240: if (getType().equals(
241: GotoConditionTypeConfig.XPATH.toString())) {
242: XmlObject xmlObject = XmlObject.Factory
243: .parse(previousStep.getTestRequest()
244: .getResponse().getContentAsString());
245: XmlObject[] selectPath = xmlObject
246: .selectPath(getExpression());
247: if (selectPath.length == 1
248: && selectPath[0] instanceof XmlBoolean) {
249: if (((XmlBoolean) selectPath[0]).getBooleanValue()) {
250: return true;
251: }
252: }
253: } else {
254: log.error("Unkown condition type: " + getType());
255: }
256:
257: return false;
258: }
259:
260: protected void setConfig(GotoConditionConfig conditionConfig) {
261: this .conditionConfig = conditionConfig;
262: }
263:
264: public String getType() {
265: return conditionConfig.getType();
266: }
267:
268: public String getName() {
269: return conditionConfig.getName();
270: }
271:
272: public String getExpression() {
273: return conditionConfig.getExpression();
274: }
275:
276: public String getTargetStep() {
277: return conditionConfig.getTargetStep();
278: }
279:
280: public void setType(String type) {
281: conditionConfig.setType(type);
282: }
283:
284: public void setName(String name) {
285: conditionConfig.setName(name);
286: }
287:
288: public void setExpression(String expression) {
289: conditionConfig.setExpression(expression);
290: }
291:
292: public void setTargetStep(String targetStep) {
293: String oldStep = getTargetStep();
294: conditionConfig.setTargetStep(targetStep);
295: initListeners();
296: propertyChangeSupport.firePropertyChange(
297: TARGET_STEP_PROPERTY, oldStep, targetStep);
298: }
299:
300: public void propertyChange(PropertyChangeEvent evt) {
301: conditionConfig.setTargetStep(evt.getNewValue().toString());
302: propertyChangeSupport.firePropertyChange(
303: TARGET_STEP_PROPERTY, evt.getOldValue(), evt
304: .getNewValue());
305: }
306: }
307: }
|