View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.JavadocCommentsTokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class MissingDeprecatedCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      public 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              JavadocCommentsTokenTypes.JAVADOC_CONTENT,
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              JavadocCommentsTokenTypes.JAVADOC_CONTENT,
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              "29: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
93              "38: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
94              "48: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
95              "55: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
96          };
97  
98          verifyWithInlineConfigParser(
99                  getPath("InputMissingDeprecatedBadJavadoc.java"), expected);
100     }
101 
102     /**
103      * Tests various special deprecation conditions such as duplicate or empty tags.
104      */
105     @Test
106     public void testSpecialCaseDeprecated() throws Exception {
107 
108         final String[] expected = {
109             "13: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
110             "21: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
111             "23: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
112             "28: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
113             "41: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
114             "50: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
115             "59: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
116             "95: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
117             "102: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
118             "109: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
119         };
120 
121         verifyWithInlineConfigParser(
122                 getPath("InputMissingDeprecatedSpecialCase.java"), expected);
123     }
124 
125     /**
126      * Tests that good forms of deprecation are not flagged.
127      */
128     @Test
129     public void testGoodDeprecated() throws Exception {
130 
131         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
132 
133         verifyWithInlineConfigParser(
134                 getPath("InputMissingDeprecatedGood.java"), expected);
135     }
136 
137     @Test
138     public void testTwoInJavadocWithoutAnnotation() throws Exception {
139 
140         final String[] expected = {
141             "17: " + getCheckMessage(MSG_KEY_JAVADOC_DUPLICATE_TAG, "@deprecated"),
142             "21: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
143         };
144 
145         verifyWithInlineConfigParser(
146                 getPath("InputMissingDeprecatedClass.java"), expected);
147     }
148 
149     @Test
150     public void testEmptyJavadocLine() throws Exception {
151 
152         final String[] expected = {
153             "18: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
154         };
155 
156         verifyWithInlineConfigParser(
157                 getPath("InputMissingDeprecatedMethod.java"), expected);
158     }
159 
160     @Test
161     public void testPackageInfo() throws Exception {
162 
163         final String[] expected = {
164             "9: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
165         };
166 
167         verifyWithInlineConfigParser(
168                 getPath("package-info.java"), expected);
169     }
170 
171     @Test
172     public void testDepPackageInfoBelowComment() throws Exception {
173 
174         final String[] expected = {
175             "14: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
176         };
177 
178         verifyWithInlineConfigParser(
179                 getPath("InputMissingDeprecatedAbovePackage.java"), expected);
180     }
181 
182     @Test
183     public void testPackageInfoBelowComment() throws Exception {
184 
185         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
186 
187         verifyWithInlineConfigParser(
188                 getPath("InputMissingDeprecatedSingleComment.java"), expected);
189     }
190 
191     @Test
192     public void testModuleInfo() throws Exception {
193 
194         final String[] expected = {
195             "12: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
196         };
197 
198         verifyWithInlineConfigParser(
199                 getNonCompilablePath("module-info/expected/module-info.java"), expected);
200     }
201 
202 }