View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.filters;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck.MSG_KEY;
24  import static com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck.MSG_JAVADOC_MISSING;
25  import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
26  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
27  
28  import java.io.File;
29  import java.nio.charset.StandardCharsets;
30  import java.util.regex.Pattern;
31  import java.util.regex.PatternSyntaxException;
32  
33  import org.junit.jupiter.api.Test;
34  
35  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
36  import com.puppycrawl.tools.checkstyle.JavaParser;
37  import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
38  import com.puppycrawl.tools.checkstyle.api.FileContents;
39  import com.puppycrawl.tools.checkstyle.api.FileText;
40  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
41  import com.puppycrawl.tools.checkstyle.api.Violation;
42  import com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck;
43  import com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck;
44  import com.puppycrawl.tools.checkstyle.checks.modifier.RedundantModifierCheck;
45  import com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck;
46  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
47  
48  public class SuppressionXpathSingleFilterTest
49          extends AbstractModuleTestSupport {
50  
51      @Override
52      public String getPackageLocation() {
53          return "com/puppycrawl/tools/checkstyle/filters/suppressionxpathsinglefilter";
54      }
55  
56      @Test
57      public void testMatching() throws Exception {
58          final String[] expected = {
59              "20:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
60                      MSG_JAVADOC_MISSING,
61                      "InputSuppressionXpathSingleFilterMatchingTokenType"),
62          };
63  
64          final String[] suppressed = {
65              "20:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
66                      MSG_JAVADOC_MISSING,
67                      "InputSuppressionXpathSingleFilterMatchingTokenType"),
68          };
69  
70          verifyFilterWithInlineConfigParser(
71              getPath("InputSuppressionXpathSingleFilterMatchingTokenType.java"), expected,
72              removeSuppressed(expected, suppressed));
73      }
74  
75      @Test
76      public void testNonMatchingTokenType() throws Exception {
77          final String[] expected = {
78              "20:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
79                      MSG_JAVADOC_MISSING,
80                      "InputSuppressionXpathSingleFilterNonMatchingTokenType"),
81          };
82  
83          final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
84  
85          verifyFilterWithInlineConfigParser(
86              getPath("InputSuppressionXpathSingleFilterNonMatchingTokenType.java"), expected,
87              removeSuppressed(expected, suppressed));
88      }
89  
90      @Test
91      public void testNonMatchingTokenTypeByXpath() throws Exception {
92          final String[] expected = {
93              "18:5: " + getCheckMessage(RedundantModifierCheck.class, "redundantModifier", "public"),
94          };
95  
96          final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
97  
98          verifyFilterWithInlineConfigParser(
99              getPath("InputSuppressionXpathSingleFilterNonMatchingTokenType2.java"), expected,
100             removeSuppressed(expected, suppressed));
101     }
102 
103     @Test
104     public void testNonMatchingLineNumber() throws Exception {
105         final String[] expected = {
106             "19:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
107                     MSG_JAVADOC_MISSING,
108                     "InputSuppressionXpathSingleFilterNonMatchingLineNumber"),
109             "22:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
110                     MSG_JAVADOC_MISSING,
111                     "TestClass"),
112         };
113 
114         final String[] suppressed = {
115             "22:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
116                     MSG_JAVADOC_MISSING,
117                     "TestClass"),
118         };
119 
120         verifyFilterWithInlineConfigParser(
121             getPath("InputSuppressionXpathSingleFilterNonMatchingLineNumber.java"), expected,
122             removeSuppressed(expected, suppressed));
123     }
124 
125     @Test
126     public void testNonMatchingColumnNumber() throws Exception {
127         final String[] expected = {
128             "23:11: " + getCheckMessage(TypeNameCheck.class, MSG_INVALID_PATTERN,
129                                         "testClass", "^[A-Z][a-zA-Z0-9]*$"),
130             "26:11: " + getCheckMessage(TypeNameCheck.class, MSG_INVALID_PATTERN,
131                                         "anotherTestClass", "^[A-Z][a-zA-Z0-9]*$"),
132         };
133         final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
134 
135         verifyFilterWithInlineConfigParser(
136             getPath("InputSuppressionXpathSingleFilterNonMatchingColumnNumber.java"), expected,
137             removeSuppressed(expected, suppressed));
138     }
139 
140     @Test
141     public void testNonMatchingColumnNumberByXpath() throws Exception {
142         final String[] expected = {
143             "22:13: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "11"),
144             "22:18: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "12"),
145         };
146 
147         final String[] suppressed = {
148             "22:18: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "12"),
149         };
150 
151         verifyFilterWithInlineConfigParser(
152             getPath("InputSuppressionXpathSingleFilterNonMatchingColumnNumber2.java"), expected,
153             removeSuppressed(expected, suppressed));
154     }
155 
156     @Test
157     public void testComplexQuery() throws Exception {
158         final String[] expected = {
159             "27:21: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "3.14"),
160             "28:16: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "123"),
161             "32:28: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "123"),
162         };
163         final String[] suppressed = {
164             "27:21: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "3.14"),
165         };
166         verifyFilterWithInlineConfigParser(
167             getPath("InputSuppressionXpathSingleFilterComplexQuery.java"), expected,
168             removeSuppressed(expected, suppressed));
169     }
170 
171     @Test
172     public void testIncorrectQuery() {
173         final String xpath = "1@#";
174         final IllegalArgumentException exc = getExpectedThrowable(
175                 IllegalArgumentException.class,
176                 () -> {
177                     createSuppressionXpathSingleFilter(
178                             "InputSuppressionXpathSingleFilterComplexQuery", "Test",
179                             null, null, xpath);
180                 });
181         assertWithMessage("Message should be: Unexpected xpath query")
182                 .that(exc.getMessage())
183                 .contains("Incorrect xpath query");
184     }
185 
186     @Test
187     public void testNoQuery() throws Exception {
188         final String[] expected = {
189             "19:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
190                     MSG_JAVADOC_MISSING,
191                     "InputSuppressionXpathSingleFilterNoQuery"),
192         };
193 
194         final String[] suppressed = {
195             "19:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
196                     MSG_JAVADOC_MISSING,
197                     "InputSuppressionXpathSingleFilterNoQuery"),
198         };
199 
200         verifyFilterWithInlineConfigParser(
201             getPath("InputSuppressionXpathSingleFilterNoQuery.java"), expected,
202             removeSuppressed(expected, suppressed));
203     }
204 
205     @Test
206     public void testNullFileName() throws Exception {
207         final String[] expected = {
208             "19:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
209                     MSG_JAVADOC_MISSING,
210                     "InputSuppressionXpathSingleFilterNullFileName"),
211         };
212 
213         final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
214 
215         verifyFilterWithInlineConfigParser(
216             getPath("InputSuppressionXpathSingleFilterNullFileName.java"), expected,
217             removeSuppressed(expected, suppressed));
218     }
219 
220     @Test
221     public void testNonMatchingFileRegexp() throws Exception {
222         final String[] expected = {
223             "19:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
224                     MSG_JAVADOC_MISSING,
225                     "InputSuppressionXpathSingleFilterNonMatchingFileRegexp"),
226         };
227 
228         final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
229 
230         verifyFilterWithInlineConfigParser(
231             getPath("InputSuppressionXpathSingleFilterNonMatchingFileRegexp.java"), expected,
232             removeSuppressed(expected, suppressed));
233     }
234 
235     @Test
236     public void testInvalidFileRegexp() {
237         final SuppressionXpathSingleFilter filter = new SuppressionXpathSingleFilter();
238         final PatternSyntaxException exc = getExpectedThrowable(
239                 PatternSyntaxException.class,
240                 () -> {
241                     filter.setFiles("e[l");
242                 });
243         assertWithMessage("Message should be: Unclosed character class")
244                 .that(exc.getMessage())
245                 .contains("Unclosed character class");
246     }
247 
248     @Test
249     public void testInvalidCheckRegexp() {
250         final SuppressionXpathSingleFilter filter = new SuppressionXpathSingleFilter();
251         final PatternSyntaxException exc = getExpectedThrowable(
252                 PatternSyntaxException.class,
253                 () -> {
254                     filter.setChecks("e[l");
255                 });
256         assertWithMessage("Message should be: Unclosed character class")
257                 .that(exc.getMessage())
258                 .contains("Unclosed character class");
259     }
260 
261     @Test
262     public void testNullViolation() throws Exception {
263         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
264         final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
265 
266         verifyFilterWithInlineConfigParser(
267             getPath("InputSuppressionXpathSingleFilterNullViolation.java"), expected,
268             removeSuppressed(expected, suppressed));
269     }
270 
271     @Test
272     public void testNonMatchingModuleId() throws Exception {
273         final String[] expected = {
274             "21:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
275                     MSG_JAVADOC_MISSING,
276                     "InputSuppressionXpathSingleFilterNonMatchingModuleId"),
277         };
278 
279         final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
280 
281         verifyFilterWithInlineConfigParser(
282             getPath("InputSuppressionXpathSingleFilterNonMatchingModuleId.java"), expected,
283             removeSuppressed(expected, suppressed));
284     }
285 
286     @Test
287     public void testMatchingModuleId() throws Exception {
288         final String[] expected = {
289             "21:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
290                     MSG_JAVADOC_MISSING,
291                     "InputSuppressionXpathSingleFilterMatchingModuleId"),
292         };
293 
294         final String[] suppressed = {
295             "21:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
296                     MSG_JAVADOC_MISSING,
297                     "InputSuppressionXpathSingleFilterMatchingModuleId"),
298         };
299 
300         verifyFilterWithInlineConfigParser(
301             getPath("InputSuppressionXpathSingleFilterMatchingModuleId.java"), expected,
302             removeSuppressed(expected, suppressed));
303     }
304 
305     @Test
306     public void testNonMatchingChecks() throws Exception {
307         final String[] expected = {
308             "20:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
309                     MSG_JAVADOC_MISSING,
310                     "InputSuppressionXpathSingleFilterNonMatchingCheck"),
311         };
312 
313         final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
314 
315         verifyFilterWithInlineConfigParser(
316             getPath("InputSuppressionXpathSingleFilterNonMatchingCheck.java"), expected,
317             removeSuppressed(expected, suppressed));
318     }
319 
320     @Test
321     public void testNonMatchingFileNameModuleIdAndCheck() throws Exception {
322         final String[] expected = {
323             "21:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
324                     MSG_JAVADOC_MISSING,
325                     "InputSuppressionXpathSingleFilterNonMatchingFileNameModuleIdAndCheck"),
326         };
327         final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
328 
329         verifyFilterWithInlineConfigParser(
330             getPath("InputSuppressionXpathSingleFilterNonMatchingFileNameModuleIdAndCheck.java"),
331             expected, removeSuppressed(expected, suppressed));
332     }
333 
334     @Test
335     public void testNullModuleIdAndNonMatchingChecks() throws Exception {
336         final String[] expected = {
337             "21:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
338                     MSG_JAVADOC_MISSING,
339                     "InputSuppressionXpathSingleFilterNullModuleIdAndNonMatchingCheck"),
340         };
341         final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
342 
343         verifyFilterWithInlineConfigParser(
344             getPath("InputSuppressionXpathSingleFilterNullModuleIdAndNonMatchingCheck.java"),
345             expected, removeSuppressed(expected, suppressed));
346     }
347 
348     @Test
349     public void testDecideByMessage() throws Exception {
350         final String[] expected = {
351             "29:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
352                     MSG_JAVADOC_MISSING,
353                     "InputSuppressionXpathSingleFilterDecideByMessage"),
354             "32:21: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "3.14"),
355             "33:16: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "123"),
356             "37:28: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "123"),
357         };
358 
359         final String[] suppressed = {
360             "29:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
361                     MSG_JAVADOC_MISSING,
362                     "InputSuppressionXpathSingleFilterDecideByMessage"),
363         };
364 
365         verifyFilterWithInlineConfigParser(
366             getPath("InputSuppressionXpathSingleFilterDecideByMessage.java"),
367             expected, removeSuppressed(expected, suppressed));
368     }
369 
370     @Test
371     public void testThrowException() throws Exception {
372         final String xpath = "//CLASS_DEF[@text='InputSuppressionXpathSingleFilterComplexQuery']";
373         final SuppressionXpathSingleFilter filter =
374                 createSuppressionXpathSingleFilter("InputSuppressionXpathSingleFilterComplexQuery",
375                         "Test", null, null, xpath);
376         final Violation message =
377                 new Violation(3, 0, TokenTypes.CLASS_DEF, "",
378                         "", null, null, "id19",
379                         getClass(), null);
380         final FileContents fileContents = new FileContents(new FileText(
381             new File(getPath("InputSuppressionXpathSingleFilterComplexQuery.java")),
382             StandardCharsets.UTF_8.name()));
383         final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents,
384                 "InputSuppressionXpathSingleFilterComplexQuery.java", message, null);
385         final IllegalStateException exc = getExpectedThrowable(
386                 IllegalStateException.class,
387                 () -> {
388                     filter.accept(ev);
389                 });
390         assertWithMessage("Exception message does not match expected one")
391                 .that(exc.getMessage())
392                 .contains("Cannot initialize context and evaluate query");
393     }
394 
395     @Test
396     public void testAllNullConfiguration() throws Exception {
397         final String[] expected = {
398             "19:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
399                     MSG_JAVADOC_MISSING,
400                     "InputSuppressionXpathSingleFilterAllNullConfiguration"),
401         };
402 
403         final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
404 
405         verifyFilterWithInlineConfigParser(
406                 getPath("InputSuppressionXpathSingleFilterAllNullConfiguration.java"),
407                 expected, removeSuppressed(expected, suppressed));
408     }
409 
410     @Test
411     public void testDecideByIdAndExpression() throws Exception {
412         final String[] expected = {
413             "21:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
414                     MSG_JAVADOC_MISSING,
415                     "InputSuppressionXpathSingleFilterDecideByIdAndExpression"),
416         };
417 
418         final String[] suppressed = {
419             "21:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
420                     MSG_JAVADOC_MISSING,
421                     "InputSuppressionXpathSingleFilterDecideByIdAndExpression"),
422         };
423 
424         verifyFilterWithInlineConfigParser(
425                 getPath("InputSuppressionXpathSingleFilterDecideByIdAndExpression.java"),
426                 expected, removeSuppressed(expected, suppressed));
427     }
428 
429     @Test
430     public void testDefaultFileProperty() throws Exception {
431         final String[] expected = {
432             "21:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
433                     MSG_JAVADOC_MISSING,
434                     "InputSuppressionXpathSingleFilterDefaultFileProperty"),
435         };
436 
437         final String[] suppressed = {
438             "21:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
439                     MSG_JAVADOC_MISSING,
440                     "InputSuppressionXpathSingleFilterDefaultFileProperty"),
441         };
442 
443         verifyFilterWithInlineConfigParser(
444                 getPath("InputSuppressionXpathSingleFilterDefaultFileProperty.java"),
445                 expected, removeSuppressed(expected, suppressed));
446     }
447 
448     @Test
449     public void testDecideByCheck() throws Exception {
450         final String[] expected = {
451             "19:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
452                     MSG_JAVADOC_MISSING,
453                     "InputSuppressionXpathSingleFilterDecideByCheck"),
454         };
455 
456         final String[] suppressed = {
457             "19:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
458                     MSG_JAVADOC_MISSING,
459                     "InputSuppressionXpathSingleFilterDecideByCheck"),
460         };
461 
462         verifyFilterWithInlineConfigParser(
463                 getPath("InputSuppressionXpathSingleFilterDecideByCheck.java"),
464                 expected, removeSuppressed(expected, suppressed));
465     }
466 
467     @Test
468     public void testDecideById() throws Exception {
469         final String[] expected = {
470             "20:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
471                     MSG_JAVADOC_MISSING,
472                     "InputSuppressionXpathSingleFilterDecideById"),
473         };
474 
475         final String[] suppressed = {
476             "20:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
477                     MSG_JAVADOC_MISSING,
478                     "InputSuppressionXpathSingleFilterDecideById"),
479         };
480 
481         verifyFilterWithInlineConfigParser(
482                 getPath("InputSuppressionXpathSingleFilterDecideById.java"),
483                 expected, removeSuppressed(expected, suppressed));
484     }
485 
486     @Test
487     public void testNonMatchingCheckRegexp() throws Exception {
488         final String[] expected = {
489             "20:1: " + getCheckMessage(MissingJavadocTypeCheck.class,
490                     MSG_JAVADOC_MISSING,
491                     "InputSuppressionXpathSingleFilterNonMatchingCheckRegexp"),
492         };
493 
494         final String[] suppressed = CommonUtil.EMPTY_STRING_ARRAY;
495 
496         verifyFilterWithInlineConfigParser(
497             getPath("InputSuppressionXpathSingleFilterNonMatchingCheckRegexp.java"), expected,
498             removeSuppressed(expected, suppressed));
499     }
500 
501     /**
502      * This test is required to cover pitest mutation
503      * for reset of 'file' field in SuppressionXpathSingleFilter.
504      * This not possible to reproduce by natural execution of Checkstyle
505      * as config is never changed in runtime.
506      * Projects that use us by api can reproduce this case.
507      * We need to allow users to reset module property to default state.
508      *
509      * @throws Exception when there is problem to load Input file
510      */
511     @Test
512     public void testUpdateFilterFileSettingInRunTime() throws Exception {
513         final File file = new File(getPath("InputSuppressionXpathSingleFilterComplexQuery.java"));
514 
515         final SuppressionXpathSingleFilter filter = new SuppressionXpathSingleFilter();
516         filter.setChecks("MagicNumber");
517         filter.finishLocalSetup();
518 
519         final Violation violation = new Violation(27, 21, TokenTypes.NUM_DOUBLE, "",
520                         "", null, null, null,
521                         MagicNumberCheck.class, null);
522 
523         final FileContents fileContents =
524                 new FileContents(new FileText(file, StandardCharsets.UTF_8.name()));
525 
526         final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(),
527                 violation, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
528 
529         assertWithMessage("match ia expected as 'files' is defaulted")
530                 .that(filter.accept(ev))
531                 .isFalse();
532 
533         // set non-default value for files
534         filter.setFiles(Pattern.quote(file.getPath() + ".never.match"));
535         filter.finishLocalSetup();
536 
537         assertWithMessage("no match is expected due to weird value in 'files'")
538                 .that(filter.accept(ev))
539                 .isTrue();
540 
541         // reset files to default value of filter
542         filter.setFiles(null);
543         filter.finishLocalSetup();
544 
545         assertWithMessage("match is expected as 'files' is defaulted")
546                 .that(filter.accept(ev))
547                 .isFalse();
548     }
549 
550     /**
551      * This test is required to cover pitest mutation
552      * for reset of 'checks' field in SuppressionXpathSingleFilter.
553      * This not possible to reproduce by natural execution of Checkstyle
554      * as config is never changed in runtime.
555      * Projects that use us by api can reproduce this case.
556      * We need to allow users to reset module property to default state.
557      *
558      * @throws Exception when there is problem to load Input file
559      */
560     @Test
561     public void testUpdateFilterChecksSettingInRunTime() throws Exception {
562         final File file = new File(getPath("InputSuppressionXpathSingleFilterComplexQuery.java"));
563 
564         final SuppressionXpathSingleFilter filter = new SuppressionXpathSingleFilter();
565         filter.setChecks("MagicNumber");
566         filter.finishLocalSetup();
567 
568         final Violation violation = new Violation(27, 21, TokenTypes.NUM_DOUBLE, "",
569                 "", null, null, null,
570                 MagicNumberCheck.class, null);
571 
572         final FileContents fileContents =
573                 new FileContents(new FileText(file, StandardCharsets.UTF_8.name()));
574 
575         final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(),
576                 violation, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
577 
578         assertWithMessage("match is expected as 'checks' is set")
579                 .that(filter.accept(ev))
580                 .isFalse();
581 
582         // reset checks to default value of filter
583         filter.setChecks(null);
584         filter.finishLocalSetup();
585 
586         assertWithMessage("no match is expected as whole filter is defaulted (empty)")
587                 .that(filter.accept(ev))
588                 .isTrue();
589     }
590 
591     /**
592      * This test is required to cover pitest mutation
593      * for reset of 'message' field in SuppressionXpathSingleFilter.
594      * This not possible to reproduce by natural execution of Checkstyle
595      * as config is never changed in runtime.
596      * Projects that use us by api can reproduce this case.
597      * We need to allow users to reset module property to default state.
598      *
599      * @throws Exception when there is problem to load Input file
600      */
601 
602     @Test
603     public void testSetMessageHandlesNullCorrectly() throws Exception {
604         final File file = new File(getPath("InputSuppressionXpathSingleFilterComplexQuery.java"));
605 
606         final SuppressionXpathSingleFilter filter = new SuppressionXpathSingleFilter();
607         filter.setMessage("MagicNumber");
608         filter.finishLocalSetup();
609 
610         final Violation violation = new Violation(27, 21, TokenTypes.NUM_DOUBLE, "",
611                 "", null, null, null,
612                 MagicNumberCheck.class, "MagicNumber");
613 
614         final FileContents fileContents =
615                 new FileContents(new FileText(file, StandardCharsets.UTF_8.name()));
616 
617         final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(),
618                 violation, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
619 
620         assertWithMessage("match is expected as 'message' is set")
621                 .that(filter.accept(ev))
622                 .isFalse();
623 
624         filter.setMessage(null);
625         filter.finishLocalSetup();
626 
627         assertWithMessage("no match is expected as whole filter is defaulted (empty)")
628                 .that(filter.accept(ev))
629                 .isTrue();
630     }
631 
632     private static SuppressionXpathSingleFilter createSuppressionXpathSingleFilter(
633             String files, String checks, String message, String moduleId, String query) {
634         final SuppressionXpathSingleFilter filter = new SuppressionXpathSingleFilter();
635         filter.setFiles(files);
636         filter.setChecks(checks);
637         filter.setMessage(message);
638         filter.setId(moduleId);
639         filter.setQuery(query);
640         filter.finishLocalSetup();
641         return filter;
642     }
643 
644 }