Skip to content

Commit

Permalink
Adding group attribute #54
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Feb 24, 2019
1 parent f9fb950 commit df1c897
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
46 changes: 44 additions & 2 deletions src/main/java/se/bjurr/violations/lib/model/Violation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import se.bjurr.violations.lib.parsers.CPPCheckParser;
import se.bjurr.violations.lib.reports.Parser;

public class Violation implements Serializable, Comparable<Violation> {
Expand All @@ -25,6 +26,7 @@ public static class ViolationBuilder {
private String source;
private Map<String, String> specifics = new HashMap<>();
private Integer startLine;
private String group;

private ViolationBuilder() {}

Expand Down Expand Up @@ -67,7 +69,7 @@ public ViolationBuilder setRule(final String rule) {
return this;
}

public ViolationBuilder setCategory(String category) {
public ViolationBuilder setCategory(final String category) {
this.category = category;
return this;
}
Expand Down Expand Up @@ -101,6 +103,11 @@ public ViolationBuilder setStartLine(final Integer startLine) {
this.startLine = startLine;
return this;
}

public ViolationBuilder setGroup(final String group) {
this.group = group;
return this;
}
}

private static final long serialVersionUID = -6052921679385466168L;
Expand All @@ -123,6 +130,12 @@ public static ViolationBuilder violationBuilder() {

private final String rule;
private final String category;
/**
* Something that identifies a group that this violation belongs to. First introduced with {@link
* CPPCheckParser} to record what error tag each violation belongs to.
*/
private final String group;

private final SEVERITY severity;
private final String source;
private final Map<String, String> specifics;
Expand All @@ -140,6 +153,7 @@ public Violation() {
reporter = null;
specifics = null;
parser = null;
group = null;
}

public Violation(final ViolationBuilder vb) {
Expand All @@ -158,6 +172,7 @@ public Violation(final ViolationBuilder vb) {
source = nullToEmpty(vb.source);
rule = nullToEmpty(vb.rule);
category = nullToEmpty(vb.category);
group = nullToEmpty(vb.group);
specifics = vb.specifics;
}

Expand All @@ -173,6 +188,7 @@ public Violation(final Violation v) {
source = v.source;
rule = v.rule;
category = v.category;
group = v.group;
specifics = v.specifics;
}

Expand All @@ -188,6 +204,13 @@ public boolean equals(final Object obj) {
return false;
}
final Violation other = (Violation) obj;
if (category == null) {
if (other.category != null) {
return false;
}
} else if (!category.equals(other.category)) {
return false;
}
if (column == null) {
if (other.column != null) {
return false;
Expand All @@ -209,6 +232,13 @@ public boolean equals(final Object obj) {
} else if (!file.equals(other.file)) {
return false;
}
if (group == null) {
if (other.group != null) {
return false;
}
} else if (!group.equals(other.group)) {
return false;
}
if (message == null) {
if (other.message != null) {
return false;
Expand Down Expand Up @@ -317,13 +347,19 @@ public Integer getStartLine() {
return startLine;
}

public String getGroup() {
return group;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (category == null ? 0 : category.hashCode());
result = prime * result + (column == null ? 0 : column.hashCode());
result = prime * result + (endLine == null ? 0 : endLine.hashCode());
result = prime * result + (file == null ? 0 : file.hashCode());
result = prime * result + (group == null ? 0 : group.hashCode());
result = prime * result + (message == null ? 0 : message.hashCode());
result = prime * result + (parser == null ? 0 : parser.hashCode());
result = prime * result + (reporter == null ? 0 : reporter.hashCode());
Expand All @@ -345,10 +381,14 @@ public String toString() {
+ file
+ ", message="
+ message
+ ", reporter="
+ ", parser="
+ parser
+ ", reporter="
+ reporter
+ ", rule="
+ rule
+ ", category="
+ category
+ ", severity="
+ severity
+ ", source="
Expand All @@ -357,6 +397,8 @@ public String toString() {
+ specifics
+ ", startLine="
+ startLine
+ ", group="
+ group
+ "]";
}

Expand Down
28 changes: 15 additions & 13 deletions src/main/java/se/bjurr/violations/lib/parsers/CPPCheckParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@
public class CPPCheckParser implements ViolationsParser {

@Override
public List<Violation> parseReportOutput(String string) throws Exception {
List<Violation> violations = new ArrayList<>();
List<String> errorChunks = getChunks(string, "<error", "</error>");
for (String errorChunk : errorChunks) {
String severity = getAttribute(errorChunk, "severity");
String msg = getAttribute(errorChunk, "msg");
String verbose = getAttribute(errorChunk, "verbose");
String id = getAttribute(errorChunk, "id");
List<String> locationChunks = getChunks(errorChunk, "<location", "/>");
for (String locationChunk : locationChunks) {
Integer line = getIntegerAttribute(locationChunk, "line");
public List<Violation> parseReportOutput(final String string) throws Exception {
final List<Violation> violations = new ArrayList<>();
final List<String> errorChunks = getChunks(string, "<error", "</error>");
for (int errorIndex = 0; errorIndex < errorChunks.size(); errorIndex++) {
final String errorChunk = errorChunks.get(errorIndex);
final String severity = getAttribute(errorChunk, "severity");
final String msg = getAttribute(errorChunk, "msg");
final String verbose = getAttribute(errorChunk, "verbose");
final String id = getAttribute(errorChunk, "id");
final List<String> locationChunks = getChunks(errorChunk, "<location", "/>");
for (final String locationChunk : locationChunks) {
final Integer line = getIntegerAttribute(locationChunk, "line");
final Optional<String> info = findAttribute(locationChunk, "info");
String fileString = getAttribute(errorChunk, "file");
final String fileString = getAttribute(errorChunk, "file");
String message = "";
if (verbose.startsWith(msg)) {
message = verbose;
Expand All @@ -49,14 +50,15 @@ public List<Violation> parseReportOutput(String string) throws Exception {
.setSeverity(toSeverity(severity)) //
.setMessage(message) //
.setRule(id) //
.setGroup(Integer.toString(errorIndex)) //
.build() //
);
}
}
return violations;
}

public SEVERITY toSeverity(String severity) {
public SEVERITY toSeverity(final String severity) {
if (severity.equalsIgnoreCase("error")) {
return ERROR;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public class CodeNarcParser implements ViolationsParser {

private SEVERITY getSeverity(Integer from) {
private SEVERITY getSeverity(final Integer from) {
if (from == 1) {
return ERROR;
}
Expand All @@ -35,7 +35,7 @@ private SEVERITY getSeverity(Integer from) {
}

@Override
public List<Violation> parseReportOutput(String string) throws Exception {
public List<Violation> parseReportOutput(final String string) throws Exception {
final List<Violation> violations = new ArrayList<>();

final Map<String, String> rules = getRules(string);
Expand Down Expand Up @@ -100,8 +100,6 @@ private String getSourceDirectory(final String string) throws Exception {
try (InputStream input = new ByteArrayInputStream(string.getBytes("UTF-8"))) {
final XMLInputFactory factory = XMLInputFactory.newInstance();
final XMLStreamReader xmlr = factory.createXMLStreamReader(input);
String name = null;
String description = null;
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/se/bjurr/violations/lib/CPPCheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void testThatViolationsCanBeParsed() {
.setRule("variableScope") //
.setMessage(MSG_1) //
.setSeverity(INFO) //
.setGroup("1") //
.build()) //
.contains( //
violationBuilder() //
Expand All @@ -53,6 +54,7 @@ public void testThatViolationsCanBeParsed() {
.setRule("variableScope") //
.setMessage(MSG_2) //
.setSeverity(ERROR) //
.setGroup("2") //
.build()) //
.hasSize(3);
}
Expand Down Expand Up @@ -113,5 +115,25 @@ public void testThatViolationsCanBeParsedByRule() {
.hasSize(1);
assertThat(violationsPerParser.get(CPPCHECK)) //
.hasSize(4);

final Map<String, List<Violation>> violationsPerGroupMap =
violationsList
.stream() //
.collect(Collectors.groupingBy(Violation::getGroup));
assertThat(violationsPerGroupMap) //
.as("There are 2 error tags and there should be 2 groups identified.") //
.hasSize(2);

final List<Violation> firstErrorTag = violationsPerGroupMap.get("0");
assertThat(firstErrorTag) //
.hasSize(2);
assertThat(firstErrorTag.get(0).getMessage()) //
.isEqualTo("Variable 'it' is reassigned a value before the old one has been used.");

final List<Violation> secondErrorTag = violationsPerGroupMap.get("1");
assertThat(secondErrorTag) //
.hasSize(2);
assertThat(secondErrorTag.get(0).getMessage()) //
.isEqualTo("Condition 'rc' is always true");
}
}

0 comments on commit df1c897

Please sign in to comment.