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.EqualsAvoidNullCheck.MSG_EQUALS_AVOID_NULL;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck.MSG_KEY;
24  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.MSG_RETURN_EXPECTED;
25  import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_ILLEGAL_REGEXP;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck;
31  import com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck;
32  import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck;
33  import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
34  import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck;
35  import com.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck;
36  import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck;
37  import com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck;
38  import com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck;
39  import com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck;
40  
41  public class SuppressionSingleFilterExamplesTest extends AbstractExamplesModuleTestSupport {
42      @Override
43      protected String getPackageLocation() {
44          return "com/puppycrawl/tools/checkstyle/filters/suppressionsinglefilter";
45      }
46  
47      @Test
48      public void testExample1() throws Exception {
49          final String[] expectedWithoutFilter = {
50              "21:17: " + getCheckMessage(MagicNumberCheck.class, MSG_KEY, "100"),
51          };
52          final String[] expectedWithFilter = {};
53  
54          verifyFilterWithInlineConfigParser(getPath("Example1.java"),
55                  expectedWithoutFilter, expectedWithFilter);
56      }
57  
58      @Test
59      public void testExample2() throws Exception {
60          final String[] expectedWithoutFilter = {
61              "19:13: " + getCheckMessage(EqualsAvoidNullCheck.class, MSG_EQUALS_AVOID_NULL),
62              "25: " + getCheckMessage(JavadocMethodCheck.class, MSG_RETURN_EXPECTED),
63          };
64          final String[] expectedWithFilter = {};
65  
66          verifyFilterWithInlineConfigParser(getPath("Example2.java"),
67                  expectedWithoutFilter, expectedWithFilter);
68      }
69  
70      @Test
71      public void testExample3() throws Exception {
72          final String[] expectedWithoutFilter = {
73              "19: " + getCheckMessage(RegexpCheck.class, MSG_ILLEGAL_REGEXP, "example"),
74              "4: " + getCheckMessage(RegexpCheck.class, MSG_ILLEGAL_REGEXP, "example"),
75          };
76          final String[] expectedWithFilter = {};
77  
78          verifyFilterWithInlineConfigParser(getPath("Example3.java"),
79                  expectedWithoutFilter, expectedWithFilter);
80      }
81  
82      @Test
83      public void testExample4() throws Exception {
84          final String[] expectedWithoutFilter = {
85              "18:12: " + getCheckMessage(NoWhitespaceAfterCheck.class,
86                      NoWhitespaceAfterCheck.MSG_KEY, "."),
87              "22:9: " + getCheckMessage(NoWhitespaceAfterCheck.class,
88                      NoWhitespaceAfterCheck.MSG_KEY, "int"),
89          };
90          final String[] expectedWithFilter = {};
91  
92          verifyFilterWithInlineConfigParser(getPath("Example4.java"),
93                  expectedWithoutFilter, expectedWithFilter);
94      }
95  
96      @Test
97      public void testExample5() throws Exception {
98          final String[] expectedWithoutFilter = {
99              "14:15: " + getCheckMessage(MethodNameCheck.class, "name.invalidPattern",
100                     "example_Method", "^[a-z][a-zA-Z0-9]*$"),
101             "17:15: " + getCheckMessage(MethodNameCheck.class, "name.invalidPattern",
102                     "Another_Method", "^[a-z][a-zA-Z0-9]*$"),
103         };
104         final String[] expectedWithFilter = {};
105 
106         verifyFilterWithInlineConfigParser(getPath("Example5.java"),
107                 expectedWithoutFilter, expectedWithFilter);
108     }
109 
110     @Test
111     public void testExample6() throws Exception {
112         final String[] expectedWithoutFilter = {
113             "14:28: " + getCheckMessage(ConstantNameCheck.class, "name.invalidPattern",
114                     "myConstant", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
115         };
116         final String[] expectedWithFilter = {};
117 
118         verifyFilterWithInlineConfigParser(getPath("Example6.java"),
119                 expectedWithoutFilter, expectedWithFilter);
120     }
121 
122     @Test
123     public void testExample7() throws Exception {
124         final String[] expectedWithoutFilter = {
125             "15:15: " + getCheckMessage(MemberNameCheck.class, "name.invalidPattern",
126                     "MyVariable", "^[a-z][a-zA-Z0-9]*$"),
127             "17:15: " + getCheckMessage(MethodNameCheck.class, "name.invalidPattern",
128                     "MyMethod", "^[a-z][a-zA-Z0-9]*$"),
129         };
130         final String[] expectedWithFilter = {};
131 
132         verifyFilterWithInlineConfigParser(getPath("Example7.java"),
133                 expectedWithoutFilter, expectedWithFilter);
134     }
135 
136     @Test
137     public void testExample8() throws Exception {
138         final String[] expectedWithoutFilter = {
139             "18:15: " + getCheckMessage(ParameterNumberCheck.class,
140                     ParameterNumberCheck.MSG_KEY, 5, 6),
141         };
142         final String[] expectedWithFilter = {};
143 
144         verifyFilterWithInlineConfigParser(getPath("Example8.java"),
145                 expectedWithoutFilter, expectedWithFilter);
146     }
147 
148     @Test
149     public void testExample9() throws Exception {
150         final String[] expectedWithoutFilter = {
151             "1: " + getCheckMessage(FileLengthCheck.class,
152                     FileLengthCheck.MSG_KEY, 21, 1),
153         };
154         final String[] expectedWithFilter = {};
155 
156         verifyFilterWithInlineConfigParser(getPath("Example9.java"),
157                 expectedWithoutFilter, expectedWithFilter);
158     }
159 
160     @Test
161     public void testExample10() throws Exception {
162         final String[] expectedWithoutFilter = {
163             "16:18: " + getCheckMessage(MemberNameCheck.class, "name.invalidPattern",
164                     "log", "^[A-Z][a-zA-Z0-9]*$"),
165         };
166         final String[] expectedWithFilter = {};
167 
168         verifyFilterWithInlineConfigParser(getPath("Example10.java"),
169                 expectedWithoutFilter, expectedWithFilter);
170     }
171 }