private void saveVars(JMeterVariables vars){ for(int i = 0; i < variableNames.length; i++){ values[i] = vars.get(variableNames[i]); } }
@Override public String get(String key) { return variables.get(key); }
/** * Get from vars the values of variableName (can be missing, contain 1 value or contain multiple values (_matchNr)) * and stores them in results * @param variableName String * @param vars {@link JMeterVariables} * @param results {@link List} where results are stored */ private void extractVariableValuesToList(String variableName, JMeterVariables vars, List<String> results) { String matchNumberAsStr = vars.get(variableName+"_matchNr"); int matchNumber = 0; if(!StringUtils.isEmpty(matchNumberAsStr)) { matchNumber = Integer.parseInt(matchNumberAsStr); } if(matchNumber > 0) { for (int i = 1; i <= matchNumber; i++) { results.add(vars.get(variableName+"_"+i)); } } else { String value = vars.get(variableName); if(!StringUtils.isEmpty(value)) { results.add(value); } } }
private Map<String, String> buildParamsMap(){ String regExRefName = getRegExRefName()+"_"; String grNames = getRegParamNamesGrNr(); String grValues = getRegExParamValuesGrNr(); JMeterVariables jmvars = getThreadContext().getVariables(); // verify if regex groups exists if(jmvars.get(regExRefName + MATCH_NR) == null || jmvars.get(regExRefName + 1 + REGEX_GROUP_SUFFIX + grNames) == null || jmvars.get(regExRefName + 1 + REGEX_GROUP_SUFFIX + grValues) == null){ return null; } int n = Integer.parseInt(jmvars.get(regExRefName + MATCH_NR)); Map<String, String> map = new HashMap<>(n); for(int i=1; i<=n; i++){ map.put(jmvars.get(regExRefName + i + REGEX_GROUP_SUFFIX + grNames), jmvars.get(regExRefName + i + REGEX_GROUP_SUFFIX + grValues)); } return map; }
/** * @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; }
@Override public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { String variableName = values[0].execute(); JMeterVariables jMeterVariables = getVariables(); if(jMeterVariables != null) { String variableValue = jMeterVariables.get(variableName); return Boolean.toString(variableValue != null); } else { return Boolean.FALSE.toString(); } }
private int removePrevCount(JMeterVariables vars, String refName) { 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) { if (log.isWarnEnabled()) { log.warn("{}: Could not parse number: '{}'.", getName(), prevString); } } } return prevCount; }
private void clearOldRefVars(JMeterVariables vars, String refName) { vars.remove(refName + REF_MATCH_NR); for (int i=1; vars.get(refName + "_" + i) != null; i++) { vars.remove(refName + "_" + i); } }
/** * Removes the variables:<br/> * basename_gn, where n=0...# of groups<br/> * basename_g = number of groups (apart from g0) */ private void removeGroups(JMeterVariables vars, String basename) { StringBuilder buf = new StringBuilder(); buf.append(basename); buf.append("_g"); // $NON-NLS-1$ int pfxlen=buf.length(); // How many groups are there? int groups; try { groups=Integer.parseInt(vars.get(buf.toString())); } catch (NumberFormatException e) { groups=0; } vars.remove(buf.toString());// Remove the group count for (int i = 0; i <= groups; i++) { buf.append(i); vars.remove(buf.toString());// remove the g0,g1...gn vars buf.setLength(pfxlen); } }
/** * Get the next or only alias. * * @return the next or only alias. * @throws IllegalArgumentException * if {@link JmeterKeyStore#clientCertAliasVarName * clientCertAliasVarName} is not empty and no key for this * alias could be found */ public String getAlias() { if(!StringUtils.isEmpty(clientCertAliasVarName)) { // We return even if result is null String aliasName = JMeterContextService.getContext().getVariables().get(clientCertAliasVarName); if(StringUtils.isEmpty(aliasName)) { log.error("No var called '{}' found", clientCertAliasVarName); throw new IllegalArgumentException("No var called '"+clientCertAliasVarName+"' found"); } return aliasName; } int length = this.names.length; if (length == 0) { // i.e. is == null return null; } return this.names[getIndexAndIncrement(length)]; }
/** * Evaluate the condition, which can be: * blank or LAST = was the last sampler OK? * otherwise, evaluate the condition to see if it is not "false" * If blank, only evaluate at the end of the loop * * Must only be called at start and end of loop * * @param loopEnd - are we at loop end? * @return true means end of loop has been reached */ private boolean endOfLoop(boolean loopEnd) { if(breakLoop) { return true; } String cnd = getCondition().trim(); log.debug("Condition string: '{}'", cnd); boolean res; // If blank, only check previous sample when at end of loop if ((loopEnd && cnd.isEmpty()) || "LAST".equalsIgnoreCase(cnd)) {// $NON-NLS-1$ JMeterVariables threadVars = JMeterContextService.getContext().getVariables(); res = "false".equalsIgnoreCase(threadVars.get(JMeterThread.LAST_SAMPLE_OK));// $NON-NLS-1$ } else { // cnd may be null if next() called us res = "false".equalsIgnoreCase(cnd);// $NON-NLS-1$ } log.debug("Condition value: '{}'", res); return res; }
buf.append("_g"); // $NON-NLS-1$ int pfxlen=buf.length(); String prevString=vars.get(buf.toString()); int previous=0; if (prevString!=null){
/** {@inheritDoc} */ @Override public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { String variableName = ((CompoundVariable) values[0]).execute(); String variableDefault = variableName; if (values.length > 1) { variableDefault = ((CompoundVariable) values[1]).execute(); } String variableValue = getVariables().get(variableName); return variableValue == null ? variableDefault : variableValue; }
/** {@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(); }
private List<String> extractMatches(SampleResult previousResult, JMeterVariables vars, int matchNumber) { if (isScopeVariable()) { String inputString = vars.get(getVariableName()); if (inputString == null && log.isWarnEnabled()) { log.warn("No variable '{}' found to process by Boundary Extractor '{}', skipping processing", getVariableName(), getName()); } return extract(getLeftBoundary(), getRightBoundary(), matchNumber, inputString); } else { Stream<String> inputs = getSampleList(previousResult).stream().map(this::getInputString); return extract(getLeftBoundary(), getRightBoundary(), matchNumber, inputs); } }
private List<MatchResult> processMatches(Pattern pattern, String regex, SampleResult result, int matchNumber, JMeterVariables vars) { log.debug("Regex = '{}'", regex); Perl5Matcher matcher = JMeterUtils.getMatcher(); List<MatchResult> matches = new ArrayList<>(); int found = 0; if (isScopeVariable()){ String inputString=vars.get(getVariableName()); if(inputString == null) { if (log.isWarnEnabled()) { log.warn("No variable '{}' found to process by RegexExtractor '{}', skipping processing", getVariableName(), getName()); } return Collections.emptyList(); } matchStrings(matchNumber, matcher, pattern, matches, found, inputString); } else { List<SampleResult> sampleList = getSampleList(result); for (SampleResult sr : sampleList) { String inputString = getInputString(sr); found = matchStrings(matchNumber, matcher, pattern, matches, found, inputString); if (matchNumber > 0 && found == matchNumber){// no need to process further break; } } } return matches; }
List<String> result = new ArrayList<>(); if (isScopeVariable()){ String inputString=vars.get(getVariableName()); if (!StringUtils.isEmpty(inputString)) { getExtractorImpl().extract(expression, attribute, matchNumber, inputString, result, found, "-1");
res.setIdleTime(pauseTime + res.getIdleTime()); res.sampleEnd(); res.setSuccessful(TRUE.equals(JMeterContextService.getContext().getVariables().get(JMeterThread.LAST_SAMPLE_OK))); res.setResponseMessage( TransactionController.NUMBER_OF_SAMPLES_IN_TRANSACTION_PREFIX
String responseData; if (getSubject().equals(SUBJECT_VARIABLE)) { responseData = vars.get(getSrcVariableName()); } else { responseData = previousResult.getResponseDataAsString(); while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++; vars.put(this.getVar() + "_matchNr", "0"); int k = 1; while (vars.get(this.getVar() + "_" + k) != null) { vars.remove(this.getVar() + "_" + k); k++;
private String getStringToCheck(SampleResult response) { String toCheck; // The string to check (Url or data) // What are we testing against? if (isScopeVariable()){ toCheck = getThreadContext().getVariables().get(getVariableName()); } else if (isTestFieldResponseData()) { toCheck = response.getResponseDataAsString(); // (bug25052) } else if (isTestFieldResponseDataAsDocument()) { toCheck = Document.getTextFromDocument(response.getResponseData()); } else if (isTestFieldResponseCode()) { toCheck = response.getResponseCode(); } else if (isTestFieldResponseMessage()) { toCheck = response.getResponseMessage(); } else if (isTestFieldRequestHeaders()) { toCheck = response.getRequestHeaders(); } else if (isTestFieldRequestData()) { toCheck = response.getSamplerData(); } else if (isTestFieldResponseHeaders()) { toCheck = response.getResponseHeaders(); } else { // Assume it is the URL toCheck = ""; final URL url = response.getURL(); if (url != null){ toCheck = url.toString(); } } return toCheck; }