View Javadoc

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