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