| 1 | |
package kg.apc.jmeter.functions; |
| 2 | |
|
| 3 | |
import kg.apc.jmeter.JMeterPluginsUtils; |
| 4 | |
import org.apache.jmeter.engine.util.CompoundVariable; |
| 5 | |
import org.apache.jmeter.functions.AbstractFunction; |
| 6 | |
import org.apache.jmeter.functions.InvalidVariableException; |
| 7 | |
import org.apache.jmeter.samplers.SampleResult; |
| 8 | |
import org.apache.jmeter.samplers.Sampler; |
| 9 | |
import org.apache.jmeter.threads.JMeterVariables; |
| 10 | |
|
| 11 | |
import java.util.Collection; |
| 12 | |
import java.util.LinkedList; |
| 13 | |
import java.util.List; |
| 14 | |
|
| 15 | |
public class Env extends AbstractFunction { |
| 16 | |
|
| 17 | 1 | private static final List<String> desc = new LinkedList<String>(); |
| 18 | |
private static final String KEY = "__env"; |
| 19 | |
|
| 20 | |
|
| 21 | |
private static final int MIN_PARAMETER_COUNT = 1; |
| 22 | |
private static final int MAX_PARAMETER_COUNT = 3; |
| 23 | |
|
| 24 | |
static { |
| 25 | 1 | desc.add("Name of environment variable"); |
| 26 | 1 | desc.add("Name of variable in which to store the result (optional)"); |
| 27 | 1 | desc.add("Default value"); |
| 28 | 1 | } |
| 29 | |
|
| 30 | |
private CompoundVariable[] values; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | 9 | public Env() { |
| 36 | 9 | } |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
@Override |
| 42 | |
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) |
| 43 | |
throws InvalidVariableException { |
| 44 | 4 | String propertyName = values[0].execute(); |
| 45 | 4 | String propertyDefault = propertyName; |
| 46 | 4 | if (values.length > 2) { |
| 47 | 1 | propertyDefault = values[2].execute(); |
| 48 | |
} |
| 49 | 4 | String propertyValue = JMeterPluginsUtils.getEnvDefault(propertyName, propertyDefault); |
| 50 | 4 | if (values.length > 1) { |
| 51 | 2 | String variableName = values[1].execute(); |
| 52 | 2 | if (variableName.length() > 0) { |
| 53 | 1 | final JMeterVariables variables = getVariables(); |
| 54 | 1 | if (variables != null) { |
| 55 | 1 | variables.put(variableName, propertyValue); |
| 56 | |
} |
| 57 | |
} |
| 58 | |
} |
| 59 | 4 | return propertyValue; |
| 60 | |
} |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
@Override |
| 66 | |
public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException { |
| 67 | 9 | checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT); |
| 68 | 7 | values = parameters.toArray(new CompoundVariable[0]); |
| 69 | 7 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
@Override |
| 75 | |
public String getReferenceKey() { |
| 76 | 3 | return KEY; |
| 77 | |
} |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
@Override |
| 83 | |
public List<String> getArgumentDesc() { |
| 84 | 1 | return desc; |
| 85 | |
} |
| 86 | |
} |