Search in sources :

Example 76 with JMeterContext

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

the class TestJSONPostProcessor method testProcessAllElementsOneMatch.

@Test
void testProcessAllElementsOneMatch() {
    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\"]");
    processor.process();
    assertThat(vars.get("varname"), CoreMatchers.is(CoreMatchers.nullValue()));
    assertThat(vars.get("varname_1"), CoreMatchers.is("one"));
    assertThat(vars.get("varname_matchNr"), CoreMatchers.is("1"));
}
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.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 77 with JMeterContext

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

the class DurationAssertionTest method setUp.

@BeforeEach
public void setUp() {
    JMeterContext jmctx = JMeterContextService.getContext();
    assertion = new DurationAssertion();
    assertion.setThreadContext(jmctx);
    JMeterVariables vars = new JMeterVariables();
    jmctx.setVariables(vars);
    sampleResult = new SampleResult();
    sampleResult.setResponseData("response Data", null);
}
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)

Example 78 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 elementLogger = LoggerFactory.getLogger(getClass().getName() + "." + getName());
    // $NON-NLS-1$ (this name is fixed)
    bindings.put("log", elementLogger);
    // $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 79 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) {
            // NOSONAR last arg would be exception
            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) {
        // NOSONAR last arg would be exception
        log.warn("Problem setting one or more BeanShell variables {}", e.toString());
    }
    return bshInterpreter;
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterException(org.apache.jorphan.util.JMeterException) JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 80 with JMeterContext

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

the class RegexExtractor method process.

/**
 * Parses the response data using regular expressions and saving the results
 * into variables for use later in the test.
 *
 * @see org.apache.jmeter.processor.PostProcessor#process()
 */
@Override
public void process() {
    initTemplate();
    JMeterContext context = getThreadContext();
    SampleResult previousResult = context.getPreviousResult();
    if (previousResult == null) {
        return;
    }
    log.debug("RegexExtractor processing result");
    // Fetch some variables
    JMeterVariables vars = context.getVariables();
    String refName = getRefName();
    int matchNumber = getMatchNumber();
    final String defaultValue = getDefaultValue();
    if (defaultValue.length() > 0 || isEmptyDefaultValue()) {
        // Only replace default if it is provided or empty default value is explicitly requested
        vars.put(refName, defaultValue);
    }
    Perl5Matcher matcher = JMeterUtils.getMatcher();
    String regex = getRegex();
    Pattern pattern = null;
    try {
        pattern = JMeterUtils.getPatternCache().getPattern(regex, Perl5Compiler.READ_ONLY_MASK);
        List<MatchResult> matches = processMatches(pattern, regex, previousResult, matchNumber, vars);
        int prevCount = 0;
        String prevString = vars.get(refName + REF_MATCH_NR);
        if (prevString != null) {
            // ensure old value is not left defined
            vars.remove(refName + REF_MATCH_NR);
            try {
                prevCount = Integer.parseInt(prevString);
            } catch (NumberFormatException nfe) {
                log.warn("Could not parse number: '{}'", prevString);
            }
        }
        // Number of refName_n variable sets to keep
        int matchCount = 0;
        try {
            MatchResult match;
            if (matchNumber >= 0) {
                // Original match behaviour
                match = getCorrectMatch(matches, matchNumber);
                if (match != null) {
                    vars.put(refName, generateResult(match));
                    saveGroups(vars, refName, match);
                } else {
                    // refname has already been set to the default (if present)
                    removeGroups(vars, refName);
                }
            } else // < 0 means we save all the matches
            {
                // remove any single matches
                removeGroups(vars, refName);
                matchCount = matches.size();
                // Save the count
                vars.put(refName + REF_MATCH_NR, Integer.toString(matchCount));
                for (int i = 1; i <= matchCount; i++) {
                    match = getCorrectMatch(matches, i);
                    if (match != null) {
                        final String refName_n = refName + UNDERSCORE + i;
                        vars.put(refName_n, generateResult(match));
                        saveGroups(vars, refName_n, match);
                    }
                }
            }
            // Remove any left-over variables
            for (int i = matchCount + 1; i <= prevCount; i++) {
                final String refName_n = refName + UNDERSCORE + i;
                vars.remove(refName_n);
                removeGroups(vars, refName_n);
            }
        } catch (RuntimeException e) {
            log.warn("Error while generating result");
        }
    } catch (MalformedCachePatternException e) {
        log.error("Error in pattern: '{}'", regex);
    } finally {
        JMeterUtils.clearMatcherMemory(matcher, pattern);
    }
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) Pattern(org.apache.oro.text.regex.Pattern) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult)

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