View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck.MSG_KEY;
23  import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_ILLEGAL_REGEXP;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck;
29  import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck;
30  import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck;
31  import com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck;
32  
33  public class SuppressionSingleFilterExamplesTest extends AbstractExamplesModuleTestSupport {
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/filters/suppressionsinglefilter";
37      }
38  
39      @Test
40      public void testExample1() throws Exception {
41          final String[] expectedWithoutFilter = {
42              "21:17: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "100"),
43          };
44          final String[] expectedWithFilter = {};
45  
46          verifyFilterWithInlineConfigParser(getPath("Example1.java"),
47                  expectedWithoutFilter, expectedWithFilter);
48      }
49  
50      @Test
51      public void testExample2() throws Exception {
52          final String[] expectedWithoutFilter = {
53              "19:12: " + getCheckMessage(NoWhitespaceAfterCheck.class,
54                      NoWhitespaceAfterCheck.MSG_KEY, "."),
55              "23:9: " + getCheckMessage(NoWhitespaceAfterCheck.class,
56                      NoWhitespaceAfterCheck.MSG_KEY, "int"),
57          };
58          final String[] expectedWithFilter = {
59              "23:9: " + getCheckMessage(NoWhitespaceAfterCheck.class,
60                      NoWhitespaceAfterCheck.MSG_KEY, "int"),
61          };
62  
63          verifyFilterWithInlineConfigParser(getPath("Example2.java"),
64                  expectedWithoutFilter, expectedWithFilter);
65      }
66  
67      @Test
68      public void testExample3() throws Exception {
69          final String[] expectedWithoutFilter = {
70              "19: " + getCheckMessage(RegexpCheck.class, MSG_ILLEGAL_REGEXP, "example"),
71              "4: " + getCheckMessage(RegexpCheck.class, MSG_ILLEGAL_REGEXP, "example"),
72          };
73          final String[] expectedWithFilter = {};
74  
75          verifyFilterWithInlineConfigParser(getPath("Example3.java"),
76                  expectedWithoutFilter, expectedWithFilter);
77      }
78  
79      @Test
80      public void testExample4() throws Exception {
81          final String[] expectedWithoutFilter = {
82              "18:15: " + getCheckMessage(MemberNameCheck.class, "name.invalidPattern",
83                      "MyVariable", "^[a-z][a-zA-Z0-9]*$"),
84          };
85          final String[] expectedWithFilter = {};
86  
87          verifyFilterWithInlineConfigParser(getPath("Example4.java"),
88                  expectedWithoutFilter, expectedWithFilter);
89      }
90  }