View Javadoc

1   /*
2    *  soapUI Pro, copyright (C) 2007-2008 eviware software ab 
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       return panel;
193    }
194 
195    private Component buildQueryEditor()
196    {
197       JPanel panel = new JPanel( new BorderLayout() );
198 
199       queryEditorModel = new ModelItemPropertyEditorModel<WsdlMockResponseTestStep>( getModelItem(), "query" );
200       panel.add( UISupport.getEditorFactory().buildXPathEditor( queryEditorModel ), BorderLayout.CENTER );
201 
202       return panel;
203    }
204 
205    private Component buildQueryMatchToolbar()
206    {
207       JXToolBar toolBar = UISupport.createSmallToolbar();
208       return toolBar;
209    }
210 
211    private AssertionsPanel buildAssertionsPanel()
212    {
213       assertionsPanel = new AssertionsPanel( getModelItem() )
214       {
215          protected void selectError( AssertionError error )
216          {
217             ModelItemXmlEditor<?, ?> editor = getResponseEditor();
218             editor.requestFocus();
219          }
220       };
221 
222       return assertionsPanel;
223    }
224 
225    @Override
226    public boolean onClose( boolean canCancel )
227    {
228       getModelItem().getTestCase().removeTestRunListener( testRunListener );
229       SoapUI.getTestMonitor().removeTestMonitorListener( testMonitorListener );
230       assertionsPanel.release();
231 
232       queryEditorModel.release();
233       matchEditorModel.release();
234 
235       return super.onClose( canCancel );
236    }
237 
238    public void setEnabled( boolean enabled )
239    {
240       super.setEnabled( enabled );
241 
242       pathField.setEnabled( enabled );
243       portField.setEnabled( enabled );
244    }
245 
246    public boolean dependsOn( ModelItem modelItem )
247    {
248       return modelItem == getModelItem() || modelItem == getModelItem().getTestCase()
249               || modelItem == getModelItem().getOperation() || modelItem == getModelItem().getOperation().getInterface()
250               || modelItem == getModelItem().getTestCase().getTestSuite()
251               || modelItem == getModelItem().getTestCase().getTestSuite().getProject();
252    }
253 
254    public class InternalTestRunListener extends TestRunListenerAdapter
255    {
256       @Override
257       public void afterRun( TestRunner testRunner, TestRunContext runContext )
258       {
259          setEnabled( true );
260       }
261 
262       @Override
263       public void beforeRun( TestRunner testRunner, TestRunContext runContext )
264       {
265          setEnabled( false );
266       }
267 
268       @Override
269       public void beforeStep( TestRunner testRunner, TestRunContext runContext )
270       {
271          if( runContext.getCurrentStep() == getModelItem() )
272          {
273             logArea.setText( logArea.getText() + new Date( System.currentTimeMillis() ).toString() +
274                     ": Waiting for request on http://127.0.0.1:" + getModelItem().getPort() + getModelItem().getPath() + "\r\n" );
275          }
276       }
277 
278       @Override
279       public void afterStep( TestRunner testRunner, TestRunContext runContext, TestStepResult result )
280       {
281          if( result.getTestStep() == getModelItem() )
282          {
283             String msg = new Date( result.getTimeStamp() ).toString() + ": Handled request in " + result.getTimeTaken() + "ms";
284             logArea.setText( logArea.getText() + msg + "\r\n" );
285          }
286       }
287    }
288 
289    private class InternalTestMonitorListener extends TestMonitorListenerAdapter
290    {
291       public void loadTestFinished( LoadTestRunner runner )
292       {
293          setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
294       }
295 
296       public void loadTestStarted( LoadTestRunner runner )
297       {
298          if( runner.getLoadTest().getTestCase() == getModelItem().getTestCase() )
299             setEnabled( false );
300       }
301 
302       public void testCaseFinished( TestRunner runner )
303       {
304          setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
305       }
306 
307       public void testCaseStarted( TestRunner runner )
308       {
309          if( runner.getTestCase() == getModelItem().getTestCase() )
310             setEnabled( false );
311       }
312    }
313 
314    public void propertyChange( PropertyChangeEvent evt )
315    {
316       super.propertyChange( evt );
317 
318       if( evt.getPropertyName().equals( WsdlMockResponseTestStep.STATUS_PROPERTY ) )
319          updateStatusIcon();
320    }
321 
322    private final class DeclareNamespacesAction extends AbstractAction
323    {
324       public DeclareNamespacesAction()
325       {
326          putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/declareNs.gif" ) );
327          putValue( Action.SHORT_DESCRIPTION, "Declare available response/request namespaces in source/target expressions" );
328       }
329 
330       public void actionPerformed( ActionEvent e )
331       {
332          try
333          {
334             WsdlMockResult lastResult = getMockResponse().getMockResult();
335             String content = null;
336             if( lastResult == null )
337             {
338                if( !UISupport.confirm( "Missing last result, declare from default request instead?", "Declare Namespaces" ) )
339                {
340                   return;
341                }
342 
343                content = getMockResponse().getMockOperation().getOperation().createRequest( true );
344             }
345             else
346             {
347                content = lastResult.getMockRequest().getRequestContent();
348             }
349 
350 
351             String path = getModelItem().getQuery();
352             if( path == null )
353                path = "";
354 
355             getModelItem().setQuery( XmlUtils.declareXPathNamespaces( content ) + path );
356          }
357          catch( Exception e1 )
358          {
359             UISupport.showErrorMessage( e1 );
360          }
361       }
362    }
363 
364 }