View Javadoc

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