1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.actions.teststep;
14
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19
20 import javax.xml.namespace.QName;
21
22 import com.eviware.soapui.SoapUI;
23 import com.eviware.soapui.impl.WorkspaceImpl;
24 import com.eviware.soapui.impl.wsdl.WsdlInterface;
25 import com.eviware.soapui.impl.wsdl.WsdlProject;
26 import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
27 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
28 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
29 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
30 import com.eviware.soapui.model.iface.Interface;
31 import com.eviware.soapui.model.project.Project;
32 import com.eviware.soapui.model.support.ModelSupport;
33 import com.eviware.soapui.model.testsuite.TestSuite;
34 import com.eviware.soapui.support.SoapUIException;
35 import com.eviware.soapui.support.UISupport;
36 import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
37 import com.eviware.x.form.XFormDialog;
38 import com.eviware.x.form.XFormField;
39 import com.eviware.x.form.XFormFieldListener;
40 import com.eviware.x.form.support.ADialogBuilder;
41 import com.eviware.x.form.support.AField;
42 import com.eviware.x.form.support.AForm;
43 import com.eviware.x.form.support.AField.AFieldType;
44
45 /***
46 * Clones a WsdlTestStep
47 *
48 * @author Ole.Matzura
49 */
50
51 public class CloneTestStepAction extends AbstractSoapUIAction<WsdlTestStep>
52 {
53 private static final String CREATE_NEW_OPTION = "<Create New>";
54 private XFormDialog dialog;
55
56 public CloneTestStepAction()
57 {
58 super( "Clone TestStep", "Clones this TestStep" );
59 }
60
61 public void perform( WsdlTestStep testStep, Object param )
62 {
63 if( dialog == null )
64 {
65 dialog = ADialogBuilder.buildDialog( Form.class );
66 dialog.getFormField( Form.PROJECT ).addFormFieldListener( new XFormFieldListener() {
67
68 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
69 {
70 if( newValue.equals( CREATE_NEW_OPTION ))
71 dialog.setOptions( Form.TESTSUITE, new String[] {CREATE_NEW_OPTION} );
72 else
73 {
74 Project project = SoapUI.getWorkspace().getProjectByName( newValue );
75 String[] names = ModelSupport.getNames( project.getTestSuiteList(), new String[] {CREATE_NEW_OPTION} );
76 dialog.setOptions( Form.TESTSUITE, names);
77 dialog.setValue( Form.TESTSUITE, names[0] );
78
79 if( names.length > 1 )
80 {
81 TestSuite testSuite = project.getTestSuiteByName( names[0] );
82 dialog.setOptions( Form.TESTCASE,
83 ModelSupport.getNames( testSuite.getTestCaseList(), new String[] {CREATE_NEW_OPTION} ));
84 }
85 else
86 {
87 dialog.setOptions( Form.TESTCASE, new String[] {CREATE_NEW_OPTION} );
88 }
89 }
90 }} );
91
92 dialog.getFormField( Form.TESTSUITE ).addFormFieldListener( new XFormFieldListener() {
93
94 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
95 {
96 if( newValue.equals( CREATE_NEW_OPTION ))
97 {
98 dialog.setOptions( Form.TESTCASE, new String[] {CREATE_NEW_OPTION} );
99 }
100 else
101 {
102 String projectName = dialog.getValue( Form.PROJECT );
103 Project project = SoapUI.getWorkspace().getProjectByName( projectName );
104 TestSuite testSuite = project.getTestSuiteByName( newValue );
105 dialog.setOptions( Form.TESTCASE, testSuite == null ? new String[] {CREATE_NEW_OPTION} :
106 ModelSupport.getNames( testSuite.getTestCaseList(), new String[] {CREATE_NEW_OPTION} ));
107 }
108 }} );
109
110 }
111
112 dialog.setBooleanValue(Form.MOVE, false);
113 dialog.setValue( Form.NAME, "Copy of " + testStep.getName() );
114 WorkspaceImpl workspace = testStep.getTestCase().getTestSuite().getProject().getWorkspace();
115 dialog.setOptions( Form.PROJECT,
116 ModelSupport.getNames( workspace.getOpenProjectList(), new String[] {CREATE_NEW_OPTION} ) );
117
118 dialog.setValue( Form.PROJECT, testStep.getTestCase().getTestSuite().getProject().getName() );
119
120 dialog.setOptions( Form.TESTSUITE,
121 ModelSupport.getNames( testStep.getTestCase().getTestSuite().getProject().getTestSuiteList(), new String[] {CREATE_NEW_OPTION} ) );
122 dialog.setValue( Form.TESTSUITE, testStep.getTestCase().getTestSuite().getName() );
123
124 dialog.setOptions( Form.TESTCASE,
125 ModelSupport.getNames( testStep.getTestCase().getTestSuite().getTestCaseList(), new String[] {CREATE_NEW_OPTION} ) );
126 dialog.setValue( Form.TESTCASE, testStep.getTestCase().getName() );
127
128 if( dialog.show() )
129 {
130 String targetProjectName = dialog.getValue( Form.PROJECT );
131 String targetTestSuiteName = dialog.getValue( Form.TESTSUITE );
132 String targetTestCaseName = dialog.getValue( Form.TESTCASE );
133 String name = dialog.getValue( Form.NAME );
134
135 WsdlProject project = testStep.getTestCase().getTestSuite().getProject();
136 WsdlTestSuite targetTestSuite = null;
137 WsdlTestCase targetTestCase = null;
138 Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
139
140
141 if( !targetProjectName.equals( project.getName() ))
142 {
143
144 requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
145
146 project = ( WsdlProject ) workspace.getProjectByName( targetProjectName );
147 if( project == null )
148 {
149 targetProjectName = UISupport.prompt( "Enter name for new Project", "Clone TestStep", "" );
150 if( targetProjectName == null )
151 return;
152
153 try
154 {
155 project = workspace.createProject( targetProjectName );
156 }
157 catch( SoapUIException e )
158 {
159 UISupport.showErrorMessage( e );
160 }
161
162 if( project == null )
163 return;
164 }
165
166 if( requiredInterfaces.size() > 0 && project.getInterfaceCount() > 0 )
167 {
168 Map<QName,WsdlInterface> bindings = new HashMap<QName,WsdlInterface>();
169 for( WsdlInterface iface : requiredInterfaces )
170 {
171 bindings.put( iface.getBindingName(), iface );
172 }
173
174 for( Interface iface : project.getInterfaceList() )
175 {
176 bindings.remove( iface.getTechnicalId());
177 }
178
179 requiredInterfaces.retainAll( bindings.values() );
180 }
181
182 if( requiredInterfaces.size() > 0 )
183 {
184 String msg = "Target project [" + targetProjectName +"] is missing required interfaces;\r\n\r\n";
185 for( WsdlInterface iface : requiredInterfaces )
186 {
187 msg += iface.getName() + " [" + iface.getBindingName() + "]\r\n";
188 }
189 msg += "\r\nThese will be cloned to the targetProject as well";
190
191 if( !UISupport.confirm( msg, "Clone TestStep" ))
192 return;
193
194 for( WsdlInterface iface : requiredInterfaces )
195 {
196 project.importInterface( iface, true, true );
197 }
198 }
199 }
200
201 targetTestSuite = project.getTestSuiteByName( targetTestSuiteName );
202 if( targetTestSuite == null )
203 {
204 targetTestSuiteName = UISupport.prompt( "Specify name for new TestSuite", "Clone TestStep",
205 "Copy of " + testStep.getTestCase().getTestSuite().getName() );
206 if( targetTestSuiteName == null )
207 return;
208
209 targetTestSuite = project.addNewTestSuite( targetTestSuiteName );
210 }
211
212 targetTestCase = targetTestSuite.getTestCaseByName( targetTestCaseName );
213 if( targetTestCase == null )
214 {
215 targetTestCaseName = UISupport.prompt( "Specify name for new TestCase", "Clone TestStep",
216 "Copy of " + testStep.getTestCase().getName() );
217 if( targetTestCaseName == null )
218 return;
219
220 targetTestCase = targetTestSuite.addNewTestCase( targetTestCaseName );
221 }
222
223 boolean move = dialog.getBooleanValue( Form.MOVE );
224
225 WsdlTestStep newTestStep = targetTestCase.importTestStep( testStep, name, -1, !move );
226 UISupport.select( newTestStep );
227
228 if( move )
229 {
230 testStep.getTestCase().removeTestStep( testStep );
231 }
232 }
233 }
234
235 @AForm(description = "Specify target Project/TestSuite/TestCase and name of cloned TestStep", name = "Clone TestStep",
236 helpUrl=HelpUrls.CLONETESTSTEP_HELP_URL, icon=UISupport.TOOL_ICON_PATH)
237 protected interface Form
238 {
239 @AField( name="TestStep Name", description = "The name of the cloned TestStep", type=AFieldType.STRING )
240 public final static String NAME = "TestStep Name";
241
242 @AField( name="Target TestCase", description = "The target TestCase for the cloned TestStep", type=AFieldType.ENUMERATION )
243 public final static String TESTCASE = "Target TestCase";
244
245 @AField( name="Target TestSuite", description = "The target TestSuite for the cloned TestStep", type=AFieldType.ENUMERATION )
246 public final static String TESTSUITE = "Target TestSuite";
247
248 @AField( name="Target Project", description = "The target Project for the cloned TestStep", type=AFieldType.ENUMERATION )
249 public final static String PROJECT = "Target Project";
250
251 @AField( name="Move instead", description = "Moves the selected TestStep instead of copying", type=AFieldType.BOOLEAN )
252 public final static String MOVE = "Move instead";
253 }
254 }