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 org.junit.jupiter.api.Test;
23  
24  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
25  
26  public class SuppressionCommentFilterExamplesTest extends AbstractExamplesModuleTestSupport {
27      @Override
28      protected String getPackageLocation() {
29          return "com/puppycrawl/tools/checkstyle/filters/suppressioncommentfilter";
30      }
31  
32      @Test
33      public void testExample1() throws Exception {
34          final String[] expectedWithoutFilter = {
35              "16:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
36              "19:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
37              "22:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
38              "26:27: Name 'var4' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
39              "32:5: Catching 'Exception' is not allowed.",
40              "37:5: Catching 'Exception' is not allowed.",
41              "39:5: Catching 'Error' is not allowed.",
42          };
43  
44          final String[] expectedWithFilter = {
45              "16:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
46              "22:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
47              "32:5: Catching 'Exception' is not allowed.",
48          };
49  
50          verifyFilterWithInlineConfigParser(getPath("Example1.java"),
51                  expectedWithoutFilter, expectedWithFilter);
52      }
53  
54      @Test
55      public void testExample2() throws Exception {
56          final String[] expectedWithoutFilter = {
57              "21:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
58              "24:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
59              "27:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
60              "31:27: Name 'var4' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
61              "37:5: Catching 'Exception' is not allowed.",
62              "42:5: Catching 'Exception' is not allowed.",
63              "43:5: Catching 'Error' is not allowed.",
64          };
65  
66          final String[] expectedWithFilter = {
67              "21:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
68              "24:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
69              "27:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
70              "37:5: Catching 'Exception' is not allowed.",
71              "42:5: Catching 'Exception' is not allowed.",
72              "43:5: Catching 'Error' is not allowed.",
73          };
74  
75          verifyFilterWithInlineConfigParser(getPath("Example2.java"),
76                  expectedWithoutFilter, expectedWithFilter);
77      }
78  
79      @Test
80      public void testExample3() throws Exception {
81          final String[] expectedWithoutFilter = {
82              "21:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
83              "24:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
84              "27:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
85              "31:27: Name 'var4' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
86              "38:5: Catching 'Exception' is not allowed.",
87              "43:5: Catching 'Exception' is not allowed.",
88              "45:5: Catching 'Error' is not allowed.",
89          };
90  
91          final String[] expectedWithFilter = {
92              "21:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
93              "24:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
94              "27:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
95              "31:27: Name 'var4' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
96              "38:5: Catching 'Exception' is not allowed.",
97              "45:5: Catching 'Error' is not allowed.",
98          };
99  
100         verifyFilterWithInlineConfigParser(getPath("Example3.java"),
101                 expectedWithoutFilter, expectedWithFilter);
102     }
103 
104     @Test
105     public void testExample4() throws Exception {
106         final String[] expectedWithoutFilter = {
107             "20:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
108             "23:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
109             "26:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
110             "30:27: Name 'var4' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
111             "36:5: Catching 'Exception' is not allowed.",
112             "41:5: Catching 'Exception' is not allowed.",
113             "43:5: Catching 'Error' is not allowed.",
114         };
115 
116         final String[] expectedWithFilter = {
117             "20:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
118             "26:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
119             "36:5: Catching 'Exception' is not allowed.",
120         };
121 
122         verifyFilterWithInlineConfigParser(getPath("Example4.java"),
123                 expectedWithoutFilter, expectedWithFilter);
124     }
125 
126     @Test
127     public void testExample5() throws Exception {
128         final String[] expectedWithoutFilter = {
129             "20:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
130             "23:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
131             "26:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
132             "30:27: Name 'var4' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
133             "37:5: Catching 'Exception' is not allowed.",
134             "42:5: Catching 'Exception' is not allowed.",
135             "44:5: Catching 'Error' is not allowed.",
136         };
137 
138         final String[] expectedWithFilter = {
139             "20:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
140             "26:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
141             "30:27: Name 'var4' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
142             "37:5: Catching 'Exception' is not allowed.",
143         };
144 
145         verifyFilterWithInlineConfigParser(getPath("Example5.java"),
146                 expectedWithoutFilter, expectedWithFilter);
147     }
148 
149     @Test
150     public void testExample6() throws Exception {
151         final String[] expectedWithoutFilter = {
152             "25:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
153             "28:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
154             "31:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
155             "35:27: Name 'var4' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
156             "41:5: Catching 'Exception' is not allowed.",
157             "46:5: Catching 'Exception' is not allowed.",
158             "48:5: Catching 'Error' is not allowed.",
159         };
160 
161         final String[] expectedWithFilter = {
162             "25:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
163             "31:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
164             "41:5: Catching 'Exception' is not allowed.",
165         };
166 
167         verifyFilterWithInlineConfigParser(getPath("Example6.java"),
168                 expectedWithoutFilter, expectedWithFilter);
169     }
170 
171     @Test
172     public void testExample7() throws Exception {
173         final String[] expectedWithoutFilter = {
174             "19:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
175             "22:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
176             "25:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
177             "31:27: Name 'var4' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
178             "36:5: Catching 'Exception' is not allowed.",
179             "40:5: Catching 'Exception' is not allowed.",
180             "42:5: Catching 'Error' is not allowed.",
181         };
182 
183         final String[] expectedWithFilter = {
184             "19:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
185             "25:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
186         };
187 
188         verifyFilterWithInlineConfigParser(getPath("Example7.java"),
189                 expectedWithoutFilter, expectedWithFilter);
190     }
191 
192     @Test
193     public void testExample8() throws Exception {
194         final String[] expectedWithoutFilter = {
195             "18:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
196             "21:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
197             "24:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
198             "28:27: Name 'var4' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
199             "34:5: Catching 'Exception' is not allowed.",
200             "39:5: Catching 'Exception' is not allowed.",
201             "40:5: Catching 'Error' is not allowed.",
202         };
203 
204         final String[] expectedWithFilter = {
205             "18:7: Name 'VAR1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
206             "21:7: Name 'VAR2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
207             "24:27: Name 'var3' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.",
208             "34:5: Catching 'Exception' is not allowed.",
209             "39:5: Catching 'Exception' is not allowed.",
210             "40:5: Catching 'Error' is not allowed.",
211         };
212 
213         verifyFilterWithInlineConfigParser(getPath("Example8.java"),
214                 expectedWithoutFilter, expectedWithFilter);
215     }
216 }