Search in sources :

Example 31 with JMeterVariables

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

the class TestResultAction method setUp.

@BeforeEach
public void setUp() {
    JMeterContext jmctx = JMeterContextService.getContext();
    resultAction = new ResultAction();
    resultAction.setThreadContext(jmctx);
    JMeterVariables vars = new JMeterVariables();
    jmctx.setVariables(vars);
    sampleResult = new SampleResult();
    sampleResult.setResponseData(data, null);
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult) ResultAction(org.apache.jmeter.reporters.ResultAction) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 32 with JMeterVariables

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

the class JMeterContextExtension method beforeEach.

@Override
public void beforeEach(ExtensionContext context) throws Exception {
    JMeterContext jMeterContext = JMeterContextService.getContext();
    jMeterContext.clear();
    jMeterContext.setVariables(new JMeterVariables());
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 33 with JMeterVariables

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

the class SimpleVariable method toString.

/**
 * @see org.apache.jmeter.functions.Function#execute
 */
@Override
public String toString() {
    String ret = null;
    JMeterVariables vars = getVariables();
    if (vars != null) {
        ret = vars.get(name);
    }
    if (ret == null) {
        return "${" + name + "}";
    }
    return ret;
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Example 34 with JMeterVariables

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

the class EvalVarFunction method execute.

/**
 * {@inheritDoc}
 */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String variableName = ((CompoundVariable) values[0]).execute();
    final JMeterVariables vars = getVariables();
    if (vars == null) {
        log.error("Variables have not yet been defined");
        return "**ERROR - see log file**";
    }
    String variableValue = vars.get(variableName);
    CompoundVariable cv = new CompoundVariable(variableValue);
    return cv.execute();
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Example 35 with JMeterVariables

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

the class FileToString method execute.

/**
 * {@inheritDoc}
 */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String fileName = ((CompoundVariable) values[0]).execute();
    // means platform default
    String encoding = null;
    if (values.length >= ENCODING) {
        encoding = ((CompoundVariable) values[ENCODING - 1]).execute().trim();
        if (encoding.length() <= 0) {
            // empty encoding, return to platform default
            encoding = null;
        }
    }
    // $NON-NLS-1$
    String myName = "";
    if (values.length >= PARAM_NAME) {
        myName = ((CompoundVariable) values[PARAM_NAME - 1]).execute().trim();
    }
    String myValue = ERR_IND;
    try {
        File file = new File(fileName);
        if (file.exists() && file.canRead()) {
            myValue = FileUtils.readFileToString(new File(fileName), encoding);
        } else {
            log.warn("Could not read open: {} ", fileName);
        }
    } catch (IOException e) {
        log.warn("Could not read file: {} {}", fileName, e.getMessage(), e);
    }
    if (myName.length() > 0) {
        JMeterVariables vars = getVariables();
        if (vars != null) {
            // Can be null if called from Config item testEnded() method
            vars.put(myName, myValue);
        }
    }
    if (log.isDebugEnabled()) {
        // $NON-NLS-1$
        log.debug("{} name: {} value: {}", Thread.currentThread().getName(), myName, myValue);
    }
    return myValue;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) IOException(java.io.IOException) File(java.io.File)

Aggregations

JMeterVariables (org.apache.jmeter.threads.JMeterVariables)193 SampleResult (org.apache.jmeter.samplers.SampleResult)71 JMeterContext (org.apache.jmeter.threads.JMeterContext)64 BeforeEach (org.junit.jupiter.api.BeforeEach)46 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)27 Test (org.junit.jupiter.api.Test)24 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)19 Test (org.junit.Test)16 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)11 Before (org.junit.Before)10 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)6 TestPlan (org.apache.jmeter.testelement.TestPlan)6 HashMap (java.util.HashMap)5 ValueReplacer (org.apache.jmeter.engine.util.ValueReplacer)5 URL (java.net.URL)4 Arguments (org.apache.jmeter.config.Arguments)4 DebugSampler (org.apache.jmeter.sampler.DebugSampler)4