Skip to content

Commit

Permalink
Allowing absent source in Checkstyle parser #23
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Sep 1, 2017
1 parent 3e1965d commit dd52241
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
24 changes: 13 additions & 11 deletions src/main/java/se/bjurr/violations/lib/parsers/CheckStyleParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.findAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.findIntegerAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getChunks;
Expand All @@ -12,6 +13,7 @@

import java.util.ArrayList;
import java.util.List;

import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.util.Optional;
Expand All @@ -20,17 +22,17 @@ public class CheckStyleParser implements ViolationsParser {

@Override
public List<Violation> parseReportOutput(String string) throws Exception {
List<Violation> violations = new ArrayList<>();
List<String> files = getChunks(string, "<file", "</file>");
for (String fileChunk : files) {
String filename = getAttribute(fileChunk, "name");
List<String> errors = getChunks(fileChunk, "<error", "/>");
for (String errorChunk : errors) {
Integer line = getIntegerAttribute(errorChunk, "line");
Optional<Integer> column = findIntegerAttribute(errorChunk, "column");
String severity = getAttribute(errorChunk, "severity");
String message = getAttribute(errorChunk, "message");
String rule = getAttribute(errorChunk, "source");
final List<Violation> violations = new ArrayList<>();
final List<String> files = getChunks(string, "<file", "</file>");
for (final String fileChunk : files) {
final String filename = getAttribute(fileChunk, "name");
final List<String> errors = getChunks(fileChunk, "<error", "/>");
for (final String errorChunk : errors) {
final Integer line = getIntegerAttribute(errorChunk, "line");
final Optional<Integer> column = findIntegerAttribute(errorChunk, "column");
final String severity = getAttribute(errorChunk, "severity");
final String message = getAttribute(errorChunk, "message");
final String rule = findAttribute(errorChunk, "source").orNull();
violations.add( //
violationBuilder() //
.setParser(CHECKSTYLE) //
Expand Down
31 changes: 27 additions & 4 deletions src/test/java/se/bjurr/violations/lib/CheckstyleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
import static se.bjurr.violations.lib.reports.Parser.CHECKSTYLE;

import java.util.List;

import org.junit.Test;

import se.bjurr.violations.lib.model.Violation;

public class CheckstyleTest {

@Test
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();
final String rootFolder = getRootFolder();

List<Violation> actual =
final List<Violation> actual =
violationsReporterApi() //
.withPattern(".*/checkstyle/main\\.xml$") //
.inFolder(rootFolder) //
Expand Down Expand Up @@ -79,9 +81,9 @@ public void testThatViolationsCanBeParsed() {

@Test
public void testThatPHPViolationsCanBeParsed() {
String rootFolder = getRootFolder();
final String rootFolder = getRootFolder();

List<Violation> actual =
final List<Violation> actual =
violationsReporterApi() //
.withPattern(".*/checkstyle/phpcheckstyle\\.xml$") //
.inFolder(rootFolder) //
Expand All @@ -97,4 +99,25 @@ public void testThatPHPViolationsCanBeParsed() {
assertThat(actual.get(0).getReporter()) //
.isEqualTo("PHP");
}

@Test
public void testThatPHPViolationsCanBeParsedIfNoSource() {
final String rootFolder = getRootFolder();

final List<Violation> actual =
violationsReporterApi() //
.withPattern(".*/checkstyle/checkstyle-no-source\\.xml$") //
.inFolder(rootFolder) //
.findAll(CHECKSTYLE) //
.withReporter("PHP") //
.violations();

assertThat(actual) //
.hasSize(1);

assertThat(actual.get(0).getMessage()) //
.isEqualTo("Must have at least one statement.");
assertThat(actual.get(0).getReporter()) //
.isEqualTo("PHP");
}
}
6 changes: 6 additions & 0 deletions src/test/resources/checkstyle/checkstyle-no-source.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="5.9">
<file name="nosource.php">
<error line="11" column="33" severity="info" message="Must have at least one statement."/>
</file>
</checkstyle>

0 comments on commit dd52241

Please sign in to comment.