View Javadoc

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