Skip to content

Commit

Permalink
Ignoring case when checking for equality JENKINS-48669
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Dec 21, 2017
1 parent 8c1de7f commit 4d7069f
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 148 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
Changelog of Violations lib.

## Unreleased
### Jira JENKINS-48669

**Ignoring case when checking for equality**


[d0dd37fb3e50ddc](https://github.com/tomasbjerre/violations-lib/commit/d0dd37fb3e50ddc) Tomas Bjerre *2017-12-21 10:10:07*


### No issue

**Doc**


[574ab91c57e497f](https://github.com/tomasbjerre/violations-lib/commit/574ab91c57e497f) Tomas Bjerre *2017-12-03 19:37:00*
[8c1de7f777b3c0e](https://github.com/tomasbjerre/violations-lib/commit/8c1de7f777b3c0e) Tomas Bjerre *2017-12-06 18:23:43*


## 1.36
Expand All @@ -36,7 +44,7 @@ Changelog of Violations lib.


## 1.34
### GitHub [#27](https://github.com/tomasbjerre/violations-lib/issues/27) PMD may miss ruleSet
### GitHub [#27](https://github.com/tomasbjerre/violations-lib/issues/27) PMD may miss ruleSet and externalInfoUrl

**Accepting PMD files without ruleset-tag**

Expand Down
26 changes: 13 additions & 13 deletions src/main/java/se/bjurr/violations/lib/parsers/CPDParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,31 @@ private SEVERITY getSeverity(Integer from) {

@Override
public List<Violation> parseReportOutput(String string) throws Exception {
List<Violation> violations = new ArrayList<>();
final List<Violation> violations = new ArrayList<>();
try (InputStream input = new ByteArrayInputStream(string.getBytes())) {

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

List<String> files = new ArrayList<>();
List<Integer> filesLine = new ArrayList<>();
final List<String> files = new ArrayList<>();
final List<Integer> filesLine = new ArrayList<>();
Integer tokens = null;
while (xmlr.hasNext()) {
int eventType = xmlr.next();
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equals("duplication")) {
if (xmlr.getLocalName().equalsIgnoreCase("duplication")) {
tokens = getIntegerAttribute(xmlr, "tokens");
}
if (xmlr.getLocalName().equals("file")) {
if (xmlr.getLocalName().equalsIgnoreCase("file")) {
files.add(getAttribute(xmlr, "path"));
filesLine.add(getIntegerAttribute(xmlr, "line"));
}
if (xmlr.getLocalName().equals("codefragment")) {
String codefragment = xmlr.getElementText().trim();
if (xmlr.getLocalName().equalsIgnoreCase("codefragment")) {
final String codefragment = xmlr.getElementText().trim();
for (int i = 0; i < filesLine.size(); i++) {
String file = files.get(i);
Integer line = filesLine.get(i);
Violation violation =
final String file = files.get(i);
final Integer line = filesLine.get(i);
final Violation violation =
violationBuilder() //
.setParser(CODENARC) //
.setFile(file) //
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/se/bjurr/violations/lib/parsers/CodeNarcParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ private SEVERITY getSeverity(Integer from) {

@Override
public List<Violation> parseReportOutput(String string) throws Exception {
List<Violation> violations = new ArrayList<>();
Map<String, String> rules = new HashMap<>();
final List<Violation> violations = new ArrayList<>();
final Map<String, String> rules = new HashMap<>();
try (InputStream input = new ByteArrayInputStream(string.getBytes())) {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlr = factory.createXMLStreamReader(input);
final XMLInputFactory factory = XMLInputFactory.newInstance();
final XMLStreamReader xmlr = factory.createXMLStreamReader(input);
String name = null;
String description = null;
while (xmlr.hasNext()) {
int eventType = xmlr.next();
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equals("Rule")) {
if (xmlr.getLocalName().equalsIgnoreCase("Rule")) {
name = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equals("Description")) {
if (xmlr.getLocalName().equalsIgnoreCase("Description")) {
description = xmlr.getElementText().trim();
rules.put(name, description);
}
Expand All @@ -57,27 +57,27 @@ public List<Violation> parseReportOutput(String string) throws Exception {

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

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

String path = null;
String name = null;
String ruleName = null;
Integer priority = null;
Integer lineNumber = null;
while (xmlr.hasNext()) {
int eventType = xmlr.next();
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equals("Package")) {
if (xmlr.getLocalName().equalsIgnoreCase("Package")) {
path = getAttribute(xmlr, "path");
}
if (xmlr.getLocalName().equals("File")) {
if (xmlr.getLocalName().equalsIgnoreCase("File")) {
name = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equals("Violation")) {
if (xmlr.getLocalName().equalsIgnoreCase("Violation")) {
ruleName = getAttribute(xmlr, "ruleName");
priority = getIntegerAttribute(xmlr, "priority");
String lineNumberString = getAttribute(xmlr, "lineNumber");
final String lineNumberString = getAttribute(xmlr, "lineNumber");
lineNumber = 1;
if (lineNumberString != null && !lineNumberString.isEmpty()) {
lineNumber = Integer.parseInt(lineNumberString);
Expand All @@ -86,7 +86,7 @@ public List<Violation> parseReportOutput(String string) throws Exception {
if (message == null) {
message = ruleName;
}
Violation violation =
final Violation violation =
violationBuilder() //
.setParser(CODENARC) //
.setFile(path + "/" + name) //
Expand Down
53 changes: 27 additions & 26 deletions src/main/java/se/bjurr/violations/lib/parsers/FindbugsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,25 @@ public static void setFindbugsMessagesXml(String findbugsMessagesXml) {
}

private Map<String, String> getMessagesPerType() {
Map<String, String> messagesPerType = new HashMap<>();
final Map<String, String> messagesPerType = new HashMap<>();
try {
if (isNullOrEmpty(findbugsMessagesXml)) {
String messagesResourceFilename = "/findbugs/messages.xml";
URL resource = FindbugsParser.class.getResource(messagesResourceFilename);
final String messagesResourceFilename = "/findbugs/messages.xml";
final URL resource = FindbugsParser.class.getResource(messagesResourceFilename);
if (resource == null) {
throw new RuntimeException("Unable to find resource " + messagesResourceFilename);
}
findbugsMessagesXml = Utils.toString(resource);
}
List<String> bugPatterns = getChunks(findbugsMessagesXml, "<BugPattern", "</BugPattern>");
for (String bugPattern : bugPatterns) {
String type = getAttribute(bugPattern, "type");
String shortDescription = getContent(bugPattern, "ShortDescription");
String details = getContent(bugPattern, "Details");
final List<String> bugPatterns =
getChunks(findbugsMessagesXml, "<BugPattern", "</BugPattern>");
for (final String bugPattern : bugPatterns) {
final String type = getAttribute(bugPattern, "type");
final String shortDescription = getContent(bugPattern, "ShortDescription");
final String details = getContent(bugPattern, "Details");
messagesPerType.put(type, shortDescription + "\n\n" + details);
}
} catch (IOException e) {
} catch (final IOException e) {
LOG.log(SEVERE, e.getMessage(), e);
}
return messagesPerType;
Expand All @@ -66,27 +67,27 @@ private Map<String, String> getMessagesPerType() {
private void parseBugInstance(
XMLStreamReader xmlr, List<Violation> violations, Map<String, String> messagesPerType)
throws XMLStreamException {
String type = getAttribute(xmlr, "type");
Integer rank = getIntegerAttribute(xmlr, "rank");
final String type = getAttribute(xmlr, "type");
final Integer rank = getIntegerAttribute(xmlr, "rank");
String message = messagesPerType.get(type);
if (message == null) {
message = type;
}
SEVERITY severity = toSeverity(rank);
final SEVERITY severity = toSeverity(rank);

List<Violation> candidates = new ArrayList<>();
final List<Violation> candidates = new ArrayList<>();

while (xmlr.hasNext()) {
int eventType = xmlr.next();
final int eventType = xmlr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (xmlr.getLocalName().equals("SourceLine")) {
Optional<Integer> startLine = findIntegerAttribute(xmlr, "start");
Optional<Integer> endLine = findIntegerAttribute(xmlr, "end");
if (xmlr.getLocalName().equalsIgnoreCase("SourceLine")) {
final Optional<Integer> startLine = findIntegerAttribute(xmlr, "start");
final Optional<Integer> endLine = findIntegerAttribute(xmlr, "end");
if (!startLine.isPresent() || !endLine.isPresent()) {
continue;
}
String filename = getAttribute(xmlr, "sourcepath");
String classname = getAttribute(xmlr, "classname");
final String filename = getAttribute(xmlr, "sourcepath");
final String classname = getAttribute(xmlr, "classname");
candidates.add( //
violationBuilder() //
.setParser(FINDBUGS) //
Expand All @@ -103,7 +104,7 @@ private void parseBugInstance(
}
}
if (eventType == XMLStreamConstants.END_ELEMENT) {
if (xmlr.getLocalName().equals("BugInstance")) {
if (xmlr.getLocalName().equalsIgnoreCase("BugInstance")) {
// End of the bug instance.
break;
}
Expand All @@ -121,18 +122,18 @@ private void parseBugInstance(

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

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

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

while (xmlr.hasNext()) {
int eventType = xmlr.next();
final int eventType = xmlr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (xmlr.getLocalName().equals("BugInstance")) {
if (xmlr.getLocalName().equalsIgnoreCase("BugInstance")) {
parseBugInstance(xmlr, violations, messagesPerType);
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/se/bjurr/violations/lib/parsers/FxCopParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,41 @@ public class FxCopParser implements ViolationsParser {

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

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

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

String targetName = null;
String typeName = null;
String classname = null;
while (xmlr.hasNext()) {
int eventType = xmlr.next();
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equals("Target")) {
if (xmlr.getLocalName().equalsIgnoreCase("Target")) {
targetName = getAttribute(xmlr, "Name").replaceAll("\\\\", "/");
}
if (xmlr.getLocalName().equals("Message")) {
if (xmlr.getLocalName().equalsIgnoreCase("Message")) {
typeName = getAttribute(xmlr, "TypeName");
}
if (xmlr.getLocalName().equals("Type")) {
if (xmlr.getLocalName().equalsIgnoreCase("Type")) {
classname = getAttribute(xmlr, "Name");
}
if (xmlr.getLocalName().equals("Issue")) {
String level = getAttribute(xmlr, "Level");
Optional<String> path = ViolationParserUtils.findAttribute(xmlr, "Path");
if (xmlr.getLocalName().equalsIgnoreCase("Issue")) {
final String level = getAttribute(xmlr, "Level");
final Optional<String> path = ViolationParserUtils.findAttribute(xmlr, "Path");
if (!path.isPresent()) {
LOG.log(FINE, "Ignoring project level issue");
continue;
}
String fileName = getAttribute(xmlr, "File");
Integer line = getIntegerAttribute(xmlr, "Line");
String message = xmlr.getElementText().replaceAll("\\s+", " ");
final String fileName = getAttribute(xmlr, "File");
final Integer line = getIntegerAttribute(xmlr, "Line");
final String message = xmlr.getElementText().replaceAll("\\s+", " ");

String filename = path.get() + "/" + fileName;
SEVERITY severity = toSeverity(level);
final String filename = path.get() + "/" + fileName;
final SEVERITY severity = toSeverity(level);
violations.add( //
violationBuilder() //
.setParser(FXCOP) //
Expand Down
42 changes: 21 additions & 21 deletions src/main/java/se/bjurr/violations/lib/parsers/GendarmeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,53 @@
public class GendarmeParser implements ViolationsParser {

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

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

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

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

String name = null;
String problem = null;
String solution = null;
while (xmlr.hasNext()) {
int eventType = xmlr.next();
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equals("rule")) {
if (xmlr.getLocalName().equalsIgnoreCase("rule")) {
name = getAttribute(xmlr, "Name");
}
if (xmlr.getLocalName().equals("problem")) {
if (xmlr.getLocalName().equalsIgnoreCase("problem")) {
problem = xmlr.getElementText().trim();
}
if (xmlr.getLocalName().equals("solution")) {
if (xmlr.getLocalName().equalsIgnoreCase("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 (xmlr.getLocalName().equalsIgnoreCase("defect")) {
final String severityString = getAttribute(xmlr, "Severity");
final String source = getAttribute(xmlr, "Source");
final SEVERITY severity = getSeverity(severityString);
final String message = problem + "\n\n" + solution;
final Pattern pattern = Pattern.compile("^(.*)\\(.([0-9]*)\\)$");
final Matcher matcher = pattern.matcher(source);
if (matcher.matches()) {
String filepath = matcher.group(1);
Integer lineNumber = Integer.parseInt(matcher.group(2));
Violation violation =
final String filepath = matcher.group(1);
final Integer lineNumber = Integer.parseInt(matcher.group(2));
final Violation violation =
violationBuilder() //
.setParser(GENDARME) //
.setFile(filepath) //
Expand Down
Loading

0 comments on commit 4d7069f

Please sign in to comment.