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             "34:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
199             "41:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
200             "49:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
201             "57:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
202             "64:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
203             "74:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
204             // Until https://github.com/checkstyle/checkstyle/issues/11425
205             "82:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
206             "93:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
207             "103:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
208             "110:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
209         };
210 
211         verifyWithInlineConfigParser(
212             getPath("InputSummaryJavadocIncorrect3.java"), expected);
213     }
214 
215     @Test
216     public void testInlineDefaultConfiguration() throws Exception {
217         final String[] expected = {
218             "22:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
219             "26:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
220             "30:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
221             "40:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
222             "44:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
223             "56:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
224             "60:12: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
225         };
226 
227         verifyWithInlineConfigParser(
228                 getPath("InputSummaryJavadocInlineDefault.java"), expected);
229     }
230 
231     @Test
232     public void testInlineDefaultConfiguration2() throws Exception {
233         final String[] expected = {
234             "18:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
235             "22:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
236             "27:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
237             "37:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
238             "54:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
239             "58:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
240             "80:8: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
241         };
242 
243         verifyWithInlineConfigParser(
244                 getPath("InputSummaryJavadocInlineDefault2.java"), expected);
245     }
246 
247     @Test
248     public void testInlineReturn() throws Exception {
249         final String[] expected = {
250             "74:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
251             "90:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
252         };
253 
254         verifyWithInlineConfigParser(
255                 getPath("InputSummaryJavadocInlineReturn.java"), expected);
256     }
257 
258     @Test
259     public void testInlineReturn2() throws Exception {
260         final String[] expected = {
261             "15:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
262         };
263 
264         verifyWithInlineConfigParser(
265                 getPath("InputSummaryJavadocInlineReturn2.java"), expected);
266     }
267 
268     @Test
269     public void testInlineReturnForbidden() throws Exception {
270         final String[] expected = {
271             "14:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
272             "21:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
273             "28:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
274         };
275 
276         verifyWithInlineConfigParser(
277                 getPath("InputSummaryJavadocInlineReturnForbidden.java"), expected);
278     }
279 
280     @Test
281     public void testPeriodAtEnd() throws Exception {
282         final String[] expected = {
283             "19:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
284             "26:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
285             "33:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
286             "40:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
287             "60:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
288             "70:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
289         };
290 
291         verifyWithInlineConfigParser(
292                 getPath("InputSummaryJavadocPeriodAtEnd.java"), expected);
293     }
294 
295     @Test
296     public void testForbiddenFragmentRelativeToPeriod() throws Exception {
297         final String[] expected = {
298             "23:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
299         };
300 
301         verifyWithInlineConfigParser(
302                 getPath("InputSummaryJavadocForbiddenFragmentRelativeToPeriod.java"), expected);
303     }
304 
305     @Test
306     public void testJapanesePeriod() throws Exception {
307         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
308 
309         verifyWithInlineConfigParser(
310                 getPath("InputSummaryJavadocJapanesePeriod.java"), expected);
311     }
312 
313     @Test
314     public void testHtmlFormatSummary() throws Exception {
315         final String[] expected = {
316             "22:17: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
317             "36:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
318             "41:11: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
319         };
320 
321         verifyWithInlineConfigParser(
322                 getPath("InputSummaryJavadocHtmlFormat.java"), expected);
323     }
324 
325     @Test
326     public void testPackageInfo() throws Exception {
327         final String[] expected = {
328             "10:4: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
329         };
330 
331         verifyWithInlineConfigParser(
332                 getPath("package-info.java"), expected);
333     }
334 
335     @Test
336     public void testPackageInfoWithAnnotation() throws Exception {
337         final String[] expected = {
338             "10:4: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
339         };
340 
341         verifyWithInlineConfigParser(
342                 getPath("inputs/package-info.java"), expected);
343     }
344 
345     @Test
346     public void testForbidden() throws Exception {
347         final String[] expected = {
348             "14:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
349         };
350 
351         verifyWithInlineConfigParser(
352                 getPath("InputSummaryJavadocTestForbiddenFragments.java"), expected);
353     }
354 
355     @Test
356     public void testEmptyPeriod() throws Exception {
357         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
358 
359         verifyWithInlineConfigParser(
360                 getPath("InputSummaryJavadocEmptyPeriod.java"), expected);
361     }
362 
363     @Test
364     public void testForbidden3() throws Exception {
365         final String[] expected = {
366             "14:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
367         };
368 
369         verifyWithInlineConfigParser(
370                 getPath("InputSummaryJavadocTestForbiddenFragments3.java"), expected);
371     }
372 
373     @Test
374     public void testForbidden2() throws Exception {
375         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
376 
377         verifyWithInlineConfigParser(
378                 getPath("InputSummaryJavadocTestForbiddenFragments2.java"), expected);
379     }
380 
381     @Test
382     public void testSummaryJavaDoc() throws Exception {
383         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
384 
385         verifyWithInlineConfigParser(
386                 getPath("InputSummaryJavadoc1.java"), expected);
387     }
388 
389     @Test
390     public void testSummaryJavaDoc2() throws Exception {
391         final String[] expected = {
392             "15:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
393         };
394 
395         verifyWithInlineConfigParser(
396                 getPath("InputSummaryJavadoc2.java"), expected);
397     }
398 
399     @Test
400     public void testInheritDoc() throws Exception {
401         final String[] expected = {
402             "14:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
403         };
404 
405         verifyWithInlineConfigParser(
406                 getPath("InputSummaryJavadocInheritDoc.java"), expected);
407     }
408 
409     @Test
410     public void testSummaryJavadocLargeJavaDoc() throws Exception {
411         final String[] expected = {
412             "13:4: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
413             "27:8: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
414             "41:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
415             "61:8: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
416         };
417 
418         verifyWithInlineConfigParser(
419                 getPath("InputSummaryJavadocLargeJavadoc.java"), expected);
420     }
421 
422     @Test
423     public void testForbiddenFragmentsTabFormatted() throws Exception {
424         final String[] expected = {
425             "15:12: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
426         };
427 
428         verifyWithInlineConfigParser(
429                 getPath("InputSummaryJavadocForbiddenFragmentsTabFormatted.java"), expected);
430     }
431 }