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 java.io.File;
23 import java.util.List;
24
25 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29 import com.puppycrawl.tools.checkstyle.checks.coding.PatternVariableAssignmentCheck;
30
31 public class XpathRegressionPatternVariableAssignmentTest extends AbstractXpathTestSupport {
32
33 private final String checkName = PatternVariableAssignmentCheck.class.getSimpleName();
34
35 @Override
36 protected String getCheckName() {
37 return checkName;
38 }
39
40 @Override
41 public String getPackageLocation() {
42 return "org/checkstyle/suppressionxpathfilter/coding/patternvariableassignment";
43 }
44
45 @Test
46 public void testMethod() throws Exception {
47 final File fileToProcess =
48 new File(getPath(
49 "InputXpathPatternVariableAssignmentMethod.java"));
50
51 final DefaultConfiguration moduleConfig =
52 createModuleConfig(PatternVariableAssignmentCheck.class);
53
54 final String[] expectedViolation = {
55 "8:7: " + getCheckMessage(PatternVariableAssignmentCheck.class,
56 PatternVariableAssignmentCheck.MSG_KEY,
57 "s"),
58 };
59
60 final List<String> expectedXpathQueries = List.of("/COMPILATION_UNIT/CLASS_DEF["
61 + "./IDENT[@text='InputXpathPatternVariableAssignmentMethod']]/OBJBLOCK/METHOD_DEF["
62 + "./IDENT[@text='test']]/SLIST/LITERAL_IF/SLIST/EXPR/ASSIGN/IDENT[@text='s']");
63
64 runVerifications(moduleConfig, fileToProcess, expectedViolation,
65 expectedXpathQueries);
66 }
67
68 @Test
69 public void testLambda() throws Exception {
70 final File fileToProcess =
71 new File(getPath(
72 "InputXpathPatternVariableAssignmentLambda.java"));
73
74 final DefaultConfiguration moduleConfig =
75 createModuleConfig(PatternVariableAssignmentCheck.class);
76
77 final String[] expectedViolation = {
78 "12:17: " + getCheckMessage(PatternVariableAssignmentCheck.class,
79 PatternVariableAssignmentCheck.MSG_KEY,
80 "x"),
81 };
82
83 final List<String> expectedXpathQueries = List.of("/COMPILATION_UNIT/CLASS_DEF["
84 + "./IDENT[@text='InputXpathPatternVariableAssignmentLambda']]/OBJBLOCK/METHOD_DEF["
85 + "./IDENT[@text='foo']]/SLIST/EXPR/METHOD_CALL/ELIST/LAMBDA["
86 + "./IDENT[@text='item']]/SLIST/LITERAL_IF/SLIST/EXPR/ASSIGN/IDENT[@text='x']");
87
88 runVerifications(moduleConfig, fileToProcess, expectedViolation,
89 expectedXpathQueries);
90 }
91
92 @Test
93 public void testClass() throws Exception {
94 final File fileToProcess =
95 new File(getPath(
96 "InputXpathPatternVariableAssignmentClass.java"));
97
98 final DefaultConfiguration moduleConfig =
99 createModuleConfig(PatternVariableAssignmentCheck.class);
100
101 final String[] expectedViolation = {
102 "10:21: " + getCheckMessage(PatternVariableAssignmentCheck.class,
103 PatternVariableAssignmentCheck.MSG_KEY,
104 "x"),
105 };
106
107 final List<String> expectedXpathQueries = List.of("/COMPILATION_UNIT/CLASS_DEF["
108 + "./IDENT[@text='InputXpathPatternVariableAssignmentClass']]/OBJBLOCK/METHOD_DEF["
109 + "./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='annClass']]/ASSIGN/EXPR/"
110 + "LITERAL_NEW[./IDENT[@text='AnonymousClass']]/OBJBLOCK/METHOD_DEF["
111 + "./IDENT[@text='test']]/SLIST/LITERAL_IF/SLIST/EXPR/ASSIGN/IDENT[@text='x']");
112
113 runVerifications(moduleConfig, fileToProcess, expectedViolation,
114 expectedXpathQueries);
115 }
116
117 }