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 Regression Test for FinalParameters
- Loading branch information
Showing
5 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
190 changes: 190 additions & 0 deletions
190
src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionFinalParametersTest.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,190 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////////////// | ||
// 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 java.io.File; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck; | ||
|
||
public class XpathRegressionFinalParametersTest extends AbstractXpathTestSupport { | ||
|
||
private final String checkName = FinalParametersCheck.class.getSimpleName(); | ||
|
||
@Override | ||
protected String getCheckName() { | ||
return checkName; | ||
} | ||
|
||
@Test | ||
public void testOne() throws Exception { | ||
final File fileToProcess = | ||
new File(getPath("SuppressionXpathRegressionFinalParameters1.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(FinalParametersCheck.class); | ||
|
||
final String[] expectedViolation = { | ||
"5:24: " + getCheckMessage(FinalParametersCheck.class, | ||
FinalParametersCheck.MSG_KEY, "argOne"), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = List.of( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters1']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]" | ||
+ "/PARAMETERS", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters1']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]" | ||
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters1']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]" | ||
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/MODIFIERS", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters1']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]" | ||
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters1']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]" | ||
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE/LITERAL_INT" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
|
||
@Test | ||
public void testTwo() throws Exception { | ||
final File fileToProcess = | ||
new File(getPath("SuppressionXpathRegressionFinalParameters2.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(FinalParametersCheck.class); | ||
|
||
moduleConfig.addProperty("tokens", "CTOR_DEF"); | ||
|
||
final String[] expectedViolation = { | ||
"5:55: " + getCheckMessage(FinalParametersCheck.class, | ||
FinalParametersCheck.MSG_KEY, "argOne"), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = List.of( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters2']]" | ||
+ "/OBJBLOCK/CTOR_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionFinalParameters2']]" | ||
+ "/PARAMETERS", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters2']]" | ||
+ "/OBJBLOCK/CTOR_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionFinalParameters2']]" | ||
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters2']]" | ||
+ "/OBJBLOCK/CTOR_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionFinalParameters2']]" | ||
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/MODIFIERS", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters2']]" | ||
+ "/OBJBLOCK/CTOR_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionFinalParameters2']]" | ||
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters2']]" | ||
+ "/OBJBLOCK/CTOR_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionFinalParameters2']]" | ||
+ "/PARAMETERS/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE/LITERAL_INT" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
|
||
@Test | ||
public void testThree() throws Exception { | ||
final File fileToProcess = | ||
new File(getPath("SuppressionXpathRegressionFinalParameters3.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(FinalParametersCheck.class); | ||
|
||
moduleConfig.addProperty("ignorePrimitiveTypes", "true"); | ||
|
||
final String[] expectedViolation = { | ||
"11:32: " + getCheckMessage(FinalParametersCheck.class, | ||
FinalParametersCheck.MSG_KEY, "argOne"), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = List.of( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters3']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/" | ||
+ "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR" | ||
+ "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK" | ||
+ "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters3']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/" | ||
+ "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR" | ||
+ "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK" | ||
+ "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS" | ||
+ "/PARAMETER_DEF[./IDENT[@text='argOne']]", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters3']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/" | ||
+ "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR" | ||
+ "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK" | ||
+ "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS" | ||
+ "/PARAMETER_DEF[./IDENT[@text='argOne']]/MODIFIERS", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters3']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/" | ||
+ "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR" | ||
+ "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK" | ||
+ "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS" | ||
+ "/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE[./IDENT[@text='String']]", | ||
|
||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT" | ||
+ "[@text='SuppressionXpathRegressionFinalParameters3']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createClass']]/SLIST/" | ||
+ "VARIABLE_DEF[./IDENT[@text='obj']]/ASSIGN/EXPR" | ||
+ "/LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK" | ||
+ "/METHOD_DEF[./IDENT[@text='method']]/PARAMETERS" | ||
+ "/PARAMETER_DEF[./IDENT[@text='argOne']]/TYPE/IDENT[@text='String']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...le/suppressionxpathfilter/finalparameters/SuppressionXpathRegressionFinalParameters1.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.finalparameters; | ||
|
||
public class SuppressionXpathRegressionFinalParameters1 { | ||
|
||
public void method(int argOne, final int argTwo) { // warn | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...le/suppressionxpathfilter/finalparameters/SuppressionXpathRegressionFinalParameters2.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.finalparameters; | ||
|
||
public class SuppressionXpathRegressionFinalParameters2 { | ||
|
||
public SuppressionXpathRegressionFinalParameters2(int argOne, final int argTwo) { // warn | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...le/suppressionxpathfilter/finalparameters/SuppressionXpathRegressionFinalParameters3.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,14 @@ | ||
package org.checkstyle.suppressionxpathfilter.finalparameters; | ||
|
||
public class SuppressionXpathRegressionFinalParameters3 { | ||
|
||
class AnonymousClass { | ||
public void method(int argOne, int argTwo) {} // ok, int is primitive | ||
} | ||
|
||
public void createClass() { | ||
AnonymousClass obj = new AnonymousClass() { | ||
public void method(String[] argOne, final String[] argTwo) {} // 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