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