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