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