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.WsdlProject;
12  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
13  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
14  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
15  import com.eviware.soapui.model.support.ModelSupport;
16  import com.eviware.soapui.support.UISupport;
17  
18  public class TestCaseToProjectDropHandler extends AbstractAfterModelItemDropHandler<WsdlTestCase, WsdlProject>
19  {
20  	public TestCaseToProjectDropHandler()
21  	{
22  		super( WsdlTestCase.class, WsdlProject.class );
23  	}
24  	
25  	@Override
26  	boolean canCopyAfter( WsdlTestCase source, WsdlProject target )
27  	{
28  		return true;
29  	}
30  
31  	@Override
32  	boolean canMoveAfter( WsdlTestCase source, WsdlProject target )
33  	{
34  		return true;
35  	}
36  
37  	@Override
38  	boolean copyAfter( WsdlTestCase testCase, WsdlProject target )
39  	{
40  		WsdlTestSuite testSuite = getTargetTestSuite( target, "Copy TestCase" );
41  		if( testSuite == null )
42  			return false;
43  		
44  		testCase = TestCaseToTestSuiteDropHandler.copyTestCase( testCase, testSuite, -1 );
45  		if( testCase != null )
46  			UISupport.select( testCase );
47  		
48  		return testCase != null;
49  	}
50  
51  	private WsdlTestSuite getTargetTestSuite( WsdlProject target, String title )
52  	{
53  		String name = "TestSuite 1";
54  		if( target.getTestSuiteCount() > 0 )
55  		{
56  			String[] names = ModelSupport.getNames( target.getTestSuiteList(), new String [] {"<Create New>"} );
57  			name = UISupport.prompt( "Specify target TestSuite for TestCase", title, names );
58  			if( name == null )
59  				return null;
60  		}
61  		
62  		WsdlTestSuite testSuite = target.getTestSuiteByName( name );
63  		if( testSuite == null )
64  		{
65  			name = UISupport.prompt( "Specify name for new TestSuite", title, "TestSuite " + (target.getTestSuiteCount()+1));
66  			if( name == null )
67  				return null;
68  			
69  			testSuite = target.addNewTestSuite( name );
70  		}
71  		
72  		Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
73  		
74  		for( int i = 0; i < testSuite.getTestCaseCount(); i++ )
75  		{
76  			WsdlTestCase testCase = testSuite.getTestCaseAt( i );
77  			
78  			for( int y = 0; y < testCase.getTestStepCount(); y++ )
79  			{
80  				WsdlTestStep testStep = testCase.getTestStepAt( y );
81  				requiredInterfaces.addAll( testStep.getRequiredInterfaces() );
82  			}
83  		}
84  		
85  		if( !DragAndDropSupport.importRequiredInterfaces( target, requiredInterfaces, title ))
86  			return null;
87  		else
88  			return testSuite;
89  	}
90  
91  	@Override
92  	boolean moveAfter( WsdlTestCase testCase, WsdlProject target )
93  	{
94  		WsdlTestSuite testSuite = getTargetTestSuite( target, "Move TestCase" );
95  		if( testSuite == null )
96  			return false;
97  		
98  		testCase = TestCaseToTestSuiteDropHandler.moveTestCase( testCase, testSuite, -1 );
99  		if( testCase != null )
100 			UISupport.select( testCase );
101 		
102 		return testCase != null;
103 	}
104 
105 	@Override
106 	String getCopyAfterInfo( WsdlTestCase source, WsdlProject target )
107 	{
108 		return "Copy TestCase [" + source.getName() + "] to TestSuite in Project [" + target.getName() + "]";
109 	}
110 
111 	@Override
112 	String getMoveAfterInfo( WsdlTestCase source, WsdlProject target )
113 	{
114 		return "Move TestCase [" + source.getName() + "] to TestSuite in Project [" + target.getName() + "]";
115 	}
116 }