1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter;
21
22 import java.io.File;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26
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.LambdaParameterNameCheck;
32
33 public class XpathRegressionLambdaParameterNameTest extends AbstractXpathTestSupport {
34
35 private final String checkName = LambdaParameterNameCheck.class.getSimpleName();
36
37 @Override
38 protected String getCheckName() {
39 return checkName;
40 }
41
42 @Test
43 public void testDefault() throws Exception {
44 final File fileToProcess =
45 new File(getPath("InputXpathLambdaParameterNameDefault.java"));
46
47 final DefaultConfiguration moduleConfig =
48 createModuleConfig(LambdaParameterNameCheck.class);
49 final String defaultPattern = "^([a-z][a-zA-Z0-9]*|_)$";
50
51 final String[] expectedViolation = {
52 "7:44: " + getCheckMessage(LambdaParameterNameCheck.class,
53 AbstractNameCheck.MSG_INVALID_PATTERN, "S", defaultPattern),
54 };
55
56 final List<String> expectedXpathQueries = Collections.singletonList(
57 "/COMPILATION_UNIT/CLASS_DEF"
58 + "[./IDENT[@text='InputXpathLambdaParameterNameDefault']]"
59 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF["
60 + "./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/IDENT[@text='S']"
61 );
62
63 runVerifications(moduleConfig, fileToProcess, expectedViolation,
64 expectedXpathQueries);
65 }
66
67 @Test
68 public void testNonDefaultPattern() throws Exception {
69 final File fileToProcess =
70 new File(getPath("InputXpathLambdaParameterNameNonDefaultPattern.java"));
71
72 final String nonDefaultPattern = "^_[a-zA-Z0-9]*$";
73
74 final DefaultConfiguration moduleConfig =
75 createModuleConfig(LambdaParameterNameCheck.class);
76 moduleConfig.addProperty("format", nonDefaultPattern);
77
78 final String[] expectedViolation = {
79 "7:45: " + getCheckMessage(LambdaParameterNameCheck.class,
80 AbstractNameCheck.MSG_INVALID_PATTERN, "s", nonDefaultPattern),
81 };
82
83 final List<String> expectedXpathQueries = Arrays.asList(
84 "/COMPILATION_UNIT/CLASS_DEF"
85 + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
86 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
87 + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS",
88
89 "/COMPILATION_UNIT/CLASS_DEF"
90 + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
91 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
92 + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
93 + "/PARAMETER_DEF[./IDENT[@text='s']]",
94
95 "/COMPILATION_UNIT/CLASS_DEF"
96 + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
97 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST"
98 + "/VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
99 + "/PARAMETER_DEF[./IDENT[@text='s']]/MODIFIERS",
100
101 "/COMPILATION_UNIT/CLASS_DEF"
102 + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
103 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
104 + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
105 + "/PARAMETER_DEF[./IDENT[@text='s']]/TYPE",
106
107 "/COMPILATION_UNIT/CLASS_DEF"
108 + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
109 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
110 + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
111 + "/PARAMETER_DEF/IDENT[@text='s']"
112 );
113
114 runVerifications(moduleConfig, fileToProcess, expectedViolation,
115 expectedXpathQueries);
116 }
117 }