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.awt.Dimension;
    18  import java.awt.event.ActionEvent;
    19  import java.awt.event.FocusAdapter;
    20  import java.awt.event.FocusEvent;
    21  import java.awt.event.MouseAdapter;
    22  import java.awt.event.MouseEvent;
    23
    24  import javax.swing.AbstractAction;
    25  import javax.swing.Action;
    26  import javax.swing.Box;
    27  import javax.swing.Icon;
    28  import javax.swing.JButton;
    29  import javax.swing.JComponent;
    30  import javax.swing.JLabel;
    31  import javax.swing.JPanel;
    32  import javax.swing.JScrollPane;
    33  import javax.swing.JSplitPane;
    34
    35  import org.apache.log4j.Logger;
    36
    37  import com.eviware.soapui.SoapUI;
    38  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
    39  import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunContext;
    40  import com.eviware.soapui.impl.wsdl.panels.support.MockTestRunner;
    41  import com.eviware.soapui.impl.wsdl.panels.support.TestRunComponentEnabler;
    42  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
    43  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
    44  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    45  import com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep;
    46  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStepResult;
    47  import com.eviware.soapui.model.ModelItem;
    48  import com.eviware.soapui.model.settings.Settings;
    49  import com.eviware.soapui.model.settings.SettingsListener;
    50  import com.eviware.soapui.support.UISupport;
    51  import com.eviware.soapui.support.components.JEditorStatusBarWithProgress;
    52  import com.eviware.soapui.support.components.JXToolBar;
    53  import com.eviware.soapui.support.log.JLogList;
    54  import com.eviware.soapui.ui.desktop.DesktopPanel;
    55
    56  /***
    57   * DesktopPanel for WsdlGroovyTestSteps
    58   *
    59   * @author Ole.Matzura
    60   */
    61
    62  public class GroovyScriptStepDesktopPanel extends JPanel implements DesktopPanel
    63  {
    64  	private final WsdlGroovyScriptTestStep groovyStep;
    65  	private GroovyEditor editor;
    66  	private JSplitPane mainSplit;
    67  	private JLogList logArea;
    68  	private Logger logger;
    69  	private TestRunComponentEnabler componentEnabler;
    70  	private RunAction runAction = new RunAction();
    71  	private JEditorStatusBarWithProgress statusBar;
    72  	private SettingsListener settingsListener;
    73
    74  	public GroovyScriptStepDesktopPanel(WsdlGroovyScriptTestStep groovyStep)
    75  	{
    76  		super( new BorderLayout() );
    77  		this.groovyStep = groovyStep;
    78  		componentEnabler = new TestRunComponentEnabler( groovyStep.getTestCase() );
    79
    80  		buildUI();
    81  		setPreferredSize( new Dimension( 600, 440 ));
    82
    83  		logger = Logger.getLogger( groovyStep.getName() + "#" + hashCode() );
    84
    85  		addFocusListener( new FocusAdapter() {
    86
    87  			public void focusGained( FocusEvent e )
    88  			{
    89  				editor.requestFocusInWindow();
    90  			}
    91
    92  			} );
    93  	}
    94
    95  	private void buildUI()
    96  	{
    97  		editor = new GroovyEditor( new ScriptStepGroovyEditorModel( ));
    98
    99  		logArea = new JLogList( "Groovy Test Log" );
    100 		logArea.addLogger( groovyStep.getName() + "#" + hashCode(), true );
    101 		logArea.getLogList().addMouseListener( new MouseAdapter() {
    102
    103 			public void mouseClicked(MouseEvent e)
    104 			{
    105 				if( e.getClickCount() < 2 )
    106 					return;
    107
    108 				String value = logArea.getLogList().getSelectedValue().toString();
    109 				if( value == null )
    110 					return;
    111
    112 				editor.selectError( value );
    113 			}} );
    114
    115 		mainSplit = UISupport.createVerticalSplit( new JScrollPane( editor ), logArea);
    116 		mainSplit.setDividerLocation( 280 );
    117 		mainSplit.setResizeWeight( 0.8 );
    118 		add( mainSplit, BorderLayout.CENTER );
    119 		add( buildToolbar(), BorderLayout.NORTH );
    120 		add( buildStatusBar(), BorderLayout.SOUTH );
    121
    122 		componentEnabler.add( editor );
    123 	}
    124
    125 	private Component buildStatusBar()
    126 	{
    127 		statusBar = new JEditorStatusBarWithProgress( editor );
    128 		return statusBar;
    129 	}
    130
    131 	private JComponent buildToolbar()
    132 	{
    133 		JXToolBar toolBar = UISupport.createToolbar();
    134 		JButton runButton = UISupport.createToolbarButton( runAction );
    135 		toolBar.add( runButton );
    136 		toolBar.add( Box.createHorizontalGlue() );
    137 		JLabel label = new JLabel("<html>Script is invoked with <code>log</code>, <code>context</code> " +
    138 						"and <code>testRunner</code> variables</html>");
    139 		label.setToolTipText( label.getText() );
    140 		label.setMaximumSize( label.getPreferredSize() );
    141
    142 		toolBar.add( label);
    143 		toolBar.addRelatedGap();
    144 		toolBar.add( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.GROOVYSTEPEDITOR_HELP_URL )));
    145
    146 		componentEnabler.add( runButton );
    147
    148 		return toolBar;
    149 	}
    150
    151 	public ModelItem getModelItem()
    152 	{
    153 		return groovyStep;
    154 	}
    155
    156 	public boolean onClose( boolean canCancel )
    157 	{
    158 		componentEnabler.release();
    159 		editor.release();
    160 		SoapUI.getSettings().removeSettingsListener( settingsListener );
    161 		logger.removeAllAppenders();
    162 		logger = null;
    163 		logArea.release();
    164 		return true;
    165 	}
    166
    167 	public JComponent getComponent()
    168 	{
    169 		return this;
    170 	}
    171
    172 	public boolean dependsOn(ModelItem modelItem)
    173 	{
    174 		return modelItem == groovyStep || modelItem == groovyStep.getTestCase() ||
    175 				modelItem == groovyStep.getTestCase().getTestSuite() ||
    176 				modelItem == groovyStep.getTestCase().getTestSuite().getProject();
    177 	}
    178
    179 	public String getTitle()
    180 	{
    181 		return groovyStep.getTestCase().getName() + " - " + groovyStep.getName();
    182 	}
    183
    184 	public String getDescription()
    185 	{
    186 		return "Goto: [" + groovyStep.getName() + "] - " + groovyStep.getTestStepTitle();
    187 	}
    188
    189 	public Icon getIcon()
    190 	{
    191 		return getModelItem().getIcon();
    192 	}
    193
    194 	private class ScriptStepGroovyEditorModel implements GroovyEditorModel
    195 	{
    196 		public String[] getKeywords()
    197 		{
    198 			return new String[] {"log", "context", "testRunner"};
    199 		}
    200
    201 		public Action getRunAction()
    202 		{
    203 			return runAction;
    204 		}
    205
    206 		public String getScript()
    207 		{
    208 			return groovyStep.getScript();
    209 		}
    210
    211 		public void setScript( String text )
    212 		{
    213 			groovyStep.setScript( text );
    214 		}
    215
    216 		public Settings getSettings()
    217 		{
    218 			return SoapUI.getSettings();
    219 		}}
    220
    221 	private class RunAction extends AbstractAction
    222 	{
    223 		public RunAction()
    224 		{
    225 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/run_groovy_script.gif" ));
    226 			putValue( Action.SHORT_DESCRIPTION, "Runs this script using a mock testRunner and testContext" );
    227 		}
    228
    229 		public void actionPerformed(ActionEvent e)
    230 		{
    231 			MockTestRunner mockTestRunner = new MockTestRunner( groovyStep.getTestCase(), logger );
    232 			statusBar.setIndeterminate( true );
    233 			WsdlTestStepResult result = (WsdlTestStepResult) groovyStep.run( mockTestRunner,
    234 					new MockTestRunContext( mockTestRunner, groovyStep ) );
    235 			statusBar.setIndeterminate( false );
    236
    237 			if( result.getError() != null )
    238 			{
    239 				String message = result.getError().getMessage();
    240
    241 				// ugly...
    242 				editor.selectError( message);
    243
    244 				UISupport.showErrorMessage( result.getError().toString() );
    245 				editor.requestFocus();
    246 			}
    247 		}
    248 	}
    249 }