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.impl.wsdl.actions.support;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import com.eviware.soapui.SoapUI;
19  import com.eviware.soapui.impl.wsdl.WsdlProject;
20  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
21  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
22  import com.eviware.soapui.model.ModelItem;
23  import com.eviware.soapui.model.testsuite.TestSuite;
24  import com.eviware.soapui.support.UISupport;
25  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
26  
27  /***
28   * Base class for actions that add TestSteps to a TestCase
29   * 
30   * @author ole.matzura
31   */
32  
33  public abstract class AbstractAddToTestCaseAction<T extends ModelItem> extends AbstractSoapUIAction<T>
34  {
35  	public AbstractAddToTestCaseAction( String name, String description )
36  	{
37  		super( name, description );
38  	}
39  
40  	protected WsdlTestCase getTargetTestCase( WsdlProject project )
41  	{
42  		List<WsdlTestCase> testCases = new ArrayList<WsdlTestCase>();
43  		List<WsdlTestSuite> testSuites = new ArrayList<WsdlTestSuite>();
44  		List<String> testCaseNames = new ArrayList<String>();
45  		WsdlTestCase testCase = null;
46  
47  		if( project.getTestSuiteCount() == 0 )
48  		{
49  			return addNewTestSuiteAndTestCase( project );
50  		}
51  
52  		for( int c = 0; c < project.getTestSuiteCount(); c++ )
53  		{
54  			WsdlTestSuite testSuite = ( WsdlTestSuite )project.getTestSuiteAt( c );
55  			for( int i = 0; i < testSuite.getTestCaseCount(); i++ )
56  			{
57  				testCase = ( WsdlTestCase )testSuite.getTestCaseAt( i );
58  
59  				testCases.add( testCase );
60  				testCaseNames.add( ( testCaseNames.size() + 1 ) + ": " + testSuite.getName() + " - " + testCase.getName() );
61  				testSuites.add( testSuite );
62  			}
63  
64  			testCases.add( null );
65  			testSuites.add( testSuite );
66  			testCaseNames.add( ( testCaseNames.size() + 1 ) + ": " + testSuite.getName() + " -> Create new TestCase" );
67  		}
68  
69  		if( testCases.size() == 0 )
70  		{
71  			List<String> testSuiteNames = new ArrayList<String>();
72  
73  			for( int c = 0; c < project.getTestSuiteCount(); c++ )
74  			{
75  				TestSuite testSuite = project.getTestSuiteAt( c );
76  				testSuiteNames.add( ( testSuiteNames.size() + 1 ) + ": " + testSuite.getName() );
77  			}
78  
79  			String selection = ( String )UISupport.prompt( "Select TestSuite to create TestCase in", "Select TestSuite",
80  					testSuiteNames.toArray() );
81  			if( selection == null )
82  				return null;
83  
84  			WsdlTestSuite testSuite = ( WsdlTestSuite )project.getTestSuiteAt( testSuiteNames.indexOf( selection ) );
85  
86  			String name = UISupport.prompt( "Enter name for TestCase create", "Create TestCase", "TestCase "
87  					+ ( testSuite.getTestCaseCount() + 1 ) );
88  			if( name == null )
89  				return null;
90  
91  			return testSuite.addNewTestCase( name );
92  		}
93  		else
94  		{
95  			testCases.add( null );
96  			testSuites.add( null );
97  			testCaseNames.add( ( testCaseNames.size() + 1 ) + ": -> Create new TestSuite" );
98  
99  			String selection = ( String )UISupport.prompt( "Select TestCase", "Select TestCase", testCaseNames.toArray() );
100 			if( selection == null )
101 				return null;
102 
103 			testCase = testCases.get( testCaseNames.indexOf( selection ) );
104 			while( testCase != null && SoapUI.getTestMonitor().hasRunningLoadTest( testCase ) )
105 			{
106 				UISupport.showErrorMessage( "Can not add to TestCase that is currently LoadTesting" );
107 
108 				selection = ( String )UISupport.prompt( "Select TestCase", "Select TestCase", testCaseNames.toArray() );
109 				if( selection == null )
110 					return null;
111 
112 				testCase = testCases.get( testCaseNames.indexOf( selection ) );
113 			}
114 
115 			// selected create new?
116 			if( testCase == null )
117 			{
118 				WsdlTestSuite testSuite = testSuites.get( testCaseNames.indexOf( selection ) );
119 
120 				// selected create new testsuite?
121 				if( testSuite == null )
122 				{
123 					return addNewTestSuiteAndTestCase( project );
124 				}
125 				else
126 				{
127 					String name = UISupport.prompt( "Enter name for TestCase create", "Create TestCase", "TestCase "
128 							+ ( testSuite.getTestCaseCount() + 1 ) );
129 					if( name == null )
130 						return null;
131 
132 					return testSuite.addNewTestCase( name );
133 				}
134 			}
135 		}
136 
137 		return testCase;
138 	}
139 
140 	protected WsdlTestCase addNewTestSuiteAndTestCase( WsdlProject project )
141 	{
142 		String testSuiteName = UISupport.prompt( "Missing TestSuite in project, enter name to create",
143 				"Create TestSuite", "TestSuite " + ( project.getTestSuiteCount() + 1 ) );
144 		if( testSuiteName == null )
145 			return null;
146 
147 		String testCaseName = UISupport.prompt( "Enter name for TestCase create", "Create TestCase", "TestCase 1" );
148 		if( testCaseName == null )
149 			return null;
150 
151 		WsdlTestSuite testSuite = ( WsdlTestSuite )project.addNewTestSuite( testSuiteName );
152 		return testSuite.addNewTestCase( testCaseName );
153 	}
154 }