Author: pmouawad
Date: Mon Oct 10 19:51:11 2011
New Revision: 1181176

URL: http://svn.apache.org/viewvc?rev=1181176&view=rev
Log:
Bug 52006 - Create a function RandomString to generate random Strings

Added:
    
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomString.java
   (with props)
Modified:
    jakarta/jmeter/trunk/docs/usermanual/functions.html
    
jakarta/jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties
    jakarta/jmeter/trunk/xdocs/changes.xml

Modified: jakarta/jmeter/trunk/docs/usermanual/functions.html
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/docs/usermanual/functions.html?rev=1181176&r1=1181175&r2=1181176&view=diff
==============================================================================
--- jakarta/jmeter/trunk/docs/usermanual/functions.html (original)
+++ jakarta/jmeter/trunk/docs/usermanual/functions.html Mon Oct 10 19:51:11 2011
@@ -485,6 +485,23 @@ generate a random number
 <tr>
 <td bgcolor="#a0ddf0"   valign="top" align="left">
 <font color="#000000" size="-1" face="arial,helvetica,sanserif">
+Calculation
+</font>
+</td>
+<td bgcolor="#a0ddf0"   valign="top" align="left">
+<font color="#000000" size="-1" face="arial,helvetica,sanserif">
+ <a href="#__Random">RandomString</a>
+</font>
+</td>
+<td bgcolor="#a0ddf0"   valign="top" align="left">
+<font color="#000000" size="-1" face="arial,helvetica,sanserif">
+generate a random string
+</font>
+</td>
+</tr>
+<tr>
+<td bgcolor="#a0ddf0"   valign="top" align="left">
+<font color="#000000" size="-1" face="arial,helvetica,sanserif">
 Scripting
 </font>
 </td>
@@ -1954,7 +1971,56 @@ No
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__CSVRead">19.5.8 __CSVRead</a>
+<a name="__RandomString">19.5.9 __RandomString</a>
+<a class="sectionlink" href="#__RandomString" title="Link to 
here">&para;</a></h3>
+</font>
+</td></tr>
+<tr><td>
+<p>
+The RandomString function returns a random String of length using characters 
in chars to use.
+</p>
+<p>
+<b>Parameters</b>
+<a name="__RandomString_parms"/>
+<a class="sectionlink" href="#__RandomString_parms" title="Link to 
here">&para;</a><table border="1" cellspacing="0" cellpadding="2">
+<tr><th>Attribute</th><th>Description</th><th>Required</th></tr>
+<tr>
+<td>Length</td>
+<td>A number
+</td>
+<td>
+Yes
+</td>
+</tr>
+<tr>
+<td>Characters to use</td>
+<td>A String contains chars used in generated chars
+</td>
+<td>
+No
+</td>
+</tr>
+<tr>
+<td>Variable Name</td>
+<td>A reference name for reusing the value
+                computed by this function.
+</td>
+<td>
+No
+</td>
+</tr>
+</table>
+</p>
+</td></tr>
+<tr><td><br></td></tr>
+</table>
+<hr>
+
+<table border="0" cellspacing="0" cellpadding="2">
+<tr><td>
+<font face="arial,helvetica,sanserif">
+<h3>
+<a name="__CSVRead">19.5.10 __CSVRead</a>
 <a class="sectionlink" href="#__CSVRead" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -2120,7 +2186,7 @@ StringFromFile
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__property">19.5.9 __property</a>
+<a name="__property">19.5.11 __property</a>
 <a class="sectionlink" href="#__property" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -2204,7 +2270,7 @@ No
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__P">19.5.10 __P</a>
+<a name="__P">19.5.12 __P</a>
 <a class="sectionlink" href="#__P" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -2297,7 +2363,7 @@ No
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__log">19.5.11 __log</a>
+<a name="__log">19.5.13 __log</a>
 <a class="sectionlink" href="#__log" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -2372,7 +2438,7 @@ For example:
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__logn">19.5.12 __logn</a>
+<a name="__logn">19.5.14 __logn</a>
 <a class="sectionlink" href="#__logn" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -2436,7 +2502,7 @@ For example:
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__BeanShell">19.5.13 __BeanShell</a>
+<a name="__BeanShell">19.5.15 __BeanShell</a>
 <a class="sectionlink" href="#__BeanShell" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -2595,7 +2661,7 @@ Remember to include any necessary quotes
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__split">19.5.14 __split</a>
+<a name="__split">19.5.16 __split</a>
 <a class="sectionlink" href="#__split" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -2729,7 +2795,7 @@ No
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__XPath">19.5.15 __XPath</a>
+<a name="__XPath">19.5.17 __XPath</a>
 <a class="sectionlink" href="#__XPath" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -2812,7 +2878,7 @@ Yes
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__setProperty">19.5.16 __setProperty</a>
+<a name="__setProperty">19.5.18 __setProperty</a>
 <a class="sectionlink" href="#__setProperty" title="Link to 
here">&para;</a></h3>
 </font>
 </td></tr>
@@ -2876,7 +2942,7 @@ No
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__time">19.5.17 __time</a>
+<a name="__time">19.5.19 __time</a>
 <a class="sectionlink" href="#__time" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -2962,7 +3028,7 @@ time.YMD=yyMMdd
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__jexl">19.5.18 __jexl</a>
+<a name="__jexl">19.5.20 __jexl</a>
 <a class="sectionlink" href="#__jexl" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -3122,7 +3188,7 @@ OUT - System.out - e.g. OUT.println("mes
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__V">19.5.19 __V</a>
+<a name="__V">19.5.21 __V</a>
 <a class="sectionlink" href="#__V" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -3187,7 +3253,7 @@ Yes
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__evalVar">19.5.20 __evalVar</a>
+<a name="__evalVar">19.5.22 __evalVar</a>
 <a class="sectionlink" href="#__evalVar" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -3235,7 +3301,7 @@ Yes
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__eval">19.5.21 __eval</a>
+<a name="__eval">19.5.23 __eval</a>
 <a class="sectionlink" href="#__eval" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -3316,7 +3382,7 @@ Yes
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__char">19.5.22 __char</a>
+<a name="__char">19.5.24 __char</a>
 <a class="sectionlink" href="#__char" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -3377,7 +3443,7 @@ ${__char(165)} = � (yen)
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__unescape">19.5.23 __unescape</a>
+<a name="__unescape">19.5.25 __unescape</a>
 <a class="sectionlink" href="#__unescape" title="Link to here">&para;</a></h3>
 </font>
 </td></tr>
@@ -3437,7 +3503,7 @@ ${__unescape(1\t2)} = 1[tab]2
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__unescapeHtml">19.5.24 __unescapeHtml</a>
+<a name="__unescapeHtml">19.5.26 __unescapeHtml</a>
 <a class="sectionlink" href="#__unescapeHtml" title="Link to 
here">&para;</a></h3>
 </font>
 </td></tr>
@@ -3517,7 +3583,7 @@ Yes
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__escapeHtml">19.5.25 __escapeHtml</a>
+<a name="__escapeHtml">19.5.27 __escapeHtml</a>
 <a class="sectionlink" href="#__escapeHtml" title="Link to 
here">&para;</a></h3>
 </font>
 </td></tr>
@@ -3581,7 +3647,7 @@ Yes
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__FileToString">19.5.26 __FileToString</a>
+<a name="__FileToString">19.5.28 __FileToString</a>
 <a class="sectionlink" href="#__FileToString" title="Link to 
here">&para;</a></h3>
 </font>
 </td></tr>
@@ -3649,7 +3715,7 @@ The file name, encoding and reference na
 <tr><td>
 <font face="arial,helvetica,sanserif">
 <h3>
-<a name="__samplerName">19.5.27 __samplerName</a>
+<a name="__samplerName">19.5.29 __samplerName</a>
 <a class="sectionlink" href="#__samplerName" title="Link to 
here">&para;</a></h3>
 </font>
 </td></tr>

Modified: 
jakarta/jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties?rev=1181176&r1=1181175&r2=1181176&view=diff
==============================================================================
--- 
jakarta/jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties 
(original)
+++ 
jakarta/jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties 
Mon Oct 10 19:51:11 2011
@@ -607,6 +607,8 @@ pt_br=Portugese (Brazilian)
 ramp_up=Ramp-Up Period (in seconds)\:
 random_control_title=Random Controller
 random_order_control_title=Random Order Controller
+random_string_length=Random string length
+random_string_chars_to_use=Chars to use for random string generation
 read_response_message=Read response is not checked. To see the response, 
please check the box in the sampler.
 read_response_note=If read response is unchecked, the sampler will not read 
the response
 read_response_note2=or set the SampleResult. This improves performance, but it 
means

Added: 
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomString.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomString.java?rev=1181176&view=auto
==============================================================================
--- 
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomString.java
 (added)
+++ 
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomString.java
 Mon Oct 10 19:51:11 2011
@@ -0,0 +1,130 @@
+/*
+ * 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 java.util.List;
+
+import org.apache.commons.lang.RandomStringUtils;
+import org.apache.commons.lang.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.JMeterVariables;
+import org.apache.jmeter.util.JMeterUtils;
+import org.apache.jorphan.logging.LoggingManager;
+import org.apache.log.Logger;
+
+/**
+ * Provides a Random function which returns a random long integer between a min
+ * (first argument) and a max (second argument).
+ *
+ */
+public class RandomString extends AbstractFunction {
+    private static final Logger log = LoggingManager.getLoggerForClass();
+
+    private static final List<String> desc = new LinkedList<String>();
+
+    private static final String KEY = "__RandomString"; //$NON-NLS-1$
+
+    static {
+        desc.add(JMeterUtils.getResString("random_string_length")); 
//$NON-NLS-1$
+        desc.add(JMeterUtils.getResString("random_string_chars_to_use")); 
//$NON-NLS-1$
+        desc.add(JMeterUtils.getResString("function_name_paropt")); 
//$NON-NLS-1$
+    }
+
+    private CompoundVariable[] values;
+
+    private static final int MAX_PARAM_COUNT = 3;
+
+    private static final int MIN_PARAM_COUNT = 1;
+    
+    private static final int CHARS = 2;
+
+    private static final int PARAM_NAME = 3;
+
+    /**
+     * No-arg constructor.
+     */
+    public RandomString() {
+        super();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public synchronized String execute(SampleResult previousResult, Sampler 
currentSampler)
+            throws InvalidVariableException {
+
+        int length = Integer.valueOf(values[0].execute());
+
+        String charsToUse = null;//means no restriction
+        if (values.length >= CHARS) {
+            charsToUse = (values[CHARS - 1]).execute().trim();
+            if (charsToUse.length() <= 0) { // empty chars, return to null
+                charsToUse = null;
+            }
+        }
+
+        String myName = "";//$NON-NLS-1$
+        if (values.length >= PARAM_NAME) {
+            myName = (values[PARAM_NAME - 1]).execute().trim();
+        }
+
+        String myValue = null;
+        if(StringUtils.isEmpty(charsToUse)) {
+            myValue = RandomStringUtils.random(length);
+        } else {
+            myValue = RandomStringUtils.random(length, charsToUse);
+        }
+ 
+        if (myName.length() > 0) {
+            JMeterVariables vars = getVariables();
+            if (vars != null) {// Can be null if called from Config item 
testEnded() method
+                vars.put(myName, myValue);
+            }
+        }
+
+        if (log.isDebugEnabled()) {
+            String tn = Thread.currentThread().getName();
+            log.debug(tn + " name:" //$NON-NLS-1$
+                    + myName + " value:" + myValue);//$NON-NLS-1$
+        }
+
+        return myValue;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public synchronized void setParameters(Collection<CompoundVariable> 
parameters) throws InvalidVariableException {
+        checkParameterCount(parameters, MIN_PARAM_COUNT, MAX_PARAM_COUNT);
+        values = parameters.toArray(new CompoundVariable[parameters.size()]);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String getReferenceKey() {
+        return KEY;
+    }
+
+    /** {@inheritDoc} */
+    public List<String> getArgumentDesc() {
+        return desc;
+    }
+}
\ No newline at end of file

Propchange: 
jakarta/jmeter/trunk/src/functions/org/apache/jmeter/functions/RandomString.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=1181176&r1=1181175&r2=1181176&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Mon Oct 10 19:51:11 2011
@@ -151,6 +151,7 @@ Mirror server now uses default port 8081
 
 <h3>Functions</h3>
 <ul>
+<li>Bug 52006 - Create a function RandomString to generate random Strings</li>
 </ul>
 
 <h3>I18N</h3>



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to