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 org.checkstyle.suppressionxpathfilter;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.annotation.SuppressWarningsCheck.MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED;
23  
24  import java.io.File;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.checks.annotation.SuppressWarningsCheck;
32  
33  public class XpathRegressionSuppressWarningsTest extends AbstractXpathTestSupport {
34      private final String checkName = SuppressWarningsCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Test
42      public void testClassDefinition() throws Exception {
43          final File fileToCheck =
44                  new File(getPath("InputXpathSuppressWarningsClassDefinition.java"));
45  
46          final DefaultConfiguration configuration =
47                  createModuleConfig(SuppressWarningsCheck.class);
48  
49          final String[] expectedViolations = {
50              "3:19: " + getCheckMessage(SuppressWarningsCheck.class,
51                      MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED,
52                      ""),
53          };
54  
55          final List<String> expectedXpathQueries = Arrays.asList(
56                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
57                  + "[@text='InputXpathSuppressWarningsClassDefinition']]"
58                  + "/MODIFIERS/ANNOTATION[./IDENT"
59                  + "[@text='SuppressWarnings']]"
60                  + "/EXPR[./STRING_LITERAL[@text='']]",
61  
62                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
63                  + "[@text='InputXpathSuppressWarningsClassDefinition']]"
64                  + "/MODIFIERS/ANNOTATION[./IDENT"
65                  + "[@text='SuppressWarnings']]"
66                  + "/EXPR/STRING_LITERAL[@text='']"
67          );
68  
69          runVerifications(configuration, fileToCheck, expectedViolations, expectedXpathQueries);
70      }
71  
72      @Test
73      public void testParameterDefinition() throws Exception {
74          final File fileToCheck =
75                  new File(getPath("InputXpathSuppressWarningsParameterDefinition.java"));
76  
77          final DefaultConfiguration configuration =
78                  createModuleConfig(SuppressWarningsCheck.class);
79  
80          final String[] expectedViolations = {
81              "5:76: " + getCheckMessage(SuppressWarningsCheck.class,
82                      MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED,
83                      ""),
84          };
85  
86          final List<String> expectedXpathQueries = Arrays.asList(
87                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
88                  + "[@text='InputXpathSuppressWarningsParameterDefinition']]"
89                  + "/OBJBLOCK/CTOR_DEF[./IDENT"
90                  + "[@text='InputXpathSuppressWarningsParameterDefinition']]"
91                  + "/PARAMETERS/PARAMETER_DEF[./IDENT"
92                  + "[@text='bar']]"
93                  + "/MODIFIERS/ANNOTATION[./IDENT"
94                  + "[@text='SuppressWarnings']]"
95                  + "/EXPR[./STRING_LITERAL[@text='']]",
96  
97                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
98                  + "[@text='InputXpathSuppressWarningsParameterDefinition']]"
99                  + "/OBJBLOCK/CTOR_DEF[./IDENT"
100                 + "[@text='InputXpathSuppressWarningsParameterDefinition']]"
101                 + "/PARAMETERS/PARAMETER_DEF[./IDENT"
102                 + "[@text='bar']]"
103                 + "/MODIFIERS/ANNOTATION[./IDENT"
104                 + "[@text='SuppressWarnings']]"
105                 + "/EXPR/STRING_LITERAL[@text='']"
106         );
107 
108         runVerifications(configuration, fileToCheck, expectedViolations, expectedXpathQueries);
109     }
110 
111     @Test
112     public void testVariableDefinition() throws Exception {
113         final File fileToCheck =
114                 new File(getPath("InputXpathSuppressWarningsVariableDefinition.java"));
115 
116         final DefaultConfiguration configuration =
117                 createModuleConfig(SuppressWarningsCheck.class);
118 
119         final String[] expectedViolations = {
120             "4:27: " + getCheckMessage(SuppressWarningsCheck.class,
121                     MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED,
122                     ""),
123         };
124 
125         final List<String> expectedXpathQueries = Arrays.asList(
126                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
127                 + "[@text='InputXpathSuppressWarningsVariableDefinition']]"
128                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT"
129                 + "[@text='foo']]"
130                 + "/MODIFIERS/ANNOTATION[./IDENT"
131                 + "[@text='SuppressWarnings']]"
132                 + "/EXPR[./STRING_LITERAL[@text='']]",
133 
134                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
135                 + "[@text='InputXpathSuppressWarningsVariableDefinition']]"
136                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT"
137                 + "[@text='foo']]"
138                 + "/MODIFIERS/ANNOTATION[./IDENT"
139                 + "[@text='SuppressWarnings']]"
140                 + "/EXPR/STRING_LITERAL[@text='']"
141         );
142 
143         runVerifications(configuration, fileToCheck, expectedViolations, expectedXpathQueries);
144     }
145 
146     @Test
147     public void testEnumDefinition() throws Exception {
148         final File fileToCheck =
149                 new File(getPath("InputXpathSuppressWarningsEnumDefinition.java"));
150 
151         final DefaultConfiguration configuration =
152                 createModuleConfig(SuppressWarningsCheck.class);
153 
154         final String[] expectedViolations = {
155             "3:19: " + getCheckMessage(SuppressWarningsCheck.class,
156                     MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED,
157                     ""),
158         };
159 
160         final List<String> expectedXpathQueries = Arrays.asList(
161                 "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
162                 + "[@text='InputXpathSuppressWarningsEnumDefinition']]"
163                 + "/MODIFIERS/ANNOTATION[./IDENT"
164                 + "[@text='SuppressWarnings']]"
165                 + "/EXPR[./STRING_LITERAL[@text='']]",
166 
167                 "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
168                 + "[@text='InputXpathSuppressWarningsEnumDefinition']]"
169                 + "/MODIFIERS/ANNOTATION[./IDENT"
170                 + "[@text='SuppressWarnings']]"
171                 + "/EXPR/STRING_LITERAL[@text='']"
172         );
173 
174         runVerifications(configuration, fileToCheck, expectedViolations, expectedXpathQueries);
175     }
176 
177     @Test
178     public void testInterfaceDefinition() throws Exception {
179         final File fileToCheck =
180                 new File(getPath("InputXpathSuppressWarningsInterfaceDefinition.java"));
181 
182         final DefaultConfiguration configuration =
183                 createModuleConfig(SuppressWarningsCheck.class);
184 
185         final String[] expectedViolations = {
186             "3:19: " + getCheckMessage(SuppressWarningsCheck.class,
187                     MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED,
188                     ""),
189         };
190 
191         final List<String> expectedXpathQueries = Arrays.asList(
192                 "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
193                 + "[@text='InputXpathSuppressWarningsInterfaceDefinition']]"
194                 + "/MODIFIERS/ANNOTATION[./IDENT"
195                 + "[@text='SuppressWarnings']]"
196                 + "/EXPR[./STRING_LITERAL[@text='']]",
197 
198                 "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
199                 + "[@text='InputXpathSuppressWarningsInterfaceDefinition']]"
200                 + "/MODIFIERS/ANNOTATION[./IDENT"
201                 + "[@text='SuppressWarnings']]"
202                 + "/EXPR/STRING_LITERAL[@text='']"
203         );
204 
205         runVerifications(configuration, fileToCheck, expectedViolations, expectedXpathQueries);
206     }
207 
208     @Test
209     public void testEnumConstantDefinition() throws Exception {
210         final File fileToCheck =
211                 new File(getPath("InputXpathSuppressWarningsEnumConstantDefinition.java"));
212 
213         final DefaultConfiguration configuration =
214                 createModuleConfig(SuppressWarningsCheck.class);
215 
216         final String[] expectedViolations = {
217             "4:27: " + getCheckMessage(SuppressWarningsCheck.class,
218                     MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED,
219                     ""),
220         };
221 
222         final List<String> expectedXpathQueries = Arrays.asList(
223                 "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
224                 + "[@text='InputXpathSuppressWarningsEnumConstantDefinition']]"
225                 + "/OBJBLOCK/ENUM_CONSTANT_DEF[./IDENT"
226                 + "[@text='FOO']]"
227                 + "/ANNOTATIONS/ANNOTATION[./IDENT"
228                 + "[@text='SuppressWarnings']]"
229                 + "/EXPR[./STRING_LITERAL[@text='']]",
230 
231                 "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
232                 + "[@text='InputXpathSuppressWarningsEnumConstantDefinition']]"
233                 + "/OBJBLOCK/ENUM_CONSTANT_DEF[./IDENT"
234                 + "[@text='FOO']]"
235                 + "/ANNOTATIONS/ANNOTATION[./IDENT"
236                 + "[@text='SuppressWarnings']]"
237                 + "/EXPR/STRING_LITERAL[@text='']"
238         );
239 
240         runVerifications(configuration, fileToCheck, expectedViolations, expectedXpathQueries);
241     }
242 
243     @Test
244     public void testMethodDefinition() throws Exception {
245         final File fileToCheck =
246                 new File(getPath("InputXpathSuppressWarningsMethodDefinition.java"));
247 
248         final DefaultConfiguration configuration =
249                 createModuleConfig(SuppressWarningsCheck.class);
250 
251         final String[] expectedViolations = {
252             "10:23: " + getCheckMessage(SuppressWarningsCheck.class,
253                     MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED,
254                     ""),
255         };
256 
257         final List<String> expectedXpathQueries = Arrays.asList(
258                "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
259                + "[@text='InputXpathSuppressWarningsMethodDefinition']]"
260                + "/OBJBLOCK/METHOD_DEF[./IDENT"
261                + "[@text='getFoo']]"
262                + "/MODIFIERS/ANNOTATION[./IDENT"
263                + "[@text='SuppressWarnings']]"
264                + "/EXPR[./STRING_LITERAL[@text='']]",
265 
266                "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
267                + "[@text='InputXpathSuppressWarningsMethodDefinition']]"
268                + "/OBJBLOCK/METHOD_DEF[./IDENT"
269                + "[@text='getFoo']]"
270                + "/MODIFIERS/ANNOTATION[./IDENT"
271                + "[@text='SuppressWarnings']]"
272                + "/EXPR/STRING_LITERAL[@text='']"
273         );
274 
275         runVerifications(configuration, fileToCheck, expectedViolations, expectedXpathQueries);
276     }
277 
278     @Test
279     public void testAnnotationDefinition() throws Exception {
280         final File fileToCheck =
281                 new File(getPath("InputXpathSuppressWarningsAnnotationDefinition.java"));
282 
283         final DefaultConfiguration configuration =
284                 createModuleConfig(SuppressWarningsCheck.class);
285 
286         final String[] expectedViolations = {
287             "3:19: " + getCheckMessage(SuppressWarningsCheck.class,
288                     MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED,
289                     ""),
290         };
291 
292         final List<String> expectedXpathQueries = Arrays.asList(
293                 "/COMPILATION_UNIT/ANNOTATION_DEF[./IDENT"
294                 + "[@text='InputXpathSuppressWarningsAnnotationDefinition']]"
295                 + "/MODIFIERS/ANNOTATION[./IDENT"
296                 + "[@text='SuppressWarnings']]"
297                 + "/EXPR[./STRING_LITERAL[@text='']]",
298 
299                 "/COMPILATION_UNIT/ANNOTATION_DEF[./IDENT"
300                 + "[@text='InputXpathSuppressWarningsAnnotationDefinition']]"
301                 + "/MODIFIERS/ANNOTATION[./IDENT"
302                 + "[@text='SuppressWarnings']]"
303                 + "/EXPR/STRING_LITERAL[@text='']"
304         );
305 
306         runVerifications(configuration, fileToCheck, expectedViolations, expectedXpathQueries);
307     }
308 
309     @Test
310     public void testAnnotationFieldDefinition() throws Exception {
311         final File fileToCheck =
312                 new File(getPath("InputXpathSuppressWarningsAnnotationFieldDefinition.java"));
313 
314         final DefaultConfiguration configuration =
315                 createModuleConfig(SuppressWarningsCheck.class);
316 
317         final String[] expectedViolations = {
318             "4:27: " + getCheckMessage(SuppressWarningsCheck.class,
319                     MSG_KEY_SUPPRESSED_WARNING_NOT_ALLOWED,
320                     ""),
321         };
322 
323         final List<String> expectedXpathQueries = Arrays.asList(
324                 "/COMPILATION_UNIT/ANNOTATION_DEF[./IDENT"
325                 + "[@text='InputXpathSuppressWarningsAnnotationFieldDefinition']]"
326                 + "/OBJBLOCK/ANNOTATION_FIELD_DEF[./IDENT"
327                 + "[@text='foo']]"
328                 + "/MODIFIERS/ANNOTATION[./IDENT"
329                 + "[@text='SuppressWarnings']]"
330                 + "/EXPR[./STRING_LITERAL[@text='']]",
331 
332                 "/COMPILATION_UNIT/ANNOTATION_DEF[./IDENT"
333                 + "[@text='InputXpathSuppressWarningsAnnotationFieldDefinition']]"
334                 + "/OBJBLOCK/ANNOTATION_FIELD_DEF[./IDENT"
335                 + "[@text='foo']]"
336                 + "/MODIFIERS/ANNOTATION[./IDENT"
337                 + "[@text='SuppressWarnings']]"
338                 + "/EXPR/STRING_LITERAL[@text='']"
339         );
340 
341         runVerifications(configuration, fileToCheck, expectedViolations, expectedXpathQueries);
342     }
343 }