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.WsdlTestSuite;
19  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
20  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
21  import com.eviware.soapui.model.iface.Interface;
22  import com.eviware.soapui.support.UISupport;
23  
24  public class TestCaseToTestSuiteDropHandler extends AbstractAfterModelItemDropHandler<WsdlTestCase, WsdlTestSuite>
25  {
26  	public TestCaseToTestSuiteDropHandler()
27  	{
28  		super( WsdlTestCase.class, WsdlTestSuite.class );
29  	}
30  
31  	@Override
32  	boolean canCopyAfter( WsdlTestCase source, WsdlTestSuite target )
33  	{
34  		return true;
35  	}
36  
37  	@Override
38  	boolean canMoveAfter( WsdlTestCase source, WsdlTestSuite target )
39  	{
40  		return true;
41  	}
42  
43  	@Override
44  	boolean copyAfter( WsdlTestCase source, WsdlTestSuite target )
45  	{
46  		WsdlTestCase testCase = copyTestCase( source, target, 0 );
47  		if( testCase != null )
48  			UISupport.select( testCase );
49  
50  		return testCase != null;
51  	}
52  
53  	public static WsdlTestCase copyTestCase( WsdlTestCase testCase, WsdlTestSuite target, int position )
54  	{
55  		String name = UISupport.prompt( "Specify name of copied TestCase", "Copy TestCase", "Copy of "
56  				+ 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<Interface> requiredInterfaces = new HashSet<Interface>();
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() + "]",
119 					"Move TestCase" ) )
120 			{
121 				WsdlTestCase importedTestCase = target.importTestCase( testCase, testCase.getName(), position, true, false );
122 				if( importedTestCase != null )
123 				{
124 					testCase.getTestSuite().removeTestCase( testCase );
125 					return importedTestCase;
126 				}
127 			}
128 		}
129 		else if( UISupport.confirm( "Move TestCase [" + testCase.getName() + "] to TestSuite [" + target.getName() + "]",
130 				"Move TestCase" ) )
131 		{
132 			Set<Interface> requiredInterfaces = new HashSet<Interface>();
133 
134 			// get required interfaces
135 			for( int y = 0; y < testCase.getTestStepCount(); y++ )
136 			{
137 				WsdlTestStep testStep = testCase.getTestStepAt( y );
138 				requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
139 			}
140 
141 			if( DragAndDropSupport.importRequiredInterfaces( target.getProject(), requiredInterfaces, "Move TestCase" ) )
142 			{
143 				WsdlTestCase importedTestCase = target.importTestCase( testCase, testCase.getName(), position, true, false );
144 				if( importedTestCase != null )
145 				{
146 					testCase.getTestSuite().removeTestCase( testCase );
147 					return importedTestCase;
148 				}
149 			}
150 		}
151 
152 		return null;
153 	}
154 
155 	@Override
156 	String getCopyAfterInfo( WsdlTestCase source, WsdlTestSuite target )
157 	{
158 		return "Copy TestCase [" + source.getName() + "] to TestSuite [" + target.getName() + "]";
159 	}
160 
161 	@Override
162 	String getMoveAfterInfo( WsdlTestCase source, WsdlTestSuite target )
163 	{
164 		return "Move TestCase [" + source.getName() + "] to TestSuite [" + target.getName() + "]";
165 	}
166 
167 }