Skip to content

Instantly share code, notes, and snippets.

@vinsguru
Last active December 26, 2016 17:18
Show Gist options
  • Save vinsguru/d6e20f682fe58fb614c3c197f0a2dbe0 to your computer and use it in GitHub Desktop.
Save vinsguru/d6e20f682fe58fb614c3c197f0a2dbe0 to your computer and use it in GitHub Desktop.
package com.testautomationguru.plugins.functions;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterVariables;
public class StrJoin extends AbstractFunction {
private static final List < String > desc = new LinkedList < String > ();
private static final String MyFunctionName = "__Join";
static {
desc.add("String 1");
desc.add("String 2");
desc.add("Delimiter");
desc.add("Name of variable in which to store the result (optional)");
}
private Object[] values;
public StrJoin() {}
public List < String > getArgumentDesc() {
return desc;
}
@Override
public String execute(SampleResult arg0, Sampler arg1) throws InvalidVariableException {
JMeterVariables vars = getVariables();
String str1 = ((CompoundVariable) values[0]).execute().trim(); //parameter 1
String str2 = ((CompoundVariable) values[1]).execute().trim(); //parameter 2
String delimiter = "";
if (values.length > 2) {
delimiter = ((CompoundVariable) values[2]).execute(); //parameter 3 - delimiter could be a space - don't trim
}
String result = str1 + delimiter + str2;
//user might want the result in a variable
if (null != vars && values.length > 3) {
String userVariable = ((CompoundVariable) values[3]).execute().trim();
vars.put(userVariable, result); //store the result in the user defined variable
}
return result;
}
@Override
public String getReferenceKey() {
return MyFunctionName;
}
@Override
public void setParameters(Collection < CompoundVariable > parameters) throws InvalidVariableException {
values = parameters.toArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment