Author: fschumacher Date: Fri Oct 21 18:39:22 2016 New Revision: 1766100 URL: http://svn.apache.org/viewvc?rev=1766100&view=rev Log: JSON Extractor doesn't index array elements when only one element is found. Based on patch by Roberto Braga (roberto.braga at sociale.it). Bugzilla Id: 60295 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/test/src/org/apache/jmeter/extractor/TestRegexExtractor.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=1766100&r1=1766099&r2=1766100&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 Fri Oct 21 18:39:22 2016 @@ -161,7 +161,8 @@ public class JSONPostProcessor extends A } } else { // else just one value extracted - placeObjectIntoVars(vars, currentRefName, extractedValues, 0); + String suffix = (matchNumber < 0) ? "_1" : ""; + placeObjectIntoVars(vars, currentRefName + suffix, extractedValues, 0); if (matchNumber < 0 && getComputeConcatenation()) { vars.put(currentRefName + ALL_SUFFIX, vars.get(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=1766100&r1=1766099&r2=1766100&view=diff ============================================================================== --- jmeter/trunk/test/src/org/apache/jmeter/extractor/TestJSONPostProcessor.java (original) +++ jmeter/trunk/test/src/org/apache/jmeter/extractor/TestJSONPostProcessor.java Fri Oct 21 18:39:22 2016 @@ -18,6 +18,8 @@ package org.apache.jmeter.extractor; +import static org.junit.Assert.assertThat; + import java.nio.charset.StandardCharsets; import net.minidev.json.parser.JSONParser; @@ -28,6 +30,7 @@ import org.apache.jmeter.samplers.Sample import org.apache.jmeter.threads.JMeterContext; import org.apache.jmeter.threads.JMeterContextService; import org.apache.jmeter.threads.JMeterVariables; +import org.hamcrest.CoreMatchers; import org.junit.Assert; import org.junit.Test; @@ -36,6 +39,40 @@ public class TestJSONPostProcessor { private static final String VAR_NAME = "varName"; @Test + public void testProcessAllElementsOneMatch() { + 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\"]"); + processor.process(); + assertThat(vars.get("varname"), CoreMatchers.is(CoreMatchers.nullValue())); + assertThat(vars.get("varname_1"), CoreMatchers.is("one")); + assertThat(vars.get("varname_matchNr"), CoreMatchers.is("1")); + } + + @Test + public void testProcessAllElementsMultipleMatches() { + 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")); + } + + @Test public void testBug59609() throws ParseException { JMeterContext context = JMeterContextService.getContext(); JSONPostProcessor processor = setupProcessor(context, "0", false); Modified: jmeter/trunk/test/src/org/apache/jmeter/extractor/TestRegexExtractor.java URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/extractor/TestRegexExtractor.java?rev=1766100&r1=1766099&r2=1766100&view=diff ============================================================================== --- jmeter/trunk/test/src/org/apache/jmeter/extractor/TestRegexExtractor.java (original) +++ jmeter/trunk/test/src/org/apache/jmeter/extractor/TestRegexExtractor.java Fri Oct 21 18:39:22 2016 @@ -23,13 +23,16 @@ import static org.junit.Assert.assertEqu import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.net.URL; + import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.threads.JMeterContext; import org.apache.jmeter.threads.JMeterContextService; import org.apache.jmeter.threads.JMeterVariables; +import org.hamcrest.CoreMatchers; import org.junit.Before; import org.junit.Test; @@ -68,6 +71,37 @@ public class TestRegexExtractor { } @Test + public void testProcessAllElementsSingleMatch() { + vars.put("content", "one"); + extractor.setMatchNumber(-1); + extractor.setRefName("varname"); + extractor.setRegex("(\\w+)"); + extractor.setScopeVariable("content"); + extractor.setThreadContext(jmctx); + extractor.setTemplate("$1$"); + extractor.process(); + assertThat(vars.get("varname"), CoreMatchers.is(CoreMatchers.nullValue())); + assertThat(vars.get("varname_1"), CoreMatchers.is("one")); + assertThat(vars.get("varname_matchNr"), CoreMatchers.is("1")); + } + + @Test + public void testProcessAllElementsMultipleMatches() { + vars.put("content", "one, two"); + extractor.setMatchNumber(-1); + extractor.setRefName("varname"); + extractor.setRegex("(\\w+)"); + extractor.setScopeVariable("content"); + extractor.setThreadContext(jmctx); + extractor.setTemplate("$1$"); + extractor.process(); + assertThat(vars.get("varname"), CoreMatchers.is(CoreMatchers.nullValue())); + assertThat(vars.get("varname_1"), CoreMatchers.is("one")); + assertThat(vars.get("varname_2"), CoreMatchers.is("two")); + assertThat(vars.get("varname_matchNr"), CoreMatchers.is("2")); + } + + @Test public void testEmptyDefaultVariable() throws Exception { extractor.setRegex("(.+?)"); extractor.setTemplate("$1$"); Modified: jmeter/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1766100&r1=1766099&r2=1766100&view=diff ============================================================================== --- jmeter/trunk/xdocs/changes.xml [utf-8] (original) +++ jmeter/trunk/xdocs/changes.xml [utf-8] Fri Oct 21 18:39:22 2016 @@ -277,6 +277,7 @@ Summary
  • 60125Report / Dashboard : Dashboard cannot be generated if the default delimiter is \t. Based on a report from Tamas Szabadi (tamas.szabadi at rightside.co)
  • 59439Report / Dashboard : AbstractOverTimeGraphConsumer.createGroupInfos() should be abstract
  • 59918Ant generated HTML report is broken (extras folder)
  • +
  • 60295JSON Extractor doesn't index array elements when only one element is found. Based on a patch by Roberto Braga (roberto.braga at sociale.it)
  • @@ -295,6 +296,7 @@ Summary
  • Maxime Chassagneux (maxime.chassagneux at gmail.com)
  • Ubik Load Pack
  • Tamas Szabadi (tamas.szabadi at rightside.co)
  • +
  • Roberto Braga (roberto.braga at soziale.it)
  • We also thank bug reporters who helped us improve JMeter.
    For this release we want to give special thanks to the following reporters for the clear reports and tests made after our fixes: