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.testcase;
14  
15  import java.io.IOException;
16  import java.util.HashMap;
17  
18  import com.eviware.soapui.SoapUI;
19  import com.eviware.soapui.impl.wsdl.WsdlProject;
20  import com.eviware.soapui.impl.wsdl.actions.project.StartLoadUI;
21  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
22  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
23  import com.eviware.soapui.integration.loadui.ContextMapping;
24  import com.eviware.soapui.integration.loadui.IntegrationUtils;
25  import com.eviware.soapui.support.StringUtils;
26  import com.eviware.soapui.support.UISupport;
27  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
28  import com.eviware.x.form.XFormDialog;
29  import com.eviware.x.form.XFormField;
30  import com.eviware.x.form.XFormFieldListener;
31  import com.eviware.x.form.support.ADialogBuilder;
32  import com.eviware.x.form.support.AField;
33  import com.eviware.x.form.support.AForm;
34  import com.eviware.x.form.support.AField.AFieldType;
35  
36  public class RunTestCaseWithLoadUIAction extends AbstractSoapUIAction<WsdlTestCase>
37  {
38  	private static final String EMPTY_OPTION = "-";
39  	private XFormDialog dialog;
40  	public static final String SOAPUI_ACTION_ID = "RunTestCaseWithLoadUIAction";
41  
42  	public RunTestCaseWithLoadUIAction()
43  	{
44  		super( "Run with loadUI", "Run this TestCase with loadUI" );
45  	}
46  
47  	public void perform( WsdlTestCase testCase, Object param )
48  	{
49  		if( !StartLoadUI.testCajoConnection() )
50  		{
51  			if( UISupport.confirm( StartLoadUI.LOADUI_LAUNCH_QUESTION, StartLoadUI.LOADUI_LAUNCH_TITLE ) )
52  			{
53  				StartLoadUI.launchLoadUI();
54  			}
55  			return;
56  		}
57  
58  		checkProjectSaved( testCase.getTestSuite().getProject() );
59  
60  		final String soapUITestCase = testCase.getName();
61  		final String soapUITestSuite = testCase.getTestSuite().getName();
62  		final String soapUIProjectPath = testCase.getTestSuite().getProject().getPath();
63  		String generatorType = EMPTY_OPTION;
64  		String analisysType = EMPTY_OPTION;
65  		if( dialog == null )
66  			dialog = ADialogBuilder.buildDialog( Form.class );
67  
68  		dialog.getFormField( Form.PROJECT ).addFormFieldListener( new XFormFieldListener()
69  		{
70  
71  			public void valueChanged( XFormField sourceField, String newValue, String oldValue )
72  			{
73  				dialog.setOptions( Form.TESTCASE, IntegrationUtils.getAvailableTestCases( newValue ) );
74  				if( dialog.getValue( Form.TESTCASE ).equals( IntegrationUtils.CREATE_ON_PROJECT_LEVEL ) )
75  				{
76  					dialog.setOptions( Form.SOAPUIRUNNER, IntegrationUtils.getAvailableRunners( newValue,
77  							IntegrationUtils.CREATE_ON_PROJECT_LEVEL ) );
78  				}
79  			}
80  		} );
81  		dialog.getFormField( Form.TESTCASE ).addFormFieldListener( new XFormFieldListener()
82  		{
83  
84  			public void valueChanged( XFormField sourceField, String newValue, String oldValue )
85  			{
86  				if( newValue.equals( IntegrationUtils.CREATE_NEW_OPTION ) )
87  				{
88  					dialog.setOptions( Form.SOAPUIRUNNER, new String[] { IntegrationUtils.CREATE_NEW_OPTION } );
89  				}
90  				else
91  				{
92  					dialog.setOptions( Form.SOAPUIRUNNER, IntegrationUtils.getAvailableRunners( dialog
93  							.getValue( Form.PROJECT ), newValue ) );
94  				}
95  			}
96  		} );
97  
98  		dialog.setOptions( Form.PROJECT, IntegrationUtils.getAvailableProjects() );
99  		if( !StringUtils.isNullOrEmpty( IntegrationUtils.getOpenedProjectName() ) )
100 		{
101 			dialog.setValue( Form.PROJECT, IntegrationUtils.getOpenedProjectName() );
102 		}
103 		else
104 		{
105 			dialog.setValue( Form.PROJECT, IntegrationUtils.CREATE_NEW_OPTION );
106 		}
107 		dialog.setOptions( Form.TESTCASE, IntegrationUtils.getAvailableTestCases( dialog.getValue( Form.PROJECT ) ) );
108 		if( !dialog.getValue( Form.PROJECT ).equals( IntegrationUtils.getOpenedProjectName() ) )
109 		{
110 			dialog.setValue( Form.TESTCASE, IntegrationUtils.CREATE_ON_PROJECT_LEVEL );
111 		}
112 
113 		dialog.setOptions( Form.SOAPUIRUNNER, IntegrationUtils.getAvailableRunners( dialog.getValue( Form.PROJECT ),
114 				dialog.getValue( Form.TESTCASE ) ) );
115 		dialog.setValue( Form.SOAPUIRUNNER, IntegrationUtils.CREATE_NEW_OPTION );
116 
117 		dialog.setOptions( Form.GENERATOR, new String[] { EMPTY_OPTION, "Fixed Rate", "Variance", "Random", "Ramp",
118 				"Virtual Users", "Fixed Load" } );
119 		dialog.setValue( Form.GENERATOR, EMPTY_OPTION );
120 
121 		dialog.setOptions( Form.STATISTICS, new String[] { EMPTY_OPTION, "Statistics" } );
122 		dialog.setValue( Form.STATISTICS, EMPTY_OPTION );
123 
124 		if( dialog.show() )
125 		{
126 			String targetProjectString = dialog.getValue( Form.PROJECT );
127 			String targetTestCaseName = !dialog.getValue( Form.TESTCASE )
128 					.equals( IntegrationUtils.CREATE_ON_PROJECT_LEVEL ) ? dialog.getValue( Form.TESTCASE ) : null;
129 			String targetSoapUIRunnerName = dialog.getValue( Form.SOAPUIRUNNER );
130 			generatorType = dialog.getValue( Form.GENERATOR );
131 			analisysType = dialog.getValue( Form.STATISTICS );
132 			if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
133 			{
134 				String openedProjectName = IntegrationUtils.getOpenedProjectName();
135 				if( !StringUtils.isNullOrEmpty( openedProjectName ) && !targetProjectString.equals( openedProjectName ) )
136 					if( UISupport.confirm( "Close currently open [" + IntegrationUtils.getOpenedProjectName()
137 							+ "] loadUI project", "Close loadUI project" ) )
138 					{
139 						IntegrationUtils.closeOpenedLoadUIProject();
140 					}
141 					else
142 					{
143 						return;
144 					}
145 
146 				HashMap<String, String> createdRunnerSettings = null;
147 				try
148 				{
149 					createdRunnerSettings = IntegrationUtils.createSoapUIRunner( soapUIProjectPath, soapUITestSuite,
150 							soapUITestCase, targetProjectString, targetTestCaseName, targetSoapUIRunnerName, generatorType,
151 							analisysType );
152 				}
153 				catch( IOException e )
154 				{
155 					UISupport.showInfoMessage( "Error while opening selected loadUI project" );
156 					return;
157 				}
158 				// next code was for getting back info on items created on loadUI side - removed for now
159 //				if( createdRunnerSettings != null )
160 //				{
161 //					String creationInfo = "SoapUI Runner created/updated under project: '"
162 //							+ createdRunnerSettings.get( ContextMapping.LOADUI_PROJECT_NAME ) + "'";
163 //					if( targetTestCaseName != null && !targetTestCaseName.equals( IntegrationUtils.CREATE_ON_PROJECT_LEVEL ) )
164 //					{
165 //						creationInfo += ", TestCase: '" + createdRunnerSettings.get( ContextMapping.LOADUI_TEST_CASE_NAME )
166 //								+ "'";
167 //					}
168 //					creationInfo += "Transfer focus to loadUI?";
169 //					if( UISupport.confirm( creationInfo, IntegrationUtils.LOADU_INFO_DIALOG_TITLE ) )
170 //					{
171 //						IntegrationUtils.bringLoadUIToFront();
172 //					}
173 //				}
174 			}
175 
176 		}
177 	}
178 
179 	private void checkProjectSaved( WsdlProject project )
180 	{
181 		String path = project.getPath();
182 		if( path == null )
183 		{
184 			try
185 			{
186 				project.save();
187 			}
188 			catch( IOException e )
189 			{
190 				SoapUI.logError( e );
191 			}
192 		}
193 
194 	}
195 
196 	@AForm( description = "Specify Items in loadUI for Running TestCase", name = "Run With loadUI", helpUrl = HelpUrls.CLONETESTCASE_HELP_URL, icon = UISupport.LOADUI_ICON_PATH )
197 	protected interface Form
198 	{
199 		@AField( name = "Target Project", description = "The target Project in loadUI", type = AFieldType.ENUMERATION )
200 		public final static String PROJECT = "Target Project";
201 
202 		@AField( name = "Target TestCase", description = "The name of the target TestCase in loadUI", type = AFieldType.ENUMERATION )
203 		public final static String TESTCASE = "Target TestCase";
204 
205 		@AField( name = "Target SoapUI Runner", description = "The target SoapUI Runner in loadUI", type = AFieldType.ENUMERATION )
206 		public final static String SOAPUIRUNNER = "Target SoapUI Runner";
207 
208 		@AField( name = "Default Generator", description = "Choose generator type in loadUI", type = AFieldType.ENUMERATION )
209 		public final static String GENERATOR = "Default Generator";
210 
211 		@AField( name = "Default Statistics", description = "Choose Statistics in loadUI", type = AFieldType.ENUMERATION )
212 		public final static String STATISTICS = "Default Statistics";
213 
214 	}
215 
216 }