View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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  
14  package com.eviware.soapui.support.dnd.handlers;
15  
16  import java.util.HashSet;
17  import java.util.Set;
18  
19  import com.eviware.soapui.impl.wsdl.WsdlInterface;
20  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
21  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
22  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
23  import com.eviware.soapui.support.UISupport;
24  
25  public class TestCaseToTestSuiteDropHandler extends AbstractAfterModelItemDropHandler<WsdlTestCase, WsdlTestSuite>
26  {
27  	public TestCaseToTestSuiteDropHandler()
28  	{
29  		super( WsdlTestCase.class, WsdlTestSuite.class );
30  	}
31  	
32  	@Override
33  	boolean canCopyAfter( WsdlTestCase source, WsdlTestSuite target )
34  	{
35  		return true;
36  	}
37  
38  	@Override
39  	boolean canMoveAfter( WsdlTestCase source, WsdlTestSuite target )
40  	{
41  		return true;
42  	}
43  
44  	@Override
45  	boolean copyAfter( WsdlTestCase source, WsdlTestSuite target )
46  	{
47  		WsdlTestCase testCase = copyTestCase( source, target, 0 );
48  		if( testCase != null )
49  			UISupport.select( testCase );
50  		
51  		return testCase != null;
52  	}
53  
54  	public static WsdlTestCase copyTestCase( WsdlTestCase testCase, WsdlTestSuite target, int position )
55  	{
56  		String name = UISupport.prompt( "Specify name of copied TestCase", "Copy TestCase", "Copy of " + testCase.getName() );
57  		if( name == null )
58  			return null;
59  		
60  		if( testCase.getTestSuite() == target )
61  		{
62  			 return target.importTestCase( testCase, name, position, true, true );
63  		}
64  		else if( testCase.getTestSuite().getProject() == target.getProject() )
65  		{
66  			return target.importTestCase( testCase, name, position, true, true );
67  		}
68  		else
69  		{
70  			Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
71  			
72  			//		 get required interfaces
73  			for( int y = 0; y < testCase.getTestStepCount(); y++ )
74  			{
75  				WsdlTestStep testStep = testCase.getTestStepAt( y );
76  				requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
77  			}
78  			
79  			if( DragAndDropSupport.importRequiredInterfaces( target.getProject(), requiredInterfaces, "Copy TestCase" ))
80  			{
81  				return target.importTestCase( testCase, name, position, true, true );
82  			}
83  		}
84  		
85  		return null;
86  	}
87  
88  	@Override
89  	boolean moveAfter( WsdlTestCase source, WsdlTestSuite target )
90  	{
91  		WsdlTestCase testCase = moveTestCase( source, target, 0 );
92  		if( testCase != null )
93  			UISupport.select( testCase );
94  		
95  		return testCase != null;
96  	}
97  
98  	public static WsdlTestCase moveTestCase( WsdlTestCase testCase, WsdlTestSuite target, int position )
99  	{
100 		if( testCase.getTestSuite() == target )
101 		{
102 			int ix = target.getIndexOfTestCase( testCase );
103 			
104 			if( position == -1 )
105 			{
106 				target.moveTestCase( ix, target.getTestCaseCount()-ix );
107 			}
108 			else if( ix >= 0 && position != ix )
109 			{
110 				int offset = position - ix;
111 				if( offset > 0 )
112 					offset--;
113 				target.moveTestCase( ix, offset);
114 			}
115 		}
116 		else if( testCase.getTestSuite().getProject() == target.getProject() )
117 		{
118 			if( UISupport.confirm( "Move TestCase [" + testCase.getName() + "] to TestSuite [" + target.getName() + "]", "Move TestCase" ))
119 			{
120 				WsdlTestCase importedTestCase = target.importTestCase( testCase, testCase.getName(), position, true, false );
121 				if( importedTestCase != null )
122 				{
123 					testCase.getTestSuite().removeTestCase( testCase );
124 					return importedTestCase;
125 				}
126 			}
127 		}
128 		else if( UISupport.confirm( "Move TestCase [" + testCase.getName() + "] to TestSuite [" + target.getName() + "]", "Move TestCase" ))
129 		{
130 			Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
131 			
132 			//	get required interfaces
133 			for( int y = 0; y < testCase.getTestStepCount(); y++ )
134 			{
135 				WsdlTestStep testStep = testCase.getTestStepAt( y );
136 				requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
137 			}
138 			
139 			if( DragAndDropSupport.importRequiredInterfaces( target.getProject(), requiredInterfaces, "Move TestCase" ))
140 			{
141 				WsdlTestCase importedTestCase = target.importTestCase( testCase, testCase.getName(), position, true, false );
142 				if( importedTestCase != null )
143 				{
144 					testCase.getTestSuite().removeTestCase( testCase );
145 					return importedTestCase;
146 				}
147 			}
148 		}
149 
150 		return null;
151 	}
152 
153 	@Override
154 	String getCopyAfterInfo( WsdlTestCase source, WsdlTestSuite target )
155 	{
156 		return "Copy TestCase [" + source.getName() + "] to TestSuite [" + target.getName() + "]";
157 	}
158 
159 	@Override
160 	String getMoveAfterInfo( WsdlTestCase source, WsdlTestSuite target )
161 	{
162 		return "Move TestCase [" + source.getName() + "] to TestSuite [" + target.getName() + "]";
163 	}
164 	
165 	
166 }