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.testsuite;
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.impl.WorkspaceImpl;
023: import com.eviware.soapui.impl.wsdl.WsdlInterface;
024: import com.eviware.soapui.impl.wsdl.WsdlProject;
025: import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
026: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
027: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
028: import com.eviware.soapui.model.iface.Interface;
029: import com.eviware.soapui.model.support.ModelSupport;
030: import com.eviware.soapui.support.SoapUIException;
031: import com.eviware.soapui.support.UISupport;
032: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
033: import com.eviware.x.form.XFormDialog;
034: import com.eviware.x.form.support.ADialogBuilder;
035: import com.eviware.x.form.support.AField;
036: import com.eviware.x.form.support.AForm;
037: import com.eviware.x.form.support.AField.AFieldType;
038:
039: /**
040: * Clones a WsdlTestSuite
041: *
042: * @author Ole.Matzura
043: */
044:
045: public class CloneTestSuiteAction extends
046: AbstractSoapUIAction<WsdlTestSuite> {
047: private XFormDialog dialog;
048:
049: public CloneTestSuiteAction() {
050: super ("Clone TestSuite", "Clones this TestSuite");
051: }
052:
053: public void perform(WsdlTestSuite testSuite, Object param) {
054: if (dialog == null)
055: dialog = ADialogBuilder.buildDialog(Form.class);
056:
057: dialog.setValue(Form.NAME, "Copy of " + testSuite.getName());
058: WorkspaceImpl workspace = testSuite.getProject().getWorkspace();
059: dialog.setOptions(Form.PROJECT, ModelSupport.getNames(workspace
060: .getProjectList(), new String[] { "<Create New>" }));
061:
062: dialog.setValue(Form.PROJECT, testSuite.getProject().getName());
063:
064: if (dialog.show()) {
065: String targetProjectName = dialog.getValue(Form.PROJECT);
066: String name = dialog.getValue(Form.NAME);
067:
068: WsdlProject project = (WsdlProject) testSuite.getProject();
069:
070: // within same project?
071: if (targetProjectName.equals(testSuite.getProject()
072: .getName())) {
073: cloneTestSuiteWithinProject(testSuite, name, project);
074: } else {
075: cloneToAnotherProject(testSuite, targetProjectName,
076: name);
077: }
078:
079: if (dialog.getBooleanValue(Form.MOVE)) {
080: testSuite.getProject().removeTestSuite(testSuite);
081: }
082: }
083: }
084:
085: public static WsdlTestSuite cloneToAnotherProject(
086: WsdlTestSuite testSuite, String targetProjectName,
087: String name) {
088: WorkspaceImpl workspace = testSuite.getProject().getWorkspace();
089: WsdlProject targetProject = (WsdlProject) workspace
090: .getProjectByName(targetProjectName);
091: if (targetProject == null) {
092: targetProjectName = UISupport
093: .prompt("Enter name for new Project",
094: "Clone TestSuite", "");
095: if (targetProjectName == null)
096: return null;
097:
098: try {
099: targetProject = workspace
100: .createProject(targetProjectName);
101: } catch (SoapUIException e) {
102: UISupport.showErrorMessage(e);
103: }
104:
105: if (targetProject == null)
106: return null;
107: }
108:
109: Set<WsdlInterface> requiredInterfaces = getRequiredInterfaces(
110: testSuite, targetProject);
111:
112: if (requiredInterfaces.size() > 0) {
113: String msg = "Target project [" + targetProjectName
114: + "] is missing required interfaces;\r\n\r\n";
115: for (WsdlInterface iface : requiredInterfaces) {
116: msg += iface.getName() + " [" + iface.getBindingName()
117: + "]\r\n";
118: }
119: msg += "\r\nThese will be cloned to the targetProject as well";
120:
121: if (!UISupport.confirm(msg, "Clone TestSuite"))
122: return null;
123:
124: for (WsdlInterface iface : requiredInterfaces) {
125: targetProject.importInterface(iface);
126: }
127: }
128:
129: testSuite = targetProject.importTestSuite(testSuite, name);
130: UISupport.select(testSuite);
131:
132: return testSuite;
133: }
134:
135: public static boolean cloneTestSuiteWithinProject(
136: WsdlTestSuite testSuite, String name, WsdlProject project) {
137: WsdlTestSuite newTestSuite = project.cloneTestSuite(testSuite,
138: name);
139: UISupport.select(newTestSuite);
140: return true;
141: }
142:
143: public static Set<WsdlInterface> getRequiredInterfaces(
144: WsdlTestSuite testSuite, WsdlProject targetProject) {
145: Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
146:
147: for (int i = 0; i < testSuite.getTestCaseCount(); i++) {
148: WsdlTestCase testCase = testSuite.getTestCaseAt(i);
149:
150: for (int y = 0; y < testCase.getTestStepCount(); y++) {
151: WsdlTestStep testStep = testCase.getTestStepAt(y);
152: requiredInterfaces.addAll(testStep
153: .getRequiredInterfaces());
154: }
155: }
156:
157: if (requiredInterfaces.size() > 0
158: && targetProject.getInterfaceCount() > 0) {
159: Map<QName, WsdlInterface> bindings = new HashMap<QName, WsdlInterface>();
160: for (WsdlInterface iface : requiredInterfaces) {
161: bindings.put(iface.getBindingName(), iface);
162: }
163:
164: for (Interface iface : targetProject.getInterfaces()) {
165: bindings.remove(iface.getBindingName());
166: }
167:
168: requiredInterfaces.retainAll(bindings.values());
169: }
170: return requiredInterfaces;
171: }
172:
173: @AForm(description="Specify target Project and name of cloned TestSuite",name="Clone TestSuite")
174: protected interface Form {
175: @AField(name="TestSuite Name",description="The name of the cloned TestSuite",type=AFieldType.STRING)
176: public final static String NAME = "TestSuite Name";
177:
178: @AField(name="Target Project",description="The target Project for the cloned TestSuite",type=AFieldType.ENUMERATION)
179: public final static String PROJECT = "Target Project";
180:
181: @AField(name="Move instead",description="Moves the selected TestSuite instead of copying",type=AFieldType.BOOLEAN)
182: public final static String MOVE = "Move instead";
183: }
184: }
|