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.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              "17:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
79              "18:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
80              "19:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
81              "24:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
82          };
83          verifyWithInlineConfigParser(
84                  getPath("InputAnnotationOnSameLineCheckPublicMethodAndVariable.java"), expected);
85      }
86  
87      @Test
88      public void testAnnotationOnSameLineCheckTokensOnMethodAndVar() throws Exception {
89          final String[] expected = {
90              "18:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation3"),
91              "19:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
92              "20:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
93              "25:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation3"),
94          };
95          verifyWithInlineConfigParser(
96                  getPath("InputAnnotationOnSameLineCheckTokensOnMethodAndVar.java"), expected);
97      }
98  
99      @Test
100     public void testAnnotationOnSameLineCheckPrivateAndDeprecatedVar() throws Exception {
101         final String[] expected = {
102             "19:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
103             "24:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
104             "27:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
105             "28:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
106         };
107         verifyWithInlineConfigParser(
108                 getPath("InputAnnotationOnSameLineCheckPrivateAndDeprecatedVar.java"), expected);
109     }
110 
111     @Test
112     public void testAnnotationOnSameLineCheckInterfaceAndEnum() throws Exception {
113         final String[] expected = {
114             "14:1: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
115             "17:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
116             "21:8: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
117             "22:71: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
118             "25:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
119             "26:35: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
120             "29:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
121             "32:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
122             "37:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
123             "40:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
124             "41:28: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
125             "42:33: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
126             "44:15: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
127             "50:17: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
128             "59:1: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
129             "62:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
130         };
131         verifyWithInlineConfigParser(
132                 getPath("InputAnnotationOnSameLineCheckInterfaceAndEnum.java"), expected);
133     }
134 
135     @Test
136     public void testAnnotationOnSameLineRecordsAndCompactCtors() throws Exception {
137         final String[] expected = {
138             "13:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "NonNull1"),
139             "17:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
140             "22:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
141             "27:9: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
142             "32:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
143             "35:27: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
144             "44:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
145             "50:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
146         };
147         verifyWithInlineConfigParser(
148                 getNonCompilablePath("InputAnnotationOnSameLineRecordsAndCompactCtors.java"),
149             expected);
150     }
151 
152 }