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