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.MissingDeprecatedCheck.MSG_KEY_ANNOTATION_MISSING_DEPRECATED;
23  import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingDeprecatedCheck.MSG_KEY_JAVADOC_DUPLICATE_TAG;
24  import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingDeprecatedCheck.MSG_KEY_JAVADOC_MISSING;
25  import static org.junit.Assert.assertArrayEquals;
26  
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 MissingDeprecatedCheckTest extends AbstractModuleTestSupport {
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/annotation/missingdeprecated";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final MissingDeprecatedCheck checkObj = new MissingDeprecatedCheck();
43          final int[] expected = {
44              TokenTypes.INTERFACE_DEF,
45              TokenTypes.CLASS_DEF,
46              TokenTypes.ANNOTATION_DEF,
47              TokenTypes.ENUM_DEF,
48              TokenTypes.METHOD_DEF,
49              TokenTypes.CTOR_DEF,
50              TokenTypes.VARIABLE_DEF,
51              TokenTypes.ENUM_CONSTANT_DEF,
52              TokenTypes.ANNOTATION_FIELD_DEF,
53          };
54          assertArrayEquals("Default required tokens are invalid",
55              expected, checkObj.getRequiredTokens());
56      }
57  
58      /**
59       * Tests that members that are only deprecated via javadoc are flagged.
60       */
61      @Test
62      public void testBadDeprecatedAnnotation() throws Exception {
63          final DefaultConfiguration checkConfig = createModuleConfig(MissingDeprecatedCheck.class);
64  
65          final String[] expected = {
66              "7: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
67              "12: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
68              "19: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
69              "26: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
70              "31: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
71              "38: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
72              "43: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
73              "51: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
74              "56: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
75          };
76  
77          verify(checkConfig, getPath("InputMissingDeprecatedBadDeprecated.java"), expected);
78      }
79  
80      /**
81       * Tests that members that are only deprecated via the annotation are flagged.
82       */
83      @Test
84      public void testBadDeprecatedJavadoc() throws Exception {
85  
86          final DefaultConfiguration checkConfig = createModuleConfig(MissingDeprecatedCheck.class);
87  
88          final String[] expected = {
89              "5: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
90              "11: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
91              "16: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
92              "23: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
93              "29: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
94              "38: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
95              "40: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
96              "48: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
97              "55: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
98          };
99  
100         verify(checkConfig, getPath("InputMissingDeprecatedBadJavadoc.java"), expected);
101     }
102 
103     /**
104      * Tests various special deprecation conditions such as duplicate or empty tags.
105      */
106     @Test
107     public void testSpecialCaseDeprecated() throws Exception {
108 
109         final DefaultConfiguration checkConfig = createModuleConfig(MissingDeprecatedCheck.class);
110 
111         final String[] expected = {
112             "5: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
113             "12: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
114             "14: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
115             "17: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
116             "19: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
117             "24: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
118             "32: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
119             "33: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
120             "33: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
121             "42: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
122             "42: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
123             "50: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
124             "51: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
125         };
126 
127         verify(checkConfig, getPath("InputMissingDeprecatedSpecialCase.java"), expected);
128     }
129 
130     /**
131      * Tests that good forms of deprecation are not flagged.
132      */
133     @Test
134     public void testGoodDeprecated() throws Exception {
135 
136         final DefaultConfiguration checkConfig = createModuleConfig(MissingDeprecatedCheck.class);
137 
138         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
139 
140         verify(checkConfig, getPath("InputMissingDeprecatedGood.java"), expected);
141     }
142 
143     @Test
144     public void testTwoInJavadocWithoutAnnotation() throws Exception {
145 
146         final DefaultConfiguration checkConfig = createModuleConfig(MissingDeprecatedCheck.class);
147 
148         final String[] expected = {
149             "7: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
150             "8: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
151             "12: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
152         };
153 
154         verify(checkConfig, getPath("InputMissingDeprecatedClass.java"), expected);
155     }
156 
157     @Test
158     public void testEmptyJavadocLine() throws Exception {
159 
160         final DefaultConfiguration checkConfig = createModuleConfig(MissingDeprecatedCheck.class);
161 
162         final String[] expected = {
163             "7: " + getCheckMessage(MSG_KEY_JAVADOC_MISSING),
164             "11: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
165         };
166 
167         verify(checkConfig, getPath("InputMissingDeprecatedMethod.java"), expected);
168     }
169 
170     @Test
171     public void testSkipNoJavadocOption() throws Exception {
172         final DefaultConfiguration checkConfig = createModuleConfig(MissingDeprecatedCheck.class);
173         checkConfig.addAttribute("skipNoJavadoc", "true");
174 
175         final String[] expected = {
176             "10: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
177             "26: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
178         };
179 
180         verify(checkConfig, getPath("InputMissingDeprecatedSkipNoJavadoc.java"), expected);
181     }
182 }