1
2
3
4
5
6
7
8
9
10
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 ) return null;
82
83 WsdlTestSuite testSuite = (WsdlTestSuite) project.getTestSuiteAt( testSuiteNames.indexOf( selection ));
84
85 String name = UISupport.prompt( "Enter name for TestCase create", "Create TestCase",
86 "TestCase " + (testSuite.getTestCaseCount()+1));
87 if( name == null ) return null;
88
89 return testSuite.addNewTestCase( name );
90 }
91 else
92 {
93 testCases.add( null );
94 testSuites.add( null );
95 testCaseNames.add( (testCaseNames.size()+1) + ": -> Create new TestSuite" );
96
97 String selection = (String) UISupport.prompt( "Select TestCase", "Select TestCase", testCaseNames.toArray() );
98 if( selection == null ) return null;
99
100 testCase = testCases.get( testCaseNames.indexOf( selection ));
101 while( testCase != null && SoapUI.getTestMonitor().hasRunningLoadTest( testCase ))
102 {
103 UISupport.showErrorMessage( "Can not add to TestCase that is currently LoadTesting" );
104
105 selection = (String) UISupport.prompt( "Select TestCase", "Select TestCase", testCaseNames.toArray() );
106 if( selection == null ) return null;
107
108 testCase = testCases.get( testCaseNames.indexOf( selection ));
109 }
110
111
112 if( testCase == null )
113 {
114 WsdlTestSuite testSuite = testSuites.get( testCaseNames.indexOf( selection ) );
115
116
117 if( testSuite == null )
118 {
119 return addNewTestSuiteAndTestCase( project );
120 }
121 else
122 {
123 String name = UISupport.prompt( "Enter name for TestCase create", "Create TestCase",
124 "TestCase " + (testSuite.getTestCaseCount()+1));
125 if( name == null ) return null;
126
127 return testSuite.addNewTestCase( name );
128 }
129 }
130 }
131
132 return testCase;
133 }
134
135 protected WsdlTestCase addNewTestSuiteAndTestCase( WsdlProject project )
136 {
137 String testSuiteName = UISupport.prompt( "Missing TestSuite in project, enter name to create", "Create TestSuite",
138 "TestSuite " + (project.getTestSuiteCount()+1) );
139 if( testSuiteName == null ) return null;
140
141 String testCaseName = UISupport.prompt( "Enter name for TestCase create", "Create TestCase",
142 "TestCase 1" );
143 if( testCaseName == null )
144 return null;
145
146 WsdlTestSuite testSuite = ( WsdlTestSuite ) project.addNewTestSuite( testSuiteName );
147 return testSuite.addNewTestCase( testCaseName );
148 }
149 }