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

View File

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

View File

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

View File

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