You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by se...@apache.org on 2008/02/26 15:36:23 UTC

svn commit: r631228 - in /jakarta/jmeter/trunk: docs/ docs/usermanual/ src/functions/org/apache/jmeter/functions/ test/src/org/apache/jmeter/functions/ xdocs/ xdocs/usermanual/

Author: sebb
Date: Tue Feb 26 06:36:21 2008
New Revision: 631228

URL: http://svn.apache.org/viewvc?rev=631228&view=rev
Log:
Add optional output variable name to Jexl function

Added:
    jakarta/jmeter/trunk/test/src/org/apache/jmeter/functions/TestJexlFunction.java   (with props)
Modified:
    jakarta/jmeter/trunk/docs/changes.html
    jakarta/jmeter/trunk/docs/usermanual/functions.html
    jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/JexlFunction.java
    jakarta/jmeter/trunk/xdocs/changes.xml
    jakarta/jmeter/trunk/xdocs/usermanual/functions.xml

Modified: jakarta/jmeter/trunk/docs/changes.html
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/docs/changes.html?rev=631228&r1=631227&r2=631228&view=diff
==============================================================================
--- jakarta/jmeter/trunk/docs/changes.html (original)
+++ jakarta/jmeter/trunk/docs/changes.html Tue Feb 26 06:36:21 2008
@@ -322,6 +322,16 @@
 						</li>
 									
 
+												<li	>
+								Bug 44378 - Turkish localisation
+						</li>
+									
+
+												<li	>
+								Add optional output variable name to Jexl function
+						</li>
+									
+
 						</ul>
 							  									 				<h4	>
 								Non-functional changes

Modified: jakarta/jmeter/trunk/docs/usermanual/functions.html
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/docs/usermanual/functions.html?rev=631228&r1=631227&r2=631228&view=diff
==============================================================================
--- jakarta/jmeter/trunk/docs/usermanual/functions.html (original)
+++ jakarta/jmeter/trunk/docs/usermanual/functions.html Tue Feb 26 06:36:21 2008
@@ -2534,6 +2534,14 @@
 											Yes
 								</td>
 		</tr>
+			<tr>
+			<td>Name of variable</td>
+			<td>						The name of the variable to set.
+			</td>
+			<td>
+											No
+								</td>
+		</tr>
 		</table>
 	</p>
 							  									 				<p	>

Modified: jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/JexlFunction.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/JexlFunction.java?rev=631228&r1=631227&r2=631228&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/JexlFunction.java (original)
+++ jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/JexlFunction.java Tue Feb 26 06:36:21 2008
@@ -28,6 +28,9 @@
 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.jorphan.logging.LoggingManager;
 import org.apache.log.Logger;
@@ -35,6 +38,7 @@
 /**
  * A function which understands Commons JEXL
  */
+// For unit tests, see TestJexlFunction
 public class JexlFunction extends AbstractFunction implements Serializable
 {
     private static final long serialVersionUID = 232L;
@@ -48,11 +52,12 @@
     static
     {
         desc.add(JMeterUtils.getResString("jexl_expression")); //$NON-NLS-1$
+        desc.add(JMeterUtils.getResString("function_name_paropt"));// $NON-NLS1$
     }
 
     private Object[] values;
 
-    public synchronized String execute(SampleResult result, Sampler sampler)
+    public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
             throws InvalidVariableException
     {
         String str = ""; //$NON-NLS-1$
@@ -60,15 +65,23 @@
         CompoundVariable var = (CompoundVariable) values[0];
         String exp = var.execute();
 
+        String varName = ""; //$NON-NLS-1$
+        if (values.length > 1) {
+            varName = ((CompoundVariable) values[1]).execute();
+        }
+
+        JMeterContext jmctx = JMeterContextService.getContext();
+        JMeterVariables vars = jmctx.getVariables();
+
         try
         {
             Expression e = ExpressionFactory.createExpression(exp);
             JexlContext jc = JexlHelper.createContext();
-            jc.getVars().put("ctx", sampler.getThreadContext()); //$NON-NLS-1$
-            jc.getVars().put("vars", getVariables()); //$NON-NLS-1$
-            jc.getVars().put("theadName", sampler.getThreadName()); //$NON-NLS-1$
-            jc.getVars().put("sampler", sampler); //$NON-NLS-1$
-            jc.getVars().put("sampleResult", result); //$NON-NLS-1$
+            jc.getVars().put("ctx", jmctx); //$NON-NLS-1$                
+            jc.getVars().put("vars", vars); //$NON-NLS-1$
+            jc.getVars().put("theadName", Thread.currentThread().getName()); //$NON-NLS-1$
+            jc.getVars().put("sampler", currentSampler); //$NON-NLS-1$ (may be null)
+            jc.getVars().put("sampleResult", previousResult); //$NON-NLS-1$ (may be null)
 
             // Now evaluate the expression, getting the result
             Object o = e.evaluate(jc);
@@ -76,10 +89,13 @@
             {
                 str = o.toString();
             }
+            if (vars != null && varName.length() > 0) {// vars will be null on TestPlan
+                vars.put(varName, str);
+            }
         } catch (Exception e)
         {
             log.error("An error occurred while evaluating the expression \""
-                    + exp + "\"");
+                    + exp + "\"\n",e);
         }
         return str;
     }
@@ -97,7 +113,7 @@
     public synchronized void setParameters(Collection parameters)
             throws InvalidVariableException
     {
-		checkParameterCount(parameters, 1);
+		checkParameterCount(parameters, 1, 2);
         values = parameters.toArray();
     }
 

Added: jakarta/jmeter/trunk/test/src/org/apache/jmeter/functions/TestJexlFunction.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/test/src/org/apache/jmeter/functions/TestJexlFunction.java?rev=631228&view=auto
==============================================================================
--- jakarta/jmeter/trunk/test/src/org/apache/jmeter/functions/TestJexlFunction.java (added)
+++ jakarta/jmeter/trunk/test/src/org/apache/jmeter/functions/TestJexlFunction.java Tue Feb 26 06:36:21 2008
@@ -0,0 +1,95 @@
+/*
+ * 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.util.Collection;
+import java.util.LinkedList;
+
+import org.apache.jmeter.engine.util.CompoundVariable;
+import org.apache.jmeter.junit.JMeterTestCase;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.threads.JMeterContext;
+import org.apache.jmeter.threads.JMeterContextService;
+import org.apache.jmeter.threads.JMeterVariables;
+
+public class TestJexlFunction extends JMeterTestCase {
+    JexlFunction function;
+
+    SampleResult result;
+
+    Collection params;
+
+    private JMeterVariables vars;
+
+    private JMeterContext jmctx = null;
+
+    public TestJexlFunction(String name) {
+        super(name);
+    }
+
+    public void setUp() {
+        function = new JexlFunction();
+        result = new SampleResult();
+        jmctx = JMeterContextService.getContext();
+        String data = "The quick brown fox";
+        result.setResponseData(data.getBytes());
+        vars = new JMeterVariables();
+        jmctx.setVariables(vars);
+        jmctx.setPreviousResult(result);
+        params = new LinkedList();
+    }
+
+    public void testParameterCount() throws Exception {
+        checkInvalidParameterCounts(function, 1, 2);
+    }
+
+    public void testSum() throws Exception {
+        params.add(new CompoundVariable("1+2+3"));
+        function.setParameters(params);
+        String ret = function.execute(result, null);
+        assertEquals("6", ret);
+    }
+
+    public void testSumVar() throws Exception {
+        params.add(new CompoundVariable("1+2+3"));
+        params.add(new CompoundVariable("TOTAL"));
+        function.setParameters(params);
+        String ret = function.execute(result, null);
+        assertEquals("6", ret);
+        assertEquals("6", vars.get("TOTAL"));
+    }
+
+    public void testReplace1() throws Exception {
+        params.add(new CompoundVariable(
+                "sampleResult.getResponseDataAsString().replaceAll('T','t')"));
+        function.setParameters(params);
+        String ret = function.execute(result, null);
+        assertEquals("the quick brown fox", ret);
+    }
+    
+    public void testReplace2() throws Exception {
+        vars.put("URL", "/query.cgi?s1=1&amp;s2=2&amp;s3=3");
+        params.add(new CompoundVariable("vars.get('URL').replaceAll('&amp;','&')"));
+        params.add(new CompoundVariable("URL"));
+        function.setParameters(params);
+        String ret = function.execute(result, null);
+        assertEquals("/query.cgi?s1=1&s2=2&s3=3", ret);
+        assertEquals(ret,vars.getObject("URL"));
+    }
+}

Propchange: jakarta/jmeter/trunk/test/src/org/apache/jmeter/functions/TestJexlFunction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/jmeter/trunk/test/src/org/apache/jmeter/functions/TestJexlFunction.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=631228&r1=631227&r2=631228&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Tue Feb 26 06:36:21 2008
@@ -95,6 +95,7 @@
 <li>Allow Global properties to be loaded from a file, e.g. -Gglobal.properties</li>
 <li>Add "Substring" option to Response Assertion</li>
 <li>Bug 44378 - Turkish localisation</li>
+<li>Add optional output variable name to Jexl function</li>
 </ul>
 
 <h4>Non-functional changes</h4>

Modified: jakarta/jmeter/trunk/xdocs/usermanual/functions.xml
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/usermanual/functions.xml?rev=631228&r1=631227&r2=631228&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/usermanual/functions.xml (original)
+++ jakarta/jmeter/trunk/xdocs/usermanual/functions.xml Tue Feb 26 06:36:21 2008
@@ -818,6 +818,7 @@
         <property name="Expression" required="Yes">
         The expression to be evaluated. 
         </property>
+        <property name="Name of variable" required="No">The name of the variable to set.</property>
 </properties>
 <p>
 JMeter 2.3 (and later versions) make the following variables available to the script:



---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org