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"));
}
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);
}
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);
}
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;
}
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);
}
}
Aggregations