View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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 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          // selected create new?
106          if( testCase == null )
107          {
108          	WsdlTestSuite testSuite = testSuites.get( testCaseNames.indexOf( selection ) );
109          	
110          	// selected create new testsuite?
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 }