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 static com.puppycrawl.tools.checkstyle.checks.coding.ParameterAssignmentCheck.MSG_KEY;
23
24 import java.io.File;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.coding.ParameterAssignmentCheck;
32
33 public class XpathRegressionParameterAssignmentTest extends AbstractXpathTestSupport {
34
35 @Override
36 protected String getCheckName() {
37 return ParameterAssignmentCheck.class.getSimpleName();
38 }
39
40 @Test
41 public void testMethods() throws Exception {
42 final String filePath =
43 getPath("InputXpathParameterAssignmentMethods.java");
44 final File fileToProcess = new File(filePath);
45
46 final DefaultConfiguration moduleConfig =
47 createModuleConfig(ParameterAssignmentCheck.class);
48
49 final String[] expectedViolations = {
50 "9:15: " + getCheckMessage(ParameterAssignmentCheck.class, MSG_KEY, "field"),
51 };
52
53 final List<String> expectedXpathQueries = Arrays.asList(
54 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
55 + "@text='InputXpathParameterAssignmentMethods']]"
56 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='Test1']]/SLIST/EXPR"
57 + "[./PLUS_ASSIGN/IDENT[@text='field']]",
58 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
59 + "@text='InputXpathParameterAssignmentMethods']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='Test1']]"
61 + "/SLIST/EXPR/PLUS_ASSIGN[./IDENT[@text='field']]"
62 );
63
64 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
65
66 }
67
68 @Test
69 public void testLambdas() throws Exception {
70 final String filePath =
71 getPath("InputXpathParameterAssignmentLambdas.java");
72 final File fileToProcess = new File(filePath);
73
74 final DefaultConfiguration moduleConfig =
75 createModuleConfig(ParameterAssignmentCheck.class);
76
77 final String[] expectedViolations = {
78 "9:32: " + getCheckMessage(ParameterAssignmentCheck.class, MSG_KEY, "q"),
79 };
80
81 final List<String> expectedXpathQueries = Arrays.asList(
82 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
83 + "@text='InputXpathParameterAssignmentLambdas']]"
84 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='obj1']]"
85 + "/ASSIGN/LAMBDA[./IDENT[@text='q']]/EXPR",
86 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
87 + "@text='InputXpathParameterAssignmentLambdas']]"
88 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='obj1']]/ASSIGN/LAMBDA[./IDENT["
89 + "@text='q']]/EXPR/POST_INC[./IDENT[@text='q']]"
90 );
91
92 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
93 }
94
95 @Test
96 public void testCtor() throws Exception {
97 final String filePath =
98 getPath("InputXpathParameterAssignmentCtor.java");
99 final File fileToProcess = new File(filePath);
100
101 final DefaultConfiguration moduleConfig =
102 createModuleConfig(ParameterAssignmentCheck.class);
103
104 final String[] expectedViolations = {
105 "9:15: " + getCheckMessage(ParameterAssignmentCheck.class, MSG_KEY, "field"),
106 };
107
108 final List<String> expectedXpathQueries = Arrays.asList(
109 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
110 + "@text='InputXpathParameterAssignmentCtor']]"
111 + "/OBJBLOCK/CTOR_DEF[./IDENT["
112 + "@text='InputXpathParameterAssignmentCtor']]"
113 + "/SLIST/EXPR[./PLUS_ASSIGN/IDENT[@text='field']]",
114 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
115 + "@text='InputXpathParameterAssignmentCtor']]"
116 + "/OBJBLOCK/CTOR_DEF[./IDENT["
117 + "@text='InputXpathParameterAssignmentCtor']]"
118 + "/SLIST/EXPR/PLUS_ASSIGN[./IDENT[@text='field']]"
119 );
120
121 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
122 }
123
124 }