Skip to content

Commit

Permalink
Issue checkstyle#11460: Fixed the default behavior of SuppressoinXpat…
Browse files Browse the repository at this point in the history
…hSingleFilter
  • Loading branch information
as23415672 committed May 24, 2022
1 parent 8b03a0a commit a97bbef
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 3 deletions.
Expand Up @@ -70,6 +70,9 @@ public class XpathFilterElement implements TreeWalkerFilter {
/** Xpath query. */
private final String xpathQuery;

/** Indicates if all properties are set to Null. */
private final boolean areAllPropertiesNull;

/**
* Creates a {@code XpathElement} instance.
*
Expand Down Expand Up @@ -118,6 +121,11 @@ public XpathFilterElement(String files, String checks,
throw new IllegalArgumentException("Unexpected xpath query: " + xpathQuery, ex);
}
}
areAllPropertiesNull = fileRegexp == null
&& checkRegexp == null
&& messageRegexp == null
&& moduleId == null
&& xpathExpression == null;
}

/**
Expand Down Expand Up @@ -171,13 +179,29 @@ public XpathFilterElement(Pattern files, Pattern checks, Pattern message,
throw new IllegalArgumentException("Incorrect xpath query: " + xpathQuery, ex);
}
}
areAllPropertiesNull = fileRegexp == null
&& checkRegexp == null
&& messageRegexp == null
&& moduleId == null
&& xpathExpression == null;
}

@Override
public boolean accept(TreeWalkerAuditEvent event) {
return !isFileNameAndModuleAndModuleNameMatching(event)
|| !isMessageNameMatching(event)
|| !isXpathQueryMatching(event);
|| !isXpathQueryMatching(event)
|| areAllPropertiesNull();
}

/**
* Check if all properties are set to null.
*
* @return true if all properties are set to null
*/

private boolean areAllPropertiesNull() {
return areAllPropertiesNull;
}

/**
Expand Down Expand Up @@ -261,7 +285,8 @@ private List<Item> getItems(TreeWalkerAuditEvent event) {

@Override
public int hashCode() {
return Objects.hash(filePattern, checkPattern, messagePattern, moduleId, xpathQuery);
return Objects.hash(filePattern, checkPattern, messagePattern,
moduleId, xpathQuery, areAllPropertiesNull);
}

@Override
Expand All @@ -277,7 +302,8 @@ public boolean equals(Object other) {
&& Objects.equals(checkPattern, xpathFilter.checkPattern)
&& Objects.equals(messagePattern, xpathFilter.messagePattern)
&& Objects.equals(moduleId, xpathFilter.moduleId)
&& Objects.equals(xpathQuery, xpathFilter.xpathQuery);
&& Objects.equals(xpathQuery, xpathFilter.xpathQuery)
&& Objects.equals(areAllPropertiesNull, xpathFilter.areAllPropertiesNull);
}

}
Expand Up @@ -327,6 +327,79 @@ public void testThrowException() throws Exception {
}
}

@Test
public void testAllNullConfiguration() throws Exception {
final String[] expected = {
"18:1: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
};

final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;

verifyFilterWithInlineConfigParser(
getPath("InputSuppressionXpathSingleFilterAllNullConfiguration.java"),
expected, removeSuppressed(expected, suppressed));
}

@Test
public void testDecideByIdAndExpression() throws Exception {
final String[] expected = {
"20:1: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
};

final String[] suppressed = {
"20:1: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
};

verifyFilterWithInlineConfigParser(
getPath("InputSuppressionXpathSingleFilterDecideByIdAndExpression.java"),
expected, removeSuppressed(expected, suppressed));
}

@Test
public void testDefaultFileProperty() throws Exception {
final String[] expected = {
"20:1: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
};

final String[] suppressed = {
"20:1: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
};

verifyFilterWithInlineConfigParser(
getPath("InputSuppressionXpathSingleFilterDefaultFileProperty.java"),
expected, removeSuppressed(expected, suppressed));
}

@Test
public void testDecideByCheck() throws Exception {
final String[] expected = {
"18:1: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
};

final String[] suppressed = {
"18:1: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
};

verifyFilterWithInlineConfigParser(
getPath("InputSuppressionXpathSingleFilterDecideByCheck.java"),
expected, removeSuppressed(expected, suppressed));
}

@Test
public void testDecideById() throws Exception {
final String[] expected = {
"19:1: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
};

final String[] suppressed = {
"19:1: " + getCheckMessage(MissingJavadocTypeCheck.class, MSG_JAVADOC_MISSING),
};

verifyFilterWithInlineConfigParser(
getPath("InputSuppressionXpathSingleFilterDecideById.java"),
expected, removeSuppressed(expected, suppressed));
}

private static SuppressionXpathSingleFilter createSuppressionXpathSingleFilter(
String files, String checks, String message, String moduleId, String query) {
final SuppressionXpathSingleFilter filter = new SuppressionXpathSingleFilter();
Expand Down
@@ -0,0 +1,19 @@
/*
SuppressionXpathSingleFilter
files = (default)(null)
checks = (default)(null)
message = (default)(null)
id = (default)(null)
query = (default)(null)
com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck
scope = (default)public
excludeScope = (default)(null)
skipAnnotations = (default)Generated
tokens = CLASS_DEF
*/
package com.puppycrawl.tools.checkstyle.filters.suppressionxpathsinglefilter;

public class InputSuppressionXpathSingleFilterAllNullConfiguration { // violation
}
@@ -0,0 +1,20 @@
/*
SuppressionXpathSingleFilter
files = (default)(null)
checks = MissingJavadocTypeCheck
message = (default)(null)
id = (default)(null)
query = (default)(null)
com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck
scope = (default)public
excludeScope = (default)(null)
skipAnnotations = (default)Generated
tokens = CLASS_DEF
*/
package com.puppycrawl.tools.checkstyle.filters.suppressionxpathsinglefilter;

public class InputSuppressionXpathSingleFilterDecideByCheck { // filtered violation

}
@@ -0,0 +1,21 @@
/*
SuppressionXpathSingleFilter
files = (default)(null)
checks = (default)(null)
message = (default)(null)
id = 007
query = (default)(null)
com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck
id = 007
scope = (default)public
excludeScope = (default)(null)
skipAnnotations = (default)Generated
tokens = CLASS_DEF
*/
package com.puppycrawl.tools.checkstyle.filters.suppressionxpathsinglefilter;

public class InputSuppressionXpathSingleFilterDecideById { // filtered violation

}
@@ -0,0 +1,22 @@
/*
SuppressionXpathSingleFilter
files = (default)(null)
checks = (default)(null)
message = (default)(null)
id = 007
query = /COMPILATION_UNIT/CLASS_DEF \
[./IDENT[@text='InputSuppressionXpathSingleFilterDecideByIdAndExpression']]
com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck
id = 007
scope = (default)public
excludeScope = (default)(null)
skipAnnotations = (default)Generated
tokens = CLASS_DEF
*/
package com.puppycrawl.tools.checkstyle.filters.suppressionxpathsinglefilter;

public class InputSuppressionXpathSingleFilterDecideByIdAndExpression { // filtered violation

}
@@ -0,0 +1,22 @@
/*
SuppressionXpathSingleFilter
files = (default)(null)
checks = MissingJavadocTypeCheck
message = Missing a Javadoc comment
id = 007
query = /COMPILATION_UNIT/CLASS_DEF \
[./IDENT[@text='InputSuppressionXpathSingleFilterDefaultFileProperty']]
com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck
id = 007
scope = (default)public
excludeScope = (default)(null)
skipAnnotations = (default)Generated
tokens = CLASS_DEF
*/
package com.puppycrawl.tools.checkstyle.filters.suppressionxpathsinglefilter;

public class InputSuppressionXpathSingleFilterDefaultFileProperty { // filtered violation

}

0 comments on commit a97bbef

Please sign in to comment.