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