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