View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 javax.xml.namespace.QName;
21  
22  import com.eviware.soapui.impl.WorkspaceImpl;
23  import com.eviware.soapui.impl.wsdl.WsdlInterface;
24  import com.eviware.soapui.impl.wsdl.WsdlProject;
25  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
26  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
27  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
28  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
29  import com.eviware.soapui.model.iface.Interface;
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.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.setValue( Form.NAME, "Copy of " + testSuite.getName() );
61     	dialog.setBooleanValue(Form.MOVE, false);
62     	WorkspaceImpl workspace = testSuite.getProject().getWorkspace();
63  		dialog.setOptions( Form.PROJECT, 
64  					ModelSupport.getNames( workspace.getOpenProjectList(), new String[] {"<Create New>"} ) );
65  		
66  		dialog.setValue( Form.PROJECT, testSuite.getProject().getName() );
67  		
68  		if( dialog.show() )
69  		{
70  			String targetProjectName = dialog.getValue( Form.PROJECT );
71  			String name = dialog.getValue( Form.NAME );
72  			
73  			WsdlProject project = (WsdlProject) testSuite.getProject();
74  			
75  			// within same project?
76  			boolean move = dialog.getBooleanValue( Form.MOVE );
77  			if( targetProjectName.equals( testSuite.getProject().getName() ))
78  			{
79  		      cloneTestSuiteWithinProject( testSuite, name, project );
80  			}
81  			else
82  			{
83  				cloneToAnotherProject( testSuite, targetProjectName, name, move );
84  			}
85  			
86  			if( move)
87  			{
88  			   testSuite.getProject().removeTestSuite( testSuite );
89  			}
90  		}
91     }
92  
93  	public static WsdlTestSuite cloneToAnotherProject( WsdlTestSuite testSuite, String targetProjectName, String name, boolean move )
94  	{
95  		WorkspaceImpl workspace = testSuite.getProject().getWorkspace();
96  		WsdlProject targetProject = ( WsdlProject ) workspace.getProjectByName( targetProjectName );
97  		if( targetProject == null )
98  		{
99  			targetProjectName = UISupport.prompt( "Enter name for new Project", "Clone TestSuite", "" );
100 			if( targetProjectName == null )
101 				return null;
102 			
103 			try
104 			{
105 				targetProject = workspace.createProject( targetProjectName );
106 			}
107 			catch( SoapUIException e )
108 			{
109 				UISupport.showErrorMessage( e );
110 			}
111 			
112 			if( targetProject == null )
113 				return null;
114 		}
115 		
116 		Set<WsdlInterface> requiredInterfaces = getRequiredInterfaces( testSuite, targetProject );
117 		
118 		if( requiredInterfaces.size() > 0 )
119 		{
120 			String msg = "Target project [" + targetProjectName  +"] is missing required interfaces;\r\n\r\n";
121 			for( WsdlInterface iface : requiredInterfaces )
122 			{
123 				msg += iface.getName() + " [" + iface.getBindingName() + "]\r\n";
124 			}
125 			msg += "\r\nThese will be cloned to the targetProject as well";
126 			
127 			if( !UISupport.confirm( msg, "Clone TestSuite" ))
128 				return null;
129 			
130 			for( WsdlInterface iface : requiredInterfaces )
131 			{
132 				targetProject.importInterface( iface, true, true );
133 			}
134 		}
135 		
136 		testSuite = targetProject.importTestSuite( testSuite, name, !move );
137 		UISupport.select( testSuite);
138 		
139 		return testSuite;
140 	}
141 
142 	public static boolean cloneTestSuiteWithinProject( WsdlTestSuite testSuite, String name, WsdlProject project )
143 	{
144 		WsdlTestSuite newTestSuite = project.importTestSuite( testSuite, name, true );
145 		UISupport.select( newTestSuite );
146 		return true;
147 	}
148 
149 	public static Set<WsdlInterface> getRequiredInterfaces( WsdlTestSuite testSuite, WsdlProject targetProject )
150 	{
151 		Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
152 		
153 		for( int i = 0; i < testSuite.getTestCaseCount(); i++ )
154 		{
155 			WsdlTestCase testCase = testSuite.getTestCaseAt( i );
156 			
157 			for( int y = 0; y < testCase.getTestStepCount(); y++ )
158 			{
159 				WsdlTestStep testStep = testCase.getTestStepAt( y );
160 				requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
161 			}
162 		}
163 		
164 		if( requiredInterfaces.size() > 0 && targetProject.getInterfaceCount() > 0 )
165 		{
166 			Map<QName,WsdlInterface> bindings = new HashMap<QName,WsdlInterface>();
167 			for( WsdlInterface iface : requiredInterfaces )
168 			{
169 				bindings.put( iface.getBindingName(), iface );
170 			}
171 			
172 			for( Interface iface : targetProject.getInterfaceList() )
173 			{
174 				bindings.remove( iface.getTechnicalId());
175 			}
176 
177 			requiredInterfaces.retainAll( bindings.values() );
178 		}
179 		return requiredInterfaces;
180 	}
181    
182    @AForm(description = "Specify target Project and name of cloned TestSuite", name = "Clone TestSuite",
183    			helpUrl=HelpUrls.CLONETESTSUITE_HELP_URL, icon=UISupport.TOOL_ICON_PATH)
184 	protected interface Form
185 	{
186    	@AField( name="TestSuite Name", description = "The name of the cloned TestSuite", type=AFieldType.STRING )
187 		public final static String NAME = "TestSuite Name";
188    	
189    	@AField( name="Target Project", description = "The target Project for the cloned TestSuite", type=AFieldType.ENUMERATION )
190 		public final static String PROJECT = "Target Project";
191    	
192    	@AField( name="Move instead", description = "Moves the selected TestSuite instead of copying", type=AFieldType.BOOLEAN )
193 		public final static String MOVE = "Move instead";
194 	}
195 }