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