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