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.header;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck.MSG_HEADER_MISMATCH;
24 import static com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck.MSG_HEADER_MISSING;
25
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.regex.Pattern;
29
30 import org.junit.jupiter.api.Test;
31
32 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
34 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
35 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
36 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
37
38
39
40
41 public class RegexpHeaderCheckTest extends AbstractModuleTestSupport {
42
43 @Override
44 protected String getPackageLocation() {
45 return "com/puppycrawl/tools/checkstyle/checks/header/regexpheader";
46 }
47
48
49
50
51 @Test
52 public void testSetHeaderNull() {
53
54 final RegexpHeaderCheck instance = new RegexpHeaderCheck();
55
56 final String header = null;
57 instance.setHeader(header);
58 final List<Pattern> headerRegexps = TestUtil.getInternalState(instance, "headerRegexps");
59
60 assertWithMessage("When header is null regexps should not be set")
61 .that(headerRegexps)
62 .isEmpty();
63 }
64
65
66
67
68 @Test
69 public void testSetHeaderEmpty() {
70
71 final RegexpHeaderCheck instance = new RegexpHeaderCheck();
72
73 final String header = "";
74 instance.setHeader(header);
75 final List<Pattern> headerRegexps = TestUtil.getInternalState(instance, "headerRegexps");
76
77 assertWithMessage("When header is empty regexps should not be set")
78 .that(headerRegexps)
79 .isEmpty();
80 }
81
82
83
84
85 @Test
86 public void testSetHeaderSimple() {
87 final RegexpHeaderCheck instance = new RegexpHeaderCheck();
88
89 final String header = "abc.*";
90 instance.setHeader(header);
91 final List<Pattern> headerRegexps = TestUtil.getInternalState(instance, "headerRegexps");
92 assertWithMessage("Expected one pattern")
93 .that(headerRegexps.size())
94 .isEqualTo(1);
95 assertWithMessage("Invalid header regexp")
96 .that(headerRegexps.get(0).pattern())
97 .isEqualTo(header);
98 }
99
100
101
102
103 @Test
104 public void testSetHeader() {
105
106 final RegexpHeaderCheck instance = new RegexpHeaderCheck();
107 try {
108 final String header = "^/**\\n * Licensed to the Apache Software Foundation (ASF)";
109 instance.setHeader(header);
110 assertWithMessage(String.format(Locale.ROOT, "%s should have been thrown",
111 IllegalArgumentException.class)).fail();
112 }
113 catch (IllegalArgumentException ex) {
114 assertWithMessage("Invalid exception message")
115 .that(ex.getMessage())
116 .isEqualTo("Unable to parse format: ^/**\\n *"
117 + " Licensed to the Apache Software Foundation (ASF)");
118 }
119 }
120
121 @Test
122 public void testDefaultConfiguration() throws Exception {
123 final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
124 createChecker(checkConfig);
125 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
126 verify(checkConfig, getPath("InputRegexpHeaderDefaultConfig.java"), expected);
127 }
128
129 @Test
130 public void testEmptyFilename() throws Exception {
131 final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
132 checkConfig.addProperty("headerFile", "");
133 try {
134 createChecker(checkConfig);
135 assertWithMessage("Checker creation should not succeed with invalid headerFile").fail();
136 }
137 catch (CheckstyleException ex) {
138 assertWithMessage("Invalid exception message")
139 .that(ex.getMessage())
140 .isEqualTo("cannot initialize module"
141 + " com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck"
142 + " - Cannot set property 'headerFile' to ''");
143 }
144 }
145
146 @Test
147 public void testRegexpHeader() throws Exception {
148 final DefaultConfiguration checkConfig =
149 createModuleConfig(RegexpHeaderCheck.class);
150 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader.header"));
151 final String[] expected = {
152 "3: " + getCheckMessage(MSG_HEADER_MISMATCH, "// Created: 2002"),
153 };
154 verify(checkConfig, getPath("InputRegexpHeaderIgnore.java"), expected);
155 }
156
157 @Test
158 public void testNonMatchingRegexpHeader() throws Exception {
159 final DefaultConfiguration checkConfig =
160 createModuleConfig(RegexpHeaderCheck.class);
161 checkConfig.addProperty("header",
162 "\\/\\/ Nth Line of Header\\n\\/\\/ Nth Line of Header\\n");
163 checkConfig.addProperty("multiLines", "1");
164 final String[] expected = {
165 "1: " + getCheckMessage(MSG_HEADER_MISMATCH, "\\/\\/ Nth Line of Header"),
166 };
167 verify(checkConfig, getPath("InputRegexpHeaderNonMatching.java"), expected);
168 }
169
170 @Test
171 public void testRegexpHeaderUrl() throws Exception {
172 final DefaultConfiguration checkConfig =
173 createModuleConfig(RegexpHeaderCheck.class);
174 checkConfig.addProperty("headerFile", getUriString("InputRegexpHeader.header"));
175 final String[] expected = {
176 "3: " + getCheckMessage(MSG_HEADER_MISMATCH, "// Created: 2002"),
177 };
178 verify(checkConfig, getPath("InputRegexpHeaderIgnore.java"), expected);
179 }
180
181 @Test
182 public void testInlineRegexpHeader() throws Exception {
183 final DefaultConfiguration checkConfig =
184 createModuleConfig(RegexpHeaderCheck.class);
185 checkConfig.addProperty("header", "^/*$\\n// .*\\n// Created: 2002\\n^//.*\\n^//.*");
186 final String[] expected = {
187 "3: " + getCheckMessage(MSG_HEADER_MISMATCH, "// Created: 2002"),
188 };
189 verify(checkConfig, getPath("InputRegexpHeaderIgnore.java"), expected);
190 }
191
192 @Test
193 public void testFailureForMultilineRegexp() throws Exception {
194 final DefaultConfiguration checkConfig =
195 createModuleConfig(RegexpHeaderCheck.class);
196 checkConfig.addProperty("header", "^(.*\\n.*)");
197 try {
198 createChecker(checkConfig);
199 assertWithMessage(
200 "Checker creation should not succeed when regexp spans multiple lines").fail();
201 }
202 catch (CheckstyleException ex) {
203 assertWithMessage("Invalid exception message")
204 .that(ex.getMessage())
205 .isEqualTo("cannot initialize module"
206 + " com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheck"
207 + " - Cannot set property 'header' to '^(.*\\n.*)'");
208 }
209 }
210
211 @Test
212 public void testInlineRegexpHeaderConsecutiveNewlines() throws Exception {
213 final DefaultConfiguration checkConfig =
214 createModuleConfig(RegexpHeaderCheck.class);
215 checkConfig.addProperty("header", "^/*$\\n// .*\\n\\n// Created: 2017\\n^//.*");
216 final String[] expected = {
217 "3: " + getCheckMessage(MSG_HEADER_MISMATCH, "^$"),
218 };
219 verify(checkConfig, getPath("InputRegexpHeaderConsecutiveNewLines.java"), expected);
220 }
221
222 @Test
223 public void testInlineRegexpHeaderConsecutiveNewlinesThroughConfigFile() throws Exception {
224 final DefaultConfiguration checkConfig =
225 createModuleConfig(RegexpHeaderCheck.class);
226 checkConfig.addProperty("headerFile", getUriString("InputRegexpHeaderNewLines.header"));
227 final String[] expected = {
228 "3: " + getCheckMessage(MSG_HEADER_MISMATCH, "^$"),
229 };
230 verify(checkConfig, getPath("InputRegexpHeaderConsecutiveNewLines.java"), expected);
231 }
232
233 @Test
234 public void testRegexpHeaderIgnore() throws Exception {
235 final DefaultConfiguration checkConfig =
236 createModuleConfig(RegexpHeaderCheck.class);
237 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader1.header"));
238 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
239 verify(checkConfig, getPath("InputRegexpHeaderIgnore.java"), expected);
240 }
241
242 @Test
243 public void testRegexpHeaderMulti1() throws Exception {
244 final DefaultConfiguration checkConfig =
245 createModuleConfig(RegexpHeaderCheck.class);
246 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader2.header"));
247 checkConfig.addProperty("multiLines", "3, 6");
248 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
249 verify(checkConfig, getPath("InputRegexpHeaderDefaultConfig.java"), expected);
250 }
251
252 @Test
253 public void testRegexpHeaderMulti2() throws Exception {
254 final DefaultConfiguration checkConfig =
255 createModuleConfig(RegexpHeaderCheck.class);
256 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader2.header"));
257 checkConfig.addProperty("multiLines", "3, 6");
258 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
259 verify(checkConfig, getPath("InputRegexpHeaderMulti2.java"), expected);
260 }
261
262 @Test
263 public void testRegexpHeaderMulti3() throws Exception {
264 final DefaultConfiguration checkConfig =
265 createModuleConfig(RegexpHeaderCheck.class);
266 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader2.header"));
267 checkConfig.addProperty("multiLines", "3, 7");
268 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
269 verify(checkConfig, getPath("InputRegexpHeaderDefaultConfig.java"), expected);
270 }
271
272 @Test
273 public void testRegexpHeaderMulti4() throws Exception {
274 final DefaultConfiguration checkConfig =
275 createModuleConfig(RegexpHeaderCheck.class);
276 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader2.header"));
277 checkConfig.addProperty("multiLines", "3, 5, 6, 7");
278 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
279 verify(checkConfig, getPath("InputRegexpHeaderMulti4.java"), expected);
280 }
281
282 @Test
283 public void testRegexpHeaderMulti5() throws Exception {
284 final DefaultConfiguration checkConfig =
285 createModuleConfig(RegexpHeaderCheck.class);
286 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader2.header"));
287 checkConfig.addProperty("multiLines", "3");
288 final String[] expected = {
289 "1: " + getCheckMessage(MSG_HEADER_MISSING),
290 };
291 verify(checkConfig, getPath("InputRegexpHeaderMulti5.java"), expected);
292 }
293
294 @Test
295 public void testRegexpHeaderMulti6() throws Exception {
296 final DefaultConfiguration checkConfig =
297 createModuleConfig(RegexpHeaderCheck.class);
298 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader2_4.header"));
299 checkConfig.addProperty("multiLines", "8974382");
300 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
301 verify(checkConfig, getPath("InputRegexpHeaderMulti6.java"), expected);
302 }
303
304 @Test
305 public void testRegexpHeaderSmallHeader() throws Exception {
306 final DefaultConfiguration checkConfig =
307 createModuleConfig(RegexpHeaderCheck.class);
308 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader2.header"));
309 checkConfig.addProperty("multiLines", "3, 6");
310 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
311 verify(checkConfig, getPath("InputRegexpHeaderSmallHeader.java"), expected);
312 }
313
314 @Test
315 public void testEmptyMultiline()
316 throws Exception {
317 final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
318 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader2.header"));
319 checkConfig.addProperty("multiLines", "");
320 final String[] expected = {
321 "1: " + getCheckMessage(MSG_HEADER_MISSING),
322 };
323 verify(checkConfig, getPath("InputRegexpHeaderSmallHeader.java"), expected);
324 }
325
326 @Test
327 public void testRegexpHeaderMulti52()
328 throws Exception {
329 final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
330 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader3.header"));
331 final String[] expected = {
332 "1: " + getCheckMessage(MSG_HEADER_MISSING),
333 };
334 verify(checkConfig, getPath("InputRegexpHeaderMulti52.java"), expected);
335 }
336
337 @Test
338 public void testIgnoreLinesSorted() throws Exception {
339 final DefaultConfiguration checkConfig =
340 createModuleConfig(RegexpHeaderCheck.class);
341 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader5.header"));
342 checkConfig.addProperty("multiLines", "7,5,3");
343 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
344 verify(checkConfig, getPath("InputRegexpHeaderIgnoreLinesSorted.java"), expected);
345 }
346
347 @Test
348 public void testHeaderWithInvalidRegexp() throws Exception {
349 final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
350 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader.invalid.header"));
351 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
352 final String path = getPath("InputRegexpHeaderMulti52.java");
353 try {
354 verify(checkConfig, path, expected);
355 assertWithMessage("IllegalArgumentException is expected").fail();
356 }
357 catch (IllegalArgumentException ex) {
358 assertWithMessage("Invalid exception message")
359 .that(ex.getMessage())
360 .isEqualTo("line 3 in header specification is not a regular expression");
361 }
362 }
363
364 @Test
365 public void testNoWarningIfSingleLinedLeft() throws Exception {
366 final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
367 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader4.header"));
368 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
369 verify(checkConfig, getPath("InputRegexpHeaderMulti5.java"), expected);
370 }
371
372 @Test
373 public void testNoHeaderMissingErrorInCaseHeaderSizeEqualToFileSize() throws Exception {
374 final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
375 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader3.header"));
376 checkConfig.addProperty("multiLines", "1");
377 final String[] expected = {
378 "5: " + getCheckMessage(MSG_HEADER_MISMATCH, "^$"),
379 };
380 verify(checkConfig, getPath("InputRegexpHeaderMulti52.java"), expected);
381 }
382
383 @Test
384 public void testCharsetProperty1() throws Exception {
385 final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
386 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader7.header"));
387 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
388 final String path = getPath("InputRegexpHeader4.java");
389 verify(checkConfig, path, expected);
390 }
391
392 @Test
393 public void testCharsetProperty2() throws Exception {
394 final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
395 checkConfig.addProperty("charset", "US-ASCII");
396 checkConfig.addProperty("headerFile", getPath("InputRegexpHeader7.header"));
397 final String[] expected = {
398
399 "3: " + getCheckMessage(MSG_HEADER_MISMATCH, "// some.class.��������.passed"),
400 };
401 final String path = getPath("InputRegexpHeader4.java");
402 verify(checkConfig, path, expected);
403
404 }
405
406 @Test
407 public void testCharsetProperty3() throws Exception {
408 final DefaultConfiguration checkConfig = createModuleConfig(RegexpHeaderCheck.class);
409 checkConfig.addProperty("headerFile",
410 getPath("InputRegexpHeader7.header"));
411 checkConfig.addProperty("charset", "US-ASCII");
412 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
413 final String path = getPath("InputRegexpHeader3.java");
414 verify(checkConfig, path, expected);
415 }
416 }