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.blocks.LeftCurlyCheck.MSG_KEY_LINE_PREVIOUS;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.MSG_VARIABLE;
24  import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.stream.Stream;
35  
36  import org.junit.jupiter.api.Test;
37  
38  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
39  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
40  import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser;
41  import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration;
42  import com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck;
43  import com.puppycrawl.tools.checkstyle.checks.coding.IllegalThrowsCheck;
44  import com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck;
45  import com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck;
46  import com.puppycrawl.tools.checkstyle.checks.modifier.RedundantModifierCheck;
47  import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
48  
49  public class SuppressionXpathSingleFilterExamplesTest extends AbstractExamplesModuleTestSupport {
50      @Override
51      protected String getPackageLocation() {
52          return "com/puppycrawl/tools/checkstyle/filters/suppressionxpathsinglefilter";
53      }
54  
55      @Test
56      public void testExample1() throws Exception {
57          final String[] expectedWithoutFilter = {
58              "21:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
59              "MyMethod", "^[a-z][a-zA-Z0-9]*$"),
60          };
61          final String[] expectedWithFilter = {};
62  
63          verifyFilterWithInlineConfigParser(getPath("Example1.java"),
64                  expectedWithoutFilter, expectedWithFilter);
65      }
66  
67      @Test
68      public void testExample2() throws Exception {
69          final String[] expectedWithoutFilter = {
70              "18:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
71              "MyMethod1", "^[a-z][a-zA-Z0-9]*$"),
72              "20:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
73              "MyMethod2", "^[a-z][a-zA-Z0-9]*$"),
74              "22:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
75              "MyMethodA", "^[a-z][a-zA-Z0-9]*$"),
76          };
77          final String[] expectedWithFilter = {
78              "22:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
79              "MyMethodA", "^[a-z][a-zA-Z0-9]*$"),
80          };
81  
82          verifyFilterWithInlineConfigParser(getPath("Example2.java"),
83                  expectedWithoutFilter, expectedWithFilter);
84      }
85  
86      @Test
87      public void testExample3() throws Exception {
88          final String[] expectedWithoutFilter = {
89              "20:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
90              "MyMethod", "^[a-z][a-zA-Z0-9]*$"),
91          };
92          final String[] expectedWithFilter = {};
93  
94          verifyFilterWithInlineConfigParser(getPath("Example3.java"),
95                  expectedWithoutFilter, expectedWithFilter);
96      }
97  
98      @Test
99      public void testExample4() throws Exception {
100         final String[] expectedWithoutFilter = {
101             "19:9: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
102             "com.puppycrawl.tools.checkstyle.filters.suppressionxpathsinglefilter",
103                     "^[A-Z]+(\\.[A-Z]*)*$"),
104         };
105         final String[] expectedWithFilter = {};
106 
107         verifyFilterWithInlineConfigParser(getPath("Example4.java"),
108                 expectedWithoutFilter, expectedWithFilter);
109     }
110 
111     @Test
112     public void testExample5() throws Exception {
113         final String[] expectedWithoutFilter = {
114             "17:3: " + getCheckMessage(RedundantModifierCheck.class,
115                     RedundantModifierCheck.MSG_KEY, "public"),
116         };
117         final String[] expectedWithFilter = {};
118 
119         verifyFilterWithInlineConfigParser(getPath("Example5.java"),
120                 expectedWithoutFilter, expectedWithFilter);
121     }
122 
123     @Test
124     public void testExample6() throws Exception {
125         final String[] expectedWithoutFilter = {
126             "17:23: " + getCheckMessage(MagicNumberCheck.class, MagicNumberCheck.MSG_KEY,
127                 "177"),
128         };
129         final String[] expectedWithFilter = {};
130 
131         verifyFilterWithInlineConfigParser(getPath("Example6.java"),
132                 expectedWithoutFilter, expectedWithFilter);
133     }
134 
135     @Test
136     public void testExample7() throws Exception {
137         final String[] expectedWithoutFilter = {
138             "21:1: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
139             "Example7", "^Abstract.+$"),
140             "22:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
141             "MyMethod", "^[a-z][a-zA-Z0-9]*$"),
142             "27:1: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
143             "AnotherClass", "^Abstract.+$"),
144             "28:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
145             "MyMethod", "^[a-z][a-zA-Z0-9]*$"),
146         };
147         final String[] expectedWithFilter = {
148             "27:1: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
149             "AnotherClass", "^Abstract.+$"),
150             "28:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
151             "MyMethod", "^[a-z][a-zA-Z0-9]*$"),
152         };
153 
154         verifyFilterWithInlineConfigParser(getPath("Example7.java"),
155                 expectedWithoutFilter, expectedWithFilter);
156     }
157 
158     @Test
159     public void testExample8() throws Exception {
160         final String[] expectedWithoutFilter = {
161             "19:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
162             "MyMethod1", "^[a-z][a-zA-Z0-9]*$"),
163             "21:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
164             "MyMethod2", "^[a-z][a-zA-Z0-9]*$"),
165             "23:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
166             "MyMethod3", "^[a-z][a-zA-Z0-9]*$"),
167         };
168         final String[] expectedWithFilter = {
169             "23:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
170             "MyMethod3", "^[a-z][a-zA-Z0-9]*$"),
171         };
172 
173         verifyFilterWithInlineConfigParser(getPath("Example8.java"),
174                 expectedWithoutFilter, expectedWithFilter);
175     }
176 
177     @Test
178     public void testExample9() throws Exception {
179         final String[] expectedWithoutFilter = {
180             "23:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
181             "testVariable1", "^[A-Z][A-Z0-9]*$"),
182             "25:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
183             "testVariable2", "^[A-Z][A-Z0-9]*$"),
184         };
185         final String[] expectedWithFilter = {
186             "25:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
187             "testVariable2", "^[A-Z][A-Z0-9]*$"),
188         };
189 
190         verifyFilterWithInlineConfigParser(getPath("Example9.java"),
191                 expectedWithoutFilter, expectedWithFilter);
192     }
193 
194     @Test
195     public void testExample10() throws Exception {
196         final String[] expectedWithoutFilter = {
197             "19:3: " + getCheckMessage(LeftCurlyCheck.class, MSG_KEY_LINE_PREVIOUS,
198             "{", 3),
199             "23:3: " + getCheckMessage(LeftCurlyCheck.class, MSG_KEY_LINE_PREVIOUS,
200             "{", 3),
201         };
202         final String[] expectedWithFilter = {
203             "23:3: " + getCheckMessage(LeftCurlyCheck.class, MSG_KEY_LINE_PREVIOUS,
204             "{", 3),
205         };
206 
207         verifyFilterWithInlineConfigParser(getPath("Example10.java"),
208                 expectedWithoutFilter, expectedWithFilter);
209     }
210 
211     @Test
212     public void testExample11() throws Exception {
213         final String[] expectedWithoutFilter = {
214             "25:5: " + getCheckMessage(RequireThisCheck.class, MSG_VARIABLE,
215                     "age", ""),
216             "29:5: " + getCheckMessage(RequireThisCheck.class, MSG_VARIABLE,
217                     "number", ""),
218         };
219         final String[] expectedWithFilter = {
220             "29:5: " + getCheckMessage(RequireThisCheck.class, MSG_VARIABLE,
221                     "number", ""),
222         };
223 
224         verifyFilterWithInlineConfigParser(getPath("Example11.java"),
225                 expectedWithoutFilter, expectedWithFilter);
226     }
227 
228     @Test
229     public void testExample12() throws Exception {
230         final String[] expectedWithoutFilter = {
231             "19:37: " + getCheckMessage(IllegalThrowsCheck.class, IllegalThrowsCheck.MSG_KEY,
232                     "RuntimeException"),
233             "23:37: " + getCheckMessage(IllegalThrowsCheck.class, IllegalThrowsCheck.MSG_KEY,
234                     "RuntimeException"),
235         };
236         final String[] expectedWithFilter = {
237             "23:37: " + getCheckMessage(IllegalThrowsCheck.class, IllegalThrowsCheck.MSG_KEY,
238                     "RuntimeException"),
239         };
240 
241         verifyFilterWithInlineConfigParser(getPath("Example12.java"),
242                 expectedWithoutFilter, expectedWithFilter);
243     }
244 
245     @Test
246     public void testExample13() throws Exception {
247         final String[] expectedWithoutFilter = {
248             "24:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
249                     "TestMethod1", "^[a-z](_?[a-zA-Z0-9]+)*$"),
250             "26:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
251                     "num", "^[A-Z][A-Z0-9]*$"),
252             "30:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
253                     "TestMethod2", "^[a-z](_?[a-zA-Z0-9]+)*$"),
254             "32:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
255                     "num", "^[A-Z][A-Z0-9]*$"),
256         };
257         final String[] expectedWithFilter = {
258             "30:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
259                     "TestMethod2", "^[a-z](_?[a-zA-Z0-9]+)*$"),
260             "32:15: " + getCheckMessage(AbstractNameCheck.class, MSG_INVALID_PATTERN,
261                     "num", "^[A-Z][A-Z0-9]*$"),
262         };
263 
264         verifyFilterWithInlineConfigParser(getPath("Example13.java"),
265                 expectedWithoutFilter, expectedWithFilter);
266     }
267 
268     @Test
269     public void testExample14() throws Exception {
270         final Map<String, List<String>> expected = new HashMap<>();
271         final List<String> messages =
272                 List.of("3:14: " + getCheckMessage(AbstractNameCheck.class,
273                             MSG_INVALID_PATTERN, "myApplication",
274                             "^[A-Z][a-zA-Z0-9]*$"));
275         expected.put(getPath("src/myApplication.java"), messages);
276 
277         final Path path = Paths.get("src/xdocs-examples/resources/" + getPackageLocation() + "/");
278 
279         final String fileWithConfig = getPath("Example14.java");
280         final TestInputConfiguration testInputConfiguration =
281                 InlineConfigParser.parse(fileWithConfig);
282         final DefaultConfiguration parsedConfig =
283                 testInputConfiguration.createConfiguration();
284         verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
285     }
286 
287     private static File[] getFilesInFolder(Path path) throws IOException {
288         try (Stream<Path> stream = Files.walk(path.toAbsolutePath())) {
289             return stream.filter(Files::isRegularFile)
290                     .map(Path::toFile)
291                     .toArray(File[]::new);
292         }
293     }
294 }