Search in sources :

Example 31 with JMeterContext

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);
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult) Logger(org.slf4j.Logger) Properties(java.util.Properties)

Example 32 with JMeterContext

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;
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterException(org.apache.jorphan.util.JMeterException) JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 33 with JMeterContext

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;
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 34 with JMeterContext

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");
    }
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) DebugSampler(org.apache.jmeter.sampler.DebugSampler) JMeterContext(org.apache.jmeter.threads.JMeterContext) CounterConfig(org.apache.jmeter.modifiers.CounterConfig) DebugSampler(org.apache.jmeter.sampler.DebugSampler) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Sampler(org.apache.jmeter.samplers.Sampler) Arguments(org.apache.jmeter.config.Arguments) Test(org.junit.Test)

Example 35 with JMeterContext

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();
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult) Before(org.junit.Before)

Aggregations

JMeterContext (org.apache.jmeter.threads.JMeterContext)47 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)33 SampleResult (org.apache.jmeter.samplers.SampleResult)21 Test (org.junit.Test)11 Sampler (org.apache.jmeter.samplers.Sampler)9 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)8 Before (org.junit.Before)8 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)7 Properties (java.util.Properties)3 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 JMeterException (org.apache.jorphan.util.JMeterException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)2 StringProperty (org.apache.jmeter.testelement.property.StringProperty)2 BeanShellInterpreter (org.apache.jmeter.util.BeanShellInterpreter)2 MatchResult (org.apache.oro.text.regex.MatchResult)2