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.actions.teststep;
014:
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.Map;
018: import java.util.Set;
019:
020: import javax.xml.namespace.QName;
021:
022: import com.eviware.soapui.SoapUI;
023: import com.eviware.soapui.impl.WorkspaceImpl;
024: import com.eviware.soapui.impl.wsdl.WsdlInterface;
025: import com.eviware.soapui.impl.wsdl.WsdlProject;
026: import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
027: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
028: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
029: import com.eviware.soapui.model.iface.Interface;
030: import com.eviware.soapui.model.project.Project;
031: import com.eviware.soapui.model.support.ModelSupport;
032: import com.eviware.soapui.model.testsuite.TestSuite;
033: import com.eviware.soapui.support.SoapUIException;
034: import com.eviware.soapui.support.UISupport;
035: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
036: import com.eviware.x.form.XFormDialog;
037: import com.eviware.x.form.XFormField;
038: import com.eviware.x.form.XFormFieldListener;
039: import com.eviware.x.form.support.ADialogBuilder;
040: import com.eviware.x.form.support.AField;
041: import com.eviware.x.form.support.AForm;
042: import com.eviware.x.form.support.AField.AFieldType;
043:
044: /**
045: * Clones a WsdlTestStep
046: *
047: * @author Ole.Matzura
048: */
049:
050: public class CloneTestStepAction extends
051: AbstractSoapUIAction<WsdlTestStep> {
052: private static final String CREATE_NEW_OPTION = "<Create New>";
053: private XFormDialog dialog;
054:
055: public CloneTestStepAction() {
056: super ("Clone TestStep", "Clones this TestStep");
057: }
058:
059: public void perform(WsdlTestStep testStep, Object param) {
060: if (dialog == null) {
061: dialog = ADialogBuilder.buildDialog(Form.class);
062: dialog.getFormField(Form.PROJECT).addFormFieldListener(
063: new XFormFieldListener() {
064:
065: public void valueChanged(
066: XFormField sourceField,
067: String newValue, String oldValue) {
068: if (newValue.equals(CREATE_NEW_OPTION))
069: dialog
070: .setOptions(
071: Form.TESTSUITE,
072: new String[] { CREATE_NEW_OPTION });
073: else {
074: Project project = SoapUI.getWorkspace()
075: .getProjectByName(newValue);
076: String[] names = ModelSupport
077: .getNames(
078: project.getTestSuites(),
079: new String[] { CREATE_NEW_OPTION });
080: dialog
081: .setOptions(Form.TESTSUITE,
082: names);
083: dialog.setValue(Form.TESTSUITE,
084: names[0]);
085:
086: if (names.length > 1) {
087: TestSuite testSuite = project
088: .getTestSuiteByName(names[0]);
089: dialog
090: .setOptions(
091: Form.TESTCASE,
092: ModelSupport
093: .getNames(
094: testSuite
095: .getTestCaseList(),
096: new String[] { CREATE_NEW_OPTION }));
097: } else {
098: dialog
099: .setOptions(
100: Form.TESTCASE,
101: new String[] { CREATE_NEW_OPTION });
102: }
103: }
104: }
105: });
106:
107: dialog.getFormField(Form.TESTSUITE).addFormFieldListener(
108: new XFormFieldListener() {
109:
110: public void valueChanged(
111: XFormField sourceField,
112: String newValue, String oldValue) {
113: if (newValue.equals(CREATE_NEW_OPTION)) {
114: dialog
115: .setOptions(
116: Form.TESTCASE,
117: new String[] { CREATE_NEW_OPTION });
118: } else {
119: String projectName = dialog
120: .getValue(Form.PROJECT);
121: Project project = SoapUI.getWorkspace()
122: .getProjectByName(projectName);
123: TestSuite testSuite = project
124: .getTestSuiteByName(newValue);
125: dialog
126: .setOptions(
127: Form.TESTCASE,
128: ModelSupport
129: .getNames(
130: testSuite
131: .getTestCaseList(),
132: new String[] { CREATE_NEW_OPTION }));
133: }
134: }
135: });
136:
137: }
138:
139: dialog.setValue(Form.NAME, "Copy of " + testStep.getName());
140: WorkspaceImpl workspace = testStep.getTestCase().getTestSuite()
141: .getProject().getWorkspace();
142: dialog.setOptions(Form.PROJECT, ModelSupport.getNames(workspace
143: .getProjectList(), new String[] { CREATE_NEW_OPTION }));
144:
145: dialog.setValue(Form.PROJECT, testStep.getTestCase()
146: .getTestSuite().getProject().getName());
147:
148: dialog.setOptions(Form.TESTSUITE, ModelSupport.getNames(
149: testStep.getTestCase().getTestSuite().getProject()
150: .getTestSuites(),
151: new String[] { CREATE_NEW_OPTION }));
152: dialog.setValue(Form.TESTSUITE, testStep.getTestCase()
153: .getTestSuite().getName());
154:
155: dialog.setOptions(Form.TESTCASE, ModelSupport.getNames(testStep
156: .getTestCase().getTestSuite().getTestCaseList(),
157: new String[] { CREATE_NEW_OPTION }));
158: dialog
159: .setValue(Form.TESTCASE, testStep.getTestCase()
160: .getName());
161:
162: if (dialog.show()) {
163: String targetProjectName = dialog.getValue(Form.PROJECT);
164: String targetTestSuiteName = dialog
165: .getValue(Form.TESTSUITE);
166: String targetTestCaseName = dialog.getValue(Form.TESTCASE);
167: String name = dialog.getValue(Form.NAME);
168:
169: WsdlProject project = testStep.getTestCase().getTestSuite()
170: .getProject();
171: WsdlTestSuite targetTestSuite = null;
172: WsdlTestCase targetTestCase = null;
173: Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
174:
175: // to another project project?
176: if (!targetProjectName.equals(project.getName())) {
177: // get required interfaces
178: requiredInterfaces.addAll(testStep
179: .getRequiredInterfaces());
180:
181: project = (WsdlProject) workspace
182: .getProjectByName(targetProjectName);
183: if (project == null) {
184: targetProjectName = UISupport.prompt(
185: "Enter name for new Project",
186: "Clone TestStep", "");
187: if (targetProjectName == null)
188: return;
189:
190: try {
191: project = workspace
192: .createProject(targetProjectName);
193: } catch (SoapUIException e) {
194: UISupport.showErrorMessage(e);
195: }
196:
197: if (project == null)
198: return;
199: }
200:
201: if (requiredInterfaces.size() > 0
202: && project.getInterfaceCount() > 0) {
203: Map<QName, WsdlInterface> bindings = new HashMap<QName, WsdlInterface>();
204: for (WsdlInterface iface : requiredInterfaces) {
205: bindings.put(iface.getBindingName(), iface);
206: }
207:
208: for (Interface iface : project.getInterfaces()) {
209: bindings.remove(iface.getBindingName());
210: }
211:
212: requiredInterfaces.retainAll(bindings.values());
213: }
214:
215: if (requiredInterfaces.size() > 0) {
216: String msg = "Target project ["
217: + targetProjectName
218: + "] is missing required interfaces;\r\n\r\n";
219: for (WsdlInterface iface : requiredInterfaces) {
220: msg += iface.getName() + " ["
221: + iface.getBindingName() + "]\r\n";
222: }
223: msg += "\r\nThese will be cloned to the targetProject as well";
224:
225: if (!UISupport.confirm(msg, "Clone TestStep"))
226: return;
227:
228: for (WsdlInterface iface : requiredInterfaces) {
229: project.importInterface(iface);
230: }
231: }
232: }
233:
234: targetTestSuite = project
235: .getTestSuiteByName(targetTestSuiteName);
236: if (targetTestSuite == null) {
237: targetTestSuiteName = UISupport.prompt(
238: "Specify name for new TestSuite",
239: "Clone TestStep", "Copy of "
240: + testStep.getTestCase().getTestSuite()
241: .getName());
242: if (targetTestSuiteName == null)
243: return;
244:
245: targetTestSuite = project
246: .addNewTestSuite(targetTestSuiteName);
247: }
248:
249: targetTestCase = targetTestSuite
250: .getTestCaseByName(targetTestCaseName);
251: if (targetTestCase == null) {
252: targetTestCaseName = UISupport.prompt(
253: "Specify name for new TestCase",
254: "Clone TestStep", "Copy of "
255: + testStep.getTestCase().getName());
256: if (targetTestCaseName == null)
257: return;
258:
259: targetTestCase = targetTestSuite
260: .addNewTestCase(targetTestCaseName);
261: }
262:
263: WsdlTestStep newTestStep = targetTestCase.importTestStep(
264: testStep, name, -1);
265: UISupport.select(newTestStep);
266:
267: if (dialog.getBooleanValue(Form.MOVE)) {
268: testStep.getTestCase().removeTestStep(testStep);
269: }
270: }
271: }
272:
273: @AForm(description="Specify target Project/TestSuite/TestCase and name of cloned TestStep",name="Clone TestStep")
274: protected interface Form {
275: @AField(name="TestStep Name",description="The name of the cloned TestStep",type=AFieldType.STRING)
276: public final static String NAME = "TestStep Name";
277:
278: @AField(name="Target TestCase",description="The target TestCase for the cloned TestStep",type=AFieldType.ENUMERATION)
279: public final static String TESTCASE = "Target TestCase";
280:
281: @AField(name="Target TestSuite",description="The target TestSuite for the cloned TestStep",type=AFieldType.ENUMERATION)
282: public final static String TESTSUITE = "Target TestSuite";
283:
284: @AField(name="Target Project",description="The target Project for the cloned TestStep",type=AFieldType.ENUMERATION)
285: public final static String PROJECT = "Target Project";
286:
287: @AField(name="Move instead",description="Moves the selected TestStep instead of copying",type=AFieldType.BOOLEAN)
288: public final static String MOVE = "Move instead";
289: }
290: }
|