View Javadoc

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