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 com.puppycrawl.tools.checkstyle.checks.annotation;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION;
24  import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION_ALONE;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class AnnotationLocationCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation";
37      }
38  
39      @Test
40      public void testGetRequiredTokens() {
41          final AnnotationLocationCheck checkObj = new AnnotationLocationCheck();
42          assertWithMessage(
43                  "AnnotationLocationCheck#getRequiredTokens should return empty array by default")
44                          .that(checkObj.getRequiredTokens())
45                          .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
46      }
47  
48      @Test
49      public void testCorrect() throws Exception {
50          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
51  
52          verifyWithInlineConfigParser(
53                  getPath("InputAnnotationLocationCorrect.java"), expected);
54      }
55  
56      @Test
57      public void testIncorrectOne() throws Exception {
58          final String[] expected = {
59              "15:11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnn"),
60              "20:15: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
61              "23:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
62              "26:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
63              "34:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
64              "38:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
65              "38:27: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnn_21"),
66              "41:8: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_21", 7, 4),
67              "45:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_21", 8, 4),
68              "46:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation3", 6, 4),
69              "47:11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation4", 10, 4),
70              "50:19: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
71              "54:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
72              "57:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 12, 8),
73              "65:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
74              "70:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_21", 12, 8),
75              "74:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_21", 12, 8),
76              "79:8: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_21", 7, 4),
77              "82:19: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
78              "84:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
79              "94:12: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_21", 11, 8),
80              "97:11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_21", 10, 8),
81              "99:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
82          };
83          verifyWithInlineConfigParser(
84                  getPath("InputAnnotationLocationIncorrectOne.java"), expected);
85      }
86  
87      @Test
88      public void testIncorrectTwo() throws Exception {
89          final String[] expected = {
90              "17:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_21", 0, 3),
91          };
92          verifyWithInlineConfigParser(
93                  getPath("InputAnnotationLocationIncorrectTwo.java"), expected);
94      }
95  
96      @Test
97      public void testIncorrectAllTokensOne() throws Exception {
98          final String[] expected = {
99              "15:11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnn3"),
100             "20:15: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation_13"),
101             "23:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation_13"),
102             "26:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation_13", 8, 4),
103             "34:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation_13", 8, 4),
104             "38:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation_13"),
105             "38:29: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnn_23"),
106             "41:8: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_23", 7, 4),
107             "45:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_23", 8, 4),
108             "46:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation_33", 6, 4),
109             "47:11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation_43", 10, 4),
110             "50:19: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation_13"),
111             "54:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation_13"),
112             "57:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation_13", 12, 8),
113             "65:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation_13"),
114             "70:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_23", 12, 8),
115             "74:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_23", 12, 8),
116             "79:8: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_23", 7, 4),
117             "83:19: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation_13"),
118             "85:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation_13"),
119             "95:12: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_23", 11, 8),
120             "98:11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_23", 10, 8),
121             "101:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation_13"),
122         };
123         verifyWithInlineConfigParser(
124                 getPath("InputAnnotationLocationIncorrect3One.java"), expected);
125     }
126 
127     @Test
128     public void testIncorrectAllTokensTwo() throws Exception {
129         final String[] expected = {
130             "17:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_23", 0, 3),
131         };
132         verifyWithInlineConfigParser(
133                 getPath("InputAnnotationLocationIncorrect3Two.java"), expected);
134     }
135 
136     @Test
137     public void testGetAcceptableTokens() {
138         final AnnotationLocationCheck constantNameCheckObj = new AnnotationLocationCheck();
139         final int[] actual = constantNameCheckObj.getAcceptableTokens();
140         final int[] expected = {
141             TokenTypes.CLASS_DEF,
142             TokenTypes.INTERFACE_DEF,
143             TokenTypes.PACKAGE_DEF,
144             TokenTypes.ENUM_CONSTANT_DEF,
145             TokenTypes.ENUM_DEF,
146             TokenTypes.METHOD_DEF,
147             TokenTypes.CTOR_DEF,
148             TokenTypes.VARIABLE_DEF,
149             TokenTypes.ANNOTATION_DEF,
150             TokenTypes.ANNOTATION_FIELD_DEF,
151             TokenTypes.RECORD_DEF,
152             TokenTypes.COMPACT_CTOR_DEF,
153         };
154         assertWithMessage("Default acceptable tokens are invalid")
155                 .that(actual)
156                 .isEqualTo(expected);
157     }
158 
159     @Test
160     public void testWithoutAnnotations() throws Exception {
161         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
162         verifyWithInlineConfigParser(
163                 getPath("InputAnnotationLocationEmpty.java"), expected);
164     }
165 
166     @Test
167     public void testWithParametersOne() throws Exception {
168         final String[] expected = {
169             "25:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation_12", 8, 4),
170             "33:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation_12", 8, 4),
171             "40:8: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_22", 7, 4),
172             "44:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_22", 8, 4),
173             "45:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation_32", 6, 4),
174             "46:11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation_42", 10, 4),
175             "56:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation_12", 12, 8),
176             "69:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_22", 12, 8),
177             "73:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_22", 12, 8),
178             "78:8: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_22", 7, 4),
179             "93:12: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_22", 11, 8),
180             "96:11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_22", 10, 8),
181         };
182         verifyWithInlineConfigParser(
183                 getPath("InputAnnotationLocationIncorrect2One.java"), expected);
184     }
185 
186     @Test
187     public void testWithParametersTwo() throws Exception {
188         final String[] expected = {
189             "17:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_22", 0, 3),
190         };
191         verifyWithInlineConfigParser(
192                 getPath("InputAnnotationLocationIncorrect2Two.java"), expected);
193     }
194 
195     @Test
196     public void testWithMultipleAnnotations() throws Exception {
197         final String[] expected = {
198             "14:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation11"),
199             "14:17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation12"),
200             "14:33: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation13"),
201         };
202         verifyWithInlineConfigParser(
203                 getPath("InputAnnotationLocationCustomAnnotationsDeclared.java"),
204                 expected);
205     }
206 
207     @Test
208     public void testAllTokens() throws Exception {
209         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
210         verifyWithInlineConfigParser(
211                 getPath("InputAnnotationLocationWithoutAnnotations.java"), expected);
212     }
213 
214     @Test
215     public void testAnnotation() throws Exception {
216         final String[] expected = {
217             "18:3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "AnnotationAnnotation", 2, 0),
218             "20:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "AnnotationAnnotation"),
219             "23:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "AnnotationAnnotation", 6, 4),
220             "24:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "AnnotationAnnotation"),
221         };
222         verifyWithInlineConfigParser(
223                 getPath("InputAnnotationLocationAnnotation.java"), expected);
224     }
225 
226     @Test
227     public void testClass() throws Exception {
228         final String[] expected = {
229             "18:3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "ClassAnnotation", 2, 0),
230             "20:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "ClassAnnotation"),
231             "23:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "ClassAnnotation", 6, 4),
232             "24:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "ClassAnnotation"),
233             "27:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "ClassAnnotation", 6, 4),
234             "29:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "ClassAnnotation"),
235         };
236         verifyWithInlineConfigParser(
237                 getPath("InputAnnotationLocationClass.java"), expected);
238     }
239 
240     @Test
241     public void testEnum() throws Exception {
242         final String[] expected = {
243             "18:3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "EnumAnnotation", 2, 0),
244             "19:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "EnumAnnotation"),
245             "22:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "EnumAnnotation", 6, 4),
246             "23:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "EnumAnnotation"),
247         };
248         verifyWithInlineConfigParser(
249                 getPath("InputAnnotationLocationEnum.java"), expected);
250     }
251 
252     @Test
253     public void testInterface() throws Exception {
254         final String[] expected = {
255             "18:3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "InterfaceAnnotation", 2, 0),
256             "20:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "InterfaceAnnotation"),
257             "23:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "InterfaceAnnotation", 6, 4),
258             "24:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "InterfaceAnnotation"),
259         };
260         verifyWithInlineConfigParser(
261                 getPath("InputAnnotationLocationInterface.java"), expected);
262     }
263 
264     @Test
265     public void testPackage() throws Exception {
266         final String[] expected = {
267             "12:3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "PackageAnnotation", 2, 0),
268             "14:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "PackageAnnotation"),
269         };
270         verifyWithInlineConfigParser(
271                 getPath("inputs/package-info.java"), expected);
272     }
273 
274     @Test
275     public void testAnnotationInForEachLoopParameterAndVariableDef() throws Exception {
276         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
277         verifyWithInlineConfigParser(
278                 getPath("InputAnnotationLocationDeprecatedAndCustom.java"), expected);
279     }
280 
281     @Test
282     public void testAnnotationMultiple() throws Exception {
283         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
284         verifyWithInlineConfigParser(
285                 getPath("InputAnnotationLocationMultiple.java"), expected);
286     }
287 
288     @Test
289     public void testAnnotationParameterized() throws Exception {
290         final String[] expected = {
291             "18:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
292             "20:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
293             "20:17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
294             "26:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
295             "26:17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
296             "26:33: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
297             "29:21: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
298         };
299         verifyWithInlineConfigParser(
300                 getPath("InputAnnotationLocationParameterized.java"), expected);
301     }
302 
303     @Test
304     public void testAnnotationSingleParameterless() throws Exception {
305         final String[] expected = {
306             "23:17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
307             "25:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
308             "27:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
309             "29:17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
310             "29:33: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
311             "31:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
312             "31:21: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
313         };
314         verifyWithInlineConfigParser(
315                 getPath("InputAnnotationLocationSingleParameterless.java"), expected);
316     }
317 
318     @Test
319     public void testAnnotationLocationRecordsAndCompactCtors() throws Exception {
320         final String[] expected = {
321             "20:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
322             "24:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
323             "28:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
324             "30:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
325             "37:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
326             "49:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
327             "59:34: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
328         };
329         verifyWithInlineConfigParser(
330                 getNonCompilablePath("InputAnnotationLocationRecordsAndCompactCtors.java"),
331             expected);
332     }
333 
334     @Test
335     public void testAnnotationLocationOnLocalAndPatternVariables() throws Exception {
336         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
337         verifyWithInlineConfigParser(
338                 getNonCompilablePath("InputAnnotationLocationLocalAndPatternVariables.java"),
339             expected);
340     }
341 
342 }