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