1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.panels.teststeps;
14
15 import java.awt.BorderLayout;
16 import java.awt.Component;
17 import java.awt.Dimension;
18 import java.awt.event.ActionEvent;
19 import java.beans.PropertyChangeEvent;
20 import java.beans.PropertyChangeListener;
21 import java.util.List;
22
23 import javax.swing.AbstractAction;
24 import javax.swing.Action;
25 import javax.swing.BorderFactory;
26 import javax.swing.JComponent;
27 import javax.swing.SwingUtilities;
28 import javax.swing.border.TitledBorder;
29
30 import com.eviware.soapui.SoapUI;
31 import com.eviware.soapui.impl.wsdl.WsdlProject;
32 import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
33 import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
34 import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunContext;
35 import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunner;
36 import com.eviware.soapui.impl.wsdl.panels.testcase.TestRunLog;
37 import com.eviware.soapui.impl.wsdl.panels.testcase.TestRunLog.TestRunLogTestRunListener;
38 import com.eviware.soapui.impl.wsdl.panels.teststeps.support.PropertyHolderTable;
39 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
40 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
41 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner;
42 import com.eviware.soapui.impl.wsdl.teststeps.WsdlRunTestCaseTestStep;
43 import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStepResult;
44 import com.eviware.soapui.model.ModelItem;
45 import com.eviware.soapui.model.support.ModelSupport;
46 import com.eviware.soapui.model.testsuite.TestCase;
47 import com.eviware.soapui.model.testsuite.TestRunContext;
48 import com.eviware.soapui.model.testsuite.TestRunner;
49 import com.eviware.soapui.support.UISupport;
50 import com.eviware.soapui.support.components.JComponentInspector;
51 import com.eviware.soapui.support.components.JInspectorPanel;
52 import com.eviware.soapui.support.components.JXToolBar;
53 import com.eviware.soapui.support.types.StringList;
54 import com.eviware.soapui.ui.support.ModelItemDesktopPanel;
55 import com.eviware.x.form.XFormDialog;
56 import com.eviware.x.form.XFormField;
57 import com.eviware.x.form.XFormFieldListener;
58 import com.eviware.x.form.support.ADialogBuilder;
59 import com.eviware.x.form.support.AField;
60 import com.eviware.x.form.support.AForm;
61 import com.eviware.x.form.support.XFormMultiSelectList;
62 import com.eviware.x.form.support.AField.AFieldType;
63
64 public class WsdlRunTestCaseStepDesktopPanel extends ModelItemDesktopPanel<WsdlRunTestCaseTestStep> implements PropertyChangeListener
65 {
66 private WsdlProject project;
67 private TitledBorder titledBorder;
68 private OptionsAction optionsAction;
69 private RunAction runAction;
70 private OpenTestCaseAction openTestCaseAction;
71 private TestRunLog testRunLog;
72 private CancelRunTestCaseAction cancelAction;
73 private XFormDialog optionsDialog;
74
75 public WsdlRunTestCaseStepDesktopPanel( WsdlRunTestCaseTestStep modelItem )
76 {
77 super( modelItem );
78
79 project = getModelItem().getTestCase().getTestSuite().getProject();
80
81 getModelItem().addPropertyChangeListener( WsdlRunTestCaseTestStep.TARGET_TESTCASE, this );
82
83 buildUI();
84 setEnabledState();
85
86 if( modelItem.getTargetTestCase() == null )
87 {
88 SwingUtilities.invokeLater( new Runnable() {
89
90 public void run()
91 {
92 optionsAction.actionPerformed( null );
93 }} );
94 }
95
96 setPreferredSize( new Dimension( 400, 600 ) );
97 }
98
99 private void setEnabledState()
100 {
101 runAction.setEnabled( getModelItem().getTargetTestCase() != null );
102 openTestCaseAction.setEnabled( getModelItem().getTargetTestCase() != null );
103 }
104
105 private void buildUI()
106 {
107 add( buildToolbar(), BorderLayout.NORTH );
108 add( buildContent(), BorderLayout.CENTER );
109 }
110
111 private Component buildContent()
112 {
113 JInspectorPanel inspectorPanel = new JInspectorPanel( createPropertiesTable() );
114
115 inspectorPanel.addInspector( new JComponentInspector( buildLog(), "TestCase Log", "log output from testcase run", true ) );
116
117 return inspectorPanel;
118 }
119
120 private JComponent buildLog()
121 {
122 testRunLog = new TestRunLog( getModelItem().getSettings() );
123 return testRunLog;
124 }
125
126 protected JComponent createPropertiesTable()
127 {
128 PropertyHolderTable propertyHolderTable = new PropertyHolderTable( getModelItem() );
129
130 titledBorder = BorderFactory.createTitledBorder( createTitleForBorder() );
131 propertyHolderTable.setBorder( titledBorder);
132
133 return propertyHolderTable;
134 }
135
136 private String createTitleForBorder()
137 {
138 WsdlTestCase targetTestCase = getModelItem().getTargetTestCase();
139 return "TestCase [" + (targetTestCase == null ? "-none selected-" : targetTestCase.getName() ) + "] Run Properties";
140 }
141
142 private Component buildToolbar()
143 {
144 JXToolBar toolbar = UISupport.createToolbar();
145
146 toolbar.add( UISupport.createToolbarButton( runAction = new RunAction() ));
147 toolbar.add( UISupport.createToolbarButton( cancelAction = new CancelRunTestCaseAction(), false ));
148 toolbar.add( UISupport.createToolbarButton( optionsAction = new OptionsAction() ));
149 toolbar.add( UISupport.createToolbarButton( openTestCaseAction = new OpenTestCaseAction() ));
150
151 toolbar.addGlue();
152 toolbar.add( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.RUNTESTCASESTEP_HELP_URL )));
153
154 return toolbar;
155 }
156
157 @Override
158 public boolean dependsOn( ModelItem modelItem )
159 {
160 WsdlRunTestCaseTestStep callStep = getModelItem();
161
162 return modelItem == callStep || modelItem == callStep.getTestCase() ||
163 modelItem == callStep.getTestCase().getTestSuite() ||
164 modelItem == callStep.getTestCase().getTestSuite().getProject();
165 }
166
167 public boolean onClose( boolean canCancel )
168 {
169 getModelItem().removePropertyChangeListener( WsdlRunTestCaseTestStep.TARGET_TESTCASE, this );
170 testRunLog.release();
171 if( optionsDialog != null )
172 {
173 optionsDialog.release();
174 optionsDialog = null;
175 }
176
177 return release();
178 }
179
180 private class RunAction extends AbstractAction
181 {
182 public RunAction()
183 {
184 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/run_testcase.gif" ));
185 putValue( Action.SHORT_DESCRIPTION, "Runs the selected TestCases" );
186 }
187
188 public void actionPerformed(ActionEvent e)
189 {
190 WsdlRunTestCaseTestStep testStep = getModelItem();
191 InternalTestRunListener testRunListener = new InternalTestRunListener();
192 testStep.addTestRunListener( testRunListener );
193
194 try
195 {
196 MockTestRunner mockTestRunner = new MockTestRunner( testStep.getTestCase(), SoapUI.ensureGroovyLog() );
197 WsdlTestStepResult result = (WsdlTestStepResult) testStep.run( mockTestRunner,
198 new MockTestRunContext( mockTestRunner, testStep ) );
199
200 Throwable er = result.getError();
201 if( er != null )
202 {
203 UISupport.showErrorMessage( er.toString() );
204 }
205 }
206 catch( Throwable t )
207 {
208 UISupport.showErrorMessage( t );
209 }
210 finally
211 {
212 testStep.removeTestRunListener( testRunListener );
213 }
214 }
215 }
216
217 private class OpenTestCaseAction extends AbstractAction
218 {
219 public OpenTestCaseAction()
220 {
221 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/testCase.gif" ));
222 putValue( Action.SHORT_DESCRIPTION, "Opens the target TestCases editor" );
223 }
224
225 public void actionPerformed(ActionEvent e)
226 {
227 WsdlTestCase targetTestCase = getModelItem().getTargetTestCase();
228 if( targetTestCase == null )
229 UISupport.showErrorMessage( "No target TestCase selected" );
230 else
231 UISupport.showDesktopPanel( targetTestCase );
232 }
233 }
234
235 private class OptionsAction extends AbstractAction
236 {
237 public OptionsAction()
238 {
239 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/options.gif" ));
240 putValue( Action.SHORT_DESCRIPTION, "Sets Options");
241 }
242
243 public void actionPerformed( ActionEvent e )
244 {
245 if( optionsDialog == null )
246 {
247 optionsDialog = ADialogBuilder.buildDialog( OptionsForm.class );
248 optionsDialog.getFormField( OptionsForm.TESTSUITE ).addFormFieldListener( new XFormFieldListener() {
249
250 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
251 {
252 List<TestCase> testCaseList = project.getTestSuiteByName( newValue ).getTestCaseList();
253 testCaseList.remove( getModelItem().getTestCase() );
254 optionsDialog.setOptions( OptionsForm.TESTCASE, ModelSupport.getNames( testCaseList ));
255
256 if( testCaseList.size() > 0 )
257 {
258 WsdlTestCase testCase = project.getTestSuiteByName( newValue ).getTestCaseAt( 0 );
259 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, testCase.getPropertyNames() );
260 ((XFormMultiSelectList)optionsDialog.getFormField( OptionsForm.RETURN_PROPERTIES )).setSelectedOptions(
261 getModelItem().getReturnProperties().toStringArray() );
262 }
263 }
264 } );
265 optionsDialog.getFormField( OptionsForm.TESTCASE ).addFormFieldListener( new XFormFieldListener() {
266
267 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
268 {
269 WsdlTestSuite testSuite = project.getTestSuiteByName( optionsDialog.getValue( OptionsForm.TESTSUITE ) );
270 WsdlTestCase testCase = testSuite.getTestCaseByName( newValue );
271 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, testCase.getPropertyNames() );
272 ((XFormMultiSelectList)optionsDialog.getFormField( OptionsForm.RETURN_PROPERTIES )).setSelectedOptions(
273 getModelItem().getReturnProperties().toStringArray() );
274 }
275 } );
276 }
277
278 WsdlTestCase targetTestCase = getModelItem().getTargetTestCase();
279
280 optionsDialog.setOptions( OptionsForm.TESTSUITE, ModelSupport.getNames( project.getTestSuiteList() ) );
281 if( targetTestCase != null )
282 {
283 optionsDialog.setValue( OptionsForm.TESTSUITE, targetTestCase.getTestSuite().getName() );
284
285 List<TestCase> testCaseList = targetTestCase.getTestSuite().getTestCaseList();
286 testCaseList.remove( getModelItem().getTestCase() );
287
288 optionsDialog.setOptions( OptionsForm.TESTCASE, ModelSupport.getNames( testCaseList ));
289 optionsDialog.setValue( OptionsForm.TESTCASE, targetTestCase.getName() );
290
291 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, targetTestCase.getPropertyNames() );
292 ((XFormMultiSelectList)optionsDialog.getFormField( OptionsForm.RETURN_PROPERTIES )).setSelectedOptions(
293 getModelItem().getReturnProperties().toStringArray() );
294 }
295 else
296 {
297 if( project.getTestSuiteCount() == 0 )
298 {
299 optionsDialog.setOptions( OptionsForm.TESTCASE, new String[0] );
300 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, new String[0] );
301 }
302 else
303 {
304 List<TestCase> testCaseList = project.getTestSuiteAt(0).getTestCaseList();
305 testCaseList.remove( getModelItem().getTestCase() );
306 optionsDialog.setOptions( OptionsForm.TESTCASE, ModelSupport.getNames( testCaseList ) );
307
308 if( testCaseList.isEmpty() )
309 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, new String[0] );
310 else
311 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, testCaseList.get( 0 ).getPropertyNames() );
312 }
313 }
314
315 if( optionsDialog.show() )
316 {
317 WsdlTestSuite testSuite = project.getTestSuiteByName( optionsDialog.getValue( OptionsForm.TESTSUITE ) );
318 getModelItem().setTargetTestCase( testSuite == null ? null :
319 testSuite.getTestCaseByName( optionsDialog.getValue( OptionsForm.TESTCASE ) ));
320 getModelItem().setReturnProperties( new StringList(
321 ((XFormMultiSelectList)optionsDialog.getFormField( OptionsForm.RETURN_PROPERTIES )).getSelectedOptions()) );
322 }
323 }
324 }
325
326 @AForm( name="Run TestCase Options", description="Set options for the Run TestCase Step below" )
327 private static interface OptionsForm
328 {
329 @AField( name="Target TestCase", description="Selects the TestCase to run", type=AFieldType.ENUMERATION)
330 public static final String TESTCASE = "Target TestCase";
331
332 @AField( name="Target TestSuite", description="Selects the containing TestSuite to run", type=AFieldType.ENUMERATION)
333 public static final String TESTSUITE = "Target TestSuite";
334
335 @AField( name="Return Properties", description="Selects the properties that are return values", type=AFieldType.MULTILIST)
336 public static final String RETURN_PROPERTIES = "Return Properties";
337 }
338
339 public void propertyChange( PropertyChangeEvent evt )
340 {
341 setEnabledState();
342 titledBorder.setTitle( createTitleForBorder() );
343 repaint();
344 }
345
346 public class InternalTestRunListener extends TestRunLogTestRunListener
347 {
348 public InternalTestRunListener()
349 {
350 super( testRunLog, true );
351 }
352
353 public void beforeRun( TestRunner testRunner, TestRunContext runContext )
354 {
355 runAction.setEnabled( false );
356 cancelAction.setEnabled( true );
357 }
358
359 public void afterRun( TestRunner testRunner, TestRunContext runContext )
360 {
361 runAction.setEnabled( true );
362 cancelAction.setEnabled( false );
363 }
364 }
365
366 public class CancelRunTestCaseAction extends AbstractAction
367 {
368 public CancelRunTestCaseAction()
369 {
370 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/stop_testcase.gif" ) );
371 putValue( Action.SHORT_DESCRIPTION, "Stops running this testcase" );
372 }
373
374 public void actionPerformed( ActionEvent e )
375 {
376 WsdlTestCaseRunner testCaseRunner = getModelItem().getTestCaseRunner();
377 if( testCaseRunner != null )
378 testCaseRunner.cancel( "Canceled from RunTestCase UI" );
379 }
380 }
381 }