Author: fschumacher Date: Sat Jan 5 12:06:49 2019 New Revision: 1850475 URL: http://svn.apache.org/viewvc?rev=1850475&view=rev Log: Extract duplicate code into private method
Refactoring to make bug 63025 easier to fix. Bugzilla Id: 63025 Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java?rev=1850475&r1=1850474&r2=1850475&view=diff ============================================================================== --- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java (original) +++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java Sat Jan 5 12:06:49 2019 @@ -39,6 +39,7 @@ import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import java.util.function.Consumer; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; @@ -2094,40 +2095,25 @@ public abstract class HTTPSamplerBase ex int totalReplaced = 0; for (JMeterProperty jMeterProperty : getArguments()) { HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue(); - String value = arg.getValue(); - if(!StringUtils.isEmpty(value)) { - Object[] result = JOrphanUtils.replaceAllWithRegex(value, regex, replaceBy, caseSensitive); - // check if there is anything to replace - int nbReplaced = ((Integer)result[1]).intValue(); - if (nbReplaced>0) { - String replacedText = (String) result[0]; - arg.setValue(replacedText); - totalReplaced += nbReplaced; - } - } - } - String value = getPath(); - if(!StringUtils.isEmpty(value)) { - Object[] result = JOrphanUtils.replaceAllWithRegex(value, regex, replaceBy, caseSensitive); - // check if there is anything to replace - int nbReplaced = ((Integer)result[1]).intValue(); - if (nbReplaced>0) { - String replacedText = (String) result[0]; - setPath(replacedText); - totalReplaced += nbReplaced; - } + totalReplaced += replaceValue(regex, replaceBy, caseSensitive, arg.getValue(), arg::setValue); } - if(!StringUtils.isEmpty(getDomain())) { - Object[] result = JOrphanUtils.replaceAllWithRegex(getDomain(), regex, replaceBy, caseSensitive); - // check if there is anything to replace - int nbReplaced = ((Integer)result[1]).intValue(); - if (nbReplaced>0) { - String replacedText = (String) result[0]; - setDomain(replacedText); - totalReplaced += nbReplaced; - } - } + totalReplaced += replaceValue(regex, replaceBy, caseSensitive, getPath(), this::setPath); + totalReplaced += replaceValue(regex, replaceBy, caseSensitive, getDomain(), this::setDomain); + return totalReplaced; } + + private int replaceValue(String regex, String replaceBy, boolean caseSensitive, String value, Consumer<String> setter) { + if (StringUtils.isBlank(value)) { + return 0; + } + Object[] result = JOrphanUtils.replaceAllWithRegex(value, regex, replaceBy, caseSensitive); + int nbReplaced = ((Integer) result[1]).intValue(); + if (nbReplaced <= 0) { + return 0; + } + setter.accept((String) result[0]); + return nbReplaced; + } }