Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #6207: Add Xpath regression test for LocalFinalVariableName
- Loading branch information
Showing
5 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
...java/org/checkstyle/suppressionxpathfilter/XpathRegressionLocalFinalVariableNameTest.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,115 @@ | ||
/////////////////////////////////////////////////////////////////////////////////////////////// | ||
// 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.naming.LocalFinalVariableNameCheck.MSG_INVALID_PATTERN; | ||
|
||
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.naming.LocalFinalVariableNameCheck; | ||
|
||
public class XpathRegressionLocalFinalVariableNameTest extends AbstractXpathTestSupport { | ||
|
||
private final String checkName = LocalFinalVariableNameCheck.class.getSimpleName(); | ||
|
||
@Override | ||
protected String getCheckName() { | ||
return checkName; | ||
} | ||
|
||
@Test | ||
public void testResource() throws Exception { | ||
final File fileToProcess = | ||
new File(getPath("SuppressionXpathRegressionLocalFinalVariableNameResource.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(LocalFinalVariableNameCheck.class); | ||
moduleConfig.addProperty("format", "^[A-Z][A-Z0-9]*$"); | ||
moduleConfig.addProperty("tokens", "PARAMETER_DEF,RESOURCE"); | ||
|
||
final String[] expectedViolation = { | ||
"7:21: " + getCheckMessage(LocalFinalVariableNameCheck.class, | ||
MSG_INVALID_PATTERN, "scanner", "^[A-Z][A-Z0-9]*$"), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Collections.singletonList( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionLocalFinalVariableNameResource']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/LITERAL_TRY" | ||
+ "/RESOURCE_SPECIFICATION/RESOURCES/RESOURCE/IDENT[@text='scanner']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
|
||
@Test | ||
public void testVariable() throws Exception { | ||
final File fileToProcess = | ||
new File(getPath("SuppressionXpathRegressionLocalFinalVariableNameVar.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(LocalFinalVariableNameCheck.class); | ||
moduleConfig.addProperty("format", "^[A-Z][a-z0-9]*$"); | ||
|
||
final String[] expectedViolation = { | ||
"5:19: " + getCheckMessage(LocalFinalVariableNameCheck.class, | ||
MSG_INVALID_PATTERN, "VAR1", "^[A-Z][a-z0-9]*$"), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Collections.singletonList( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionLocalFinalVariableNameVar']]" | ||
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/VARIABLE_DEF" | ||
+ "/IDENT[@text='VAR1']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
|
||
@Test | ||
public void testInnerClass() throws Exception { | ||
final File fileToProcess = | ||
new File(getPath("SuppressionXpathRegressionLocalFinalVariableNameInner.java")); | ||
|
||
final DefaultConfiguration moduleConfig = | ||
createModuleConfig(LocalFinalVariableNameCheck.class); | ||
moduleConfig.addProperty("format", "^[A-Z][a-z0-9]*$"); | ||
|
||
final String[] expectedViolation = { | ||
"8:23: " + getCheckMessage(LocalFinalVariableNameCheck.class, | ||
MSG_INVALID_PATTERN, "VAR1", "^[A-Z][a-z0-9]*$"), | ||
}; | ||
|
||
final List<String> expectedXpathQueries = Collections.singletonList( | ||
"/COMPILATION_UNIT/CLASS_DEF[./IDENT[" | ||
+ "@text='SuppressionXpathRegressionLocalFinalVariableNameInner']]" | ||
+ "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK" | ||
+ "/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/VARIABLE_DEF" | ||
+ "/IDENT[@text='VAR1']" | ||
); | ||
|
||
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...hfilter/localfinalvariablename/SuppressionXpathRegressionLocalFinalVariableNameInner.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,11 @@ | ||
package org.checkstyle.suppressionxpathfilter.localfinalvariablename; | ||
|
||
import java.util.Scanner; | ||
|
||
public class SuppressionXpathRegressionLocalFinalVariableNameInner { | ||
class InnerClass { | ||
void MyMethod() { | ||
final int VAR1 = 10; // warn | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...lter/localfinalvariablename/SuppressionXpathRegressionLocalFinalVariableNameResource.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,12 @@ | ||
package org.checkstyle.suppressionxpathfilter.localfinalvariablename; | ||
|
||
import java.util.Scanner; | ||
|
||
public class SuppressionXpathRegressionLocalFinalVariableNameResource { | ||
void MyMethod() { | ||
try(Scanner scanner = new Scanner("ABC")) { // warn | ||
final int VAR1 = 5; | ||
final int var1 = 10; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...athfilter/localfinalvariablename/SuppressionXpathRegressionLocalFinalVariableNameVar.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.localfinalvariablename; | ||
|
||
public class SuppressionXpathRegressionLocalFinalVariableNameVar { | ||
void MyMethod() { | ||
final int VAR1 = 5; // 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