Skip to content

Commit

Permalink
Merge pull request #55 from jetty-project/issue_54_json_npe
Browse files Browse the repository at this point in the history
Fixes #54 - NPE when using JSON resources.
  • Loading branch information
sbordet authored Feb 2, 2021
2 parents e47dedd + 1db5cf7 commit e8f58b2
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,50 @@ Info newInfo(LoadGenerator generator) {

@Override
public void toJSON(JSON.Output out) {
out.add("method", getMethod());
out.add("path", getPath());
String method = getMethod();
if (method != null) {
out.add("method", method);
}
String path = getPath();
if (path == null) {
path = "/";
}
out.add("path", path);
out.add("requestLength", getRequestLength());
out.add("requestHeaders", toMap(getRequestHeaders()));
out.add("resources", getResources());
out.add("responseLength", getResponseLength());
HttpFields requestHeaders = getRequestHeaders();
if (requestHeaders != null) {
out.add("requestHeaders", toMap(requestHeaders));
}
List<Resource> resources = getResources();
if (resources != null) {
out.add("resources", resources);
}
}

@Override
public void fromJSON(Map map) {
method((String)map.get("method"));
path((String)map.get("path"));
requestLength(((Number)map.get("requestLength")).longValue());
String method = (String)map.get("method");
if (method != null) {
method(method);
}
String path = (String)map.get("path");
if (path == null) {
path = "/";
}
path(path);
Number requestLength = (Number)map.get("requestLength");
if (requestLength != null) {
requestLength(requestLength.longValue());
}
Number responseLength = (Number)map.get("responseLength");
if (responseLength != null) {
responseLength(responseLength.longValue());
}
@SuppressWarnings("unchecked")
Map<String, Object> requestHeaders = (Map<String, Object>)map.get("requestHeaders");
requestHeaders(toHttpFields(requestHeaders));
resources(toResources(map.get("resources")));
responseLength(((Number)map.get("responseLength")).longValue());
}

private static Map<String, Object> toMap(HttpFields fields) {
Expand All @@ -244,9 +270,11 @@ private static Map<String, Object> toMap(HttpFields fields) {

private static HttpFields toHttpFields(Map<String, Object> map) {
HttpFields fields = new HttpFields();
map.entrySet().stream()
.map(entry -> new HttpField(entry.getKey(), Arrays.stream((Object[])entry.getValue()).map(String::valueOf).collect(Collectors.joining(","))))
.forEach(fields::put);
if (map != null) {
map.entrySet().stream()
.map(entry -> new HttpField(entry.getKey(), Arrays.stream((Object[])entry.getValue()).map(String::valueOf).collect(Collectors.joining(","))))
.forEach(fields::put);
}
return fields;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,14 @@ public void toJSON(JSON.Output out) {
@Override
public void fromJSON(Map map) {
setServerVersion((String)map.get("serverVersion"));
setProcessorCount(((Number)map.get("processorCount")).intValue());
setTotalMemory(((Number)map.get("totalMemory")).longValue());
Number processorCount = (Number)map.get("processorCount");
if (processorCount != null) {
setProcessorCount(processorCount.intValue());
}
Number totalMemory = (Number)map.get("totalMemory");
if (totalMemory != null) {
setTotalMemory(totalMemory.longValue());
}
setGitHash((String)map.get("gitHash"));
setJavaVersion((String)map.get("javaVersion"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,22 @@ Resource getResource(LoadGenerator.Builder builder) throws Exception {
return evaluateGroovy(reader, context);
}
}
throw new IllegalArgumentException("resource not defined");
return new Resource("/");
}

static Resource evaluateJSON(Path profilePath) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(profilePath, StandardCharsets.UTF_8)) {
JSON json = new JSON();
Resource resource = new Resource();
resource.fromJSON((Map<?, ?>)json.parse(new JSON.ReaderSource(reader)));
return resource;
return evaluateJSON(reader);
}
}

static Resource evaluateJSON(Reader reader) {
JSON json = new JSON();
Resource resource = new Resource();
resource.fromJSON((Map<?, ?>)json.parse(new JSON.ReaderSource(reader)));
return resource;
}

static Resource evaluateGroovy(Reader script, Map<String, Object> context) {
CompilerConfiguration config = new CompilerConfiguration(CompilerConfiguration.DEFAULT);
config.setDebug(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -79,7 +81,7 @@ public void stopJetty() throws Exception {
}

@Test
public void simpleTest() {
public void testSimple() {
String[] args = new String[]{
"--warmup-iterations",
"10",
Expand Down Expand Up @@ -108,7 +110,7 @@ public void simpleTest() {

@Test
@Ignore("see FailFastTest")
public void failFast() {
public void testFailFast() {
String[] args = new String[]{
"--warmup-iterations",
"10",
Expand Down Expand Up @@ -160,7 +162,7 @@ public void onCommit(Request request) {
}

@Test
public void fromGroovyToJSON() throws Exception {
public void testFromGroovyToJSON() throws Exception {
try (Reader reader = Files.newBufferedReader(Paths.get("src/test/resources/tree_resources.groovy"))) {
Resource resource = LoadGeneratorStarterArgs.evaluateGroovy(reader, Map.of());
Path tmpPath = Files.createTempFile("resources_", ".tmp");
Expand All @@ -175,13 +177,45 @@ public void fromGroovyToJSON() throws Exception {
}

@Test
public void calculate_descendant_number() throws Exception {
public void testCalculateDescendantCount() throws Exception {
try (Reader reader = Files.newBufferedReader(Paths.get("src/test/resources/tree_resources.groovy"))) {
Resource resource = LoadGeneratorStarterArgs.evaluateGroovy(reader, Map.of());
Assert.assertEquals(17, resource.descendantCount());
}
}

@Test
public void testSimplestJSON() {
String path = "/index.html";
try (StringReader reader = new StringReader("{\"path\":\"" + path + "\"}")) {
Resource resource = LoadGeneratorStarterArgs.evaluateJSON(reader);
Assert.assertEquals(path, resource.getPath());
}
}

@Test
public void testFullJSON() {
try (StringReader reader = new StringReader("" +
"{" +
"\"method\":\"POST\"," +
"\"path\":\"/index.html\"," +
"\"requestLength\":1," +
"\"responseLength\":2," +
"\"requestHeaders\":{\"Foo\":[\"Bar\"]}," +
"\"resources\":[{\"path\":\"/styles.css\"}]" +
"}")) {
Resource resource = LoadGeneratorStarterArgs.evaluateJSON(reader);
Assert.assertEquals("POST", resource.getMethod());
Assert.assertEquals("/index.html", resource.getPath());
Assert.assertEquals(1, resource.getRequestLength());
Assert.assertEquals(2, resource.getResponseLength());
Assert.assertEquals("Bar", resource.getRequestHeaders().get("Foo"));
List<Resource> children = resource.getResources();
Assert.assertEquals(1, children.size());
Assert.assertEquals("/styles.css", children.get(0).getPath());
}
}

private static class TestServlet extends HttpServlet {
private final AtomicInteger getNumber = new AtomicInteger(0);
private final AtomicInteger postNumber = new AtomicInteger(0);
Expand Down

0 comments on commit e8f58b2

Please sign in to comment.