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.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  
35      @Override
36      public String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/filters/suppressionsinglefilter";
38      }
39  
40      @Test
41      public void testExample1() throws Exception {
42          final String[] expectedWithoutFilter = {
43              "21:28: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "5"),
44              "24:17: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "100"),
45              "26:12: " + getCheckMessage(NoWhitespaceAfterCheck.class,
46                      NoWhitespaceAfterCheck.MSG_KEY, "."),
47              "30:9: " + getCheckMessage(NoWhitespaceAfterCheck.class,
48                      NoWhitespaceAfterCheck.MSG_KEY, "int"),
49          };
50          final String[] expectedWithFilter = {};
51  
52          verifyFilterWithInlineConfigParser(getPath("Example1.java"),
53                  expectedWithoutFilter, expectedWithFilter);
54      }
55  
56      @Test
57      public void testExample2() throws Exception {
58          final String[] expectedWithoutFilter = {
59              "22:12: " + getCheckMessage(NoWhitespaceAfterCheck.class,
60                      NoWhitespaceAfterCheck.MSG_KEY, "."),
61              "26:9: " + getCheckMessage(NoWhitespaceAfterCheck.class,
62                      NoWhitespaceAfterCheck.MSG_KEY, "int"),
63          };
64          final String[] expectedWithFilter = {
65              "26:9: " + getCheckMessage(NoWhitespaceAfterCheck.class,
66                      NoWhitespaceAfterCheck.MSG_KEY, "int"),
67          };
68  
69          verifyFilterWithInlineConfigParser(getPath("Example2.java"),
70                  expectedWithoutFilter, expectedWithFilter);
71      }
72  
73      @Test
74      public void testExample3() throws Exception {
75          final String[] expectedWithoutFilter = {
76              "4: " + getCheckMessage(RegexpCheck.class, MSG_ILLEGAL_REGEXP, "example"),
77              "19: " + getCheckMessage(RegexpCheck.class, MSG_ILLEGAL_REGEXP, "example"),
78              "28: " + getCheckMessage(RegexpCheck.class, MSG_ILLEGAL_REGEXP, "example"),
79          };
80          final String[] expectedWithFilter = {};
81  
82          verifyFilterWithInlineConfigParser(getPath("Example3.java"),
83                  expectedWithoutFilter, expectedWithFilter);
84      }
85  
86      @Test
87      public void testExample4() throws Exception {
88          final String[] expectedWithoutFilter = {
89              "18:15: " + getCheckMessage(MemberNameCheck.class, "name.invalidPattern",
90                      "MyVariable", "^[a-z][a-zA-Z0-9]*$"),
91          };
92          final String[] expectedWithFilter = {};
93  
94          verifyFilterWithInlineConfigParser(getPath("Example4.java"),
95                  expectedWithoutFilter, expectedWithFilter);
96      }
97  
98      @Test
99      public void testExample5() throws Exception {
100         final String[] expectedWithoutFilter = {
101             "16:28: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "5"),
102             "19:17: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "100"),
103         };
104         final String[] expectedWithFilter = {
105             "16:28: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "5"),
106         };
107 
108         verifyFilterWithInlineConfigParser(getPath("Example5.java"),
109                 expectedWithoutFilter, expectedWithFilter);
110     }
111 
112     @Test
113     public void testExample6() throws Exception {
114         final String[] expectedWithoutFilter = {
115             "15:28: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "5"),
116             "18:17: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "100"),
117         };
118         final String[] expectedWithFilter = {};
119 
120         verifyFilterWithInlineConfigParser(getPath("Example6.java"),
121                 expectedWithoutFilter, expectedWithFilter);
122     }
123 
124     @Test
125     public void testExample7() throws Exception {
126         final String[] expectedWithoutFilter = {
127             "17:28: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "5"),
128             "20:17: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "100"),
129         };
130         final String[] expectedWithFilter = {};
131 
132         verifyFilterWithInlineConfigParser(getPath("Example7.java"),
133                 expectedWithoutFilter, expectedWithFilter);
134     }
135 
136 }