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.testsuite;
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.impl.WorkspaceImpl;
21  import com.eviware.soapui.impl.support.AbstractInterface;
22  import com.eviware.soapui.impl.wsdl.WsdlProject;
23  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
24  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
25  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
26  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
27  import com.eviware.soapui.model.iface.Interface;
28  import com.eviware.soapui.model.support.ModelSupport;
29  import com.eviware.soapui.model.testsuite.TestSuite;
30  import com.eviware.soapui.support.SoapUIException;
31  import com.eviware.soapui.support.UISupport;
32  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
33  import com.eviware.x.form.XFormDialog;
34  import com.eviware.x.form.XFormField;
35  import com.eviware.x.form.XFormFieldListener;
36  import com.eviware.x.form.support.ADialogBuilder;
37  import com.eviware.x.form.support.AField;
38  import com.eviware.x.form.support.AForm;
39  import com.eviware.x.form.support.AField.AFieldType;
40  
41  /***
42   * Clones a WsdlTestSuite
43   * 
44   * @author Ole.Matzura
45   */
46  
47  public class CloneTestSuiteAction extends AbstractSoapUIAction<WsdlTestSuite>
48  {
49  	private XFormDialog dialog;
50  
51  	public CloneTestSuiteAction()
52  	{
53  		super( "Clone TestSuite", "Clones this TestSuite" );
54  	}
55  
56  	public void perform( WsdlTestSuite testSuite, Object param )
57  	{
58  		if( dialog == null )
59  			dialog = ADialogBuilder.buildDialog( Form.class );
60  
61  		dialog.getFormField( Form.CLONE_DESCRIPTION ).addFormFieldListener( new XFormFieldListener()
62  		{
63  
64  			public void valueChanged( XFormField sourceField, String newValue, String oldValue )
65  			{
66  				if( dialog.getBooleanValue( Form.CLONE_DESCRIPTION ) )
67  				{
68  					dialog.getFormField( Form.DESCRIPTION ).setEnabled( false );
69  				}
70  				else
71  				{
72  					dialog.getFormField( Form.DESCRIPTION ).setEnabled( true );
73  				}
74  
75  			}
76  		} );
77  		dialog.setValue( Form.NAME, "Copy of " + testSuite.getName() );
78  		dialog.setBooleanValue( Form.MOVE, false );
79  		dialog.setBooleanValue( Form.CLONE_DESCRIPTION, true );
80  		dialog.getFormField( Form.DESCRIPTION ).setEnabled( false );
81  		dialog.setValue( Form.DESCRIPTION, testSuite.getDescription() );
82  		WorkspaceImpl workspace = testSuite.getProject().getWorkspace();
83  		dialog.setOptions( Form.PROJECT, ModelSupport.getNames( workspace.getOpenProjectList(),
84  				new String[] { "<Create New>" } ) );
85  
86  		dialog.setValue( Form.PROJECT, testSuite.getProject().getName() );
87  
88  		if( dialog.show() )
89  		{
90  			String targetProjectName = dialog.getValue( Form.PROJECT );
91  			String name = dialog.getValue( Form.NAME );
92  
93  			WsdlProject project = ( WsdlProject )testSuite.getProject();
94  
95  			// within same project?
96  			boolean move = dialog.getBooleanValue( Form.MOVE );
97  			boolean cloneDescription = dialog.getBooleanValue( Form.CLONE_DESCRIPTION );
98  			String description = testSuite.getDescription();
99  			if( !cloneDescription )
100 			{
101 				description = dialog.getValue( Form.DESCRIPTION );
102 			}
103 
104 			TestSuite result = null;
105 
106 			if( targetProjectName.equals( testSuite.getProject().getName() ) )
107 			{
108 				result = cloneTestSuiteWithinProject( testSuite, name, project, description );
109 			}
110 			else
111 			{
112 				result = cloneToAnotherProject( testSuite, targetProjectName, name, move, description );
113 			}
114 
115 			if( move && result != null )
116 			{
117 				testSuite.getProject().removeTestSuite( testSuite );
118 			}
119 		}
120 	}
121 
122 	public static WsdlTestSuite cloneToAnotherProject( WsdlTestSuite testSuite, String targetProjectName, String name,
123 			boolean move, String description )
124 	{
125 		WorkspaceImpl workspace = testSuite.getProject().getWorkspace();
126 		WsdlProject targetProject = ( WsdlProject )workspace.getProjectByName( targetProjectName );
127 		if( targetProject == null )
128 		{
129 			targetProjectName = UISupport.prompt( "Enter name for new Project", "Clone TestSuite", "" );
130 			if( targetProjectName == null )
131 				return null;
132 
133 			try
134 			{
135 				targetProject = workspace.createProject( targetProjectName, null );
136 			}
137 			catch( SoapUIException e )
138 			{
139 				UISupport.showErrorMessage( e );
140 			}
141 
142 			if( targetProject == null )
143 				return null;
144 		}
145 
146 		Set<Interface> requiredInterfaces = getRequiredInterfaces( testSuite, targetProject );
147 
148 		if( requiredInterfaces.size() > 0 )
149 		{
150 			String msg = "Target project [" + targetProjectName + "] is missing required Interfaces;\r\n\r\n";
151 			for( Interface iface : requiredInterfaces )
152 			{
153 				msg += iface.getName() + " [" + iface.getTechnicalId() + "]\r\n";
154 			}
155 			msg += "\r\nShould these be cloned to the targetProject as well?";
156 
157 			Boolean result = UISupport.confirmOrCancel( msg, "Clone TestSuite" );
158 			if( result == null )
159 				return null;
160 
161 			if( result )
162 			{
163 				for( Interface iface : requiredInterfaces )
164 				{
165 					targetProject.importInterface( ( AbstractInterface<?> )iface, true, true );
166 				}
167 			}
168 		}
169 
170 		testSuite = targetProject.importTestSuite( testSuite, name, -1, !move, description );
171 		UISupport.select( testSuite );
172 
173 		return testSuite;
174 	}
175 
176 	public static TestSuite cloneTestSuiteWithinProject( WsdlTestSuite testSuite, String name, WsdlProject project,
177 			String description )
178 	{
179 		WsdlTestSuite newTestSuite = project.importTestSuite( testSuite, name, -1, true, description );
180 		UISupport.select( newTestSuite );
181 		return newTestSuite;
182 	}
183 
184 	public static Set<Interface> getRequiredInterfaces( WsdlTestSuite testSuite, WsdlProject targetProject )
185 	{
186 		Set<Interface> requiredInterfaces = new HashSet<Interface>();
187 
188 		for( int i = 0; i < testSuite.getTestCaseCount(); i++ )
189 		{
190 			WsdlTestCase testCase = testSuite.getTestCaseAt( i );
191 
192 			for( int y = 0; y < testCase.getTestStepCount(); y++ )
193 			{
194 				WsdlTestStep testStep = testCase.getTestStepAt( y );
195 				requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
196 			}
197 		}
198 
199 		if( requiredInterfaces.size() > 0 && targetProject.getInterfaceCount() > 0 )
200 		{
201 			Map<String, Interface> bindings = new HashMap<String, Interface>();
202 			for( Interface iface : requiredInterfaces )
203 			{
204 				bindings.put( iface.getTechnicalId(), iface );
205 			}
206 
207 			for( Interface iface : targetProject.getInterfaceList() )
208 			{
209 				bindings.remove( iface.getTechnicalId() );
210 			}
211 
212 			requiredInterfaces.retainAll( bindings.values() );
213 		}
214 		return requiredInterfaces;
215 	}
216 
217 	@AForm( description = "Specify target Project and name of cloned TestSuite", name = "Clone TestSuite", helpUrl = HelpUrls.CLONETESTSUITE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
218 	protected interface Form
219 	{
220 		@AField( name = "TestSuite Name", description = "The name of the cloned TestSuite", type = AFieldType.STRING )
221 		public final static String NAME = "TestSuite Name";
222 
223 		@AField( name = "Target Project", description = "The target Project for the cloned TestSuite", type = AFieldType.ENUMERATION )
224 		public final static String PROJECT = "Target Project";
225 
226 		@AField( name = "Move instead", description = "Moves the selected TestSuite instead of copying", type = AFieldType.BOOLEAN )
227 		public final static String MOVE = "Move instead";
228 
229 		@AField( name = "Clone description", description = "Clones the description of selected TestSuite", type = AFieldType.BOOLEAN )
230 		public final static String CLONE_DESCRIPTION = "Clone description";
231 
232 		@AField( name = "Description", description = "Description of new TestSuite", type = AFieldType.STRINGAREA )
233 		public final static String DESCRIPTION = "Description";
234 	}
235 }