Bug 59991 - New function __groovy to evaluate Groovy Script
Contributed by Ubik Load Pack
Bugzilla Id: 59991

git-svn-id: https://svn.apache.org/repos/asf/jmeter/trunk@1756144 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/core/org/apache/jmeter/resources/messages.properties b/src/core/org/apache/jmeter/resources/messages.properties
index 561ff62..0f514f7 100644
--- a/src/core/org/apache/jmeter/resources/messages.properties
+++ b/src/core/org/apache/jmeter/resources/messages.properties
@@ -382,6 +382,7 @@
 graph_results_no_samples=No of Samples
 graph_results_throughput=Throughput
 graph_results_title=Graph Results
+groovy_function_expression=Expression to evaluate
 grouping_add_separators=Add separators between groups
 grouping_in_controllers=Put each group in a new controller
 grouping_in_transaction_controllers=Put each group in a new transaction controller
diff --git a/src/core/org/apache/jmeter/resources/messages_fr.properties b/src/core/org/apache/jmeter/resources/messages_fr.properties
index c20b0c1..feeb69a 100644
--- a/src/core/org/apache/jmeter/resources/messages_fr.properties
+++ b/src/core/org/apache/jmeter/resources/messages_fr.properties
@@ -375,6 +375,7 @@
 graph_results_no_samples=Nombre d'\u00E9chantillons
 graph_results_throughput=D\u00E9bit
 graph_results_title=Graphique de r\u00E9sultats
+groovy_function_expression=Expression \u00E0 \u00E9valuer
 grouping_add_separators=Ajouter des s\u00E9parateurs entre les groupes
 grouping_in_controllers=Mettre chaque groupe dans un nouveau contr\u00F4leur
 grouping_in_transaction_controllers=Mettre chaque groupe dans un nouveau contr\u00F4leur de transaction
diff --git a/src/functions/org/apache/jmeter/functions/Groovy.java b/src/functions/org/apache/jmeter/functions/Groovy.java
new file mode 100644
index 0000000..874ccc9
--- /dev/null
+++ b/src/functions/org/apache/jmeter/functions/Groovy.java
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.jmeter.functions;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Properties;
+
+import javax.script.Bindings;
+import javax.script.ScriptEngine;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.jmeter.engine.util.CompoundVariable;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.samplers.Sampler;
+import org.apache.jmeter.threads.JMeterContext;
+import org.apache.jmeter.threads.JMeterContextService;
+import org.apache.jmeter.threads.JMeterVariables;
+import org.apache.jmeter.util.JMeterUtils;
+import org.apache.jmeter.util.JSR223TestElement;
+import org.apache.jorphan.logging.LoggingManager;
+import org.apache.log.Logger;
+
+/**
+ * __groovy function 
+ * Provides a Groovy interpreter
+ * @since 3.1
+ */
+public class Groovy extends AbstractFunction {
+    private static final Logger log = LoggingManager.getLoggerForClass();
+
+    private static final String GROOVY_ENGINE_NAME = "groovy";
+    
+    private static final List<String> DESCRIPTION = new LinkedList<>();
+
+    private static final String KEY = "__groovy"; //$NON-NLS-1$
+
+    public static final String INIT_FILE = "groovy.utilities"; //$NON-NLS-1$
+
+    static {
+        DESCRIPTION.add(JMeterUtils.getResString("groovy_function_expression"));// $NON-NLS1$
+        DESCRIPTION.add(JMeterUtils.getResString("function_name_paropt"));// $NON-NLS1$
+    }
+
+    private Object[] values;
+    private ScriptEngine scriptEngine;
+
+
+    public Groovy() {
+    }
+    
+    /**
+     * Populate variables to be passed to scripts
+     * @param bindings Bindings
+     */
+    protected void populateBindings(Bindings bindings) {
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
+            throws InvalidVariableException {
+        Bindings bindings = scriptEngine.createBindings();
+        populateBindings(bindings);
+
+
+        String script = ((CompoundVariable) values[0]).execute();
+        String varName = ""; //$NON-NLS-1$
+        if (values.length > 1) {
+            varName = ((CompoundVariable) values[1]).execute().trim();
+        }
+
+        String resultStr = ""; //$NON-NLS-1$
+        try {
+
+            // Pass in some variables
+            if (currentSampler != null) {
+                bindings.put("sampler", currentSampler); // $NON-NLS-1$ 
+            }
+
+            if (previousResult != null) {
+                bindings.put("prev", previousResult); //$NON-NLS-1$
+            }
+            bindings.put("log", log); // $NON-NLS-1$ (this name is fixed)
+            // Add variables for access to context and variables
+            bindings.put("threadName", Thread.currentThread().getName());
+            JMeterContext jmctx = JMeterContextService.getContext();
+            bindings.put("ctx", jmctx); // $NON-NLS-1$ (this name is fixed)
+            JMeterVariables vars = jmctx.getVariables();
+            bindings.put("vars", vars); // $NON-NLS-1$ (this name is fixed)
+            Properties props = JMeterUtils.getJMeterProperties();
+            bindings.put("props", props); // $NON-NLS-1$ (this name is fixed)
+            // For use in debugging:
+            bindings.put("OUT", System.out); // $NON-NLS-1$ (this name is fixed)
+
+            
+            // Execute the script
+            Object out = scriptEngine.eval(script, bindings);
+            if (out != null) {
+                resultStr = out.toString();
+            }
+            
+            if (varName.length() > 0) {// vars will be null on TestPlan
+                if(vars != null) {
+                    vars.put(varName, resultStr);
+                }
+            }
+        } catch (Exception ex) // Mainly for bsh.EvalError
+        {
+            log.warn("Error running groovy script", ex);
+        }
+        if(log.isDebugEnabled()) {
+            log.debug("__groovy("+script+","+varName+")=" + resultStr);
+        }
+        return resultStr;
+
+    }
+
+    /*
+     * Helper method for use by scripts
+     *
+     */
+    public void log_info(String s) {
+        log.info(s);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
+        checkParameterCount(parameters, 1, 2);
+        values = parameters.toArray();
+        scriptEngine = JSR223TestElement.getInstance().getEngineByName(GROOVY_ENGINE_NAME); //$NON-NLS-N$
+
+        String fileName = JMeterUtils.getProperty(INIT_FILE);
+        if(!StringUtils.isEmpty(fileName)) {
+            File file = new File(fileName);
+            if(!(file.exists() && file.canRead())) {
+                // File maybe relative to JMeter home
+                File oldFile = file;
+                file = new File(JMeterUtils.getJMeterHome(), fileName);
+                if(!(file.exists() && file.canRead())) {
+                    throw new InvalidVariableException("Cannot read file, neither from:"+oldFile.getAbsolutePath()+
+                            ", nor from:"+file.getAbsolutePath()+", check property '"+INIT_FILE+"'");
+                }
+            }
+            try (FileReader fr = new FileReader(file); BufferedReader reader = new BufferedReader(fr)) {
+                Bindings bindings = scriptEngine.createBindings();
+                bindings.put("log", log);
+                scriptEngine.eval(reader, bindings);
+            } catch(Exception ex) {
+                throw new InvalidVariableException("Failed loading script:"+file.getAbsolutePath(), ex);
+            }
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String getReferenceKey() {
+        return KEY;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public List<String> getArgumentDesc() {
+        return DESCRIPTION;
+    }
+}
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index fb5e607..7922ca1 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -114,6 +114,7 @@
 <h3>Functions</h3>
 <ul>
     <li><bug>59963</bug>New Function <code>__RandomFromMultipleVars</code>: Ability to compute a random value from values of 1 or more variables. Contributed by Ubik Load Pack (support at ubikloadpack.com)</li>
+    <li><bug>59991</bug>New function __groovy to evaluate Groovy Script. Contributed by Ubik Load Pack (support at ubikloadpack.com)</li>
 </ul>
 
 <h3>I18N</h3>
diff --git a/xdocs/usermanual/functions.xml b/xdocs/usermanual/functions.xml
index 94a4d8c..106f7d8 100644
--- a/xdocs/usermanual/functions.xml
+++ b/xdocs/usermanual/functions.xml
@@ -125,6 +125,7 @@
         <tr><td>Calculation</td><td> <a href="#__RandomFromMultipleVars">RandomFromMultipleVars</a></td><td>extracts an element from the values of a set of variables separated by <code>|</code></td><td>3.1</td></tr>
         <tr><td>Calculation</td><td> <a href="#__RandomString">RandomString</a></td><td>generate a random string</td><td>2.6</td></tr>
         <tr><td>Calculation</td><td> <a href="#__UUID">UUID</a></td><td>generate a random type 4 UUID</td><td>2.9</td></tr>
+        <tr><td>Scripting</td><td> <a href="#__groovy">groovy</a></td><td>run a Groovy script</td><td>3.1</td></tr>
         <tr><td>Scripting</td><td> <a href="#__BeanShell">BeanShell</a></td><td>run a BeanShell script</td><td>1.X</td></tr>
         <tr><td>Scripting</td><td> <a href="#__javaScript">javaScript</a></td><td>process JavaScript (Mozilla Rhino)</td><td>1.9</td></tr>
         <tr><td>Scripting</td><td> <a href="#__jexl">jexl</a></td><td>evaluate a Commons Jexl expression. This function is DEPRECATED as of JMeter 3.0, it will be removed in 3.1 version.</td><td>jexl1(1.1)</td></tr>
@@ -909,6 +910,56 @@
 </note>
 </component>
 
+<component index="&sect-num;.5.13" name="__groovy">
+<description>
+    <p>
+    The g function evaluates <a href="http://groovy-lang.org/" >Apache Groovy</a> scripts passed to it, and returns the result.
+</p>
+<p>
+If the property "<code>groovy.utilities</code>" is defined, it will be loaded by the ScriptEngine. 
+This can be used to define common methods and variables. There is a
+sample init file in the bin directory: <code>utility.groovy</code>.
+</p>
+<p>
+The following variables are set before the script is executed:
+<ul>
+<li><code>log</code> - the logger for the BeanShell function (*)</li>
+<li><code>ctx</code> - the current JMeter context variable</li>
+<li><code>vars</code> - the current JMeter variables</li>
+<li><code>props</code> - JMeter Properties object</li>
+<li><code>threadName</code> - the threadName (String)</li>
+<li><code>sampler</code> - the current Sampler, if any</li>
+<li><code>prev</code> - the previous SampleResult, if any</li>
+<li><code>OUT</code> - System.out</li>
+</ul>
+(*) means that this is set before the init file, if any, is processed. 
+Other variables vary from invocation to invocation.
+</p>
+</description>
+
+<properties>
+        <property name="Expression to evaluate" required="Yes">A groovy script (not a file name)
+        <note>Argument values that themselves contain commas should be escaped as necessary. 
+        If you need to include a comma in your parameter value, escape it like this: '<code>\,</code>'</note>
+        </property>
+        <property name="Name of variable" required="No">A reference name for reusing the value
+               computed by this function.</property>
+        
+</properties>
+<p>
+Example:
+<dl>
+<dt><code>${__groovy(123*456)}</code></dt><dd>returns <code>56088</code></dd>
+<dt><code>${__groovy("${var}".substring(0\,2))}</code></dt><dd>If var's value is <code>JMeter</code>, it will return <code>JM</code> as it runs <code>String.substring(0,2)</code>. Note
+that <code>,</code> has been escaped to <code>\,</code> </dd>
+</dl>
+</p>
+<note>
+Remember to include any necessary quotes for text strings and JMeter variables that represent text strings.
+</note>
+</component>
+
+
 <component index="&sect-num;.5.14" name="__split">
 <description>
     <p>