mirror of https://github.com/apache/jmeter.git
Add longSum() function and some unit tests for it and intSum()
git-svn-id: https://svn.apache.org/repos/asf/jakarta/jmeter/trunk@598724 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f268d6ad82
commit
e761573740
|
|
@ -32,7 +32,7 @@ import org.apache.jmeter.util.JMeterUtils;
|
|||
/**
|
||||
* Provides an intSum function that adds two or more integer values.
|
||||
*
|
||||
* @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
|
||||
* @see LongSum
|
||||
*/
|
||||
public class IntSum extends AbstractFunction implements Serializable {
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* 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.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Provides a longSum function that adds two or more long values.
|
||||
* @see IntSum
|
||||
*/
|
||||
public class LongSum extends AbstractFunction implements Serializable {
|
||||
|
||||
private static final List desc = new LinkedList();
|
||||
|
||||
private static final String KEY = "__longSum"; //$NON-NLS-1$
|
||||
|
||||
static {
|
||||
desc.add(JMeterUtils.getResString("longsum_param_1")); //$NON-NLS-1$
|
||||
desc.add(JMeterUtils.getResString("longsum_param_2")); //$NON-NLS-1$
|
||||
desc.add(JMeterUtils.getResString("function_name_param")); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
private Object[] values;
|
||||
|
||||
/**
|
||||
* No-arg constructor.
|
||||
*/
|
||||
public LongSum() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone this Add object.
|
||||
*
|
||||
* @return A new Add object.
|
||||
* @throws CloneNotSupportedException
|
||||
*/
|
||||
public Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the function.
|
||||
*
|
||||
* @see Function#execute(SampleResult, Sampler)
|
||||
*/
|
||||
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
|
||||
throws InvalidVariableException {
|
||||
|
||||
JMeterVariables vars = getVariables();
|
||||
|
||||
long sum = 0;
|
||||
String varName = ((CompoundVariable) values[values.length - 1]).execute();
|
||||
|
||||
for (int i = 0; i < values.length - 1; i++) {
|
||||
sum += Long.parseLong(((CompoundVariable) values[i]).execute());
|
||||
}
|
||||
|
||||
String totalString = Long.toString(sum);
|
||||
vars.put(varName, totalString);
|
||||
|
||||
return totalString;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the parameters for the function.
|
||||
*
|
||||
* @see Function#setParameters(Collection)
|
||||
*/
|
||||
public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
|
||||
values = parameters.toArray();
|
||||
|
||||
if (values.length < 3) {
|
||||
throw new InvalidVariableException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the invocation key for this function.
|
||||
*
|
||||
* @see Function#getReferenceKey()
|
||||
*/
|
||||
public String getReferenceKey() {
|
||||
return KEY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the description of this function.
|
||||
*
|
||||
* @see Function#getArgumentDesc()
|
||||
*/
|
||||
public List getArgumentDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
|
@ -198,6 +198,10 @@ public class PackageTest extends JMeterTestCase {
|
|||
eval.addTest(new PackageTest("evalTest2"));
|
||||
allsuites.addTest(eval);
|
||||
|
||||
TestSuite intSum = new TestSuite("Sums");
|
||||
intSum.addTest(new PackageTest("sumTest"));
|
||||
allsuites.addTest(intSum);
|
||||
|
||||
return allsuites;
|
||||
}
|
||||
|
||||
|
|
@ -898,5 +902,53 @@ public class PackageTest extends JMeterTestCase {
|
|||
evalVar.setParameters(parms);
|
||||
s = evalVar.execute(null,null);
|
||||
assertEquals("select name from customers",s);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkInvalidParameterCounts(AbstractFunction func, int max) throws Exception {
|
||||
Collection parms = new LinkedList();
|
||||
for (int c=0; c <= max; c++){
|
||||
try {
|
||||
func.setParameters(parms);
|
||||
fail("Should have generated InvalidVariableException for "+c+" parameters");
|
||||
} catch (InvalidVariableException ignored) {
|
||||
}
|
||||
parms.add("");
|
||||
}
|
||||
}
|
||||
public void sumTest() throws Exception {
|
||||
IntSum is = new IntSum();
|
||||
checkInvalidParameterCounts(is,2);
|
||||
checkSum(is,"3", new String[]{"1","2"});
|
||||
checkSum(is,"1", new String[]{"-1","1","1","1","-1","0"});
|
||||
String maxIntVal = Integer.toString(Integer.MAX_VALUE);
|
||||
String minIntVal = Integer.toString(Integer.MIN_VALUE);
|
||||
checkSum(is,maxIntVal, new String[]{maxIntVal,"0"});
|
||||
checkSum(is,minIntVal, new String[]{maxIntVal,"1"}); // wrap-round check
|
||||
|
||||
is = null; // prevent accidental use below
|
||||
|
||||
LongSum ls = new LongSum();
|
||||
checkInvalidParameterCounts(ls,2);
|
||||
checkSum(ls,"3", new String[]{"1","2"});
|
||||
checkSum(ls,"1", new String[]{"-1","1","1","1","-1","0"});
|
||||
String maxIntVal_1 = Long.toString(1+(long)Integer.MAX_VALUE);
|
||||
checkSum(ls,maxIntVal, new String[]{maxIntVal,"0"});
|
||||
checkSum(ls,maxIntVal_1, new String[]{maxIntVal,"1"}); // no wrap-round check
|
||||
String maxLongVal = Long.toString(Long.MAX_VALUE);
|
||||
String minLongVal = Long.toString(Long.MIN_VALUE);
|
||||
checkSum(ls,maxLongVal, new String[]{maxLongVal,"0"});
|
||||
checkSum(ls,minLongVal, new String[]{maxLongVal,"1"}); // wrap-round check
|
||||
}
|
||||
|
||||
// Perform a sum and check the results
|
||||
private void checkSum(AbstractFunction func, String value, String [] addends) throws Exception {
|
||||
Collection parms = new LinkedList();
|
||||
for (int i=0; i< addends.length; i++){
|
||||
parms.add(new CompoundVariable(addends[i]));
|
||||
}
|
||||
parms.add(new CompoundVariable("Result"));
|
||||
func.setParameters(parms);
|
||||
assertEquals(value,func.execute(null,null));
|
||||
assertEquals(value,vars.getObject("Result"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@
|
|||
<h4>Improvements</h4>
|
||||
<ul>
|
||||
<li>CSV files can now handle fields with embedded delimiters.</li>
|
||||
<li>longSum() function added</li>
|
||||
</ul>
|
||||
|
||||
<h4>Non-functional changes</h4>
|
||||
|
|
|
|||
|
|
@ -78,7 +78,8 @@ Variables are local to a thread; properties are common to all threads.
|
|||
<tr><td> <a href="#__regexFunction">regexFunction</a></td><td>parse previous response using a regular expression</td></tr>
|
||||
<tr><td> <a href="#__counter">counter</a></td><td>generate an incrementing number</td></tr>
|
||||
<tr><td> <a href="#__threadNum">threadNum</a></td><td>get thread number</td></tr>
|
||||
<tr><td> <a href="#__intSum">intSum</a></td><td>add numbers</td></tr>
|
||||
<tr><td> <a href="#__intSum">intSum</a></td><td>add int numbers</td></tr>
|
||||
<tr><td> <a href="#__longSum">longSum</a></td><td>add long numbers</td></tr>
|
||||
<tr><td> <a href="#__StringFromFile">StringFromFile</a></td><td>read a line from a file</td></tr>
|
||||
<tr><td> <a href="#__machineName">machineName</a></td><td>get the local machine name</td></tr>
|
||||
<tr><td> <a href="#__javaScript">javaScript</a></td><td>process JavaScript (Mozilla Rhino)</td></tr>
|
||||
|
|
@ -279,7 +280,7 @@ This function does not work in any Configuration elements (e.g. User Defined Var
|
|||
</note>
|
||||
</component>
|
||||
|
||||
<component index="§-num;.5.4" name="__intSum">
|
||||
<component index="§-num;.5.4a" name="__intSum">
|
||||
<description><p>The intsum function can be used to compute the sum of two or more integer values.
|
||||
</p></description>
|
||||
|
||||
|
|
@ -292,6 +293,19 @@ This function does not work in any Configuration elements (e.g. User Defined Var
|
|||
</properties>
|
||||
</component>
|
||||
|
||||
<component index="§-num;.5.4b" name="__longSum">
|
||||
<description><p>The longSum function can be used to compute the sum of two or more long values.
|
||||
</p></description>
|
||||
|
||||
<properties>
|
||||
<property name="First argument" required="Yes">The first long value.</property>
|
||||
<property name="Second argument" required="Yes">The second long value.</property>
|
||||
<property name="nth argument" required="No">The nth long value.</property>
|
||||
<property name="last argument" required="Yes">A reference name for reusing the value
|
||||
computed by this function.</property>
|
||||
</properties>
|
||||
</component>
|
||||
|
||||
<!-- Alternate spelling -->
|
||||
<a name="_StringFromFile"/>
|
||||
<component index="§-num;.5.5" name="__StringFromFile">
|
||||
|
|
|
|||
Loading…
Reference in New Issue