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.Dimension;
17 import java.awt.event.ItemEvent;
18 import java.awt.event.ItemListener;
19 import java.beans.PropertyChangeEvent;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22
23 import javax.swing.JButton;
24 import javax.swing.JComboBox;
25 import javax.swing.JComponent;
26 import javax.swing.JPanel;
27 import javax.swing.ListModel;
28 import javax.swing.text.Document;
29
30 import com.eviware.soapui.SoapUI;
31 import com.eviware.soapui.impl.rest.RestRequestInterface;
32 import com.eviware.soapui.impl.support.components.ModelItemXmlEditor;
33 import com.eviware.soapui.impl.support.panels.AbstractHttpXmlRequestDesktopPanel;
34 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
35 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
36 import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestInterface;
37 import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStepInterface;
38 import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestInterface;
39 import com.eviware.soapui.impl.wsdl.teststeps.actions.AddAssertionAction;
40 import com.eviware.soapui.model.ModelItem;
41 import com.eviware.soapui.model.iface.Submit;
42 import com.eviware.soapui.model.iface.SubmitContext;
43 import com.eviware.soapui.model.iface.Request.SubmitException;
44 import com.eviware.soapui.model.support.ModelSupport;
45 import com.eviware.soapui.model.testsuite.AssertionError;
46 import com.eviware.soapui.model.testsuite.AssertionsListener;
47 import com.eviware.soapui.model.testsuite.LoadTestRunner;
48 import com.eviware.soapui.model.testsuite.TestAssertion;
49 import com.eviware.soapui.model.testsuite.TestCaseRunner;
50 import com.eviware.soapui.model.testsuite.Assertable.AssertionStatus;
51 import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
52 import com.eviware.soapui.support.DocumentListenerAdapter;
53 import com.eviware.soapui.support.ListDataChangeListener;
54 import com.eviware.soapui.support.StringUtils;
55 import com.eviware.soapui.support.UISupport;
56 import com.eviware.soapui.support.components.JComponentInspector;
57 import com.eviware.soapui.support.components.JInspectorPanel;
58 import com.eviware.soapui.support.components.JInspectorPanelFactory;
59 import com.eviware.soapui.support.components.JUndoableTextField;
60 import com.eviware.soapui.support.components.JXToolBar;
61 import com.eviware.soapui.support.log.JLogList;
62
63 public class HttpTestRequestDesktopPanel extends
64 AbstractHttpXmlRequestDesktopPanel<HttpTestRequestStepInterface, HttpTestRequestInterface<?>>
65 {
66 private JLogList logArea;
67 private InternalTestMonitorListener testMonitorListener = new InternalTestMonitorListener();
68 private JButton addAssertionButton;
69 protected boolean updatingRequest;
70 private AssertionsPanel assertionsPanel;
71 private JInspectorPanel inspectorPanel;
72 private JComponentInspector<?> assertionInspector;
73 private JComponentInspector<?> logInspector;
74 private InternalAssertionsListener assertionsListener = new InternalAssertionsListener();
75 private long startTime;
76 private SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
77 private boolean updating;
78 private JUndoableTextField pathTextField;
79 private JComboBox methodCombo;
80
81 public HttpTestRequestDesktopPanel( HttpTestRequestStepInterface testStep )
82 {
83 super( testStep, testStep.getTestRequest() );
84
85 SoapUI.getTestMonitor().addTestMonitorListener( testMonitorListener );
86 setEnabled( !SoapUI.getTestMonitor().hasRunningTest( testStep.getTestCase() ) );
87
88 testStep.getTestRequest().addAssertionsListener( assertionsListener );
89
90 getSubmitButton().setEnabled( getSubmit() == null && StringUtils.hasContent( getRequest().getEndpoint() ) );
91 }
92
93 protected JComponent buildLogPanel()
94 {
95 logArea = new JLogList( "Request Log" );
96
97 logArea.getLogList().getModel().addListDataListener( new ListDataChangeListener()
98 {
99 public void dataChanged( ListModel model )
100 {
101 logInspector.setTitle( "Request Log (" + model.getSize() + ")" );
102 }
103 } );
104
105 return logArea;
106 }
107
108 protected AssertionsPanel buildAssertionsPanel()
109 {
110 return new AssertionsPanel( getRequest() )
111 {
112 protected void selectError( AssertionError error )
113 {
114 ModelItemXmlEditor<?, ?> editor = ( ModelItemXmlEditor<?, ?> )getResponseEditor();
115 editor.requestFocus();
116 }
117 };
118 }
119
120 public void setContent( JComponent content )
121 {
122 inspectorPanel.setContentComponent( content );
123 }
124
125 public void removeContent( JComponent content )
126 {
127 inspectorPanel.setContentComponent( null );
128 }
129
130 protected String getHelpUrl()
131 {
132 return HelpUrls.TESTREQUESTEDITOR_HELP_URL;
133 }
134
135 protected JComponent buildContent()
136 {
137 JComponent component = super.buildContent();
138
139 inspectorPanel = JInspectorPanelFactory.build( component );
140 assertionsPanel = buildAssertionsPanel();
141
142 assertionInspector = new JComponentInspector<JComponent>( assertionsPanel, "Assertions ("
143 + getModelItem().getAssertionCount() + ")", "Assertions for this Test Request", true );
144
145 inspectorPanel.addInspector( assertionInspector );
146
147 logInspector = new JComponentInspector<JComponent>( buildLogPanel(), "Request Log (0)", "Log of requests", true );
148 inspectorPanel.addInspector( logInspector );
149 inspectorPanel.setDefaultDividerLocation( 0.6F );
150 inspectorPanel.setCurrentInspector( "Assertions" );
151
152 updateStatusIcon();
153
154 getSubmitButton().setEnabled( getSubmit() == null && StringUtils.hasContent( getRequest().getEndpoint() ) );
155
156 return inspectorPanel.getComponent();
157 }
158
159 protected JComponent buildEndpointComponent()
160 {
161 return null;
162 }
163
164 private void updateStatusIcon()
165 {
166 AssertionStatus status = getModelItem().getTestRequest().getAssertionStatus();
167 switch( status )
168 {
169 case FAILED :
170 {
171 assertionInspector.setIcon( UISupport.createImageIcon( "/failed_assertion.gif" ) );
172 inspectorPanel.activate( assertionInspector );
173 break;
174 }
175 case UNKNOWN :
176 {
177 assertionInspector.setIcon( UISupport.createImageIcon( "/unknown_assertion.gif" ) );
178 break;
179 }
180 case VALID :
181 {
182 assertionInspector.setIcon( UISupport.createImageIcon( "/valid_assertion.gif" ) );
183 inspectorPanel.deactivate();
184 break;
185 }
186 }
187 }
188
189 protected void addMethodCombo( JXToolBar toolbar )
190 {
191 methodCombo = new JComboBox( RestRequestInterface.RequestMethod.getMethods() );
192
193 methodCombo.setSelectedItem( getRequest().getMethod() );
194 methodCombo.setToolTipText( "Set desired HTTP method" );
195 methodCombo.addItemListener( new ItemListener()
196 {
197 public void itemStateChanged( ItemEvent e )
198 {
199 updatingRequest = true;
200 getRequest().setMethod( ( RestRequestInterface.RequestMethod )methodCombo.getSelectedItem() );
201 updatingRequest = false;
202 }
203 } );
204
205 toolbar.addLabeledFixed( "Method", methodCombo );
206 toolbar.addSeparator();
207 }
208
209 protected void addToolbarComponents( JXToolBar toolbar )
210 {
211 toolbar.addSeparator();
212 addMethodCombo( toolbar );
213
214 pathTextField = new JUndoableTextField();
215 pathTextField.setPreferredSize( new Dimension( 300, 20 ) );
216 pathTextField.setText( getRequest().getEndpoint() );
217 pathTextField.setToolTipText( pathTextField.getText() );
218 pathTextField.getDocument().addDocumentListener( new DocumentListenerAdapter()
219 {
220 @Override
221 public void update( Document document )
222 {
223 if( updating )
224 return;
225
226 updating = true;
227 getRequest().setEndpoint( pathTextField.getText() );
228 updating = false;
229 }
230 } );
231
232 toolbar.addLabeledFixed( "Request URL:", pathTextField );
233
234 toolbar.addSeparator();
235 }
236
237 @Override
238 protected JComponent buildToolbar()
239 {
240 addAssertionButton = createActionButton( new AddAssertionAction( getRequest() ), true );
241
242 JPanel panel = new JPanel( new BorderLayout() );
243 panel.add( super.buildToolbar(), BorderLayout.NORTH );
244
245 JXToolBar toolbar = UISupport.createToolbar();
246 addToolbarComponents( toolbar );
247
248 panel.add( toolbar, BorderLayout.SOUTH );
249 return panel;
250
251 }
252
253 protected void insertButtons( JXToolBar toolbar )
254 {
255 toolbar.add( addAssertionButton );
256 }
257
258 public void setEnabled( boolean enabled )
259 {
260 if( enabled == true )
261 enabled = !SoapUI.getTestMonitor().hasRunningLoadTest( getModelItem().getTestCase() );
262
263 super.setEnabled( enabled );
264 addAssertionButton.setEnabled( enabled );
265 assertionsPanel.setEnabled( enabled );
266
267 if( SoapUI.getTestMonitor().hasRunningLoadTest( getRequest().getTestCase() ) )
268 {
269 getRequest().removeSubmitListener( this );
270 }
271 else
272 {
273 getRequest().addSubmitListener( this );
274 }
275 }
276
277 protected Submit doSubmit() throws SubmitException
278 {
279 return getRequest().submit( new WsdlTestRunContext( getModelItem() ), true );
280 }
281
282 private final class InternalAssertionsListener implements AssertionsListener
283 {
284 public void assertionAdded( TestAssertion assertion )
285 {
286 assertionInspector.setTitle( "Assertions (" + getModelItem().getAssertionCount() + ")" );
287 }
288
289 public void assertionRemoved( TestAssertion assertion )
290 {
291 assertionInspector.setTitle( "Assertions (" + getModelItem().getAssertionCount() + ")" );
292 }
293
294 public void assertionMoved( TestAssertion assertion, int ix, int offset )
295 {
296 assertionInspector.setTitle( "Assertions (" + getModelItem().getAssertionCount() + ")" );
297 }
298 }
299
300 public boolean beforeSubmit( Submit submit, SubmitContext context )
301 {
302 boolean result = super.beforeSubmit( submit, context );
303 startTime = System.currentTimeMillis();
304 return result;
305 }
306
307 protected void logMessages( String message, String infoMessage )
308 {
309 super.logMessages( message, infoMessage );
310 logArea.addLine( sdf.format( new Date( startTime ) ) + " - " + message );
311 }
312
313 @Override
314 public void afterSubmit( Submit submit, SubmitContext context )
315 {
316 super.afterSubmit( submit, context );
317 if( !isHasClosed() )
318 updateStatusIcon();
319 }
320
321 public boolean onClose( boolean canCancel )
322 {
323 if( super.onClose( canCancel ) )
324 {
325 assertionsPanel.release();
326 inspectorPanel.release();
327 SoapUI.getTestMonitor().removeTestMonitorListener( testMonitorListener );
328 logArea.release();
329 getModelItem().getTestRequest().removeAssertionsListener( assertionsListener );
330 return true;
331 }
332
333 return false;
334 }
335
336 public boolean dependsOn( ModelItem modelItem )
337 {
338 if( getRequest().getOperation() == null )
339 return modelItem == getRequest() || modelItem == getModelItem()
340 || ModelSupport.getModelItemProject( getRequest() ) == modelItem
341 || modelItem == getModelItem().getTestCase() || modelItem == getModelItem().getTestCase().getTestSuite();
342 else
343 return modelItem == getRequest() || modelItem == getModelItem() || modelItem == getRequest().getOperation()
344 || modelItem == getRequest().getOperation().getInterface()
345 || modelItem == getRequest().getOperation().getInterface().getProject()
346 || modelItem == getModelItem().getTestCase() || modelItem == getModelItem().getTestCase().getTestSuite();
347 }
348
349 private class InternalTestMonitorListener extends TestMonitorListenerAdapter
350 {
351 public void loadTestFinished( LoadTestRunner runner )
352 {
353 setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
354 }
355
356 public void loadTestStarted( LoadTestRunner runner )
357 {
358 if( runner.getLoadTest().getTestCase() == getModelItem().getTestCase() )
359 setEnabled( false );
360 }
361
362 public void testCaseFinished( TestCaseRunner runner )
363 {
364 setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
365 }
366
367 public void testCaseStarted( TestCaseRunner runner )
368 {
369 if( runner.getTestCase() == getModelItem().getTestCase() )
370 setEnabled( false );
371 }
372 }
373
374 public void propertyChange( PropertyChangeEvent evt )
375 {
376 if( evt.getPropertyName().equals( RestTestRequestInterface.STATUS_PROPERTY ) )
377 {
378 updateStatusIcon();
379 }
380 else if( evt.getPropertyName().equals( "path" ) )
381 {
382 getSubmitButton().setEnabled( getSubmit() == null && StringUtils.hasContent( getRequest().getEndpoint() ) );
383 }
384
385 super.propertyChange( evt );
386 }
387
388 }