Skip to content

Commit bbb28a7

Browse files
Lmh-javaromani
authored andcommitted
Issue #6207: Add Xpath regression test for LocalFinalVariableName
1 parent 1bb3d56 commit bbb28a7

File tree

5 files changed

+145
-1
lines changed

5 files changed

+145
-1
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3+
// Copyright (C) 2001-2024 the original author or authors.
4+
//
5+
// This library is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU Lesser General Public
7+
// License as published by the Free Software Foundation; either
8+
// version 2.1 of the License, or (at your option) any later version.
9+
//
10+
// This library is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// Lesser General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Lesser General Public
16+
// License along with this library; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
///////////////////////////////////////////////////////////////////////////////////////////////
19+
20+
package org.checkstyle.suppressionxpathfilter;
21+
22+
import static com.puppycrawl.tools.checkstyle.checks.naming.LocalFinalVariableNameCheck.MSG_INVALID_PATTERN;
23+
24+
import java.io.File;
25+
import java.util.Collections;
26+
import java.util.List;
27+
28+
import org.junit.jupiter.api.Test;
29+
30+
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31+
import com.puppycrawl.tools.checkstyle.checks.naming.LocalFinalVariableNameCheck;
32+
33+
public class XpathRegressionLocalFinalVariableNameTest extends AbstractXpathTestSupport {
34+
35+
private final String checkName = LocalFinalVariableNameCheck.class.getSimpleName();
36+
37+
@Override
38+
protected String getCheckName() {
39+
return checkName;
40+
}
41+
42+
@Test
43+
public void testResource() throws Exception {
44+
final File fileToProcess =
45+
new File(getPath("SuppressionXpathRegressionLocalFinalVariableNameResource.java"));
46+
47+
final DefaultConfiguration moduleConfig =
48+
createModuleConfig(LocalFinalVariableNameCheck.class);
49+
moduleConfig.addProperty("format", "^[A-Z][A-Z0-9]*$");
50+
moduleConfig.addProperty("tokens", "PARAMETER_DEF,RESOURCE");
51+
52+
final String[] expectedViolation = {
53+
"7:21: " + getCheckMessage(LocalFinalVariableNameCheck.class,
54+
MSG_INVALID_PATTERN, "scanner", "^[A-Z][A-Z0-9]*$"),
55+
};
56+
57+
final List<String> expectedXpathQueries = Collections.singletonList(
58+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT["
59+
+ "@text='SuppressionXpathRegressionLocalFinalVariableNameResource']]"
60+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/LITERAL_TRY"
61+
+ "/RESOURCE_SPECIFICATION/RESOURCES/RESOURCE/IDENT[@text='scanner']"
62+
);
63+
64+
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
65+
}
66+
67+
@Test
68+
public void testVariable() throws Exception {
69+
final File fileToProcess =
70+
new File(getPath("SuppressionXpathRegressionLocalFinalVariableNameVar.java"));
71+
72+
final DefaultConfiguration moduleConfig =
73+
createModuleConfig(LocalFinalVariableNameCheck.class);
74+
moduleConfig.addProperty("format", "^[A-Z][a-z0-9]*$");
75+
76+
final String[] expectedViolation = {
77+
"5:19: " + getCheckMessage(LocalFinalVariableNameCheck.class,
78+
MSG_INVALID_PATTERN, "VAR1", "^[A-Z][a-z0-9]*$"),
79+
};
80+
81+
final List<String> expectedXpathQueries = Collections.singletonList(
82+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT["
83+
+ "@text='SuppressionXpathRegressionLocalFinalVariableNameVar']]"
84+
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/VARIABLE_DEF"
85+
+ "/IDENT[@text='VAR1']"
86+
);
87+
88+
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
89+
}
90+
91+
@Test
92+
public void testInnerClass() throws Exception {
93+
final File fileToProcess =
94+
new File(getPath("SuppressionXpathRegressionLocalFinalVariableNameInner.java"));
95+
96+
final DefaultConfiguration moduleConfig =
97+
createModuleConfig(LocalFinalVariableNameCheck.class);
98+
moduleConfig.addProperty("format", "^[A-Z][a-z0-9]*$");
99+
100+
final String[] expectedViolation = {
101+
"8:23: " + getCheckMessage(LocalFinalVariableNameCheck.class,
102+
MSG_INVALID_PATTERN, "VAR1", "^[A-Z][a-z0-9]*$"),
103+
};
104+
105+
final List<String> expectedXpathQueries = Collections.singletonList(
106+
"/COMPILATION_UNIT/CLASS_DEF[./IDENT["
107+
+ "@text='SuppressionXpathRegressionLocalFinalVariableNameInner']]"
108+
+ "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK"
109+
+ "/METHOD_DEF[./IDENT[@text='MyMethod']]/SLIST/VARIABLE_DEF"
110+
+ "/IDENT[@text='VAR1']"
111+
);
112+
113+
runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
114+
}
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.checkstyle.suppressionxpathfilter.localfinalvariablename;
2+
3+
import java.util.Scanner;
4+
5+
public class SuppressionXpathRegressionLocalFinalVariableNameInner {
6+
class InnerClass {
7+
void MyMethod() {
8+
final int VAR1 = 10; // warn
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.checkstyle.suppressionxpathfilter.localfinalvariablename;
2+
3+
import java.util.Scanner;
4+
5+
public class SuppressionXpathRegressionLocalFinalVariableNameResource {
6+
void MyMethod() {
7+
try(Scanner scanner = new Scanner("ABC")) { // warn
8+
final int VAR1 = 5;
9+
final int var1 = 10;
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.checkstyle.suppressionxpathfilter.localfinalvariablename;
2+
3+
public class SuppressionXpathRegressionLocalFinalVariableNameVar {
4+
void MyMethod() {
5+
final int VAR1 = 5; // warn
6+
}
7+
}

src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
9797
"DesignForExtension",
9898
"HideUtilityClassConstructor",
9999
"InterfaceTypeParameterName",
100-
"LocalFinalVariableName",
101100
"LocalVariableName",
102101
"MethodTypeParameterName",
103102
"ModifiedControlVariable",

0 commit comments

Comments
 (0)