View Javadoc

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