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.testcase;
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.support.SoapUIException;
033: import com.eviware.soapui.support.UISupport;
034: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
035: import com.eviware.x.form.XFormDialog;
036: import com.eviware.x.form.XFormField;
037: import com.eviware.x.form.XFormFieldListener;
038: import com.eviware.x.form.support.ADialogBuilder;
039: import com.eviware.x.form.support.AField;
040: import com.eviware.x.form.support.AForm;
041: import com.eviware.x.form.support.AField.AFieldType;
042:
043: /**
044: * Clones a WsdlTestSuite
045: *
046: * @author Ole.Matzura
047: */
048:
049: public class CloneTestCaseAction extends
050: AbstractSoapUIAction<WsdlTestCase> {
051: private static final String CREATE_NEW_OPTION = "<Create New>";
052: private XFormDialog dialog;
053:
054: public CloneTestCaseAction() {
055: super ("Clone TestCase", "Clones this TestCase");
056: }
057:
058: public void perform(WsdlTestCase testCase, Object param) {
059: if (dialog == null) {
060: dialog = ADialogBuilder.buildDialog(Form.class);
061: dialog.getFormField(Form.PROJECT).addFormFieldListener(
062: new XFormFieldListener() {
063:
064: public void valueChanged(
065: XFormField sourceField,
066: String newValue, String oldValue) {
067: if (newValue.equals(CREATE_NEW_OPTION))
068: dialog
069: .setOptions(
070: Form.TESTSUITE,
071: new String[] { CREATE_NEW_OPTION });
072: else {
073: Project project = SoapUI.getWorkspace()
074: .getProjectByName(newValue);
075: dialog
076: .setOptions(
077: Form.TESTSUITE,
078: ModelSupport
079: .getNames(
080: project
081: .getTestSuites(),
082: new String[] { CREATE_NEW_OPTION }));
083: }
084: }
085: });
086: }
087:
088: dialog.setValue(Form.NAME, "Copy of " + testCase.getName());
089: WorkspaceImpl workspace = testCase.getTestSuite().getProject()
090: .getWorkspace();
091: dialog.setOptions(Form.PROJECT, ModelSupport.getNames(workspace
092: .getProjectList(), new String[] { CREATE_NEW_OPTION }));
093:
094: dialog.setValue(Form.PROJECT, testCase.getTestSuite()
095: .getProject().getName());
096:
097: dialog.setOptions(Form.TESTSUITE, ModelSupport.getNames(
098: testCase.getTestSuite().getProject().getTestSuites(),
099: new String[] { CREATE_NEW_OPTION }));
100:
101: dialog.setValue(Form.TESTSUITE, testCase.getTestSuite()
102: .getName());
103: boolean hasLoadTests = testCase.getLoadTestCount() > 0;
104: dialog.setBooleanValue(Form.CLONE_LOADTESTS, hasLoadTests);
105: dialog.getFormField(Form.CLONE_LOADTESTS).setEnabled(
106: hasLoadTests);
107:
108: if (dialog.show()) {
109: String targetProjectName = dialog.getValue(Form.PROJECT);
110: String targetTestSuiteName = dialog
111: .getValue(Form.TESTSUITE);
112: String name = dialog.getValue(Form.NAME);
113:
114: WsdlProject project = testCase.getTestSuite().getProject();
115: WsdlTestSuite targetTestSuite = null;
116: Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
117:
118: // to another project project?
119: if (!targetProjectName.equals(project.getName())) {
120: // get required interfaces
121: for (int y = 0; y < testCase.getTestStepCount(); y++) {
122: WsdlTestStep testStep = testCase.getTestStepAt(y);
123: requiredInterfaces.addAll(testStep
124: .getRequiredInterfaces());
125: }
126:
127: project = (WsdlProject) workspace
128: .getProjectByName(targetProjectName);
129: if (project == null) {
130: targetProjectName = UISupport.prompt(
131: "Enter name for new Project",
132: "Clone TestCase", "");
133: if (targetProjectName == null)
134: return;
135:
136: try {
137: project = workspace
138: .createProject(targetProjectName);
139: } catch (SoapUIException e) {
140: UISupport.showErrorMessage(e);
141: }
142:
143: if (project == null)
144: return;
145: }
146:
147: if (requiredInterfaces.size() > 0
148: && project.getInterfaceCount() > 0) {
149: Map<QName, WsdlInterface> bindings = new HashMap<QName, WsdlInterface>();
150: for (WsdlInterface iface : requiredInterfaces) {
151: bindings.put(iface.getBindingName(), iface);
152: }
153:
154: for (Interface iface : project.getInterfaces()) {
155: bindings.remove(iface.getBindingName());
156: }
157:
158: requiredInterfaces.retainAll(bindings.values());
159: }
160:
161: if (requiredInterfaces.size() > 0) {
162: String msg = "Target project ["
163: + targetProjectName
164: + "] is missing required interfaces;\r\n\r\n";
165: for (WsdlInterface iface : requiredInterfaces) {
166: msg += iface.getName() + " ["
167: + iface.getBindingName() + "]\r\n";
168: }
169: msg += "\r\nThese will be cloned to the targetProject as well";
170:
171: if (!UISupport.confirm(msg, "Clone TestCase"))
172: return;
173:
174: for (WsdlInterface iface : requiredInterfaces) {
175: project.importInterface(iface);
176: }
177: }
178: }
179:
180: targetTestSuite = project
181: .getTestSuiteByName(targetTestSuiteName);
182: if (targetTestSuite == null) {
183: targetTestSuiteName = UISupport.prompt(
184: "Specify name for new TestSuite",
185: "Clone TestCase", "Copy of "
186: + testCase.getTestSuite().getName());
187: if (targetTestSuiteName == null)
188: return;
189:
190: targetTestSuite = project
191: .addNewTestSuite(targetTestSuiteName);
192: }
193:
194: WsdlTestCase newTestCase = targetTestSuite.importTestCase(
195: testCase, name, -1, dialog
196: .getBooleanValue(Form.CLONE_LOADTESTS));
197: UISupport.select(newTestCase);
198:
199: if (dialog.getBooleanValue(Form.MOVE)) {
200: testCase.getTestSuite().removeTestCase(testCase);
201: }
202: }
203: }
204:
205: @AForm(description="Specify target Project/TestSuite and name of cloned TestCase",name="Clone TestCase")
206: protected interface Form {
207: @AField(name="TestCase Name",description="The name of the cloned TestCase",type=AFieldType.STRING)
208: public final static String NAME = "TestCase Name";
209:
210: @AField(name="Target TestSuite",description="The target TestSuite for the cloned TestCase",type=AFieldType.ENUMERATION)
211: public final static String TESTSUITE = "Target TestSuite";
212:
213: @AField(name="Target Project",description="The target Project for the cloned TestCase",type=AFieldType.ENUMERATION)
214: public final static String PROJECT = "Target Project";
215:
216: @AField(name="Clone LoadTests",description="Clone contained LoadTests",type=AFieldType.BOOLEAN)
217: public final static String CLONE_LOADTESTS = "Clone LoadTests";
218:
219: @AField(name="Move instead",description="Moves the selected TestCase instead of copying",type=AFieldType.BOOLEAN)
220: public final static String MOVE = "Move instead";
221: }
222: }
|