Search in sources :

Example 91 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class Jexl2Function 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
        Script e = getJexlEngine().createScript(exp);
        Object o = e.execute(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) Script(org.apache.commons.jexl2.Script) JMeterContext(org.apache.jmeter.threads.JMeterContext) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Example 92 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class JSONPostProcessor method process.

@Override
public void process() {
    JMeterContext context = getThreadContext();
    JMeterVariables vars = context.getVariables();
    List<String> jsonResponses = extractJsonResponse(context, vars);
    String[] refNames = getRefNames().split(SEPARATOR);
    String[] jsonPathExpressions = getJsonPathExpressions().split(SEPARATOR);
    String[] defaultValues = getDefaultValues().split(SEPARATOR);
    int[] matchNumbers = getMatchNumbersAsInt(defaultValues.length);
    validateSameLengthOfArguments(refNames, jsonPathExpressions, defaultValues);
    for (int i = 0; i < jsonPathExpressions.length; i++) {
        int matchNumber = matchNumbers[i];
        String currentRefName = refNames[i].trim();
        String currentJsonPath = jsonPathExpressions[i].trim();
        clearOldRefVars(vars, currentRefName);
        try {
            if (jsonResponses.isEmpty()) {
                handleEmptyResponse(vars, defaultValues[i], currentRefName);
            } else {
                List<Object> extractedValues = extractValues(jsonResponses, currentJsonPath);
                handleResult(vars, defaultValues[i], matchNumber, currentRefName, extractedValues);
            }
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.error("Error processing JSON content in {}, message: {}", getName(), e.getLocalizedMessage(), e);
            } else {
                log.error("Error processing JSON content in {}, message: {}", getName(), e.getLocalizedMessage());
            }
            // if something goes wrong, add default value
            vars.put(currentRefName, defaultValues[i]);
        }
    }
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) ParseException(java.text.ParseException)

Example 93 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class TestAction method sample.

/**
 * {@inheritDoc}
 */
@Override
public SampleResult sample(Entry e) {
    JMeterContext context = JMeterContextService.getContext();
    int target = getTarget();
    int action = getAction();
    if (action == PAUSE) {
        pause(getDurationAsString());
    } else if (action == STOP || action == STOP_NOW) {
        if (target == THREAD) {
            if (log.isInfoEnabled()) {
                log.info(MSG_STOP_CURRENT_THREAD, getName());
            }
            context.getThread().stop();
        } else if (target == TEST) {
            if (action == STOP_NOW) {
                if (log.isInfoEnabled()) {
                    log.info(MSG_STOP_CURRENT_THREAD, getName());
                }
                context.getThread().stop();
                if (log.isInfoEnabled()) {
                    log.info("Stopping all threads now from element {}", getName());
                }
                context.getEngine().stopTest();
            } else {
                if (log.isInfoEnabled()) {
                    log.info(MSG_STOP_CURRENT_THREAD, getName());
                }
                context.getThread().stop();
                if (log.isInfoEnabled()) {
                    log.info("Stopping all threads from element {}", getName());
                }
                context.getEngine().askThreadsToStop();
            }
        }
    } else if (action == RESTART_NEXT_LOOP) {
        log.info("Restarting next thread loop from element {}", getName());
        context.setTestLogicalAction(TestLogicalAction.START_NEXT_ITERATION_OF_THREAD);
    } else if (action == START_NEXT_ITERATION_CURRENT_LOOP) {
        log.info("Switching to next loop iteration from element {}", getName());
        context.setTestLogicalAction(TestLogicalAction.START_NEXT_ITERATION_OF_CURRENT_LOOP);
    } else if (action == BREAK_CURRENT_LOOP) {
        log.info("Breaking current loop from element {}", getName());
        context.setTestLogicalAction(TestLogicalAction.BREAK_CURRENT_LOOP);
    }
    // This means no sample is saved
    return null;
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 94 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class SplitFunctionTest method setUp.

@BeforeEach
public void setUp() {
    JMeterContext jmeterContext = JMeterContextService.getContext();
    jmeterContext.setVariables(new JMeterVariables());
    vars = jmeterContext.getVariables();
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 95 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class TestChangeCase method setUp.

@BeforeEach
public void setUp() {
    changeCase = new ChangeCase();
    JMeterContext jmctx = JMeterContextService.getContext();
    String data = "dummy data";
    result = new SampleResult();
    result.setResponseData(data, null);
    JMeterVariables vars = new JMeterVariables();
    jmctx.setVariables(vars);
    jmctx.setPreviousResult(result);
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

JMeterContext (org.apache.jmeter.threads.JMeterContext)98 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)64 SampleResult (org.apache.jmeter.samplers.SampleResult)47 Test (org.junit.Test)19 Test (org.junit.jupiter.api.Test)18 BeforeEach (org.junit.jupiter.api.BeforeEach)13 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)10 Sampler (org.apache.jmeter.samplers.Sampler)9 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)4 Properties (java.util.Properties)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 JMeterException (org.apache.jorphan.util.JMeterException)3 HashMap (java.util.HashMap)2 Bindings (javax.script.Bindings)2 Arguments (org.apache.jmeter.config.Arguments)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2