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.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       * This tests that classes not extending anything explicitly will be correctly
41       * flagged for only including the inheritDoc tag.
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       * This tests that classes not extending anything explicitly will be correctly
60       * flagged for only including the inheritDoc tag even in Java 5 compatibility mode.
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       * This tests classes that are extending things explicitly will be correctly
78       * flagged for only including the inheritDoc tag.
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      * This tests classes that are extending things explicitly will NOT be flagged while in
101      * Java 5 compatibility mode.
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      * This tests anonymous inner classes that are overriding methods are correctly flagged
114      * for only including the inheritDoc tag.
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      * This tests anonymous inner classes that are overriding methods are NOT flagged while in
131      * Java 5 compatibility mode.
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      * Tests that inheritDoc misuse is properly flagged or missing Javadocs do not cause a problem.
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      * This tests that classes not extending anything explicitly will be correctly
157      * flagged for only including the inheritDoc tag.
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      * This tests that classes not extending anything explicitly will be correctly
170      * flagged for only including the inheritDoc tag even in Java 5 compatibility mode.
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      * This tests classes that are extending things explicitly will be correctly
183      * flagged for only including the inheritDoc tag.
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      * This tests classes that are extending things explicitly will NOT be flagged while in
195      * Java 5 compatibility mode.
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      * This tests anonymous inner classes that are overriding methods are correctly flagged
208      * for only including the inheritDoc tag.
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      * This tests anonymous inner classes that are overriding methods are NOT flagged while in
220      * Java 5 compatibility mode.
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 }