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.Collections;
25 import java.util.List;
26
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30 import com.puppycrawl.tools.checkstyle.checks.coding.StringLiteralEqualityCheck;
31
32 public class XpathRegressionStringLiteralEqualityTest extends AbstractXpathTestSupport {
33
34 private final String checkName = StringLiteralEqualityCheck.class.getSimpleName();
35
36 @Override
37 protected String getCheckName() {
38 return checkName;
39 }
40
41 @Test
42 public void testEqualityTrue() throws Exception {
43 final File fileToProcess =
44 new File(getPath("InputXpathStringLiteralEqualityTrue.java"));
45 final DefaultConfiguration moduleConfig =
46 createModuleConfig(StringLiteralEqualityCheck.class);
47 final String[] expectedViolation = {
48 "6:17: " + getCheckMessage(StringLiteralEqualityCheck.class,
49 StringLiteralEqualityCheck.MSG_KEY, "=="),
50 };
51 final List<String> expectedXpathQueries = Arrays.asList(
52 "/COMPILATION_UNIT"
53 + "/CLASS_DEF[./IDENT[@text='InputXpathStringLiteralEqualityTrue']]"
54 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
55 + "/SLIST/LITERAL_IF/EXPR",
56 "/COMPILATION_UNIT"
57 + "/CLASS_DEF[./IDENT[@text='InputXpathStringLiteralEqualityTrue']]"
58 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
59 + "/SLIST/LITERAL_IF/EXPR/EQUAL[./IDENT[@text='foo']]"
60
61 );
62
63 runVerifications(moduleConfig, fileToProcess, expectedViolation,
64 expectedXpathQueries);
65 }
66
67 @Test
68 public void testEqualityFalse() throws Exception {
69 final File fileToProcess =
70 new File(getPath("InputXpathStringLiteralEqualityFalse.java"));
71 final DefaultConfiguration moduleConfig =
72 createModuleConfig(StringLiteralEqualityCheck.class);
73 final String[] expectedViolation = {
74 "6:20: " + getCheckMessage(StringLiteralEqualityCheck.class,
75 StringLiteralEqualityCheck.MSG_KEY, "!="),
76 };
77 final List<String> expectedXpathQueries = Arrays.asList(
78 "/COMPILATION_UNIT"
79 + "/CLASS_DEF[./IDENT[@text='InputXpathStringLiteralEqualityFalse']]"
80 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
81 + "/SLIST/LITERAL_WHILE/EXPR",
82 "/COMPILATION_UNIT"
83 + "/CLASS_DEF[./IDENT[@text='InputXpathStringLiteralEqualityFalse']]"
84 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
85 + "/SLIST/LITERAL_WHILE/EXPR/NOT_EQUAL[./IDENT[@text='foo']]"
86
87 );
88
89 runVerifications(moduleConfig, fileToProcess, expectedViolation,
90 expectedXpathQueries);
91 }
92
93 @Test
94 public void testEqualityExp() throws Exception {
95 final File fileToProcess =
96 new File(getPath("InputXpathStringLiteralEqualityExp.java"));
97 final DefaultConfiguration moduleConfig =
98 createModuleConfig(StringLiteralEqualityCheck.class);
99 final String[] expectedViolation = {
100 "6:29: " + getCheckMessage(StringLiteralEqualityCheck.class,
101 StringLiteralEqualityCheck.MSG_KEY, "=="),
102 };
103 final List<String> expectedXpathQueries = Collections.singletonList(
104 "/COMPILATION_UNIT"
105 + "/CLASS_DEF[./IDENT[@text='InputXpathStringLiteralEqualityExp']]"
106 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
107 + "/SLIST/VARIABLE_DEF[./IDENT[@text='flag']]"
108 + "/ASSIGN/EXPR/EQUAL[./IDENT[@text='foo']]"
109
110 );
111
112 runVerifications(moduleConfig, fileToProcess, expectedViolation,
113 expectedXpathQueries);
114 }
115 }