Bug 63204 - RenderAsJSON#prettyJSON : JSONParser#parse cannot return JSONValue

Also increase coverage
Bugzilla Id: 63204

git-svn-id: https://svn.apache.org/repos/asf/jmeter/trunk@1854276 13f79535-47bb-0310-9956-ffa450edef68

Former-commit-id: c1e9540f2f
This commit is contained in:
Philippe Mouawad 2019-02-24 19:21:14 +00:00
parent 911d48dfc7
commit 0d5bf6fd71
4 changed files with 28 additions and 17 deletions

View File

@ -58,7 +58,7 @@ import org.slf4j.LoggerFactory;
public class RenderAsJsonRenderer implements ResultRenderer, ActionListener {
private static final Logger log = LoggerFactory.getLogger(RenderAsJsonRenderer.class);
private static final String NO_MATCH = "NO MATCH"; //$NON-NLS-1$
private static final String TAB_SEPARATOR = " "; //$NON-NLS-1$
private static final String JSONPATH_TESTER_COMMAND = "jsonpath_tester"; // $NON-NLS-1$
@ -121,7 +121,7 @@ public class RenderAsJsonRenderer implements ResultRenderer, ActionListener {
try {
List<Object> matchStrings = extractWithJSonPath(textToParse, jsonPathExpressionField.getText());
if (matchStrings.isEmpty()) {
return "NO MATCH"; //$NON-NLS-1$
return NO_MATCH; //$NON-NLS-1$
} else {
StringBuilder builder = new StringBuilder();
int i = 0;
@ -149,14 +149,8 @@ public class RenderAsJsonRenderer implements ResultRenderer, ActionListener {
@Override
public void renderResult(SampleResult sampleResult) {
String response = ViewResultsFullVisualizer.getResponseAsString(sampleResult);
try {
jsonDataField.setText(response == null ? "" : RenderAsJSON.prettyJSON(response, TAB_SEPARATOR)); //$NON-NLS-1$
jsonDataField.setCaretPosition(0);
} catch (Exception e) {
log.error("Exception converting to XML: {}, message: {}", response, e.getMessage(), e); //$NON-NLS-1$ $NON-NLS-2$
jsonDataField.setText("Exception converting to XML: "+response+ ", message: "+e.getMessage()); //$NON-NLS-1$ $NON-NLS-2$
jsonDataField.setCaretPosition(0);
}
jsonDataField.setText(response == null ? "" : RenderAsJSON.prettyJSON(response, TAB_SEPARATOR)); //$NON-NLS-1$
jsonDataField.setCaretPosition(0);
}

View File

@ -28,7 +28,6 @@ import org.apache.jmeter.util.JMeterUtils;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONStyle;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;
@ -76,9 +75,6 @@ public class RenderAsJSON extends SamplerResultTab implements ResultRenderer {
} else if (o instanceof JSONArray) {
return ((JSONArray) o)
.toJSONString(new PrettyJSONStyle(tabSeparator));
} else if (o instanceof JSONValue) {
return ((JSONValue) o)
.toJSONString(new PrettyJSONStyle(tabSeparator));
}
} catch (ParseException e) {
return json;

View File

@ -57,21 +57,41 @@ class RenderAsJsonRendererSpec extends JMeterSpec {
sut.jsonDataField.getText() == ""
}
def "render JSON Response"() {
def "render '#input' as JSON Response to '#output'"() {
given:
sut.init();
def sampleResult = new SampleResult();
sampleResult.setResponseData("{name:\"Ludwig\",age: 23,city: \"Bonn\"}");
when:
sampleResult.setResponseData(input);
sut.renderResult(sampleResult)
then:
sut.jsonDataField.getText() == '''{
output == sut.jsonDataField.getText()
where:
input | output
"This is not json" | "This is not json"
"{name:\"Ludwig\",age: 23,city: \"Bonn\"}" | '''{
"city": "Bonn",
"name": "Ludwig",
"age": 23
}'''
}
def "execute '#expression' on '#input' results into '#output'"() {
given:
sut.init();
sut.jsonPathExpressionField.setText(expression);
def sampleResult = new SampleResult();
when:
sut.executeAndJSonPathTester(input);
then:
output == sut.jsonPathResultField.getText()
where:
input | expression | output
"{name:\"Ludwig\",age: 23,city: \"Bonn\"}" | "\$..name" | "Result[0]=Ludwig\n"
"This is not json" | "\$..name" | "NO MATCH"
"{name:\"Ludwig\",age: 23,city: \"Bonn\"}" | "\$.." | "Exception: Path must not end with a '.' or '..'"
}
def "clearData clears expected fields"() {
given:
sut.init()

View File

@ -136,6 +136,7 @@ Summary
<h3>Listeners</h3>
<ul>
<li><bug>63204</bug>RenderAsJSON#prettyJSON : JSONParser#parse cannot return JSONValue</li>
</ul>
<h3>Timers, Assertions, Config, Pre- &amp; Post-Processors</h3>