View Javadoc

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