refactor(接口测试): 重构提取内容相关方法

This commit is contained in:
fit2-zhao 2020-09-27 14:10:47 +08:00
parent 6ebac26151
commit 364c008351
4 changed files with 12 additions and 17 deletions

View File

@ -83,7 +83,6 @@ public class RegexExtractor extends AbstractScopedTestElement implements PostPro
private transient List<Object> template;
private JMeterVariables regexVars;
/**
* Parses the response data using regular expressions and saving the results
* into variables for use later in the test.
@ -93,7 +92,6 @@ public class RegexExtractor extends AbstractScopedTestElement implements PostPro
@Override
public void process() {
initTemplate();
regexVars = new JMeterVariables();
JMeterContext context = getThreadContext();
SampleResult previousResult = context.getPreviousResult();
if (previousResult == null) {
@ -109,7 +107,6 @@ public class RegexExtractor extends AbstractScopedTestElement implements PostPro
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);
regexVars.put(refName, defaultValue);
}
Perl5Matcher matcher = JMeterUtils.getMatcher();
@ -135,8 +132,6 @@ public class RegexExtractor extends AbstractScopedTestElement implements PostPro
match = getCorrectMatch(matches, matchNumber);
if (match != null) {
vars.put(refName, generateResult(match));
regexVars.put(refName, generateResult(match));
saveGroups(vars, refName, match);
} else {
// refname has already been set to the default (if present)
@ -147,15 +142,11 @@ public class RegexExtractor extends AbstractScopedTestElement implements PostPro
removeGroups(vars, refName); // remove any single matches
matchCount = matches.size();
vars.put(refName + REF_MATCH_NR, Integer.toString(matchCount));// Save the count
regexVars.put(refName + REF_MATCH_NR, Integer.toString(matchCount));// Save the count
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));
regexVars.put(refName_n, generateResult(match));
saveGroups(vars, refName_n, match);
}
}
@ -166,7 +157,7 @@ public class RegexExtractor extends AbstractScopedTestElement implements PostPro
vars.remove(refName_n);
removeGroups(vars, refName_n);
}
previousResult.addVars(regexVars);
previousResult.addVars(refName, vars.get(refName));
} catch (RuntimeException e) {
log.warn("Error while generating result");
}

View File

@ -152,7 +152,7 @@ public class XPath2Extractor
for (int i = matchCount + 2; i <= prevCount; i++) {
vars.remove(concat(refName, i));
}
previousResult.addVars(vars);
previousResult.addVars(refName,vars.get(refName));
} catch (Exception e) {// Saxon exception
if (log.isWarnEnabled()) {
log.warn("Exception while processing '{}', message:{}", getXPathQuery(), e.getMessage());

View File

@ -35,6 +35,7 @@ import org.slf4j.LoggerFactory;
/**
* JSON-PATH based extractor
*
* @since 3.0
*/
public class JSONPostProcessor
@ -89,7 +90,7 @@ public class JSONPostProcessor
}
}
SampleResult previousResult = context.getPreviousResult();
previousResult.addVars(vars);
previousResult.addVars(currentRefName, vars.get(currentRefName));
} catch (Exception e) {
// if something wrong, default value added
if (log.isDebugEnabled()) {
@ -172,7 +173,7 @@ public class JSONPostProcessor
}
// extract at position
if (matchNumber > extractedValues.size()) {
if(log.isDebugEnabled()) {
if (log.isDebugEnabled()) {
log.debug(
"matchNumber({}) exceeds number of items found({}), default value will be used",
matchNumber, extractedValues.size());
@ -194,7 +195,7 @@ public class JSONPostProcessor
}
private void handleEmptyResponse(JMeterVariables vars, String[] defaultValues, int i, String currentRefName) {
if(log.isDebugEnabled()) {
if (log.isDebugEnabled()) {
log.debug("Response or source variable is null or empty for {}", getName());
}
vars.put(currentRefName, defaultValues[i]);
@ -221,7 +222,7 @@ public class JSONPostProcessor
private void clearOldRefVars(JMeterVariables vars, String refName) {
vars.remove(refName + REF_MATCH_NR);
for (int i=1; vars.get(refName + "_" + i) != null; i++) {
for (int i = 1; vars.get(refName + "_" + i) != null; i++) {
vars.remove(refName + "_" + i);
}
}

View File

@ -1612,8 +1612,11 @@ public class SampleResult implements Serializable, Cloneable, Searchable {
this.testLogicalAction = testLogicalAction;
}
public void addVars(JMeterVariables vars) {
this.vars = vars;
public void addVars(String key, String value) {
if (this.vars == null) {
this.vars = new JMeterVariables();
}
this.vars.put(key, value);
}
public JMeterVariables getVars() {