View Javadoc

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