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