forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#6207: Add XPath IT Regression for VisibilityModifier
- Loading branch information
Showing
6 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionVisibilityModifierTest.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,130 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code and other text files for adherence to a set of rules. | ||
// Copyright (C) 2001-2024 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
/////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
package org.checkstyle.suppressionxpathfilter; | ||
|
||
import static com.puppycrawl.tools.checkstyle.checks.design.VisibilityModifierCheck.MSG_KEY; | ||
|
||
import java.io.File; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.checks.design.VisibilityModifierCheck; | ||
|
||
public class XpathRegressionVisibilityModifierTest extends AbstractXpathTestSupport { | ||
|
||
private final String checkName = VisibilityModifierCheck.class.getSimpleName(); | ||
|
||
@Override | ||
protected String getCheckName() { | ||
return checkName; | ||
} | ||
|
||
@Test | ||
public void testDefaultModifier() throws Exception { | ||
final File fileToProcess = | ||
new File(getPath("SuppressionXpathRegressionVisibilityModifierDefault.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(VisibilityModifierCheck.class); | ||
|
||
final String[] expectedViolation = { | ||
"6:9: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY, "field"), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Collections.singletonList( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionVisibilityModifierDefault']]" | ||
+ "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='field']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
|
||
@Test | ||
public void testAnnotation() throws Exception { | ||
final File fileToProcess = | ||
new File(getPath("SuppressionXpathRegressionVisibilityModifierAnnotation.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(VisibilityModifierCheck.class); | ||
moduleConfig.addProperty("ignoreAnnotationCanonicalNames", "Deprecated"); | ||
|
||
final String[] expectedViolation = { | ||
"5:12: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY, | ||
"annotatedString"), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Collections.singletonList( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionVisibilityModifierAnnotation']]" | ||
+ "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='annotatedString']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
|
||
@Test | ||
public void testAnonymousClass() throws Exception { | ||
final File fileToProcess = | ||
new File(getPath("SuppressionXpathRegressionVisibilityModifierAnonymous.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(VisibilityModifierCheck.class); | ||
|
||
final String[] expectedViolation = { | ||
"6:23: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY, "field1"), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Collections.singletonList( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionVisibilityModifierAnonymous']]" | ||
+ "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='runnable']]" | ||
+ "/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Runnable']]" | ||
+ "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='field1']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
|
||
@Test | ||
public void testInnerClass() throws Exception { | ||
final File fileToProcess = | ||
new File(getPath("SuppressionXpathRegressionVisibilityModifierInner.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(VisibilityModifierCheck.class); | ||
|
||
final String[] expectedViolation = { | ||
"7:20: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY, "field2"), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Collections.singletonList( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionVisibilityModifierInner']]" | ||
+ "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/" | ||
+ "VARIABLE_DEF/IDENT[@text='field2']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...pathfilter/visibilitymodifier/SuppressionXpathRegressionVisibilityModifierAnnotation.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 org.checkstyle.suppressionxpathfilter.visibilitymodifier; | ||
|
||
public class SuppressionXpathRegressionVisibilityModifierAnnotation { | ||
@java.lang.Deprecated | ||
String annotatedString; // warn | ||
|
||
@Deprecated | ||
String shortCustomAnnotated; | ||
} |
13 changes: 13 additions & 0 deletions
13
...xpathfilter/visibilitymodifier/SuppressionXpathRegressionVisibilityModifierAnonymous.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,13 @@ | ||
package org.checkstyle.suppressionxpathfilter.visibilitymodifier; | ||
|
||
public class SuppressionXpathRegressionVisibilityModifierAnonymous { | ||
private Runnable runnable = new Runnable() { | ||
|
||
public String field1; // warn | ||
|
||
@Override | ||
public void run() { | ||
|
||
} | ||
}; | ||
} |
7 changes: 7 additions & 0 deletions
7
...onxpathfilter/visibilitymodifier/SuppressionXpathRegressionVisibilityModifierDefault.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,7 @@ | ||
package org.checkstyle.suppressionxpathfilter.visibilitymodifier; | ||
|
||
public class SuppressionXpathRegressionVisibilityModifierDefault { | ||
private int myPrivateField; // ok, private class member is allowed | ||
|
||
int field; // warn | ||
} |
9 changes: 9 additions & 0 deletions
9
...sionxpathfilter/visibilitymodifier/SuppressionXpathRegressionVisibilityModifierInner.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 org.checkstyle.suppressionxpathfilter.visibilitymodifier; | ||
|
||
public class SuppressionXpathRegressionVisibilityModifierInner { | ||
class InnerClass { | ||
private int field1; | ||
|
||
public int field2; // warn | ||
} | ||
} |
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