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 java.io.File;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck;
32  
33  public class XpathRegressionAnnotationUseStyleTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = AnnotationUseStyleCheck.class.getSimpleName();
36  
37      @Override
38      protected String getPackageLocation() {
39          return "org/checkstyle/suppressionxpathfilter/annotation/annotationusestyle";
40      }
41  
42      @Override
43      protected String getCheckName() {
44          return checkName;
45      }
46  
47      @Test
48      public void testOne() throws Exception {
49          final File fileToProcess = new File(getPath(
50                  "InputXpathAnnotationUseStyleOne.java"));
51  
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(AnnotationUseStyleCheck.class);
54  
55          final String[] expectedViolation = {
56              "4:1: " + getCheckMessage(AnnotationUseStyleCheck.class,
57                      AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_INCORRECT_STYLE,
58                      "COMPACT_NO_ARRAY"),
59          };
60  
61          final List<String> expectedXpathQueries = Arrays.asList(
62                  "/COMPILATION_UNIT/CLASS_DEF"
63                          + "[./IDENT[@text='InputXpathAnnotationUseStyleOne']]"
64                          + "/MODIFIERS/ANNOTATION[./IDENT[@text='SuppressWarnings']]",
65                  "/COMPILATION_UNIT/CLASS_DEF"
66                          + "[./IDENT[@text='InputXpathAnnotationUseStyleOne']]"
67                          + "/MODIFIERS/ANNOTATION[./IDENT[@text='SuppressWarnings']]/AT"
68          );
69  
70          runVerifications(moduleConfig, fileToProcess, expectedViolation,
71                  expectedXpathQueries);
72      }
73  
74      @Test
75      public void testTwo() throws Exception {
76          final File fileToProcess = new File(getPath(
77                  "InputXpathAnnotationUseStyleTwo.java"));
78  
79          final DefaultConfiguration moduleConfig =
80                  createModuleConfig(AnnotationUseStyleCheck.class);
81  
82          moduleConfig.addProperty("closingParens", "ALWAYS");
83          moduleConfig.addProperty("elementStyle", "ignore");
84          moduleConfig.addProperty("trailingArrayComma", "ignore");
85  
86          final String[] expectedViolation = {
87              "3:1: " + getCheckMessage(AnnotationUseStyleCheck.class,
88                      AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_PARENS_MISSING),
89          };
90  
91          final List<String> expectedXpathQueries = Arrays.asList(
92                  "/COMPILATION_UNIT/CLASS_DEF"
93                          + "[./IDENT[@text='InputXpathAnnotationUseStyleTwo']]",
94                  "/COMPILATION_UNIT/CLASS_DEF"
95                          + "[./IDENT[@text='InputXpathAnnotationUseStyleTwo']]"
96                          + "/MODIFIERS",
97                  "/COMPILATION_UNIT/CLASS_DEF"
98                          + "[./IDENT[@text='InputXpathAnnotationUseStyleTwo']]"
99                          + "/MODIFIERS/ANNOTATION[./IDENT[@text='Deprecated']]",
100                 "/COMPILATION_UNIT/CLASS_DEF"
101                         + "[./IDENT[@text='InputXpathAnnotationUseStyleTwo']]"
102                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='Deprecated']]/AT"
103         );
104 
105         runVerifications(moduleConfig, fileToProcess, expectedViolation,
106                 expectedXpathQueries);
107     }
108 
109     @Test
110     public void testThree() throws Exception {
111         final File fileToProcess = new File(getPath(
112                 "InputXpathAnnotationUseStyleThree.java"));
113 
114         final DefaultConfiguration moduleConfig =
115                 createModuleConfig(AnnotationUseStyleCheck.class);
116 
117         moduleConfig.addProperty("trailingArrayComma", "ignore");
118 
119         final String[] expectedViolation = {
120             "4:5: " + getCheckMessage(AnnotationUseStyleCheck.class,
121                     AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_INCORRECT_STYLE,
122                     "COMPACT_NO_ARRAY"),
123         };
124 
125         final List<String> expectedXpathQueries = Arrays.asList(
126                 "/COMPILATION_UNIT/CLASS_DEF"
127                         + "[./IDENT[@text='InputXpathAnnotationUseStyleThree']]"
128                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]",
129                 "/COMPILATION_UNIT/CLASS_DEF"
130                         + "[./IDENT[@text='InputXpathAnnotationUseStyleThree']]"
131                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/MODIFIERS",
132                 "/COMPILATION_UNIT/CLASS_DEF"
133                         + "[./IDENT[@text='InputXpathAnnotationUseStyleThree']]"
134                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/MODIFIERS"
135                         + "/ANNOTATION[./IDENT[@text='SuppressWarnings']]",
136                 "/COMPILATION_UNIT/CLASS_DEF"
137                         + "[./IDENT[@text='InputXpathAnnotationUseStyleThree']]"
138                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/MODIFIERS"
139                         + "/ANNOTATION[./IDENT[@text='SuppressWarnings']]/AT"
140         );
141 
142         runVerifications(moduleConfig, fileToProcess, expectedViolation,
143                 expectedXpathQueries);
144     }
145 
146     @Test
147     public void testFour() throws Exception {
148         final File fileToProcess = new File(getPath(
149                 "InputXpathAnnotationUseStyleFour.java"));
150 
151         final DefaultConfiguration moduleConfig =
152                 createModuleConfig(AnnotationUseStyleCheck.class);
153 
154         moduleConfig.addProperty("closingParens", "ignore");
155         moduleConfig.addProperty("elementStyle", "ignore");
156         moduleConfig.addProperty("trailingArrayComma", "ALWAYS");
157 
158         final String[] expectedViolation = {
159             "3:20: " + getCheckMessage(AnnotationUseStyleCheck.class,
160                     AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
161         };
162 
163         final List<String> expectedXpathQueries = Collections.singletonList(
164                 "/COMPILATION_UNIT/CLASS_DEF"
165                         + "[./IDENT[@text='InputXpathAnnotationUseStyleFour']]"
166                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='SuppressWarnings']]"
167                         + "/ANNOTATION_ARRAY_INIT/RCURLY"
168         );
169 
170         runVerifications(moduleConfig, fileToProcess, expectedViolation,
171                 expectedXpathQueries);
172     }
173 
174     @Test
175     public void testFive() throws Exception {
176         final File fileToProcess = new File(getPath(
177                 "InputXpathAnnotationUseStyleFive.java"));
178 
179         final DefaultConfiguration moduleConfig =
180                 createModuleConfig(AnnotationUseStyleCheck.class);
181 
182         moduleConfig.addProperty("closingParens", "ignore");
183         moduleConfig.addProperty("elementStyle", "COMPACT");
184         moduleConfig.addProperty("trailingArrayComma", "ignore");
185 
186         final String[] expectedViolation = {
187             "3:1: " + getCheckMessage(AnnotationUseStyleCheck.class,
188                     AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_INCORRECT_STYLE,
189                     "COMPACT"),
190         };
191 
192         final List<String> expectedXpathQueries = Arrays.asList(
193                 "/COMPILATION_UNIT/CLASS_DEF"
194                         + "[./IDENT[@text='InputXpathAnnotationUseStyleFive']]",
195                 "/COMPILATION_UNIT/CLASS_DEF"
196                         + "[./IDENT[@text='InputXpathAnnotationUseStyleFive']]"
197                          + "/MODIFIERS",
198                 "/COMPILATION_UNIT/CLASS_DEF"
199                         + "[./IDENT[@text='InputXpathAnnotationUseStyleFive']]"
200                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='SuppressWarnings']]",
201                 "/COMPILATION_UNIT/CLASS_DEF"
202                         + "[./IDENT[@text='InputXpathAnnotationUseStyleFive']]"
203                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='SuppressWarnings']]/AT"
204         );
205 
206         runVerifications(moduleConfig, fileToProcess, expectedViolation,
207                 expectedXpathQueries);
208     }
209 
210     @Test
211     public void testSix() throws Exception {
212         final File fileToProcess = new File(getPath(
213                 "InputXpathAnnotationUseStyleSix.java"));
214 
215         final DefaultConfiguration moduleConfig =
216                 createModuleConfig(AnnotationUseStyleCheck.class);
217 
218         moduleConfig.addProperty("closingParens", "ignore");
219         moduleConfig.addProperty("elementStyle", "EXPANDED");
220         moduleConfig.addProperty("trailingArrayComma", "ignore");
221 
222         final String[] expectedViolation = {
223             "3:1: " + getCheckMessage(AnnotationUseStyleCheck.class,
224                     AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_INCORRECT_STYLE,
225                     "EXPANDED"),
226         };
227 
228         final List<String> expectedXpathQueries = Arrays.asList(
229                 "/COMPILATION_UNIT/CLASS_DEF"
230                         + "[./IDENT[@text='InputXpathAnnotationUseStyleSix']]",
231                 "/COMPILATION_UNIT/CLASS_DEF"
232                         + "[./IDENT[@text='InputXpathAnnotationUseStyleSix']]"
233                         + "/MODIFIERS",
234                 "/COMPILATION_UNIT/CLASS_DEF"
235                         + "[./IDENT[@text='InputXpathAnnotationUseStyleSix']]"
236                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='SuppressWarnings']]",
237                 "/COMPILATION_UNIT/CLASS_DEF"
238                         + "[./IDENT[@text='InputXpathAnnotationUseStyleSix']]"
239                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='SuppressWarnings']]/AT"
240         );
241 
242         runVerifications(moduleConfig, fileToProcess, expectedViolation,
243                 expectedXpathQueries);
244     }
245 
246     @Test
247     public void testSeven() throws Exception {
248         final File fileToProcess = new File(getPath(
249                 "InputXpathAnnotationUseStyleSeven.java"));
250 
251         final DefaultConfiguration moduleConfig =
252                 createModuleConfig(AnnotationUseStyleCheck.class);
253 
254         final String[] expectedViolation = {
255             "4:1: " + getCheckMessage(AnnotationUseStyleCheck.class,
256                     AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_INCORRECT_STYLE,
257                     "COMPACT_NO_ARRAY"),
258         };
259 
260         final List<String> expectedXpathQueries = Arrays.asList(
261                 "/COMPILATION_UNIT/CLASS_DEF"
262                         + "[./IDENT[@text='InputXpathAnnotationUseStyleSeven']]"
263                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='SuppressWarnings']]",
264                 "/COMPILATION_UNIT/CLASS_DEF"
265                         + "[./IDENT[@text='InputXpathAnnotationUseStyleSeven']]"
266                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='SuppressWarnings']]/AT"
267         );
268 
269         runVerifications(moduleConfig, fileToProcess, expectedViolation,
270                 expectedXpathQueries);
271     }
272 
273     @Test
274     public void testEight() throws Exception {
275         final File fileToProcess = new File(getPath(
276                 "InputXpathAnnotationUseStyleEight.java"));
277 
278         final DefaultConfiguration moduleConfig =
279                 createModuleConfig(AnnotationUseStyleCheck.class);
280 
281         moduleConfig.addProperty("closingParens", "ignore");
282         moduleConfig.addProperty("elementStyle", "ignore");
283         moduleConfig.addProperty("trailingArrayComma", "NEVER");
284 
285         final String[] expectedViolation = {
286             "3:31: " + getCheckMessage(AnnotationUseStyleCheck.class,
287                     AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
288         };
289 
290         final List<String> expectedXpathQueries = Collections.singletonList(
291                 "/COMPILATION_UNIT/CLASS_DEF"
292                         + "[./IDENT[@text='InputXpathAnnotationUseStyleEight']]"
293                         + "/MODIFIERS/ANNOTATION[./IDENT[@text='SuppressWarnings']]"
294                         + "/ANNOTATION_ARRAY_INIT/COMMA"
295         );
296 
297         runVerifications(moduleConfig, fileToProcess, expectedViolation,
298                 expectedXpathQueries);
299     }
300 }