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.MemberNameCheck;
32
33 public class XpathRegressionMemberNameTest extends AbstractXpathTestSupport {
34
35 private final String checkName = MemberNameCheck.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/membername";
45 }
46
47 @Test
48 public void testDefault() throws Exception {
49 final File fileToProcess =
50 new File(getPath("InputXpathMemberNameDefault.java"));
51
52 final String pattern = "^[a-z][a-zA-Z0-9]*$";
53 final DefaultConfiguration moduleConfig =
54 createModuleConfig(MemberNameCheck.class);
55
56 final String[] expectedViolation = {
57 "5:17: " + getCheckMessage(MemberNameCheck.class,
58 AbstractNameCheck.MSG_INVALID_PATTERN, "NUM2", pattern),
59 };
60
61 final List<String> expectedXpathQueries = Collections.singletonList(
62 "/COMPILATION_UNIT"
63 + "/CLASS_DEF[./IDENT[@text"
64 + "='InputXpathMemberNameDefault']]"
65 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='NUM2']"
66 );
67 runVerifications(moduleConfig, fileToProcess, expectedViolation,
68 expectedXpathQueries);
69 }
70
71 @Test
72 public void testIgnoreProtected() throws Exception {
73 final File fileToProcess =
74 new File(getPath("InputXpathMemberNameIgnoreProtected.java"));
75
76 final String pattern = "^m[A-Z][a-zA-Z0-9]*$";
77 final DefaultConfiguration moduleConfig =
78 createModuleConfig(MemberNameCheck.class);
79 moduleConfig.addProperty("format", "^m[A-Z][a-zA-Z0-9]*$");
80 moduleConfig.addProperty("applyToProtected", "false");
81 moduleConfig.addProperty("applyToPackage", "false");
82
83 final String[] expectedViolation = {
84 "6:20: " + getCheckMessage(MemberNameCheck.class,
85 AbstractNameCheck.MSG_INVALID_PATTERN, "NUM1", pattern),
86 };
87
88 final List<String> expectedXpathQueries = Collections.singletonList(
89 "/COMPILATION_UNIT"
90 + "/CLASS_DEF[./IDENT[@text"
91 + "='InputXpathMemberNameIgnoreProtected']]"
92 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]"
93 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='NUM1']"
94 );
95 runVerifications(moduleConfig, fileToProcess, expectedViolation,
96 expectedXpathQueries);
97 }
98
99 }