You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by pm...@apache.org on 2016/12/21 12:49:02 UTC
svn commit: r1775409 - in /jmeter/trunk:
src/jorphan/org/apache/jorphan/util/JOrphanUtils.java
test/src/org/apache/jorphan/util/TestJorphanUtils.java
Author: pmouawad
Date: Wed Dec 21 12:49:02 2016
New Revision: 1775409
URL: http://svn.apache.org/viewvc?rev=1775409&view=rev
Log:
Bug 54525 - Search Feature : Enhance it with ability to replace
Part 1 : create replace that counts number of replacements
Bugzilla Id: 54525
Modified:
jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.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=1775409&r1=1775408&r2=1775409&view=diff
==============================================================================
--- jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java (original)
+++ jmeter/trunk/src/jorphan/org/apache/jorphan/util/JOrphanUtils.java Wed Dec 21 12:49:02 2016
@@ -29,6 +29,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
+import java.util.regex.Matcher;
import org.apache.commons.lang3.StringUtils;
@@ -616,4 +617,40 @@ public final class JOrphanUtils {
}
}
}
+
+ /**
+ * Replace in source all matches of regex by replacement taking
+ * into account case if caseSensitive is true
+ * @param source Source text
+ * @param regex Regular expression
+ * @param replacement Replacement text to which function applies a quoting
+ * @param caseSensitive is case taken into account
+ * @return array of Object where first row is the replaced text, second row is the number of replacement that occured
+ */
+ public static Object[] replaceAllWithRegex(
+ String source, String regex, String replacement, boolean caseSensitive) {
+ java.util.regex.Pattern pattern = caseSensitive ?
+ java.util.regex.Pattern.compile(regex) :
+ java.util.regex.Pattern.compile(regex, java.util.regex.Pattern.CASE_INSENSITIVE);
+ String previousText = null;
+ String newText = source;
+ replacement = Matcher.quoteReplacement(replacement);
+ Matcher matcher = pattern.matcher(newText);
+ int totalReplaced = 0;
+ while(!newText.equals(previousText)) {
+ previousText = newText;
+ newText = matcher.replaceFirst(replacement);
+ matcher = pattern.matcher(newText);
+ if(newText.equals(previousText)) {
+ break;
+ } else {
+ totalReplaced++;
+ }
+ }
+
+ return new Object[]{
+ newText,
+ totalReplaced
+ };
+ }
}
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=1775409&r1=1775408&r2=1775409&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jorphan/util/TestJorphanUtils.java (original)
+++ jmeter/trunk/test/src/org/apache/jorphan/util/TestJorphanUtils.java Wed Dec 21 12:49:02 2016
@@ -27,6 +27,7 @@ import static org.junit.Assert.assertFal
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import org.junit.Assert;
import org.junit.Test;
public class TestJorphanUtils {
@@ -356,4 +357,35 @@ public class TestJorphanUtils {
in = new StringBuilder("A");
assertEquals(" A", JOrphanUtils.rightAlign(in, 8).toString());
}
+
+ @Test
+ public void testReplaceAllWithRegex() {
+ Assert.assertArrayEquals(new Object[] {"toto", 0},
+ JOrphanUtils.replaceAllWithRegex("toto","ti", "ta", true));
+ Assert.assertArrayEquals(new Object[] {"toto", 0},
+ JOrphanUtils.replaceAllWithRegex("toto","TO", "TI", true));
+ Assert.assertArrayEquals(new Object[] {"TITI", 2},
+ JOrphanUtils.replaceAllWithRegex("toto","TO", "TI", false));
+ Assert.assertArrayEquals(new Object[] {"TITI", 2},
+ JOrphanUtils.replaceAllWithRegex("toto","to", "TI", true));
+ Assert.assertArrayEquals(new Object[] {"TITIti", 2},
+ JOrphanUtils.replaceAllWithRegex("tototi","to", "TI", true));
+ Assert.assertArrayEquals(new Object[] {"TOTIti", 1},
+ JOrphanUtils.replaceAllWithRegex("TOtoti","to", "TI", true));
+ Assert.assertArrayEquals(new Object[] {"TOTI", 1},
+ JOrphanUtils.replaceAllWithRegex("TOtoti","to.*", "TI", true));
+ Assert.assertArrayEquals(new Object[] {"TOTI", 1},
+ JOrphanUtils.replaceAllWithRegex("TOtoti","to.*ti", "TI", true));
+ Assert.assertArrayEquals(new Object[] {"TOTITITITIaTITITIti", 7},
+ JOrphanUtils.replaceAllWithRegex("TO1232a123ti","[0-9]", "TI", true));
+ Assert.assertArrayEquals(new Object[] {"TOTIaTIti", 2},
+ JOrphanUtils.replaceAllWithRegex("TO1232a123ti","[0-9]+", "TI", true));
+
+ Assert.assertArrayEquals(new Object[] {"TO${var}2a${var}ti", 2},
+ JOrphanUtils.replaceAllWithRegex("TO1232a123ti","123", "${var}", true));
+
+ Assert.assertArrayEquals(new Object[] {"TO${var}2a${var}ti${var2}", 2},
+ JOrphanUtils.replaceAllWithRegex("TO1232a123ti${var2}","123", "${var}", true));
+
+ }
}