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.RestTestRequest;
21  import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep;
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  public class RestTestRequestDesktopPanel extends AbstractRestRequestDesktopPanel<RestTestRequestStep, RestTestRequest>
45  {
46     private JLogList logArea;
47     private InternalTestMonitorListener testMonitorListener = new InternalTestMonitorListener();
48     private JButton addAssertionButton;
49     protected boolean updatingRequest;
50     private AssertionsPanel assertionsPanel;
51     private JInspectorPanel inspectorPanel;
52     private JComponentInspector<?> assertionInspector;
53     private JComponentInspector<?> logInspector;
54     private InternalAssertionsListener assertionsListener = new InternalAssertionsListener();
55     private long startTime;
56     private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
57  
58     public RestTestRequestDesktopPanel(RestTestRequestStep requestStep)
59     {
60        super(requestStep, requestStep.getTestRequest());
61  
62        SoapUI.getTestMonitor().addTestMonitorListener(testMonitorListener);
63        setEnabled(!SoapUI.getTestMonitor().hasRunningTest(requestStep.getTestCase()));
64  
65        requestStep.getTestRequest().addAssertionsListener(assertionsListener);
66     }
67  
68     protected JComponent buildLogPanel()
69     {
70        logArea = new JLogList("Request Log");
71  
72        logArea.getLogList().getModel().addListDataListener(new ListDataChangeListener()
73        {
74  
75           public void dataChanged(ListModel model)
76           {
77              logInspector.setTitle("Request Log (" + model.getSize() + ")");
78           }
79        });
80  
81        return logArea;
82     }
83  
84     protected AssertionsPanel buildAssertionsPanel()
85     {
86        return new AssertionsPanel(getRequest())
87        {
88           protected void selectError(AssertionError error)
89           {
90              ModelItemXmlEditor<?, ?> editor = (ModelItemXmlEditor<?, ?>) getResponseEditor();
91              editor.requestFocus();
92           }
93        };
94     }
95  
96     public void setContent(JComponent content)
97     {
98        inspectorPanel.setContentComponent(content);
99     }
100 
101    public void removeContent(JComponent content)
102    {
103       inspectorPanel.setContentComponent(null);
104    }
105 
106    protected String getHelpUrl()
107    {
108       return HelpUrls.TESTREQUESTEDITOR_HELP_URL;
109    }
110 
111    protected JComponent buildContent()
112    {
113       JComponent component = super.buildContent();
114 
115       inspectorPanel = JInspectorPanelFactory.build(component);
116       assertionsPanel = buildAssertionsPanel();
117 
118       assertionInspector = new JComponentInspector<JComponent>(assertionsPanel, "Assertions ("
119               + getModelItem().getAssertionCount() + ")", "Assertions for this Test Request", true);
120 
121       inspectorPanel.addInspector(assertionInspector);
122 
123       logInspector = new JComponentInspector<JComponent>(buildLogPanel(), "Request Log (0)", "Log of requests", true);
124       inspectorPanel.addInspector(logInspector);
125       inspectorPanel.setDefaultDividerLocation(0.6F);
126       inspectorPanel.setCurrentInspector("Assertions");
127 
128       updateStatusIcon();
129 
130       return inspectorPanel.getComponent();
131    }
132 
133    private void updateStatusIcon()
134    {
135       AssertionStatus status = getModelItem().getTestRequest().getAssertionStatus();
136       switch (status)
137       {
138          case FAILED:
139          {
140             assertionInspector.setIcon(UISupport.createImageIcon("/failed_assertion.gif"));
141             inspectorPanel.activate(assertionInspector);
142             break;
143          }
144          case UNKNOWN:
145          {
146             assertionInspector.setIcon(UISupport.createImageIcon("/unknown_assertion.gif"));
147             break;
148          }
149          case VALID:
150          {
151             assertionInspector.setIcon(UISupport.createImageIcon("/valid_assertion.gif"));
152             inspectorPanel.deactivate();
153             break;
154          }
155       }
156    }
157 
158    protected JComponent buildToolbar()
159    {
160       addAssertionButton = createActionButton(new AddAssertionAction(getRequest()), true);
161       return super.buildToolbar();
162    }
163 
164    protected void insertButtons(JXToolBar toolbar)
165    {
166       toolbar.add(addAssertionButton);
167       super.insertButtons( toolbar );
168    }
169 
170    public void setEnabled(boolean enabled)
171    {
172       if (enabled == true)
173          enabled = !SoapUI.getTestMonitor().hasRunningLoadTest(getModelItem().getTestCase());
174 
175       super.setEnabled(enabled);
176       addAssertionButton.setEnabled(enabled);
177       assertionsPanel.setEnabled(enabled);
178 
179       if (SoapUI.getTestMonitor().hasRunningLoadTest(getRequest().getTestCase()))
180       {
181          getRequest().removeSubmitListener(this);
182       }
183       else
184       {
185          getRequest().addSubmitListener(this);
186       }
187    }
188 
189    protected Submit doSubmit() throws SubmitException
190    {
191       return getRequest().submit(new WsdlTestRunContext(getModelItem()), true);
192    }
193 
194    private final class InternalAssertionsListener implements AssertionsListener
195    {
196       public void assertionAdded(TestAssertion assertion)
197       {
198          assertionInspector.setTitle("Assertions (" + getModelItem().getAssertionCount() + ")");
199       }
200 
201       public void assertionRemoved(TestAssertion assertion)
202       {
203          assertionInspector.setTitle("Assertions (" + getModelItem().getAssertionCount() + ")");
204       }
205    }
206 
207    public boolean beforeSubmit(Submit submit, SubmitContext context)
208    {
209       boolean result = super.beforeSubmit(submit, context);
210       startTime = System.currentTimeMillis();
211       return result;
212    }
213 
214    protected void logMessages(String message, String infoMessage)
215    {
216       super.logMessages(message, infoMessage);
217       logArea.addLine(sdf.format(new Date(startTime)) + " - " + message);
218    }
219 
220    @Override
221    public void afterSubmit(Submit submit, SubmitContext context)
222    {
223       super.afterSubmit(submit, context);
224       if( !isHasClosed() )
225         updateStatusIcon();
226    }
227 
228    public boolean onClose(boolean canCancel)
229    {
230       if (super.onClose(canCancel))
231       {
232          assertionsPanel.release();
233          SoapUI.getTestMonitor().removeTestMonitorListener(testMonitorListener);
234          logArea.release();
235          getModelItem().getTestRequest().removeAssertionsListener(assertionsListener);
236          return true;
237       }
238 
239       return false;
240    }
241 
242    public boolean dependsOn(ModelItem modelItem)
243    {
244       return modelItem == getRequest() || modelItem == getModelItem() || modelItem == getRequest().getOperation()
245               || modelItem == getRequest().getOperation().getInterface()
246               || modelItem == getRequest().getOperation().getInterface().getProject()
247               || modelItem == getModelItem().getTestCase() || modelItem == getModelItem().getTestCase().getTestSuite();
248    }
249 
250    private class InternalTestMonitorListener extends TestMonitorListenerAdapter
251    {
252       public void loadTestFinished(LoadTestRunner runner)
253       {
254          setEnabled(!SoapUI.getTestMonitor().hasRunningTest(getModelItem().getTestCase()));
255       }
256 
257       public void loadTestStarted(LoadTestRunner runner)
258       {
259          if (runner.getLoadTest().getTestCase() == getModelItem().getTestCase())
260             setEnabled(false);
261       }
262 
263       public void testCaseFinished(TestRunner runner)
264       {
265          setEnabled(!SoapUI.getTestMonitor().hasRunningTest(getModelItem().getTestCase()));
266       }
267 
268       public void testCaseStarted(TestRunner runner)
269       {
270          if (runner.getTestCase() == getModelItem().getTestCase())
271             setEnabled(false);
272       }
273    }
274 
275    public void propertyChange(PropertyChangeEvent evt)
276    {
277       super.propertyChange(evt);
278 
279       if (evt.getPropertyName().equals(RestTestRequest.STATUS_PROPERTY))
280          updateStatusIcon();
281    }
282 }