View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck.MSG_KEY_ANNOTATION_MISSING_OVERRIDE;
23  import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck.MSG_KEY_TAG_NOT_VALID_ON;
24  import static org.junit.Assert.assertEquals;
25  
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
33  
34  public class MissingOverrideCheckTest extends AbstractModuleTestSupport {
35  
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/annotation/missingoverride";
39      }
40  
41      /**
42       * This tests that classes not extending anything explicitly will be correctly
43       * flagged for only including the inheritDoc tag.
44       */
45      @Test
46      public void testBadOverrideFromObject() throws Exception {
47          final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
48          checkConfig.addAttribute("javaFiveCompatibility", "false");
49  
50          final String[] expected = {
51              "8: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
52              "30: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
53              "41: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
54              "50: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
55          };
56  
57          verify(checkConfig, getPath("InputMissingOverrideBadOverrideFromObject.java"), expected);
58      }
59  
60      /**
61       * This tests that classes not extending anything explicitly will be correctly
62       * flagged for only including the inheritDoc tag even in Java 5 compatibility mode.
63       */
64      @Test
65      public void testBadOverrideFromObjectJ5Compatible() throws Exception {
66          final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
67          checkConfig.addAttribute("javaFiveCompatibility", "true");
68  
69          final String[] expected = {
70              "8: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
71              "30: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
72              "41: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
73              "50: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
74          };
75  
76          verify(checkConfig, getPath("InputMissingOverrideBadOverrideFromObject.java"), expected);
77      }
78  
79      /**
80       * This tests classes that are extending things explicitly will be correctly
81       * flagged for only including the inheritDoc tag.
82       */
83      @Test
84      public void testBadOverrideFromOther() throws Exception {
85          final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
86          final String[] expected = {
87              "10: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
88              "26: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
89              "34: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
90              "40: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
91              "47: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
92              "53: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
93              "63: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
94          };
95  
96          verify(checkConfig, 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         final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
106         checkConfig.addAttribute("javaFiveCompatibility", "true");
107 
108         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
109 
110         verify(checkConfig, getPath("InputMissingOverrideBadOverrideFromOther.java"), expected);
111     }
112 
113     /**
114      * This tests anonymous inner classes that are overriding methods are correctly flagged
115      * for only including the inheritDoc tag.
116      */
117     @Test
118     public void testBadAnnotationOverride() throws Exception {
119         final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
120         final String[] expected = {
121             "10: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
122             "16: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
123             "29: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
124             "35: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
125         };
126 
127         verify(checkConfig, getPath("InputMissingOverrideBadAnnotation.java"), expected);
128     }
129 
130     /**
131      * This tests anonymous inner classes that are overriding methods are NOT flagged while in
132      * Java 5 compatibility mode.
133      */
134     @Test
135     public void testBadAnnotationOverrideJ5Compatible() throws Exception {
136         final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
137         checkConfig.addAttribute("javaFiveCompatibility", "true");
138         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
139 
140         verify(checkConfig, getPath("InputMissingOverrideBadAnnotation.java"), expected);
141     }
142 
143     /**
144      * Tests that inheritDoc misuse is properly flagged or missing Javadocs do not cause a problem.
145      */
146     @Test
147     public void testNotOverride() throws Exception {
148         final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
149         final String[] expected = {
150             "8: " + getCheckMessage(MSG_KEY_TAG_NOT_VALID_ON, "{@inheritDoc}"),
151             "15: " + getCheckMessage(MSG_KEY_TAG_NOT_VALID_ON, "{@inheritDoc}"),
152         };
153 
154         verify(checkConfig, getPath("InputMissingOverrideNotOverride.java"), expected);
155     }
156 
157     /**
158      * This tests that classes not extending anything explicitly will be correctly
159      * flagged for only including the inheritDoc tag.
160      */
161     @Test
162     public void testGoodOverrideFromObject() throws Exception {
163         final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
164         checkConfig.addAttribute("javaFiveCompatibility", "false");
165 
166         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
167 
168         verify(checkConfig, getPath("InputMissingOverrideGoodOverrideFromObject.java"), expected);
169     }
170 
171     /**
172      * This tests that classes not extending anything explicitly will be correctly
173      * flagged for only including the inheritDoc tag even in Java 5 compatibility mode.
174      */
175     @Test
176     public void testGoodOverrideFromObjectJ5Compatible() throws Exception {
177         final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
178         checkConfig.addAttribute("javaFiveCompatibility", "true");
179 
180         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
181 
182         verify(checkConfig, getPath("InputMissingOverrideGoodOverrideFromObject.java"), expected);
183     }
184 
185     /**
186      * This tests classes that are extending things explicitly will be correctly
187      * flagged for only including the inheritDoc tag.
188      */
189     @Test
190     public void testGoodOverrideFromOther() throws Exception {
191         final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
192         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
193 
194         verify(checkConfig, getPath("InputMissingOverrideGoodOverrideFromOther.java"), expected);
195     }
196 
197     /**
198      * This tests classes that are extending things explicitly will NOT be flagged while in
199      * Java 5 compatibility mode.
200      */
201     @Test
202     public void testGoodOverrideFromOtherJ5Compatible() throws Exception {
203         final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
204         checkConfig.addAttribute("javaFiveCompatibility", "true");
205 
206         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
207 
208         verify(checkConfig, getPath("InputMissingOverrideGoodOverrideFromOther.java"), expected);
209     }
210 
211     /**
212      * This tests anonymous inner classes that are overriding methods are correctly flagged
213      * for only including the inheritDoc tag.
214      */
215     @Test
216     public void testGoodAnnotationOverride() throws Exception {
217         final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
218         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
219 
220         verify(checkConfig, getPath("InputMissingOverrideGoodOverride.java"), expected);
221     }
222 
223     /**
224      * This tests anonymous inner classes that are overriding methods are NOT flagged while in
225      * Java 5 compatibility mode.
226      */
227     @Test
228     public void testGoodAnnotationOverrideJ5Compatible() throws Exception {
229         final DefaultConfiguration checkConfig = createModuleConfig(MissingOverrideCheck.class);
230         checkConfig.addAttribute("javaFiveCompatibility", "true");
231         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
232 
233         verify(checkConfig, getPath("InputMissingOverrideGoodOverride.java"), expected);
234     }
235 
236     @Test
237     public void testGetAcceptableTokens() {
238         final int[] expectedTokens = {TokenTypes.METHOD_DEF };
239         final MissingOverrideCheck check = new MissingOverrideCheck();
240         final int[] actual = check.getAcceptableTokens();
241         assertEquals("Invalid acceptable token size", 1, actual.length);
242         Assert.assertArrayEquals("Default required tokens are invalid", expectedTokens, actual);
243     }
244 
245 }