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.awt.BorderLayout;
    16  import java.awt.Component;
    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.JPanel;
    23  import javax.swing.JSplitPane;
    24  import javax.swing.JTabbedPane;
    25  import javax.swing.JToolBar;
    26
    27  import com.eviware.soapui.SoapUI;
    28  import com.eviware.soapui.impl.wsdl.panels.request.AbstractWsdlRequestDesktopPanel;
    29  import com.eviware.soapui.impl.wsdl.panels.request.components.SoapMessageXmlEditor;
    30  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    31  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
    32  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
    33  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
    34  import com.eviware.soapui.impl.wsdl.teststeps.actions.AddAssertionAction;
    35  import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError;
    36  import com.eviware.soapui.model.ModelItem;
    37  import com.eviware.soapui.model.iface.Submit;
    38  import com.eviware.soapui.model.iface.SubmitContext;
    39  import com.eviware.soapui.model.iface.Request.SubmitException;
    40  import com.eviware.soapui.model.testsuite.LoadTestRunner;
    41  import com.eviware.soapui.model.testsuite.TestRunner;
    42  import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
    43  import com.eviware.soapui.support.UISupport;
    44  import com.eviware.soapui.support.log.JLogList;
    45
    46  /***
    47   * DesktopPanel for WsdlTestRequest. Essentially a copy of WsdlRequestDesktopPanel with assertions.
    48   *
    49   * @author Ole.Matzura
    50   */
    51
    52  public class WsdlTestRequestDesktopPanel extends AbstractWsdlRequestDesktopPanel<WsdlTestRequestStep, WsdlTestRequest>
    53  {
    54     private JLogList logArea;
    55     private InternalTestMonitorListener testMonitorListener = new InternalTestMonitorListener();
    56  	private JButton addAssertionButton;
    57  	protected boolean updatingRequest;
    58  	private WsdlTestRequestDesktopPanel.InternalSubmitListener submitListener;
    59  	private AssertionsPanel assertionsPanel;
    60  	private JSplitPane outerSplit;
    61
    62     public WsdlTestRequestDesktopPanel( WsdlTestRequestStep requestStep )
    63     {
    64        super( requestStep );
    65
    66        init( requestStep.getTestRequest() );
    67
    68        SoapUI.getTestMonitor().addTestMonitorListener( testMonitorListener );
    69        setEnabled( !SoapUI.getTestMonitor().hasRunningTest( requestStep.getTestCase() ) );
    70     }
    71
    72     private Component buildLogPanel()
    73     {
    74        JTabbedPane tabbedPane = new JTabbedPane( JTabbedPane.RIGHT );
    75        tabbedPane.setTabLayoutPolicy( JTabbedPane.SCROLL_TAB_LAYOUT );
    76
    77        logArea = new JLogList("Request Log");
    78
    79        JPanel panel = new JPanel(new BorderLayout());
    80        panel.add(logArea, BorderLayout.CENTER);
    81
    82        tabbedPane.addTab( "Assertions", buildAssertionsPanel() );
    83        tabbedPane.addTab( "Request Log", panel );
    84        return UISupport.createTabPanel( tabbedPane, true );
    85     }
    86
    87     private Component buildAssertionsPanel()
    88     {
    89     	assertionsPanel = new AssertionsPanel( getRequest() )
    90     	{
    91  			protected void selectError(AssertionError error)
    92  			{
    93  				SoapMessageXmlEditor editor = getResponseEditor();
    94  				editor.requestFocus();
    95  			}
    96     	};
    97
    98     	return assertionsPanel;
    99     }
    100
    101 	public void setContent(JComponent content)
    102 	{
    103 		outerSplit.setTopComponent( content );
    104 	}
    105
    106 	public void removeContent(JComponent content)
    107 	{
    108 		outerSplit.setTopComponent( null );
    109 	}
    110
    111 	protected String getHelpUrl()
    112 	{
    113 		return HelpUrls.TESTREQUESTEDITOR_HELP_URL;
    114 	}
    115
    116 	protected JComponent buildContent()
    117    {
    118    	JComponent component = super.buildContent();
    119
    120       outerSplit = UISupport.createVerticalSplit();
    121       outerSplit.setTopComponent( component );
    122       outerSplit.setBottomComponent( buildLogPanel() );
    123       outerSplit.setDividerLocation( 350 );
    124       outerSplit.setResizeWeight( 0.9 );
    125       outerSplit.setBorder( null );
    126
    127       return outerSplit;
    128    }
    129
    130    protected SoapMessageXmlEditor buildRequestEditor()
    131 	{
    132 		SoapMessageXmlEditor editor = super.buildRequestEditor();
    133 		return editor;
    134 	}
    135
    136 	protected SoapMessageXmlEditor buildResponseEditor()
    137 	{
    138 		SoapMessageXmlEditor editor = super.buildResponseEditor();
    139 		return editor;
    140 	}
    141
    142    protected JComponent buildToolbar()
    143 	{
    144    	addAssertionButton = createActionButton(new AddAssertionAction(getRequest()), true );
    145 		return super.buildToolbar();
    146 	}
    147
    148 	protected void insertButtons(JToolBar toolbar)
    149 	{
    150 		toolbar.add( addAssertionButton );
    151 	}
    152
    153 	public void setEnabled( boolean enabled )
    154    {
    155 		if( enabled == true )
    156 			enabled = !SoapUI.getTestMonitor().hasRunningLoadTest( getModelItem().getTestCase() );
    157
    158 		super.setEnabled( enabled );
    159       addAssertionButton.setEnabled( enabled );
    160       assertionsPanel.setEnabled( enabled );
    161
    162       if( SoapUI.getTestMonitor().hasRunningLoadTest( getRequest().getTestCase() ))
    163       {
    164       	getRequest().removeSubmitListener( submitListener );
    165       }
    166       else
    167       {
    168       	getRequest().addSubmitListener( submitListener );
    169       }
    170    }
    171
    172    protected Submit doSubmit() throws SubmitException
    173 	{
    174 		return getRequest().submit( new WsdlTestRunContext(getModelItem()), true );
    175 	}
    176
    177    protected InternalSubmitListener createSubmitListener()
    178 	{
    179 		submitListener = new InternalSubmitListener();
    180 		return submitListener;
    181 	}
    182
    183 	private class InternalSubmitListener extends AbstractWsdlRequestDesktopPanel.InternalSubmitListener
    184    {
    185       private long startTime;
    186       private SimpleDateFormat sdf;
    187
    188       private InternalSubmitListener()
    189       {
    190       	super();
    191          sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    192       }
    193
    194       public boolean beforeSubmit(Submit submit, SubmitContext context)
    195       {
    196       	boolean result = super.beforeSubmit( submit, context );
    197          startTime = System.currentTimeMillis();
    198          return result;
    199       }
    200
    201       protected void logMessages(String message, String infoMessage)
    202 		{
    203          super.logMessages( message, infoMessage );
    204          logArea.addLine( sdf.format( new Date( startTime )) + " - " + message );
    205       }
    206    }
    207
    208 	public boolean onClose( boolean canCancel )
    209 	{
    210 		if( super.onClose( canCancel ))
    211 		{
    212 			assertionsPanel.release();
    213 			SoapUI.getTestMonitor().removeTestMonitorListener( testMonitorListener );
    214 			logArea.release();
    215 			return true;
    216 		}
    217
    218       return false;
    219 	}
    220
    221 	public boolean dependsOn(ModelItem modelItem)
    222 	{
    223 		return modelItem == getRequest() || modelItem == getModelItem() || modelItem == getRequest().getOperation() ||
    224 		    modelItem == getRequest().getOperation().getInterface() ||
    225 		    modelItem == getRequest().getOperation().getInterface().getProject() ||
    226 		    modelItem == getModelItem().getTestCase() || modelItem == getModelItem().getTestCase().getTestSuite();
    227 	}
    228
    229 	private class InternalTestMonitorListener extends TestMonitorListenerAdapter
    230 	{
    231 		public void loadTestFinished(LoadTestRunner runner)
    232 		{
    233 			setEnabled( !SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
    234 		}
    235
    236 		public void loadTestStarted(LoadTestRunner runner)
    237 		{
    238 			if( runner.getLoadTest().getTestCase() == getModelItem().getTestCase() )
    239 				setEnabled( false );
    240 		}
    241
    242 		public void testCaseFinished(TestRunner runner)
    243 		{
    244 			setEnabled(	!SoapUI.getTestMonitor().hasRunningTest( getModelItem().getTestCase() ) );
    245 		}
    246
    247 		public void testCaseStarted(TestRunner runner)
    248 		{
    249 			if( runner.getTestCase() == getModelItem().getTestCase())
    250 				setEnabled( false );
    251 		}
    252 	}
    253
    254 	@Override
    255 	public String getDescription()
    256 	{
    257 		return "TestRequest: [" + getModelItem().getName() + "] - " + getModelItem().getTestStepTitle();
    258 	}
    259
    260 	public String getTitle()
    261 	{
    262 		return getModelItem().getTestCase().getName() + " - " + getModelItem().getName() +
    263 		    " (" + getModelItem().getTestRequest().getOperation().getName() + ")";
    264 	}
    265 }