View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.iface;
14  
15  import java.awt.event.ActionEvent;
16  
17  import javax.swing.AbstractAction;
18  import javax.swing.Action;
19  
20  import com.eviware.soapui.impl.wsdl.WsdlInterface;
21  import com.eviware.soapui.impl.wsdl.WsdlOperation;
22  import com.eviware.soapui.impl.wsdl.WsdlProject;
23  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
24  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
25  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory;
26  import com.eviware.soapui.model.support.ModelSupport;
27  import com.eviware.soapui.support.UISupport;
28  import com.eviware.x.form.XFormDialog;
29  import com.eviware.x.form.support.ADialogBuilder;
30  import com.eviware.x.form.support.AField;
31  import com.eviware.x.form.support.AForm;
32  import com.eviware.x.form.support.AField.AFieldType;
33  
34  public class GenerateTestSuiteAction extends AbstractAction
35  {
36  	private final WsdlInterface iface;
37  
38  	public GenerateTestSuiteAction( WsdlInterface iface )
39  	{
40  		super( "Generate TestSuite" );
41  		
42  		putValue( Action.SHORT_DESCRIPTION, "Generates TestSuite with TestCase(s) for all Operations in this Interface");
43  		this.iface = iface;
44  	}
45  
46  	public void actionPerformed( ActionEvent e )
47  	{
48  		generateTestSuite( iface );
49  	}
50  
51  	public WsdlTestSuite generateTestSuite( WsdlInterface iface )
52  	{
53  		XFormDialog dialog = ADialogBuilder.buildDialog( GenerateForm.class );
54  		
55  		WsdlProject project = ( WsdlProject ) iface.getProject();
56  		String[] testSuites = ModelSupport.getNames( new String[] {"<create>"}, project.getTestSuites());
57  		dialog.setOptions( GenerateForm.TESTSUITE, testSuites );
58  		
59  		if( dialog.show() )
60  		{
61  			String testSuiteName = dialog.getValue( GenerateForm.TESTSUITE );
62  			
63  			if( testSuiteName.equals( "<create>" ))
64  				testSuiteName = UISupport.prompt( "Enter name of TestSuite to create", "Generate TestSuite", iface.getName() + " TestSuite" );
65  			
66  			if( testSuiteName != null && testSuiteName.trim().length() > 0)
67  			{
68  				WsdlTestSuite testSuite = ( WsdlTestSuite ) project.getTestSuiteByName( testSuiteName );
69  				
70  				if( testSuite == null )
71  				{
72  					testSuite = ( WsdlTestSuite ) project.addNewTestSuite( testSuiteName );
73  				}
74  				
75  				int style = dialog.getValueIndex( GenerateForm.STYLE );
76  				if( style == 0 )
77  					generateMulipleTestCases( testSuite, iface );
78  				else if( style == 1 )
79  					generateSingleTestCase( testSuite, iface );
80  				
81  				return testSuite;
82  			}
83  		}
84  		
85  		return null;
86  	}
87  	
88  	private void generateSingleTestCase( WsdlTestSuite testSuite,  WsdlInterface iface  )
89  	{
90  		WsdlTestCase testCase = testSuite.addNewTestCase( iface.getName() + " TestSuite" );
91  		
92  		for( int i = 0; i < iface.getOperationCount(); i++ )
93  		{
94  			WsdlOperation operation = ( WsdlOperation ) iface.getOperationAt( i );
95  			testCase.addTestStep( WsdlTestRequestStepFactory.createConfig( operation, operation.getName() ) );
96  		}
97  		
98  		UISupport.showDesktopPanel( testCase );
99  	}
100 
101 	private void generateMulipleTestCases( WsdlTestSuite testSuite,  WsdlInterface iface  )
102 	{
103 		for( int i = 0; i < iface.getOperationCount(); i++ )
104 		{
105 			WsdlOperation operation = ( WsdlOperation ) iface.getOperationAt( i );
106 			WsdlTestCase testCase = testSuite.addNewTestCase( operation.getName() + " TestCase" );
107 			testCase.addTestStep( WsdlTestRequestStepFactory.createConfig( operation, operation.getName() ) );
108 		}
109 		
110 		UISupport.showDesktopPanel( testSuite );
111 	}
112 
113 	@AForm( name="Generate TestSuite", description = "Generates TestSuite with TestCase(s) for all Operations in this Interface")
114 	private class GenerateForm 
115 	{
116 		@AField(name = "TestSuite", description = "The TestSuite to create or use", type = AFieldType.ENUMERATION )
117 		public final static String TESTSUITE = "TestSuite";
118 		
119 		@AField(name = "Style", description = "Select the style of TestCases to create", 
120 					type = AFieldType.ENUMERATION, values = { "One TestCase for each Operation", "Single TestCase with one Request for each Operation"} )
121 		public final static String STYLE = "Style";
122 	}
123 }