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.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck;
28  import com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck;
29  import com.puppycrawl.tools.checkstyle.checks.metrics.ClassDataAbstractionCouplingCheck;
30  import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
31  import com.puppycrawl.tools.checkstyle.checks.naming.MemberNameCheck;
32  import com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck;
33  
34  public class SuppressWithNearbyCommentFilterExamplesTest extends AbstractExamplesModuleTestSupport {
35  
36      @Override
37      public String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/filters/suppresswithnearbycommentfilter";
39      }
40  
41      @Test
42      public void testExample1() throws Exception {
43          final String[] expectedWithFilter = {
44  
45          };
46          final String[] expectedWithoutFilter = {
47              "13:27: " + getCheckMessage(NoWhitespaceAfterCheck.class,
48                      NoWhitespaceAfterCheck.MSG_KEY, "int"),
49          };
50  
51          verifyFilterWithInlineConfigParser(getPath("Example1.java"), expectedWithoutFilter,
52                  expectedWithFilter);
53      }
54  
55      @Test
56      public void testExample2() throws Exception {
57          final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
58  
59          final String[] expectedWithFilter = {
60              "17:30: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
61                      "array", pattern),
62          };
63          final String[] expectedWithoutFilter = {
64              "17:30: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
65                      "array", pattern),
66              "19:27: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
67                      "lowerCaseConstant", pattern),
68          };
69  
70          verifyFilterWithInlineConfigParser(getPath("Example2.java"), expectedWithoutFilter,
71                  expectedWithFilter);
72      }
73  
74      @Test
75      public void testExample3() throws Exception {
76          final String[] expectedWithFilter = {
77  
78          };
79          final String[] expectedWithoutFilter = {
80              "27:5: " + getCheckMessage(IllegalCatchCheck.class, IllegalCatchCheck.MSG_KEY,
81                      "RuntimeException"),
82          };
83  
84          verifyFilterWithInlineConfigParser(getPath("Example3.java"), expectedWithoutFilter,
85                  expectedWithFilter);
86      }
87  
88      @Test
89      public void testExample4() throws Exception {
90          final String[] expectedWithFilter = {
91  
92          };
93          final String[] expectedWithoutFilter = {
94              "15:27: " + getCheckMessage(NoWhitespaceAfterCheck.class,
95                      NoWhitespaceAfterCheck.MSG_KEY, "int"),
96          };
97  
98          verifyFilterWithInlineConfigParser(getPath("Example4.java"), expectedWithoutFilter,
99                  expectedWithFilter);
100     }
101 
102     @Test
103     public void testExample5() throws Exception {
104         final String[] expectedWithFilter = {
105             "15:27: " + getCheckMessage(NoWhitespaceAfterCheck.class,
106                     NoWhitespaceAfterCheck.MSG_KEY, "int"),
107         };
108         final String[] expectedWithoutFilter = {
109             "15:27: " + getCheckMessage(NoWhitespaceAfterCheck.class,
110                     NoWhitespaceAfterCheck.MSG_KEY, "int"),
111         };
112 
113         verifyFilterWithInlineConfigParser(getPath("Example5.java"), expectedWithoutFilter,
114                 expectedWithFilter);
115     }
116 
117     @Test
118     public void testExample7() throws Exception {
119         final String[] expectedWithFilter = {};
120 
121         final String[] expectedWithoutFilter = {
122             "15:27: " + getCheckMessage(NoWhitespaceAfterCheck.class,
123                     NoWhitespaceAfterCheck.MSG_KEY, "int"),
124         };
125 
126         verifyFilterWithInlineConfigParser(getPath("Example7.java"), expectedWithoutFilter,
127                 expectedWithFilter);
128     }
129 
130     @Test
131     public void testExample8() throws Exception {
132         final String[] expectedWithFilter = {};
133 
134         final String[] expectedWithoutFilter = {
135             "15:27: " + getCheckMessage(NoWhitespaceAfterCheck.class,
136                     NoWhitespaceAfterCheck.MSG_KEY, "int"),
137         };
138 
139         verifyFilterWithInlineConfigParser(getPath("Example8.java"), expectedWithoutFilter,
140                 expectedWithFilter);
141     }
142 
143     @Test
144     public void testExample6() throws Exception {
145         final String[] expectedWithFilter = {
146             "19:27: " + getCheckMessage(NoWhitespaceAfterCheck.class,
147                     NoWhitespaceAfterCheck.MSG_KEY, "int"),
148         };
149         final String[] expectedWithoutFilter = {
150             "19:27: " + getCheckMessage(NoWhitespaceAfterCheck.class,
151                     NoWhitespaceAfterCheck.MSG_KEY, "int"),
152         };
153 
154         verifyFilterWithInlineConfigParser(getPath("Example6.java"), expectedWithoutFilter,
155                 expectedWithFilter);
156     }
157 
158     @Test
159     public void testUseCase1() throws Exception {
160         final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
161 
162         final String[] expectedWithFilter = {
163             "26:20: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
164                     "lowerCaseConstant5", pattern),
165         };
166         final String[] expectedWithoutFilter = {
167             "19:20: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
168                     "lowerCaseConstant1", pattern),
169             "21:20: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
170                     "lowerCaseConstant2", pattern),
171             "23:20: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
172                     "lowerCaseConstant3", pattern),
173             "25:20: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
174                     "lowerCaseConstant4", pattern),
175             "26:20: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
176                     "lowerCaseConstant5", pattern),
177         };
178 
179         verifyFilterWithInlineConfigParser(getPath("UseCase1.java"), expectedWithoutFilter,
180                 expectedWithFilter);
181     }
182 
183     @Test
184     public void testUseCase2() throws Exception {
185         final String[] expectedWithFilter = {
186 
187         };
188         final String[] expectedWithoutFilter = {
189             "17:15: " + getCheckMessage(MemberNameCheck.class, MSG_INVALID_PATTERN,
190                     "D2", "^[a-z][a-zA-Z0-9]*$"),
191         };
192 
193         verifyFilterWithInlineConfigParser(getPath("UseCase2.java"), expectedWithoutFilter,
194                 expectedWithFilter);
195     }
196 
197     @Test
198     public void testUseCase3() throws Exception {
199         final String[] expectedWithFilters = {
200 
201         };
202         final String[] expectedWithoutFilters = {
203             "19:27: " + getCheckMessage(NoWhitespaceAfterCheck.class,
204                     NoWhitespaceAfterCheck.MSG_KEY, "int"),
205             "19:30: " + getCheckMessage(ConstantNameCheck.class, MSG_INVALID_PATTERN,
206                     "array", "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"),
207         };
208 
209         verifyFilterWithInlineConfigParser(getPath("UseCase3.java"), expectedWithoutFilters,
210                 expectedWithFilters);
211     }
212 
213     @Test
214     public void testUseCase4() throws Exception {
215         final String[] expectedWithFilter = {
216 
217         };
218         final String[] expectedWithoutFilter = {
219 
220         };
221 
222         verifyFilterWithInlineConfigParser(getPath("UseCase4.java"), expectedWithoutFilter,
223                 expectedWithFilter);
224     }
225 
226     @Test
227     public void testUseCase5() throws Exception {
228         final String[] expectedWithFilter = {
229 
230         };
231         final String[] expectedWithoutFilter = {
232             "21:1: " + getCheckMessage(ClassDataAbstractionCouplingCheck.class,
233                     ClassDataAbstractionCouplingCheck.MSG_KEY, 2, 1, "[Example1, Example2]"),
234             "24:23: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY,
235                     "10022"),
236         };
237 
238         verifyFilterWithInlineConfigParser(getPath("UseCase5.java"), expectedWithoutFilter,
239                 expectedWithFilter);
240     }
241 
242 }