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