1
2
3
4
5 package com.eviware.soapui.impl.wsdl.panels.teststeps;
6
7 import com.eviware.soapui.SoapUI;
8 import com.eviware.soapui.impl.support.components.ModelItemXmlEditor;
9 import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
10 import com.eviware.soapui.impl.wsdl.mock.WsdlMockResult;
11 import com.eviware.soapui.impl.wsdl.panels.mockoperation.AbstractWsdlMockResponseDesktopPanel;
12 import com.eviware.soapui.impl.wsdl.teststeps.WsdlMockResponseTestStep;
13 import com.eviware.soapui.model.ModelItem;
14 import com.eviware.soapui.model.support.TestRunListenerAdapter;
15 import com.eviware.soapui.model.testsuite.*;
16 import com.eviware.soapui.model.testsuite.AssertionError;
17 import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
18 import com.eviware.soapui.support.DocumentListenerAdapter;
19 import com.eviware.soapui.support.ModelItemPropertyEditorModel;
20 import com.eviware.soapui.support.UISupport;
21 import com.eviware.soapui.support.components.JComponentInspector;
22 import com.eviware.soapui.support.components.JInspectorPanel;
23 import com.eviware.soapui.support.components.JInspectorPanelFactory;
24 import com.eviware.soapui.support.components.JXToolBar;
25 import com.eviware.soapui.support.xml.XmlUtils;
26
27 import javax.swing.*;
28 import javax.swing.text.Document;
29 import java.awt.*;
30 import java.awt.event.ActionEvent;
31 import java.beans.PropertyChangeEvent;
32 import java.util.Date;
33
34 public class WsdlMockResponseStepDesktopPanel extends AbstractWsdlMockResponseDesktopPanel<WsdlMockResponseTestStep, WsdlMockResponse>
35 {
36 private JTextArea logArea;
37 private AssertionsPanel assertionsPanel;
38 private JTextField portField;
39 private JTextField pathField;
40 private InternalTestRunListener testRunListener;
41 private InternalTestMonitorListener testMonitorListener = new InternalTestMonitorListener();
42 private JInspectorPanel inspectorPanel;
43 private JComponentInspector<JComponent> assertionInspector;
44 private JComponentInspector<JComponent> logInspector;
45 private ModelItemPropertyEditorModel<WsdlMockResponseTestStep> queryEditorModel;
46 private ModelItemPropertyEditorModel<WsdlMockResponseTestStep> matchEditorModel;
47
48 public WsdlMockResponseStepDesktopPanel( WsdlMockResponseTestStep mockResponseStep )
49 {
50 super( mockResponseStep );
51 init( mockResponseStep.getMockResponse() );
52
53 testRunListener = new InternalTestRunListener();
54 mockResponseStep.getTestCase().addTestRunListener( testRunListener );
55
56 SoapUI.getTestMonitor().addTestMonitorListener( testMonitorListener );
57 setEnabled( !SoapUI.getTestMonitor().hasRunningTest( mockResponseStep.getTestCase() ) );
58 }
59
60 @Override
61 protected JComponent buildContent()
62 {
63 inspectorPanel = JInspectorPanelFactory.build( super.buildContent() );
64
65 assertionsPanel = buildAssertionsPanel();
66
67 assertionInspector = new JComponentInspector<JComponent>( assertionsPanel, "Assertions ("
68 + getModelItem().getAssertionCount() + ")", "Assertions for this Test Request", true );
69
70 inspectorPanel.addInspector( assertionInspector );
71
72 logInspector = new JComponentInspector<JComponent>( buildLogPanel(), "Request Log (0)", "Log of requests", true );
73 inspectorPanel.addInspector( logInspector );
74
75 inspectorPanel.addInspector( new JComponentInspector<JComponent>( buildQueryMatchPanel(), "Query/Match",
76 "Query/Match configuration", true ) );
77
78 inspectorPanel.setDefaultDividerLocation( 0.6F );
79 inspectorPanel.setCurrentInspector( "Assertions" );
80
81 updateStatusIcon();
82
83 return inspectorPanel.getComponent();
84 }
85
86 private void updateStatusIcon()
87 {
88 Assertable.AssertionStatus status = getModelItem().getAssertionStatus();
89 switch( status )
90 {
91 case FAILED:
92 {
93 assertionInspector.setIcon( UISupport.createImageIcon( "/failed_assertion.gif" ) );
94 inspectorPanel.activate( assertionInspector );
95 break;
96 }
97 case UNKNOWN:
98 {
99 assertionInspector.setIcon( UISupport.createImageIcon( "/unknown_assertion.gif" ) );
100 break;
101 }
102 case VALID:
103 {
104 assertionInspector.setIcon( UISupport.createImageIcon( "/valid_assertion.gif" ) );
105 inspectorPanel.deactivate();
106 break;
107 }
108 }
109 }
110
111 private JComponent buildLogPanel()
112 {
113 logArea = new JTextArea();
114 logArea.setEditable( false );
115 logArea.setToolTipText( "Response Log" );
116
117 JPanel panel = new JPanel( new BorderLayout() );
118 panel.add( new JScrollPane( logArea ), BorderLayout.CENTER );
119
120 return panel;
121 }
122
123 public void setContent( JComponent content )
124 {
125 inspectorPanel.setContentComponent( content );
126 }
127
128 public void removeContent( JComponent content )
129 {
130 inspectorPanel.setContentComponent( null );
131 }
132
133 @Override
134 protected void createToolbar( JXToolBar toolbar )
135 {
136 toolbar.addUnrelatedGap();
137 toolbar.addFixed( new JLabel( "Path" ) );
138 toolbar.addRelatedGap();
139 pathField = new JTextField( getModelItem().getPath(), 15 );
140 pathField.getDocument().addDocumentListener( new DocumentListenerAdapter()
141 {
142
143 @Override
144 public void update( Document document )
145 {
146 getModelItem().setPath( pathField.getText() );
147 }
148 } );
149
150 toolbar.addFixed( pathField );
151
152 toolbar.addUnrelatedGap();
153 toolbar.addFixed( new JLabel( "Port" ) );
154 toolbar.addRelatedGap();
155 portField = new JTextField( String.valueOf( getModelItem().getPort() ), 5 );
156 portField.getDocument().addDocumentListener( new DocumentListenerAdapter()
157 {
158
159 @Override
160 public void update( Document document )
161 {
162 try
163 {
164 getModelItem().setPort( Integer.parseInt( portField.getText() ) );
165 }
166 catch( NumberFormatException e )
167 {
168 }
169 }
170 } );
171
172 toolbar.addFixed( portField );
173 }
174
175 private JComponent buildQueryMatchPanel()
176 {
177 JPanel panel = new JPanel( new BorderLayout() );
178 panel.add( buildQueryMatchToolbar(), BorderLayout.NORTH );
179 JSplitPane splitPane = UISupport.createHorizontalSplit( buildQueryEditor(), buildMatchEditor() );
180 panel.add( splitPane, BorderLayout.CENTER );
181 splitPane.setDividerLocation( 0.5f );
182 return panel;
183 }
184
185 private Component buildMatchEditor()
186 {
187 JPanel panel = new JPanel( new BorderLayout() );
188
189 matchEditorModel = new ModelItemPropertyEditorModel<WsdlMockResponseTestStep>( getModelItem(), "match" );
190 panel.add( UISupport.getEditorFactory().buildXmlEditor( matchEditorModel ), BorderLayout.CENTER );
191
192 UISupport.addTitledBorder( panel, "Matching Value" );
193
194 return panel;
195 }
196
197 private Component buildQueryEditor()
198 {
199 JPanel panel = new JPanel( new BorderLayout() );
200
201 queryEditorModel = new ModelItemPropertyEditorModel<WsdlMockResponseTestStep>( getModelItem(), "query" );
202 panel.add( UISupport.getEditorFactory().buildXPathEditor( queryEditorModel ), BorderLayout.CENTER );
203
204 UISupport.addTitledBorder( panel, "XPath Query" );
205
206 return panel;
207 }
208
209 private Component buildQueryMatchToolbar()
210 {
211 JXToolBar toolBar = UISupport.createSmallToolbar();
212 return toolBar;
213 }
214
215 private AssertionsPanel buildAssertionsPanel()
216 {
217 assertionsPanel = new AssertionsPanel( getModelItem() )
218 {
219 protected void selectError( AssertionError error )
220 {
221 ModelItemXmlEditor<?, ?> editor = getResponseEditor();
222 editor.requestFocus();
223 }
224 };
225
226 return assertionsPanel;
227 }
228
229 @Override
230 public boolean onClose( boolean canCancel )
231 {
232 getModelItem().getTestCase().removeTestRunListener( testRunListener );
233 SoapUI.getTestMonitor().removeTestMonitorListener( testMonitorListener );
234 assertionsPanel.release();
235
236 queryEditorModel.release();
237 matchEditorModel.release();
238
239 return super.onClose( canCancel );
240 }
241
242 public void setEnabled( boolean enabled )
243 {
244 super.setEnabled( enabled );
245
246 pathField.setEnabled( enabled );
247 portField.setEnabled( enabled );
248 }
249
250 public boolean dependsOn( ModelItem modelItem )
251 {
252 return modelItem == getModelItem() || modelItem == getModelItem().getTestCase()
253 || modelItem == getModelItem().getOperation() || modelItem == getModelItem().getOperation().getInterface()
254 || modelItem == getModelItem().getTestCase().getTestSuite()
255 || modelItem == getModelItem().getTestCase().getTestSuite().getProject();
256 }
257
258 public class InternalTestRunListener extends TestRunListenerAdapter
259 {
260 @Override
261 public void afterRun( TestRunner testRunner, TestRunContext runContext )
262 {
263 setEnabled( true );
264 }
265
266 @Override
267 public void beforeRun( TestRunner testRunner, TestRunContext runContext )
268 {
269 setEnabled( false );
270 }
271
272 @Override
273 public void beforeStep( TestRunner testRunner, TestRunContext runContext )
274 {
275 if( runContext.getCurrentStep() == getModelItem() )
276 {
277 logArea.setText( logArea.getText() + new Date( System.currentTimeMillis() ).toString() +
278 ": Waiting for request on http://127.0.0.1:" + getModelItem().getPort() + getModelItem().getPath() + "\r\n" );
279 }
280 }
281
282 @Override
283 public void afterStep( TestRunner testRunner, TestRunContext runContext, TestStepResult result )
284 {
285 if( result.getTestStep() == getModelItem() )
286 {
287 String msg = new Date( result.getTimeStamp() ).toString() + ": Handled request in " + result.getTimeTaken() + "ms";
288 logArea.setText( logArea.getText() + msg + "\r\n" );
289 }
290 }
291 }
292
293 private class InternalTestMonitorListener extends TestMonitorListenerAdapter
294 {
295 public void loadTestFinished( LoadTestRunner runner )
296 {
297 setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
298 }
299
300 public void loadTestStarted( LoadTestRunner runner )
301 {
302 if( runner.getLoadTest().getTestCase() == getModelItem().getTestCase() )
303 setEnabled( false );
304 }
305
306 public void testCaseFinished( TestRunner runner )
307 {
308 setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
309 }
310
311 public void testCaseStarted( TestRunner runner )
312 {
313 if( runner.getTestCase() == getModelItem().getTestCase() )
314 setEnabled( false );
315 }
316 }
317
318 public void propertyChange( PropertyChangeEvent evt )
319 {
320 super.propertyChange( evt );
321
322 if( evt.getPropertyName().equals( WsdlMockResponseTestStep.STATUS_PROPERTY ) )
323 updateStatusIcon();
324 }
325
326 private final class DeclareNamespacesAction extends AbstractAction
327 {
328 public DeclareNamespacesAction()
329 {
330 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/declareNs.gif" ) );
331 putValue( Action.SHORT_DESCRIPTION, "Declare available response/request namespaces in source/target expressions" );
332 }
333
334 public void actionPerformed( ActionEvent e )
335 {
336 try
337 {
338 WsdlMockResult lastResult = getMockResponse().getMockResult();
339 String content = null;
340 if( lastResult == null )
341 {
342 if( !UISupport.confirm( "Missing last result, declare from default request instead?", "Declare Namespaces" ) )
343 {
344 return;
345 }
346
347 content = getMockResponse().getMockOperation().getOperation().createRequest( true );
348 }
349 else
350 {
351 content = lastResult.getMockRequest().getRequestContent();
352 }
353
354
355 String path = getModelItem().getQuery();
356 if( path == null )
357 path = "";
358
359 getModelItem().setQuery( XmlUtils.declareXPathNamespaces( content ) + path );
360 }
361 catch( Exception e1 )
362 {
363 UISupport.showErrorMessage( e1 );
364 }
365 }
366 }
367
368 }