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.javadoc;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck.MSG_JAVADOC_MISSING;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class MissingJavadocTypeCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/javadoc/missingjavadoctype";
36      }
37  
38      @Test
39      public void testGetRequiredTokens() {
40          final MissingJavadocTypeCheck missingJavadocTypeCheck = new MissingJavadocTypeCheck();
41          assertWithMessage(
42                  "MissingJavadocTypeCheck#getRequiredTokens should return empty array by default")
43                          .that(missingJavadocTypeCheck.getRequiredTokens())
44                          .isEmpty();
45      }
46  
47      @Test
48      public void testGetAcceptableTokens() {
49          final MissingJavadocTypeCheck missingJavadocTypeCheck = new MissingJavadocTypeCheck();
50  
51          final int[] actual = missingJavadocTypeCheck.getAcceptableTokens();
52          final int[] expected = {
53              TokenTypes.INTERFACE_DEF,
54              TokenTypes.CLASS_DEF,
55              TokenTypes.ENUM_DEF,
56              TokenTypes.ANNOTATION_DEF,
57              TokenTypes.RECORD_DEF,
58          };
59  
60          assertWithMessage("Default acceptable tokens are invalid")
61                  .that(actual)
62                  .isEqualTo(expected);
63      }
64  
65      @Test
66      public void testTagsOne() throws Exception {
67          final String[] expected = {
68              "14:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
69              "44:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
70              "69:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
71          };
72          verifyWithInlineConfigParser(
73                  getPath("InputMissingJavadocTypeTagsOne.java"), expected);
74      }
75  
76      @Test
77      public void testTagsTwo() throws Exception {
78          final String[] expected = {
79              "20:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
80          };
81          verifyWithInlineConfigParser(
82                  getPath("InputMissingJavadocTypeTagsTwo.java"), expected);
83      }
84  
85      @Test
86      public void testTagsThree() throws Exception {
87          final String[] expected = {
88              "20:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
89          };
90          verifyWithInlineConfigParser(
91                  getPath("InputMissingJavadocTypeTagsThree.java"), expected);
92      }
93  
94      @Test
95      public void testTagsFour() throws Exception {
96          final String[] expected = {
97              "20:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
98          };
99          verifyWithInlineConfigParser(
100                 getPath("InputMissingJavadocTypeTagsFour.java"), expected);
101     }
102 
103     @Test
104     public void testInner() throws Exception {
105         final String[] expected = {
106             "19:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
107             "26:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
108             "32:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
109         };
110         verifyWithInlineConfigParser(
111                 getPath("InputMissingJavadocTypeInner.java"), expected);
112     }
113 
114     @Test
115     public void testStrict() throws Exception {
116         final String[] expected = {
117             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
118             "15:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
119             "20:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
120             "40:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
121         };
122         verifyWithInlineConfigParser(
123                 getPath("InputMissingJavadocTypePublicOnly1.java"), expected);
124     }
125 
126     @Test
127     public void testProtected() throws Exception {
128         final String[] expected = {
129             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
130         };
131         verifyWithInlineConfigParser(
132                 getPath("InputMissingJavadocTypePublicOnly2.java"), expected);
133     }
134 
135     @Test
136     public void testPublic() throws Exception {
137         final String[] expected = {
138             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
139             "44:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
140         };
141         verifyWithInlineConfigParser(
142                 getPath("InputMissingJavadocTypeScopeInnerInterfaces1.java"),
143                expected);
144     }
145 
146     @Test
147     public void testProtest() throws Exception {
148         final String[] expected = {
149             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
150             "35:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
151             "44:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
152             "71:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
153         };
154         verifyWithInlineConfigParser(
155                 getPath("InputMissingJavadocTypeScopeInnerInterfaces2.java"),
156                expected);
157     }
158 
159     @Test
160     public void testPkg() throws Exception {
161         final String[] expected = {
162             "22:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
163             "24:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
164             "26:13: " + getCheckMessage(MSG_JAVADOC_MISSING),
165         };
166         verifyWithInlineConfigParser(
167                 getPath("InputMissingJavadocTypeScopeInnerClasses1.java"), expected);
168     }
169 
170     @Test
171     public void testEclipse() throws Exception {
172         final String[] expected = {
173             "22:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
174         };
175         verifyWithInlineConfigParser(
176                 getPath("InputMissingJavadocTypeScopeInnerClasses2.java"), expected);
177     }
178 
179     @Test
180     public void testScopesOne() throws Exception {
181         final String[] expected = {
182             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
183             "25:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
184             "37:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
185             "49:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
186         };
187         verifyWithInlineConfigParser(
188                 getPath("InputMissingJavadocTypeNoJavadoc1One.java"),
189                expected);
190     }
191 
192     @Test
193     public void testScopesTwo() throws Exception {
194         final String[] expected = {
195             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
196             "15:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
197             "26:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
198             "38:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
199             "50:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
200             "62:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
201             "74:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
202         };
203         verifyWithInlineConfigParser(
204                 getPath("InputMissingJavadocTypeNoJavadoc1Two.java"),
205                expected);
206     }
207 
208     @Test
209     public void testLimitViolationsBySpecifyingTokens() throws Exception {
210         final String[] expected = {
211             "15:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
212         };
213         verifyWithInlineConfigParser(
214                 getPath("InputMissingJavadocTypeNoJavadocOnInterface.java"),
215                expected);
216     }
217 
218     @Test
219     public void testScopes2() throws Exception {
220         final String[] expected = {
221             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
222             "25:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
223         };
224         verifyWithInlineConfigParser(
225                 getPath("InputMissingJavadocTypeNoJavadoc2.java"),
226                expected);
227     }
228 
229     @Test
230     public void testExcludeScope() throws Exception {
231         final String[] expected = {
232             "37:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
233             "49:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
234             "62:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
235             "73:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
236             "85:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
237             "97:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
238             "109:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
239             "121:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
240         };
241         verifyWithInlineConfigParser(
242                 getPath("InputMissingJavadocTypeNoJavadoc3.java"),
243                expected);
244     }
245 
246     @Test
247     public void testDontAllowUnusedParameterTag() throws Exception {
248         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
249         verifyWithInlineConfigParser(
250                 getPath("InputMissingJavadocTypeUnusedParamInJavadocForClass.java"),
251                 expected);
252     }
253 
254     @Test
255     public void testSkipAnnotationsDefault() throws Exception {
256 
257         final String[] expected = {
258             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
259             "17:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
260             "21:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
261         };
262         verifyWithInlineConfigParser(
263                 getPath("InputMissingJavadocTypeSkipAnnotations1.java"),
264             expected);
265     }
266 
267     @Test
268     public void testSkipAnnotationsWithFullyQualifiedName() throws Exception {
269         final String[] expected = {
270             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
271             "17:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
272             "21:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
273         };
274         verifyWithInlineConfigParser(
275                 getPath("InputMissingJavadocTypeSkipAnnotations2.java"),
276                 expected);
277     }
278 
279     @Test
280     public void testSkipAnnotationsAllowed() throws Exception {
281 
282         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
283         verifyWithInlineConfigParser(
284                 getPath("InputMissingJavadocTypeSkipAnnotations3.java"),
285             expected);
286     }
287 
288     @Test
289     public void testSkipAnnotationsNotAllowed() throws Exception {
290 
291         final String[] expected = {
292             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
293             "17:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
294             "21:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
295         };
296         verifyWithInlineConfigParser(
297                 getPath("InputMissingJavadocTypeSkipAnnotations4.java"),
298             expected);
299     }
300 
301     @Test
302     public void testMissingJavadocTypeCheckRecords() throws Exception {
303 
304         final String[] expected = {
305             "14:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
306             "15:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
307             "19:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
308             "23:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
309             "31:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
310             "32:13: " + getCheckMessage(MSG_JAVADOC_MISSING),
311             "41:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
312         };
313         verifyWithInlineConfigParser(
314                 getNonCompilablePath("InputMissingJavadocTypeRecords.java"),
315             expected);
316     }
317 
318     @Test
319     public void testInterfaceMemberScopeIsPublic() throws Exception {
320 
321         final String[] expected = {
322             "13:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
323             "15:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
324             "19:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
325         };
326         verifyWithInlineConfigParser(
327                 getPath("InputMissingJavadocTypeInterfaceMemberScopeIsPublic.java"),
328             expected);
329     }
330 
331     @Test
332     public void testQualifiedAnnotation1() throws Exception {
333         final String[] expected = {
334             "16:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
335             "20:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
336             "23:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
337         };
338         verifyWithInlineConfigParser(
339             getPath("InputMissingJavadocTypeQualifiedAnnotation1.java"), expected);
340     }
341 
342     @Test
343     public void testQualifiedAnnotation2() throws Exception {
344         final String[] expected = {
345             "20:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
346             "23:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
347         };
348         verifyWithInlineConfigParser(
349             getPath("InputMissingJavadocTypeQualifiedAnnotation2.java"), expected);
350     }
351 
352     @Test
353     public void testQualifiedAnnotation3() throws Exception {
354         final String[] expected = {
355             "16:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
356             "22:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
357         };
358         verifyWithInlineConfigParser(
359             getPath("InputMissingJavadocTypeQualifiedAnnotation3.java"), expected);
360     }
361 
362     @Test
363     public void testQualifiedAnnotation4() throws Exception {
364         final String[] expected = {
365             "17:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
366             "21:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
367         };
368         verifyWithInlineConfigParser(
369             getPath("InputMissingJavadocTypeQualifiedAnnotation4.java"), expected);
370     }
371 
372     @Test
373     public void testQualifiedAnnotation5() throws Exception {
374         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
375         verifyWithInlineConfigParser(
376             getPath("InputMissingJavadocTypeQualifiedAnnotation5.java"), expected);
377     }
378 
379     @Test
380     public void testMultipleQualifiedAnnotation() throws Exception {
381         final String[] expected = {
382             "29:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
383             "38:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
384         };
385         verifyWithInlineConfigParser(
386             getPath("InputMissingJavadocTypeMultipleQualifiedAnnotation.java"), expected);
387     }
388 
389     @Test
390     public void testQualifiedAnnotationWithParameters() throws Exception {
391         final String[] expected = {
392             "33:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
393             "37:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
394             "42:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
395             "50:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
396         };
397         verifyWithInlineConfigParser(
398             getPath("InputMissingJavadocTypeQualifiedAnnotationWithParameters.java"),
399             expected);
400     }
401 
402 }