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 static com.puppycrawl.tools.checkstyle.checks.coding.MultipleStringLiteralsCheck.MSG_KEY;
23
24 import java.io.File;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
30 import org.junit.jupiter.api.Test;
31
32 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33 import com.puppycrawl.tools.checkstyle.checks.coding.MultipleStringLiteralsCheck;
34
35 public class XpathRegressionMultipleStringLiteralsTest extends AbstractXpathTestSupport {
36
37 @Override
38 protected String getCheckName() {
39
40 return MultipleStringLiteralsCheck.class.getSimpleName();
41 }
42
43 @Override
44 protected String getPackageLocation() {
45 return "org/checkstyle/suppressionxpathfilter/coding/multiplestringliterals";
46 }
47
48 @Test
49 public void testDefault() throws Exception {
50 final File fileToProcess = new File(
51 getPath("InputXpathMultipleStringLiteralsDefault.java"));
52
53 final DefaultConfiguration moduleConfig =
54 createModuleConfig(MultipleStringLiteralsCheck.class);
55
56 final String[] expectedViolations = {
57 "4:16: " + getCheckMessage(MultipleStringLiteralsCheck.class,
58 MSG_KEY, "\"StringContents\"", 2),
59 };
60
61 final List<String> expectedXpathQueries = Arrays.asList(
62 "/COMPILATION_UNIT/CLASS_DEF"
63 + "[./IDENT[@text='InputXpathMultipleStringLiteralsDefault']]"
64 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a']]"
65 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='StringContents']]",
66 "/COMPILATION_UNIT/CLASS_DEF"
67 + "[./IDENT[@text='InputXpathMultipleStringLiteralsDefault']]"
68 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a']]"
69 + "/ASSIGN/EXPR/STRING_LITERAL[@text='StringContents']"
70 );
71
72 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
73 }
74
75 @Test
76 public void testAllowDuplicates() throws Exception {
77 final File fileToProcess = new File(
78 getPath("InputXpathMultipleStringLiteralsAllowDuplicates.java"));
79
80 final DefaultConfiguration moduleConfig =
81 createModuleConfig(MultipleStringLiteralsCheck.class);
82 moduleConfig.addProperty("allowedDuplicates", "2");
83
84 final String[] expectedViolations = {
85 "8:19: " + getCheckMessage(MultipleStringLiteralsCheck.class,
86 MSG_KEY, "\", \"", 3),
87 };
88
89 final List<String> expectedXpathQueries = Collections.singletonList(
90 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
91 + "[@text='InputXpathMultipleStringLiteralsAllowDuplicates']]"
92 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myTest']]/SLIST/VARIABLE_DEF"
93 + "[./IDENT[@text='a5']]/ASSIGN/EXPR/PLUS[./STRING_LITERAL[@text=', ']]"
94 + "/PLUS/STRING_LITERAL[@text=', ']"
95 );
96
97 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
98 }
99
100 @Test
101 public void testIgnoreRegexp() throws Exception {
102 final File fileToProcess = new File(
103 getPath("InputXpathMultipleStringLiteralsIgnoreRegexp.java"));
104
105 final DefaultConfiguration moduleConfig =
106 createModuleConfig(MultipleStringLiteralsCheck.class);
107 moduleConfig.addProperty("ignoreStringsRegexp", "((\"\")|(\", \"))$");
108
109 final String[] expectedViolations = {
110 "7:19: " + getCheckMessage(MultipleStringLiteralsCheck.class,
111 MSG_KEY, "\"DoubleString\"", 2),
112 };
113
114 final List<String> expectedXpathQueries = Collections.singletonList(
115 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
116 + "[@text='InputXpathMultipleStringLiteralsIgnoreRegexp']]"
117 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myTest']]/SLIST/VARIABLE_DEF"
118 + "[./IDENT[@text='a3']]/ASSIGN/EXPR/PLUS/STRING_LITERAL[@text='DoubleString']"
119 );
120
121 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
122 }
123
124 @Test
125 public void testIgnoreOccurrenceContext() throws Exception {
126 final String filePath =
127 "InputXpathMultipleStringLiteralsIgnoreOccurrenceContext.java";
128 final File fileToProcess = new File(getPath(filePath));
129
130 final DefaultConfiguration moduleConfig =
131 createModuleConfig(MultipleStringLiteralsCheck.class);
132 moduleConfig.addProperty("ignoreOccurrenceContext", "");
133
134 final String[] expectedViolations = {
135 "5:17: " + getCheckMessage(MultipleStringLiteralsCheck.class,
136 MSG_KEY, "\"unchecked\"", 3),
137 };
138
139 final List<String> expectedXpathQueries = Arrays.asList(
140 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
141 + "'InputXpathMultipleStringLiteralsIgnoreOccurrenceContext']]"
142 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a1']]"
143 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='unchecked']]",
144 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
145 + "'InputXpathMultipleStringLiteralsIgnoreOccurrenceContext']]"
146 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a1']]"
147 + "/ASSIGN/EXPR/STRING_LITERAL[@text='unchecked']"
148 );
149
150 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
151 }
152
153 }