Skip to content

Commit

Permalink
Issue checkstyle#6490: Add base structure for Sun Style Coverage Report
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed Apr 8, 2019
1 parent f80901a commit 0a39068
Show file tree
Hide file tree
Showing 3 changed files with 936 additions and 56 deletions.
13 changes: 7 additions & 6 deletions config/pmd-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,16 @@

<rule ref="category/java/design.xml/UseObjectForClearerAPI">
<properties>
<!-- This is checking for amount of arguments more that 3 (hardcoded).
But in make sense to start making violations on items where
amount of arguments more that 7. Extra abstraction(object)
for single method does not make code more readable or
easy to maintain -->
<!-- This is checking for amount of String arguments more than 3 (hardcoded).
Extra abstraction(object) for single method does not make code more readable or
easy to maintain. The rule should also only check public methods but has a bug.
Suppress the false-positives. -->
<property name="violationSuppressXPath"
value="//ClassOrInterfaceDeclaration[
@Image='SuppressionXpathSingleFilterTest']
//MethodDeclarator[@Image='createSuppressionXpathSingleFilter']"/>
//MethodDeclarator[@Image='createSuppressionXpathSingleFilter']
| //ClassOrInterfaceDeclaration[ @Image='XdocsPagesTest']
//MethodDeclarator[@Image='validateRuleNameOrder']"/>
</properties>
</rule>
</ruleset>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

package com.puppycrawl.tools.checkstyle.internal;

import static java.lang.Integer.parseInt;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.CoreMatchers.describedAs;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;

import java.beans.PropertyDescriptor;
import java.io.File;
Expand All @@ -46,6 +50,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Assert;
Expand Down Expand Up @@ -1347,6 +1352,7 @@ else if (path.toFile().getName().contains("sun")) {
}

String lastRuleName = null;
String[] lastRuleNameParts = null;

for (int position = 0; position < sources.getLength(); position++) {
final Node row = sources.item(position);
Expand All @@ -1358,14 +1364,8 @@ else if (path.toFile().getName().contains("sun")) {
}

final String ruleName = columns.get(1).getTextContent().trim();

if (lastRuleName != null) {
Assert.assertTrue(
fileName + " rule '" + ruleName + "' is out of order compared to '"
+ lastRuleName + "'",
ruleName.toLowerCase(Locale.ENGLISH).compareTo(
lastRuleName.toLowerCase(Locale.ENGLISH)) >= 0);
}
lastRuleNameParts = validateRuleNameOrder(
fileName, lastRuleName, lastRuleNameParts, ruleName);

if (!"--".equals(ruleName)) {
validateStyleAnchors(XmlUtil.findChildElementsByTag(columns.get(0), "a"),
Expand All @@ -1389,6 +1389,86 @@ else if (path.toFile().getName().contains("sun")) {
}
}

private static String[] validateRuleNameOrder(String fileName, String lastRuleName,
String[] lastRuleNameParts, String ruleName) {
final boolean[] beforeFirstSpace = {true};
final int ruleNumberPartsAmount = (int) ruleName.chars()
.peek(character -> {
if (character == ' ') {
beforeFirstSpace[0] = false;
}
})
.filter(character -> beforeFirstSpace[0] && character == '.')
.count() + 1;
final String[] ruleNameParts = ruleName.split("[. ]", ruleNumberPartsAmount + 1);

if (lastRuleName != null) {
final int lastRuleNumberPartsAmount = lastRuleNameParts.length - 1;
boolean lastRuleNumberPartWasEqual = false;
final String outOfOrderReason = fileName + " rule '" + ruleName
+ "' is out of order compared to '" + lastRuleName + "'";
int partIndex;
for (partIndex = 0; partIndex < ruleNumberPartsAmount; partIndex++) {
if (lastRuleNumberPartsAmount <= partIndex) {
// equal up to here and last rule has less parts,
// thus order is correct, stop comparing
break;
}

final String ruleNumberPart = ruleNameParts[partIndex];
final String lastRuleNumberPart = lastRuleNameParts[partIndex];
final boolean ruleNumberPartsAreNumeric = IntStream.concat(
ruleNumberPart.chars(),
lastRuleNumberPart.chars()
).allMatch(Character::isDigit);

if (ruleNumberPartsAreNumeric) {
final int numericRuleNumberPart = parseInt(ruleNumberPart);
final int numericLastRuleNumberPart = parseInt(lastRuleNumberPart);
Assert.assertThat(
outOfOrderReason,
numericRuleNumberPart < numericLastRuleNumberPart,
describedAs("'%0' should not be less than '%1'",
is(false),
numericRuleNumberPart, numericLastRuleNumberPart));
}
else {
Assert.assertThat(
outOfOrderReason,
ruleNumberPart.compareToIgnoreCase(lastRuleNumberPart) > 0,
describedAs("'%0' should not be greater than '%1'",
is(false),
ruleNumberPart, lastRuleNumberPart));
}
lastRuleNumberPartWasEqual = ruleNumberPart.equalsIgnoreCase(lastRuleNumberPart);
}
if (ruleNumberPartsAmount == partIndex && lastRuleNumberPartWasEqual) {
if (lastRuleNumberPartsAmount == partIndex) {
final String ruleNamePart = ruleNameParts[partIndex];
final String lastRuleNamePart = lastRuleNameParts[partIndex];
final int order = ruleNamePart.compareToIgnoreCase(lastRuleNamePart);
Assert.assertThat(
fileName + " rule '" + ruleName + "' is appearing twice",
order,
describedAs("'%0' should not be equal to '%1'",
is(not(0)),
ruleName, lastRuleName));
Assert.assertThat(
outOfOrderReason,
order > 0,
describedAs("'%0' should be greater than '%1'",
is(true),
ruleNamePart, lastRuleNamePart));
}
else {
Assert.fail(outOfOrderReason);
}
}
}

return ruleNameParts;
}

private static void validateStyleAnchors(Set<Node> anchors, String fileName, String ruleName) {
Assert.assertEquals(fileName + " rule '" + ruleName + "' must have two row anchors", 2,
anchors.size());
Expand Down
Loading

0 comments on commit 0a39068

Please sign in to comment.