Author: fschumacher
Date: Sat Jan 5 21:51:33 2019
New Revision: 1850515
URL: http://svn.apache.org/viewvc?rev=1850515&view=rev
Log:
Further code deduplication for Replaceable
Part of Bugzilla Id: 63025
Modified:
jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/HeaderManager.java
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java
jmeter/trunk/test/src/org/apache/jorphan/util/TestJorphanUtils.java
Modified: jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java
URL:
http://svn.apache.org/viewvc/jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java?rev=1850515&r1=1850514&r2=1850515&view=diff
==============================================================================
--- jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java
(original)
+++ jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java Sat Jan
5 21:51:33 2019
@@ -29,6 +29,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
+import java.util.function.Consumer;
import java.util.regex.Matcher;
import org.apache.commons.io.FileUtils;
@@ -680,4 +681,17 @@ public final class JOrphanUtils {
totalReplaced
};
}
+
+ public static int replaceValue(String regex, String replaceBy, boolean
caseSensitive, String value, Consumer<String> setter) {
+ if (StringUtils.isBlank(value)) {
+ return 0;
+ }
+ Object[] result = replaceAllWithRegex(value, regex, replaceBy,
caseSensitive);
+ int nbReplaced = ((Integer) result[1]).intValue();
+ if (nbReplaced <= 0) {
+ return 0;
+ }
+ setter.accept((String) result[0]);
+ return nbReplaced;
+ }
}
Modified:
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/HeaderManager.java
URL:
http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/HeaderManager.java?rev=1850515&r1=1850514&r2=1850515&view=diff
==============================================================================
---
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/HeaderManager.java
(original)
+++
jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/HeaderManager.java
Sat Jan 5 21:51:33 2019
@@ -29,7 +29,6 @@ import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
-import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.gui.Replaceable;
import org.apache.jmeter.testelement.TestElement;
@@ -291,17 +290,7 @@ public class HeaderManager extends Confi
for (int i = 0; i < hdrs.size(); i++) {
final JMeterProperty hdr = hdrs.get(i);
Header head = (Header) hdr.getObjectValue();
- String value = head.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];
- head.setValue(replacedText);
- totalReplaced += nbReplaced;
- }
- }
+ JOrphanUtils.replaceValue(regex, replaceBy, caseSensitive,
head.getValue(), head::setValue);
}
return totalReplaced;
}
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=1850515&r1=1850514&r2=1850515&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 21:51:33 2019
@@ -39,7 +39,6 @@ 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;
@@ -2095,28 +2094,15 @@ public abstract class HTTPSamplerBase ex
int totalReplaced = 0;
for (JMeterProperty jMeterProperty : getArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
- totalReplaced += replaceValue(regex, replaceBy, caseSensitive,
arg.getValue(), arg::setValue);
+ totalReplaced += JOrphanUtils.replaceValue(regex, replaceBy,
caseSensitive, arg.getValue(), arg::setValue);
}
- totalReplaced += replaceValue(regex, replaceBy, caseSensitive,
getPath(), this::setPath);
- totalReplaced += replaceValue(regex, replaceBy, caseSensitive,
getDomain(), this::setDomain);
+ totalReplaced += JOrphanUtils.replaceValue(regex, replaceBy,
caseSensitive, getPath(), this::setPath);
+ totalReplaced += JOrphanUtils.replaceValue(regex, replaceBy,
caseSensitive, getDomain(), this::setDomain);
for (String key: Arrays.asList(PORT, PROTOCOL)) {
- totalReplaced += replaceValue(regex, replaceBy, caseSensitive,
getPropertyAsString(key), s -> setProperty(key, s));
+ totalReplaced += JOrphanUtils.replaceValue(regex, replaceBy,
caseSensitive, getPropertyAsString(key), s -> setProperty(key, s));
}
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;
- }
}
Modified: jmeter/trunk/test/src/org/apache/jorphan/util/TestJorphanUtils.java
URL:
http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jorphan/util/TestJorphanUtils.java?rev=1850515&r1=1850514&r2=1850515&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jorphan/util/TestJorphanUtils.java
(original)
+++ jmeter/trunk/test/src/org/apache/jorphan/util/TestJorphanUtils.java Sat Jan
5 21:51:33 2019
@@ -334,4 +334,27 @@ public class TestJorphanUtils {
JOrphanUtils.replaceAllWithRegex("TO1232a123ti${var2}","123",
"${var}", true));
}
+
+ @Test
+ public void testReplaceValueWithNullValue() {
+ Assert.assertThat(Integer.valueOf(JOrphanUtils.replaceValue(null,
null, false, null, null)),
+ CoreMatchers.is(Integer.valueOf(0)));
+ }
+
+ @Test
+ public void testReplaceValueWithValidValueAndValidSetter() {
+ Holder h = new Holder();
+ Assert.assertThat(Integer.valueOf(JOrphanUtils.replaceValue("\\d+",
"${port}", true, "80", s -> h.value = s)),
+ CoreMatchers.is(Integer.valueOf(1)));
+ Assert.assertThat(h.value, CoreMatchers.is("${port}"));
+ }
+
+ private static class Holder {
+ String value;
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testReplaceValueWithNullSetterThatGetsCalled() {
+ JOrphanUtils.replaceValue("\\d+", "${port}", true, "80", null);
+ }
}