View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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  import java.util.Set;
17  
18  import com.eviware.soapui.impl.wsdl.WsdlProject;
19  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
20  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
21  import com.eviware.soapui.model.iface.Interface;
22  import com.eviware.soapui.model.testsuite.TestCase;
23  import com.eviware.soapui.support.UISupport;
24  
25  public class TestSuiteToTestSuiteDropHandler extends
26  		AbstractBeforeAfterModelItemDropHandler<WsdlTestSuite, WsdlTestSuite>
27  {
28  	public TestSuiteToTestSuiteDropHandler()
29  	{
30  		super( WsdlTestSuite.class, WsdlTestSuite.class );
31  	}
32  
33  	@Override
34  	boolean canCopyAfter( WsdlTestSuite source, WsdlTestSuite target )
35  	{
36  		return true;
37  	}
38  
39  	@Override
40  	boolean canMoveAfter( WsdlTestSuite source, WsdlTestSuite target )
41  	{
42  		return source != target;
43  	}
44  
45  	@Override
46  	boolean copyAfter( WsdlTestSuite source, WsdlTestSuite target )
47  	{
48  		WsdlTestSuite testCase = copyTestSuite( source, target.getProject(), target
49  				.getProject().getIndexOfTestSuite( target ) + 1 );
50  
51  		if( testCase != null )
52  			UISupport.select( testCase );
53  
54  		return testCase != null;
55  	}
56  
57  	@Override
58  	boolean moveAfter( WsdlTestSuite source, WsdlTestSuite target )
59  	{
60  		WsdlTestSuite testCase = moveTestSuite( source, target.getProject(), target
61  				.getProject().getIndexOfTestSuite( target ) + 1 );
62  
63  		if( testCase != null )
64  			UISupport.select( testCase );
65  
66  		return testCase != null;
67  	}
68  
69  	@Override
70  	String getCopyAfterInfo( WsdlTestSuite source, WsdlTestSuite target )
71  	{
72  		return "Copy TestSuite [" + source.getName() + "] to Project [" + target.getProject().getName() + "]";
73  	}
74  
75  	@Override
76  	String getMoveAfterInfo( WsdlTestSuite source, WsdlTestSuite target )
77  	{
78  		return source == target ? "Move TestCase [" + source.getName() + "] within TestSuite" : "Move TestCase ["
79  				+ source.getName() + "] to TestSuite in Project [" + target.getName() + "]";
80  	}
81  
82  	@Override
83  	boolean canCopyBefore( WsdlTestSuite source, WsdlTestSuite target )
84  	{
85  		return true;
86  	}
87  
88  	@Override
89  	boolean canMoveBefore( WsdlTestSuite source, WsdlTestSuite target )
90  	{
91  		return source != target;
92  	}
93  
94  	@Override
95  	boolean copyBefore( WsdlTestSuite source, WsdlTestSuite target )
96  	{
97  		WsdlTestSuite testCase = copyTestSuite( source, source.getProject(), target.getProject().getIndexOfTestSuite( target ) );
98  
99  		if( testCase != null )
100 			UISupport.select( testCase );
101 
102 		return testCase != null;
103 	}
104 
105 	@Override
106 	String getCopyBeforeInfo( WsdlTestSuite source, WsdlTestSuite target )
107 	{
108 		return getCopyAfterInfo( source, target );
109 	}
110 
111 	@Override
112 	String getMoveBeforeInfo( WsdlTestSuite source, WsdlTestSuite target )
113 	{
114 		return getMoveAfterInfo( source, target );
115 	}
116 
117 	@Override
118 	boolean moveBefore( WsdlTestSuite source, WsdlTestSuite target )
119 	{
120 		WsdlTestSuite testCase = moveTestSuite( source, target.getProject(), target.getProject().getIndexOfTestSuite(
121 				target ) );
122 
123 		if( testCase != null )
124 			UISupport.select( testCase );
125 
126 		return testCase != null;
127 	}
128 
129 	public static WsdlTestSuite moveTestSuite( WsdlTestSuite testSuite, WsdlProject target, int position )
130 	{
131 		if( testSuite.getProject() == target )
132 		{
133 			int ix = target.getIndexOfTestSuite( testSuite );
134 
135 			if( position == -1 )
136 			{
137 				target.moveTestSuite( ix, target.getTestSuiteCount() - ix );
138 			}
139 			else if( ix >= 0 && position != ix )
140 			{
141 				int offset = position - ix;
142 				if( offset > 0 )
143 					offset-- ;
144 				target.moveTestSuite( ix, offset );
145 			}
146 		}
147 		else if( UISupport.confirm( "Move TestSuite [" + testSuite.getName() + "] to Project [" + target.getName() + "]",
148 				"Move TestSuite" ) )
149 		{
150 			Set<Interface> requiredInterfaces = new HashSet<Interface>();
151 
152 			// get required interfaces
153 			for( TestCase testCase : testSuite.getTestCaseList() )
154 			{
155 				for( int y = 0; y < testCase.getTestStepCount(); y++ )
156 				{
157 					WsdlTestStep testStep = ( WsdlTestStep )testCase.getTestStepAt( y );
158 					requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
159 				}
160 			}
161 
162 			if( DragAndDropSupport.importRequiredInterfaces( target, requiredInterfaces, "Move TestSuite" ) )
163 			{
164 				WsdlTestSuite importedTestSuite = target.importTestSuite( testSuite, testSuite.getName(), position, true, null );
165 				if( importedTestSuite != null )
166 				{
167 					testSuite.getProject().removeTestSuite( testSuite );
168 					return importedTestSuite;
169 				}
170 			}
171 		}
172 
173 		return null;
174 	}
175 	
176 	public static WsdlTestSuite copyTestSuite( WsdlTestSuite testSuite, WsdlProject target, int position )
177 	{
178 		String name = UISupport.prompt( "Specify name of copied TestCase", "Copy TestCase", "Copy of "
179 				+ testSuite.getName() );
180 		if( name == null )
181 			return null;
182 
183 		if( testSuite.getProject() == target )
184 		{
185 			return target.importTestSuite( testSuite, name, position, true, null );
186 		}
187 		else
188 		{
189 			Set<Interface> requiredInterfaces = new HashSet<Interface>();
190 
191 			// get required interfaces
192 			for( TestCase testCase : testSuite.getTestCaseList() )
193 			{
194 				for( int y = 0; y < testCase.getTestStepCount(); y++ )
195 				{
196 					WsdlTestStep testStep = ( WsdlTestStep )testCase.getTestStepAt( y );
197 					requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
198 				}
199 			}
200 
201 
202 			if( DragAndDropSupport.importRequiredInterfaces( target, requiredInterfaces, "Move TestSuite" ) )
203 			{
204 				return target.importTestSuite( testSuite, testSuite.getName(), position, true, null );
205 			}
206 		}
207 
208 		return null;
209 	}
210 }