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.MissingOverrideCheck.MSG_KEY_ANNOTATION_MISSING_OVERRIDE;
24 import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck.MSG_KEY_TAG_NOT_VALID_ON;
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 MissingOverrideCheckTest extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/annotation/missingoverride";
37 }
38
39
40
41
42
43 @Test
44 public void testBadOverrideFromObject() throws Exception {
45
46 final String[] expected = {
47 "15:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
48 "36:9: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
49 "46:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
50 "51:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
51 "74:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
52 };
53
54 verifyWithInlineConfigParser(
55 getPath("InputMissingOverrideBadOverrideFromObject.java"), expected);
56 }
57
58
59
60
61
62 @Test
63 public void testBadOverrideFromObjectJ5Compatible() throws Exception {
64
65 final String[] expected = {
66 "15:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
67 "36:9: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
68 "46:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
69 "55:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
70 };
71
72 verifyWithInlineConfigParser(
73 getPath("InputMissingOverrideBadOverrideFromObjectJava5.java"), expected);
74 }
75
76
77
78
79
80 @Test
81 public void testBadOverrideFromOther() throws Exception {
82 final String[] expected = {
83 "17:3: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
84 "33:3: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
85 "41:3: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
86 "46:3: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
87 "53:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
88 "58:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
89 "68:3: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
90 "73:3: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
91 "79:3: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
92 "85:3: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
93 };
94
95 verifyWithInlineConfigParser(
96 getPath("InputMissingOverrideBadOverrideFromOther.java"), expected);
97 }
98
99
100
101
102
103 @Test
104 public void testBadOverrideFromOtherJ5Compatible() throws Exception {
105
106 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
107
108 verifyWithInlineConfigParser(
109 getPath("InputMissingOverrideBadOverrideFromOtherJava5.java"), expected);
110 }
111
112
113
114
115
116 @Test
117 public void testBadAnnotationOverride() throws Exception {
118 final String[] expected = {
119 "17:5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
120 "23:9: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
121 "36:7: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
122 "42:11: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
123 };
124
125 verifyWithInlineConfigParser(
126 getPath("InputMissingOverrideBadAnnotation.java"), expected);
127 }
128
129
130
131
132
133 @Test
134 public void testBadAnnotationOverrideJ5Compatible() throws Exception {
135 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
136
137 verifyWithInlineConfigParser(
138 getPath("InputMissingOverrideBadAnnotationJava5.java"), expected);
139 }
140
141
142
143
144 @Test
145 public void testNotOverride() throws Exception {
146 final String[] expected = {
147 "15:3: " + getCheckMessage(MSG_KEY_TAG_NOT_VALID_ON, "{@inheritDoc}"),
148 "20:3: " + getCheckMessage(MSG_KEY_TAG_NOT_VALID_ON, "{@inheritDoc}"),
149 };
150
151 verifyWithInlineConfigParser(
152 getPath("InputMissingOverrideNotOverride.java"), expected);
153 }
154
155
156
157
158
159 @Test
160 public void testGoodOverrideFromObject() throws Exception {
161
162 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
163
164 verifyWithInlineConfigParser(
165 getPath("InputMissingOverrideGoodOverrideFromObject.java"), expected);
166 }
167
168
169
170
171
172 @Test
173 public void testGoodOverrideFromObjectJ5Compatible() throws Exception {
174
175 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
176
177 verifyWithInlineConfigParser(
178 getPath("InputMissingOverrideGoodOverrideFromObjectJava5.java"), expected);
179 }
180
181
182
183
184
185 @Test
186 public void testGoodOverrideFromOther() throws Exception {
187 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
188
189 verifyWithInlineConfigParser(
190 getPath("InputMissingOverrideGoodOverrideFromOther.java"), expected);
191 }
192
193
194
195
196
197 @Test
198 public void testGoodOverrideFromOtherJ5Compatible() throws Exception {
199
200 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
201
202 verifyWithInlineConfigParser(
203 getPath("InputMissingOverrideGoodOverrideFromOtherJava5.java"), expected);
204 }
205
206
207
208
209
210 @Test
211 public void testGoodAnnotationOverride() throws Exception {
212 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
213
214 verifyWithInlineConfigParser(
215 getPath("InputMissingOverrideGoodOverride.java"), expected);
216 }
217
218
219
220
221
222 @Test
223 public void testGoodAnnotationOverrideJ5Compatible() throws Exception {
224 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
225
226 verifyWithInlineConfigParser(
227 getPath("InputMissingOverrideGoodOverrideJava5.java"), expected);
228 }
229
230 @Test
231 public void testGetAcceptableTokens() {
232 final int[] expectedTokens = {TokenTypes.METHOD_DEF };
233 final MissingOverrideCheck check = new MissingOverrideCheck();
234 final int[] actual = check.getAcceptableTokens();
235 assertWithMessage("Invalid acceptable token size")
236 .that(actual.length)
237 .isEqualTo(1);
238 assertWithMessage("Default required tokens are invalid")
239 .that(actual)
240 .isEqualTo(expectedTokens);
241 }
242
243 }