Am 20.10.2016 um 16:31 schrieb Felix Schumacher:
Am 20.10.2016 16:01, schrieb Roberto Braga:
I think there is a bug in the file
apache-jmeter-3.0-src/src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java
at line 163, there is the else where i goes if only one match
The below src code is not in sync with the current code.
But I think the important step for you is to place an additional var
with 'name_1' into vars in the case, when only one element is found
and -1 is used to indicate, that all elements are wanted.
If you want to update your patch you could have a look at the current
trunk and use the function stringify(Object).
After comparing the current behaviour of JSON Extractor with that of the
Regex Extractor, I think we should extract to <varname>_1 only, when
matchNumber is -1 and only one element could be matched.
I have attached a patch and two test cases, that show the behaviour for
Regex and (modified) JSON Extractor.
Regards,
Felix
Regards,
Felix
} else {
// else just one value extracted
Object obj = extractedValues.get(0);
String objAsString =
obj != null ? obj.toString() : ""; //$NON-NLS-1$
* vars.put(currentRefName, *
* objAsString); *
* if (matchNumber < 0 && getComputeConcatenation()) {*
* vars.put(currentRefName + ALL_SUFFIX, objAsString);*
* }*
}
The part which set the vars is worng it should be like the following:
} else {
// else just one value extracted
Object obj = extractedValues.get(0);
String objAsString =
obj != null ? obj.toString() : ""; //$NON-NLS-1$
* if (matchNumber < 0 && getComputeConcatenation()) {*
* vars.put(currentRefName + "_1", *
* objAsString);*
* vars.put(currentRefName + ALL_SUFFIX, objAsString);*
* } else {*
* vars.put(currentRefName, *
* objAsString);*
* }*
}
In this way it respect the matchNumber docs specification even if only
one value is extracted.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@jmeter.apache.org
For additional commands, e-mail: user-h...@jmeter.apache.org
>From 006fab5d6a622bc3e54a196aa00428e412d99a84 Mon Sep 17 00:00:00 2001
From: Felix Schumacher <felix.schumac...@internetallee.de>
Date: Thu, 20 Oct 2016 21:38:46 +0200
Subject: [PATCH] Extract json elements into varname_1 when matchnumber is -1
and only one element is found.
---
.../extractor/json/jsonpath/JSONPostProcessor.java | 2 +-
.../jmeter/extractor/RegexExtractorTest.java | 51 ++++++++++++++++++++++
.../json/jsonpath/JSONPostProcessorTest.java | 50 +++++++++++++++++++++
3 files changed, 102 insertions(+), 1 deletion(-)
create mode 100644 test/src/org/apache/jmeter/extractor/RegexExtractorTest.java
create mode 100644 test/src/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessorTest.java
diff --git a/src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java b/src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java
index d8c1856..f9ab1f5 100644
--- a/src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java
+++ b/src/components/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessor.java
@@ -161,7 +161,7 @@ public class JSONPostProcessor extends AbstractScopedTestElement implements Seri
}
} else {
// else just one value extracted
- placeObjectIntoVars(vars, currentRefName, extractedValues, 0);
+ placeObjectIntoVars(vars, currentRefName + "_1", extractedValues, 0);
if (matchNumber < 0 && getComputeConcatenation()) {
vars.put(currentRefName + ALL_SUFFIX, vars.get(currentRefName));
}
diff --git a/test/src/org/apache/jmeter/extractor/RegexExtractorTest.java b/test/src/org/apache/jmeter/extractor/RegexExtractorTest.java
new file mode 100644
index 0000000..7503fd7
--- /dev/null
+++ b/test/src/org/apache/jmeter/extractor/RegexExtractorTest.java
@@ -0,0 +1,51 @@
+package org.apache.jmeter.extractor;
+
+import static org.junit.Assert.*;
+
+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;
+
+public class RegexExtractorTest {
+
+ JMeterVariables vars;
+ JMeterContext context;
+ RegexExtractor extractor;
+
+ @Before
+ public void setUp() {
+ vars = new JMeterVariables();
+ context = JMeterContextService.getContext();
+ context.setVariables(vars);
+ extractor = new RegexExtractor();
+ extractor.setMatchNumber(-1);
+ extractor.setRefName("varname");
+ extractor.setRegex("(\\w+)");
+ extractor.setScopeVariable("content");
+ extractor.setThreadContext(context);
+ extractor.setTemplate("$1$");
+ context.setPreviousResult(new SampleResult());
+ }
+
+ @Test
+ public void testProcessAllElementsSingleMatch() {
+ vars.put("content", "one");
+ extractor.process();
+ assertThat(vars.get("varname"), CoreMatchers.is(CoreMatchers.nullValue()));
+ assertThat(vars.get("varname_1"), CoreMatchers.is("one"));
+ }
+
+ @Test
+ public void testProcessAllElementsMultipleMatches() {
+ vars.put("content", "one, two");
+ 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"));
+ }
+
+}
diff --git a/test/src/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessorTest.java b/test/src/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessorTest.java
new file mode 100644
index 0000000..7b88d22
--- /dev/null
+++ b/test/src/org/apache/jmeter/extractor/json/jsonpath/JSONPostProcessorTest.java
@@ -0,0 +1,50 @@
+package org.apache.jmeter.extractor.json.jsonpath;
+
+import static org.junit.Assert.assertThat;
+
+import org.apache.jmeter.junit.JMeterTestCase;
+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;
+
+public class JSONPostProcessorTest extends JMeterTestCase {
+
+ JMeterContext context;
+ JMeterVariables vars;
+ JSONPostProcessor processor = new JSONPostProcessor();
+
+ @Before
+ public void setUp() {
+ vars = new JMeterVariables();
+ context = JMeterContextService.getContext();
+ processor = new JSONPostProcessor();
+ processor.setComputeConcatenation(true);
+ processor.setDefaultValues("NONE");
+ processor.setJsonPathExpressions("$[*]");
+ processor.setMatchNumbers("-1");
+ processor.setRefNames("varname");
+ processor.setScopeVariable("contentvar");
+ processor.setThreadContext(context );
+ context.setVariables(vars);
+ }
+
+ @Test
+ public void testProcessAllElementsOneMatch() {
+ vars.put("contentvar", "[\"one\"]");
+ processor.process();
+ assertThat(vars.get("varname"), CoreMatchers.is(CoreMatchers.nullValue()));
+ assertThat(vars.get("varname_1"), CoreMatchers.is("one"));
+ }
+
+ @Test
+ public void testProcessAllElementsMultipleMatches() {
+ vars.put("contentvar", "[\"one\", \"two\"]");
+ processor.process();
+ assertThat(vars.get("varname_1"), CoreMatchers.is("one"));
+ assertThat(vars.get("varname_2"), CoreMatchers.is("two"));
+ }
+
+}
--
2.7.4
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@jmeter.apache.org
For additional commands, e-mail: user-h...@jmeter.apache.org