1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps;
14
15 import groovy.lang.Binding;
16 import groovy.lang.GroovyShell;
17 import groovy.lang.Script;
18
19 import javax.swing.ImageIcon;
20
21 import org.apache.log4j.Logger;
22
23 import com.eviware.soapui.SoapUI;
24 import com.eviware.soapui.config.TestStepConfig;
25 import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
26 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
27 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
28 import com.eviware.soapui.impl.wsdl.teststeps.actions.CloneTestStepAction;
29 import com.eviware.soapui.model.support.DefaultTestStepProperty;
30 import com.eviware.soapui.model.testsuite.TestRunContext;
31 import com.eviware.soapui.model.testsuite.TestRunner;
32 import com.eviware.soapui.model.testsuite.TestStepResult;
33 import com.eviware.soapui.model.testsuite.TestRunner.Status;
34 import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
35 import com.eviware.soapui.monitor.TestMonitor;
36 import com.eviware.soapui.support.ScriptingSupport;
37 import com.eviware.soapui.support.UISupport;
38 import com.eviware.soapui.support.action.ActionSupport;
39 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
40 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
41
42 public class WsdlGroovyScriptTestStep extends WsdlTestStep
43 {
44 private final static Logger logger = Logger.getLogger( "groovy.log" );
45 private String scriptText = "";
46 private Object scriptResult;
47 private ImageIcon failedIcon;
48 private ImageIcon okIcon;
49 private Binding binding;
50 private GroovyShell shell;
51 private Script script;
52
53 public WsdlGroovyScriptTestStep(WsdlTestCase testCase, TestStepConfig config)
54 {
55 super(testCase, config, false);
56
57 addAction( ActionSupport.SEPARATOR_ACTION );
58 addAction( new CloneTestStepAction( this, "Groovy Script" ) );
59 addAction( ActionSupport.SEPARATOR_ACTION );
60 addAction( new ShowOnlineHelpAction( HelpUrls.GROOVYSTEP_HELP_URL ));
61
62 okIcon = UISupport.createImageIcon("/groovy_script.gif");
63 setIcon( okIcon );
64 failedIcon = UISupport.createImageIcon( "/groovy_script_failed.gif" );
65
66 if( config.getConfig() == null )
67 {
68 saveScript( config );
69 }
70 else
71 {
72 readConfig( config );
73 }
74
75 addProperty( new DefaultTestStepProperty( "result", true,
76 new DefaultTestStepProperty.PropertyHandlerAdapter() {
77
78 public String getValue()
79 {
80 return scriptResult == null ? null : scriptResult.toString();
81 }
82 }, this ));
83
84 binding = new Binding();
85 shell = ScriptingSupport.createGroovyShell( binding);
86 }
87
88 public Logger getLogger()
89 {
90 SoapUI.ensureGroovyLog();
91 return logger;
92 }
93
94 private void readConfig(TestStepConfig config)
95 {
96 XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config.getConfig() );
97 scriptText = reader.readString( "script", "" );
98 }
99
100 private void saveScript(TestStepConfig config)
101 {
102 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
103 builder.add( "script", scriptText );
104 config.setConfig( builder.finish() );
105 }
106
107 public void resetConfigOnMove(TestStepConfig config)
108 {
109 super.resetConfigOnMove( config );
110 readConfig( config );
111 }
112
113 public TestStepResult run(TestRunner testRunner, TestRunContext context)
114 {
115 TestMonitor testMonitor = SoapUI.getTestMonitor();
116 boolean hasRunningLoadTest = testMonitor == null ? false : testMonitor.hasRunningLoadTest( getTestCase() );
117
118 SoapUI.ensureGroovyLog();
119
120 WsdlTestStepResult result = new WsdlTestStepResult( this );
121 Logger log = (Logger) context.getProperty( "log" );
122 if( log == null )
123 log = logger;
124
125 try
126 {
127 if( scriptText.trim().length() > 0 )
128 synchronized( this )
129 {
130 binding.setVariable("context", context);
131 binding.setVariable("testRunner", testRunner);
132 binding.setVariable( "log", log);
133
134 if( script == null )
135 {
136 script = shell.parse( scriptText );
137 script.setBinding( binding );
138 }
139
140 result.setTimeStamp( System.currentTimeMillis() );
141 result.startTimer();
142 scriptResult = script.run();
143 result.stopTimer();
144
145 if( scriptResult != null )
146 result.addMessage( "Script-result: " + scriptResult.toString() );
147 }
148
149
150 Status testRunnerStatus = testRunner.getStatus();
151 if( testRunnerStatus == Status.FAILED )
152 result.setStatus( TestStepStatus.FAILED );
153 else if( testRunnerStatus == Status.CANCELED )
154 result.setStatus( TestStepStatus.CANCELED );
155 else
156 result.setStatus( TestStepStatus.OK );
157 }
158 catch (Throwable e)
159 {
160 log.error( e );
161 result.stopTimer();
162 result.addMessage( e.getMessage() );
163 result.setError( e );
164 result.setStatus( TestStepStatus.FAILED );
165 }
166 finally
167 {
168 if( !hasRunningLoadTest )
169 setIcon( testRunner.getStatus() == Status.FAILED ? failedIcon : okIcon );
170
171 binding.getVariables().clear();
172 }
173
174 return result;
175 }
176
177 public String getScript()
178 {
179 return scriptText;
180 }
181
182 public void setScript( String scriptText )
183 {
184 if( scriptText.equals( this.scriptText ))
185 return;
186
187 this.scriptText = scriptText;
188 script = null;
189 saveScript( getConfig() );
190 }
191 }
192