View Javadoc

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