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 2017/03/04 09:57:09 UTC

svn commit: r1785447 - in /jmeter/trunk: src/functions/org/apache/jmeter/functions/RegexFunction.java test/src/org/apache/jmeter/functions/TestRegexFunction.java

Author: pmouawad
Date: Sat Mar  4 09:57:08 2017
New Revision: 1785447

URL: http://svn.apache.org/viewvc?rev=1785447&view=rev
Log:
Fix sonar error on possible NPE by enforcing collection does not contain null values
Add Tests 

Modified:
    jmeter/trunk/src/functions/org/apache/jmeter/functions/RegexFunction.java
    jmeter/trunk/test/src/org/apache/jmeter/functions/TestRegexFunction.java

Modified: jmeter/trunk/src/functions/org/apache/jmeter/functions/RegexFunction.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/functions/org/apache/jmeter/functions/RegexFunction.java?rev=1785447&r1=1785446&r2=1785447&view=diff
==============================================================================
--- jmeter/trunk/src/functions/org/apache/jmeter/functions/RegexFunction.java (original)
+++ jmeter/trunk/src/functions/org/apache/jmeter/functions/RegexFunction.java Sat Mar  4 09:57:08 2017
@@ -30,8 +30,6 @@ import org.apache.jmeter.samplers.Sample
 import org.apache.jmeter.samplers.Sampler;
 import org.apache.jmeter.threads.JMeterVariables;
 import org.apache.jmeter.util.JMeterUtils;
-import org.slf4j.LoggerFactory;
-import org.slf4j.Logger;
 import org.apache.oro.text.MalformedCachePatternException;
 import org.apache.oro.text.regex.MatchResult;
 import org.apache.oro.text.regex.Pattern;
@@ -39,6 +37,8 @@ import org.apache.oro.text.regex.Pattern
 import org.apache.oro.text.regex.PatternMatcherInput;
 import org.apache.oro.text.regex.Perl5Compiler;
 import org.apache.oro.text.regex.Util;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 /**
  * Implements regular expression parsing of sample results and variables
  * @since 1.X
@@ -157,7 +157,9 @@ public class RegexFunction extends Abstr
             PatternMatcherInput input = new PatternMatcherInput(textToMatch);
             while (matcher.contains(input, searchPattern)) {
                 MatchResult match = matcher.getMatch();
-                collectAllMatches.add(match);
+                if(match != null) {
+                    collectAllMatches.add(match);
+                } 
             }
         } finally {
             if (name.length() > 0){
@@ -165,7 +167,7 @@ public class RegexFunction extends Abstr
             }
         }
 
-        if (collectAllMatches.size() == 0) {
+        if (collectAllMatches.isEmpty()) {
             return defaultValue;
         }
 
@@ -188,6 +190,9 @@ public class RegexFunction extends Abstr
         } else {
             try {
                 int index = Integer.parseInt(valueIndex) - 1;
+                if(index >= collectAllMatches.size()) {
+                    return defaultValue;
+                }
                 MatchResult result = collectAllMatches.get(index);
                 return generateResult(result, name, tmplt, vars);
             } catch (NumberFormatException e) {
@@ -195,18 +200,14 @@ public class RegexFunction extends Abstr
                 MatchResult result = collectAllMatches
                         .get((int) (collectAllMatches.size() * ratio + .5) - 1);
                 return generateResult(result, name, tmplt, vars);
-            } catch (IndexOutOfBoundsException e) {
-                return defaultValue;
             }
         }
 
     }
 
     private void saveGroups(MatchResult result, String namep, JMeterVariables vars) {
-        if (result != null) {
-            for (int x = 0; x < result.groups(); x++) {
-                vars.put(namep + "_g" + x, result.group(x)); //$NON-NLS-1$
-            }
+        for (int x = 0; x < result.groups(); x++) {
+            vars.put(namep + "_g" + x, result.group(x)); //$NON-NLS-1$
         }
     }
 

Modified: jmeter/trunk/test/src/org/apache/jmeter/functions/TestRegexFunction.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/functions/TestRegexFunction.java?rev=1785447&r1=1785446&r2=1785447&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/functions/TestRegexFunction.java (original)
+++ jmeter/trunk/test/src/org/apache/jmeter/functions/TestRegexFunction.java Sat Mar  4 09:57:08 2017
@@ -350,6 +350,52 @@ public class TestRegexFunction extends J
             String match = variable.execute(result, null);
             assertEquals("_pinposition2", match);
         }
+        
+        @Test
+        public void testExtractionIndexTooHigh() throws Exception {
+            params = new LinkedList<>();
+            params.add(new CompoundVariable("<value field=\"(pinposition\\d+)\">(\\d+)</value>"));
+            params.add(new CompoundVariable("_$1$"));
+            params.add(new CompoundVariable("10"));
+            params.add(new CompoundVariable(""));
+            params.add(new CompoundVariable("No Value Found"));
+            variable.setParameters(params);
+            String match = variable.execute(result, null);
+            assertEquals("No Value Found", match);
+        }
+        
+        @Test
+        public void testRandomExtraction() throws Exception {
+            params = new LinkedList<>();
+            params.add(new CompoundVariable("<company-xmlext-query-ret>(.+?)</company-xmlext-query-ret>"));
+            params.add(new CompoundVariable("$1$"));
+            params.add(new CompoundVariable("RAND"));
+            params.add(new CompoundVariable(""));
+            params.add(new CompoundVariable("No Value Found"));
+            variable.setParameters(params);
+            String match = variable.execute(result, null);
+            assertEquals("<row>" + "<value field=\"RetCode\">" + "LIS_OK</value><value"
+                    + " field=\"RetCodeExtension\"></value>" + "<value field=\"alias\"></value><value"
+                    + " field=\"positioncount\"></value>" + "<value field=\"invalidpincount\">0</value><value"
+                    + " field=\"pinposition1\">1</value><value" + " field=\"pinpositionvalue1\"></value><value"
+                    + " field=\"pinposition2\">5</value><value" + " field=\"pinpositionvalue2\"></value><value"
+                    + " field=\"pinposition3\">6</value><value" + " field=\"pinpositionvalue3\"></value>"
+                    + "</row>", match);
+        }
+        
+        
+        @Test(expected=NumberFormatException.class)
+        public void testExtractionIndexNotNumeric() throws Exception {
+            params = new LinkedList<>();
+            params.add(new CompoundVariable("<value field=\"(pinposition\\d+)\">(\\d+)</value>"));
+            params.add(new CompoundVariable("_$1$"));
+            params.add(new CompoundVariable("0.333a"));
+            params.add(new CompoundVariable(""));
+            params.add(new CompoundVariable("No Value Found"));
+            variable.setParameters(params);
+            String match = variable.execute(result, null);
+            assertEquals("No Value Found", match);
+        }
 
         @Test
         public void testVariableExtraction4() throws Exception {