use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class Jexl3Function method execute.
/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
//$NON-NLS-1$
String str = "";
CompoundVariable var = (CompoundVariable) values[0];
String exp = var.execute();
//$NON-NLS-1$
String varName = "";
if (values.length > 1) {
varName = ((CompoundVariable) values[1]).execute().trim();
}
JMeterContext jmctx = JMeterContextService.getContext();
JMeterVariables vars = jmctx.getVariables();
try {
JexlContext jc = new MapContext();
//$NON-NLS-1$
jc.set("log", log);
//$NON-NLS-1$
jc.set("ctx", jmctx);
//$NON-NLS-1$
jc.set("vars", vars);
//$NON-NLS-1$
jc.set("props", JMeterUtils.getJMeterProperties());
// Previously mis-spelt as theadName
//$NON-NLS-1$
jc.set("threadName", Thread.currentThread().getName());
//$NON-NLS-1$ (may be null)
jc.set("sampler", currentSampler);
//$NON-NLS-1$ (may be null)
jc.set("sampleResult", previousResult);
//$NON-NLS-1$
jc.set("OUT", System.out);
// Now evaluate the script, getting the result
JexlExpression e = getJexlEngine().createExpression(exp);
Object o = e.evaluate(jc);
if (o != null) {
str = o.toString();
}
if (vars != null && varName.length() > 0) {
// vars will be null on TestPlan
vars.put(varName, str);
}
} catch (Exception e) {
log.error("An error occurred while evaluating the expression \"" + exp + "\"\n", e);
}
return str;
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class CookieManager method add.
/**
* Add a cookie.
*
* @param c cookie to be added
*/
public void add(Cookie c) {
String cv = c.getValue();
String cn = c.getName();
// Can't have two matching cookies
removeMatchingCookies(c);
if (DELETE_NULL_COOKIES && (null == cv || cv.length() == 0)) {
if (log.isDebugEnabled()) {
log.debug("Dropping cookie with null value {}", c.toString());
}
} else {
if (log.isDebugEnabled()) {
log.debug("Add cookie to store {}", c.toString());
}
getCookies().addItem(c);
if (SAVE_COOKIES) {
JMeterContext context = getThreadContext();
if (context.isSamplingStarted()) {
context.getVariables().put(COOKIE_NAME_PREFIX + cn, cv);
}
}
}
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class TestLoopController method testBug54467.
@Test
public void testBug54467() throws Exception {
JMeterContext jmctx = JMeterContextService.getContext();
LoopController loop = new LoopController();
Map<String, String> variables = new HashMap<>();
ReplaceStringWithFunctions transformer = new ReplaceStringWithFunctions(new CompoundVariable(), variables);
jmctx.setVariables(new JMeterVariables());
StringProperty prop = new StringProperty(LoopController.LOOPS, "${__Random(1,12,)}");
JMeterProperty newProp = transformer.transformValue(prop);
newProp.setRunningVersion(true);
loop.setProperty(newProp);
loop.addTestElement(new TestSampler("random run"));
loop.setRunningVersion(true);
loop.initialize();
int loops = loop.getLoops();
for (int i = 0; i < loops; i++) {
Sampler s = loop.next();
assertNotNull(s);
}
assertNull(loop.next());
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class TestJSONPostProcessor method testProcessAllElementsMultipleMatches.
@Test
public void testProcessAllElementsMultipleMatches() {
JMeterContext context = JMeterContextService.getContext();
JSONPostProcessor processor = setupProcessor(context, "-1", true);
JMeterVariables vars = new JMeterVariables();
processor.setDefaultValues("NONE");
processor.setJsonPathExpressions("$[*]");
processor.setRefNames("varname");
processor.setScopeVariable("contentvar");
context.setVariables(vars);
vars.put("contentvar", "[\"one\", \"two\"]");
processor.process();
assertThat(vars.get("varname_1"), CoreMatchers.is("one"));
assertThat(vars.get("varname_2"), CoreMatchers.is("two"));
assertThat(vars.get("varname_matchNr"), CoreMatchers.is("2"));
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class TestJSONPostProcessor method testProcessRandomElementMultipleMatches.
@Test
public void testProcessRandomElementMultipleMatches() {
JMeterContext context = JMeterContextService.getContext();
JSONPostProcessor processor = setupProcessor(context, "0", true);
JMeterVariables vars = new JMeterVariables();
processor.setDefaultValues("NONE");
processor.setJsonPathExpressions("$[*]");
processor.setRefNames("varname");
processor.setScopeVariable("contentvar");
context.setVariables(vars);
vars.put("contentvar", "[\"one\", \"two\"]");
processor.process();
assertThat(vars.get("varname"), CoreMatchers.is(CoreMatchers.anyOf(CoreMatchers.is("one"), CoreMatchers.is("two"))));
assertThat(vars.get("varname_1"), CoreMatchers.is(CoreMatchers.nullValue()));
assertThat(vars.get("varname_2"), CoreMatchers.is(CoreMatchers.nullValue()));
assertThat(vars.get("varname_matchNr"), CoreMatchers.is(CoreMatchers.nullValue()));
}
Aggregations