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