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