forked from checkstyle/checkstyle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#5879: Suppression xpath single filter
- Loading branch information
1 parent
e2f7bb6
commit afd6b0c
Showing
5 changed files
with
225 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
60 changes: 60 additions & 0 deletions
60
src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathSingleFilter.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,60 @@ | ||
package com.puppycrawl.tools.checkstyle.filters; | ||
|
||
import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent; | ||
import com.puppycrawl.tools.checkstyle.TreeWalkerFilter; | ||
import com.puppycrawl.tools.checkstyle.api.AutomaticBean; | ||
import com.puppycrawl.tools.checkstyle.api.CheckstyleException; | ||
|
||
/** | ||
* This filter processes {@link TreeWalkerAuditEvent} | ||
* objects based on the criteria of files, checks, message, module id, xpathQuery. | ||
*/ | ||
public class SuppressionXpathSingleFilter extends AutomaticBean implements | ||
TreeWalkerFilter { | ||
|
||
/** The pattern for file names. */ | ||
private String files; | ||
|
||
/** The pattern for check class names. */ | ||
private String checks; | ||
|
||
/** The pattern for message names. */ | ||
private String message; | ||
|
||
/** Module id filter. */ | ||
private String id; | ||
|
||
/** Xpath query. */ | ||
private String query; | ||
|
||
public void setFiles(String files) { | ||
this.files = files; | ||
} | ||
|
||
public void setChecks(String checks) { | ||
this.checks = checks; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public void setQuery(String query) { | ||
this.query = query; | ||
} | ||
|
||
@Override | ||
public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) { | ||
XpathFilter xpathFilter = new XpathFilter(files, checks, message, id, query); | ||
return xpathFilter.accept(treeWalkerAuditEvent); | ||
} | ||
|
||
@Override | ||
protected void finishLocalSetup() throws CheckstyleException { | ||
// No code by default | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathSingleFilterTest.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,74 @@ | ||
package com.puppycrawl.tools.checkstyle.filters; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport; | ||
import com.puppycrawl.tools.checkstyle.JavaParser; | ||
import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent; | ||
import com.puppycrawl.tools.checkstyle.api.*; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class SuppressionXpathSingleFilterTest | ||
extends AbstractModuleTestSupport { | ||
private File file; | ||
private FileContents fileContents; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
file = new File(getPath("InputSuppressionXpathSingleFilter.java")); | ||
fileContents = new FileContents(new FileText(file, | ||
StandardCharsets.UTF_8.name())); | ||
} | ||
|
||
@Override | ||
protected String getPackageLocation() { | ||
return "com/puppycrawl/tools/checkstyle/filters/suppressionxpathsinglefilter"; | ||
} | ||
|
||
@Test | ||
public void testDefault() throws Exception { | ||
final String xpath = "/CLASS_DEF[@text='InputTest']" + | ||
"//METHOD_DEF[@text='changeAge']//ASSIGN[@text='age']/IDENT"; | ||
final SuppressionXpathSingleFilter filter = createSuppressionXpathSingleFilter( | ||
"InputSuppressionXpathSingleFilter", null, null, null, null); | ||
final TreeWalkerAuditEvent ev = getEvent(3, 8, TokenTypes.VARIABLE_DEF); | ||
assertFalse("Event should be rejected", filter.accept(ev)); | ||
} | ||
|
||
@Test | ||
public void testRequireThis() throws Exception { | ||
final String xpath = "/CLASS_DEF[@text='InputTest']" + | ||
"//METHOD_DEF[@text='changeAge']//ASSIGN[@text='age']/IDENT"; | ||
final SuppressionXpathSingleFilter filter = createSuppressionXpathSingleFilter( | ||
"InputSuppressionXpathSingleFilter", "RequireThis", null, null, xpath); | ||
final TreeWalkerAuditEvent ev = getEvent(3, 8, TokenTypes.VARIABLE_DEF); | ||
assertTrue("Event should be accepted", filter.accept(ev)); | ||
} | ||
|
||
private static SuppressionXpathSingleFilter createSuppressionXpathSingleFilter( | ||
String files, String checks, String message, String id, String query) | ||
throws CheckstyleException { | ||
final SuppressionXpathSingleFilter filter = new SuppressionXpathSingleFilter(); | ||
filter.setFiles(files); | ||
filter.setChecks(checks); | ||
filter.setMessage(message); | ||
filter.setId(id); | ||
filter.setQuery(query); | ||
filter.finishLocalSetup(); | ||
return filter; | ||
} | ||
|
||
private TreeWalkerAuditEvent getEvent(int line, int column, int tokenType) | ||
throws Exception { | ||
final LocalizedMessage message = | ||
new LocalizedMessage(line, column, tokenType, "", "", null, null, null, | ||
getClass(), null); | ||
return new TreeWalkerAuditEvent(fileContents, file.getName(), message, | ||
JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS)); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ls/checkstyle/filters/suppressionxpathsinglefilter/InputSuppressionXpathSingleFilter.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,9 @@ | ||
package com.puppycrawl.tools.checkstyle.filters.suppressionxpathsinglefilter; | ||
|
||
public class InputSuppressionXpathSingleFilter { | ||
private int age = 23; | ||
|
||
public void changeAge() { | ||
age = 24; | ||
} | ||
} |
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