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.support.components.ModelItemXmlEditor;
17  import com.eviware.soapui.impl.wsdl.panels.request.AbstractWsdlRequestDesktopPanel;
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.WsdlTestRequest;
21  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
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.testsuite.Assertable.AssertionStatus;
28  import com.eviware.soapui.model.testsuite.AssertionError;
29  import com.eviware.soapui.model.testsuite.*;
30  import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
31  import com.eviware.soapui.support.ListDataChangeListener;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.components.JComponentInspector;
34  import com.eviware.soapui.support.components.JInspectorPanel;
35  import com.eviware.soapui.support.components.JInspectorPanelFactory;
36  import com.eviware.soapui.support.components.JXToolBar;
37  import com.eviware.soapui.support.log.JLogList;
38  
39  import javax.swing.*;
40  import java.beans.PropertyChangeEvent;
41  import java.text.SimpleDateFormat;
42  import java.util.Date;
43  
44  /***
45   * DesktopPanel for WsdlTestRequest. Essentially a copy of
46   * WsdlRequestDesktopPanel with assertions.
47   *
48   * @author Ole.Matzura
49   */
50  
51  public class WsdlTestRequestDesktopPanel extends AbstractWsdlRequestDesktopPanel<WsdlTestRequestStep, WsdlTestRequest>
52  {
53     private JLogList logArea;
54     private InternalTestMonitorListener testMonitorListener = new InternalTestMonitorListener();
55     private JButton addAssertionButton;
56     protected boolean updatingRequest;
57     private AssertionsPanel assertionsPanel;
58     private JInspectorPanel inspectorPanel;
59     private JComponentInspector<?> assertionInspector;
60     private JComponentInspector<?> logInspector;
61     private InternalAssertionsListener assertionsListener = new InternalAssertionsListener();
62     private long startTime;
63     private SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
64  
65     public WsdlTestRequestDesktopPanel( WsdlTestRequestStep requestStep )
66     {
67        super( requestStep, requestStep.getTestRequest() );
68  
69        SoapUI.getTestMonitor().addTestMonitorListener( testMonitorListener );
70        setEnabled( !SoapUI.getTestMonitor().hasRunningTest( requestStep.getTestCase() ) );
71  
72        requestStep.getTestRequest().addAssertionsListener( assertionsListener );
73     }
74  
75     protected JComponent buildLogPanel()
76     {
77        logArea = new JLogList( "Request Log" );
78  
79        logArea.getLogList().getModel().addListDataListener( new ListDataChangeListener()
80        {
81           public void dataChanged( ListModel model )
82           {
83              logInspector.setTitle( "Request Log (" + model.getSize() + ")" );
84           }
85        } );
86  
87        return logArea;
88     }
89  
90     protected AssertionsPanel buildAssertionsPanel()
91     {
92        return new AssertionsPanel( getRequest() )
93        {
94           protected void selectError( AssertionError error )
95           {
96              ModelItemXmlEditor<?, ?> editor = getResponseEditor();
97              editor.requestFocus();
98           }
99        };
100    }
101 
102    public void setContent( JComponent content )
103    {
104       inspectorPanel.setContentComponent( content );
105    }
106 
107    public void removeContent( JComponent content )
108    {
109       inspectorPanel.setContentComponent( null );
110    }
111 
112    protected String getHelpUrl()
113    {
114       return HelpUrls.TESTREQUESTEDITOR_HELP_URL;
115    }
116 
117    protected JComponent buildContent()
118    {
119       JComponent component = super.buildContent();
120 
121       inspectorPanel = JInspectorPanelFactory.build( component );
122       assertionsPanel = buildAssertionsPanel();
123 
124       assertionInspector = new JComponentInspector<JComponent>( assertionsPanel, "Assertions ("
125               + getModelItem().getAssertionCount() + ")", "Assertions for this Test Request", true );
126 
127       inspectorPanel.addInspector( assertionInspector );
128 
129       logInspector = new JComponentInspector<JComponent>( buildLogPanel(), "Request Log (0)", "Log of requests", true );
130       inspectorPanel.addInspector( logInspector );
131       inspectorPanel.setDefaultDividerLocation( 0.6F );
132       inspectorPanel.setCurrentInspector( "Assertions" );
133 
134       updateStatusIcon();
135 
136       return inspectorPanel.getComponent();
137    }
138 
139    private void updateStatusIcon()
140    {
141       AssertionStatus status = getModelItem().getTestRequest().getAssertionStatus();
142       switch( status )
143       {
144          case FAILED:
145          {
146             assertionInspector.setIcon( UISupport.createImageIcon( "/failed_assertion.gif" ) );
147             inspectorPanel.activate( assertionInspector );
148             break;
149          }
150          case UNKNOWN:
151          {
152             assertionInspector.setIcon( UISupport.createImageIcon( "/unknown_assertion.gif" ) );
153             break;
154          }
155          case VALID:
156          {
157             assertionInspector.setIcon( UISupport.createImageIcon( "/valid_assertion.gif" ) );
158             inspectorPanel.deactivate();
159             break;
160          }
161       }
162    }
163 
164    protected JComponent buildToolbar()
165    {
166       addAssertionButton = createActionButton( new AddAssertionAction( getRequest() ), true );
167       return super.buildToolbar();
168    }
169 
170    protected void insertButtons( JXToolBar toolbar )
171    {
172       toolbar.add( addAssertionButton );
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       return modelItem == getRequest() || modelItem == getModelItem() || modelItem == getRequest().getOperation()
251               || modelItem == getRequest().getOperation().getInterface()
252               || modelItem == getRequest().getOperation().getInterface().getProject()
253               || modelItem == getModelItem().getTestCase() || modelItem == getModelItem().getTestCase().getTestSuite();
254    }
255 
256    private class InternalTestMonitorListener extends TestMonitorListenerAdapter
257    {
258       public void loadTestFinished( LoadTestRunner runner )
259       {
260          setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
261       }
262 
263       public void loadTestStarted( LoadTestRunner runner )
264       {
265          if( runner.getLoadTest().getTestCase() == getModelItem().getTestCase() )
266             setEnabled( false );
267       }
268 
269       public void testCaseFinished( TestRunner runner )
270       {
271          setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
272       }
273 
274       public void testCaseStarted( TestRunner runner )
275       {
276          if( runner.getTestCase() == getModelItem().getTestCase() )
277             setEnabled( false );
278       }
279    }
280 
281    public void propertyChange( PropertyChangeEvent evt )
282    {
283       super.propertyChange( evt );
284 
285       if( evt.getPropertyName().equals( WsdlTestRequest.STATUS_PROPERTY ) )
286          updateStatusIcon();
287    }
288 }