View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.support.editor.inspectors.script;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Dimension;
17  import java.awt.event.ActionEvent;
18  import java.beans.PropertyChangeListener;
19  
20  import javax.swing.AbstractAction;
21  import javax.swing.Action;
22  import javax.swing.Box;
23  import javax.swing.JButton;
24  import javax.swing.JComponent;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  
28  import com.eviware.soapui.SoapUI;
29  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
30  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
31  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
32  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
33  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
34  import com.eviware.soapui.model.ModelItem;
35  import com.eviware.soapui.model.settings.Settings;
36  import com.eviware.soapui.support.UISupport;
37  import com.eviware.soapui.support.components.JXToolBar;
38  import com.eviware.soapui.support.editor.EditorView;
39  import com.eviware.soapui.support.editor.inspectors.AbstractXmlInspector;
40  import com.eviware.soapui.support.editor.xml.XmlDocument;
41  import com.eviware.soapui.support.types.StringToStringMap;
42  import com.eviware.soapui.support.xml.XmlUtils;
43  
44  public class MockResponseScriptInspector extends AbstractXmlInspector
45  {
46  	private final WsdlMockResponse mockResponse;
47  	private GroovyEditor responseScriptEditor;
48  	private RunScriptAction runScriptAction = new RunScriptAction();
49  	private JPanel panel;
50  
51  	protected MockResponseScriptInspector( WsdlMockResponse mockResponse )
52  	{
53  		super( "Script", "Script for this MockResponse", true, ScriptInspectorFactory.INSPECTOR_ID );
54  		this.mockResponse = mockResponse;
55  	}
56  
57  	public JComponent getComponent()
58  	{
59  		if( panel == null )
60  			buildResponseScriptEditor();
61  
62  		return panel;
63  	}
64  
65  	@Override
66  	public void activate()
67  	{
68  		responseScriptEditor.requestFocusInWindow();
69  	}
70  
71  	protected void buildResponseScriptEditor()
72  	{
73  		responseScriptEditor = new GroovyEditor( new MockResponseGroovyEditorModel() );
74  
75  		panel = new JPanel( new BorderLayout() );
76  		panel.add( buildScriptToolbar(), BorderLayout.NORTH );
77  		panel.add( responseScriptEditor, BorderLayout.CENTER );
78  	}
79  
80  	private JComponent buildScriptToolbar()
81  	{
82  		JXToolBar toolBar = UISupport.createToolbar();
83  		JButton runButton = UISupport.createToolbarButton( runScriptAction );
84  		toolBar.add( runButton );
85  		toolBar.add( Box.createHorizontalGlue() );
86  		JLabel label = new JLabel( "<html>Script is invoked with <code>log</code>, <code>context</code>, "
87  				+ "<code>requestContext</code>, <code>mockRequest</code> and <code>mockResponse</code> variables</html>" );
88  		label.setToolTipText( label.getText() );
89  		label.setMaximumSize( label.getPreferredSize() );
90  
91  		toolBar.add( label );
92  		toolBar.addUnrelatedGap();
93  		toolBar.addFixed( UISupport.createActionButton(
94  				new ShowOnlineHelpAction( HelpUrls.MOCKRESPONSE_SCRIPT_HELP_URL ), true ) );
95  
96  		return toolBar;
97  	}
98  
99  	@Override
100 	public void release()
101 	{
102 		super.release();
103 
104 		responseScriptEditor.release();
105 	}
106 
107 	private class RunScriptAction extends AbstractAction
108 	{
109 		public RunScriptAction()
110 		{
111 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/run_groovy_script.gif" ) );
112 			putValue( Action.SHORT_DESCRIPTION, "Runs this script using mock httpRequest/httpResponse objects" );
113 		}
114 
115 		public void actionPerformed( ActionEvent e )
116 		{
117 			try
118 			{
119 				mockResponse.evaluateScript( mockResponse.getMockResult() == null ? null : mockResponse.getMockResult()
120 						.getMockRequest() );
121 
122 				StringToStringMap values = null;
123 				if( mockResponse.getMockResult() != null )
124 					values = mockResponse.getMockResult().getMockRequest().getContext().toStringToStringMap();
125 
126 				if( values == null || values.isEmpty() )
127 				{
128 					UISupport.showInfoMessage( "No values were returned" );
129 				}
130 				else
131 				{
132 					String msg = "<html><body>Returned values:<br>";
133 
134 					for( String name : values.keySet() )
135 					{
136 						msg += XmlUtils.entitize( name ) + " : " + XmlUtils.entitize( values.get( name ) ) + "<br>";
137 					}
138 
139 					msg += "</body></html>";
140 
141 					UISupport.showExtendedInfo( "Result", "Result of MockResponse Script", msg, new Dimension( 500, 400 ) );
142 				}
143 			}
144 			catch( Throwable e1 )
145 			{
146 				responseScriptEditor.selectError( e1.getMessage() );
147 				UISupport.showErrorMessage( e1.toString() );
148 			}
149 		}
150 	}
151 
152 	private class MockResponseGroovyEditorModel implements GroovyEditorModel
153 	{
154 		public String[] getKeywords()
155 		{
156 			return new String[] { "context", "mockRequest", "mockResponse", "log", "requestContext" };
157 		}
158 
159 		public Action getRunAction()
160 		{
161 			return runScriptAction;
162 		}
163 
164 		public String getScript()
165 		{
166 			return mockResponse.getScript();
167 		}
168 
169 		public void setScript( String text )
170 		{
171 			mockResponse.setScript( text );
172 		}
173 
174 		public Settings getSettings()
175 		{
176 			return SoapUI.getSettings();
177 		}
178 
179 		public String getScriptName()
180 		{
181 			return null;
182 		}
183 
184 		public void addPropertyChangeListener( PropertyChangeListener listener )
185 		{
186 		}
187 
188 		public void removePropertyChangeListener( PropertyChangeListener listener )
189 		{
190 		}
191 
192 		public ModelItem getModelItem()
193 		{
194 			return mockResponse;
195 		}
196 	}
197 
198 	@Override
199 	public boolean isEnabledFor( EditorView<XmlDocument> view )
200 	{
201 		return true;
202 	}
203 }