Bug 62178 - Add default value to __V function

Contributed by Orimarko
Bugzilla Id: 62178

git-svn-id: https://svn.apache.org/repos/asf/jmeter/trunk@1827141 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Philippe Mouawad 2018-03-18 14:30:39 +00:00
parent d8033ba491
commit 6b9da6c0ce
4 changed files with 27 additions and 8 deletions

View File

@ -33,10 +33,11 @@ import org.apache.jmeter.util.JMeterUtils;
*
* Parameters:
* - variable name
* - default value
*
* Returns:
* - the variable value, but if not found
* - the variable name itself
* - the default value if set, and if not the variable name itself
* @since 2.3RC3
*/
public class Variable extends AbstractFunction {
@ -47,25 +48,29 @@ public class Variable extends AbstractFunction {
// Number of parameters expected - used to reject invalid calls
private static final int MIN_PARAMETER_COUNT = 1;
private static final int MAX_PARAMETER_COUNT = 1;
private static final int MAX_PARAMETER_COUNT = 2;
static {
desc.add(JMeterUtils.getResString("variable_name_param")); //$NON-NLS-1$
desc.add(JMeterUtils.getResString("property_default_param")); //$NON-NLS-1$
}
private Object[] values;
public Variable() {
public Variable() {
//used for test
}
/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler)
throws InvalidVariableException {
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
String variableName = ((CompoundVariable) values[0]).execute();
String variableDefault = variableName;
if (values.length > 1) {
variableDefault = ((CompoundVariable) values[1]).execute();
}
String variableValue = getVariables().get(variableName);
return variableValue == null? variableName : variableValue;
return variableValue == null ? variableDefault : variableValue;
}
/** {@inheritDoc} */

View File

@ -57,7 +57,17 @@ public class VariableTest extends JMeterTestCase {
parms = makeParams("V",null,null);
r.setParameters(parms);
s = r.execute(null,null);
assertEquals("A",s);
assertEquals("A",s);
parms = makeParams("V","DEFAULT",null);
r.setParameters(parms);
s = r.execute(null,null);
assertEquals("A",s);
parms = makeParams("EMPTY","DEFAULT",null);
r.setParameters(parms);
s = r.execute(null,null);
assertEquals("DEFAULT",s);
parms = makeParams("X",null,null);
r.setParameters(parms);

View File

@ -106,6 +106,7 @@ this behaviour, set <code>httpclient.reset_state_on_thread_group_iteration=false
<h3>Functions</h3>
<ul>
<li><bug>62178</bug>Add default value to <code>__V</code> function. Contributed by orimarko at gmail.com</li>
</ul>
<h3>I18N</h3>

View File

@ -1244,6 +1244,9 @@ i.intValue(); // or use i.longValue()
<property name="Variable name" required="Yes">
The variable to be evaluated.
</property>
<property name="Default value" required="No">
The default value in case no variable found, if it's empty and no variable found function returns the variable name
</property>
</properties>
</component>