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.javadoc;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_FIRST_SENTENCE;
24  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_JAVADOC;
25  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_JAVADOC_MISSING;
26  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_MISSING_PERIOD;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33  
34  public class SummaryJavadocCheckTest extends AbstractModuleTestSupport {
35  
36      @Override
37      public String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/javadoc/summaryjavadoc";
39      }
40  
41      @Test
42      public void testGetRequiredTokens() {
43          final SummaryJavadocCheck checkObj = new SummaryJavadocCheck();
44          final int[] expected = {TokenTypes.BLOCK_COMMENT_BEGIN };
45          assertWithMessage("Default required tokens are invalid")
46                  .that(checkObj.getRequiredTokens())
47                  .isEqualTo(expected);
48      }
49  
50      @Test
51      public void testCorrect() throws Exception {
52          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
53  
54          verifyWithInlineConfigParser(
55                  getPath("InputSummaryJavadocCorrect.java"), expected);
56      }
57  
58      @Test
59      public void testCorrect2() throws Exception {
60          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
61  
62          verifyWithInlineConfigParser(
63                  getPath("InputSummaryJavadocCorrect2.java"), expected);
64      }
65  
66      @Test
67      public void testInlineCorrect() throws Exception {
68          final String[] expected = {
69              "112:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
70          };
71  
72          verifyWithInlineConfigParser(
73                  getPath("InputSummaryJavadocInlineCorrect.java"), expected);
74      }
75  
76      @Test
77      public void testInlineCorrectTwo() throws Exception {
78          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
79  
80          verifyWithInlineConfigParser(
81                  getPath("InputSummaryJavadocInlineCorrect2.java"), expected);
82      }
83  
84      @Test
85      public void testIncorrect() throws Exception {
86          final String[] expected = {
87              "20:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
88              "25:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
89              "43:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
90              "48:13: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
91              "58:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
92              "64:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
93              "69:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
94              "80:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
95          };
96          verifyWithInlineConfigParser(
97                  getPath("InputSummaryJavadocIncorrect.java"), expected);
98      }
99  
100     @Test
101     public void testIncorrect2() throws Exception {
102         final String[] expected = {
103             "20:9: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
104             "40:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
105             "53:13: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
106             "58:13: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
107             "63:13: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
108             "69:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
109             "74:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
110             "77:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
111         };
112         verifyWithInlineConfigParser(
113                 getPath("InputSummaryJavadocIncorrect4.java"), expected);
114     }
115 
116     @Test
117     public void testInlineForbidden() throws Exception {
118         final String[] expected = {
119             "26:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
120             "31:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
121             "36:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
122             "41:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
123             "45:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
124             "49:12: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
125             "59:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
126         };
127         verifyWithInlineConfigParser(
128                 getPath("InputSummaryJavadocInlineForbidden.java"), expected);
129     }
130 
131     @Test
132     public void testInlineForbidden2() throws Exception {
133         final String[] expected = {
134             "20:20: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
135             "34:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
136             "48:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
137             "54:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
138         };
139         verifyWithInlineConfigParser(
140                 getPath("InputSummaryJavadocInlineForbidden2.java"), expected);
141     }
142 
143     @Test
144     public void testPeriod() throws Exception {
145         final String[] expected = {
146             "14:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
147             "19:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
148             "37:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
149         };
150 
151         verifyWithInlineConfigParser(
152                 getPath("InputSummaryJavadocPeriod.java"), expected);
153     }
154 
155     @Test
156     public void testNoPeriod() throws Exception {
157         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
158 
159         verifyWithInlineConfigParser(
160                 getPath("InputSummaryJavadocNoPeriod.java"), expected);
161     }
162 
163     @Test
164     public void testDefaultConfiguration() throws Exception {
165         final String[] expected = {
166             "19:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
167             "24:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
168             "42:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
169             "47:13: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
170             "63:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
171             "68:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
172             "79:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
173         };
174 
175         verifyWithInlineConfigParser(
176                 getPath("InputSummaryJavadocIncorrect2.java"), expected);
177     }
178 
179     @Test
180     public void testDefaultConfiguration2() throws Exception {
181         final String[] expected = {
182             "38:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
183             "51:13: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
184             "56:13: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
185             "61:13: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
186             "67:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
187             "72:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
188             "75:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
189         };
190 
191         verifyWithInlineConfigParser(
192                 getPath("InputSummaryJavadocIncorrect5.java"), expected);
193     }
194 
195     @Test
196     public void testIncorrectUsageOfSummaryTag() throws Exception {
197         final String[] expected = {
198             "35:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
199             "41:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
200             "49:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
201             "58:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
202             "64:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
203             "75:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
204             "81:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
205             "93:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
206             "103:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
207             "109:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
208         };
209 
210         verifyWithInlineConfigParser(
211             getPath("InputSummaryJavadocIncorrect3.java"), expected);
212     }
213 
214     @Test
215     public void testInlineDefaultConfiguration() throws Exception {
216         final String[] expected = {
217             "22:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
218             "26:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
219             "30:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
220             "40:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
221             "44:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
222             "56:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
223             "60:12: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
224         };
225 
226         verifyWithInlineConfigParser(
227                 getPath("InputSummaryJavadocInlineDefault.java"), expected);
228     }
229 
230     @Test
231     public void testInlineDefaultConfiguration2() throws Exception {
232         final String[] expected = {
233             "18:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
234             "22:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
235             "27:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
236             "37:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
237             "54:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
238             "58:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
239             "80:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
240         };
241 
242         verifyWithInlineConfigParser(
243                 getPath("InputSummaryJavadocInlineDefault2.java"), expected);
244     }
245 
246     @Test
247     public void testInlineReturn() throws Exception {
248         final String[] expected = {
249             "74:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
250             "90:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
251         };
252 
253         verifyWithInlineConfigParser(
254                 getPath("InputSummaryJavadocInlineReturn.java"), expected);
255     }
256 
257     @Test
258     public void testInlineReturn2() throws Exception {
259         final String[] expected = {
260             "15:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
261         };
262 
263         verifyWithInlineConfigParser(
264                 getPath("InputSummaryJavadocInlineReturn2.java"), expected);
265     }
266 
267     @Test
268     public void testInlineReturnForbidden() throws Exception {
269         final String[] expected = {
270             "14:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
271             "21:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
272             "28:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
273         };
274 
275         verifyWithInlineConfigParser(
276                 getPath("InputSummaryJavadocInlineReturnForbidden.java"), expected);
277     }
278 
279     @Test
280     public void testPeriodAtEnd() throws Exception {
281         final String[] expected = {
282             "19:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
283             "26:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
284             "33:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
285             "40:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
286             "60:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
287             "70:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
288         };
289 
290         verifyWithInlineConfigParser(
291                 getPath("InputSummaryJavadocPeriodAtEnd.java"), expected);
292     }
293 
294     @Test
295     public void testForbiddenFragmentRelativeToPeriod() throws Exception {
296         final String[] expected = {
297             "23:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
298         };
299 
300         verifyWithInlineConfigParser(
301                 getPath("InputSummaryJavadocForbiddenFragmentRelativeToPeriod.java"), expected);
302     }
303 
304     @Test
305     public void testJapanesePeriod() throws Exception {
306         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
307 
308         verifyWithInlineConfigParser(
309                 getPath("InputSummaryJavadocJapanesePeriod.java"), expected);
310     }
311 
312     @Test
313     public void testHtmlFormatSummary() throws Exception {
314         final String[] expected = {
315             "22:17: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
316             "36:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
317             "41:11: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
318         };
319 
320         verifyWithInlineConfigParser(
321                 getPath("InputSummaryJavadocHtmlFormat.java"), expected);
322     }
323 
324     @Test
325     public void testPackageInfo() throws Exception {
326         final String[] expected = {
327             "10:4: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
328         };
329 
330         verifyWithInlineConfigParser(
331                 getPath("package-info.java"), expected);
332     }
333 
334     @Test
335     public void testPackageInfoWithAnnotation() throws Exception {
336         final String[] expected = {
337             "10:4: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
338         };
339 
340         verifyWithInlineConfigParser(
341                 getPath("inputs/package-info.java"), expected);
342     }
343 
344     @Test
345     public void testForbidden() throws Exception {
346         final String[] expected = {
347             "14:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
348         };
349 
350         verifyWithInlineConfigParser(
351                 getPath("InputSummaryJavadocTestForbiddenFragments.java"), expected);
352     }
353 
354     @Test
355     public void testEmptyPeriod() throws Exception {
356         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
357 
358         verifyWithInlineConfigParser(
359                 getPath("InputSummaryJavadocEmptyPeriod.java"), expected);
360     }
361 
362     @Test
363     public void testForbidden3() throws Exception {
364         final String[] expected = {
365             "14:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
366         };
367 
368         verifyWithInlineConfigParser(
369                 getPath("InputSummaryJavadocTestForbiddenFragments3.java"), expected);
370     }
371 
372     @Test
373     public void testForbidden2() throws Exception {
374         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
375 
376         verifyWithInlineConfigParser(
377                 getPath("InputSummaryJavadocTestForbiddenFragments2.java"), expected);
378     }
379 
380     @Test
381     public void testSummaryJavaDoc() throws Exception {
382         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
383 
384         verifyWithInlineConfigParser(
385                 getPath("InputSummaryJavadoc1.java"), expected);
386     }
387 
388     @Test
389     public void testSummaryJavaDoc2() throws Exception {
390         final String[] expected = {
391             "15:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
392         };
393 
394         verifyWithInlineConfigParser(
395                 getPath("InputSummaryJavadoc2.java"), expected);
396     }
397 
398     @Test
399     public void testInheritDoc() throws Exception {
400         final String[] expected = {
401             "14:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
402         };
403 
404         verifyWithInlineConfigParser(
405                 getPath("InputSummaryJavadocInheritDoc.java"), expected);
406     }
407 
408     @Test
409     public void testSummaryJavadocLargeJavaDoc() throws Exception {
410         final String[] expected = {
411             "26:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
412             "40:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
413             "60:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
414         };
415 
416         verifyWithInlineConfigParser(
417                 getPath("InputSummaryJavadocLargeJavadoc.java"), expected);
418     }
419 
420     @Test
421     public void testForbiddenFragmentsTabFormatted() throws Exception {
422         final String[] expected = {
423             "15:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
424         };
425 
426         verifyWithInlineConfigParser(
427                 getPath("InputSummaryJavadocForbiddenFragmentsTabFormatted.java"), expected);
428     }
429 
430     @Test
431     public void testNestedHtmlSummary() throws Exception {
432         final String[] expected = {
433             "38:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
434             "44:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
435             "50:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
436             "56:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
437             "94:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
438         };
439 
440         verifyWithInlineConfigParser(
441                 getPath("InputSummaryJavadocNestedHtml.java"), expected);
442     }
443 
444     @Test
445     public void testFirstSentenceSpansHtmlTags() throws Exception {
446         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
447 
448         verifyWithInlineConfigParser(
449                 getPath("InputSummaryJavadocFirstSentenceSpansHtmlTags.java"), expected);
450     }
451 
452     @Test
453     public void testAsteriskBannerJavadoc() throws Exception {
454         final String[] expected = {
455             "14:9: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
456             "20:9: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
457             "27:9: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
458         };
459 
460         verifyWithInlineConfigParser(
461                 getPath("InputSummaryJavadocAsteriskBanner.java"), expected);
462     }
463 
464     @Test
465     public void testInlineReturn3() throws Exception {
466         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
467         verifyWithInlineConfigParser(
468                 getPath("InputSummaryJavadocInlineReturn3.java"), expected);
469     }
470 
471 }