| 1 | |
package kg.apc.jmeter.functions; |
| 2 | |
|
| 3 | |
import org.apache.jmeter.engine.util.CompoundVariable; |
| 4 | |
import org.apache.jmeter.functions.AbstractFunction; |
| 5 | |
import org.apache.jmeter.functions.InvalidVariableException; |
| 6 | |
import org.apache.jmeter.samplers.SampleResult; |
| 7 | |
import org.apache.jmeter.samplers.Sampler; |
| 8 | |
import org.apache.jmeter.threads.JMeterVariables; |
| 9 | |
|
| 10 | |
import java.util.Collection; |
| 11 | |
import java.util.LinkedList; |
| 12 | |
import java.util.List; |
| 13 | |
|
| 14 | |
public class If extends AbstractFunction { |
| 15 | 1 | private static final List<String> desc = new LinkedList<String>(); |
| 16 | |
private static final String KEY = "__if"; |
| 17 | |
|
| 18 | |
static { |
| 19 | 1 | desc.add("Actual value"); |
| 20 | 1 | desc.add("Expected value"); |
| 21 | 1 | desc.add("Result if actual == expected"); |
| 22 | 1 | desc.add("Result if actual != expected"); |
| 23 | 1 | desc.add("Name of variable in which to store the result (optional)"); |
| 24 | 1 | } |
| 25 | |
|
| 26 | |
private Object[] values; |
| 27 | |
|
| 28 | 4 | public If() { |
| 29 | 4 | } |
| 30 | |
|
| 31 | |
@Override |
| 32 | |
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) |
| 33 | |
throws InvalidVariableException { |
| 34 | |
|
| 35 | 2 | String actual = getParameter(0); |
| 36 | 2 | String expected = getParameter(1); |
| 37 | |
|
| 38 | 2 | String result = null; |
| 39 | 2 | if (actual.equals(expected)) { |
| 40 | 0 | result = getParameter(2).toString(); |
| 41 | |
} else { |
| 42 | 2 | result = getParameter(3).toString(); |
| 43 | |
} |
| 44 | |
|
| 45 | 2 | JMeterVariables vars = getVariables(); |
| 46 | 2 | if (vars != null && values.length > 4) { |
| 47 | 0 | String varName = getParameter(4).trim(); |
| 48 | 0 | vars.put(varName, result); |
| 49 | |
} |
| 50 | |
|
| 51 | 2 | return result; |
| 52 | |
} |
| 53 | |
|
| 54 | |
@Override |
| 55 | |
public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException { |
| 56 | 2 | checkMinParameterCount(parameters, 4); |
| 57 | 2 | values = parameters.toArray(); |
| 58 | 2 | } |
| 59 | |
|
| 60 | |
@Override |
| 61 | |
public String getReferenceKey() { |
| 62 | 1 | return KEY; |
| 63 | |
} |
| 64 | |
|
| 65 | |
@Override |
| 66 | |
public List<String> getArgumentDesc() { |
| 67 | 1 | return desc; |
| 68 | |
} |
| 69 | |
|
| 70 | |
private String getParameter(int i) { |
| 71 | 6 | return ((CompoundVariable) values[i]).execute(); |
| 72 | |
} |
| 73 | |
} |