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