View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.Toolkit;
18  import java.awt.event.MouseAdapter;
19  import java.awt.event.MouseEvent;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  import javax.swing.JButton;
24  import javax.swing.JComponent;
25  import javax.swing.JList;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  import javax.swing.JSplitPane;
29  import javax.swing.JTabbedPane;
30  import javax.swing.JTextArea;
31  import javax.swing.JToolBar;
32  
33  import com.eviware.soapui.SoapUI;
34  import com.eviware.soapui.impl.wsdl.panels.request.AbstractWsdlRequestDesktopPanel;
35  import com.eviware.soapui.impl.wsdl.panels.request.components.SoapMessageXmlEditor;
36  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
37  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
38  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
39  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
40  import com.eviware.soapui.impl.wsdl.teststeps.actions.AddAssertionAction;
41  import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError;
42  import com.eviware.soapui.model.ModelItem;
43  import com.eviware.soapui.model.iface.Submit;
44  import com.eviware.soapui.model.iface.SubmitContext;
45  import com.eviware.soapui.model.iface.Request.SubmitException;
46  import com.eviware.soapui.model.testsuite.LoadTestRunner;
47  import com.eviware.soapui.model.testsuite.TestRunner;
48  import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
49  import com.eviware.soapui.support.UISupport;
50  import com.eviware.soapui.support.xml.JXEditTextArea;
51  
52  /***
53   * DesktopPanel for WsdlTestRequest. Essentially a copy of WsdlRequestDesktopPanel with assertions.
54   * 
55   * @author Ole.Matzura
56   */
57  
58  public class WsdlTestRequestDesktopPanel extends AbstractWsdlRequestDesktopPanel<WsdlTestRequestStep, WsdlTestRequest>
59  {
60     private JTextArea logArea;
61     private InternalTestMonitorListener testMonitorListener = new InternalTestMonitorListener();
62  	private JButton addAssertionButton;
63  	protected boolean updatingRequest;
64  	private WsdlTestRequestDesktopPanel.InternalSubmitListener submitListener;
65  	private AssertionsPanel assertionsPanel;
66  	private JSplitPane outerSplit;
67  
68     public WsdlTestRequestDesktopPanel( WsdlTestRequestStep requestStep )
69     {
70        super( requestStep );
71        
72        init( requestStep.getTestRequest() );
73        
74        SoapUI.getTestMonitor().addTestMonitorListener( testMonitorListener );
75        setEnabled( !SoapUI.getTestMonitor().hasRunningTest( requestStep.getTestCase() ) );
76     }
77  
78     private Component buildLogPanel()
79     {
80        JTabbedPane tabbedPane = new JTabbedPane( JTabbedPane.RIGHT );
81        tabbedPane.setTabLayoutPolicy( JTabbedPane.SCROLL_TAB_LAYOUT );
82        
83        logArea = new JTextArea();
84        logArea.setEditable(false);
85        logArea.setToolTipText("Request Log");
86        
87        JPanel panel = new JPanel(new BorderLayout());
88        panel.add(new JScrollPane(logArea), BorderLayout.CENTER);
89        
90        tabbedPane.addTab( "Assertions", buildAssertionsPanel() );
91        tabbedPane.addTab( "Request Log", panel );
92        return UISupport.createTabPanel( tabbedPane, true );
93     }
94     
95     private Component buildAssertionsPanel()
96     {
97     	assertionsPanel = new AssertionsPanel( getRequest() )
98     	{
99  			protected void selectError(AssertionError error)
100 			{
101 				SoapMessageXmlEditor editor = getResponseEditor();
102 				editor.requestFocus();
103 			}
104    	};
105    	
106    	return assertionsPanel;
107    }
108    
109 	public void setContent(JComponent content)
110 	{
111 		outerSplit.setTopComponent( content );
112 	}
113 
114 	public void removeContent(JComponent content)
115 	{
116 		outerSplit.setTopComponent( 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       outerSplit = UISupport.createVerticalSplit();
129       outerSplit.setTopComponent( component );
130       outerSplit.setBottomComponent( buildLogPanel() );
131       outerSplit.setDividerLocation( 350 );
132       outerSplit.setResizeWeight( 0.9 );
133       outerSplit.setBorder( null );
134 
135       return outerSplit;
136    }
137    
138    protected SoapMessageXmlEditor buildRequestEditor()
139 	{
140 		SoapMessageXmlEditor editor = super.buildRequestEditor();
141 		return editor;
142 	}
143 
144 	protected SoapMessageXmlEditor buildResponseEditor()
145 	{
146 		SoapMessageXmlEditor editor = super.buildResponseEditor();
147 		return editor;
148 	}
149 
150    protected JComponent buildToolbar()
151 	{
152    	addAssertionButton = createActionButton(new AddAssertionAction(getRequest()), true);
153 		return super.buildToolbar();
154 	}
155 
156 	protected void insertButtons(JToolBar toolbar)
157 	{
158 		toolbar.add( addAssertionButton );
159 	}
160 
161 	public void setEnabled( boolean enabled )
162    {
163 		if( enabled == true )
164 			enabled = !SoapUI.getTestMonitor().hasRunningLoadTest( getModelItem().getTestCase() );
165 		
166 		super.setEnabled( enabled );
167       addAssertionButton.setEnabled( enabled );
168       assertionsPanel.setEnabled( enabled );
169       
170       if( SoapUI.getTestMonitor().hasRunningLoadTest( getRequest().getTestCase() ))
171       {
172       	getRequest().removeSubmitListener( submitListener );
173       }
174       else
175       {
176       	getRequest().addSubmitListener( submitListener );
177       }
178    }
179    
180    protected Submit doSubmit() throws SubmitException
181 	{
182 		return getRequest().submit( new WsdlTestRunContext(getModelItem()), true );
183 	}
184 
185    protected InternalSubmitListener createSubmitListener()
186 	{
187 		submitListener = new InternalSubmitListener();
188 		return submitListener;
189 	}
190    
191 	private class InternalSubmitListener extends AbstractWsdlRequestDesktopPanel.InternalSubmitListener
192    {
193       private long startTime;
194       private SimpleDateFormat sdf;
195 
196       private InternalSubmitListener()
197       {
198       	super();
199          sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
200       }
201 
202       public boolean beforeSubmit(Submit submit, SubmitContext context)
203       {
204       	boolean result = super.beforeSubmit( submit, context );
205          startTime = System.currentTimeMillis();
206          return result;
207       }
208 
209       protected void logMessages(String message, String infoMessage)
210 		{
211          super.logMessages( message, infoMessage );
212       	
213          String logText = logArea.getText() + sdf.format( new Date( startTime )) + " - " + message + "\r\n";
214          logArea.setText( logText );
215          logArea.setCaretPosition( logText.length() );
216       }
217    }
218    
219 	public boolean onClose( boolean canCancel )
220 	{
221 		if( super.onClose( canCancel ))
222 		{
223 			assertionsPanel.release();
224 			SoapUI.getTestMonitor().removeTestMonitorListener( testMonitorListener );
225 			
226 			return true;
227 		}
228 		
229       return false;
230 	}
231 
232    private final static class ValidationListMouseAdapter extends MouseAdapter
233 	{
234 		private final JList list;
235 		private final JXEditTextArea textArea;
236 
237 		public ValidationListMouseAdapter(JList list, JXEditTextArea textArea)
238 		{
239 			this.list = list;
240 			this.textArea = textArea;
241 		}
242 
243 		public void mouseClicked(MouseEvent e)
244 		{
245 		   if( e.getClickCount() < 2 ) return;
246 		
247 		   int ix = list.getSelectedIndex();
248 		   if( ix == -1 ) return;
249 		   
250 		   Object obj = list.getModel().getElementAt( ix );
251 		   if( obj instanceof AssertionError )
252 		   {
253 		   	AssertionError error = (AssertionError) obj;
254 		   	if( error.getLineNumber() >= 0 )
255 		   	{
256 		   		textArea.setCaretPosition( textArea.getLineStartOffset( error.getLineNumber()-1 ) );
257 		   		textArea.requestFocus();
258 		   	}
259 		   	else Toolkit.getDefaultToolkit().beep();
260 		   }
261 		   else Toolkit.getDefaultToolkit().beep();
262 		}
263 	}
264    
265 	public boolean dependsOn(ModelItem modelItem)
266 	{
267 		return modelItem == getRequest() || modelItem == getModelItem() || modelItem == getRequest().getOperation() ||
268 		    modelItem == getRequest().getOperation().getInterface() || 
269 		    modelItem == getRequest().getOperation().getInterface().getProject() ||
270 		    modelItem == getModelItem().getTestCase() || modelItem == getModelItem().getTestCase().getTestSuite();
271 	}
272 	
273 	private class InternalTestMonitorListener extends TestMonitorListenerAdapter
274 	{
275 		public void loadTestFinished(LoadTestRunner runner)
276 		{
277 			setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
278 		}
279 
280 		public void loadTestStarted(LoadTestRunner runner)
281 		{
282 			if( runner.getLoadTest().getTestCase() == getModelItem().getTestCase() )
283 				setEnabled( false );
284 		}
285 
286 		public void testCaseFinished(TestRunner runner)
287 		{
288 			setEnabled(	!SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
289 		}
290 
291 		public void testCaseStarted(TestRunner runner)
292 		{
293 			if( runner.getTestCase() == getModelItem().getTestCase())
294 				setEnabled( false );
295 		}
296 	}
297 
298 	@Override
299 	public String getDescription()
300 	{
301 		return "TestRequest: [" + getModelItem().getName() + "] - " + getModelItem().getTestStepTitle();
302 	}
303 
304 	public String getTitle()
305 	{
306 		return getModelItem().getTestCase().getName() + " - " + getModelItem().getName() + 
307 		    " (" + getModelItem().getTestRequest().getOperation().getName() + ")";
308 	}
309 }