use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class JSR223TestElement method populateBindings.
/**
* Populate variables to be passed to scripts
* @param bindings Bindings
*/
protected void populateBindings(Bindings bindings) {
final String label = getName();
final String fileName = getFilename();
final String scriptParameters = getParameters();
// Use actual class name for log
final Logger logger = LoggerFactory.getLogger(getClass());
// $NON-NLS-1$ (this name is fixed)
bindings.put("log", logger);
// $NON-NLS-1$ (this name is fixed)
bindings.put("Label", label);
// $NON-NLS-1$ (this name is fixed)
bindings.put("FileName", fileName);
// $NON-NLS-1$ (this name is fixed)
bindings.put("Parameters", scriptParameters);
//$NON-NLS-1$
String[] args = JOrphanUtils.split(scriptParameters, " ");
// $NON-NLS-1$ (this name is fixed)
bindings.put("args", args);
// Add variables for access to context and variables
JMeterContext jmctx = JMeterContextService.getContext();
// $NON-NLS-1$ (this name is fixed)
bindings.put("ctx", jmctx);
JMeterVariables vars = jmctx.getVariables();
// $NON-NLS-1$ (this name is fixed)
bindings.put("vars", vars);
Properties props = JMeterUtils.getJMeterProperties();
// $NON-NLS-1$ (this name is fixed)
bindings.put("props", props);
// For use in debugging:
// NOSONAR $NON-NLS-1$ (this name is fixed)
bindings.put("OUT", System.out);
// Most subclasses will need these:
Sampler sampler = jmctx.getCurrentSampler();
// $NON-NLS-1$ (this name is fixed)
bindings.put("sampler", sampler);
SampleResult prev = jmctx.getPreviousResult();
// $NON-NLS-1$ (this name is fixed)
bindings.put("prev", prev);
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class BeanShellTestElement method getBeanShellInterpreter.
/**
* Get the interpreter and set up standard script variables.
* <p>
* Sets the following script variables:
* <ul>
* <li>ctx</li>
* <li>Label</li>
* <li>prev</li>
* <li>props</li>
* <li>vars</li>
* </ul>
* @return the interpreter
*/
protected BeanShellInterpreter getBeanShellInterpreter() {
if (isResetInterpreter()) {
try {
bshInterpreter.reset();
} catch (ClassNotFoundException e) {
log.error("Cannot reset BeanShell: " + e.toString());
}
}
JMeterContext jmctx = JMeterContextService.getContext();
JMeterVariables vars = jmctx.getVariables();
try {
//$NON-NLS-1$
bshInterpreter.set("ctx", jmctx);
//$NON-NLS-1$
bshInterpreter.set("Label", getName());
//$NON-NLS-1$
bshInterpreter.set("prev", jmctx.getPreviousResult());
bshInterpreter.set("props", JMeterUtils.getJMeterProperties());
//$NON-NLS-1$
bshInterpreter.set("vars", vars);
} catch (JMeterException e) {
log.warn("Problem setting one or more BeanShell variables " + e);
}
return bshInterpreter;
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class FunctionProperty method getStringValue.
/**
* Executes the function (and caches the value for the duration of the test
* iteration) if the property is a running version. Otherwise, the raw
* string representation of the function is provided.
*
* @see JMeterProperty#getStringValue()
*/
@Override
public String getStringValue() {
// Expensive, so
JMeterContext ctx = JMeterContextService.getContext();
// once
if (!isRunningVersion()) /*|| !ctx.isSamplingStarted()*/
{
log.debug("Not running version, return raw function string");
return function.getRawParameters();
}
if (!ctx.isSamplingStarted()) {
return function.execute();
}
log.debug("Running version, executing function");
int iter = ctx.getVariables() != null ? ctx.getVariables().getIteration() : -1;
if (iter < testIteration) {
testIteration = -1;
}
if (iter > testIteration || cacheValue == null) {
testIteration = iter;
cacheValue = function.execute();
}
return cacheValue;
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class TestIfController method testBug53768.
/**
* See Bug 53768
*
* @throws Exception
* if something fails
*/
@Test
public void testBug53768() throws Exception {
LoopController controller = new LoopController();
controller.setLoops(1);
controller.setContinueForever(false);
Arguments arguments = new Arguments();
arguments.addArgument("VAR1", "0", "=");
DebugSampler debugSampler1 = new DebugSampler();
debugSampler1.setName("VAR1 = ${VAR1}");
IfController ifCont = new IfController("true==false");
ifCont.setUseExpression(false);
ifCont.setEvaluateAll(false);
IfController ifCont2 = new IfController("true==true");
ifCont2.setUseExpression(false);
ifCont2.setEvaluateAll(false);
CounterConfig counterConfig = new CounterConfig();
counterConfig.setStart(1);
counterConfig.setIncrement(1);
counterConfig.setVarName("VAR1");
DebugSampler debugSampler2 = new DebugSampler();
debugSampler2.setName("VAR1 = ${VAR1}");
controller.addTestElement(arguments);
controller.addTestElement(debugSampler1);
controller.addTestElement(ifCont);
ifCont.addTestElement(ifCont2);
ifCont2.addTestElement(counterConfig);
controller.addTestElement(debugSampler2);
controller.initialize();
controller.setRunningVersion(true);
ifCont.setRunningVersion(true);
ifCont2.setRunningVersion(true);
counterConfig.setRunningVersion(true);
arguments.setRunningVersion(true);
debugSampler1.setRunningVersion(true);
debugSampler2.setRunningVersion(true);
ifCont2.addIterationListener(counterConfig);
JMeterVariables vars = new JMeterVariables();
JMeterContext jmctx = JMeterContextService.getContext();
jmctx.setVariables(vars);
vars.put("VAR1", "0");
try {
Sampler sampler = controller.next();
sampler.sample(null);
assertEquals("0", vars.get("VAR1"));
sampler = controller.next();
sampler.sample(null);
assertEquals("0", vars.get("VAR1"));
} catch (StackOverflowError e) {
fail("Stackoverflow occurred in testStackOverflow");
}
}
use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.
the class XmlAssertionTest method setUp.
@Before
public void setUp() {
JMeterContext jmctx = JMeterContextService.getContext();
assertion = new XMLAssertion();
assertion.setThreadContext(jmctx);
JMeterVariables vars = new JMeterVariables();
jmctx.setVariables(vars);
sampleResult = new SampleResult();
}
Aggregations