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.MissingDeprecatedCheck.MSG_KEY_ANNOTATION_MISSING_DEPRECATED;
24  import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingDeprecatedCheck.MSG_KEY_JAVADOC_DUPLICATE_TAG;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class MissingDeprecatedCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/annotation/missingdeprecated";
37      }
38  
39      @Test
40      public void testGetDefaultJavadocTokens() {
41          final MissingDeprecatedCheck missingDeprecatedCheck =
42                  new MissingDeprecatedCheck();
43          final int[] expected = {
44              JavadocTokenTypes.JAVADOC,
45          };
46  
47          assertWithMessage("Default javadoc tokens are invalid")
48                  .that(missingDeprecatedCheck.getDefaultJavadocTokens())
49                  .isEqualTo(expected);
50      }
51  
52      @Test
53      public void testGetRequiredJavadocTokens() {
54          final MissingDeprecatedCheck checkObj = new MissingDeprecatedCheck();
55          final int[] expected = {
56              JavadocTokenTypes.JAVADOC,
57          };
58          assertWithMessage("Default required javadoc tokens are invalid")
59                  .that(checkObj.getRequiredJavadocTokens())
60                  .isEqualTo(expected);
61      }
62  
63      /**
64       * Tests that members that are only deprecated via javadoc are flagged.
65       */
66      @Test
67      public void testBadDeprecatedAnnotation() throws Exception {
68  
69          final String[] expected = {
70              "14: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
71              "19: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
72              "26: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
73              "33: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
74              "38: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
75              "45: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
76              "50: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
77              "58: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
78              "63: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
79          };
80  
81          verifyWithInlineConfigParser(
82                  getPath("InputMissingDeprecatedBadDeprecated.java"), expected);
83      }
84  
85      /**
86       * Tests that members that are only deprecated via the annotation are flagged.
87       */
88      @Test
89      public void testBadDeprecatedJavadoc() throws Exception {
90  
91          final String[] expected = {
92              "18: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
93              "36: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
94              "45: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
95              "55: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
96              "62: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
97          };
98  
99          verifyWithInlineConfigParser(
100                 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 String[] expected = {
110             "12: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
111             "19: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
112             "21: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
113             "26: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
114             "40: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
115             "49: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
116             "58: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
117             "99: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
118             "106: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
119             "113: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
120         };
121 
122         verifyWithInlineConfigParser(
123                 getPath("InputMissingDeprecatedSpecialCase.java"), expected);
124     }
125 
126     /**
127      * Tests that good forms of deprecation are not flagged.
128      */
129     @Test
130     public void testGoodDeprecated() throws Exception {
131 
132         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
133 
134         verifyWithInlineConfigParser(
135                 getPath("InputMissingDeprecatedGood.java"), expected);
136     }
137 
138     @Test
139     public void testTwoInJavadocWithoutAnnotation() throws Exception {
140 
141         final String[] expected = {
142             "15: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
143             "19: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
144         };
145 
146         verifyWithInlineConfigParser(
147                 getPath("InputMissingDeprecatedClass.java"), expected);
148     }
149 
150     @Test
151     public void testEmptyJavadocLine() throws Exception {
152 
153         final String[] expected = {
154             "18: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
155         };
156 
157         verifyWithInlineConfigParser(
158                 getPath("InputMissingDeprecatedMethod.java"), expected);
159     }
160 
161     @Test
162     public void testPackageInfo() throws Exception {
163 
164         final String[] expected = {
165             "9: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
166         };
167 
168         verifyWithInlineConfigParser(
169                 getPath("package-info.java"), expected);
170     }
171 
172     @Test
173     public void testDepPackageInfoBelowComment() throws Exception {
174 
175         final String[] expected = {
176             "14: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
177         };
178 
179         verifyWithInlineConfigParser(
180                 getPath("InputMissingDeprecatedAbovePackage.java"), expected);
181     }
182 
183     @Test
184     public void testPackageInfoBelowComment() throws Exception {
185 
186         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
187 
188         verifyWithInlineConfigParser(
189                 getPath("InputMissingDeprecatedSingleComment.java"), expected);
190     }
191 }