View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.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 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.support.SoapUIException;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
34  import com.eviware.x.form.XFormDialog;
35  import com.eviware.x.form.XFormField;
36  import com.eviware.x.form.XFormFieldListener;
37  import com.eviware.x.form.support.ADialogBuilder;
38  import com.eviware.x.form.support.AField;
39  import com.eviware.x.form.support.AForm;
40  import com.eviware.x.form.support.AField.AFieldType;
41  
42  /***
43   * Clones a WsdlTestSuite
44   * 
45   * @author Ole.Matzura
46   */
47  
48  /*
49   * There is a separate class for the pro version ProCloneTestCaseAction any
50   * changes made here should reflect to that class too TODO refactor these two
51   * classes so that only one class needs to be changed in case of changing the
52   * core functionality
53   */
54  public class CloneTestCaseAction extends AbstractSoapUIAction<WsdlTestCase>
55  {
56  	private static final String CREATE_NEW_OPTION = "<Create New>";
57  	private XFormDialog dialog;
58  
59  	public CloneTestCaseAction()
60  	{
61  		super( "Clone TestCase", "Clones this TestCase" );
62  	}
63  
64  	public void perform( WsdlTestCase testCase, Object param )
65  	{
66  		if( dialog == null )
67  		{
68  			dialog = ADialogBuilder.buildDialog( Form.class );
69  			dialog.getFormField( Form.PROJECT ).addFormFieldListener( new XFormFieldListener()
70  			{
71  
72  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
73  				{
74  					if( newValue.equals( CREATE_NEW_OPTION ) )
75  						dialog.setOptions( Form.TESTSUITE, new String[] { CREATE_NEW_OPTION } );
76  					else
77  					{
78  						Project project = SoapUI.getWorkspace().getProjectByName( newValue );
79  						dialog.setOptions( Form.TESTSUITE, ModelSupport.getNames( project.getTestSuiteList(),
80  								new String[] { CREATE_NEW_OPTION } ) );
81  					}
82  				}
83  			} );
84  			dialog.getFormField( Form.CLONE_DESCRIPTION ).addFormFieldListener( new XFormFieldListener()
85  			{
86  
87  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
88  				{
89  					if( dialog.getBooleanValue( Form.CLONE_DESCRIPTION ) )
90  					{
91  						dialog.getFormField( Form.DESCRIPTION ).setEnabled( false );
92  					}
93  					else
94  					{
95  						dialog.getFormField( Form.DESCRIPTION ).setEnabled( true );
96  					}
97  
98  				}
99  			} );
100 		}
101 
102 		dialog.setBooleanValue( Form.MOVE, false );
103 		dialog.setBooleanValue( Form.CLONE_DESCRIPTION, true );
104 		dialog.getFormField( Form.DESCRIPTION ).setEnabled( false );
105 		dialog.setValue( Form.DESCRIPTION, testCase.getDescription() );
106 		dialog.setValue( Form.NAME, "Copy of " + testCase.getName() );
107 		WorkspaceImpl workspace = testCase.getTestSuite().getProject().getWorkspace();
108 		dialog.setOptions( Form.PROJECT, ModelSupport.getNames( workspace.getOpenProjectList(),
109 				new String[] { CREATE_NEW_OPTION } ) );
110 
111 		dialog.setValue( Form.PROJECT, testCase.getTestSuite().getProject().getName() );
112 
113 		dialog.setOptions( Form.TESTSUITE, ModelSupport.getNames(
114 				testCase.getTestSuite().getProject().getTestSuiteList(), new String[] { CREATE_NEW_OPTION } ) );
115 
116 		dialog.setValue( Form.TESTSUITE, testCase.getTestSuite().getName() );
117 		boolean hasLoadTests = testCase.getLoadTestCount() > 0;
118 		dialog.setBooleanValue( Form.CLONE_LOADTESTS, hasLoadTests );
119 		dialog.getFormField( Form.CLONE_LOADTESTS ).setEnabled( hasLoadTests );
120 
121 		if( dialog.show() )
122 		{
123 			String targetProjectName = dialog.getValue( Form.PROJECT );
124 			String targetTestSuiteName = dialog.getValue( Form.TESTSUITE );
125 			String name = dialog.getValue( Form.NAME );
126 
127 			WsdlProject project = testCase.getTestSuite().getProject();
128 			WsdlTestSuite targetTestSuite = null;
129 			Set<Interface> requiredInterfaces = new HashSet<Interface>();
130 
131 			// to another project project?
132 			if( !targetProjectName.equals( project.getName() ) )
133 			{
134 				// get required interfaces
135 				for( int y = 0; y < testCase.getTestStepCount(); y++ )
136 				{
137 					WsdlTestStep testStep = testCase.getTestStepAt( y );
138 					requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
139 				}
140 
141 				project = ( WsdlProject )workspace.getProjectByName( targetProjectName );
142 				if( project == null )
143 				{
144 					targetProjectName = UISupport.prompt( "Enter name for new Project", "Clone TestCase", "" );
145 					if( targetProjectName == null )
146 						return;
147 
148 					try
149 					{
150 						project = workspace.createProject( targetProjectName, null );
151 					}
152 					catch( SoapUIException e )
153 					{
154 						UISupport.showErrorMessage( e );
155 					}
156 
157 					if( project == null )
158 						return;
159 				}
160 
161 				if( requiredInterfaces.size() > 0 && project.getInterfaceCount() > 0 )
162 				{
163 					Map<String, Interface> bindings = new HashMap<String, Interface>();
164 					for( Interface iface : requiredInterfaces )
165 					{
166 						bindings.put( iface.getTechnicalId(), iface );
167 					}
168 
169 					for( Interface iface : project.getInterfaceList() )
170 					{
171 						bindings.remove( iface.getTechnicalId() );
172 					}
173 
174 					requiredInterfaces.retainAll( bindings.values() );
175 				}
176 
177 				if( requiredInterfaces.size() > 0 )
178 				{
179 					String msg = "Target project [" + targetProjectName + "] is missing required Interfaces;\r\n\r\n";
180 					for( Interface iface : requiredInterfaces )
181 					{
182 						msg += iface.getName() + " [" + iface.getTechnicalId() + "]\r\n";
183 					}
184 					msg += "\r\nShould these will be cloned to the targetProject as well?";
185 
186 					Boolean result = UISupport.confirmOrCancel( msg, "Clone TestCase" );
187 					if( result == null )
188 						return;
189 
190 					if( result )
191 					{
192 						for( Interface iface : requiredInterfaces )
193 						{
194 							project.importInterface( ( AbstractInterface<?> )iface, true, true );
195 						}
196 					}
197 				}
198 			}
199 
200 			targetTestSuite = project.getTestSuiteByName( targetTestSuiteName );
201 			if( targetTestSuite == null )
202 			{
203 				targetTestSuiteName = UISupport.prompt( "Specify name for new TestSuite", "Clone TestCase", "Copy of "
204 						+ testCase.getTestSuite().getName() );
205 				if( targetTestSuiteName == null )
206 					return;
207 
208 				targetTestSuite = project.addNewTestSuite( targetTestSuiteName );
209 			}
210 
211 			boolean move = dialog.getBooleanValue( Form.MOVE );
212 			WsdlTestCase newTestCase = targetTestSuite.importTestCase( testCase, name, -1, dialog
213 					.getBooleanValue( Form.CLONE_LOADTESTS ), !move );
214 			UISupport.select( newTestCase );
215 
216 			if( move )
217 			{
218 				testCase.getTestSuite().removeTestCase( testCase );
219 			}
220 			boolean cloneDescription = dialog.getBooleanValue( Form.CLONE_DESCRIPTION );
221 			if( !cloneDescription )
222 			{
223 				newTestCase.setDescription( dialog.getValue( Form.DESCRIPTION ) );
224 			}
225 		}
226 	}
227 
228 	@AForm( description = "Specify target Project/TestSuite and name of cloned TestCase", name = "Clone TestCase", helpUrl = HelpUrls.CLONETESTCASE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
229 	protected interface Form
230 	{
231 		@AField( name = "TestCase Name", description = "The name of the cloned TestCase", type = AFieldType.STRING )
232 		public final static String NAME = "TestCase Name";
233 
234 
235 		@AField( name = "Target Project", description = "The target Project for the cloned TestCase", type = AFieldType.ENUMERATION )
236 		public final static String PROJECT = "Target Project";
237 
238 
239 		@AField( name = "Target TestSuite", description = "The target TestSuite for the cloned TestCase", type = AFieldType.ENUMERATION )
240 		public final static String TESTSUITE = "Target TestSuite"; 
241 		
242 		@AField( name = "Clone LoadTests", description = "Clone contained LoadTests", type = AFieldType.BOOLEAN )
243 		public final static String CLONE_LOADTESTS = "Clone LoadTests";
244 
245 		@AField( name = "Move instead", description = "Moves the selected TestCase instead of copying", type = AFieldType.BOOLEAN )
246 		public final static String MOVE = "Move instead";
247 
248 		@AField( name = "Clone description", description = "Clones the description of selected TestCase", type = AFieldType.BOOLEAN )
249 		public final static String CLONE_DESCRIPTION = "Clone description";
250 
251 		@AField( name = "Description", description = "Description of new TestCase", type = AFieldType.STRINGAREA )
252 		public final static String DESCRIPTION = "Description";
253 	}
254 }