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