Hi Felix,
Thanks for patch.

I would create a bugzilla for this one and amend changes (incompatible
part) as it could introduce regressions in scripts if users relied on buggy
behaviour.

I can do it if needed.
Thx
Regards

On Thursday, December 1, 2016, <fschumac...@apache.org> wrote:

> Author: fschumacher
> Date: Thu Dec  1 18:39:36 2016
> New Revision: 1772247
>
> URL: http://svn.apache.org/viewvc?rev=1772247&view=rev
> Log:
> Clear leftover variables before extracting new ones in JSON Extractor.
> Based on a patch by Qi Chen (qi.chensh at ele.me)
>
> This closes #235 on github.
>
> Modified:
>     jmeter/trunk/src/components/org/apache/jmeter/extractor/json/jsonpath/
> JSONPostProcessor.java
>     jmeter/trunk/test/src/org/apache/jmeter/extractor/
> TestJSONPostProcessor.java
>     jmeter/trunk/xdocs/changes.xml
>
> Modified: jmeter/trunk/src/components/org/apache/jmeter/extractor/
> json/jsonpath/JSONPostProcessor.java
> URL: http://svn.apache.org/viewvc/jmeter/trunk/src/components/
> org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java?rev=
> 1772247&r1=1772246&r2=1772247&view=diff
> ============================================================
> ==================
> --- 
> jmeter/trunk/src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java
> (original)
> +++ 
> jmeter/trunk/src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java
> Thu Dec  1 18:39:36 2016
> @@ -99,6 +99,7 @@ public class JSONPostProcessor extends A
>              int matchNumber = matchNumbers[i];
>              String currentRefName = refNames[i].trim();
>              String currentJsonPath = jsonPathExpressions[i].trim();
> +            clearOldRefVars(vars, currentRefName);
>              try {
>                  if (jsonResponse.isEmpty()) {
>                      vars.put(currentRefName, defaultValues[i]);
> @@ -167,7 +168,9 @@ public class JSONPostProcessor extends A
>                                  vars.put(currentRefName + ALL_SUFFIX,
> vars.get(currentRefName));
>                              }
>                          }
> -                        vars.put(currentRefName + REF_MATCH_NR,
> Integer.toString(extractedValues.size()));
> +                        if (matchNumber != 0) {
> +                            vars.put(currentRefName + REF_MATCH_NR,
> Integer.toString(extractedValues.size()));
> +                        }
>                      }
>                  }
>              } catch (Exception e) {
> @@ -183,6 +186,13 @@ public class JSONPostProcessor extends A
>          }
>      }
>
> +    private void clearOldRefVars(JMeterVariables vars, String refName) {
> +        vars.remove(refName + REF_MATCH_NR);
> +        for (int i=1; vars.get(refName + "_" + i) != null; i++) {
> +            vars.remove(refName + "_" + i);
> +        }
> +    }
> +
>      private void placeObjectIntoVars(JMeterVariables vars, String
> currentRefName,
>              List<Object> extractedValues, int matchNr) {
>          vars.put(currentRefName,
>
> Modified: jmeter/trunk/test/src/org/apache/jmeter/extractor/
> TestJSONPostProcessor.java
> URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/
> apache/jmeter/extractor/TestJSONPostProcessor.java?
> rev=1772247&r1=1772246&r2=1772247&view=diff
> ============================================================
> ==================
> --- 
> jmeter/trunk/test/src/org/apache/jmeter/extractor/TestJSONPostProcessor.java
> (original)
> +++ 
> jmeter/trunk/test/src/org/apache/jmeter/extractor/TestJSONPostProcessor.java
> Thu Dec  1 18:39:36 2016
> @@ -73,6 +73,70 @@ public class TestJSONPostProcessor {
>      }
>
>      @Test
> +    public void testProcessRandomElementMultipleMatches() {
> +        JMeterContext context = JMeterContextService.getContext();
> +        JSONPostProcessor processor = setupProcessor(context, "0", true);
> +        JMeterVariables vars = new JMeterVariables();
> +        processor.setDefaultValues("NONE");
> +        processor.setJsonPathExpressions("$[*]");
> +        processor.setRefNames("varname");
> +        processor.setScopeVariable("contentvar");
> +        context.setVariables(vars);
> +        vars.put("contentvar", "[\"one\", \"two\"]");
> +        processor.process();
> +        assertThat(vars.get("varname"), 
> CoreMatchers.is(CoreMatchers.anyOf(CoreMatchers.is("one"),
> CoreMatchers.is("two"))));
> +        assertThat(vars.get("varname_1"), CoreMatchers.is(CoreMatchers.
> nullValue()));
> +        assertThat(vars.get("varname_2"), CoreMatchers.is(CoreMatchers.
> nullValue()));
> +        assertThat(vars.get("varname_matchNr"),
> CoreMatchers.is(CoreMatchers.nullValue()));
> +    }
> +
> +    @Test
> +    public void testPR235CaseEmptyResponse() {
> +        JMeterContext context = JMeterContextService.getContext();
> +        JSONPostProcessor processor = setupProcessor(context, "-1", true);
> +        JMeterVariables vars = new JMeterVariables();
> +        processor.setDefaultValues("NONE");
> +        processor.setJsonPathExpressions("$[*]");
> +        processor.setRefNames("varname");
> +        processor.setScopeVariable("contentvar");
> +        context.setVariables(vars);
> +        vars.put("contentvar", "[\"one\", \"two\"]");
> +        processor.process();
> +        assertThat(vars.get("varname_1"), CoreMatchers.is("one"));
> +        assertThat(vars.get("varname_2"), CoreMatchers.is("two"));
> +        assertThat(vars.get("varname_matchNr"), CoreMatchers.is("2"));
> +        vars.put("contentvar", "");
> +        processor.process();
> +        assertThat(vars.get("varname_matchNr"),
> CoreMatchers.is(CoreMatchers.nullValue()));
> +        assertThat(vars.get("varname_1"), CoreMatchers.is(CoreMatchers.
> nullValue()));
> +        assertThat(vars.get("varname_2"), CoreMatchers.is(CoreMatchers.
> nullValue()));
> +    }
> +
> +    @Test
> +    public void testPR235CaseMatchOneWithZero() {
> +        JMeterContext context = JMeterContextService.getContext();
> +        JSONPostProcessor processor = setupProcessor(context, "-1", true);
> +        JMeterVariables vars = new JMeterVariables();
> +        processor.setDefaultValues("NONE");
> +        processor.setJsonPathExpressions("$[*]");
> +        processor.setRefNames("varname");
> +        processor.setScopeVariable("contentvar");
> +        context.setVariables(vars);
> +        vars.put("contentvar", "[\"one\", \"two\"]");
> +        processor.process();
> +        assertThat(vars.get("varname_1"), CoreMatchers.is("one"));
> +        assertThat(vars.get("varname_2"), CoreMatchers.is("two"));
> +        assertThat(vars.get("varname_matchNr"), CoreMatchers.is("2"));
> +        vars.put("contentvar", "[\"A\", \"B\"]");
> +        processor.setMatchNumbers("0");
> +        processor.process();
> +        assertThat(vars.get("varname"), 
> CoreMatchers.is(CoreMatchers.anyOf(CoreMatchers.is("A"),
> CoreMatchers.is("B"))));
> +        assertThat(vars.get("varname_matchNr"),
> CoreMatchers.is(CoreMatchers.nullValue()));
> +        assertThat(vars.get("varname_1"), CoreMatchers.is(CoreMatchers.
> nullValue()));
> +        assertThat(vars.get("varname_2"), CoreMatchers.is(CoreMatchers.
> nullValue()));
> +    }
> +
> +    @Test
>      public void testBug59609() throws ParseException {
>          JMeterContext context = JMeterContextService.getContext();
>          JSONPostProcessor processor = setupProcessor(context, "0", false);
> @@ -91,8 +155,9 @@ public class TestJSONPostProcessor {
>
>          JSONParser parser = new JSONParser(0);
>          Object expectedValue = parser.parse(innerValue);
> -        Assert.assertEquals(expectedValue, parser.parse(vars.get(VAR_
> NAME)));
> -        Assert.assertEquals("1", vars.get(VAR_NAME + "_matchNr"));
> +        assertThat(parser.parse(vars.get(VAR_NAME)),
> CoreMatchers.is(expectedValue));
> +        assertThat(vars.get(VAR_NAME + "_matchNr"),
> CoreMatchers.is(CoreMatchers.nullValue()));
> +        assertThat(vars.get(VAR_NAME + "_1"),
> CoreMatchers.is(CoreMatchers.nullValue()));
>      }
>
>      @Test
>
> Modified: jmeter/trunk/xdocs/changes.xml
> URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.
> xml?rev=1772247&r1=1772246&r2=1772247&view=diff
> ============================================================
> ==================
> --- jmeter/trunk/xdocs/changes.xml [utf-8] (original)
> +++ jmeter/trunk/xdocs/changes.xml [utf-8] Thu Dec  1 18:39:36 2016
> @@ -168,6 +168,8 @@ Fill in some detail.
>
>  <h3>Timers, Assertions, Config, Pre- &amp; Post-Processors</h3>
>  <ul>
> +    <li><pr>235</pr>Clear old variables before extracting new ones in
> JSON Extractor.
> +    Based on a patch by Qi Chen (qi.chensh at ele.me)</li>
>  </ul>
>
>  <h3>Functions</h3>
>
>
>

-- 
Cordialement.
Philippe Mouawad.

Reply via email to