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 testRunLog.clear();
197 MockTestRunner mockTestRunner = new MockTestRunner( testStep.getTestCase(), SoapUI.ensureGroovyLog() );
198 WsdlTestStepResult result = (WsdlTestStepResult) testStep.run( mockTestRunner,
199 new MockTestRunContext( mockTestRunner, testStep ) );
200
201 Throwable er = result.getError();
202 if( er != null )
203 {
204 UISupport.showErrorMessage( er.toString() );
205 }
206 }
207 catch( Throwable t )
208 {
209 UISupport.showErrorMessage( t );
210 }
211 finally
212 {
213 testStep.removeTestRunListener( testRunListener );
214 }
215 }
216 }
217
218 private class OpenTestCaseAction extends AbstractAction
219 {
220 public OpenTestCaseAction()
221 {
222 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/testCase.gif" ));
223 putValue( Action.SHORT_DESCRIPTION, "Opens the target TestCases editor" );
224 }
225
226 public void actionPerformed(ActionEvent e)
227 {
228 WsdlTestCase targetTestCase = getModelItem().getTargetTestCase();
229 if( targetTestCase == null )
230 UISupport.showErrorMessage( "No target TestCase selected" );
231 else
232 UISupport.showDesktopPanel( targetTestCase );
233 }
234 }
235
236 private class OptionsAction extends AbstractAction
237 {
238 public OptionsAction()
239 {
240 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/options.gif" ));
241 putValue( Action.SHORT_DESCRIPTION, "Sets Options");
242 }
243
244 public void actionPerformed( ActionEvent e )
245 {
246 if( optionsDialog == null )
247 {
248 optionsDialog = ADialogBuilder.buildDialog( OptionsForm.class );
249 optionsDialog.getFormField( OptionsForm.TESTSUITE ).addFormFieldListener( new XFormFieldListener() {
250
251 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
252 {
253 List<TestCase> testCaseList = project.getTestSuiteByName( newValue ).getTestCaseList();
254 testCaseList.remove( getModelItem().getTestCase() );
255 optionsDialog.setOptions( OptionsForm.TESTCASE, ModelSupport.getNames( testCaseList ));
256
257 if( testCaseList.size() > 0 )
258 {
259 WsdlTestCase testCase = project.getTestSuiteByName( newValue ).getTestCaseAt( 0 );
260 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, testCase.getPropertyNames() );
261 ((XFormMultiSelectList)optionsDialog.getFormField( OptionsForm.RETURN_PROPERTIES )).setSelectedOptions(
262 getModelItem().getReturnProperties().toStringArray() );
263 }
264 }
265 } );
266 optionsDialog.getFormField( OptionsForm.TESTCASE ).addFormFieldListener( new XFormFieldListener() {
267
268 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
269 {
270 WsdlTestSuite testSuite = project.getTestSuiteByName( optionsDialog.getValue( OptionsForm.TESTSUITE ) );
271 WsdlTestCase testCase = testSuite.getTestCaseByName( newValue );
272 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, testCase.getPropertyNames() );
273 ((XFormMultiSelectList)optionsDialog.getFormField( OptionsForm.RETURN_PROPERTIES )).setSelectedOptions(
274 getModelItem().getReturnProperties().toStringArray() );
275 }
276 } );
277 }
278
279 WsdlTestCase targetTestCase = getModelItem().getTargetTestCase();
280
281 optionsDialog.setOptions( OptionsForm.TESTSUITE, ModelSupport.getNames( project.getTestSuiteList() ) );
282 if( targetTestCase != null )
283 {
284 optionsDialog.setValue( OptionsForm.TESTSUITE, targetTestCase.getTestSuite().getName() );
285
286 List<TestCase> testCaseList = targetTestCase.getTestSuite().getTestCaseList();
287 testCaseList.remove( getModelItem().getTestCase() );
288
289 optionsDialog.setOptions( OptionsForm.TESTCASE, ModelSupport.getNames( testCaseList ));
290 optionsDialog.setValue( OptionsForm.TESTCASE, targetTestCase.getName() );
291
292 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, targetTestCase.getPropertyNames() );
293 ((XFormMultiSelectList)optionsDialog.getFormField( OptionsForm.RETURN_PROPERTIES )).setSelectedOptions(
294 getModelItem().getReturnProperties().toStringArray() );
295 }
296 else
297 {
298 if( project.getTestSuiteCount() == 0 )
299 {
300 optionsDialog.setOptions( OptionsForm.TESTCASE, new String[0] );
301 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, new String[0] );
302 }
303 else
304 {
305 List<TestCase> testCaseList = project.getTestSuiteAt(0).getTestCaseList();
306 testCaseList.remove( getModelItem().getTestCase() );
307 optionsDialog.setOptions( OptionsForm.TESTCASE, ModelSupport.getNames( testCaseList ) );
308
309 if( testCaseList.isEmpty() )
310 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, new String[0] );
311 else
312 optionsDialog.setOptions( OptionsForm.RETURN_PROPERTIES, testCaseList.get( 0 ).getPropertyNames() );
313 }
314 }
315
316 if( optionsDialog.show() )
317 {
318 WsdlTestSuite testSuite = project.getTestSuiteByName( optionsDialog.getValue( OptionsForm.TESTSUITE ) );
319 getModelItem().setTargetTestCase( testSuite == null ? null :
320 testSuite.getTestCaseByName( optionsDialog.getValue( OptionsForm.TESTCASE ) ));
321 getModelItem().setReturnProperties( new StringList(
322 ((XFormMultiSelectList)optionsDialog.getFormField( OptionsForm.RETURN_PROPERTIES )).getSelectedOptions()) );
323 }
324 }
325 }
326
327 @AForm( name="Run TestCase Options", description="Set options for the Run TestCase Step below" )
328 private static interface OptionsForm
329 {
330 @AField( name="Target TestCase", description="Selects the TestCase to run", type=AFieldType.ENUMERATION)
331 public static final String TESTCASE = "Target TestCase";
332
333 @AField( name="Target TestSuite", description="Selects the containing TestSuite to run", type=AFieldType.ENUMERATION)
334 public static final String TESTSUITE = "Target TestSuite";
335
336 @AField( name="Return Properties", description="Selects the properties that are return values", type=AFieldType.MULTILIST)
337 public static final String RETURN_PROPERTIES = "Return Properties";
338 }
339
340 public void propertyChange( PropertyChangeEvent evt )
341 {
342 setEnabledState();
343 titledBorder.setTitle( createTitleForBorder() );
344 repaint();
345 }
346
347 public class InternalTestRunListener extends TestRunLogTestRunListener
348 {
349 public InternalTestRunListener()
350 {
351 super( testRunLog, true );
352 }
353
354 public void beforeRun( TestRunner testRunner, TestRunContext runContext )
355 {
356 runAction.setEnabled( false );
357 cancelAction.setEnabled( true );
358 }
359
360 public void afterRun( TestRunner testRunner, TestRunContext runContext )
361 {
362 runAction.setEnabled( true );
363 cancelAction.setEnabled( false );
364 }
365 }
366
367 public class CancelRunTestCaseAction extends AbstractAction
368 {
369 public CancelRunTestCaseAction()
370 {
371 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/stop_testcase.gif" ) );
372 putValue( Action.SHORT_DESCRIPTION, "Stops running this testcase" );
373 }
374
375 public void actionPerformed( ActionEvent e )
376 {
377 WsdlTestCaseRunner testCaseRunner = getModelItem().getTestCaseRunner();
378 if( testCaseRunner != null )
379 testCaseRunner.cancel( "Canceled from RunTestCase UI" );
380 }
381 }
382 }