View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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      protected 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 testInlineCorrect() throws Exception {
60          final String[] expected = {
61              "112: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
62          };
63  
64          verifyWithInlineConfigParser(
65                  getPath("InputSummaryJavadocInlineCorrect.java"), expected);
66      }
67  
68      @Test
69      public void testIncorrect() throws Exception {
70          final String[] expected = {
71              "20: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
72              "25: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
73              "43: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
74              "48: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
75              "58: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
76              "64: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
77              "69: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
78              "80: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
79              "94: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
80              "114: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
81              "127: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
82              "132: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
83              "137: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
84              "143: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
85              "148: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
86              "151: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
87          };
88          verifyWithInlineConfigParser(
89                  getPath("InputSummaryJavadocIncorrect.java"), expected);
90      }
91  
92      @Test
93      public void testInlineForbidden() throws Exception {
94          final String[] expected = {
95              "26: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
96              "31: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
97              "36: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
98              "41: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
99              "45: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
100             "49: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
101             "59: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
102             "80: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
103             "94: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
104             "108: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
105             "114: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
106         };
107         verifyWithInlineConfigParser(
108                 getPath("InputSummaryJavadocInlineForbidden.java"), expected);
109     }
110 
111     @Test
112     public void testPeriod() throws Exception {
113         final String[] expected = {
114             "14: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
115             "19: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
116             "37: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
117         };
118 
119         verifyWithInlineConfigParser(
120                 getPath("InputSummaryJavadocPeriod.java"), expected);
121     }
122 
123     @Test
124     public void testNoPeriod() throws Exception {
125         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
126 
127         verifyWithInlineConfigParser(
128                 getPath("InputSummaryJavadocNoPeriod.java"), expected);
129     }
130 
131     @Test
132     public void testDefaultConfiguration() throws Exception {
133         final String[] expected = {
134             "19: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
135             "24: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
136             "42: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
137             "47: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
138             "63: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
139             "68: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
140             "79: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
141             "113: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
142             "126: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
143             "131: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
144             "136: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
145             "142: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
146             "147: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
147             "150: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
148         };
149 
150         verifyWithInlineConfigParser(
151                 getPath("InputSummaryJavadocIncorrect2.java"), expected);
152     }
153 
154     @Test
155     public void testIncorrectUsageOfSummaryTag() throws Exception {
156         final String[] expected = {
157             "34: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
158             "41: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
159             "49: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
160             "57: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
161             "64: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
162             "74: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
163             // Until https://github.com/checkstyle/checkstyle/issues/11425
164             "82: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
165             "93: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
166             "103: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
167             "110: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
168         };
169 
170         verifyWithInlineConfigParser(
171             getPath("InputSummaryJavadocIncorrect3.java"), expected);
172     }
173 
174     @Test
175     public void testInlineDefaultConfiguration() throws Exception {
176         final String[] expected = {
177             "22: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
178             "26: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
179             "30: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
180             "40: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
181             "44: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
182             "56: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
183             "60: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
184             "116: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
185             "120: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
186             "125: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
187             "136: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
188             "153: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
189             "157: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
190             "179: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
191         };
192 
193         verifyWithInlineConfigParser(
194                 getPath("InputSummaryJavadocInlineDefault.java"), expected);
195     }
196 
197     @Test
198     public void testInlineReturn() throws Exception {
199         final String[] expected = {
200             "74: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
201         };
202 
203         verifyWithInlineConfigParser(
204                 getPath("InputSummaryJavadocInlineReturn.java"), expected);
205     }
206 
207     @Test
208     public void testInlineReturn2() throws Exception {
209         final String[] expected = {
210             "15: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
211         };
212 
213         verifyWithInlineConfigParser(
214                 getPath("InputSummaryJavadocInlineReturn2.java"), expected);
215     }
216 
217     @Test
218     public void testInlineReturnForbidden() throws Exception {
219         final String[] expected = {
220             "14: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
221             "21: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
222             "28: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
223         };
224 
225         verifyWithInlineConfigParser(
226                 getPath("InputSummaryJavadocInlineReturnForbidden.java"), expected);
227     }
228 
229     @Test
230     public void testPeriodAtEnd() throws Exception {
231         final String[] expected = {
232             "19: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
233             "26: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
234             "33: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
235             "40: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
236             "60: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
237             "70: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
238         };
239 
240         verifyWithInlineConfigParser(
241                 getPath("InputSummaryJavadocPeriodAtEnd.java"), expected);
242     }
243 
244     @Test
245     public void testForbiddenFragmentRelativeToPeriod() throws Exception {
246         final String[] expected = {
247             "23: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
248         };
249 
250         verifyWithInlineConfigParser(
251                 getPath("InputSummaryJavadocForbiddenFragmentRelativeToPeriod.java"), expected);
252     }
253 
254     @Test
255     public void testJapanesePeriod() throws Exception {
256         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
257 
258         verifyWithInlineConfigParser(
259                 getPath("InputSummaryJavadocJapanesePeriod.java"), expected);
260     }
261 
262     @Test
263     public void testHtmlFormatSummary() throws Exception {
264         final String[] expected = {
265             "22: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
266             "36: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
267             "41: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
268         };
269 
270         verifyWithInlineConfigParser(
271                 getPath("InputSummaryJavadocHtmlFormat.java"), expected);
272     }
273 
274     @Test
275     public void testPackageInfo() throws Exception {
276         final String[] expected = {
277             "10: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
278         };
279 
280         verifyWithInlineConfigParser(
281                 getPath("package-info.java"), expected);
282     }
283 
284     @Test
285     public void testPackageInfoWithAnnotation() throws Exception {
286         final String[] expected = {
287             "10: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
288         };
289 
290         verifyWithInlineConfigParser(
291                 getPath("inputs/package-info.java"), expected);
292     }
293 
294     @Test
295     public void testForbidden() throws Exception {
296         final String[] expected = {
297             "14: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
298         };
299 
300         verifyWithInlineConfigParser(
301                 getPath("InputSummaryJavadocTestForbiddenFragments.java"), expected);
302     }
303 
304     @Test
305     public void testEmptyPeriod() throws Exception {
306         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
307 
308         verifyWithInlineConfigParser(
309                 getPath("InputSummaryJavadocEmptyPeriod.java"), expected);
310     }
311 
312     @Test
313     public void testForbidden3() throws Exception {
314         final String[] expected = {
315             "14: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
316         };
317 
318         verifyWithInlineConfigParser(
319                 getPath("InputSummaryJavadocTestForbiddenFragments3.java"), expected);
320     }
321 
322     @Test
323     public void testForbidden2() throws Exception {
324         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
325 
326         verifyWithInlineConfigParser(
327                 getPath("InputSummaryJavadocTestForbiddenFragments2.java"), expected);
328     }
329 
330     @Test
331     public void testSummaryJavaDoc() throws Exception {
332         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
333 
334         verifyWithInlineConfigParser(
335                 getPath("InputSummaryJavadoc1.java"), expected);
336     }
337 
338     @Test
339     public void testSummaryJavaDoc2() throws Exception {
340         final String[] expected = {
341             "15: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
342         };
343 
344         verifyWithInlineConfigParser(
345                 getPath("InputSummaryJavadoc2.java"), expected);
346     }
347 
348     @Test
349     public void testInheritDoc() throws Exception {
350         final String[] expected = {
351             "14: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
352         };
353 
354         verifyWithInlineConfigParser(
355                 getPath("InputSummaryJavadocInheritDoc.java"), expected);
356     }
357 
358     @Test
359     public void testSummaryJavadocLargeJavaDoc() throws Exception {
360         final String[] expected = {
361             "13: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
362             "27: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
363             "41: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
364             "61: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
365         };
366 
367         verifyWithInlineConfigParser(
368                 getPath("InputSummaryJavadocLargeJavadoc.java"), expected);
369     }
370 }