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.io.PrintWriter;
016: import java.util.ArrayList;
017: import java.util.Arrays;
018: import java.util.List;
019:
020: import javax.swing.ImageIcon;
021:
022: import com.eviware.soapui.config.TestStepConfig;
023: import com.eviware.soapui.config.TransferValuesStepConfig;
024: import com.eviware.soapui.config.ValueTransferConfig;
025: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
026: import com.eviware.soapui.impl.wsdl.teststeps.actions.ShowTransferValuesResultsAction;
027: import com.eviware.soapui.model.testsuite.TestRunContext;
028: import com.eviware.soapui.model.testsuite.TestRunner;
029: import com.eviware.soapui.model.testsuite.TestStepResult;
030: import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
031: import com.eviware.soapui.support.UISupport;
032:
033: /**
034: * WsdlTestStep for transferring values from a WsdlTestRequest response to a
035: * WsdlTestRequest request using XPath expressions
036: *
037: * @author Ole.Matzura
038: */
039:
040: public class TransferResponseValuesTestStep extends WsdlTestStep {
041: private TransferValuesStepConfig transferStepConfig;
042: private boolean canceled;
043: private List<PropertyTransfer> transfers = new ArrayList<PropertyTransfer>();
044: private ImageIcon failedIcon;
045: private ImageIcon okIcon;
046:
047: public TransferResponseValuesTestStep(WsdlTestCase testCase,
048: TestStepConfig config, boolean forLoadTest) {
049: super (testCase, config, true, forLoadTest);
050:
051: if (!forLoadTest) {
052: okIcon = UISupport.createImageIcon("/value_transfer.gif");
053: failedIcon = UISupport
054: .createImageIcon("/value_transfer_failed.gif");
055:
056: setIcon(okIcon);
057:
058: // addAction( ActionSupport.SEPARATOR_ACTION );
059: // addAction( new CloneTestStepAction( this, "PropertyTransfer" ) );
060: // addAction( ActionSupport.SEPARATOR_ACTION );
061: // addAction( new ShowOnlineHelpAction( HelpUrls.PROPERTYTRANSFER_HELP_URL ));
062: }
063:
064: if (testCase == null)
065: postInit(config);
066: }
067:
068: public void postInit(TestStepConfig config) {
069: if (config.getConfig() != null) {
070: transferStepConfig = (TransferValuesStepConfig) config
071: .getConfig().changeType(
072: TransferValuesStepConfig.type);
073: for (int c = 0; c < transferStepConfig
074: .sizeOfTransfersArray(); c++) {
075: transfers.add(new PropertyTransfer(this ,
076: transferStepConfig.getTransfersArray(c)));
077: }
078: } else {
079: transferStepConfig = (TransferValuesStepConfig) config
080: .addNewConfig().changeType(
081: TransferValuesStepConfig.type);
082: }
083: }
084:
085: public TransferValuesStepConfig getTransferConfig() {
086: return transferStepConfig;
087: }
088:
089: public void resetConfigOnMove(TestStepConfig config) {
090: super .resetConfigOnMove(config);
091:
092: transferStepConfig = (TransferValuesStepConfig) config
093: .getConfig().changeType(TransferValuesStepConfig.type);
094: for (int c = 0; c < transferStepConfig.sizeOfTransfersArray(); c++) {
095: transfers.get(c).setConfigOnMove(
096: transferStepConfig.getTransfersArray(c));
097: }
098: }
099:
100: public TestStepResult run(TestRunner runner, TestRunContext context) {
101: ValueTransferResult result = new ValueTransferResult();
102: result.addAction(new ShowTransferValuesResultsAction(result),
103: true);
104:
105: canceled = false;
106:
107: long startTime = System.currentTimeMillis();
108:
109: for (int c = 0; c < transfers.size(); c++) {
110: PropertyTransfer valueTransfer = transfers.get(c);
111:
112: try {
113: if (canceled) {
114: result.setStatus(TestStepStatus.CANCELED);
115: result.setTimeTaken(System.currentTimeMillis()
116: - startTime);
117: return result;
118: }
119:
120: String[] values = valueTransfer
121: .transferProperties(context);
122: if (values != null && values.length > 0) {
123: String name = valueTransfer.getName();
124: result.addMessage("Performed transfer [" + name
125: + "]");
126: result.addTransferResult(valueTransfer, values);
127: }
128: } catch (PropertyTransferException e) {
129: result.addMessage("Error performing transfer ["
130: + valueTransfer.getName() + "]");
131: result.addTransferResult(valueTransfer,
132: new String[] { e.toString() });
133:
134: if (transfers.get(c).getFailOnError()) {
135: result.setError(e);
136: result.setStatus(TestStepStatus.FAILED);
137: result.setTimeTaken(System.currentTimeMillis()
138: - startTime);
139:
140: if (failedIcon != null)
141: setIcon(failedIcon);
142:
143: return result;
144: }
145: }
146: }
147:
148: if (okIcon != null)
149: setIcon(okIcon);
150:
151: result.setStatus(TestStepStatus.OK);
152: result.setTimeTaken(System.currentTimeMillis() - startTime);
153:
154: return result;
155: }
156:
157: public boolean cancel() {
158: canceled = true;
159: return canceled;
160: }
161:
162: public int getTransferCount() {
163: return transferStepConfig.sizeOfTransfersArray();
164: }
165:
166: public PropertyTransfer getTransferAt(int index) {
167: return transfers.get(index);
168: }
169:
170: public PropertyTransfer addTransfer(String name) {
171: PropertyTransfer transfer = new PropertyTransfer(this ,
172: transferStepConfig.addNewTransfers());
173: transfer.setName(name);
174: transfer.setFailOnError(true);
175: transfers.add(transfer);
176: return transfer;
177: }
178:
179: public void removeTransferAt(int index) {
180: transfers.remove(index).release();
181: transferStepConfig.removeTransfers(index);
182: }
183:
184: public TestStepResult createFailedResult(String message) {
185: ValueTransferResult result = new ValueTransferResult();
186: result.setStatus(TestStepStatus.FAILED);
187: result.addMessage(message);
188:
189: return result;
190: }
191:
192: public void release() {
193: super .release();
194:
195: for (PropertyTransfer transfer : transfers) {
196: transfer.release();
197: }
198: }
199:
200: public class ValueTransferResult extends WsdlTestStepResult {
201: private List<ValueTransferConfig> transfers = new ArrayList<ValueTransferConfig>();
202: private List<String[]> values = new ArrayList<String[]>();
203:
204: public ValueTransferResult() {
205: super (TransferResponseValuesTestStep.this );
206: }
207:
208: public void addTransferResult(PropertyTransfer transfer,
209: String[] values) {
210: // save a copy, so we dont mirror changes
211: transfers.add((ValueTransferConfig) transfer.getConfig()
212: .copy());
213: this .values.add(values);
214: }
215:
216: public int getTransferCount() {
217: return transfers == null ? 0 : transfers.size();
218: }
219:
220: public ValueTransferConfig getTransferAt(int index) {
221: return transfers == null ? null : transfers.get(index);
222: }
223:
224: public String[] getTransferredValuesAt(int index) {
225: return values == null ? null : values.get(index);
226: }
227:
228: public void discard() {
229: super .discard();
230:
231: transfers = null;
232: values = null;
233: }
234:
235: public void writeTo(PrintWriter writer) {
236: super .writeTo(writer);
237:
238: if (!isDiscarded()) {
239: writer
240: .println("----------------------------------------------------");
241: for (int c = 0; c < transfers.size(); c++) {
242: ValueTransferConfig transfer = transfers.get(c);
243: writer.println(transfer.getName()
244: + " transferred ["
245: + Arrays.toString(values.get(c))
246: + "] from [" + transfer.getSourceStep()
247: + "." + transfer.getSourceType() + "] to ["
248: + transfer.getTargetStep() + "."
249: + transfer.getTargetType() + "]");
250: if (transfer.getSourcePath() != null) {
251: writer
252: .println("------------ source path -------------");
253: writer.println(transfer.getSourcePath());
254: }
255: if (transfer.getTargetPath() != null) {
256: writer
257: .println("------------ target path -------------");
258: writer.println(transfer.getTargetPath());
259: }
260: }
261: }
262: }
263: }
264:
265: public PropertyTransfer getTransferByName(String name) {
266: for (int c = 0; c < getTransferCount(); c++) {
267: PropertyTransfer transfer = getTransferAt(c);
268: if (transfer.getName().equals(name))
269: return transfer;
270: }
271:
272: return null;
273: }
274: }
|