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