1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support.editor.inspectors.script;
14
15 import java.awt.BorderLayout;
16 import java.awt.event.ActionEvent;
17
18 import javax.swing.AbstractAction;
19 import javax.swing.Action;
20 import javax.swing.Box;
21 import javax.swing.JButton;
22 import javax.swing.JComponent;
23 import javax.swing.JLabel;
24 import javax.swing.JPanel;
25 import javax.swing.JScrollPane;
26
27 import com.eviware.soapui.SoapUI;
28 import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
29 import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
30 import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
31 import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
32 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
33 import com.eviware.soapui.model.settings.Settings;
34 import com.eviware.soapui.support.UISupport;
35 import com.eviware.soapui.support.components.JXToolBar;
36 import com.eviware.soapui.support.editor.EditorView;
37 import com.eviware.soapui.support.editor.inspectors.AbstractXmlInspector;
38 import com.eviware.soapui.support.editor.xml.XmlDocument;
39 import com.eviware.soapui.support.types.StringToStringMap;
40
41 public class MockResponseScriptInspector extends AbstractXmlInspector
42 {
43 private final WsdlMockResponse mockResponse;
44 private GroovyEditor responseScriptEditor;
45 private RunScriptAction runScriptAction = new RunScriptAction();
46 private JPanel panel;
47
48 protected MockResponseScriptInspector( WsdlMockResponse mockResponse )
49 {
50 super( "Script", "Script for this MockResponse", true, ScriptInspectorFactory.INSPECTOR_ID );
51 this.mockResponse = mockResponse;
52 }
53
54 public JComponent getComponent()
55 {
56 if( panel == null )
57 buildResponseScriptEditor();
58
59 return panel;
60 }
61
62 @Override
63 public void activate()
64 {
65 responseScriptEditor.requestFocusInWindow();
66 }
67
68 protected void buildResponseScriptEditor()
69 {
70 responseScriptEditor = new GroovyEditor( new MockResponseGroovyEditorModel() );
71
72 panel = new JPanel( new BorderLayout() )
73 {
74
75
76 };
77 panel.add( buildScriptToolbar(), BorderLayout.NORTH );
78 panel.add( new JScrollPane( responseScriptEditor ), BorderLayout.CENTER );
79 }
80
81 private JComponent buildScriptToolbar()
82 {
83 JXToolBar toolBar = UISupport.createToolbar();
84 JButton runButton = UISupport.createToolbarButton( runScriptAction );
85 toolBar.add( runButton );
86 toolBar.add( Box.createHorizontalGlue() );
87 JLabel label = new JLabel("<html>Script is invoked with <code>log</code>, <code>context</code>, " +
88 "<code>requestContext</code>, <code>mockRequest</code> and <code>mockResponse</code> variables</html>");
89 label.setToolTipText( label.getText() );
90 label.setMaximumSize( label.getPreferredSize() );
91
92 toolBar.add( label);
93 toolBar.addUnrelatedGap();
94 toolBar.addFixed( UISupport.createActionButton( 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 :
120 mockResponse.getMockResult().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 += name + " : " + values.get( name ) + "<br>";
137 }
138
139 msg +="</body></html>";
140
141 UISupport.showInfoMessage( msg );
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
185 @Override
186 public boolean isEnabledFor( EditorView<XmlDocument> view )
187 {
188 return true;
189 }
190 }