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