-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a4e651
commit 3ac58cc
Showing
8 changed files
with
1,115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/se/bjurr/violations/lib/parsers/GendarmeParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.