1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 "28: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 "20:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation3"),
91 "21:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
92 "22:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
93 "28: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 "25:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
104 "29:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
105 "30: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 "23:71: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
118 "26:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
119 "27:35: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
120 "31:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
121 "34:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
122 "39:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
123 "43:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
124 "45:28: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
125 "47:33: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
126 "50:15: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
127 "56:17: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
128 "65:1: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
129 "68: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 "18:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
140 "24:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
141 "29:9: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
142 "35:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
143 "38:27: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
144 "49:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
145 "56:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
146 };
147 verifyWithInlineConfigParser(
148 getNonCompilablePath("InputAnnotationOnSameLineRecordsAndCompactCtors.java"),
149 expected);
150 }
151
152 @Test
153 public void testAnnotationOnSameLinePatternVariables() throws Exception {
154 final String[] expected = {
155 "37:8: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
156 };
157 verifyWithInlineConfigParser(
158 getNonCompilablePath("InputAnnotationOnSameLinePatternVariables.java"),
159 expected);
160 }
161
162 }