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