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