1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.MultipleStringLiteralsCheck.MSG_KEY;
24
25 import java.io.File;
26 import java.util.Arrays;
27 import java.util.List;
28
29 import org.junit.jupiter.api.Test;
30
31 import com.google.common.collect.ImmutableMap;
32 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
34 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
35
36 public class MultipleStringLiteralsCheckTest extends AbstractModuleTestSupport {
37
38 @Override
39 protected String getPackageLocation() {
40 return "com/puppycrawl/tools/checkstyle/checks/coding/multiplestringliterals";
41 }
42
43 @Test
44 public void testIt() throws Exception {
45
46 final String[] expected = {
47 "14:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
48 "17:17: " + getCheckMessage(MSG_KEY, "\"\"", 4),
49 "19:23: " + getCheckMessage(MSG_KEY, "\", \"", 3),
50 };
51
52 verifyWithInlineConfigParser(
53 getPath("InputMultipleStringLiterals.java"),
54 expected);
55 }
56
57 @Test
58 public void testItIgnoreEmpty() throws Exception {
59
60 final String[] expected = {
61 "14:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
62 "19:23: " + getCheckMessage(MSG_KEY, "\", \"", 3),
63 };
64
65 verifyWithInlineConfigParser(
66 getPath("InputMultipleStringLiterals2.java"),
67 expected);
68 }
69
70 @Test
71 public void testMultipleInputs() throws Exception {
72 final DefaultConfiguration checkConfig =
73 createModuleConfig(MultipleStringLiteralsCheck.class);
74 checkConfig.addProperty("allowedDuplicates", "2");
75
76 final String firstInput = getPath("InputMultipleStringLiterals3.java");
77 final String secondInput = getPath("InputMultipleStringLiteralsNoWarnings.java");
78
79 final File[] inputs = {new File(firstInput), new File(secondInput)};
80
81 final List<String> expectedFirstInput = Arrays.asList(
82 "14:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
83 "19:23: " + getCheckMessage(MSG_KEY, "\", \"", 3)
84 );
85 final List<String> expectedSecondInput = Arrays.asList(CommonUtil.EMPTY_STRING_ARRAY);
86
87 verify(createChecker(checkConfig), inputs,
88 ImmutableMap.of(firstInput, expectedFirstInput,
89 secondInput, expectedSecondInput));
90 }
91
92 @Test
93 public void testItIgnoreEmptyAndComspace() throws Exception {
94
95 final String[] expected = {
96 "14:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
97 };
98
99 verifyWithInlineConfigParser(
100 getPath("InputMultipleStringLiterals4.java"),
101 expected);
102 }
103
104 @Test
105 public void testItWithoutIgnoringAnnotations() throws Exception {
106
107 final String[] expected = {
108 "28:23: " + getCheckMessage(MSG_KEY, "\"unchecked\"", 4),
109 };
110
111 verifyWithInlineConfigParser(
112 getPath("InputMultipleStringLiterals5.java"),
113 expected);
114 }
115
116 @Test
117 public void testTokensNotNull() {
118 final MultipleStringLiteralsCheck check = new MultipleStringLiteralsCheck();
119 assertWithMessage("Acceptable tokens should not be null")
120 .that(check.getAcceptableTokens())
121 .isNotNull();
122 assertWithMessage("Default tokens should not be null")
123 .that(check.getDefaultTokens())
124 .isNotNull();
125 assertWithMessage("Required tokens should not be null")
126 .that(check.getRequiredTokens())
127 .isNotNull();
128 }
129
130 @Test
131 public void testDefaultConfiguration() throws Exception {
132 final String[] expected = {
133 "14:16: " + getCheckMessage(MSG_KEY, "\"StringContents\"", 3),
134 "16:17: " + getCheckMessage(MSG_KEY, "\"DoubleString\"", 2),
135 "19:23: " + getCheckMessage(MSG_KEY, "\", \"", 3),
136 };
137
138 verifyWithInlineConfigParser(
139 getPath("InputMultipleStringLiterals6.java"),
140 expected);
141 }
142
143 @Test
144 public void testIgnores() throws Exception {
145 final String[] expected = {
146 "28:23: " + getCheckMessage(MSG_KEY, "\"unchecked\"", 4),
147 };
148
149 verifyWithInlineConfigParser(
150 getPath("InputMultipleStringLiterals7.java"),
151 expected);
152 }
153
154 @Test
155 public void testMultipleStringLiteralsTextBlocks() throws Exception {
156
157 final String[] expected = {
158 "14:22: " + getCheckMessage(MSG_KEY, "\"string\"", 3),
159 "19:25: " + getCheckMessage(MSG_KEY, "\"other string\"", 2),
160 "23:25: " + getCheckMessage(MSG_KEY, "\"other string\\n\"", 2),
161 "30:25: " + getCheckMessage(MSG_KEY, "\"<html>\\u000D\\u000A\\n\\u2000\\n "
162 + " <body>\\u000D\\u000A\\n\\u2000\\n <p>Hello, world</p>\\u000D\\"
163 + "u000A\\n\\u2000\\n </body>\\u000D\\u000A\\n\\u2000\\n</html>\\u000D\\"
164 + "u000A\\u2000\\n\"", 2),
165 "37:34: " + getCheckMessage(MSG_KEY, "\"fun with\\n\\n whitespace\\t"
166 + "\\r\\n and other escapes \\\"\"\"\\n\"", 2),
167 "42:34: " + getCheckMessage(MSG_KEY, "\"\\b \\f \\\\ \\0 \\1 \\2 "
168 + "\\r \\r\\n \\\\r\\\\n \\\\''\\n\\\\11 \\\\57 \\n\\\\n\\n\\\\\\n\\n \\\\ \"\"a "
169 + "\"a\\n\\\\' \\\\\\' \\'\\n\"", 2),
170 "65:20: " + getCheckMessage(MSG_KEY, "\"foo\"", 4),
171 "73:19: " + getCheckMessage(MSG_KEY, "\"another test\"", 2),
172 "77:20: " + getCheckMessage(MSG_KEY, "\"\"", 6),
173 "88:23: " + getCheckMessage(MSG_KEY, "\" .\\n .\\n.\\n\"", 2),
174 "104:24: " + getCheckMessage(MSG_KEY, "\" foo\\n\\n\\n "
175 + " bar\"", 2),
176 };
177
178 verifyWithInlineConfigParser(
179 getNonCompilablePath("InputMultipleStringLiteralsTextBlocks.java"),
180 expected);
181 }
182
183 }