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