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.MethodNameCheck;
32
33 public class XpathRegressionMethodNameTest extends AbstractXpathTestSupport {
34
35 private final String checkName = MethodNameCheck.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/methodname";
45 }
46
47 @Test
48 public void testDefault() throws Exception {
49 final File fileToProcess =
50 new File(getPath("InputXpathMethodNameDefault.java"));
51
52 final String pattern = "^[a-z][a-zA-Z0-9]*$";
53 final DefaultConfiguration moduleConfig =
54 createModuleConfig(MethodNameCheck.class);
55
56 final String[] expectedViolation = {
57 "6:16: " + getCheckMessage(MethodNameCheck.class,
58 AbstractNameCheck.MSG_INVALID_PATTERN, "SecondMethod", pattern),
59 };
60
61 final List<String> expectedXpathQueries = Collections.singletonList(
62 "/COMPILATION_UNIT"
63 + "/CLASS_DEF[./IDENT[@text"
64 + "='InputXpathMethodNameDefault']]"
65 + "/OBJBLOCK/METHOD_DEF/IDENT[@text='SecondMethod']"
66 );
67 runVerifications(moduleConfig, fileToProcess, expectedViolation,
68 expectedXpathQueries);
69 }
70
71 @Test
72 public void testInnerClass() throws Exception {
73 final File fileToProcess =
74 new File(getPath("InputXpathMethodNameInner.java"));
75
76 final String pattern = "^[a-z](_?[a-zA-Z0-9]+)*$";
77 final DefaultConfiguration moduleConfig =
78 createModuleConfig(MethodNameCheck.class);
79 moduleConfig.addProperty("format", "^[a-z](_?[a-zA-Z0-9]+)*$");
80
81 final String[] expectedViolation = {
82 "7:21: " + getCheckMessage(MethodNameCheck.class,
83 AbstractNameCheck.MSG_INVALID_PATTERN, "MyMethod2", pattern),
84 };
85
86 final List<String> expectedXpathQueries = Collections.singletonList(
87 "/COMPILATION_UNIT"
88 + "/CLASS_DEF[./IDENT[@text"
89 + "='InputXpathMethodNameInner']]"
90 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]"
91 + "/OBJBLOCK/METHOD_DEF/IDENT[@text='MyMethod2']"
92 );
93 runVerifications(moduleConfig, fileToProcess, expectedViolation,
94 expectedXpathQueries);
95 }
96
97 @Test
98 public void testCustomProperties() throws Exception {
99 final File fileToProcess =
100 new File(getPath("InputXpathMethodNameCustomProperties.java"));
101
102 final String pattern = "^[a-z](_?[a-zA-Z0-9]+)*$";
103 final DefaultConfiguration moduleConfig =
104 createModuleConfig(MethodNameCheck.class);
105 moduleConfig.addProperty("format", "^[a-z](_?[a-zA-Z0-9]+)*$");
106 moduleConfig.addProperty("applyToPublic", "false");
107 moduleConfig.addProperty("applyToProtected", "false");
108
109 final String[] expectedViolation = {
110 "7:19: " + getCheckMessage(MethodNameCheck.class,
111 AbstractNameCheck.MSG_INVALID_PATTERN,
112 "ThirdMethod", pattern),
113 };
114
115 final List<String> expectedXpathQueries = Collections.singletonList(
116 "/COMPILATION_UNIT"
117 + "/INTERFACE_DEF[./IDENT[@text='Check']]"
118 + "/OBJBLOCK/METHOD_DEF/IDENT[@text='ThirdMethod']"
119 );
120 runVerifications(moduleConfig, fileToProcess, expectedViolation,
121 expectedXpathQueries);
122 }
123
124 }