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 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.OpenjdkAnnotationLocationCheck.MSG_KEY_ANNOTATION_ALONE_OR_SAME;
24  import static com.puppycrawl.tools.checkstyle.checks.annotation.OpenjdkAnnotationLocationCheck.MSG_KEY_ANNOTATION_ON_TARGET_LINE;
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 OpenjdkAnnotationLocationCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      public String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/annotation/openjdkannotationlocation";
37      }
38  
39      @Test
40      public void testGetRequiredTokens() {
41          final OpenjdkAnnotationLocationCheck obj =
42              new OpenjdkAnnotationLocationCheck();
43          assertWithMessage(
44            "OpenjdkAnnotationLocation#getRequiredTokens should return empty array by default")
45                  .that(obj.getRequiredTokens())
46                  .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
47      }
48  
49      @Test
50      public void testGetAcceptableTokens() {
51          final OpenjdkAnnotationLocationCheck obj =
52                  new OpenjdkAnnotationLocationCheck();
53          final int[] actual = obj.getAcceptableTokens();
54          final int[] expected = {
55              TokenTypes.CLASS_DEF,
56              TokenTypes.INTERFACE_DEF,
57              TokenTypes.PACKAGE_DEF,
58              TokenTypes.ENUM_CONSTANT_DEF,
59              TokenTypes.ENUM_DEF,
60              TokenTypes.METHOD_DEF,
61              TokenTypes.CTOR_DEF,
62              TokenTypes.VARIABLE_DEF,
63              TokenTypes.ANNOTATION_DEF,
64              TokenTypes.ANNOTATION_FIELD_DEF,
65              TokenTypes.RECORD_DEF,
66              TokenTypes.COMPACT_CTOR_DEF,
67              TokenTypes.MODULE_DEF,
68          };
69          assertWithMessage("Default acceptable tokens are invalid")
70                  .that(actual)
71                  .isEqualTo(expected);
72      }
73  
74      @Test
75      public void testAnnotation1() throws Exception {
76          final String[] expected = {
77              "16:21: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
78                      "InputOpenjdkAnnotationLocation"),
79              "16:21: " + getCheckMessage(MSG_KEY_ANNOTATION_ALONE_OR_SAME,
80                      "InputOpenjdkAnnotationLocation"),
81              "22:25: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "valueBad"),
82              "42:25: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "valueBadTwo"),
83              "42:25: " + getCheckMessage(MSG_KEY_ANNOTATION_ALONE_OR_SAME, "valueBadTwo"),
84          };
85  
86          verifyWithInlineConfigParser(
87                  getPath("InputOpenjdkAnnotationLocation.java"), expected);
88      }
89  
90      @Test
91      public void testAnnotation2() throws Exception {
92          final String[] expected = {
93              "26:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ALONE_OR_SAME, "method4"),
94              "44:1: " + getCheckMessage(MSG_KEY_ANNOTATION_ALONE_OR_SAME, "TestClassBad"),
95              "49:21: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "a"),
96              "49:21: " + getCheckMessage(MSG_KEY_ANNOTATION_ALONE_OR_SAME, "a"),
97              "65:21: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "e"),
98              "69:21: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
99                      "methodNotSingleLineBad"),
100             "84:25: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
101                     "multilineLocalVariable"),
102         };
103 
104         verifyWithInlineConfigParser(
105                 getPath("InputOpenjdkAnnotationLocation2.java"), expected);
106     }
107 
108     @Test
109     public void testAnnotation3() throws Exception {
110         final String[] expected = {
111             "16:24: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
112                     "InputOpenjdkAnnotationLocation3"),
113             "16:24: " + getCheckMessage(MSG_KEY_ANNOTATION_ALONE_OR_SAME,
114                     "InputOpenjdkAnnotationLocation3"),
115             "22:28: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "ENUM_VALUE_BAD"),
116         };
117 
118         verifyWithInlineConfigParser(
119                 getPath("InputOpenjdkAnnotationLocation3.java"), expected);
120     }
121 
122     @Test
123     public void testAnnotation4() throws Exception {
124         final String[] expected = {
125             "20:17: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "method"),
126             "27:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ALONE_OR_SAME, "badMethod"),
127             "42:21: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "temp1"),
128         };
129 
130         verifyWithInlineConfigParser(
131                 getPath("InputOpenjdkAnnotationLocation4.java"), expected);
132     }
133 
134     @Test
135     public void testAnnotationCompact() throws Exception {
136         final String[] expected = {
137             "22:29: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "badField6"),
138             "32:13: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "badField7"),
139             "37:17: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "local1"),
140             "54:1: " + getCheckMessage(MSG_KEY_ANNOTATION_ALONE_OR_SAME, "helperMethodThree"),
141         };
142 
143         verifyWithInlineConfigParser(
144                 getNonCompilablePath(
145                         "compact/InputOpenjdkAnnotationLocationCompactSourceFile.java"), expected);
146     }
147 
148     @Test
149     public void testAnnotation5() throws Exception {
150         final String[] expected = {
151             "12:84: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
152                     "InputOpenjdkAnnotationLocation5"),
153         };
154 
155         verifyWithInlineConfigParser(
156                 getPath("InputOpenjdkAnnotationLocation5.java"), expected);
157     }
158 
159     @Test
160     public void testSingleLineTypesAndConstructor() throws Exception {
161         final String[] expected = {
162             "20:46: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "MultilineC"),
163             "27:48: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "MultilineRat"),
164             "39:38: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
165                     "GenericMethods2"),
166             "46:34: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
167                     "MultilineInterface"),
168             "52:34: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
169                     "MultilineEnum"),
170             "59:38: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
171                     "VALUE_WITH_BODY"),
172             "66:34: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
173                     "MultilineRecord"),
174             "75:38: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE,
175                     "MultilineCompactConstructor"),
176         };
177 
178         verifyWithInlineConfigParser(
179                 getPath("InputOpenjdkAnnotationLocation6.java"), expected);
180     }
181 
182     @Test
183     public void testPackage() throws Exception {
184         final String[] expected = {
185             "10:84: " + getCheckMessage(MSG_KEY_ANNOTATION_ALONE_OR_SAME, "inputs"),
186         };
187         verifyWithInlineConfigParser(
188                 getPath("inputs/package-info.java"), expected);
189     }
190 
191     @Test
192     public void testModuleAnnotationOnTargetLine() throws Exception {
193         final String[] expected = {
194             "12:13: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "app"),
195         };
196         verifyWithInlineConfigParser(
197                 getNonCompilablePath(
198                         "module-info/annotation-on-target-line/module-info.java"), expected);
199     }
200 
201     @Test
202     public void testModuleMixedAnnotations() throws Exception {
203         final String[] expected = {
204             "10:10: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_TARGET_LINE, "app2"),
205             "10:10: " + getCheckMessage(MSG_KEY_ANNOTATION_ALONE_OR_SAME, "app2"),
206         };
207         verifyWithInlineConfigParser(
208                 getNonCompilablePath("module-info/mixed-annotations/module-info.java"), expected);
209     }
210 
211     @Test
212     public void testOpenModule() throws Exception {
213         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
214         verifyWithInlineConfigParser(
215                 getNonCompilablePath("module-info/open/module-info.java"), expected);
216     }
217 
218     @Test
219     public void testSingleLinePackage() throws Exception {
220         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
221         verifyWithInlineConfigParser(
222                 getPath("inputs/singleline/package-info.java"), expected);
223     }
224 
225 }