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