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 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.AnnotationOnSameLineCheck.MSG_KEY_ANNOTATION_ON_SAME_LINE;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class AnnotationOnSameLineCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/annotation/annotationonsameline";
36      }
37  
38      @Test
39      public void testGetRequiredTokens() {
40          final AnnotationOnSameLineCheck check = new AnnotationOnSameLineCheck();
41          assertWithMessage(
42                  "AnnotationOnSameLineCheck#getRequiredTokens should return empty array by default")
43                          .that(check.getRequiredTokens())
44                          .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
45      }
46  
47      @Test
48      public void testGetAcceptableTokens() {
49          final AnnotationOnSameLineCheck constantNameCheckObj = new AnnotationOnSameLineCheck();
50          final int[] actual = constantNameCheckObj.getAcceptableTokens();
51          final int[] expected = {
52              TokenTypes.CLASS_DEF,
53              TokenTypes.INTERFACE_DEF,
54              TokenTypes.ENUM_DEF,
55              TokenTypes.METHOD_DEF,
56              TokenTypes.CTOR_DEF,
57              TokenTypes.VARIABLE_DEF,
58              TokenTypes.PARAMETER_DEF,
59              TokenTypes.ANNOTATION_DEF,
60              TokenTypes.TYPECAST,
61              TokenTypes.LITERAL_THROWS,
62              TokenTypes.IMPLEMENTS_CLAUSE,
63              TokenTypes.TYPE_ARGUMENT,
64              TokenTypes.LITERAL_NEW,
65              TokenTypes.DOT,
66              TokenTypes.ANNOTATION_FIELD_DEF,
67              TokenTypes.RECORD_DEF,
68              TokenTypes.COMPACT_CTOR_DEF,
69          };
70          assertWithMessage("Default acceptable tokens are invalid")
71                  .that(actual)
72                  .isEqualTo(expected);
73      }
74  
75      @Test
76      public void testAnnotationOnSameLineCheckPublicMethodAndVariable() throws Exception {
77          final String[] expected = {
78              "20:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
79              "21:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
80              "22:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
81              "30:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation2"),
82              "30:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
83          };
84          verifyWithInlineConfigParser(
85                  getPath("InputAnnotationOnSameLineCheckPublicMethodAndVariable.java"), expected);
86      }
87  
88      @Test
89      public void testAnnotationOnSameLineCheckTokensOnMethodAndVar() throws Exception {
90          final String[] expected = {
91              "20:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation3"),
92              "21:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
93              "22:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
94              "30:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation4"),
95              "30:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation3"),
96          };
97          verifyWithInlineConfigParser(
98                  getPath("InputAnnotationOnSameLineCheckTokensOnMethodAndVar.java"), expected);
99      }
100 
101     @Test
102     public void testAnnotationOnSameLineCheckPrivateAndDeprecatedVar() throws Exception {
103         final String[] expected = {
104             "19:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
105             "25:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
106             "29:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
107             "30:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
108         };
109         verifyWithInlineConfigParser(
110                 getPath("InputAnnotationOnSameLineCheckPrivateAndDeprecatedVar.java"), expected);
111     }
112 
113     @Test
114     public void testAnnotationOnSameLineCheckInterfaceAndEnum() throws Exception {
115         final String[] expected = {
116             "14:1: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
117             "17:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
118             "21:8: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
119             "23:71: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
120             "26:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
121             "27:35: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
122             "31:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
123             "34:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
124             "39:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
125             "43:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
126             "45:28: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
127             "47:33: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
128             "50:15: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
129             "56:17: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
130             "65:1: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
131             "68:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
132         };
133         verifyWithInlineConfigParser(
134                 getPath("InputAnnotationOnSameLineCheckInterfaceAndEnum.java"), expected);
135     }
136 
137     @Test
138     public void testAnnotationOnSameLineRecordsAndCompactCtors() throws Exception {
139         final String[] expected = {
140             "13:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "NonNull1"),
141             "18:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
142             "24:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
143             "29:9: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
144             "35:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
145             "41:13: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "NonNull1"),
146             "41:23: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
147             "51:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
148             "58:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
149         };
150         verifyWithInlineConfigParser(
151                 getNonCompilablePath("InputAnnotationOnSameLineRecordsAndCompactCtors.java"),
152             expected);
153     }
154 
155     @Test
156     public void testAnnotationOnSameLinePatternVariables() throws Exception {
157         final String[] expected = {
158             "37:8: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
159         };
160         verifyWithInlineConfigParser(
161                 getNonCompilablePath("InputAnnotationOnSameLinePatternVariables.java"),
162             expected);
163     }
164 
165     @Test
166     public void testAnnotationOnSameLine() throws Exception {
167         final String[] expected = {
168             "19:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
169             "23:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
170             "36:14: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
171             "43:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
172             "43:17: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
173         };
174         verifyWithInlineConfigParser(
175                 getPath("InputAnnotationOnSameLine.java"), expected);
176     }
177 
178     @Test
179     public void testAnnotationOnSameLine2() throws Exception {
180         final String[] expected = {
181             "18:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
182             "22:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
183             "26:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
184             "30:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
185         };
186         verifyWithInlineConfigParser(
187                 getNonCompilablePath("InputAnnotationOnSameLine.java"),
188             expected);
189     }
190 }