Skip to content

Commit

Permalink
Gendarme
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Nov 5, 2016
1 parent 7a4e651 commit 3ac58cc
Show file tree
Hide file tree
Showing 8 changed files with 1,115 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ It supports:
* [_Findbugs_](http://findbugs.sourceforge.net/)
* [_Flake8_](http://flake8.readthedocs.org/en/latest/) ([_Pep8_](https://github.com/PyCQA/pycodestyle), [_Mccabe_](https://pypi.python.org/pypi/mccabe), [_PyFlakes_](https://pypi.python.org/pypi/pyflakes))
* [_FxCop_](https://en.wikipedia.org/wiki/FxCop)
* [_Gendarme_](http://www.mono-project.com/docs/tools+libraries/tools/gendarme/)
* [_JSHint_](http://jshint.com/)
* _Lint_ A common XML format, used by different linters.
* [_PerlCritic_](https://github.com/Perl-Critic)
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/GendarmeParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package se.bjurr.violations.lib.parsers;

import static com.google.common.collect.Lists.newArrayList;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
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.getAttribute;
import static se.bjurr.violations.lib.reports.Reporter.GENDARME;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

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

public class GendarmeParser implements ViolationsParser {

private SEVERITY getSeverity(String severityString) {
if (severityString.equals("Low")) {
return INFO;
} else if (severityString.equals("Medium")) {
return WARN;
} else if (severityString.equals("High")) {
return ERROR;
} else if (severityString.equals("Critical")) {
return ERROR;
}
return WARN;
}

@Override
public List<Violation> parseFile(String string) throws Exception {
List<Violation> violations = newArrayList();

try (InputStream input = new ByteArrayInputStream(string.getBytes())) {

XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlr = factory.createXMLStreamReader(input);

String name = null;
String problem = null;
String solution = null;
while (xmlr.hasNext()) {
int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equals("rule")) {
name = getAttribute(xmlr, "Name");
}
if (xmlr.getLocalName().equals("problem")) {
problem = xmlr.getElementText().trim();
}
if (xmlr.getLocalName().equals("solution")) {
solution = xmlr.getElementText().trim();
}
if (xmlr.getLocalName().equals("defect")) {
String severityString = getAttribute(xmlr, "Severity");
String source = getAttribute(xmlr, "Source");
SEVERITY severity = getSeverity(severityString);
String message = problem + "\n\n" + solution;
Pattern pattern = Pattern.compile("^(.*)\\(.([0-9]*)\\)$");
Matcher matcher = pattern.matcher(source);
if (matcher.matches()) {
String filepath = matcher.group(1);
Integer lineNumber = Integer.parseInt(matcher.group(2));
Violation violation = violationBuilder()//
.setReporter(GENDARME)//
.setFile(filepath)//
.setMessage(message)//
.setRule(name)//
.setSeverity(severity)//
.setStartLine(lineNumber)//
.build();
violations.add(violation);
}
}
}
}
}
return violations;
}
}
2 changes: 2 additions & 0 deletions src/main/java/se/bjurr/violations/lib/reports/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import se.bjurr.violations.lib.parsers.FindbugsParser;
import se.bjurr.violations.lib.parsers.Flake8Parser;
import se.bjurr.violations.lib.parsers.FxCopParser;
import se.bjurr.violations.lib.parsers.GendarmeParser;
import se.bjurr.violations.lib.parsers.JSHintParser;
import se.bjurr.violations.lib.parsers.LintParser;
import se.bjurr.violations.lib.parsers.PMDParser;
Expand All @@ -43,6 +44,7 @@ public enum Reporter {
FINDBUGS(new FindbugsParser()), //
FLAKE8(new Flake8Parser()), //
FXCOP(new FxCopParser()), //
GENDARME(new GendarmeParser()), //
JSHINT(new JSHintParser()), //
LINT(new LintParser()), //
PERLCRITIC(new PerlCriticParser()), //
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/se/bjurr/violations/lib/GendarmeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package se.bjurr.violations.lib;

import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.reports.Reporter.GENDARME;

import java.util.List;

import org.junit.Test;

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

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

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/gendarme/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(GENDARME) //
.violations();

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

assertThat(actual.get(0).getMessage())//
.startsWith("This me");
assertThat(actual.get(0).getFile())//
.isEqualTo("c:/Dev/src/hudson/Hudson.Domain/Dog.cs");
assertThat(actual.get(0).getSeverity())//
.isEqualTo(INFO);
assertThat(actual.get(0).getRule().get())//
.isEqualTo("MethodCanBeMadeStaticRule");
assertThat(actual.get(0).getStartLine())//
.isEqualTo(10);
assertThat(actual.get(0).getEndLine())//
.isEqualTo(10);

assertThat(actual.get(5).getMessage())//
.startsWith("A constructor ca");
}

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

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/gendarme/Gendarme_unix\\.xml$") //
.inFolder(rootFolder) //
.findAll(GENDARME) //
.violations();

assertThat(actual)//
.hasSize(2);
}
}
Loading

0 comments on commit 3ac58cc

Please sign in to comment.