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.Arrays;
24 import java.util.List;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29 import com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck;
30
31 public class XpathRegressionInnerAssignmentTest extends AbstractXpathTestSupport {
32
33 private final String checkName = InnerAssignmentCheck.class.getSimpleName();
34
35 @Override
36 protected String getCheckName() {
37 return checkName;
38 }
39
40 @Test
41 public void testInnerAssignment() throws Exception {
42 final File fileToProcess = new
43 File(getPath("InputXpathInnerAssignment.java"));
44
45 final DefaultConfiguration moduleConfig = createModuleConfig(InnerAssignmentCheck.class);
46
47 final String[] expectedViolation = {
48 "7:15: " + getCheckMessage(InnerAssignmentCheck.class, InnerAssignmentCheck.MSG_KEY),
49 };
50
51 final List<String> expectedXpathQueries = List.of(
52 "/COMPILATION_UNIT"
53 + "/CLASS_DEF[./IDENT[@text='InputXpathInnerAssignment']]"
54 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod']]"
55 + "/SLIST/EXPR/ASSIGN[./IDENT[@text='a']]/ASSIGN[./IDENT[@text='b']]"
56 );
57
58 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
59 }
60
61 @Test
62 public void testArrays() throws Exception {
63 final File fileToProcess = new
64 File(getPath("InputXpathInnerAssignmentArrays.java"));
65
66 final DefaultConfiguration moduleConfig = createModuleConfig(InnerAssignmentCheck.class);
67
68 final String[] expectedViolation = {
69 "6:55: " + getCheckMessage(InnerAssignmentCheck.class, InnerAssignmentCheck.MSG_KEY),
70 };
71
72 final List<String> expectedXpathQueries = Arrays.asList(
73 "/COMPILATION_UNIT"
74 + "/CLASS_DEF[./IDENT[@text='InputXpathInnerAssignmentArrays']]"
75 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod']]"
76 + "/SLIST/VARIABLE_DEF[./IDENT[@text='doubleArray']]"
77 + "/ASSIGN/EXPR/LITERAL_NEW/ARRAY_INIT/EXPR[./ASSIGN/IDENT[@text='myDouble']]",
78 "/COMPILATION_UNIT"
79 + "/CLASS_DEF[./IDENT[@text='InputXpathInnerAssignmentArrays']]"
80 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod']]/"
81 + "SLIST/VARIABLE_DEF[./IDENT[@text='doubleArray']]"
82 + "/ASSIGN/EXPR/LITERAL_NEW/ARRAY_INIT/EXPR/ASSIGN[./IDENT[@text='myDouble']]"
83 );
84
85 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
86 }
87 }