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.Collections;
24 import java.util.List;
25
26 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30 import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
31 import com.puppycrawl.tools.checkstyle.checks.naming.IllegalIdentifierNameCheck;
32
33 public class XpathRegressionIllegalIdentifierNameTest extends AbstractXpathTestSupport {
34
35 private final String checkName = IllegalIdentifierNameCheck.class.getSimpleName();
36
37 @Override
38 protected String getCheckName() {
39 return checkName;
40 }
41
42 @Override
43 protected String getPackageLocation() {
44 return "org/checkstyle/suppressionxpathfilter/naming/illegalidentifiername";
45 }
46
47 @Test
48 public void testOne() throws Exception {
49 final File fileToProcess = new File(getPath(
50 "InputXpathIllegalIdentifierNameOne.java"));
51
52 final DefaultConfiguration moduleConfig =
53 createModuleConfig(IllegalIdentifierNameCheck.class);
54
55 final String format = "^(?!var$|\\S*\\$)\\S+$";
56
57 final String[] expectedViolation = {
58 "10:20: " + getCheckMessage(IllegalIdentifierNameCheck.class,
59 AbstractNameCheck.MSG_INVALID_PATTERN, "var", format),
60 };
61
62 final List<String> expectedXpathQueries = Collections.singletonList(
63 "/COMPILATION_UNIT/RECORD_DEF"
64 + "[./IDENT[@text='InputXpathIllegalIdentifierNameOne'"
65 + "]]/RECORD_COMPONENTS/RECORD_COMPONENT_DEF/IDENT[@text='var']"
66 );
67
68 runVerifications(moduleConfig, fileToProcess, expectedViolation,
69 expectedXpathQueries);
70 }
71
72 @Test
73 public void testTwo() throws Exception {
74 final File fileToProcess = new File(getPath(
75 "InputXpathIllegalIdentifierNameTwo.java"));
76
77 final DefaultConfiguration moduleConfig =
78 createModuleConfig(IllegalIdentifierNameCheck.class);
79
80 final String format = "^(?!var$|\\S*\\$)\\S+$";
81
82 final String[] expectedViolation = {
83 "9:17: " + getCheckMessage(IllegalIdentifierNameCheck.class,
84 AbstractNameCheck.MSG_INVALID_PATTERN, "te$t", format),
85 };
86
87 final List<String> expectedXpathQueries = Collections.singletonList(
88 "/COMPILATION_UNIT/CLASS_DEF"
89 + "[./IDENT[@text='InputXpathIllegalIdentifierNameTwo']"
90 + "]/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/PARAMETERS/PARAMETER_DEF"
91 + "/IDENT[@text='te$t']"
92 );
93
94 runVerifications(moduleConfig, fileToProcess, expectedViolation,
95 expectedXpathQueries);
96 }
97 }