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