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