View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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 java.beans.PropertyChangeEvent;
16  import java.beans.PropertyChangeListener;
17  import java.text.SimpleDateFormat;
18  import java.util.Date;
19  
20  import javax.swing.JButton;
21  import javax.swing.JComponent;
22  import javax.swing.JToolBar;
23  import javax.swing.ListModel;
24  
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.impl.wsdl.panels.request.AbstractWsdlRequestDesktopPanel;
27  import com.eviware.soapui.impl.wsdl.panels.request.components.SoapMessageXmlEditor;
28  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
29  import com.eviware.soapui.impl.wsdl.support.assertions.Assertable.AssertionStatus;
30  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
31  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
32  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
33  import com.eviware.soapui.impl.wsdl.teststeps.actions.AddAssertionAction;
34  import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError;
35  import com.eviware.soapui.model.ModelItem;
36  import com.eviware.soapui.model.iface.Submit;
37  import com.eviware.soapui.model.iface.SubmitContext;
38  import com.eviware.soapui.model.iface.Request.SubmitException;
39  import com.eviware.soapui.model.testsuite.LoadTestRunner;
40  import com.eviware.soapui.model.testsuite.TestRunner;
41  import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
42  import com.eviware.soapui.support.ListDataChangeListener;
43  import com.eviware.soapui.support.UISupport;
44  import com.eviware.soapui.support.components.JComponentInspector;
45  import com.eviware.soapui.support.components.JInspectorPanel;
46  import com.eviware.soapui.support.log.JLogList;
47  
48  /***
49   * DesktopPanel for WsdlTestRequest. Essentially a copy of WsdlRequestDesktopPanel with assertions.
50   * 
51   * @author Ole.Matzura
52   */
53  
54  public class WsdlTestRequestDesktopPanel extends AbstractWsdlRequestDesktopPanel<WsdlTestRequestStep, WsdlTestRequest> implements PropertyChangeListener
55  {
56     private JLogList logArea;
57     private InternalTestMonitorListener testMonitorListener = new InternalTestMonitorListener();
58  	private JButton addAssertionButton;
59  	protected boolean updatingRequest;
60  	private WsdlTestRequestDesktopPanel.InternalSubmitListener submitListener;
61  	private AssertionsPanel assertionsPanel;
62  	private JInspectorPanel inspectorPanel;
63  	private JComponentInspector assertionInspector;
64  	private JComponentInspector logInspector;
65  	
66  	public WsdlTestRequestDesktopPanel( WsdlTestRequestStep requestStep )
67     {
68        super( requestStep );
69        
70        init( requestStep.getTestRequest() );
71        
72        SoapUI.getTestMonitor().addTestMonitorListener( testMonitorListener );
73        setEnabled( !SoapUI.getTestMonitor().hasRunningTest( requestStep.getTestCase() ) );
74        
75        requestStep.getTestRequest().addPropertyChangeListener( WsdlTestRequest.STATUS_PROPERTY, this );
76     }
77  
78     protected JComponent buildLogPanel()
79     {
80        logArea = new JLogList("Request Log");
81        
82        logArea.getLogList().getModel().addListDataListener( new ListDataChangeListener() {
83  
84  			public void dataChanged( ListModel model )
85  			{
86  				logInspector.setTitle( "Request Log (" + model.getSize() + ")" );
87  			}}
88  				);
89        
90        return logArea;
91     }
92     
93     protected AssertionsPanel buildAssertionsPanel()
94     {
95     	return new AssertionsPanel( getRequest() )
96     	{
97  			protected void selectError(AssertionError error)
98  			{
99  				SoapMessageXmlEditor editor = getResponseEditor();
100 				editor.requestFocus();
101 			}
102    	};
103    }
104    
105 	public void setContent(JComponent content)
106 	{
107 		inspectorPanel.setContentComponent( content );
108 	}
109 
110 	public void removeContent(JComponent content)
111 	{
112 		inspectorPanel.setContentComponent( null );
113 	}
114 	
115 	protected String getHelpUrl()
116 	{
117 		return HelpUrls.TESTREQUESTEDITOR_HELP_URL;
118 	}
119 
120 	protected JComponent buildContent()
121    {
122    	JComponent component = super.buildContent();
123    	
124    	inspectorPanel = new JInspectorPanel( component );
125    	assertionsPanel = buildAssertionsPanel();
126 		assertionInspector = inspectorPanel.addInspector( new JComponentInspector( assertionsPanel, "Assertions", 
127 				   				"Assertions for this Test Request", true) );
128 		
129    	logInspector = new JComponentInspector( 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;
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(JToolBar toolbar)
171 	{
172 		toolbar.add( addAssertionButton );
173 	}
174 
175 	public void setEnabled( boolean enabled )
176    {
177 		if( enabled == true )
178 			enabled = !SoapUI.getTestMonitor().hasRunningLoadTest( getModelItem().getTestCase() );
179 		
180 		super.setEnabled( enabled );
181       addAssertionButton.setEnabled( enabled );
182       assertionsPanel.setEnabled( enabled );
183       
184       if( SoapUI.getTestMonitor().hasRunningLoadTest( getRequest().getTestCase() ))
185       {
186       	getRequest().removeSubmitListener( submitListener );
187       }
188       else
189       {
190       	getRequest().addSubmitListener( submitListener );
191       }
192    }
193    
194    protected Submit doSubmit() throws SubmitException
195 	{
196 		return getRequest().submit( new WsdlTestRunContext(getModelItem()), true );
197 	}
198 
199    protected InternalSubmitListener createSubmitListener()
200 	{
201 		submitListener = new InternalSubmitListener();
202 		return submitListener;
203 	}
204    
205 	private class InternalSubmitListener extends AbstractWsdlRequestDesktopPanel.InternalSubmitListener
206    {
207       private long startTime;
208       private SimpleDateFormat sdf;
209 
210       private InternalSubmitListener()
211       {
212       	super();
213          sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
214       }
215 
216       public boolean beforeSubmit(Submit submit, SubmitContext context)
217       {
218       	boolean result = super.beforeSubmit( submit, context );
219          startTime = System.currentTimeMillis();
220          return result;
221       }
222 
223       protected void logMessages(String message, String infoMessage)
224 		{
225          super.logMessages( message, infoMessage );
226          logArea.addLine( sdf.format( new Date( startTime )) + " - " + message );
227       }
228 
229 		@Override
230 		public void afterSubmit( Submit submit, SubmitContext context )
231 		{
232 			super.afterSubmit( submit, context );
233 			updateStatusIcon();
234 		}
235    }
236    
237 	public boolean onClose( boolean canCancel )
238 	{
239 		if( super.onClose( canCancel ))
240 		{
241 			assertionsPanel.release();
242 			SoapUI.getTestMonitor().removeTestMonitorListener( testMonitorListener );
243 			logArea.release();
244 			getModelItem().getTestRequest().removePropertyChangeListener( WsdlTestRequest.STATUS_PROPERTY, this );
245 			return true;
246 		}
247 		
248 		
249       return false;
250 	}
251 	
252 	public boolean dependsOn(ModelItem modelItem)
253 	{
254 		return modelItem == getRequest() || modelItem == getModelItem() || modelItem == getRequest().getOperation() ||
255 		    modelItem == getRequest().getOperation().getInterface() || 
256 		    modelItem == getRequest().getOperation().getInterface().getProject() ||
257 		    modelItem == getModelItem().getTestCase() || modelItem == getModelItem().getTestCase().getTestSuite();
258 	}
259 	
260 	private class InternalTestMonitorListener extends TestMonitorListenerAdapter
261 	{
262 		public void loadTestFinished(LoadTestRunner runner)
263 		{
264 			setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
265 		}
266 
267 		public void loadTestStarted(LoadTestRunner runner)
268 		{
269 			if( runner.getLoadTest().getTestCase() == getModelItem().getTestCase() )
270 				setEnabled( false );
271 		}
272 
273 		public void testCaseFinished(TestRunner runner)
274 		{
275 			setEnabled(	!SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
276 		}
277 
278 		public void testCaseStarted(TestRunner runner)
279 		{
280 			if( runner.getTestCase() == getModelItem().getTestCase())
281 				setEnabled( false );
282 		}
283 	}
284 
285 	public void propertyChange( PropertyChangeEvent evt )
286 	{
287 		updateStatusIcon();
288 	}
289 }