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 SuppressionXpathFilterExamplesTest extends AbstractExamplesModuleTestSupport {
27  
28      @Override
29      protected String getPackageLocation() {
30          return "com/puppycrawl/tools/checkstyle/filters/suppressionxpathfilter";
31      }
32  
33      @Test
34      public void testExample1() throws Exception {
35  
36          final String[] expectedWithoutFilter = {
37              "21:3: Cyclomatic Complexity is 4 (max allowed is 3).",
38          };
39  
40          final String[] expectedWithFilter = {};
41  
42          System.setProperty("config.folder", "src/xdocs-examples/resources/"
43              + getPackageLocation());
44          verifyFilterWithInlineConfigParser(getPath("Example1.java"),
45                  expectedWithoutFilter,
46                  expectedWithFilter);
47      }
48  
49      @Test
50      public void testExample2() throws Exception {
51  
52          final String[] expectedWithoutFilter = {
53              "14:1: 'package' should be separated from previous line.",
54              "15:1: 'CLASS_DEF' should be separated from previous line.",
55          };
56  
57          final String[] expectedWithFilter = {
58              "15:1: 'CLASS_DEF' should be separated from previous line.",
59          };
60  
61          System.setProperty("config.folder", "src/xdocs-examples/resources/"
62              + getPackageLocation());
63          verifyFilterWithInlineConfigParser(getPath("Example2.java"),
64                  expectedWithoutFilter,
65                  expectedWithFilter);
66      }
67  
68      @Test
69      public void testExample3() throws Exception {
70  
71          final String[] expectedWithoutFilter = {
72              "21:31: '{' at column 31 should be on a new line.",
73              "26:31: '{' at column 31 should be on a new line.",
74          };
75  
76          final String[] expectedWithFilter = {
77              "26:31: '{' at column 31 should be on a new line.",
78          };
79  
80          System.setProperty("config.folder", "src/xdocs-examples/resources/"
81              + getPackageLocation());
82          verifyFilterWithInlineConfigParser(getPath("Example3.java"),
83                  expectedWithoutFilter,
84                  expectedWithFilter);
85      }
86  
87      @Test
88      public void testExample4() throws Exception {
89  
90          final String[] expectedWithoutFilter = {
91              "17:3: 'VARIABLE_DEF' should be separated from previous line.",
92              "18:3: 'METHOD_DEF' should be separated from previous line.",
93          };
94  
95          final String[] expectedWithFilter = {
96              "17:3: 'VARIABLE_DEF' should be separated from previous line.",
97          };
98  
99          System.setProperty("config.folder", "src/xdocs-examples/resources/"
100             + getPackageLocation());
101         verifyFilterWithInlineConfigParser(getPath("Example4.java"),
102                 expectedWithoutFilter,
103                 expectedWithFilter);
104     }
105 
106     @Test
107     public void testExample5() throws Exception {
108 
109         final String[] expectedWithoutFilter = {
110             "17:15: Name 'SetSomeVar' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
111             "19:15: Name 'TestMethod' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
112         };
113 
114         final String[] expectedWithFilter = {
115             "19:15: Name 'TestMethod' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
116         };
117 
118         System.setProperty("config.folder", "src/xdocs-examples/resources/"
119             + getPackageLocation());
120         verifyFilterWithInlineConfigParser(getPath("Example5.java"),
121                 expectedWithoutFilter,
122                 expectedWithFilter);
123     }
124 
125     @Test
126     public void testExample6() throws Exception {
127 
128         final String[] expectedWithoutFilter = {
129             "19:9: Name 'TestVariable' must match pattern '^([a-z][a-zA-Z0-9]*|_)$'.",
130             "21:9: Name 'WeirdName' must match pattern '^([a-z][a-zA-Z0-9]*|_)$'.",
131         };
132 
133         final String[] expectedWithFilter = {
134             "21:9: Name 'WeirdName' must match pattern '^([a-z][a-zA-Z0-9]*|_)$'.",
135         };
136 
137         System.setProperty("config.folder", "src/xdocs-examples/resources/"
138             + getPackageLocation());
139         verifyFilterWithInlineConfigParser(getPath("Example6.java"),
140                 expectedWithoutFilter,
141                 expectedWithFilter);
142     }
143 
144     @Test
145     public void testExample7() throws Exception {
146 
147         final String[] expectedWithoutFilter = {
148             "17:15: Name 'DoMATH' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
149             "18:15: Name 'DoEng' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
150             "22:19: '11' is a magic number.",
151             "23:8: Name 'FOO' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
152         };
153 
154         final String[] expectedWithFilter = {
155             "18:15: Name 'DoEng' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
156             "22:19: '11' is a magic number.",
157         };
158 
159         System.setProperty("config.folder", "src/xdocs-examples/resources/"
160             + getPackageLocation());
161         verifyFilterWithInlineConfigParser(getPath("Example7.java"),
162                 expectedWithoutFilter,
163                 expectedWithFilter);
164     }
165 
166     @Test
167     public void testExample8() throws Exception {
168 
169         final String[] expectedWithoutFilter = {
170             "22:5: Reference to instance variable 'age' needs \"this.\".",
171             "26:12: Reference to instance variable 'age' needs \"this.\".",
172         };
173 
174         final String[] expectedWithFilter = {
175             "26:12: Reference to instance variable 'age' needs \"this.\".",
176         };
177 
178         System.setProperty("config.folder", "src/xdocs-examples/resources/"
179             + getPackageLocation());
180         verifyFilterWithInlineConfigParser(getPath("Example8.java"),
181                 expectedWithoutFilter,
182                 expectedWithFilter);
183     }
184 
185     @Test
186     public void testExample9() throws Exception {
187 
188         final String[] expectedWithoutFilter = {
189             "17:37: Throwing 'RuntimeException' is not allowed.",
190             "21:37: Throwing 'RuntimeException' is not allowed.",
191         };
192 
193         final String[] expectedWithFilter = {
194             "21:37: Throwing 'RuntimeException' is not allowed.",
195         };
196 
197         System.setProperty("config.folder", "src/xdocs-examples/resources/"
198             + getPackageLocation());
199         verifyFilterWithInlineConfigParser(getPath("Example9.java"),
200                 expectedWithoutFilter,
201                 expectedWithFilter);
202     }
203 
204     @Test
205     public void testExample10() throws Exception {
206 
207         final String[] expectedWithoutFilter = {
208             "17:9: 'public' modifier out of order with the JLS suggestions.",
209             "18:14: 'abstract' modifier out of order with the JLS suggestions.",
210             "23:14: 'abstract' modifier out of order with the JLS suggestions.",
211         };
212 
213         final String[] expectedWithFilter = {
214             "23:14: 'abstract' modifier out of order with the JLS suggestions.",
215         };
216 
217         System.setProperty("config.folder", "src/xdocs-examples/resources/"
218             + getPackageLocation());
219         verifyFilterWithInlineConfigParser(getPath("Example10.java"),
220                 expectedWithoutFilter,
221                 expectedWithFilter);
222     }
223 
224     @Test
225     public void testExample11() throws Exception {
226 
227         final String[] expectedWithoutFilter = {
228             "16:27: '11' is a magic number.",
229         };
230 
231         final String[] expectedWithFilter = {};
232 
233         System.setProperty("config.folder", "src/xdocs-examples/resources/"
234             + getPackageLocation());
235         verifyFilterWithInlineConfigParser(getPath("Example11.java"),
236                 expectedWithoutFilter,
237                 expectedWithFilter);
238     }
239 
240     @Test
241     public void testExample12() throws Exception {
242 
243         final String[] expectedWithoutFilter = {
244             "16:27: '11' is a magic number.",
245         };
246 
247         final String[] expectedWithFilter = {};
248 
249         System.setProperty("config.folder", "src/xdocs-examples/resources/"
250             + getPackageLocation());
251         verifyFilterWithInlineConfigParser(getPath("Example12.java"),
252                 expectedWithoutFilter,
253                 expectedWithFilter);
254     }
255 
256     @Test
257     public void testExample13() throws Exception {
258 
259         final String[] expectedWithoutFilter = {
260             "18:15: Name 'Test1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
261             "22:15: Name 'Test2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
262         };
263 
264         final String[] expectedWithFilter = {};
265 
266         System.setProperty("config.folder", "src/xdocs-examples/resources/"
267             + getPackageLocation());
268         verifyFilterWithInlineConfigParser(getPath("Example13.java"),
269                 expectedWithoutFilter,
270                 expectedWithFilter);
271     }
272 
273     @Test
274     public void testExample14() throws Exception {
275 
276         final String[] expectedWithoutFilter = {
277             "18:15: Name 'Test1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
278             "22:15: Name 'Test2' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
279         };
280 
281         final String[] expectedWithFilter = {
282             "18:15: Name 'Test1' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
283         };
284 
285         System.setProperty("config.folder", "src/xdocs-examples/resources/"
286             + getPackageLocation());
287         verifyFilterWithInlineConfigParser(getPath("Example14.java"),
288                 expectedWithoutFilter,
289                 expectedWithFilter);
290     }
291 
292 }