From 364c0083517865e6934754ec058d549257e858a0 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Sun, 27 Sep 2020 14:10:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E9=87=8D=E6=9E=84=E6=8F=90=E5=8F=96=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/apache/jmeter/extractor/RegexExtractor.java | 11 +---------- .../org/apache/jmeter/extractor/XPath2Extractor.java | 2 +- .../extractor/json/jsonpath/JSONPostProcessor.java | 9 +++++---- .../java/org/apache/jmeter/samplers/SampleResult.java | 7 +++++-- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/backend/src/main/java/org/apache/jmeter/extractor/RegexExtractor.java b/backend/src/main/java/org/apache/jmeter/extractor/RegexExtractor.java index 356b19ba1d..606a77d652 100644 --- a/backend/src/main/java/org/apache/jmeter/extractor/RegexExtractor.java +++ b/backend/src/main/java/org/apache/jmeter/extractor/RegexExtractor.java @@ -83,7 +83,6 @@ public class RegexExtractor extends AbstractScopedTestElement implements PostPro private transient List 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"); } diff --git a/backend/src/main/java/org/apache/jmeter/extractor/XPath2Extractor.java b/backend/src/main/java/org/apache/jmeter/extractor/XPath2Extractor.java index 4946ecafe2..8d0412704a 100644 --- a/backend/src/main/java/org/apache/jmeter/extractor/XPath2Extractor.java +++ b/backend/src/main/java/org/apache/jmeter/extractor/XPath2Extractor.java @@ -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()); diff --git a/backend/src/main/java/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java b/backend/src/main/java/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java index f879b2f5fb..b035faadb0 100644 --- a/backend/src/main/java/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java +++ b/backend/src/main/java/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java @@ -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); } } diff --git a/backend/src/main/java/org/apache/jmeter/samplers/SampleResult.java b/backend/src/main/java/org/apache/jmeter/samplers/SampleResult.java index 4d1c3af09e..359a8615fa 100644 --- a/backend/src/main/java/org/apache/jmeter/samplers/SampleResult.java +++ b/backend/src/main/java/org/apache/jmeter/samplers/SampleResult.java @@ -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() {