1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter.naming;
21
22 import java.io.File;
23 import java.util.List;
24
25 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29 import com.puppycrawl.tools.checkstyle.checks.naming.GoogleNonConstantFieldNameCheck;
30
31 public class XpathRegressionGoogleNonConstantFieldNameTest extends AbstractXpathTestSupport {
32
33 private static final Class<GoogleNonConstantFieldNameCheck> CLASS =
34 GoogleNonConstantFieldNameCheck.class;
35
36 @Override
37 protected String getCheckName() {
38 return CLASS.getSimpleName();
39 }
40
41 @Override
42 public String getPackageLocation() {
43 return "org/checkstyle/suppressionxpathfilter/naming/googlenonconstantfieldname";
44 }
45
46 @Test
47 public void testNonConstantFieldName() throws Exception {
48 final File fileToProcess = new File(
49 getPath("InputXpathGoogleNonConstantFieldName.java"));
50
51 final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
52
53 final String[] expectedViolation = {
54 "5:9: " + getCheckMessage(CLASS, GoogleNonConstantFieldNameCheck.MSG_KEY_INVALID_FORMAT,
55 "Foo"),
56 };
57
58 final List<String> expectedXpathQueries = List.of(
59 "/COMPILATION_UNIT/CLASS_DEF"
60 + "[./IDENT[@text='InputXpathGoogleNonConstantFieldName']]"
61 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='Foo']"
62 );
63
64 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
65 }
66
67 @Test
68 public void testInnerClassField() throws Exception {
69 final File fileToProcess = new File(
70 getPath("InputXpathGoogleNonConstantFieldNameInnerClass.java"));
71 final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
72 final String[] expectedViolation = {
73 "6:14: " + getCheckMessage(CLASS,
74 GoogleNonConstantFieldNameCheck.MSG_KEY_INVALID_FORMAT,
75 "gradle_9_5_1"),
76 };
77 final List<String> expectedXpathQueries = List.of(
78 "/COMPILATION_UNIT/INTERFACE_DEF"
79 + "[./IDENT[@text='InputXpathGoogleNonConstantFieldNameInnerClass']]"
80 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]"
81 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='gradle_9_5_1']"
82 );
83 runVerifications(moduleConfig, fileToProcess, expectedViolation,
84 expectedXpathQueries);
85 }
86
87 @Test
88 public void testAnonymousClassField() throws Exception {
89 final File fileToProcess = new File(
90 getPath("InputXpathGoogleNonConstantFieldNameAnonymousClass.java"));
91 final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
92 final String[] expectedViolation = {
93 "6:13: " + getCheckMessage(CLASS,
94 GoogleNonConstantFieldNameCheck.MSG_KEY_INVALID_FORMAT,
95 "f"),
96 };
97 final List<String> expectedXpathQueries = List.of(
98 "/COMPILATION_UNIT/CLASS_DEF"
99 + "[./IDENT[@text='InputXpathGoogleNonConstantFieldNameAnonymousClass']]"
100 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='run']]/ASSIGN/EXPR"
101 + "/LITERAL_NEW[./IDENT[@text='Runnable']]/OBJBLOCK/VARIABLE_DEF/IDENT[@text='f']"
102 );
103 runVerifications(moduleConfig, fileToProcess, expectedViolation,
104 expectedXpathQueries);
105 }
106 }