View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.support.dnd.handlers;
14  
15  import java.util.HashSet;
16  
17  import com.eviware.soapui.SoapUI;
18  import com.eviware.soapui.impl.wsdl.WsdlProject;
19  import com.eviware.soapui.impl.wsdl.WsdlRequest;
20  import com.eviware.soapui.impl.wsdl.actions.request.AddRequestToTestCaseAction;
21  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
22  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
23  import com.eviware.soapui.model.iface.Interface;
24  import com.eviware.soapui.support.UISupport;
25  import com.eviware.soapui.support.action.SoapUIAction;
26  
27  public class RequestToTestStepDropHandler extends AbstractBeforeAfterModelItemDropHandler<WsdlRequest, WsdlTestStep>
28  {
29  	public RequestToTestStepDropHandler()
30  	{
31  		super( WsdlRequest.class, WsdlTestStep.class );
32  	}
33  
34  	@Override
35  	boolean canCopyAfter( WsdlRequest source, WsdlTestStep target )
36  	{
37  		return true;
38  	}
39  
40  	@Override
41  	boolean canMoveAfter( WsdlRequest source, WsdlTestStep target )
42  	{
43  		return true;
44  	}
45  
46  	@Override
47  	boolean copyAfter( WsdlRequest source, WsdlTestStep target )
48  	{
49  		return addRequestToTestCase( source, target.getTestCase(), target.getTestCase().getIndexOfTestStep( target ) + 1 );
50  	}
51  
52  	private boolean addRequestToTestCase( WsdlRequest source, WsdlTestCase testCase, int index )
53  	{
54  		if( !UISupport.confirm( "Add Request [" + source.getName() + "] to TestCase [" + testCase.getName() + "]",
55  				"Add Request to TestCase" ) )
56  			return false;
57  
58  		WsdlProject targetProject = testCase.getTestSuite().getProject();
59  		if( targetProject != source.getOperation().getInterface().getProject() )
60  		{
61  			HashSet<Interface> requiredInterfaces = new HashSet<Interface>();
62  			requiredInterfaces.add( source.getOperation().getInterface() );
63  
64  			if( !DragAndDropSupport
65  					.importRequiredInterfaces( targetProject, requiredInterfaces, "Add Request to TestCase" ) )
66  			{
67  				return false;
68  			}
69  		}
70  
71  		SoapUIAction<WsdlRequest> action = SoapUI.getActionRegistry().getAction(
72  				AddRequestToTestCaseAction.SOAPUI_ACTION_ID );
73  		return ( ( AddRequestToTestCaseAction )action ).addRequest( testCase, source, index ) != null;
74  	}
75  
76  	@Override
77  	boolean moveAfter( WsdlRequest source, WsdlTestStep target )
78  	{
79  		return addRequestToTestCase( source, target.getTestCase(), target.getTestCase().getIndexOfTestStep( target ) + 1 );
80  	}
81  
82  	@Override
83  	String getCopyAfterInfo( WsdlRequest source, WsdlTestStep target )
84  	{
85  		return "Add Request [" + source.getName() + "] to TestCase [" + target.getTestCase().getName() + "]";
86  	}
87  
88  	@Override
89  	String getMoveAfterInfo( WsdlRequest source, WsdlTestStep target )
90  	{
91  		return getCopyAfterInfo( source, target );
92  	}
93  
94  	@Override
95  	boolean canCopyBefore( WsdlRequest source, WsdlTestStep target )
96  	{
97  		return addRequestToTestCase( source, target.getTestCase(), target.getTestCase().getIndexOfTestStep( target ) );
98  	}
99  
100 	@Override
101 	boolean canMoveBefore( WsdlRequest source, WsdlTestStep target )
102 	{
103 		return true;
104 	}
105 
106 	@Override
107 	boolean copyBefore( WsdlRequest source, WsdlTestStep target )
108 	{
109 		return false;
110 	}
111 
112 	@Override
113 	String getCopyBeforeInfo( WsdlRequest source, WsdlTestStep target )
114 	{
115 		return getCopyAfterInfo( source, target );
116 	}
117 
118 	@Override
119 	String getMoveBeforeInfo( WsdlRequest source, WsdlTestStep target )
120 	{
121 		return getCopyAfterInfo( source, target );
122 	}
123 
124 	@Override
125 	boolean moveBefore( WsdlRequest source, WsdlTestStep target )
126 	{
127 		return addRequestToTestCase( source, target.getTestCase(), target.getTestCase().getIndexOfTestStep( target ) );
128 	}
129 }