Skip to content

Commit

Permalink
cflint#323 - Changing JSON structure and changing expected test results
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealAgentK committed Jul 26, 2017
1 parent 4c39b77 commit 53f327c
Show file tree
Hide file tree
Showing 102 changed files with 2,206 additions and 1,788 deletions.
138 changes: 80 additions & 58 deletions src/main/java/com/cflint/JSONOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,77 +26,99 @@ public void setPrettyPrint(final boolean prettyPrint) {

public void output(final BugList bugList, final Writer writer, CFLintStats stats) throws IOException {
final BugCounts counts = stats.getCounts();
// final StringBuilder sb = new StringBuilder();
final JsonFactory jsonF = new JsonFactory();
final JsonGenerator jg = jsonF.createGenerator(writer);

if (prettyPrint) {
jg.useDefaultPrettyPrinter();
}
jg.writeStartArray();
List<String> keys = new ArrayList<String>(bugList.getBugList().keySet());
Collections.sort(keys);
for (final String key : keys) {
List<BugInfo> currentList = bugList.getBugList().get(key);
final Iterator<BugInfo> iterator = currentList.iterator();
BugInfo bugInfo = iterator.hasNext() ? iterator.next() : null;
BugInfo prevbugInfo;

while (bugInfo != null) {
final String severity = currentList.get(0).getSeverity();
final String code = currentList.get(0).getMessageCode();

jg.writeStartObject();
jg.writeStringField("severity", notNull(severity));
jg.writeStringField("id", code);
jg.writeStringField("message", code);
jg.writeStringField("category", "CFLINT");
jg.writeStringField("abbrev", abbrev(code));
jg.writeFieldName("locations");
jg.writeStartArray();
do {

// start global object
jg.writeStartObject();

// start issues array
jg.writeFieldName("issues");
jg.writeStartArray();

List<String> keys = new ArrayList<String>(bugList.getBugList().keySet());
Collections.sort(keys);

for (final String key : keys) {
List<BugInfo> currentList = bugList.getBugList().get(key);
final Iterator<BugInfo> iterator = currentList.iterator();
BugInfo bugInfo = iterator.hasNext() ? iterator.next() : null;
BugInfo prevbugInfo;

while (bugInfo != null) {
final String severity = currentList.get(0).getSeverity();
final String code = currentList.get(0).getMessageCode();

jg.writeStartObject();
jg.writeStringField("file", notNull(bugInfo.getFilename()));
jg.writeStringField("fileName", filename(bugInfo.getFilename()));
jg.writeStringField("function", filename(bugInfo.getFunction()));
jg.writeNumberField("column", bugInfo.getColumn());
jg.writeNumberField("line", bugInfo.getLine());
jg.writeStringField("message", notNull(bugInfo.getMessage()));
jg.writeStringField("variable", notNull(bugInfo.getVariable()));
jg.writeStringField("expression", notNull(bugInfo.getExpression()));
jg.writeStringField("severity", notNull(severity));
jg.writeStringField("id", code);
jg.writeStringField("message", code);
jg.writeStringField("category", "CFLINT");
jg.writeStringField("abbrev", abbrev(code));
jg.writeFieldName("locations");
jg.writeStartArray();
do {
jg.writeStartObject();
jg.writeStringField("file", notNull(bugInfo.getFilename()));
jg.writeStringField("fileName", filename(bugInfo.getFilename()));
jg.writeStringField("function", filename(bugInfo.getFunction()));
jg.writeNumberField("column", bugInfo.getColumn());
jg.writeNumberField("line", bugInfo.getLine());
jg.writeStringField("message", notNull(bugInfo.getMessage()));
jg.writeStringField("variable", notNull(bugInfo.getVariable()));
jg.writeStringField("expression", notNull(bugInfo.getExpression()));
jg.writeEndObject();
prevbugInfo = bugInfo;
bugInfo = iterator.hasNext() ? iterator.next() : null;
} while (isGrouped(prevbugInfo, bugInfo));
jg.writeEndArray();
jg.writeEndObject();
prevbugInfo = bugInfo;
bugInfo = iterator.hasNext() ? iterator.next() : null;
} while (isGrouped(prevbugInfo, bugInfo));
jg.writeEndArray();
jg.writeEndObject();
}
}
}

jg.writeStartObject();
jg.writeNumberField("totalfiles", stats.getFileCount());
jg.writeEndObject();
// end issues array
jg.writeEndArray();

jg.writeStartObject();
jg.writeFieldName("totalsize");
jg.writeNumber(stats.getTotalSize());
jg.writeEndObject();

for (final String code : counts.bugTypes()) {
// start summary object
jg.writeFieldName("summary");
jg.writeStartObject();
jg.writeStringField("code", code);
jg.writeNumberField("count", counts.getCode(code));
jg.writeNumberField("totalfiles", stats.getFileCount());
jg.writeFieldName("totalsize");
jg.writeNumber(stats.getTotalSize());
// start countByCode array
jg.writeFieldName("countByCode");
jg.writeStartArray();
for (final String code : counts.bugTypes()) {
jg.writeStartObject();
jg.writeStringField("code", code);
jg.writeNumberField("count", counts.getCode(code));
jg.writeEndObject();
}
// end countByCode array
jg.writeEndArray();
// start countBySeverity array
jg.writeFieldName("countBySeverity");
jg.writeStartArray();
for (final String severity : BugCounts.levels) {
if (counts.getSeverity(severity) > 0) {
jg.writeStartObject();
jg.writeStringField("severity", severity);
jg.writeNumberField("count", counts.getSeverity(severity));
jg.writeEndObject();
}
}
// end countBySeverity array
jg.writeEndArray();
// end summary object
jg.writeEndObject();
}
for (final String severity : BugCounts.levels) {
if (counts.getSeverity(severity) > 0) {
jg.writeStartObject();
jg.writeStringField("severity", severity);
jg.writeNumberField("count", counts.getSeverity(severity));
jg.writeEndObject();
}
}

jg.writeEndArray();
// end global object
jg.writeEndObject();

jg.close();
writer.close();
}
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/com/cflint/TestJSONOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public void testOutput() throws IOException {
bugList.add(bugInfo);
CFLintStats stats = new CFLintStats(123456L,1,new BigInteger("545454"));
outputer.output(bugList, writer, stats);
String expectedText = "[{\"severity\":\"\",\"id\":\"PARSE_ERROR\",\"message\":\"PARSE_ERROR\",\"category\":\"CFLINT\",\"abbrev\":\"PE\",\"locations\":[{\"file\":\"c:\\\\temp\\\\test.cfc\",\"fileName\":\"test.cfc\",\"function\":\"testf\",\"column\":1,\"line\":1,\"message\":\"\",\"variable\":\"\",\"expression\":\"\"}]},{\"totalfiles\":1},{\"totalsize\":545454}]";
// assertEquals(JSONValue.parse(expectedText),JSONValue.parse(writer.toString()));
String expectedText = "{\"issues\":[{\"severity\":\"\",\"id\":\"PARSE_ERROR\",\"message\":\"PARSE_ERROR\",\"category\":\"CFLINT\",\"abbrev\":\"PE\",\"locations\":[{\"file\":\"c:\\\\temp\\\\test.cfc\",\"fileName\":\"test.cfc\",\"function\":\"testf\",\"column\":1,\"line\":1,\"message\":\"\",\"variable\":\"\",\"expression\":\"\"}]}],\"summary\":{\"totalfiles\":1,\"totalsize\":545454,\"countByCode\":[],\"countBySeverity\":[]}}";

assertEquals(expectedText,writer.toString());
}

Expand All @@ -43,7 +43,8 @@ public void testStats() throws IOException {
counts.add("PARSE_ERROR", null);
CFLintStats stats = new CFLintStats(123456L,1,new BigInteger("545454"),counts);
outputer.output(bugList, writer, stats);
String expectedText = "[{\"severity\":\"\",\"id\":\"PARSE_ERROR\",\"message\":\"PARSE_ERROR\",\"category\":\"CFLINT\",\"abbrev\":\"PE\",\"locations\":[{\"file\":\"c:\\\\temp\\\\test.cfc\",\"fileName\":\"test.cfc\",\"function\":\"testf\",\"column\":1,\"line\":1,\"message\":\"\",\"variable\":\"\",\"expression\":\"\"}]},{\"totalfiles\":1},{\"totalsize\":545454},{\"code\":\"PARSE_ERROR\",\"count\":1}]";
String expectedText = "{\"issues\":[{\"severity\":\"\",\"id\":\"PARSE_ERROR\",\"message\":\"PARSE_ERROR\",\"category\":\"CFLINT\",\"abbrev\":\"PE\",\"locations\":[{\"file\":\"c:\\\\temp\\\\test.cfc\",\"fileName\":\"test.cfc\",\"function\":\"testf\",\"column\":1,\"line\":1,\"message\":\"\",\"variable\":\"\",\"expression\":\"\"}]}],\"summary\":{\"totalfiles\":1,\"totalsize\":545454,\"countByCode\":[{\"code\":\"PARSE_ERROR\",\"count\":1}],\"countBySeverity\":[]}}";

assertEquals(expectedText,writer.toString());
}

Expand All @@ -55,7 +56,8 @@ public void testStatsWithSeverity() throws IOException {
counts.add("PARSE_ERROR", "ERROR");
CFLintStats stats = new CFLintStats(123456L,1,new BigInteger("545454"),counts);
outputer.output(bugList, writer, stats);
String expectedText = "[{\"severity\":\"ERROR\",\"id\":\"PARSE_ERROR\",\"message\":\"PARSE_ERROR\",\"category\":\"CFLINT\",\"abbrev\":\"PE\",\"locations\":[{\"file\":\"c:\\\\temp\\\\test.cfc\",\"fileName\":\"test.cfc\",\"function\":\"testf\",\"column\":1,\"line\":1,\"message\":\"\",\"variable\":\"\",\"expression\":\"\"}]},{\"totalfiles\":1},{\"totalsize\":545454},{\"code\":\"PARSE_ERROR\",\"count\":1},{\"severity\":\"ERROR\",\"count\":1}]";
String expectedText = "{\"issues\":[{\"severity\":\"ERROR\",\"id\":\"PARSE_ERROR\",\"message\":\"PARSE_ERROR\",\"category\":\"CFLINT\",\"abbrev\":\"PE\",\"locations\":[{\"file\":\"c:\\\\temp\\\\test.cfc\",\"fileName\":\"test.cfc\",\"function\":\"testf\",\"column\":1,\"line\":1,\"message\":\"\",\"variable\":\"\",\"expression\":\"\"}]}],\"summary\":{\"totalfiles\":1,\"totalsize\":545454,\"countByCode\":[{\"code\":\"PARSE_ERROR\",\"count\":1}],\"countBySeverity\":[{\"severity\":\"ERROR\",\"count\":1}]}}";

assertEquals(expectedText,writer.toString());
}

Expand Down
156 changes: 78 additions & 78 deletions src/test/resources/com/cflint/integration/output.expected.json
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@
[ {
"severity" : "INFO",
"id" : "COMPONENT_INVALID_NAME",
"message" : "COMPONENT_INVALID_NAME",
"category" : "CFLINT",
"abbrev" : "CI",
"locations" : [ {
"file" : "C:\\Users\\Ryan\\git\\CFLint\\src\\test\\resources\\com\\cflint\\integration\\inherit\\test.cfc",
"fileName" : "test.cfc",
"function" : "",
"column" : 0,
"line" : 1,
"message" : "Component name test is not a valid name. Please use CamelCase and start with a capital letter.",
"variable" : "",
"expression" : "component {\n public string function getStringFromStruct(interestingVar) {\n return interestingVar;\n\n }\n}"
} ]
}, {
"severity" : "INFO",
"id" : "FUNCTION_HINT_MISSING",
"message" : "FUNCTION_HINT_MISSING",
"category" : "CFLINT",
"abbrev" : "FH",
"locations" : [ {
"file" : "C:\\Users\\Ryan\\git\\CFLint\\src\\test\\resources\\com\\cflint\\integration\\inherit\\test.cfc",
"fileName" : "test.cfc",
"function" : "getStringFromStruct",
"column" : 16,
"line" : 2,
"message" : "Function getStringFromStruct is missing a hint.",
"variable" : "getStringFromStruct",
"expression" : "public string function getStringFromStruct(interestingVar) {\n return interestingVar;\n\n }"
} ]
}, {
"severity" : "INFO",
"id" : "FUNCTION_HINT_MISSING",
"message" : "FUNCTION_HINT_MISSING",
"category" : "CFLINT",
"abbrev" : "FH",
"locations" : [ {
"file" : "C:\\Users\\Ryan\\git\\CFLint\\src\\test\\resources\\com\\cflint\\integration\\standalone\\test.cfc",
"fileName" : "test.cfc",
"function" : "getStringFromStruct",
"column" : 16,
"line" : 2,
"message" : "Function getStringFromStruct is missing a hint.",
"variable" : "getStringFromStruct",
"expression" : "public string function getStringFromStruct(interestingVar) {\n return interestingVar;\n\n }"
} ]
}, {
"severity" : "INFO",
"id" : "FUNCTION_HINT_MISSING",
"message" : "FUNCTION_HINT_MISSING",
"category" : "CFLINT",
"abbrev" : "FH",
"locations" : [ {
"file" : "C:\\Users\\Ryan\\git\\CFLint\\src\\test\\resources\\com\\cflint\\integration\\test.cfc",
"fileName" : "test.cfc",
"function" : "getStringFromStruct",
"column" : 16,
"line" : 2,
"message" : "Function getStringFromStruct is missing a hint.",
"variable" : "getStringFromStruct",
"expression" : "public string function getStringFromStruct(interestingVar) {\n return interestingVar;\n\n }"
} ]
}, {
"totalfiles" : 3
}, {
"totalsize" : 18
}, {
"code" : "FUNCTION_HINT_MISSING",
"count" : 3
}, {
"code" : "COMPONENT_INVALID_NAME",
"count" : 1
}, {
"severity" : "INFO",
"count" : 4
} ]
{
"issues" : [ {
"severity" : "INFO",
"id" : "COMPONENT_INVALID_NAME",
"message" : "COMPONENT_INVALID_NAME",
"category" : "CFLINT",
"abbrev" : "CI",
"locations" : [ {
"fileName" : "test.cfc",
"function" : "",
"column" : 0,
"line" : 1,
"message" : "Component name test is not a valid name. Please use CamelCase and start with a capital letter.",
"variable" : "",
"expression" : "component {/n public string function getStringFromStruct(interestingVar) {/n return interestingVar;/n/n }/n}"
} ]
}, {
"severity" : "INFO",
"id" : "FUNCTION_HINT_MISSING",
"message" : "FUNCTION_HINT_MISSING",
"category" : "CFLINT",
"abbrev" : "FH",
"locations" : [ {
"fileName" : "test.cfc",
"function" : "getStringFromStruct",
"column" : 16,
"line" : 2,
"message" : "Function getStringFromStruct is missing a hint.",
"variable" : "getStringFromStruct",
"expression" : "public string function getStringFromStruct(interestingVar) {/n return interestingVar;/n/n }"
} ]
}, {
"severity" : "INFO",
"id" : "FUNCTION_HINT_MISSING",
"message" : "FUNCTION_HINT_MISSING",
"category" : "CFLINT",
"abbrev" : "FH",
"locations" : [ {
"fileName" : "test.cfc",
"function" : "getStringFromStruct",
"column" : 16,
"line" : 2,
"message" : "Function getStringFromStruct is missing a hint.",
"variable" : "getStringFromStruct",
"expression" : "public string function getStringFromStruct(interestingVar) {/n return interestingVar;/n/n }"
} ]
}, {
"severity" : "INFO",
"id" : "FUNCTION_HINT_MISSING",
"message" : "FUNCTION_HINT_MISSING",
"category" : "CFLINT",
"abbrev" : "FH",
"locations" : [ {
"fileName" : "test.cfc",
"function" : "getStringFromStruct",
"column" : 16,
"line" : 2,
"message" : "Function getStringFromStruct is missing a hint.",
"variable" : "getStringFromStruct",
"expression" : "public string function getStringFromStruct(interestingVar) {/n return interestingVar;/n/n }"
} ]
} ],
"summary" : {
"totalfiles" : 3,
"totalsize" : 18,
"countByCode" : [ {
"code" : "FUNCTION_HINT_MISSING",
"count" : 3
}, {
"code" : "COMPONENT_INVALID_NAME",
"count" : 1
} ],
"countBySeverity" : [ {
"severity" : "INFO",
"count" : 4
} ]
}
}
Loading

0 comments on commit 53f327c

Please sign in to comment.