1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.actions.testcase;
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.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 WsdlTestSuite
46 *
47 * @author Ole.Matzura
48 */
49
50 public class CloneTestCaseAction extends AbstractSoapUIAction<WsdlTestCase>
51 {
52 private static final String CREATE_NEW_OPTION = "<Create New>";
53 private XFormDialog dialog;
54
55 public CloneTestCaseAction()
56 {
57 super( "Clone TestCase", "Clones this TestCase" );
58 }
59
60 public void perform( WsdlTestCase testCase, 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 dialog.setOptions( Form.TESTSUITE,
75 ModelSupport.getNames( project.getTestSuiteList(), new String[] {CREATE_NEW_OPTION} ));
76 }
77 }} );
78 }
79
80 dialog.setBooleanValue(Form.MOVE, false);
81 dialog.setValue( Form.NAME, "Copy of " + testCase.getName() );
82 WorkspaceImpl workspace = testCase.getTestSuite().getProject().getWorkspace();
83 dialog.setOptions( Form.PROJECT,
84 ModelSupport.getNames( workspace.getOpenProjectList(), new String[] {CREATE_NEW_OPTION} ) );
85
86 dialog.setValue( Form.PROJECT, testCase.getTestSuite().getProject().getName() );
87
88 dialog.setOptions( Form.TESTSUITE,
89 ModelSupport.getNames( testCase.getTestSuite().getProject().getTestSuiteList(), new String[] {CREATE_NEW_OPTION} ) );
90
91 dialog.setValue( Form.TESTSUITE, testCase.getTestSuite().getName() );
92 boolean hasLoadTests = testCase.getLoadTestCount() > 0;
93 dialog.setBooleanValue( Form.CLONE_LOADTESTS, hasLoadTests );
94 dialog.getFormField( Form.CLONE_LOADTESTS ).setEnabled( hasLoadTests );
95
96 if( dialog.show() )
97 {
98 String targetProjectName = dialog.getValue( Form.PROJECT );
99 String targetTestSuiteName = dialog.getValue( Form.TESTSUITE );
100 String name = dialog.getValue( Form.NAME );
101
102 WsdlProject project = testCase.getTestSuite().getProject();
103 WsdlTestSuite targetTestSuite = null;
104 Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
105
106
107 if( !targetProjectName.equals( project.getName() ))
108 {
109
110 for( int y = 0; y < testCase.getTestStepCount(); y++ )
111 {
112 WsdlTestStep testStep = testCase.getTestStepAt( y );
113 requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
114 }
115
116 project = ( WsdlProject ) workspace.getProjectByName( targetProjectName );
117 if( project == null )
118 {
119 targetProjectName = UISupport.prompt( "Enter name for new Project", "Clone TestCase", "" );
120 if( targetProjectName == null )
121 return;
122
123 try
124 {
125 project = workspace.createProject( targetProjectName );
126 }
127 catch( SoapUIException e )
128 {
129 UISupport.showErrorMessage( e );
130 }
131
132 if( project == null )
133 return;
134 }
135
136 if( requiredInterfaces.size() > 0 && project.getInterfaceCount() > 0 )
137 {
138 Map<QName,WsdlInterface> bindings = new HashMap<QName,WsdlInterface>();
139 for( WsdlInterface iface : requiredInterfaces )
140 {
141 bindings.put( iface.getBindingName(), iface );
142 }
143
144 for( Interface iface : project.getInterfaceList() )
145 {
146 bindings.remove( iface.getTechnicalId());
147 }
148
149 requiredInterfaces.retainAll( bindings.values() );
150 }
151
152 if( requiredInterfaces.size() > 0 )
153 {
154 String msg = "Target project [" + targetProjectName +"] is missing required interfaces;\r\n\r\n";
155 for( WsdlInterface iface : requiredInterfaces )
156 {
157 msg += iface.getName() + " [" + iface.getBindingName() + "]\r\n";
158 }
159 msg += "\r\nThese will be cloned to the targetProject as well";
160
161 if( !UISupport.confirm( msg, "Clone TestCase" ))
162 return;
163
164 for( WsdlInterface iface : requiredInterfaces )
165 {
166 project.importInterface( iface, true, true );
167 }
168 }
169 }
170
171 targetTestSuite = project.getTestSuiteByName( targetTestSuiteName );
172 if( targetTestSuite == null )
173 {
174 targetTestSuiteName = UISupport.prompt( "Specify name for new TestSuite", "Clone TestCase",
175 "Copy of " + testCase.getTestSuite().getName() );
176 if( targetTestSuiteName == null )
177 return;
178
179 targetTestSuite = project.addNewTestSuite( targetTestSuiteName );
180 }
181
182 boolean move = dialog.getBooleanValue( Form.MOVE );
183 WsdlTestCase newTestCase = targetTestSuite.importTestCase( testCase, name, -1,
184 dialog.getBooleanValue( Form.CLONE_LOADTESTS ), !move );
185 UISupport.select( newTestCase );
186
187 if( move )
188 {
189 testCase.getTestSuite().removeTestCase( testCase );
190 }
191 }
192 }
193
194 @AForm(description = "Specify target Project/TestSuite and name of cloned TestCase", name = "Clone TestCase",
195 helpUrl=HelpUrls.CLONETESTCASE_HELP_URL, icon=UISupport.TOOL_ICON_PATH)
196 protected interface Form
197 {
198 @AField( name="TestCase Name", description = "The name of the cloned TestCase", type=AFieldType.STRING )
199 public final static String NAME = "TestCase Name";
200
201 @AField( name="Target TestSuite", description = "The target TestSuite for the cloned TestCase", type=AFieldType.ENUMERATION )
202 public final static String TESTSUITE = "Target TestSuite";
203
204 @AField( name="Target Project", description = "The target Project for the cloned TestCase", type=AFieldType.ENUMERATION )
205 public final static String PROJECT = "Target Project";
206
207 @AField( name="Clone LoadTests", description = "Clone contained LoadTests", type=AFieldType.BOOLEAN )
208 public final static String CLONE_LOADTESTS = "Clone LoadTests";
209
210 @AField( name="Move instead", description = "Moves the selected TestCase instead of copying", type=AFieldType.BOOLEAN )
211 public final static String MOVE = "Move instead";
212 }
213 }