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