View | Details | Raw Unified | Return to bug 62178
Collapse All | Expand All

(-)src/functions/org/apache/jmeter/functions/Variable.java (-10 / +16 lines)
Lines 33-42 Link Here
33
 *
33
 *
34
 * Parameters:
34
 * Parameters:
35
 * - variable name
35
 * - variable name
36
 * - default value
36
 *
37
 *
37
 * Returns:
38
 * Returns:
38
 * - the variable value, but if not found
39
 * - the variable value, but if not found
39
 * - the variable name itself
40
 * - the default value if sent, and if not the variable name itself
40
 * @since 2.3RC3
41
 * @since 2.3RC3
41
 */
42
 */
42
public class Variable extends AbstractFunction {
43
public class Variable extends AbstractFunction {
Lines 47-73 Link Here
47
48
48
    // Number of parameters expected - used to reject invalid calls
49
    // Number of parameters expected - used to reject invalid calls
49
    private static final int MIN_PARAMETER_COUNT = 1;
50
    private static final int MIN_PARAMETER_COUNT = 1;
50
    private static final int MAX_PARAMETER_COUNT = 1;
51
    private static final int MAX_PARAMETER_COUNT = 2;
51
52
52
    static {
53
    static {
53
        desc.add(JMeterUtils.getResString("variable_name_param")); //$NON-NLS-1$
54
    	desc.add(JMeterUtils.getResString("variable_name_param")); //$NON-NLS-1$
55
        desc.add(JMeterUtils.getResString("property_default_param")); //$NON-NLS-1$
54
    }
56
    }
55
57
56
    private Object[] values;
58
    private Object[] values;
57
59
58
    public Variable() {
60
    public Variable() { 
61
    	//used for test
59
    }
62
    }
60
63
61
    /** {@inheritDoc} */
64
    /** {@inheritDoc} */
62
    @Override
65
    @Override
63
    public String execute(SampleResult previousResult, Sampler currentSampler)
66
    public String execute(SampleResult previousResult, Sampler currentSampler)
64
            throws InvalidVariableException {
67
			throws InvalidVariableException {
65
        String variableName = ((CompoundVariable) values[0]).execute();
68
		String variableName = ((CompoundVariable) values[0]).execute();
66
        String variableValue = getVariables().get(variableName);
69
		String variableDefault = variableName;
67
        return variableValue == null? variableName : variableValue;
70
		if (values.length > 1) {
71
			variableDefault = ((CompoundVariable) values[1]).execute();
72
		}
73
		String variableValue = getVariables().get(variableName);
74
		return variableValue == null ? variableDefault : variableValue;
75
	}
68
76
69
    }
70
71
    /** {@inheritDoc} */
77
    /** {@inheritDoc} */
72
    @Override
78
    @Override
73
    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
79
    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
(-)test/src/org/apache/jmeter/functions/VariableTest.java (-1 / +11 lines)
Lines 57-64 Link Here
57
        parms = makeParams("V",null,null);
57
        parms = makeParams("V",null,null);
58
        r.setParameters(parms);
58
        r.setParameters(parms);
59
        s = r.execute(null,null);
59
        s = r.execute(null,null);
60
        assertEquals("A",s);
60
        assertEquals("A",s);        
61
        
61
        
62
        parms = makeParams("V","DEFAULT",null);
63
        r.setParameters(parms);
64
        s = r.execute(null,null);
65
        assertEquals("A",s);        
66
        
67
        parms = makeParams("EMPTY","DEFAULT",null);
68
        r.setParameters(parms);
69
        s = r.execute(null,null);
70
        assertEquals("DEFAULT",s);        
71
        
62
        parms = makeParams("X",null,null);
72
        parms = makeParams("X",null,null);
63
        r.setParameters(parms);
73
        r.setParameters(parms);
64
        s = r.execute(null,null);
74
        s = r.execute(null,null);
(-)xdocs/usermanual/functions.xml (+3 lines)
Lines 1244-1249 Link Here
1244
        <property name="Variable name" required="Yes">
1244
        <property name="Variable name" required="Yes">
1245
        The variable to be evaluated. 
1245
        The variable to be evaluated. 
1246
        </property>
1246
        </property>
1247
        <property name="Default value" required="No">
1248
        The default value in case no variable found, if it's empty and no variable found function returning the variable name 
1249
        </property>
1247
</properties>
1250
</properties>
1248
</component>
1251
</component>
1249
1252

Return to bug 62178