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