View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.panels.teststeps;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.rest.panels.request.AbstractRestRequestDesktopPanel;
17  import com.eviware.soapui.impl.support.components.ModelItemXmlEditor;
18  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
19  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
20  import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep;
21  import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequest;
22  import com.eviware.soapui.impl.wsdl.teststeps.actions.AddAssertionAction;
23  import com.eviware.soapui.model.ModelItem;
24  import com.eviware.soapui.model.iface.Request.SubmitException;
25  import com.eviware.soapui.model.iface.Submit;
26  import com.eviware.soapui.model.iface.SubmitContext;
27  import com.eviware.soapui.model.support.ModelSupport;
28  import com.eviware.soapui.model.testsuite.Assertable.AssertionStatus;
29  import com.eviware.soapui.model.testsuite.AssertionError;
30  import com.eviware.soapui.model.testsuite.*;
31  import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
32  import com.eviware.soapui.support.ListDataChangeListener;
33  import com.eviware.soapui.support.StringUtils;
34  import com.eviware.soapui.support.UISupport;
35  import com.eviware.soapui.support.components.JComponentInspector;
36  import com.eviware.soapui.support.components.JInspectorPanel;
37  import com.eviware.soapui.support.components.JInspectorPanelFactory;
38  import com.eviware.soapui.support.components.JXToolBar;
39  import com.eviware.soapui.support.log.JLogList;
40  
41  import javax.swing.*;
42  import java.beans.PropertyChangeEvent;
43  import java.text.SimpleDateFormat;
44  import java.util.Date;
45  
46  public class HttpTestRequestDesktopPanel extends AbstractRestRequestDesktopPanel<HttpTestRequestStep, RestTestRequest>
47  {
48     private JLogList logArea;
49     private InternalTestMonitorListener testMonitorListener = new InternalTestMonitorListener();
50     private JButton addAssertionButton;
51     protected boolean updatingRequest;
52     private AssertionsPanel assertionsPanel;
53     private JInspectorPanel inspectorPanel;
54     private JComponentInspector<?> assertionInspector;
55     private JComponentInspector<?> logInspector;
56     private InternalAssertionsListener assertionsListener = new InternalAssertionsListener();
57     private long startTime;
58     private SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
59  
60     public HttpTestRequestDesktopPanel( HttpTestRequestStep requestStep )
61     {
62        super( requestStep, requestStep.getTestRequest() );
63  
64        SoapUI.getTestMonitor().addTestMonitorListener( testMonitorListener );
65        setEnabled( !SoapUI.getTestMonitor().hasRunningTest( requestStep.getTestCase() ) );
66  
67        requestStep.getTestRequest().addAssertionsListener( assertionsListener );
68  
69        getSubmitButton().setEnabled( getSubmit() == null && StringUtils.hasContent( getRequest().getPath() ) );
70     }
71  
72     protected JComponent buildLogPanel()
73     {
74        logArea = new JLogList( "Request Log" );
75  
76        logArea.getLogList().getModel().addListDataListener( new ListDataChangeListener()
77        {
78           public void dataChanged( ListModel model )
79           {
80              logInspector.setTitle( "Request Log (" + model.getSize() + ")" );
81           }
82        } );
83  
84        return logArea;
85     }
86  
87     protected AssertionsPanel buildAssertionsPanel()
88     {
89        return new AssertionsPanel( getRequest() )
90        {
91           protected void selectError( AssertionError error )
92           {
93              ModelItemXmlEditor<?, ?> editor = (ModelItemXmlEditor<?, ?>) getResponseEditor();
94              editor.requestFocus();
95           }
96        };
97     }
98  
99     public void setContent( JComponent content )
100    {
101       inspectorPanel.setContentComponent( content );
102    }
103 
104    public void removeContent( JComponent content )
105    {
106       inspectorPanel.setContentComponent( null );
107    }
108 
109    protected String getHelpUrl()
110    {
111       return HelpUrls.TESTREQUESTEDITOR_HELP_URL;
112    }
113 
114    protected JComponent buildContent()
115    {
116       JComponent component = super.buildContent();
117 
118       inspectorPanel = JInspectorPanelFactory.build( component );
119       assertionsPanel = buildAssertionsPanel();
120 
121       assertionInspector = new JComponentInspector<JComponent>( assertionsPanel, "Assertions ("
122               + getModelItem().getAssertionCount() + ")", "Assertions for this Test Request", true );
123 
124       inspectorPanel.addInspector( assertionInspector );
125 
126       logInspector = new JComponentInspector<JComponent>( buildLogPanel(), "Request Log (0)", "Log of requests", true );
127       inspectorPanel.addInspector( logInspector );
128       inspectorPanel.setDefaultDividerLocation( 0.6F );
129       inspectorPanel.setCurrentInspector( "Assertions" );
130 
131       updateStatusIcon();
132 
133       getSubmitButton().setEnabled( getSubmit() == null && StringUtils.hasContent( getRequest().getPath() ) );
134 
135       return inspectorPanel.getComponent();
136    }
137 
138    private void updateStatusIcon()
139    {
140       AssertionStatus status = getModelItem().getTestRequest().getAssertionStatus();
141       switch( status )
142       {
143          case FAILED:
144          {
145             assertionInspector.setIcon( UISupport.createImageIcon( "/failed_assertion.gif" ) );
146             inspectorPanel.activate( assertionInspector );
147             break;
148          }
149          case UNKNOWN:
150          {
151             assertionInspector.setIcon( UISupport.createImageIcon( "/unknown_assertion.gif" ) );
152             break;
153          }
154          case VALID:
155          {
156             assertionInspector.setIcon( UISupport.createImageIcon( "/valid_assertion.gif" ) );
157             inspectorPanel.deactivate();
158             break;
159          }
160       }
161    }
162 
163    protected JComponent buildToolbar()
164    {
165       addAssertionButton = createActionButton( new AddAssertionAction( getRequest() ), true );
166       return super.buildToolbar();
167    }
168 
169    protected void insertButtons( JXToolBar toolbar )
170    {
171       toolbar.add( addAssertionButton );
172 
173       super.insertButtons( toolbar );
174    }
175 
176    public void setEnabled( boolean enabled )
177    {
178       if( enabled == true )
179          enabled = !SoapUI.getTestMonitor().hasRunningLoadTest( getModelItem().getTestCase() );
180 
181       super.setEnabled( enabled );
182       addAssertionButton.setEnabled( enabled );
183       assertionsPanel.setEnabled( enabled );
184 
185       if( SoapUI.getTestMonitor().hasRunningLoadTest( getRequest().getTestCase() ) )
186       {
187          getRequest().removeSubmitListener( this );
188       }
189       else
190       {
191          getRequest().addSubmitListener( this );
192       }
193    }
194 
195    protected Submit doSubmit() throws SubmitException
196    {
197       return getRequest().submit( new WsdlTestRunContext( getModelItem() ), true );
198    }
199 
200    private final class InternalAssertionsListener implements AssertionsListener
201    {
202       public void assertionAdded( TestAssertion assertion )
203       {
204          assertionInspector.setTitle( "Assertions (" + getModelItem().getAssertionCount() + ")" );
205       }
206 
207       public void assertionRemoved( TestAssertion assertion )
208       {
209          assertionInspector.setTitle( "Assertions (" + getModelItem().getAssertionCount() + ")" );
210       }
211    }
212 
213    public boolean beforeSubmit( Submit submit, SubmitContext context )
214    {
215       boolean result = super.beforeSubmit( submit, context );
216       startTime = System.currentTimeMillis();
217       return result;
218    }
219 
220    protected void logMessages( String message, String infoMessage )
221    {
222       super.logMessages( message, infoMessage );
223       logArea.addLine( sdf.format( new Date( startTime ) ) + " - " + message );
224    }
225 
226    @Override
227    public void afterSubmit( Submit submit, SubmitContext context )
228    {
229       super.afterSubmit( submit, context );
230       if( !isHasClosed() )
231         updateStatusIcon();
232    }
233 
234    public boolean onClose( boolean canCancel )
235    {
236       if( super.onClose( canCancel ) )
237       {
238          assertionsPanel.release();
239          SoapUI.getTestMonitor().removeTestMonitorListener( testMonitorListener );
240          logArea.release();
241          getModelItem().getTestRequest().removeAssertionsListener( assertionsListener );
242          return true;
243       }
244 
245       return false;
246    }
247 
248    public boolean dependsOn( ModelItem modelItem )
249    {
250       if( getRequest().getOperation() == null )
251          return  modelItem == getRequest() || modelItem == getModelItem()
252                || ModelSupport.getModelItemProject( getRequest() ) == modelItem 
253                || modelItem == getModelItem().getTestCase() || modelItem == getModelItem().getTestCase().getTestSuite();
254       else
255          return modelItem == getRequest() || modelItem == getModelItem() || modelItem == getRequest().getOperation()
256               || modelItem == getRequest().getOperation().getInterface()
257               || modelItem == getRequest().getOperation().getInterface().getProject()
258               || modelItem == getModelItem().getTestCase() || modelItem == getModelItem().getTestCase().getTestSuite();
259    }
260 
261    private class InternalTestMonitorListener extends TestMonitorListenerAdapter
262    {
263       public void loadTestFinished( LoadTestRunner runner )
264       {
265          setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
266       }
267 
268       public void loadTestStarted( LoadTestRunner runner )
269       {
270          if( runner.getLoadTest().getTestCase() == getModelItem().getTestCase() )
271             setEnabled( false );
272       }
273 
274       public void testCaseFinished( TestRunner runner )
275       {
276          setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
277       }
278 
279       public void testCaseStarted( TestRunner runner )
280       {
281          if( runner.getTestCase() == getModelItem().getTestCase() )
282             setEnabled( false );
283       }
284    }
285 
286    public void propertyChange( PropertyChangeEvent evt )
287    {
288       if( evt.getPropertyName().equals( RestTestRequest.STATUS_PROPERTY ) )
289       {
290          updateStatusIcon();
291       }
292       else if( evt.getPropertyName().equals( "path" ) )
293       {
294          getSubmitButton().setEnabled( getSubmit() == null && StringUtils.hasContent( getRequest().getPath() ) );
295       }
296 
297       super.propertyChange( evt );
298    }
299 }