Search in sources :

Example 21 with JMeterContext

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;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 22 with JMeterContext

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

Example 23 with JMeterContext

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());
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) ReplaceStringWithFunctions(org.apache.jmeter.engine.util.ReplaceStringWithFunctions) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HashMap(java.util.HashMap) StringProperty(org.apache.jmeter.testelement.property.StringProperty) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Sampler(org.apache.jmeter.samplers.Sampler) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.Test)

Example 24 with JMeterContext

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"));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONPostProcessor(org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor) Test(org.junit.Test)

Example 25 with JMeterContext

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()));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONPostProcessor(org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor) Test(org.junit.Test)

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